Documentation ¶
Index ¶
- Variables
- type Counter
- type Event
- type Loop
- type NamedFunc
- type NamedFuncs
- func (funcs *NamedFuncs) Add(name string, fun func()) *NamedFuncs
- func (funcs *NamedFuncs) AddBool(name string, fun func() bool) *NamedFuncs
- func (funcs *NamedFuncs) Delete(name string) error
- func (funcs *NamedFuncs) FuncIndex(name string) (int, error)
- func (funcs *NamedFuncs) InsertAfter(after, name string, fun func() bool) error
- func (funcs *NamedFuncs) InsertAt(i int, name string, fun func() bool)
- func (funcs *NamedFuncs) InsertBefore(before, name string, fun func() bool) error
- func (funcs *NamedFuncs) Prepend(name string, fun func() bool)
- func (funcs *NamedFuncs) Replace(name string, fun func() bool) error
- func (funcs NamedFuncs) Run() bool
- func (funcs *NamedFuncs) String() string
- type Scope
- type Stack
- func (st *Stack) AddOnEndToAll(name string, fun func(mode, time enums.Enum))
- func (st *Stack) AddOnStartToAll(name string, fun func(mode, time enums.Enum))
- func (st *Stack) AddTime(time enums.Enum, ctrMax int) *Stack
- func (st *Stack) AddTimeIncr(time enums.Enum, ctrMax, ctrIncr int) *Stack
- func (st *Stack) ClearStep()
- func (st *Stack) CountersToStats(stats *estats.Stats)
- func (st *Stack) DocString() string
- func (st *Stack) Level(i int) *Loop
- func (st *Stack) SetStep(numSteps int, stopLevel enums.Enum)
- func (st *Stack) TimeAbove(time enums.Enum) enums.Enum
- func (st *Stack) TimeBelow(time enums.Enum) enums.Enum
- type Stacks
- func (ls *Stacks) AddEventAllModes(time enums.Enum, name string, atCtr int, fun func())
- func (ls *Stacks) AddOnEndToAll(name string, fun func(mode, time enums.Enum))
- func (ls *Stacks) AddOnStartToAll(name string, fun func(mode, time enums.Enum))
- func (ls *Stacks) AddStack(mode enums.Enum) *Stack
- func (ls *Stacks) ClearStep(mode enums.Enum)
- func (ls *Stacks) Cont()
- func (ls *Stacks) DocString() string
- func (ls *Stacks) Init()
- func (ls *Stacks) InitMode(mode enums.Enum)
- func (ls *Stacks) IsRunning() bool
- func (ls *Stacks) Loop(mode, time enums.Enum) *Loop
- func (ls *Stacks) ModeStack() *Stack
- func (ls *Stacks) ResetAndRun(mode enums.Enum)
- func (ls *Stacks) ResetCounters()
- func (ls *Stacks) ResetCountersBelow(mode enums.Enum, time enums.Enum)
- func (ls *Stacks) ResetCountersByMode(mode enums.Enum)
- func (ls *Stacks) Run(mode enums.Enum)
- func (ls *Stacks) Step(mode enums.Enum, numSteps int, stopLevel enums.Enum)
- func (ls *Stacks) Stop(level enums.Enum)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // If you want to debug the flow of time, set this to true. PrintControlFlow = false )
Functions ¶
This section is empty.
Types ¶
type Counter ¶
type Counter struct { // Cur is the current counter value. Cur int // Max is the maximum counter value. // Only used if > 0 ([Loop] requires an IsDone condition to stop). Max int // Inc is the increment per iteration. Inc int }
Counter combines an integer with a maximum value. It supports time tracking within looper.
func (*Counter) Incr ¶
func (ct *Counter) Incr()
Incr increments the counter by Inc. Does not interact with Max.
func (*Counter) Set ¶
Set sets the Cur value with return value indicating whether it is different from current Cur.
func (*Counter) SetCurMaxPlusN ¶
SetCurMaxPlusN sets the Cur value and Max as Cur + N -- run N more beyond current.
type Event ¶
type Event struct { // Name of this event. Name string // AtCounter is the counter value upon which this Event occurs. AtCounter int // OnEvent are the functions to run when Counter == AtCounter. OnEvent NamedFuncs }
A Event has function(s) that can be called at a particular point in the loop, when the counter is AtCounter value.
type Loop ¶
type Loop struct { // Counter increments every time through the loop, up to [Counter.Max]. Counter Counter // Events occur when Counter.Cur is at their AtCounter. Events []*Event // OnStart functions are called at the beginning of each loop iteration. OnStart NamedFuncs // OnEnd functions are called at the end of each loop iteration. OnEnd NamedFuncs // IsDone functions are called after each loop iteration, // and if any return true, then the loop iteration is terminated. IsDone NamedFuncs // StepCount is the default step count for this loop level. StepCount int }
Loop contains one level of a multi-level iteration stack, with functions that can be called at the start and end of each iteration of the loop, and a Counter that increments for each iteration, terminating if >= Max, or IsDone returns true. Within each iteration, any sub-loop at the next level down in its Stack runs its full set of iterations. The control flow is:
for { Events[Counter == AtCounter] // run events at counter OnStart() Run Sub-Loop to completion OnEnd() Counter += Inc if Counter >= Max || IsDone() { break } }
func (*Loop) AddEvent ¶
AddEvent adds a new event at given counter. If an event already exists for that counter, the function is added to the list for that event.
func (*Loop) EventByCounter ¶
EventByCounter returns event for given atCounter value, nil if not found.
func (*Loop) EventByName ¶
EventByName returns event by name, nil if not found.
type NamedFunc ¶
NamedFunc is a function closure with a name. Function returns a bool which is needed for stopping condition but is otherwise not used.
type NamedFuncs ¶
type NamedFuncs []NamedFunc
NamedFuncs is an ordered list of named functions.
func (*NamedFuncs) Add ¶
func (funcs *NamedFuncs) Add(name string, fun func()) *NamedFuncs
Add adds a named function (with no bool return value).
func (*NamedFuncs) AddBool ¶
func (funcs *NamedFuncs) AddBool(name string, fun func() bool) *NamedFuncs
AddBool adds a named function with a bool return value, for IsDone case.
func (*NamedFuncs) Delete ¶
func (funcs *NamedFuncs) Delete(name string) error
Delete deletes function of given name.
func (*NamedFuncs) FuncIndex ¶
func (funcs *NamedFuncs) FuncIndex(name string) (int, error)
FuncIndex finds index of function by name. Returns not found err message if not found.
func (*NamedFuncs) InsertAfter ¶
func (funcs *NamedFuncs) InsertAfter(after, name string, fun func() bool) error
InsertAfter inserts function after other function of given name.
func (*NamedFuncs) InsertAt ¶
func (funcs *NamedFuncs) InsertAt(i int, name string, fun func() bool)
InsertAt inserts function at given index.
func (*NamedFuncs) InsertBefore ¶
func (funcs *NamedFuncs) InsertBefore(before, name string, fun func() bool) error
InsertBefore inserts function before other function of given name.
func (*NamedFuncs) Prepend ¶
func (funcs *NamedFuncs) Prepend(name string, fun func() bool)
Prepend adds a function to the start of the list.
func (*NamedFuncs) Replace ¶
func (funcs *NamedFuncs) Replace(name string, fun func() bool) error
Replace replaces function with other function of given name.
func (NamedFuncs) Run ¶
func (funcs NamedFuncs) Run() bool
Run runs all of the functions, returning true if any of the functions returned true.
func (*NamedFuncs) String ¶
func (funcs *NamedFuncs) String() string
String prints the list of named functions.
type Scope ¶
type Scope int
Scope is a combined Mode + Time value. Mode is encoded by multiples of 1000 and Time is added to that.
type Stack ¶
type Stack struct { // Mode identifies the mode of processing this stack performs, e.g., Train or Test. Mode enums.Enum // Loops is the set of Loops for this Stack, keyed by the timescale. // Order is determined by the Order list. Loops map[enums.Enum]*Loop // Order is the list and order of time scales looped over by this stack of loops. // The ordered is from top to bottom, so longer timescales like Run should be at // the beginning and shorter timescales like Trial should be and the end. Order []enums.Enum // OnInit are functions to run for Init function of this stack, // which also resets the counters for this stack. OnInit NamedFuncs // StopNext will stop running at the end of the current StopLevel if set. StopNext bool // StopFlag will stop running ASAP if set. StopFlag bool // StopLevel sets the Time level to stop at the end of. // This is the current active Step level, which will be reset when done. StopLevel enums.Enum // StopCount determines how many iterations at StopLevel before actually stopping. // This is the current active Step control value. StopCount int // StepLevel is a saved copy of StopLevel for stepping. // This is what was set for last Step call (which sets StopLevel) or by GUI. StepLevel enums.Enum // StepCount is a saved copy of StopCount for stepping. // This is what was set for last Step call (which sets StopCount) or by GUI. StepCount int }
Stack contains a list of Loops to run, for a given Mode of processing. The order of Loop stacks is determined by the Order list.
func (*Stack) AddOnEndToAll ¶
AddOnEndToAll adds given function taking mode and time args to OnEnd in all loops.
func (*Stack) AddOnStartToAll ¶
AddOnStartToAll adds given function taking mode and time args to OnStart in all loops.
func (*Stack) AddTime ¶
AddTime adds a new timescale to this Stack with a given ctrMax number of iterations. The order in which this method is invoked is important, as it adds loops in order from top to bottom. Sets a default increment of 1 for the counter -- see AddTimeIncr for different increment.
func (*Stack) AddTimeIncr ¶
AddTimeIncr adds a new timescale to this Stack with a given ctrMax number of iterations, and increment per step. The order in which this method is invoked is important, as it adds loops in order from top to bottom.
func (*Stack) ClearStep ¶
func (st *Stack) ClearStep()
ClearStep clears the active stepping control state: StopNext and StopFlag.
func (*Stack) CountersToStats ¶
CountersToStats sets the current counter values to estats Int values by their time names only (no eval Mode). These values can then be read by elog LogItems to record the counters in logs. Typically, a TrialName string is also expected to be set, to describe the current trial (Step) contents in a useful way, and other relevant info (e.g., group / category info) can also be set.
func (*Stack) DocString ¶
DocString returns an indented summary of the loops and functions in the Stack.
func (*Stack) Level ¶
Level returns the Loop at the given ordinal level in the Order list. Will panic if out of range.
func (*Stack) SetStep ¶
SetStep sets stepping to given level and number of iterations. If numSteps == 0 then the default for the given stops
type Stacks ¶
type Stacks struct { // Stacks is the map of stacks by Mode. Stacks map[enums.Enum]*Stack // Mode has the current evaluation mode. Mode enums.Enum // contains filtered or unexported fields }
Stacks holds data relating to multiple stacks of loops, as well as the logic for stepping through it. It also holds helper methods for constructing the data. It's also a control object for stepping through Stacks of Loops. It holds data about how the flow is going.
Example ¶
stacks := NewStacks() stacks.AddStack(etime.Train). AddTime(etime.Epoch, 3). AddTime(etime.Trial, 2) // add function closures: stacks.Loop(etime.Train, etime.Epoch).OnStart.Add("Epoch Start", func() { fmt.Println("Epoch Start") }) stacks.Loop(etime.Train, etime.Epoch).OnEnd.Add("Epoch End", func() { fmt.Println("Epoch End") }) stacks.Loop(etime.Train, etime.Trial).OnStart.Add("Trial Run", func() { fmt.Println(" Trial Run") }) // add events: stacks.Loop(etime.Train, etime.Epoch).AddEvent("EpochTwoEvent", 2, func() { fmt.Println("Epoch==2") }) stacks.Loop(etime.Train, etime.Trial).AddEvent("TrialOneEvent", 1, func() { fmt.Println(" Trial==1") }) // fmt.Println(stacks.DocString()) stacks.Run(etime.Train)
Output: Epoch Start Trial Run Trial==1 Trial Run Epoch End Epoch Start Trial Run Trial==1 Trial Run Epoch End Epoch==2 Epoch Start Trial Run Trial==1 Trial Run Epoch End
func (*Stacks) AddEventAllModes ¶
AddEventAllModes adds a new event for all modes at given timescale.
func (*Stacks) AddOnEndToAll ¶
AddOnEndToAll adds given function taking mode and time args to OnEnd in all stacks, loops
func (*Stacks) AddOnStartToAll ¶
AddOnStartToAll adds given function taking mode and time args to OnStart in all stacks, loops
func (*Stacks) ClearStep ¶
ClearStep clears stepping variables from given mode, so it will run to completion in a subsequent Cont(). Called by Run
func (*Stacks) Cont ¶
func (ls *Stacks) Cont()
Cont continues running based on current state of the stacks. This is common pathway for Step and Run, which set state and call Cont. Programatic calling of Step can continue with Cont.
func (*Stacks) DocString ¶
DocString returns an indented summary of the loops and functions in the stack.
func (*Stacks) Init ¶
func (ls *Stacks) Init()
Init initializes all stacks. See Stacks.InitMode for more info.
func (*Stacks) InitMode ¶
InitMode initializes Stack of given mode, resetting counters and calling the OnInit functions.
func (*Stacks) ResetAndRun ¶
ResetAndRun calls ResetCountersByMode on this mode and then Run. This ensures that the Stack is run from the start, regardless of what state it might have been in.
func (*Stacks) ResetCounters ¶
func (ls *Stacks) ResetCounters()
ResetCounters resets the Cur on all loop Counters, and resets the Stacks's place in the loops.
func (*Stacks) ResetCountersBelow ¶
ResetCountersBelow resets the Cur on all loop Counters below given level (inclusive), and resets the Stacks's place in the loops.
func (*Stacks) ResetCountersByMode ¶
ResetCountersByMode resets counters for given mode.
func (*Stacks) Run ¶
Run runs the stack of loops for given mode (Train, Test, etc). This resets any stepping settings for this stack and runs until completion or stopped externally.