Utilities (pyavs.utils)¶
The utils module provides configuration, validation, and path management utilities.
Configuration Management¶
Configuration management for pyAVS package.
This module provides functions for managing data paths and package configuration.
Historically this module kept its own separate global config dict, independent of
the unified pyavs.config (PyAVSConfig/ConfigManager) system. That meant
pyavs.set_data_path() (which writes to the unified system) and
get_data_path() used internally throughout the core library (which read from
this module’s old separate dict) never agreed with each other unless callers
passed data_path= explicitly everywhere. The functions below now proxy to the
unified global config so both entry points read/write the same underlying store.
- pyavs.utils.config.set_data_path(path: str) None[source]¶
Set the base data path for the AVS dataset.
- Parameters:
path (str) – Path to the AVS BIDS dataset directory
- pyavs.utils.config.get_data_path() str | None[source]¶
Get the current data path.
- Returns:
Current data path, or None if not set
- Return type:
str or None
- pyavs.utils.config.setup_data_directory(path: str | None = None) str[source]¶
Set up data directory with automatic detection.
- Parameters:
path (str, optional) – Data path. If None, uses the unified config’s auto-detect cascade (PYAVS_DATA_PATH env var -> ~/.config/pyavs/config.json).
- Returns:
Data path that was set
- Return type:
- Raises:
FileNotFoundError – If no path is given and auto-detection fails.
- pyavs.utils.config.get_config() Dict[str, Any][source]¶
Get current configuration as a plain dict.
Deprecated in favor of
pyavs.config.get_config(), which returns the richerConfigManager/PyAVSConfigobject. Kept as a thin shim for backward compatibility.- Returns:
Current configuration, with
data_pathreflecting the live unified config.- Return type:
- pyavs.utils.config.update_config(**kwargs) None[source]¶
Update auxiliary configuration parameters (server, output_prefix, cache_dir, verbose). Use
set_data_path()to update the data path itself.- Parameters:
**kwargs – Configuration parameters to update
- pyavs.utils.config.get_derivatives_root() str | None[source]¶
Get the pyAVS derivatives write root.
Defaults to
<data_path>/derivatives/pyavs, overridable via thePYAVS_DERIVATIVES_PATHenvironment variable or the config’sderivatives_pathfield — useful when the dataset copy is read-only.- Returns:
Derivatives root, or None if no data path is configured.
- Return type:
str or None
Path Utilities¶
Path utilities for pyAVS package.
Naming helpers (sub-XX/ses-XX labels, session letters, native
subject-session IDs) and every dataset path now live in pyavs.layout.
The session-naming functions below are thin aliases re-exported from there so
existing imports keep working; new code should prefer pyavs.layout.
The legacy path builders that used to live here — get_bids_path and
get_legacy_paths — are gone. get_bids_path built
sub-01/ses-01/meg/sub-01_ses-01_task-avs_run-01_raw.fif, a filename that
exists in neither the public release nor the internal tree (the release
preserves native scanner names), so every “try BIDS first” branch built on it
always missed. get_legacy_paths addressed the internal results/as01_01/
tree, which pyAVS no longer supports. Use pyavs.layout.Layout instead.
- pyavs.utils.paths.convert_session_to_letter(session: int) str¶
Convert a session number to its MEG-filename letter.
The MEG naming convention at MPI represents sessions as letters (1, 2, 3 → a, b, c).
- Parameters:
session (int) – Session number (1-based).
- Returns:
Session letter.
- Return type:
- Raises:
ValueError – If
sessionis outside 1-26.
- pyavs.utils.paths.convert_letter_to_session(letter: str) int¶
Convert a session letter back to a session number.
- Parameters:
letter (str) – Session letter.
- Returns:
Session number (1-based).
- Return type:
- Raises:
ValueError – If
letteris not a lowercase ASCII letter.
- pyavs.utils.paths.get_subject_session_id(subject_id: int, session: int, prefix: str = 'as') str¶
Build the native subject-session ID used in raw MEG/ET filenames.
- pyavs.utils.paths.get_derivatives_path(data_path: str, subject_id: int, session: int | None = None, datatype: str | None = None) str[source]¶
Get the pyAVS derivatives directory for a subject (and optionally session).
- Parameters:
- Returns:
e.g.
<root>/derivatives/pyavs/sub-01/ses-01/meg- Return type:
- pyavs.utils.paths.get_max_blocks(session: int) int[source]¶
Get maximum number of blocks for a given session.
- pyavs.utils.paths.get_default_subjects_dir() str[source]¶
Get the FreeSurfer subjects directory.
Checks in order:
The
SUBJECTS_DIRenvironment variable, if it points somewhere that exists.<data_path>/derivatives/freesurferfrom the configured AVS root (seepyavs.configure()), which the public release ships as a ready-to-use MNESUBJECTS_DIR.
- Returns:
Path to the subjects directory.
- Return type:
- Raises:
ValueError – If
SUBJECTS_DIRis unset and no AVS data path is configured.
Data Validation¶
Validation utilities for pyAVS package.
This module provides functions for validating data integrity and input parameters.
- pyavs.utils.validation.validate_subject_id(subject_id: int) int[source]¶
Validate subject ID.
- Parameters:
subject_id (int) – Subject ID to validate
- Raises:
ValueError – If subject ID is invalid
- pyavs.utils.validation.validate_session(session: int) int[source]¶
Validate session number.
- Parameters:
session (int) – Session number to validate
- Raises:
ValueError – If session number is invalid
- pyavs.utils.validation.validate_blocks(blocks: int | List[int] | None, session: int) List[int][source]¶
Validate and normalize block specification.
- pyavs.utils.validation.validate_data_integrity(data_path: str, subject_id: int, session: int, blocks: List[int] | None = None) Dict[str, Any][source]¶
Validate data integrity for a subject/session.
Derivatives Path Conventions¶
BIDS derivatives path/naming conventions – see Dataset Structure.
Unified derivatives directory utilities for pyAVS package.
This module provides standardized functions for creating BIDS-compliant derivatives directory structures and paths for all pyAVS data products.
- class pyavs.utils.derivatives.DerivativesManager(data_path: str | None = None)[source]¶
Bases:
objectUnified manager for all derivatives directory operations.
Ensures a consistent structure, matching the public release:
derivatives/pyavs/sub-{subject_id:02d}/ses-{session:02d}/{datatype}/.Products keyed by a parameter signature rather than by session (
filters/,population_codes/) stay directly under the derivatives root, since they are not per-session artifacts.- __init__(data_path: str | None = None)[source]¶
Initialize derivatives manager.
- Parameters:
data_path (str, optional) – Base data path. If None, uses configured data path.
- get_preprocessed_path(subject_id: int, session: int, create: bool = False) Path[source]¶
Get the path for preprocessed (Maxwell-filtered) MEG data.
Structure: derivatives/pyavs/sub-XX/ses-XX/meg/
- get_population_codes_path(parameter_signature: str, subject_id: int, session: int, create: bool = False) Path[source]¶
Get BIDS-compliant path for population codes.
Structure: derivatives/pyavs/population_codes/{signature}/sub-XX/ses-XX/
- get_source_reconstruction_path(subject_id: int, session: int, method: str = 'beamformer', atlas: str = 'glasser', orientation: str = 'normal', hemisphere: str = 'both', filter_spec: str = 'filter_0.2_200', create: bool = False) Path[source]¶
Get the path for source reconstruction data.
Structure: derivatives/pyavs/sub-XX/ses-XX/source/{method}/{atlas}/
- Parameters:
subject_id (int) – Subject ID
session (int) – Session number
method (str) – Reconstruction method (default: ‘beamformer’)
atlas (str) – Brain atlas (default: ‘glasser’)
orientation (str) – Source orientation (default: ‘normal’)
hemisphere (str) – Hemisphere (default: ‘both’)
filter_spec (str) – Filter specification (default: ‘filter_0.2_200’)
- Returns:
BIDS-compliant source reconstruction path
- Return type:
Path
- get_noise_covariance_path(subject_id: int, create: bool = False) Path[source]¶
Get the path for noise covariance matrices.
Structure: derivatives/pyavs/sub-XX/source/noise_covariance/
Noise covariance is estimated from empty-room recordings pooled across sessions, so it lives at the subject level rather than under a session.
- get_filters_path(parameter_signature: str, create: bool = False) Path[source]¶
Get BIDS-compliant path for beamformer filters.
Structure: derivatives/pyavs/filters/{signature}/
- Parameters:
parameter_signature (str) – Unique parameter signature
- Returns:
BIDS-compliant filters path
- Return type:
Path
- get_epochs_path(subject_id: int, session: int, event_type: str = 'saccade', create: bool = False) Path[source]¶
Get the path for epoched data.
Structure: derivatives/pyavs/sub-XX/ses-XX/epochs/
- create_bids_filename(subject_id: int, session: int, task: str = 'avs', datatype: str = 'meg', suffix: str = 'raw-sss', extension: str = '.fif', run: int | None = None, recording: str | None = None, **entities) str[source]¶
Create BIDS-compliant filename.
- Parameters:
subject_id (int) – Subject ID
session (int) – Session number
task (str) – Task name (default: ‘avs’)
datatype (str) – Data type (default: ‘meg’)
suffix (str) – File suffix (default: ‘raw-sss’)
extension (str) – File extension (default: ‘.fif’)
run (int, optional) – Run/block number
recording (str, optional) – Recording type (for empty room)
**entities – Additional BIDS entities
- Returns:
BIDS-compliant filename
- Return type:
- pyavs.utils.derivatives.get_derivatives_manager(data_path: str | None = None) DerivativesManager[source]¶
Get derivatives manager instance.
- pyavs.utils.derivatives.get_bids_preprocessed_path(subject_id: int, session: int, data_path: str | None = None, create: bool = False) Path[source]¶
Get the preprocessed MEG data directory. Pass
create=Trueto make it.
- pyavs.utils.derivatives.get_bids_population_codes_path(parameter_signature: str, subject_id: int, session: int, data_path: str | None = None, create: bool = False) Path[source]¶
Get the population codes directory. Pass
create=Trueto make it.
Logging¶
Centralized logging configuration for pyAVS package.
This module provides a unified logging system for all pyAVS components, allowing for consistent log formatting, levels, and output handling.
- class pyavs.utils.logging.ColoredFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)[source]¶
Bases:
FormatterColored formatter for console output.
- COLORS = {'CRITICAL': '\x1b[35m', 'DEBUG': '\x1b[36m', 'ERROR': '\x1b[31m', 'INFO': '\x1b[32m', 'RESET': '\x1b[0m', 'WARNING': '\x1b[33m'}¶
- format(record)[source]¶
Format the specified record as text.
The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.
- class pyavs.utils.logging.PyAVSLogger[source]¶
Bases:
objectCentralized logger for pyAVS package.
Provides consistent logging across all modules with configurable output levels, formats, and destinations.
- classmethod configure(level: str | int = 'INFO', console: bool = True, file_path: str | Path | None = None, format_string: str | None = None, date_format: str | None = None, use_colors: bool = True, max_file_size: int = 10485760, backup_count: int = 5) None[source]¶
Configure the global logging system for pyAVS.
- Parameters:
level (str or int, optional) – Logging level (‘DEBUG’, ‘INFO’, ‘WARNING’, ‘ERROR’, ‘CRITICAL’) (default: ‘INFO’)
console (bool, optional) – Whether to output to console (default: True)
file_path (str or Path, optional) – Path to log file. If None, no file logging (default: None)
format_string (str, optional) – Custom format string for log messages
date_format (str, optional) – Custom date format for timestamps
use_colors (bool, optional) – Whether to use colored output in console (default: True)
max_file_size (int, optional) – Maximum log file size in bytes before rotation (default: 10MB)
backup_count (int, optional) – Number of backup files to keep during rotation (default: 5)
- classmethod get_logger(name: str) Logger[source]¶
Get a logger instance for a specific module.
- Parameters:
name (str) – Logger name, typically the module name
- Returns:
Logger instance
- Return type:
- pyavs.utils.logging.get_logger(name: str) Logger[source]¶
Get a logger instance (convenience function).
- Parameters:
name (str) – Logger name
- Returns:
Logger instance
- Return type:
- pyavs.utils.logging.configure_logging(**kwargs) None[source]¶
Configure logging system (convenience function).
- Parameters:
**kwargs – Arguments passed to PyAVSLogger.configure()
- pyavs.utils.logging.set_log_level(level: str | int, logger_name: str | None = None) None[source]¶
Set logging level (convenience function).
- class pyavs.utils.logging.temporary_log_level(level: str | int, logger_name: str | None = None)[source]¶
Bases:
objectContext manager to temporarily change log level.
- Usage:
- with temporary_log_level(‘DEBUG’):
# Debug logging enabled logger.debug(“This will be shown”)
- pyavs.utils.logging.log_processing_start(logger: Logger, operation: str, details: Dict[str, Any] | None = None) None[source]¶
Log the start of a processing operation.
- Parameters:
logger (logging.Logger) – Logger instance
operation (str) – Description of the operation
details (dict, optional) – Additional details to log
- pyavs.utils.logging.log_processing_end(logger: Logger, operation: str, success: bool = True, duration: float | None = None, details: Dict[str, Any] | None = None) None[source]¶
Log the end of a processing operation.
- Parameters:
logger (logging.Logger) – Logger instance
operation (str) – Description of the operation
success (bool, optional) – Whether operation was successful (default: True)
duration (float, optional) – Duration in seconds
details (dict, optional) – Additional details to log
Eye-Tracking Utilities¶
Saccade/fixation matching and related eye-tracking helpers.
Eye tracking utilities for pyAVS package.
This module provides utilities for processing and analyzing eye tracking data, including functions for matching saccades to fixations and extracting temporal relationships between eye movement events.
- pyavs.utils.eye_tracking.match_saccades_to_fixations(saccades_meta_df: DataFrame, fixations_meta_df: DataFrame, saccade_type: Literal['pre-saccade', 'post-saccade'] = 'pre-saccade') DataFrame[source]¶
Match saccades to fixations based on temporal adjacency.
This function identifies saccade-fixation pairs by analyzing the temporal sequence of events within each scene. It matches events that occur consecutively with zero time gap between them.
- Parameters:
saccades_meta_df (pd.DataFrame) – Metadata for saccades. Must contain columns: ‘sceneID’, ‘type’, ‘start_time’, ‘end_time’, ‘duration’
fixations_meta_df (pd.DataFrame) – Metadata for fixations. Must contain columns: ‘sceneID’, ‘type’, ‘start_time’, ‘end_time’, ‘duration’, ‘fix_sequence’
saccade_type (Literal["pre-saccade", "post-saccade"], default="pre-saccade") – Type of matching to perform: - “pre-saccade”: Match saccade -> fixation sequences - “post-saccade”: Match fixation -> saccade sequences
- Returns:
Matched saccades with associated fixation information. Includes all original saccade columns plus: - ‘associated_fix_sequence’: Sequence number of matched fixation - ‘associated_fix_start_time’: Start time of matched fixation - ‘associated_fixation_duration’: Duration of matched fixation
- Return type:
pd.DataFrame
Notes
Only pairs with exactly 0 time difference between consecutive events are included (i.e., saccade.end_time == fixation.start_time for pre-saccade, or fixation.end_time == saccade.start_time for post-saccade).
Examples
>>> # Match saccades to subsequent fixations >>> matched_df = match_saccades_to_fixations( ... saccades_df, fixations_df, saccade_type="pre-saccade" ... ) >>> >>> # Access matched fixation durations >>> fixation_durations = matched_df['associated_fixation_duration']