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) AddLevel(level enums.Enum, counterMax int) *Stack
- func (st *Stack) AddLevelIncr(level enums.Enum, counterMax, counterIncr int) *Stack
- func (st *Stack) AddOnEndToAll(name string, fun func(mode, level enums.Enum))
- func (st *Stack) AddOnStartToAll(name string, fun func(mode, level enums.Enum))
- func (st *Stack) ClearStep()
- func (st *Stack) Counters() []int
- func (st *Stack) CountersString() string
- func (st *Stack) DocString() string
- func (st *Stack) Level(i int) *Loop
- func (st *Stack) LevelAbove(level enums.Enum) (enums.Enum, bool)
- func (st *Stack) LevelBelow(level enums.Enum) (enums.Enum, bool)
- func (st *Stack) SetStep(numSteps int, stopLevel enums.Enum)
- type Stacks
- func (ls *Stacks) AddEventAllModes(level enums.Enum, name string, atCtr int, fun func())
- func (ls *Stacks) AddOnEndToAll(name string, fun func(mode, level enums.Enum))
- func (ls *Stacks) AddOnEndToLoop(level enums.Enum, name string, fun func(mode enums.Enum))
- func (ls *Stacks) AddOnStartToAll(name string, fun func(mode, level enums.Enum))
- func (ls *Stacks) AddOnStartToLoop(level enums.Enum, name string, fun func(mode enums.Enum))
- func (ls *Stacks) AddStack(mode, stepLevel enums.Enum) *Stack
- func (ls *Stacks) ClearStep(mode enums.Enum)
- func (ls *Stacks) Cont() enums.Enum
- 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, level enums.Enum) *Loop
- func (ls *Stacks) ModeStack() *Stack
- func (ls *Stacks) Modes() []enums.Enum
- func (ls *Stacks) ResetAndRun(mode enums.Enum) enums.Enum
- func (ls *Stacks) ResetCounters()
- func (ls *Stacks) ResetCountersBelow(mode enums.Enum, level enums.Enum)
- func (ls *Stacks) ResetCountersByMode(mode enums.Enum)
- func (ls *Stacks) Run(mode enums.Enum) enums.Enum
- func (ls *Stacks) Step(mode enums.Enum, numSteps int, stopLevel enums.Enum) 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 processing, 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 iteration 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 iteration 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 + Level value. Mode is encoded by multiples of 1000 and Level 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 level enum value.
// Order is determined by the Order list.
Loops map[enums.Enum]*Loop
// Order is the list and order of levels looped over by this stack of loops.
// The order is from top to bottom, so longer timescales like Run should be at
// the start and shorter level timescales like Trial should be at the end.
Order []enums.Enum
// OnInit are functions to run when Init is called, to restart processing,
// 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 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, which distinguishes this stack, and is its key in the map of Stacks. The order of Loop stacks is determined by the Order list of loop levels.
func (*Stack) AddLevel ¶
AddLevel adds a new level to this Stack with a given counterMax 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 AddLevelIncr for different increment.
func (*Stack) AddLevelIncr ¶
AddLevelIncr adds a new level to this Stack with a given counterMax 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) AddOnEndToAll ¶
AddOnEndToAll adds given function taking mode and level args to OnEnd in all loops.
func (*Stack) AddOnStartToAll ¶
AddOnStartToAll adds given function taking mode and level args to OnStart in all loops.
func (*Stack) ClearStep ¶
func (st *Stack) ClearStep()
ClearStep clears the active stepping control state: StopNext and StopFlag.
func (*Stack) Counters ¶
Counters returns a slice of the current counter values for this stack, in Order.
func (*Stack) CountersString ¶
CountersString returns a string with loop level and counter values.
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) LevelAbove ¶
LevelAbove returns the level above the given level in the stack returning false if this is the highest level, or given level does not exist in order.
func (*Stack) LevelBelow ¶
LevelBelow returns the level below the given level in the stack returning false if this is the lowest level, or given level does not exist in order.
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(levels.Train, levels.Trial).
AddLevel(levels.Epoch, 3).
AddLevel(levels.Trial, 2)
// add function closures:
stacks.Loop(levels.Train, levels.Epoch).OnStart.Add("Epoch Start", func() { fmt.Println("Epoch Start") })
stacks.Loop(levels.Train, levels.Epoch).OnEnd.Add("Epoch End", func() { fmt.Println("Epoch End") })
stacks.Loop(levels.Train, levels.Trial).OnStart.Add("Trial Run", func() { fmt.Println(" Trial Run") })
// add events:
stacks.Loop(levels.Train, levels.Epoch).AddEvent("EpochTwoEvent", 2, func() { fmt.Println("Epoch==2") })
stacks.Loop(levels.Train, levels.Trial).AddEvent("TrialOneEvent", 1, func() { fmt.Println(" Trial==1") })
// fmt.Println(stacks.DocString())
stacks.Run(levels.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 loop level.
func (*Stacks) AddOnEndToAll ¶
AddOnEndToAll adds given function taking mode and level args to OnEnd in all stacks, loops
func (*Stacks) AddOnEndToLoop ¶
AddOnEndToLoop adds given function taking mode arg to OnEnd in all stacks for given loop.
func (*Stacks) AddOnStartToAll ¶
AddOnStartToAll adds given function taking mode and level args to OnStart in all stacks, loops
func (*Stacks) AddOnStartToLoop ¶
AddOnStartToLoop adds given function taking mode arg to OnStart in all stacks for given loop.
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 ¶
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. Returns the level that was running when it stopped.
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) Modes ¶
Modes returns a sorted list of stack modes, for iterating in Mode enum value order.
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. Returns the level that was running when it stopped.
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. Returns the level that was running when it stopped.