fix: apply safe standalone MNE preprocessors via callable (#885)#1037
Open
YihengLi-1 wants to merge 1 commit into
Open
fix: apply safe standalone MNE preprocessors via callable (#885)#1037YihengLi-1 wants to merge 1 commit into
YihengLi-1 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #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.