Use direct NumPy MAD in the windowed correlation loop#197
Open
sappelhoff wants to merge 2 commits into
Open
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #197 +/- ##
=======================================
Coverage 97.86% 97.86%
=======================================
Files 7 7
Lines 842 844 +2
=======================================
+ Hits 824 826 +2
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
Replace the two
scipy.stats.median_abs_deviation(..., axis=1)calls in the windowed loop offind_bad_by_correlationwith the equivalent direct NumPy expressionnp.median(np.abs(x - np.median(x, axis=1, keepdims=True)), axis=1).Why
median_abs_deviationcarries ascipyaxis_nan_policywrapper (broadcast/concatenate bookkeeping) that adds noticeable overhead when called many times on small arrays — here roughly 2× per correlation window (~1100 windows). On an 18.5-min, 99-channel @ 500 Hz recording:find_bad_by_correlation: 5.06 s → 4.51 s (on top of Vectorize bandpass filtering in NoisyChannels._get_filtered_data #195)Correctness
The default
median_abs_deviation(scale1.0, center = median) is exactlymedian(|x − median(x)|):np.array_equal, maxdiff0.0againstmedian_abs_deviationin a direct micro-check.tests/test_matprep_compare.py.get_bads(as_dict=True)and all_extra_infoarrays (including the per-windowmax_correlations,noise_levels,channel_amplitudes) unchanged on the real recording (reject"omit"/None) and the eegbci fixture (matlab_strictFalse/True).Note: the same swap would also speed up
find_bad_by_nan_flatandfind_bad_by_hfnoise; kept out here to keep the diff minimal — happy to follow up if wanted.