Skip to content

fix: apply safe standalone MNE preprocessors via callable (#885)#1037

Open
YihengLi-1 wants to merge 1 commit into
braindecode:masterfrom
YihengLi-1:fix-standalone-preprocessor-a-class-885
Open

fix: apply safe standalone MNE preprocessors via callable (#885)#1037
YihengLi-1 wants to merge 1 commit into
braindecode:masterfrom
YihengLi-1:fix-standalone-preprocessor-a-class-885

Conversation

@YihengLi-1
Copy link
Copy Markdown

Summary

Fixes #885 for the subset of standalone-function preprocessors that can be fixed safely.

The auto-generated standalone-function preprocessors in mne_preprocess.py passed the function name (a string) to Preprocessor instead of the callable, so they never actually applied their wrapped MNE function. #885 lists 19 affected classes.

However, a blanket "pass the callable" fix is unsafe. When fn is a callable and apply_on_array=False, Preprocessor.apply reassigns the dataset's recording to whatever fn returns. Several of these MNE functions return auxiliary data rather than the modified recording — e.g. annotate_amplitude returns a (annotations, bads) tuple, find_bad_channels_lof returns a list, set_eeg_reference (standalone) returns a (raw, ref_data) tuple. Wrapping those as callables would silently replace the recording with that auxiliary object — worse than the current failure.

What this PR does

  • Introduces a small, explicit allow-list _SAFE_STANDALONE_FUNCTIONS of standalone functions verified to return the modified instance, and routes only those through the callable path. Currently three functions:
    • ComputeCurrentSourceDensity (compute_current_source_density)
    • SetBipolarReference (set_bipolar_reference)
    • OversampledTemporalProjection (oversampled_temporal_projection)
  • Every other standalone function keeps its existing behaviour unchanged.

I verified the classification empirically (constructing each class, applying it to a Raw, and checking the return type). The remaining standalone functions fall into:

  • Return auxiliary data (tuple/list/Annotations) — unsafe to pass as a callable as-is: annotate_amplitude, annotate_nan, compute_bridged_electrodes, find_bad_channels_lof, standalone set_eeg_reference, …
  • Need a second instance / list input (don't fit the single-recording preprocessor model): equalize_bads, equalize_channels, realign_raw.
  • Operate on arrays (filter_data) — already documented to use Filter instead.

These need their own handling/design and are intentionally out of scope here.

Tests

  • test_safe_standalone_preprocessor_uses_callable — parametrized over the three safe classes: asserts .fn is the callable (not a string) and that apply returns a Raw.
  • test_safe_standalone_preprocessor_runs_in_pipeline — runs ComputeCurrentSourceDensity end-to-end through preprocess.
  • test_unsafe_standalone_preprocessor_keeps_string_fn — guard: AnnotateAmplitude.fn stays the string "annotate_amplitude", so a future change can't silently turn an auxiliary-data function into a data-corrupting callable.
  • Un-skipped the existing test_compute_csd, which now passes.

Full file: 109 passed, 14 skipped, 0 failed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant