Skip to content

Vectorize bandpass filtering in NoisyChannels._get_filtered_data - #195

Merged
sappelhoff merged 2 commits into
mainfrom
perf/vectorize-filtfilt
Jun 15, 2026
Merged

sappelhoff merged 2 commits into
mainfrom
perf/vectorize-filtfilt

Conversation

@sappelhoff

Copy link
Copy Markdown
Owner

Summary

Vectorize the bandpass filter in NoisyChannels._get_filtered_data by filtering all channels at once with scipy.signal.filtfilt(..., axis=1) instead of looping filtfilt over each channel into a pre-allocated np.zeros_like buffer.

The per-channel loop recomputed the FIR initial conditions (lfilter_zi → linalg.solve) once per channel; axis=1 computes them once and runs the filter in C across all rows.

Why

_get_filtered_data dominates the runtime of find_bad_by_correlation (and any path that needs the filtered signal). On an 18.5-min, 99-channel @ 500 Hz recording:

  • filtfilt step: 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×)
  • It also drops the explicit full-size np.zeros_like output buffer.

Correctness

Bit-for-bit identical output — filtfilt along axis=1 applies the same per-row algorithm, and lfilter_zi depends only on the filter coefficients, not the data:

  • np.array_equal with the old per-channel loop, maxdiff 0.0, on the real recording and random arrays of shapes (64, 9601), (10, 1000), (1, 5000), (99, 553759).
  • Full test suite passes (47 passed), including tests/test_matprep_compare.py (numeric MATLAB-PREP equivalence).
  • get_bads(as_dict=True) and all _extra_info arrays unchanged on the real recording (reject "omit" and None) and the eegbci fixture (matlab_strict False and True).

@codecov

codecov Bot commented Jun 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.85%. Comparing base (54e2554) to head (cf6173f).
⚠️ Report is 21 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sappelhoff
sappelhoff merged commit 24530cb into main Jun 15, 2026
13 of 14 checks passed
@sappelhoff
sappelhoff deleted the perf/vectorize-filtfilt branch June 15, 2026 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant