Vectorize bandpass filtering in NoisyChannels._get_filtered_data - #195
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #195 +/- ##
==========================================
- Coverage 97.86% 97.85% -0.01%
==========================================
Files 7 7
Lines 842 839 -3
==========================================
- Hits 824 821 -3
Misses 18 18 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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
Vectorize the bandpass filter in
NoisyChannels._get_filtered_databy filtering all channels at once withscipy.signal.filtfilt(..., axis=1)instead of loopingfiltfiltover each channel into a pre-allocatednp.zeros_likebuffer.The per-channel loop recomputed the FIR initial conditions (
lfilter_zi→linalg.solve) once per channel;axis=1computes them once and runs the filter in C across all rows.Why
_get_filtered_datadominates the runtime offind_bad_by_correlation(and any path that needs the filtered signal). On an 18.5-min, 99-channel @ 500 Hz recording:filtfiltstep: 9.98 s → 2.40 s (4.16×) (clean wall-clock, median of 5)find_bad_by_correlation: 12.14 s → 5.06 s (2.40×)np.zeros_likeoutput buffer.Correctness
Bit-for-bit identical output —
filtfiltalongaxis=1applies the same per-row algorithm, andlfilter_zidepends only on the filter coefficients, not the data:np.array_equalwith the old per-channel loop, maxdiff0.0, on the real recording and random arrays of shapes(64, 9601),(10, 1000),(1, 5000),(99, 553759).tests/test_matprep_compare.py(numeric MATLAB-PREP equivalence).get_bads(as_dict=True)and all_extra_infoarrays unchanged on the real recording (reject"omit"andNone) and the eegbci fixture (matlab_strictFalseandTrue).