Detrend in place in NoisyChannels.__init__ to avoid full-array copies#196
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #196 +/- ##
=======================================
Coverage 97.85% 97.85%
=======================================
Files 7 7
Lines 839 839
=======================================
Hits 821 821
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
Detrend the EEG signal in place during
NoisyChannels.__init__, avoiding two transient full-size copies of the recording during construction.removeTrendgains acopy=Truekeyword argument that is passed through tomne.filter.filter_data. The default (copy=True) preserves the exact current behavior for every existing caller.NoisyChannels.__init__now callsremoveTrend(self.raw_mne._data, ..., copy=False)instead ofremoveTrend(self.raw_mne.get_data(), ...).Why
The previous code allocated two avoidable full-array copies while detrending:
get_data()always returns a copy (verified: it does not share memory with_data).mne.filter.filter_data(copy=True)allocates a separate output buffer.Filtering in place removes both. On an 18.5-min, 99-channel @ 500 Hz recording (~439 MB),
filter_datapeaks at +887 MB → +443 MB (one full-array transient removed), and__init__drops from ~1.94 s to ~1.77 s.The
matlab_strictpath already filters in place, socopyonly affects the default (MNE high-pass) branch.Correctness
mne.filter.filter_data(copy=False)is bit-identical tocopy=True(verifiednp.array_equal);copyonly controls whether MNE allocates a new array.tests/test_matprep_compare.py.get_bads(as_dict=True)and all_extra_infoarrays unchanged on the real recording (reject"omit"/None) and the eegbci fixture (matlab_strictFalse/True).