Repository navigation
Closed
Description
Detail Bug Report
Summary
- Context: The
braindecode/preprocessing/__init__.pyfile imports and exposes 19 automatically-generated preprocessor classes (likeFilterData,AnnotateAmplitude, etc.) that wrap standalone MNE functions. - Bug: These 19 standalone function preprocessors cannot be used because they incorrectly pass the function name as a string instead of the actual function object to the parent
Preprocessorclass. - Actual vs. expected: When a user creates an instance like
FilterData(sfreq=100, l_freq=1, h_freq=40), the instance'sfnattribute becomes the string'filter_data'instead of the actualmne.filter.filter_datafunction, causing the preprocessor to fail when applied to Raw objects. - Impact: 19 publicly documented preprocessors (
FilterData,AnnotateAmplitude,AnnotateBreak,AnnotateMovement,AnnotateMuscleZscore,AnnotateNan,CombineChannels,ComputeBridgedElectrodes,ComputeCurrentSourceDensity,EqualizeBads,EqualizeChannels,FindBadChannelsLof,FixStimArtifact,InterpolateBridgedElectrodes,MaxwellFilter,OversampledTemporalProjection,RealignRaw,RegressArtifact,SetBipolarReference) are completely broken and will fail with anAttributeErrorwhen users try to use them.
Code with bug
# braindecode/preprocessing/mne_preprocess.py
def _generate_init_method(func, force_copy_false=False):
"""
Generate an __init__ method for a class based on the function's signature.
"""
func_name = func.__name__ # func_name is the string name
parameters = list(inspect.signature(func).parameters.values())
# ... parameter processing ...
def init_method(self, *args, **kwargs):
# ... argument validation ...
Preprocessor.__init__(self, fn=func_name, apply_on_array=False, **init_kwargs)
# <-- BUG 🔴 passes func_name (string) instead of func (callable) for standalone functions
return init_methodExample
Minimal reproduction using FilterData (wraps mne.filter.filter_data, a standalone function):
import mne, numpy as np
from braindecode.preprocessing import FilterData
info = mne.create_info(['Cz', 'Pz'], 250, ch_types='eeg')
raw = mne.io.RawArray(np.random.randn(2, 2500), info, verbose=False)
fd = FilterData(sfreq=250, l_freq=1, h_freq=40)
assert isinstance(fd.fn, str) and fd.fn == 'filter_data'
fd.apply(raw)Observed error:
AttributeError: MNE object does not have a filter_data method
Why: Preprocessor.apply() treats fn as a method name and calls raw.filter_data(...); filter_data is a standalone function (mne.filter.filter_data), not a Raw method.
Recommended fix
In braindecode/preprocessing/mne_preprocess.py, ensure the generated __init__ passes the callable for standalone functions and the string name for methods, and propagate is_standalone into the generator:
# pass is_standalone into the init generator
class_attrs = {
"__init__": _generate_init_method(function, force_copy_false, is_standalone), # <-- FIX 🟢
"__doc__": wrapper_note + (function.__doc__ or ""),
"__repr__": _generate_repr_method(class_name),
"fn": function if is_standalone else function.__name__,
"_is_standalone": is_standalone,
}
# update the generator to use is_standalone
def _generate_init_method(func, force_copy_false=False, is_standalone=False):
func_name = func.__name__
parameters = list(inspect.signature(func).parameters.values())
# ...
def init_method(self, *args, **kwargs):
# ...
fn_param = func if is_standalone else func_name # <-- FIX 🟢 use callable for standalone
Preprocessor.__init__(self, fn=fn_param, apply_on_array=False, **init_kwargs)
return init_methodMetadata
Metadata
Assignees
Labels
No labels
Activity
bruAristimunha commentedon Dec 19, 2025
I am not super convinced here, because we have test that cover this
adammounir commentedon Jun 18, 2026
Hi @bruAristimunha , I looked into this.
The bug is real (I reproduced it on master):
fnis the string name (e.g.,FilterData().fn == 'filter_data').applyroutes down the "string = Raw method name" path and raises:The coverage is only apparent because the broken code path is never actually executed:
pytest.mark.skip(e.g.,FilterData→ "low-level function, use Filter instead",ComputeCurrentSourceDensity→ "requires montage setup",FindBadChannelsLof,SetBipolarReference,EqualizeBads, …).test_compute_csdstarted withpytest.skip(...).The 19 are not homogeneous and each needs its own design:
Next Steps & Suggestion
bruAristimunha commentedon Jun 18, 2026
Amazing @adammounir
fix: apply safe standalone MNE preprocessors via callable (#885)
Merge pull request #1037 from YihengLi-1/fix-standalone-preprocessor-…