braintools.cogtask.create_task

Contents

braintools.cogtask.create_task#

braintools.cogtask.create_task(phases, input_features, output_features, trial_init=None, name=None, seed=None, output_mode='categorical', num_classes=None, dt=None)[source]#

Convenience factory for creating tasks.

Parameters:
  • phases (Phase) – The phase structure.

  • input_features (Feature or FeatureSet) – Input feature definitions.

  • output_features (Feature or FeatureSet) – Output feature definitions.

  • trial_init (Callable[[Context], None] | None) – Trial initialization function.

  • name (str | None) – Task name.

  • seed (int | None) – Random seed.

  • output_mode (Literal['categorical', 'vector']) – Output mode.

  • num_classes (int | None) – Number of categorical classes for the 1-D label target. Used to size a classifier head in output_mode='categorical'; falls back to num_outputs when unset. Ignored in output_mode='vector'.

  • dt (float or Quantity, optional) – Time step used to resolve phase durations. Defaults to brainstate.environ.get_dt() when not given.

Returns:

Configured task instance.

Return type:

Task

Note

The time step (dt) defaults to brainstate.environ.get_dt() when not set.

Examples

>>> import brainunit as u
>>> from braintools.cogtask import (
...     Feature, Fixation, Response, Stimulus, circular, create_task, label)
>>> task = create_task(
...     phases=(
...         Fixation(100 * u.ms, inputs={'fixation': 1.0})
...         >> Stimulus(500 * u.ms, inputs={'stimulus': circular('direction')})
...         >> Response(100 * u.ms, outputs={'label': label('ground_truth')})
...     ),
...     input_features=Feature(1, 'fixation') + Feature(2, 'stimulus'),
...     output_features=Feature(1, 'fixation') + Feature(2, 'choice'),
...     trial_init=lambda ctx: ctx.update(
...         ground_truth=ctx.rng.choice(2),
...         direction=ctx.rng.uniform(0, 2 * 3.14159)
...     ),
...     seed=0,
...     dt=10.0 * u.ms,
... )
>>> task.num_inputs, task.num_classes
(3, 3)
>>> X, Y, info = task.sample_trial(0)
>>> X.shape, Y.shape
((70, 3), (70,))