Utils module

append_data_to_file(filename, data)

Appends a data frame to an (possibly) existing tsv file :param filename: path to the file :type filename: str :param data: data to be appended to the file :type data: dataframe

dircheck(path2dir)

Checks if a directory exists! if it does not exist, it creates it :param dir_path: path to the directory you want to be created :type dir_path: str, path

find_stim(const, task_name, *parts, must_exist=True)

Resolve a single stimulus file for a task across the stimulus roots.

Searches <root>/<task_name>/<parts...> in each root (local first, package last) and returns the first path that exists. Because the local roots are searched first, appending the package fallback can never change a lookup that already succeeds - it can only rescue one that would otherwise fail.

Parameters:
  • const – the experiment constants module/object.

  • task_name (str) – task subfolder name (usually self.name).

  • *parts (str) – path components under the task folder - a filename, or e.g. 'clips', 'foo.mov' for a nested subfolder.

  • must_exist (bool) – if True (default) raise FileNotFoundError when the file is in none of the roots; if False, return the path under the first root (useful when writing a file rather than reading one).

Returns:

Path – the resolved stimulus path.

find_stim_dir(const, task_name)

Resolve a task’s stimulus directory across the stimulus roots.

Returns the first <root>/<task_name> that is an existing directory. Use this for the cases that operate on the folder itself (e.g. globbing for a set of files) rather than resolving one named file. If no root has the folder, the path under the first root is returned so callers get a sensible location for error messages.

Parameters:
  • const – the experiment constants module/object.

  • task_name (str) – task subfolder name (usually self.name).

Returns:

Path – the resolved task stimulus directory.

get_task_class(const, class_name)

Searches for the task class in the list of task modules and returns it :param const: constant.py object containing the list of task modules to search for the task class :type const: constant object :param class_name: name of the task class to be searched for :type class_name: str

Returns:

TaskClass (class) – the task class that was searched for

get_task_file_class(const, class_name)

Searches for the TaskFile class in the list of task modules and returns it. Mirrors get_task_class but for the file-generation side. Custom TaskFile classes follow a ‘<class>File’ naming convention so they can coexist with their matching Task class in the same module (e.g. SilentWord and SilentWordFile in my_tasks.py).

Parameters:
  • const (constant object) – constants.py object containing the task_modules list

  • class_name (str) – base class name from task_table.tsv (e.g. ‘SilentWord’)

Returns:

TaskFileClass (class) – the TaskFile class that was searched for

get_task_table(exp_dir=None)

Reads the task_table.tsv file from the experimental directory and the package direction and concatenates them, avoiding duplicates :param exp_dir: path to the experiment directory. If None, returns only the general table. :type exp_dir: str, path, optional

Returns:

task_table (dataframe) – dataframe containing the task table

stim_roots(const)

Return the ordered list of stimulus root directories to search.

Uses const.stim_dirs (a list of roots) if it is defined, otherwise falls back to the single const.stim_dir. The package’s bundled stimuli folder is always appended as the final fallback (deduplicated), so built-in tasks still resolve even when the primary root points at an experiment-local folder.

Parameters:

const – the experiment constants module/object.

Returns:

list[Path] – roots to search, in priority order (local first, package last).