Phase#
- class braintools.cogtask.Phase(duration, name=None)[source]#
Base class for task phases (epochs/periods).
A phase represents a time interval with specific: - Input encoding rules (how to fill input features) - Output/target encoding rules (what the expected output should be) - Duration (fixed or sampled)
Phases are composable via: -
>>operator: sequential concatenation -*operator: repetition -|operator: parallel composition- Parameters:
Examples
>>> import brainstate >>> import brainunit as u >>> from braintools.cogtask import Context, Fixation, Stimulus, Response, concat >>> # Sequential composition >>> phases = Fixation(100 * u.ms) >> Stimulus(500 * u.ms) >> Response(100 * u.ms) >>> phases Sequence(Fixation >> Stimulus >> Response) >>> # The same sequence built with ``concat`` >>> concat([Fixation(100 * u.ms), Stimulus(500 * u.ms), Response(100 * u.ms)]) Sequence(Fixation >> Stimulus >> Response) >>> # Repetition and parallel composition >>> Stimulus(100 * u.ms) * 5 Repeat(Stimulus, 5) >>> Stimulus(100 * u.ms, name='visual') | Stimulus(100 * u.ms, name='audio') Parallel(visual | audio) >>> >>> # Durations resolve to time steps with the environment's dt >>> with brainstate.environ.context(dt=1.0 * u.ms): ... phases.get_duration(Context()) 700
- children()[source]#
Return the immediate child phases of a compound phase.
Leaf phases return
[]. Subclasses likeSequence,Repeat,Parallel,If,Switch,Whileoverride this so that theTaskcan traverse the whole tree to bind features.
- abstractmethod encode_inputs(ctx)[source]#
Fill ctx.inputs[phase_start:phase_end] with input encoding.
Called once per phase after duration is determined. Must modify ctx.inputs in-place.
- abstractmethod encode_outputs(ctx)[source]#
Fill ctx.outputs[phase_start:phase_end] with target encoding.
Called once per phase after duration is determined. Must modify ctx.outputs in-place.
- max_steps(ctx)[source]#
Static upper bound on this phase’s length in timesteps.
Must return a Python int with no dependence on traced values. Used by
Taskin variable-length mode to size shape-stable buffers. The default delegates toget_durationwhich is correct for fixed-duration phases. Variable-duration phases (e.g. those wrappingTruncExp/UniformDuration) override this to return the truncation upper bound divided byctx.dt.
- step_count(ctx)[source]#
Traced actual length of this phase in timesteps.
Returns a
jax.Arrayint32scalar. May depend onctx[...]values populated bytrial_init. Must satisfy0 <= step_count(ctx) <= max_steps(ctx)for every trial.The default returns a static value equal to
get_duration; that is correct for any phase whose actual length matches its upper bound. Variable-duration phases override this to compute the traced length fromctxstate without anyint(...)cast.- Return type:
Array