behav_utils.config#

behav_utils.config — Project Configuration

Load a YAML config that maps your CSV columns to internal field names, defines task parameters, and registers session filter presets.

load_config(path) reads a config file and checks every key; load_experiment(config) then loads the experiment it describes. The configuration guide runs both on a small example tree.

See behav_utils/config_presets/config_minimal.yaml for a starter template, config_presets/config_full_reference.yaml for every option, and docs/config_guide.md / docs/config_examples/ for worked examples.

behav_utils.config.load_config(path)[source]#

Load a project config from YAML and check every key.

Unknown or misspelt keys are refused rather than ignored, and relative paths in the config resolve against the file’s folder.

Parameters:

path (str | Path) – The YAML file.

Returns:

The checked config.

Raises:
  • FileNotFoundError – path does not exist.

  • ConfigError – The config cannot be used as written: an unknown key (the message lists the valid ones and the closest match), a missing required key or a bad value.

Return type:

ProjectConfig

behav_utils.config.load_cohorts(path)[source]#

Load the cohorts: block from the project YAML.

Cohort membership only (which animals belong to which cohort); per-animal attributes belong in the animal metadata, not here. Import-safe: reads the raw YAML and touches nothing else, so behaviour-only report scripts can call it without pulling in the fitting stack.

Parameters:

path (str | Path) – Path to the YAML config file.

Returns:

{cohort_key: [animal_id, ...]} (empty dict if no cohorts: block).

Raises:
  • FileNotFoundError – Config file doesn’t exist.

  • ValueError – cohorts: is present but not a mapping of lists.

Return type:

Dict[str, List[str]]

class behav_utils.config.ProjectConfig(name, description='', reader=<factory>, animal_metadata=None, experimenter='', task=<factory>, tasks=<factory>, task_by=None, scoring=<factory>, clock=<factory>, columns=<factory>, session_metadata=<factory>, extra_columns=<factory>, session_types=<factory>, session_presets=<factory>, sessions_to_ignore=<factory>, source_dir=None)[source]#

A whole project config, as load_config returns it; checked on construction.

Each attribute is one block of the YAML file, and the configuration guide describes every key. The loading functions take it, and so do the analyses and plots that need the task or the column mapping.

Variables:
  • name (str) – project.name; ‘Unnamed Project’ when the file gives none.

  • description (str) – project.description.

  • reader (behav_utils.config.schema.CsvFolderReaderConfig | behav_utils.config.schema.TableReaderConfig) – How sessions are found and read (reader:): folders of CSV files, or one table.

  • animal_metadata (str | None) – Path of a JSON file of per-animal metadata, {animal_id: {...}}, or None.

  • experimenter (str) – project.experimenter: whose data, unless an animal’s metadata says otherwise.

  • task (behav_utils.config.schema.TaskConfig) – The task block used by default (task:).

  • tasks (Dict[str, behav_utils.config.schema.TaskConfig]) – Named alternative task blocks (tasks:), which task_by picks per session.

  • task_by (behav_utils.config.schema.TaskSelector | None) – Which session-metadata field picks a named task block; None when there are none.

  • scoring (behav_utils.config.schema.ScoringConfig) – What happens when a recorded score disagrees with the task’s (scoring:).

  • clock (behav_utils.config.schema.ClockConfig) – The recording clock every time column is on (clock:).

  • columns (Dict[str, behav_utils.config.schema.ColumnMapping]) – Column mappings, internal name to ColumnMapping (columns:).

  • session_metadata (Dict[str, behav_utils.config.schema.SessionMetadataMapping]) – Session-level metadata columns (session_metadata:).

  • extra_columns (List[str]) – Columns to load without mapping them to a field (extra_columns:).

  • session_types (Dict[str, Dict[str, List[str]]]) – Session-type overrides, {type_name: {animal_id: [entries]}}, stamped onto sessions at load time. The names are the project’s own; the library itself derives only ‘regular’ and ‘opto’.

  • session_presets (Dict[str, Dict[str, Any]]) – Named session presets, as raw dicts, registered at load time.

  • sessions_to_ignore (Dict[str, List[str]]) – Sessions dropped at load time and never built, {animal_id: [entries]}; an entry is a date or a session id without the animal prefix.

  • source_dir (str | None) – Folder of the YAML file the config came from, against which relative paths resolve; None when the config was built in code.

Parameters:
  • name (str)

  • description (str)

  • reader (CsvFolderReaderConfig | TableReaderConfig)

  • animal_metadata (str | None)

  • experimenter (str)

  • task (TaskConfig)

  • tasks (Dict[str, TaskConfig])

  • task_by (TaskSelector | None)

  • scoring (ScoringConfig)

  • clock (ClockConfig)

  • columns (Dict[str, ColumnMapping])

  • session_metadata (Dict[str, SessionMetadataMapping])

  • extra_columns (List[str])

  • session_types (Dict[str, Dict[str, List[str]]])

  • session_presets (Dict[str, Dict[str, Any]])

  • sessions_to_ignore (Dict[str, List[str]])

  • source_dir (str | None)

task_config_for(metadata)[source]#

The task block for a session, from its metadata.

Parameters:

metadata – The session’s metadata fields.

Returns:

The pair (name, block) for the named alternative that task_by picks, else ('default', task).

Return type:

Tuple[str, TaskConfig]

resolve_path(path)[source]#

A path from the config, made absolute.

Environment variables ($NAME) are expanded; a relative path is taken relative to the config file’s folder, or to the working directory for a config built in code.

Parameters:

path (str | Path) – The path as written.

Returns:

The absolute path.

Return type:

Path

get_csv_name(internal_name)[source]#

The CSV column name mapped to an internal field.

Parameters:

internal_name (str) – The field’s name in the library, such as ‘stimulus’.

