behav_utils.tasks#
behav_utils.tasks — what counts as correct.
A Task decides each trial’s category, its correct response and how a response is scored, for
arrays of trials. Every session carries one, built from the config’s task: block (or a named
alternative in tasks:), and the loader scores every trial with it; recorded correct/outcome
columns are checked against that score, never used instead of it.
The built-in tasks are TwoAFC (two categories; b_side says which side of the boundary is B),
GoNoGo (a blank response is a withhold) and Reproduction (a continuous response, correct within
tolerance of the stimulus). Outcome holds the codes in TrialData.outcome (Outcome.CORRECT,
Outcome.ABORT, …).
Add a task with a frozen dataclass subclass of Task and @register_task (see EXTENDING.md).
Each task declares capabilities; statistics and readouts declare what they need, and require
raises RequirementError (naming any counterpart) when a task lacks it.
Stimulus distributions (Uniform, TruncatedNormal, Discrete, or a StimulusDistribution
subclass) say how a design draws stimuli and what an observer believes about them.
- class behav_utils.tasks.Task[source]#
A task: categories, the correct response and scoring, for arrays of trials.
Subclasses are frozen dataclasses whose fields are the task’s parameters (what a config sets), with a class-level
nameand the decoratorregister_task. They implementcategoryandtruth;complete_responsesandscorehave defaults that suit most tasks.capabilitiesnames what the task provides (seeCAPABILITIES); statistics and readouts declare what they need, andrequirechecks one against the other. The names are open: a task of your own can add its own, e.g.capabilities = TwoAFC.capabilities | {'cue'}.- Variables:
name (ClassVar[str]) – The task’s name in a config’s
task.type(class-level).capabilities (ClassVar[frozenset]) – What the task provides, as a frozenset of capability names (class-level).
- abstract property response_space: ResponseSpace#
What a response can be.
- abstractmethod category(stimulus, context=None)[source]#
The stimulus class per trial.
- Parameters:
stimulus – Stimuli on the library’s axis.
context (Mapping[str, Any] | None) – Session-level values a task may read, such as a contingency; None for none.
- Returns:
The class per trial as floats, NaN where a stimulus has none.
- Return type:
ndarray
- abstractmethod truth(stimulus, context=None)[source]#
The correct response per trial.
- Parameters:
stimulus – Stimuli on the library’s axis.
context (Mapping[str, Any] | None) – Session-level values a task may read, such as a contingency; None for none.
- Returns:
The correct response per trial as floats, NaN where none is correct.
- Return type:
ndarray
- complete_responses(response, abort, context=None)[source]#
Responses as the task reads them.
The default leaves them as they are; a task in which withholding is an answer, such as go/no-go, fills it in here.
- Parameters:
response – The responses as loaded, NaN where none was recorded.
abort – Whether each trial was aborted.
context (Mapping[str, Any] | None) – Session-level values a task may read, such as a contingency; None for none.
- Returns:
The responses the task scores.
- Return type:
ndarray
- score(response, truth, abort, context=None)[source]#
Outcome codes for each trial.
In order: abort, then no response, then no correct response (
UNSCORED), thenCORRECTwhere the response equals the truth andERRORotherwise. A task with another notion of correct, such as a tolerance around a continuous truth, overrides it.- Parameters:
response – The responses, completed by
complete_responses.truth – The correct responses, from
truth.abort – Whether each trial was aborted.
context (Mapping[str, Any] | None) – Session-level values a task may read, such as a contingency; None for none.
- Returns:
Outcomecodes, as int8.- Return type:
ndarray
- class behav_utils.tasks.TwoAFC(b_side='positive', on_boundary='A')[source]#
Two-alternative forced choice.
Category B is the stimuli on
b_sideof the boundary (default the positive side), A the rest, and the correct response is the category. A stimulus exactly on the boundary counts ason_boundary: ‘A’ (the default), ‘B’, or ‘unscored’ (no correct response, as on a rig that rewards at random there).- Variables:
b_side (str) – The side of the boundary that is category B, ‘positive’ or ‘negative’.
on_boundary (str) – What a stimulus exactly on the boundary counts as: ‘A’, ‘B’ or ‘unscored’.
- Parameters:
b_side (str)
on_boundary (str)
- property response_space: ResponseSpace#
What a response can be.
- category(stimulus, context=None)[source]#
The stimulus class per trial.
- Parameters:
stimulus – Stimuli on the library’s axis.
context (Mapping[str, Any] | None) – Session-level values a task may read, such as a contingency; None for none.
- Returns:
The class per trial as floats, NaN where a stimulus has none.
- Return type:
ndarray
- truth(stimulus, context=None)[source]#
The correct response per trial.
- Parameters:
stimulus – Stimuli on the library’s axis.
context (Mapping[str, Any] | None) – Session-level values a task may read, such as a contingency; None for none.
- Returns:
The correct response per trial as floats, NaN where none is correct.
- Return type:
ndarray
- class behav_utils.tasks.GoNoGo(go_side='positive', on_boundary='no_go')[source]#
Go/no-go.
The correct response is go (1) to stimuli on
go_sideof the boundary and no-go (0, withholding) to the rest. A blank response that is not an abort is a withhold, so it is scored, not lost. A stimulus exactly on the boundary counts ason_boundary: ‘no_go’ (the default), ‘go’ or ‘unscored’.- Variables:
go_side (str) – The side of the boundary where go is correct, ‘positive’ or ‘negative’.
on_boundary (str) – What a stimulus exactly on the boundary counts as: ‘no_go’, ‘go’ or ‘unscored’.
- Parameters:
go_side (str)
on_boundary (str)
- property response_space: ResponseSpace#
What a response can be.
- category(stimulus, context=None)[source]#
The stimulus class per trial.
- Parameters:
stimulus – Stimuli on the library’s axis.
context (Mapping[str, Any] | None) – Session-level values a task may read, such as a contingency; None for none.
- Returns:
The class per trial as floats, NaN where a stimulus has none.
- Return type:
ndarray
- truth(stimulus, context=None)[source]#
The correct response per trial.
- Parameters:
stimulus – Stimuli on the library’s axis.
context (Mapping[str, Any] | None) – Session-level values a task may read, such as a contingency; None for none.
- Returns:
The correct response per trial as floats, NaN where none is correct.
- Return type:
ndarray
- complete_responses(response, abort, context=None)[source]#
Responses as the task reads them.
The default leaves them as they are; a task in which withholding is an answer, such as go/no-go, fills it in here.
- Parameters:
response – The responses as loaded, NaN where none was recorded.
abort – Whether each trial was aborted.
context (Mapping[str, Any] | None) – Session-level values a task may read, such as a contingency; None for none.
- Returns:
The responses the task scores.
- Return type:
ndarray
- class behav_utils.tasks.Reproduction(tolerance=0.1)[source]#
Reproduce the stimulus.
The correct response is the stimulus itself, and a response is correct when it lies within
toleranceof it, the boundary included, on the library’s axis: a rat that sees a dot at x on a line and pokes at y is correct when|y − x| ≤ tolerance. There are no categories (categoryis NaN). The statistics reference lists the statistics for continuous responses.- Variables:
tolerance (float) – The largest distance from the stimulus, on the library’s axis, that still counts as correct; must be positive.
- Parameters:
tolerance (float)
- property response_space: ResponseSpace#
What a response can be.
- category(stimulus, context=None)[source]#
The stimulus class per trial.
- Parameters:
stimulus – Stimuli on the library’s axis.
context (Mapping[str, Any] | None) – Session-level values a task may read, such as a contingency; None for none.
- Returns:
The class per trial as floats, NaN where a stimulus has none.
- Return type:
ndarray
- truth(stimulus, context=None)[source]#
The correct response per trial.
- Parameters:
stimulus – Stimuli on the library’s axis.
context (Mapping[str, Any] | None) – Session-level values a task may read, such as a contingency; None for none.
- Returns:
The correct response per trial as floats, NaN where none is correct.
- Return type:
ndarray
- score(response, truth, abort, context=None)[source]#
Outcome codes for each trial.
In order: abort, then no response, then no correct response (
UNSCORED), thenCORRECTwhere the response equals the truth andERRORotherwise. A task with another notion of correct, such as a tolerance around a continuous truth, overrides it.- Parameters:
response – The responses, completed by
complete_responses.truth – The correct responses, from
truth.abort – Whether each trial was aborted.
context (Mapping[str, Any] | None) – Session-level values a task may read, such as a contingency; None for none.
- Returns:
Outcomecodes, as int8.- Return type:
ndarray
- class behav_utils.tasks.Outcome(*values)[source]#
How a trial was scored;
TrialData.outcomeholds these as small integers.- Variables:
ERROR – Responded, and not with the correct response.
CORRECT – Responded with the correct response.
NO_RESPONSE – The response window passed with no response, where withholding is not an answer.
ABORT – The trial ended before the response window.
UNSCORED – Responded, but the task defines no correct response for this trial.
- class behav_utils.tasks.ResponseSpace(kind, labels=())[source]#
What a response can be.
- Variables:
kind (str) – ‘binary’ (0 or 1), ‘categorical’ (0 to K−1) or ‘continuous’.
labels (tuple) – Names of the discrete values, in order; empty for a continuous response.
- Parameters:
kind (str)
labels (tuple)
- behav_utils.tasks.register_task(cls)[source]#
Class decorator: make a Task subclass known by its
name, so configs and stores can name it.
- behav_utils.tasks.task_from_dict(d)[source]#
Rebuild a task from
to_dictoutput.- Parameters:
d (Mapping[str, Any]) –
{'type': name, **parameters}.- Returns:
The task.
- Return type:
- behav_utils.tasks.list_tasks()[source]#
The registered task types.
- Returns:
The names a config’s
task.typemay take.- Return type:
list
- class behav_utils.tasks.StimulusDistribution[source]#
A distribution of stimuli:
sample,pdfandsupport;gridcomes with it.- abstract property support: tuple#
where the stimuli lie.
- Type:
(low, high)
- abstractmethod sample(n, rng)[source]#
Draw stimuli from the distribution.
- Parameters:
n (int) – How many.
rng (Generator) – Random generator.
- Returns:
nstimuli.- Return type:
ndarray
- abstractmethod pdf(x)[source]#
The density at
x; the probability mass, for a discrete distribution.- Parameters:
x – Points on the library’s axis.
- Returns:
The density at each point.
- Return type:
ndarray
- grid(n=2000)[source]#
The distribution on a grid, for computing with it.
The points are the midpoints of
nequal cells over the support. Midpoints keep a symmetric distribution symmetric about the boundary, which is never itself a point.- Parameters:
n (int) – How many cells.
- Returns:
(points, weights), the weights summing to 1.- Return type:
tuple
- class behav_utils.tasks.Uniform(low=-1.0, high=1.0)[source]#
Uniform on [low, high].
- Variables:
low (float) – Lower end of the support.
high (float) – Upper end of the support.
- Parameters:
low (float)
high (float)
- property support: tuple#
where the stimuli lie.
- Type:
(low, high)
- class behav_utils.tasks.TruncatedNormal(mean=0.0, sd=0.3, low=-1.0, high=1.0)[source]#
A normal (
mean,sd) restricted to [low, high].- Variables:
mean (float) – Mean of the normal before truncation.
sd (float) – SD of the normal before truncation.
low (float) – Lower end of the support.
high (float) – Upper end of the support.
- Parameters:
mean (float)
sd (float)
low (float)
high (float)
- property support: tuple#
where the stimuli lie.
- Type:
(low, high)
- class behav_utils.tasks.Discrete(values, weights=None)[source]#
A finite set of stimuli
valuesdrawn withweights(equal if omitted; normalised).- Variables:
values (tuple) – The stimuli that can occur.
weights (tuple | None) – Their probabilities, normalised to sum to 1; equal when not given.
- Parameters:
values (tuple)
weights (tuple | None)
- property support: tuple#
where the stimuli lie.
- Type:
(low, high)
- sample(n, rng)[source]#
Draw stimuli from the distribution.
- Parameters:
n (int) – How many.
rng (Generator) – Random generator.
- Returns:
nstimuli.- Return type:
ndarray
- exception behav_utils.tasks.RequirementError[source]#
A statistic or readout was asked of a task that lacks a capability it needs.
- behav_utils.tasks.require(task, needs, what, instead=None)[source]#
Raise
RequirementErrorunlesstaskdeclares every capability inneeds.whatnames the statistic or readout.insteadmaps a capability to a counterpart to suggest when the task has it ({'withhold': 'go_bias'}). The message says what is missing, what the task has, the counterpart or that there is none, and flags a name no registered task declares (a typo?).- Parameters:
task (Task) – The task to check.
needs – The capability names required.
what (str) – The statistic or readout asking, named in the message.
instead (Mapping[str, str] | None) – Maps a capability to a counterpart to suggest when the task has it; None for none.
- Raises:
RequirementError – The task lacks one of
needs.- Return type:
None
Constants#
CAPABILITIES maps each capability a task can declare to what it means. The
statistics reference lists them, with the statistics each built-in task refuses.