mne_denoise.zapline.ZapLine#
- class mne_denoise.zapline.ZapLine(sfreq: float, line_freq: float | None = 60.0, n_remove: int | str = 'auto', n_harmonics: int | None = None, nfft: int = 1024, nkeep: int | None = None, rank: int | None = None, reg: float = 1e-09, threshold: float = 3.0, adaptive: bool = False, adaptive_params: dict | None = None)[source]#
ZapLine Transformer for line noise removal.
Implements the ZapLine algorithm [1] for removing power line noise (50/60 Hz) and its harmonics from M/EEG recordings. Inherits from
DSSand is compatible with scikit-learn and MNE-Python objects.The algorithm uses Denoising Source Separation (DSS) to find spatial filters that maximize power at the line noise frequency, then projects out these noise components while preserving neural signals.
The cleaning process follows these steps:
Smoothing: Apply a moving average filter with period matching the line frequency to separate slowly-varying (“smooth”) and fast (“residual”) components
DSS: Apply DSS to the residual using
LineNoiseBiasto find components that maximize line noise powerArtifact removal: Project out the top noise components from the residual
Reconstruction: Add back the smooth component to obtain cleaned data
- Parameters:
sfreq (float) – Sampling frequency in Hz. Required.
line_freq (float | None, default=60.0) – Line noise frequency in Hz (typically 50 or 60 Hz). If
Noneandadaptive=True, the frequency is auto-detected. IfNoneandadaptive=False, raises an error.n_remove (int | 'auto', default='auto') – Number of noise components to remove. If
'auto', uses iterative outlier removal on DSS eigenvalues [1]. Ifint, removes exactly that many components.n_harmonics (int | None, default=None) – Number of harmonics to include in the bias function. If
None, auto-determined based on Nyquist frequency.nfft (int, default=1024) – FFT length for the spectral bias function.
nkeep (int | None, default=None) – Number of PCA components to retain before DSS. If
None, usesrankor auto-determined from data.rank (int | None, default=None) – Rank of the data for whitening. If
None, auto-determined.reg (float, default=1e-9) – Regularization parameter for DSS covariance inversion.
threshold (float, default=3.0) – Sigma threshold for iterative outlier removal when
n_remove='auto'.adaptive (bool, default=False) – If
True, use adaptive ZapLine-plus mode [2] with: - Automatic frequency detection - Data segmentation based on covariance stationarity - Per-segment parameter adaptationadaptive_params (dict | None, default=None) – Parameters for adaptive mode. See Notes for available options.
- filters_#
Spatial filters (unmixing matrix) for the removed noise components.
- Type:
ndarray, shape (n_removed, n_channels)
- patterns_#
Spatial patterns (mixing matrix) for the removed noise components.
- Type:
ndarray, shape (n_channels, n_removed)
- eigenvalues_#
DSS eigenvalues (ratio of line-noise power to total power).
- Type:
ndarray, shape (n_components,)
See also
DSSParent class implementing Denoising Source Separation.
LineNoiseBiasBias function for line noise.
mne_denoise.zapline.adaptiveAdaptive ZapLine-plus utilities.
Notes
Adaptive Mode Parameters (
adaptive_paramsdict):fmin: float (default 17.0) - Minimum frequency for noise detectionfmax: float (default 99.0) - Maximum frequency for noise detectionprocess_harmonics: bool (default False) - Process detected harmonicsmax_harmonics: int - Maximum number of harmonics to processhybrid_fallback: bool (default False) - Use notch filter fallbackmin_chunk_len: float (default 30.0) - Minimum segment length in secondsn_remove_params: dict - Parameters for component selectionqa_params: dict - Parameters for QA (max_sigma, min_sigma)
Examples
Basic usage with MNE Raw object:
>>> from mne_denoise.zapline import ZapLine >>> # Remove 50 Hz line noise >>> zapline = ZapLine(sfreq=1000, line_freq=50.0) >>> raw_clean = zapline.fit_transform(raw)
Using automatic component selection:
>>> zapline = ZapLine(sfreq=1000, line_freq=60.0, n_remove="auto") >>> zapline.fit(data) >>> print(f"Removed {zapline.n_removed_} components") >>> cleaned = zapline.transform(data)
Adaptive mode (ZapLine-plus):
>>> zapline = ZapLine(sfreq=1000, line_freq=None, adaptive=True) >>> cleaned = zapline.fit_transform(epochs) # Auto-detects noise frequencies
References
[1] (1,2)de Cheveigné, A. (2020). ZapLine: A simple and effective method to remove power line artifacts. NeuroImage, 207, 116356.
[2]Klug, M., & Kloosterman, N. A. (2022). Zapline-plus: A Zapline extension for automatic and adaptive removal of frequency-specific noise artifacts in M/EEG. Human Brain Mapping, 43(9), 2743-2758.
- __init__(sfreq: float, line_freq: float | None = 60.0, n_remove: int | str = 'auto', n_harmonics: int | None = None, nfft: int = 1024, nkeep: int | None = None, rank: int | None = None, reg: float = 1e-09, threshold: float = 3.0, adaptive: bool = False, adaptive_params: dict | None = None)[source]#
Methods
__init__(sfreq[, line_freq, n_remove, ...])fit(X[, y])Fit ZapLine spatial filters to data.
fit_transform(X[, y])Fit and transform data in one step.
get_metadata_routing()Get metadata routing of this object.
get_normalized_patterns()Get L2-normalized spatial patterns for visualization.
get_params([deep])Get parameters for this estimator.
inverse_transform(sources[, component_indices])Transform sources back to sensor space.
set_fit_request(*[, weights])Configure whether metadata should be requested to be passed to the
fitmethod.set_inverse_transform_request(*[, ...])Configure whether metadata should be requested to be passed to the
inverse_transformmethod.set_output(*[, transform])Set output container.
set_params(**params)Set the parameters of this estimator.
transform(X)Apply ZapLine cleaning to remove line noise.