Returns:

The column’s name in the files, or None when the field is not mapped.

Return type:

str | None

get_all_csv_columns()[source]#

Every CSV column name this config refers to.

Returns:

The column names.

Return type:

List[str]

property required_csv_columns: List[str]#

CSV columns that must be present (non-optional).

property optional_csv_columns: List[str]#

CSV columns that are optional.

class behav_utils.config.TaskConfig(primary_stimulus=None, primary_choice=None, boundary=0.0, stimulus_range=(-1.0, 1.0), stimulus_scale='linear', choice_mapping=<factory>, type='two_afc', params=<factory>)[source]#

One task block of the config.

It says how the stimulus and the response are read and mapped onto the library’s axis, and which task (type with its settings) decides what counts as correct.

The library computes everything on a normalised stimulus axis: [-1, 1] with the category boundary at 0 (binning, the psychometric fit bounds, the hard/easy split). normalise_stimulus maps raw values onto it, piecewise linearly on the chosen scale: boundary → 0, range[0] → -1, range[1] → +1. The default (range (-1, 1), boundary 0, linear) is the identity, so data already on that axis is untouched.

Variables:
  • primary_stimulus (str | None) – Which input column is THE stimulus for psychometric fitting. None if there is none.

  • primary_choice (str | None) – Which output column is THE choice.

  • boundary (float) – Category boundary in raw stimulus units

  • stimulus_range (Tuple[float, float]) – Raw stimulus range (min, max); must bracket boundary

  • stimulus_scale (str) – ‘linear’ or ‘log’ — the axis on which the raw stimulus is mapped to the library’s axis (see normalise_stimulus)

  • type (str) – The task (‘two_afc’ by default, ‘go_nogo’, or a registered one)

  • params (Dict[str, Any]) – The task’s own settings (b_side or go_side, on_boundary), checked against it

  • choice_mapping (behav_utils.config.schema.ChoiceMapping) – How raw choice values convert to categories

Parameters:
  • primary_stimulus (str | None)

  • primary_choice (str | None)

  • boundary (float)

  • stimulus_range (Tuple[float, float])

  • stimulus_scale (str)

  • choice_mapping (ChoiceMapping)

  • type (str)

  • params (Dict[str, Any])

make_task()[source]#

The task this block describes, which alone decides what counts as correct for its sessions.

Returns:

A Task of the block’s type, with its own parameters (b_side, go_side, on_boundary).

Return type:

Task

property responses_continuous: bool#

Whether this task’s responses are values on the stimulus scale (Reproduction, or yours).

normalise_continuous(raw)[source]#

Raw values mapped to [-1, 1] for a continuous task.

One linear map on the chosen scale, with the range’s midpoint at 0, so equal raw distances stay equal and errors and a tolerance mean the same everywhere on the range. Stimulus and response both pass through it.

Parameters:

raw – Raw values; NaN passes through.

Returns:

The values on the library’s axis.

Return type:

ndarray

normalise_stimulus(raw)[source]#

Raw stimuli mapped to [-1, 1], with the boundary at 0.

Piecewise linear on the chosen scale, so an asymmetric range still puts the boundary at 0 and the range’s ends at ±1. Values outside the range map beyond ±1; they are not clipped here (the binning clips).

Parameters:

raw – Raw stimuli; NaN passes through.

Returns:

The stimuli on the library’s axis.

Return type:

ndarray

class behav_utils.config.TaskSelector(field, values)[source]#

task_by: which named task block a session uses.

The block is picked by the value of one of the session’s metadata fields; sessions whose value is not listed use the default task.

Variables:
  • field (str) – The session-metadata field whose value picks the block.

  • values (Dict[str, str]) – Maps a value of that field to the name of a block under tasks.

Parameters:
  • field (str)

  • values (Dict[str, str])

class behav_utils.config.ScoringConfig(on_mismatch='warn', outcome_labels=<factory>)[source]#

scoring: what happens when a recorded score disagrees with the task’s.

Variables:
  • on_mismatch (str) – When a recorded correct or outcome column disagrees with the task’s score: ‘warn’, ‘raise’ or ‘ignore’.

  • outcome_labels (Dict[str, str]) – Which recorded outcome labels mean which outcome.

Parameters:
  • on_mismatch (str)

  • outcome_labels (Dict[str, str])

class behav_utils.config.ClockConfig(name='rig', unit='s', on_problem='warn', order=<factory>)[source]#

clock: the recording clock every time column is on.

Event times (columns named t_*) and reaction_time are converted to float64 seconds and kept on this clock, not re-zeroed, so other recordings on the same clock line up with the trials directly. name labels the clock (SessionData.clock); unit is the unit the files use, which a column can override with its own unit. on_problem says what happens when an event column goes backwards across trials, a reaction time is negative, or a trial breaks order (the event names in the order they must happen within a trial).

Variables:
  • name (str) – The clock’s label, kept as SessionData.clock.

  • unit (str) – The unit the files use: ‘s’, ‘ms’ or ‘us’.

  • on_problem (str) – What a time going backwards, a negative reaction time or events out of order does: ‘warn’, ‘raise’ or ‘ignore’.

  • order (List[str]) – Optional: the order in which the event columns happen within a trial.

Parameters:
  • name (str)

  • unit (str)

  • on_problem (str)

  • order (List[str])

to_seconds(values, unit=None)[source]#

values as float64 seconds.

Parameters:
  • values – Times in unit.

  • unit (str | None) – The unit to convert from; None for the clock’s unit.

Returns:

The times in seconds, as a float64 array.

Return type:

ndarray

exception behav_utils.config.ConfigError[source]#

A config that cannot be used as written: an unknown key, a missing required key, a bad value.

A ValueError, so code that caught ValueError from load_config still does.