Tasks#

The tasks module provides the task-based architecture for rate RNNs, separating cognitive tasks from neural network models.

Core Classes#

class rate.tasks.AbstractTask(settings: Dict[str, Any])[source]#

Bases: ABC

Abstract base class for rate-based RNN tasks.

This class defines the interface that all task implementations should follow. Each task is responsible for generating input stimuli and target outputs according to the specific task requirements.

abstract generate_stimulus(trial_type: str | None = None, seed: bool | None = False) Tuple[ndarray, Any][source]#

Generate input stimulus for the task.

Parameters:
  • trial_type (Optional[str]) – Specific trial type to generate (task-dependent).

  • seed (Optional[bool]) – Whether to use a fixed random seed.

Returns:

Input stimulus array and label/condition.

Return type:

Tuple[np.ndarray, Any]

abstract generate_target(label: Any, seed: bool | None = False) ndarray[source]#

Generate target output for the task given a label.

Parameters:
  • label (Any) – Task label or condition.

  • seed (Optional[bool]) – Whether to use a fixed random seed.

Returns:

Target output array.

Return type:

np.ndarray

simulate_trial(trial_type: str | None = None, seed: bool | None = False) Tuple[ndarray, ndarray, Any][source]#

Simulate a complete trial by generating stimulus and target.

Parameters:
  • trial_type (Optional[str]) – Specific trial type to generate (task-dependent).

  • seed (Optional[bool]) – Whether to use a fixed random seed.

Returns:

Stimulus, target, and label.

Return type:

Tuple[np.ndarray, np.ndarray, Any]

abstract validate_settings() None[source]#

Validate that all required settings are present and valid.

Raises:

ValueError – If required settings are missing or invalid.

class rate.tasks.GoNogoTask(settings: Dict[str, Any])[source]#

Bases: AbstractTask

Go/NoGo impulse control task implementation.

In this task, the network must respond to “Go” stimuli and withhold responses to “NoGo” stimuli, testing impulse control and decision-making capabilities.

generate_stimulus(trial_type: str | None = None, seed: bool | None = False) Tuple[ndarray, int][source]#

Generate the input stimulus matrix for the Go-NoGo task.

Parameters:
  • trial_type (Optional[str]) – ‘go’ or ‘nogo’ for specific trial types. If None, randomly selected.

  • seed (Optional[bool]) – Whether to use a fixed random seed.

Returns:

Tuple containing:
  • u: 1xT stimulus matrix

  • label: Either 1 (Go trial) or 0 (NoGo trial)

Return type:

Tuple[np.ndarray, int]

generate_target(label: int, seed: bool | None = False) ndarray[source]#

Generate the target output signal for the Go-NoGo task.

Parameters:
  • label (int) – Either 1 (Go trial) or 0 (NoGo trial).

  • seed (Optional[bool]) – Whether to use a fixed random seed.

Returns:

1xT target signal array.

Return type:

np.ndarray

validate_settings() None[source]#

Validate Go/NoGo task settings.

class rate.tasks.XORTask(settings: Dict[str, Any])[source]#

Bases: AbstractTask

XOR temporal logic task implementation.

This task presents two sequential stimuli (+1 or -1) and requires the network to output +1 if the stimuli are the same and -1 if they are different.

generate_stimulus(trial_type: str | None = None, seed: bool | None = False) Tuple[ndarray, str][source]#

Generate the input stimulus matrix for the XOR task.

Parameters:
  • trial_type (Optional[str]) – Specific pattern (‘++’, ‘+-’, ‘-+’, ‘–‘). If None, randomly selected.

  • seed (Optional[bool]) – Whether to use a fixed random seed.

Returns:

Tuple containing:
  • u: 2xT stimulus matrix

  • label: Either ‘same’ or ‘diff’

Return type:

Tuple[np.ndarray, str]

generate_target(label: str, seed: bool | None = False) ndarray[source]#

Generate the target output signal for the XOR task.

Parameters:
  • label (str) – Either ‘same’ or ‘diff’.

  • seed (Optional[bool]) – Whether to use a fixed random seed.

