Skip to content

[Detail Bug] Standalone MNE preprocessor classes pass string name to Preprocessor, breaking 19 public preprocessors #885

Closed

Description

@detail-app

Detail Bug Report

https://app.detail.dev/org_af056a4d-4057-4d23-a697-93536d2e2d5d/bugs/bug_61351e45-eaaf-4581-8ade-9191bafbe169

Summary

  • Context: The braindecode/preprocessing/__init__.py file imports and exposes 19 automatically-generated preprocessor classes (like FilterData, 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 Preprocessor class.
  • Actual vs. expected: When a user creates an instance like FilterData(sfreq=100, l_freq=1, h_freq=40), the instance's fn attribute becomes the string 'filter_data' instead of the actual mne.filter.filter_data function, 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 an AttributeError when 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_method

Example

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_method

Activity

  1. bruAristimunha commented on Dec 19, 2025

    @bruAristimunha
    Collaborator

    I am not super convinced here, because we have test that cover this

  2. adammounir commented on Jun 18, 2026

    @adammounir
    Collaborator

    Hi @bruAristimunha , I looked into this.

    The bug is real (I reproduced it on master):

    • The Cause: For the standalone-function preprocessors, the instance attribute fn is the string name (e.g., FilterData().fn == 'filter_data').
    • The Effect: This shadows the correct class-level callable, so apply routes down the "string = Raw method name" path and raises:

      AttributeError: MNE object does not have a filter_data method.

    The coverage is only apparent because the broken code path is never actually executed:

    • They list these classes, but every standalone function is pytest.mark.skip (e.g., FilterData → "low-level function, use Filter instead", ComputeCurrentSourceDensity → "requires montage setup", FindBadChannelsLof, SetBipolarReference, EqualizeBads, …).
    • test_compute_csd started with pytest.skip(...).

    The 19 are not homogeneous and each needs its own design:


    Next Steps & Suggestion

  3. bruAristimunha commented on Jun 18, 2026

    @bruAristimunha
    Collaborator

    Amazing @adammounir

  4. added 2 commits that reference this issue on Jun 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions