Opticka 2.16.1
Opticka is an experiment manager for behavioral research.
|
run a task via a series of states. More...
run a task via a series of states.
stateMachine allows a set of 'states' to be run, with functions executed on entering state, within the state, and on exiting the state. States can be linked, so a 'middle' state can be run after a 'start' state. States can run in a loop (run()
method) and use either real time as assesed using the clockFcn fHandle property or via tick time, where each update() to the stateMachine is a 'tick'. Tick time is useful when controlled via an external manager like the Psychophysics toolbox which uses display refresh as a natural tick timer.
States have 4 fundamental evaluation points: ENTER, WITHIN, TRANSITION and EXIT. Each evaluation point takes a cell array of functions to run. TRANSITION evaluation is used to allow logic to switch from a default transition path to an alternate. For example, you can imagine a default stimulus > incorrect transition, but if the subject answers correctly you can use the transition evaluation to switch instead to the correct state.
To run a demo, try the following:
To see how to run the stateMacine from a PTB loop, see runExperiment.runTask()
; and check DefaultStateInfo.m and METHODS.md
Copyright ©2014-2022 Ian Max Andolina — released: LGPL3, see LICENCE.md
Public Member Functions | |
function | stateMachine (in varargin) |
Class constructor. | |
function | addStates (in me, in newStates) |
Add new states to the state machine. | |
function | addState (in me, in newState) |
add a single State to the state machine | |
function | editStateByName (in me, in stateName, in varargin) |
function | getState (in me, in stateName) |
getState retrieve a named state from the state list | |
function | update (in me) |
update the state machine, normally run via an external loop | |
function | forceTransition (in me, in stateName) |
forceTransition force the state machine into a new named state | |
function | start (in me) |
start the state machine | |
function | finish (in me, in force) |
finish stop the state machine | |
function | run (in me) |
run automomously run the state machine | |
function | isStateName (in me, in stateName) |
Check whether a string is the name of a state. | |
function | evalExitFcn (in me, in value) |
evalExitFcn sets current state skipExit value | |
function | printCurrentTick (in me) |
printcurrentTick prints current (and total) ticks to command window | |
function | UUID (in me) |
UUID function to return current UUID via a method. | |
function | reset (in me) |
reset the object | |
function | runDemo (in me) |
runDemo runs a sample state machine session | |
function | warmUp (in me) |
warmup state machine | |
function set | skipExitStates (in me, in list) |
skip exit state functions: sets an N x 2 cell array | |
function | showLog (in me) |
show the log if present | |
Public Member Functions inherited from optickaCore | |
function | optickaCore (in varargin) |
Class constructor. | |
function get | fullName (in me) |
function | getALF (in me, in subject, in sessionPrefix, in lab, in create) |
function | findAttributes (in me, in attrName, in attrValue) |
function | findAttributesandType (in me, in attrName, in attrValue, in type) |
find properties of object with specific attributes, for example all properties whose GetAcccess attribute is public and type is logical. | |
function | findPropertyDefault (in me, in propName) |
function | clone (in me) |
Use this syntax to make a deep copy of the object, i.e. OBJ_OUT has the same field values, but will not behave as a handle-copy of me anymore. | |
function | checkSuperclasses (in List) |
function | editProperties (in me, in properties) |
method to modify a set of properties | |
function | setProp (in me, in property, in value) |
method to fast change a particular value. This is useful for use in anonymous functions, like in the state machine. | |
function | initialiseGlobals (in me, in doReset, in doOpen) |
function | initialiseSaveFile (in me) |
Initialise Save prefix. | |
function | checkPaths (in me) |
checks the paths are valid | |
Static Public Member Functions | |
static function | plotLogs (in log, in tin) |
loadobj handler | |
Static Public Member Functions inherited from optickaCore | |
static function | makeArgs (in args) |
Converts cell args to structure array. | |
static function | addDefaults (in args, in defs) |
add default options to arg input | |
static function | hasKey (in in, in key) |
check if a struct / object has a propery / field | |
static function | getKeys (in device) |
PTB Get key presses, stops key bouncing. | |
Public Attributes | |
Property | stateList |
our state list, stored as a structure | |
Property | realTime |
Property | timeDelta |
Property | clockFcn |
clock function to use (GetSecs from PTB is optimal…) | |
Property | skipExitStates |
Property | tempNextState |
Property | verbose |
verbose logging to command window? | |
Property | waitFcn |
pause function (WaitSecs from PTB is optimal…) | |
Property | fnTimers |
do we run timers for function evaluations? | |
Property | logSize |
size of the log arrays to preallocate | |
Property | isRunning |
true or false, whether this object is currently busy running | |
Property | totalTicks |
total number of ticks, updated via runBriefly() and update() | |
Property | startTime |
time at start of stateMachine | |
Property | finalTime |
final time a finish | |
Property | finalTick |
final ticks at finish | |
Property | currentState |
current state | |
Property | currentName |
current state name | |
Property | currentUUID |
current state uuid | |
Property | currentIndex |
current state index | |
Property | currentTick |
ticks within the current state | |
Property | currentTime |
time within current state | |
Property | currentEntryTime |
time entered current state | |
Property | currentEntryFcn |
current entry function | |
Property | currentWithinFcn |
current within function | |
Property | currentTransitionFcn |
current transition function | |
Property | currentExitFcn |
current exit function | |
Property | nextTickOut |
number of ticks before next transition when realTime = false | |
Property | nextTimeOut |
time before next transition when realTime = true | |
Property | stateListIndex |
Index with name and index number for each state. | |
Property | log |
run state information | |
Public Attributes inherited from optickaCore | |
Property | name |
object name | |
Property | comment |
comment | |
Property | verbose |
verbose logging, subclasses must assign this. This is normally logical true/false | |
Property | dateStamp |
clock() dateStamp set on construction | |
Property | uuid |
universal ID | |
Property | paths |
storage of various paths | |
Property | fullName |
The fullName is the object name combined with its uuid and class name. | |
Protected Member Functions | |
function | transitionToStateWithName (in me, in nextName) |
transition to a named state | |
function | exitCurrentState (in me) |
exit current state | |
function | enterStateAtIndex (in me, in thisIndex) |
enters a particular state | |
function | initialiseLog (in me, in n) |
initialise the log arrays to improve performance | |
function | finaliseLog (in me) |
clear up log arrays | |
Protected Member Functions inherited from optickaCore | |
function | parseArgs (in me, in args, in allowedProperties) |
Sets properties from a structure or normal arguments pairs, ignores invalid or non-allowed properties. | |
function | addArgs (in me, in args) |
function | setPaths (in me) |
Sets properties from a structure or normal arguments pairs, ignores invalid or non-allowed properties. | |
function | getFonts (in me) |
set paths for object | |
function | toStructure (in me) |
Converts properties to a structure. | |
function | getType (in me, in in) |
function | logOutput (in me, in in, in message, in override) |
Give a metaproperty return the likely property class. | |
function | salutation (in me, in varargin) |
Prints messages dependent on verbosity. | |
Protected Attributes | |
Property | nStates |
number of states | |
Property | thisN |
current state number | |
Property | isFinishing |
should we run the finish function | |
Property | stateFields |
field names of allStates struct array, defining state behaviors | |
Property | stateDefaults |
default values of allStates struct array fields | |
Property | allowedProperties |
properties allowed during construction | |
Property | logFields |
Property | logValues |
Protected Attributes inherited from optickaCore | |
Property | cloning |
are we cloning this from another object | |
Property | mversion |
Matlab version number, this is transient so it is not saved. | |
Property | sansFont |
sans font | |
Property | monoFont |
monoFont | |
Property | className |
class name | |
Property | savePrefix |
save prefix generated from clock time | |
Property | fullName_ |
cached full name | |
function stateMachine::stateMachine | ( | in | varargin | ) |
Class constructor.
More detailed description of what the constructor does.
args | are passed as a structure of properties which is parsed. |
function stateMachine::addState | ( | in | me, |
in | newState | ||
) |
add a single State to the state machine
newState | a state structure |
function stateMachine::addStates | ( | in | me, |
in | newStates | ||
) |
Add new states to the state machine.
newStates | a cell array with information defining a state. |
function stateMachine::editStateByName | ( | in | me, |
in | stateName, | ||
in | varargin | ||
) |
Edit fields of an existing state.
stateName | string name of an existing state in allStates |
varargin | flexible number of field-value paris to edit the fields of the stateName state. |
Assigns the given values to the given fields of the existing state that has the name stateName. varargin represents a flexible number of traling arguments passed to editStateByName(). The first argument in each pair should be one of the field names of the allStates struct, which include the default state fields described for addField() and the names of any sharedEntry or sharedExit fevalables. The second argument in each pair should be a value to assign to the named field.
Editing the name field of a state might cause the state machine to misbehave.
Returns the index into allStates of the stateName state. If stateName is not the name of an existing state, returns [].
|
protected |
enters a particular state
thisIndex,the | index number of the state |
function stateMachine::evalExitFcn | ( | in | me, |
in | value | ||
) |
evalExitFcn sets current state skipExit value
|
protected |
exit current state
return
|
protected |
clear up log arrays
return
function stateMachine::finish | ( | in | me, |
in | force | ||
) |
finish stop the state machine
function stateMachine::forceTransition | ( | in | me, |
in | stateName | ||
) |
forceTransition force the state machine into a new named state
stateName | name of the state to transition to |
function stateMachine::getState | ( | in | me, |
in | stateName | ||
) |
getState retrieve a named state from the state list
stateName | name of a particular state |
|
protected |
initialise the log arrays to improve performance
n | number of entries |
function stateMachine::isStateName | ( | in | me, |
in | stateName | ||
) |
Check whether a string is the name of a state.
stateName | a state name |
|
static |
loadobj handler
plot timing logs
function stateMachine::printCurrentTick | ( | in | me | ) |
printcurrentTick prints current (and total) ticks to command window
function stateMachine::reset | ( | in | me | ) |
reset the object
function stateMachine::run | ( | in | me | ) |
run automomously run the state machine
function stateMachine::runDemo | ( | in | me | ) |
runDemo runs a sample state machine session
function stateMachine::showLog | ( | in | me | ) |
show the log if present
return
function set stateMachine::skipExitStates | ( | in | me, |
in | list | ||
) |
skip exit state functions: sets an N x 2 cell array
list | Nx2 cell array list of strings to compare |
function stateMachine::start | ( | in | me | ) |
start the state machine
|
protected |
transition to a named state
nextName | the next state to switch to |
function stateMachine::update | ( | in | me | ) |
update the state machine, normally run via an external loop
function stateMachine::UUID | ( | in | me | ) |
UUID function to return current UUID via a method.
function stateMachine::warmUp | ( | in | me | ) |
warmup state machine
|
protected |
properties allowed during construction
Property stateMachine::clockFcn |
clock function to use (GetSecs from PTB is optimal…)
Property stateMachine::currentEntryFcn |
current entry function
Property stateMachine::currentEntryTime |
time entered current state
Property stateMachine::currentExitFcn |
current exit function
Property stateMachine::currentIndex |
current state index
Property stateMachine::currentName |
current state name
Property stateMachine::currentState |
current state
Property stateMachine::currentTick |
ticks within the current state
Property stateMachine::currentTime |
time within current state
Property stateMachine::currentTransitionFcn |
current transition function
Property stateMachine::currentUUID |
current state uuid
Property stateMachine::currentWithinFcn |
current within function
Property stateMachine::finalTick |
final ticks at finish
Property stateMachine::finalTime |
final time a finish
Property stateMachine::fnTimers |
do we run timers for function evaluations?
|
protected |
should we run the finish function
Property stateMachine::isRunning |
true or false, whether this object is currently busy running
Property stateMachine::log |
run state information
|
protected |
Property stateMachine::logSize |
size of the log arrays to preallocate
|
protected |
Property stateMachine::nextTickOut |
number of ticks before next transition when realTime = false
Property stateMachine::nextTimeOut |
time before next transition when realTime = true
|
protected |
number of states
Property stateMachine::realTime |
use real time (true, using @clockFcn) or ticks (false) to mark state time. Real time is more accurate / robust against unexpected delays. Ticks uses timeDelta per tick and a tick timer (each update loop is 1 tick) for time measurement. This is simpler, can be controlled by an external driver that deals with timing, and without supervision but delays in the external update may cause drift.
Property stateMachine::skipExitStates |
N x 2 cell array of strings to compare, list to skip the current -> next state's exit functions; for example skipExitStates = {'fixate',{'incorrect','breakfix'}}; means that if the currentstate is 'fixate' and the next state is either incorrect OR breakfix, then skip the FIXATE exit state. Add multiple rows for skipping multiple state's exit states.
Property stateMachine::startTime |
time at start of stateMachine
|
protected |
default values of allStates struct array fields
|
protected |
field names of allStates struct array, defining state behaviors
Property stateMachine::stateList |
our state list, stored as a structure
Property stateMachine::stateListIndex |
Index with name and index number for each state.
Property stateMachine::tempNextState |
for a state transition you can override the next state, but this is reset on the transition, so you need logic at runtime to set this value each time. This can be used in an experiment where you set this when you are in state A, and based on a probability you can transition to state B or state C for example. See taskSequence.trialVar and runExperiment.updateNextState for the tools to use this.
|
protected |
current state number
Property stateMachine::timeDelta |
timedelta for time > ticks calculation, assume 0.1ms (1e-4) by default can set to IFI of display. This sets the "resolution" when realTime == false
Property stateMachine::totalTicks |
total number of ticks, updated via runBriefly() and update()
Property stateMachine::verbose |
verbose logging to command window?
Property stateMachine::waitFcn |
pause function (WaitSecs from PTB is optimal…)