meeg_utils.preprocessing#
Preprocessing module for MEG/EEG data.
This module provides preprocessing pipelines for MEG and EEG data, including filtering, bad channel detection, line noise removal, and ICA artifact removal.
- class meeg_utils.preprocessing.BatchPreprocessingPipeline(input_paths, output_dir=None, n_jobs=1, use_cuda=False, random_state=42)[source]#
Batch preprocessing pipeline for multiple MEG/EEG datasets.
- Parameters:
input_paths (list[str | Path | BIDSPath]) – List of input paths to process.
output_dir (str | Path | None, optional) – Output directory for all datasets. If None, uses BIDS derivatives.
n_jobs (int, optional) – Number of parallel jobs. Default is 1.
use_cuda (bool, optional) – Whether to use CUDA. Default is False.
random_state (int, optional) – Random seed. Default is 42.
- output_dir#
Output directory.
- Type:
Path | None
- __init__(input_paths, output_dir=None, n_jobs=1, use_cuda=False, random_state=42)[source]#
Initialize batch preprocessing pipeline.
- run(filter_params=None, detect_bad_channels=True, remove_line_noise=True, apply_ica=True, ica_params=None, save_intermediate=False, skip_existing=False, save_logs=True, logging_level='INFO')[source]#
Run batch preprocessing on all datasets.
- Parameters:
filter_params (dict | None, optional) – Filtering parameters.
detect_bad_channels (bool, optional) – Whether to detect bad channels. Default is True.
remove_line_noise (bool, optional) – Whether to remove line noise. Default is True.
apply_ica (bool, optional) – Whether to apply ICA. Default is True.
ica_params (dict | None, optional) – ICA parameters.
save_intermediate (bool, optional) – Whether to save intermediate files. Default is False.
skip_existing (bool, optional) – Whether to skip datasets with existing output. Default is False.
save_logs (bool, optional) – Whether to save log files. Default is True.
logging_level (str, optional) – Logging level for the batch process. Default is “INFO”.
- class meeg_utils.preprocessing.PreprocessingPipeline(input_path, output_dir=None, n_jobs=1, use_cuda=False, random_state=42)[source]#
Main preprocessing pipeline for MEG/EEG data.
This class provides a comprehensive preprocessing pipeline including: - Flexible input path parsing (string, Path, BIDSPath) - Data loading with automatic datatype inference - Filtering and resampling - Bad channel detection and interpolation - Line noise removal (zapline) - ICA artifact removal with automatic/manual labeling
- Parameters:
input_path (str | Path | BIDSPath | BaseRaw) – Path to the input data, or a Raw object directly. Can be a plain string, pathlib.Path, mne_bids.BIDSPath object, or an MNE Raw object (useful for testing).
output_dir (str | Path | None, optional) – Directory for saving outputs. If None, uses BIDS derivatives.
n_jobs (int, optional) – Number of parallel jobs. Default is 1.
use_cuda (bool, optional) – Whether to use CUDA acceleration. Default is False.
random_state (int, optional) – Random seed for reproducibility. Default is 42.
- input_path#
Parsed input path (None if Raw was provided directly).
- Type:
Path | BIDSPath | None
- output_dir#
Output directory path.
- Type:
Path
- raw#
Loaded raw data.
- Type:
BaseRaw | None
- datatype#
Inferred datatype (“eeg” or “meg”).
- Type:
DType | None
- __init__(input_path, output_dir=None, n_jobs=1, use_cuda=False, random_state=42)[source]#
Initialize preprocessing pipeline.
- load_data()[source]#
Load data from input path.
- Returns:
Loaded raw data.
- Return type:
BaseRaw
- Raises:
ValueError – If data cannot be loaded.
- filter_and_resample(highpass=0.1, lowpass=100.0, sfreq=250.0)[source]#
Apply bandpass filter and resample data.
- Parameters:
- Returns:
Filtered and resampled raw data.
- Return type:
BaseRaw
- Raises:
AssertionError – If filter parameters are invalid.
ValueError – If data is not loaded.
- detect_and_fix_bad_channels(fix=True, reset_bads=True, origin=(0.0, 0.0, 0.04))[source]#
Detect and optionally interpolate bad channels.
- Parameters:
- Returns:
Raw data with bad channels marked/fixed.
- Return type:
BaseRaw
- Raises:
ValueError – If data not loaded or datatype not inferred.
- remove_line_noise(fline=50.0)[source]#
Remove power line noise using zapline method.
- Parameters:
fline (float, optional) – Power line frequency in Hz. Default is 50.0.
- Returns:
Raw data with line noise removed.
- Return type:
BaseRaw
- Raises:
ValueError – If data not loaded or datatype not inferred.
- apply_ica(n_components=None, method='infomax', regress=True, manual_labels=None)[source]#
Apply ICA for artifact removal.
- Parameters:
n_components (int | None, optional) – Number of ICA components. If None, uses default (20 for EEG, 40 for MEG).
method (str, optional) – ICA method. Default is “infomax”.
regress (bool, optional) – Whether to regress out artifact components. Default is False.
manual_labels (list[str] | None, optional) – Manual labels for ICA components. If None, uses automatic labeling.
- Returns:
Raw data with ICA applied (if regress=True).
- Return type:
BaseRaw
- Raises:
ValueError – If data not loaded or datatype not inferred.
- run(filter_params=None, detect_bad_channels=True, remove_line_noise=True, apply_ica=True, ica_params=None, save_intermediate=False)[source]#
Run complete preprocessing pipeline.
- Parameters:
filter_params (dict | None, optional) – Filtering parameters. Keys: highpass, lowpass, sfreq.
detect_bad_channels (bool, optional) – Whether to detect and fix bad channels. Default is True.
remove_line_noise (bool, optional) – Whether to remove line noise. Default is True.
apply_ica (bool, optional) – Whether to apply ICA. Default is True.
ica_params (dict | None, optional) – ICA parameters. Keys: n_components, method, regress.
save_intermediate (bool, optional) – Whether to save intermediate files. Default is False.
- Returns:
Preprocessed raw data.
- Return type:
BaseRaw
Modules
Bad channel detection for EEG and MEG data. |
|
Batch preprocessing for multiple MEG/EEG datasets. |
|
ICA-based artifact removal for EEG and MEG data. |
|
Line noise removal using zapline methods. |
|
Preprocessing pipeline for single MEG/EEG datasets. |