meeg_utils.preprocessing.pipeline#

Preprocessing pipeline for single MEG/EEG datasets.

This module provides the main PreprocessingPipeline class for processing individual MEG/EEG datasets with filtering, bad channel detection, line noise removal, and ICA artifact removal.

Classes

PreprocessingPipeline(input_path[, ...])

Main preprocessing pipeline for MEG/EEG data.

class meeg_utils.preprocessing.pipeline.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

n_jobs#

Number of parallel jobs or “cuda”.

Type:

int | str

random_state#

Random seed.

Type:

int

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:
  • highpass (float, optional) – High-pass filter frequency in Hz. Default is 0.1.

  • lowpass (float, optional) – Low-pass filter frequency in Hz. Default is 100.0.

  • sfreq (float, optional) – Target sampling frequency in Hz. Default is 250.0.

Returns:

Filtered and resampled raw data.

Return type:

BaseRaw

Raises:
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:
  • fix (bool, optional) – Whether to interpolate bad channels. Default is True.

  • reset_bads (bool, optional) – Whether to reset bads list after interpolation. Default is True.

  • origin (tuple, optional) – Origin for MEG interpolation. Default is (0.0, 0.0, 0.04).

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

save(filename=None)[source]#

Save preprocessed data.

Parameters:

filename (str | Path | None, optional) – Output filename. If None, generates BIDS-compliant name.