Returns:

A 1D target signal array of shape (T-1,).

Return type:

np.ndarray

validate_settings() None[source]#

Validate XOR task settings.

class rate.tasks.ManteTask(settings: Dict[str, Any])[source]#

Bases: AbstractTask

Context-dependent sensory integration task from Mante et al (2013).

This task requires the network to perform context-dependent decision making where the relevant sensory modality (color or motion) is determined by a context cue.

generate_stimulus(trial_type: str | None = None, seed: bool | None = False) Tuple[ndarray, int][source]#

Generate the input stimulus matrix for the sensory integration task.

Parameters:
  • trial_type (Optional[str]) – ‘color’ or ‘motion’ for specific contexts. If None, randomly selected.

  • seed (Optional[bool]) – Whether to use a fixed random seed.

Returns:

Tuple containing:
  • u: 4xT stimulus matrix

  • label: Either +1 or -1 (the correct decision)

Return type:

Tuple[np.ndarray, int]

generate_target(label: int, seed: bool | None = False) ndarray[source]#

Generate the target output signal for the sensory integration task.

Parameters:
  • label (int) – Either +1 or -1, the correct decision.

  • seed (Optional[bool]) – Whether to use a fixed random seed.

Returns:

A 1D target signal array of shape (T-1,).

Return type:

np.ndarray

validate_settings() None[source]#

Validate Mante task settings.

Factory Classes#

class rate.tasks.TaskFactory[source]#

Bases: object

Factory class for creating task instances.

classmethod create_task(task_name: str, settings: Dict[str, Any]) AbstractTask[source]#

Create a task instance by type.

Parameters:
  • task_name (str) – Name of task (‘go_nogo’, ‘xor’, ‘mante’).

  • settings (Dict[str, Any]) – Task settings.

Returns:

Created task instance.

Return type:

AbstractTask

Raises:

ValueError – If task type is not recognized.

classmethod list_available_tasks() list[source]#

List all available task types.

classmethod register_task(task_name: str, task_class: type) None[source]#

Register a task class.

Overview#

The tasks module implements a modern task-based architecture that separates cognitive task logic from neural network implementations. This design promotes modularity, extensibility, and code reuse.

Key Benefits:

  • Modular Design: Tasks are independent of specific neural network implementations

  • Extensible Framework: Easy to add new cognitive tasks

  • Consistent Interface: All tasks follow the same AbstractTask interface

  • Factory Pattern: Convenient task creation via TaskFactory

Task Workflow:

  1. Task Creation: Use TaskFactory.create_task() or instantiate directly

  2. Stimulus Generation: Call generate_stimulus() to create input patterns

  3. Target Generation: Call generate_target() to create expected outputs

  4. Trial Simulation: Use simulate_trial() for complete trial workflow

Supported Tasks:

  • Go-NoGo: Binary decision task with response inhibition

  • XOR: Temporal exclusive OR requiring working memory

  • Mante: Context-dependent sensory integration task

Example Usage#

from rate.tasks import TaskFactory

# Create a Go-NoGo task
settings = {'T': 200, 'stim_on': 50, 'stim_dur': 25}
task = TaskFactory.create_task('go_nogo', settings)

# Generate specific trial types
go_stimulus, go_label = task.generate_stimulus('go')
nogo_stimulus, nogo_label = task.generate_stimulus('nogo')

# Generate targets
go_target = task.generate_target(go_label)
nogo_target = task.generate_target(nogo_label)

# Complete trial simulation
stimulus, target, label = task.simulate_trial()

Custom Task Creation#

from rate.tasks import AbstractTask

class MyCustomTask(AbstractTask):
    def validate_settings(self):
        # Validate required settings
        pass

    def generate_stimulus(self, trial_type=None, seed=False):
        # Generate custom stimulus
        return stimulus, label

    def generate_target(self, label, seed=False):
        # Generate custom target
        return target

# Register with factory
TaskFactory.register_task('my_custom', MyCustomTask)