Repository navigation
fix: apply safe standalone MNE preprocessors via callable (#885) - #1037
adammounir merged 4 commits into
Conversation
|
@adammounir, did you have the chance to replicate the error and review this PR? |
|
Hi @YihengLi-1 @bruAristimunha — I took an independent look while reading around #885. A few data points that might help move this forward: Reproduced on current import mne, numpy as np
from braindecode.preprocessing import FilterData
info = mne.create_info(["Cz", "Pz"], sfreq=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)
print(FilterData.fn) # <function filter_data at 0x...> -> class attr is the callable (correct)
print(fd.fn) # 'filter_data' -> instance attr shadows it (the bug)
fd.apply(raw) # AttributeError: MNE object does not have a filter_data method.So the root cause is exactly as described: the generated One small thing: since this intentionally fixes 3 of the 19 (the rest split into separate categories — auxiliary-data returns, second-instance inputs, array funcs — each needing its own design), the Happy to pick up a follow-up for one of the remaining categories (the auxiliary-data group looks like the meatiest) if useful. |
adammounir
left a comment
There was a problem hiding this comment.
Thanks @YihengLi-1,
I reproduced the bug on master and validated this branch locally:
The 3 safe classes now expose a callable fn, while the auxiliary-data ones (FilterData, AnnotateAmplitude, FindBadChannelsLof) correctly stay on the string path, safely guarded by test_unsafe_standalone_preprocessor_keeps_string_fn.
This conservative allow-list + guard-test approach is good, as it avoids the footgun of silently replacing a recording with a tuple or list.
Picking up @bkowshik 's point, since this fixes 3 of the 19, I changed the description from Fixes #885 to Addresses #885 and opened #1055 to track the rest so #885 isn't auto-closed on merge. LGTM, thanks for the contribution !
Summary
Addresses #885 for the subset of standalone-function preprocessors that can be fixed safely.
The auto-generated standalone-function preprocessors in
mne_preprocess.pypassed the function name (a string) toPreprocessorinstead 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
fnis a callable andapply_on_array=False,Preprocessor.applyreassigns the dataset's recording to whateverfnreturns. Several of these MNE functions return auxiliary data rather than the modified recording — e.g.annotate_amplitudereturns a(annotations, bads)tuple,find_bad_channels_lofreturns 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
_SAFE_STANDALONE_FUNCTIONSof 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)I verified the classification empirically (constructing each class, applying it to a Raw, and checking the return type). The remaining standalone functions fall into:
annotate_amplitude,annotate_nan,compute_bridged_electrodes,find_bad_channels_lof, standaloneset_eeg_reference, …equalize_bads,equalize_channels,realign_raw.filter_data) — already documented to useFilterinstead.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.fnis the callable (not a string) and thatapplyreturns aRaw.test_safe_standalone_preprocessor_runs_in_pipeline— runsComputeCurrentSourceDensityend-to-end throughpreprocess.test_unsafe_standalone_preprocessor_keeps_string_fn— guard:AnnotateAmplitude.fnstays the string"annotate_amplitude", so a future change can't silently turn an auxiliary-data function into a data-corrupting callable.test_compute_csd, which now passes.Full file: 109 passed, 14 skipped, 0 failed.