Skip to content

Commit

Permalink
removed all remaining Try variants -- always return the error where r…
Browse files Browse the repository at this point in the history
…elevant and use errors.Log1 etc to skip it if not wanted
  • Loading branch information
rcoreilly committed Aug 20, 2024
1 parent 250dd48 commit 40d67f5
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 115 deletions.
33 changes: 11 additions & 22 deletions actrf/actrfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package actrf

import (
"fmt"
"log"

"cogentcore.org/core/base/errors"
"cogentcore.org/core/tensor"
)

Expand All @@ -22,25 +22,15 @@ type RFs struct {
RFs []*RF
}

// RFByName returns RF of given name, nil if not found
func (af *RFs) RFByName(name string) *RF {
if af.NameMap == nil {
return nil
}
idx, ok := af.NameMap[name]
if ok {
return af.RFs[idx]
}
return nil
}

// RFByNameTry returns RF of given name, nil and error msg if not found
func (af *RFs) RFByNameTry(name string) (*RF, error) {
rf := af.RFByName(name)
if rf == nil {
return nil, fmt.Errorf("Name: %s not found in list of named RFs", name)
// RFByName returns RF of given name, nil and error msg if not found.
func (af *RFs) RFByName(name string) (*RF, error) {
if af.NameMap != nil {
idx, ok := af.NameMap[name]
if ok {
return af.RFs[idx], nil
}
}
return rf, nil
return nil, fmt.Errorf("Name: %s not found in list of named RFs", name)
}

// AddRF adds a new RF, calling Init on it using given act, src tensors
Expand All @@ -58,9 +48,8 @@ func (af *RFs) AddRF(name string, act, src tensor.Tensor) *RF {

// Add adds a new act sample to the accumulated data for given named rf
func (af *RFs) Add(name string, act, src tensor.Tensor, thr float32) error {
rf, err := af.RFByNameTry(name)
if err != nil {
log.Println(err)
rf, err := af.RFByName(name)
if errors.Log(err) != nil {
return err
}
rf.Add(act, src, thr)
Expand Down
2 changes: 1 addition & 1 deletion econfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var (

// Config is the overall config setting function, processing config files
// and command-line arguments, in the following order:
// - Apply any `def:` field tag default values.
// - Apply any `default:` field tag default values.
// - Look for `--config`, `--cfg`, or `-c` arg, specifying a config file on the command line.
// - Fall back on default config file name passed to `Config` function, if arg not found.
// - Read any `Include[s]` files in config file in deepest-first (natural) order,
Expand Down
2 changes: 1 addition & 1 deletion econfig/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package econfig

import "cogentcore.org/core/base/reflectx"

// SetFromDefaults sets Config values from field tag `def:` values.
// SetFromDefaults sets Config values from field tag `default:` values.
// Parsing errors are automatically logged.
func SetFromDefaults(cfg any) error {
return reflectx.SetFromDefaultTags(cfg)
Expand Down
2 changes: 1 addition & 1 deletion egui/grids.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (gui *GUI) ViewActRFs(atf *actrf.RFs) {
nm := rf.Name
tg := gui.Grid(nm)
if tg.Tensor == nil {
rf := atf.RFByName(nm)
rf, _ := atf.RFByName(nm)
tg.SetTensor(&rf.NormRF)
} else {
tg.NeedsRender()
Expand Down
2 changes: 1 addition & 1 deletion emer/netparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (pr *NetParams) SetAllSheet(sheetName string) error {
if err != nil {
return err
}
psheet, err := pr.Params.SheetByNameTry(sheetName)
psheet, err := pr.Params.SheetByName(sheetName)
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions env/ctrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ func (cs *Counters) ByScope(tm etime.Times) *Counter {
return cs.Counters[tm]
}

// ByTimeTry returns counter by timescale key -- returns nil, error if not found
func (cs *Counters) ByTimeTry(tm etime.Times) (*Counter, error) {
// ByTime returns counter by timescale key. returns nil, error if not found.
func (cs *Counters) ByTime(tm etime.Times) (*Counter, error) {
ct, ok := cs.Counters[tm]
if ok {
return ct, nil
}
err := fmt.Errorf("env.Counters: scope not found: %s", tm.String())
return nil, err
return nil, fmt.Errorf("env.Counters: scope not found: %s", tm.String())
}

// Init does Init on all the counters
Expand Down
2 changes: 1 addition & 1 deletion esg/conds.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (cd *Cond) Validate(rl *Rule, it *Item, rls *Rules) []error {
return []error{fmt.Errorf("Rule: %v Item: %v Rule Condition has empty Rule", rl.Name, it.String())}
}
if cd.Rule[0] != '\'' {
_, err := rls.RuleTry(cd.Rule)
_, err := rls.Rule(cd.Rule)
if err != nil {
return []error{err}
}
Expand Down
4 changes: 2 additions & 2 deletions esg/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (el *Elem) String() string {
func (el *Elem) Gen(rl *Rule, rls *Rules) {
switch el.El {
case RuleEl:
rl := rls.Rule(el.Value)
rl, _ := rls.Rule(el.Value)
rl.Gen(rls)
case TokenEl:
if rls.Trace {
Expand All @@ -128,7 +128,7 @@ func (el *Elem) Gen(rl *Rule, rls *Rules) {
func (el *Elem) Validate(it *Item, rl *Rule, rls *Rules) []error {
switch el.El {
case RuleEl:
_, err := rls.RuleTry(el.Value)
_, err := rls.Rule(el.Value)
if err != nil {
return []error{err}
}
Expand Down
15 changes: 5 additions & 10 deletions esg/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,13 @@ func (rls *Rules) Init() {
}
}

// Rule returns rule of given name (nil if not found)
func (rls *Rules) Rule(name string) *Rule {
return rls.Map[name]
}

// RuleTry returns rule of given name, and error if not found
func (rls *Rules) RuleTry(name string) (*Rule, error) {
// Rule returns rule of given name, and error if not found
func (rls *Rules) Rule(name string) (*Rule, error) {
rl, ok := rls.Map[name]
if !ok {
return nil, fmt.Errorf("Rule: %v not found in Rules: %v", name, rls.Name)
if ok {
return rl, nil
}
return rl, nil
return nil, fmt.Errorf("Rule: %v not found in Rules: %v", name, rls.Name)
}

// HasFired returns true if rule of given name has fired
Expand Down
6 changes: 5 additions & 1 deletion looper/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ type Manager struct {

// GetLoop returns the Loop associated with an evaluation mode and timescale.
func (man *Manager) GetLoop(modes etime.Modes, times etime.Times) *Loop {
return man.Stacks[modes].Loops[times]
st := man.Stacks[modes]
if st == nil {
return nil
}
return st.Loops[times]
}

// NewManager returns a new initialized manager
Expand Down
1 change: 1 addition & 0 deletions netview/netview.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func (nv *NetView) SetNet(net emer.Network) {
nv.DataMu.Unlock()
nv.UpdateTree() // need children
nv.UpdateLayers()
nv.Current()
}

// SetVar sets the variable to view and updates the display
Expand Down
2 changes: 1 addition & 1 deletion params/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (ps *Sets) DiffsFirst() string {
// DiffsWithin reports all the cases where the same param path is being set
// to different values within given sheet.
func (ps *Sets) DiffsWithin(sheetName string) string {
sht, err := ps.SheetByNameTry(sheetName)
sht, err := ps.SheetByName(sheetName)
if err != nil {
return err.Error()
}
Expand Down
19 changes: 6 additions & 13 deletions params/hypers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"bytes"
"encoding/json"
"fmt"
"log"

"cogentcore.org/core/base/errors"
)

// HyperValues is a string-value map for storing hyperparameter values
Expand Down Expand Up @@ -43,24 +44,16 @@ func (hv *HyperValues) CopyFrom(cp HyperValues) {
// Params. "Min" and "Max" guid the range, and "Sigma" describes a Gaussian.
type Hypers map[string]HyperValues //types:add

// ParamByNameTry returns given parameter, by name.
// Returns error if not found.
func (pr *Hypers) ParamByNameTry(name string) (map[string]string, error) {
// ParamByName returns given parameter, by name.
// Returns and logs error if not found.
func (pr *Hypers) ParamByName(name string) (map[string]string, error) {
vl, ok := (*pr)[name]
if !ok {
err := fmt.Errorf("params.Params: parameter named %v not found", name)
log.Println(err)
return map[string]string{}, err
return vl, errors.Log(fmt.Errorf("params.Params: parameter named %v not found", name))
}
return vl, nil
}

// ParamByName returns given parameter by name (just does the map access)
// Returns "" if not found -- use Try version for error
func (pr *Hypers) ParamByName(name string) map[string]string {
return (*pr)[name]
}

// SetByName sets given parameter by name to given value.
// (just a wrapper around map set function)
func (pr *Hypers) SetByName(name string, value map[string]string) {
Expand Down
69 changes: 21 additions & 48 deletions params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ package params

import (
"fmt"
"log"

"cogentcore.org/core/base/errors"
)

// Params is a name-value map for parameter values that can be applied
Expand All @@ -24,24 +25,16 @@ import (
// parameters to.
type Params map[string]string //types:add

// ParamByNameTry returns given parameter, by name.
// Returns error if not found.
func (pr *Params) ParamByNameTry(name string) (string, error) {
// ParamByName returns given parameter, by name.
// Returns and logs error if not found.
func (pr *Params) ParamByName(name string) (string, error) {
vl, ok := (*pr)[name]
if !ok {
err := fmt.Errorf("params.Params: parameter named %v not found", name)
log.Println(err)
return "", err
return "", errors.Log(fmt.Errorf("params.Params: parameter named %v not found", name))
}
return vl, nil
}

// ParamByName returns given parameter by name (just does the map access)
// Returns "" if not found -- use Try version for error
func (pr *Params) ParamByName(name string) string {
return (*pr)[name]
}

// SetByName sets given parameter by name to given value.
// (just a wrapper around map set function)
func (pr *Params) SetByName(name, value string) {
Expand Down Expand Up @@ -86,7 +79,7 @@ func (sl *Sel) SetString(param string, val string) {

// ParamVal returns the value of given parameter
func (sl *Sel) ParamValue(param string) (string, error) {
return sl.Params.ParamByNameTry(param)
return sl.Params.ParamByName(param)
}

///////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -115,32 +108,20 @@ func (sh *Sheet) ElemLabel(idx int) string {
return (*sh)[idx].Sel
}

// SelByNameTry returns given selector within the Sheet, by Name.
// Returns nil and error if not found.
func (sh *Sheet) SelByNameTry(sel string) (*Sel, error) {
sl := sh.SelByName(sel)
if sl == nil {
err := fmt.Errorf("params.Sheet: Sel named %v not found", sel)
log.Println(err)
return nil, err
}
return sl, nil
}

// SelByName returns given selector within the Sheet, by Name.
// Returns nil if not found -- use Try version for error
func (sh *Sheet) SelByName(sel string) *Sel {
// Returns and logs error if not found.
func (sh *Sheet) SelByName(sel string) (*Sel, error) {
for _, sl := range *sh {
if sl.Sel == sel {
return sl
return sl, nil
}
}
return nil
return nil, errors.Log(fmt.Errorf("params.Sheet: Sel named %v not found", sel))
}

// SetFloat sets the value of given parameter, in selection sel
func (sh *Sheet) SetFloat(sel, param string, val float64) error {
sp, err := sh.SelByNameTry(sel)
sp, err := sh.SelByName(sel)
if err != nil {
return err
}
Expand All @@ -150,7 +131,7 @@ func (sh *Sheet) SetFloat(sel, param string, val float64) error {

// SetString sets the value of given parameter, in selection sel
func (sh *Sheet) SetString(sel, param string, val string) error {
sp, err := sh.SelByNameTry(sel)
sp, err := sh.SelByName(sel)
if err != nil {
return err
}
Expand All @@ -160,7 +141,7 @@ func (sh *Sheet) SetString(sel, param string, val string) error {

// ParamVal returns the value of given parameter, in selection sel
func (sh *Sheet) ParamValue(sel, param string) (string, error) {
sp, err := sh.SelByNameTry(sel)
sp, err := sh.SelByName(sel)
if err != nil {
return "", err
}
Expand All @@ -175,28 +156,20 @@ func (sh *Sheet) ParamValue(sel, param string) (string, error) {
// and different such configurations can be chosen by name to apply as desired.
type Sets map[string]*Sheet //git:add

// SheetByNameTry tries to find given set by name, and returns error
// if not found (also logs the error)
func (ps *Sets) SheetByNameTry(name string) (*Sheet, error) {
// SheetByName tries to find given set by name.
// Returns and logs error if not found.
func (ps *Sets) SheetByName(name string) (*Sheet, error) {
st, ok := (*ps)[name]
if ok {
return st, nil
}
err := fmt.Errorf("params.Sets: Param Sheet named %s not found", name)
log.Println(err)
return nil, err
}

// SheetByName returns given sheet by name -- for use when confident
// that it exists, as a nil will return if not found with no error
func (ps *Sets) SheetByName(name string) *Sheet {
return (*ps)[name]
return nil, errors.Log(fmt.Errorf("params.Sets: Param Sheet named %s not found", name))
}

// SetFloat sets the value of given parameter, in selection sel,
// in sheet and set.
func (ps *Sets) SetFloat(sheet, sel, param string, val float64) error {
sp, err := ps.SheetByNameTry(sheet)
sp, err := ps.SheetByName(sheet)
if err != nil {
return err
}
Expand All @@ -206,7 +179,7 @@ func (ps *Sets) SetFloat(sheet, sel, param string, val float64) error {
// SetString sets the value of given parameter, in selection sel,
// in sheet and set. Returns error if anything is not found.
func (ps *Sets) SetString(sheet, sel, param string, val string) error {
sp, err := ps.SheetByNameTry(sheet)
sp, err := ps.SheetByName(sheet)
if err != nil {
return err
}
Expand All @@ -216,7 +189,7 @@ func (ps *Sets) SetString(sheet, sel, param string, val string) error {
// ParamVal returns the value of given parameter, in selection sel,
// in sheet and set. Returns error if anything is not found.
func (ps *Sets) ParamValue(sheet, sel, param string) (string, error) {
sp, err := ps.SheetByNameTry(sheet)
sp, err := ps.SheetByName(sheet)
if err != nil {
return "", err
}
Expand Down
Loading

0 comments on commit 40d67f5

Please sign in to comment.