Task#

class pypillometry.sim.Task(name='task', blocks=(), repeat_n=None, repeat_duration=None, start=0)[source]#

Bases: SimStep

Design of an experiment: blocks of trials with stimuli and responses.

A task is built by chaining: block() starts a block of trials, stimulus() and response() add events to each trial of the current block, rest() adds a pause and repeat() repeats the blocks. Each method returns a new task. Times are fixed ("250 ms", plain numbers are ms) or distributions ("uniform(2 s, 10 s)", "lognormal(350 ms, 70 ms)", see parse_dist()).

In a simulation (Simulation.task), the task makes the events of the data, with the metadata kind ("stimulus" or "response"), modality, trial (numbered over the task), block, condition, duration (of stimuli, ms), luminance, rt (of responses: the time from the event they follow, ms) and the values given in meta of the events. Ready-made tasks are in pypillometry.sim.tasks.

Parameters:

name (str) – name of the task

Examples

>>> import pypillometry as pp
>>> sart = (pp.sim.Task("SART")
...         .block(n=225, soa="1150 ms", conditions={"go": 8/9, "nogo": 1/9})
...         .stimulus("digit", "visual", duration="250 ms")
...         .stimulus("mask", "visual", at="250 ms", duration="900 ms")
...         .response("press", after="digit", rt="lognormal(350 ms, 70 ms)",
...                   p={"go": 0.97, "nogo": 0.3}))
>>> import numpy as np
>>> ev, end = sart.generate(np.random.default_rng(1))
>>> sorted(str(x) for x in set(ev["label"]))
['digit', 'mask', 'press']

Methods

apply

change the arrays of the running simulation run

block

Start a block of trials.

eyes_in

the eyes of the simulation the step applies to

from_dict

the step from to_dict()

generate

Simulate the events of the task.

plot

Schematic of the trials of each block of the task.

repeat

Repeat the blocks of the task.

response

Add a response to each trial of the current block.

rest

Add a pause without events.

stimulus

Add a stimulus to each trial of the current block.

to_dict

name and parameters of the step (numbers, strings, lists, dicts)

Details

static from_dict(d)#

the step from to_dict()

Return type:

SimStep

apply(run, rng)[source]#

change the arrays of the running simulation run

Return type:

None

block(n=None, duration=None, soa=None, isi=None, conditions=None)[source]#

Start a block of trials.

Parameters:
  • n (int, optional) – number of trials

  • duration (float, str or distribution, optional) – duration of the block (trials start until it is over); n and/or duration must be given

  • soa (float, str or distribution, optional) – time from the start of one trial to the start of the next

  • isi (float, str or distribution, optional) – time from the end of one trial (its last event, or the end of the last stimulus) to the start of the next; default 0 if neither soa nor isi is given (then a block with a duration but no n needs trials that take time)

  • conditions (dict or list, optional) – conditions of the trials with their probabilities (e.g. {"go": 8/9, "nogo": 1/9}; a list for equal probabilities)

Return type:

Self

eyes_in(run)#

the eyes of the simulation the step applies to

Return type:

tuple[str, ...]

generate(rng)[source]#

Simulate the events of the task.

Parameters:

rng (np.random.Generator)

Return type:

tuple[dict[str, ndarray], float]

Returns:

  • events (dict) – column -> array: onset (ms), label and the metadata (see Task), sorted by onset

  • end (float) – end of the task (ms)

plot(axes=None, figsize=None)[source]#

Schematic of the trials of each block of the task.

One panel per block with events: one lane per event, stimuli as bars (coloured by modality), the onsets that vary as a band from the 10% to the 90% quantile around the mean, the spacing of the trials and the probabilities of the events.

Parameters:
  • axes (sequence of Axes, optional) – axes to draw on (one per block with events); default: a new figure

  • figsize (tuple, optional) – size of the new figure

Return type:

Figure

repeat(n=None, duration=None)[source]#

Repeat the blocks of the task.

Parameters:
  • n (int, optional) – number of times the blocks are run

  • duration (float, str or distribution, optional) – the blocks are run again until this duration is reached

Return type:

Self

response(label, after=None, rt=0, p=1.0, modality='motor', meta=None)[source]#

Add a response to each trial of the current block.

Parameters:
  • label (str) – label of the event

  • after (str, optional) – label of the earlier event of the trial that it responds to; if None, rt is relative to the start of the trial

  • rt (float, str or distribution) – time from the event after (the reaction time; may be negative, e.g. for taps that anticipate a tone)

  • p (float or dict) – probability of the response (per condition with a dict; 0 for conditions that are not given)

  • modality (str) – default "motor"

  • meta (dict, optional) – further metadata (see stimulus())

Return type:

Self

rest(duration)[source]#

Add a pause without events.

Parameters:

duration (float, str or distribution) – duration of the pause

Return type:

Self

stimulus(label, modality='visual', at=0, after=None, duration=None, timeout='30 s', p=1.0, luminance=None, meta=None)[source]#

Add a stimulus to each trial of the current block.

Parameters:
  • label (str) – label of the event

  • modality (str) – "visual" (default), "auditory" or another modality

  • at (float, str or distribution) – onset relative to the start of the trial, or to the event after (default 0)

  • after (str, optional) – label of an earlier event of the trial that at refers to

  • duration (float, str or distribution, optional) – duration of the stimulus; "until response": until the first response that follows it (or timeout)

  • timeout (float, str or distribution) – duration of a stimulus "until response" without a response (default 30 s)

  • p (float or dict) – probability of the stimulus (per condition with a dict; 0 for conditions that are not given)

  • luminance (float, optional) – change of the luminance at the onset (for visual stimuli; used by future pupil responses)

  • meta (dict, optional) – further metadata, name -> values: a value, a list (drawn with equal probabilities), a dict value -> probability, a dict condition -> one of these, or {"nback": k, "values": [...]} for the stimuli of an n-back task (in condition "target" the value of k trials before, otherwise another value)

Return type:

Self

to_dict()#

name and parameters of the step (numbers, strings, lists, dicts)

Return type:

dict[str, Any]