Skip to content

Run PREP in stages (backward-compatible), reviving #99 - #191

Merged
sappelhoff merged 7 commits into
mainfrom
prep-stages-73
Jun 13, 2026
Merged

sappelhoff merged 7 commits into
mainfrom
prep-stages-73

Conversation

@sappelhoff

@sappelhoff sappelhoff commented Jun 13, 2026 •

Copy link
Copy Markdown
Owner

PR Description

This implements PREP-in-stages and optional final interpolation, fully backward compatibly. It revives and modernizes the work in the now-closed #99 by @a-hurst — this PR supersedes #99 (which could not be merged: it was based on 2021-era code, carried breaking changes, and was never finished).

Credit: the two core commits are cherry-picked (with -x provenance) from Austin Hurst's original branch, preserving him as their git author; the remaining commits carry Co-authored-by: Austin Hurst trailers.

What's new (all additive)

  • PrepPipeline.remove_line_noise(line_freqs=None) and PrepPipeline.robust_reference(max_iterations=None, interpolate_bads=True) — the individual PREP stages that fit() runs in sequence. Running them manually is identical to fit() (verified by a test).
  • interpolate_bads=False on robust_reference() (and Reference.perform_reference()), plus a standalone Reference.interpolate_bads() building block, to skip the final bad-channel interpolation and inspect/handle the bads yourself.
  • robust_reference() emits a UserWarning if line-noise removal wasn't done first (suppressed inside fit(), including the empty-line_freqs case).

Backward compatibility

This is the central design constraint:

  • fit() is byte-identical to before (verified: a manual staged run equals fit()).
  • No public attribute was renamed or removed (EEG_new/EEG_clean/EEG, noisy_channels_*, bad_before_interpolation, still_noisy_channels, reference_*, interpolated_channels all unchanged).
  • filter_kwargs semantics preserved (e.g. method="fir" still works) — the existing test_prep_pipeline_filter_kwargs and the full test_matprep_compare.py MATLAB-strict suite pass unchanged.

A latent bug in the original #99 guard was also fixed: it raised when robust referencing found zero bad channels (an empty list); the guards now use is None sentinels.

Intentionally deferred (out of scope, would be breaking)

The more ambitious parts of #99 are not included here, to keep this PR non-breaking. If wanted, they should go through an explicit deprecation cycle later:

  • folding the loose noisy_channels_* / bad-channel attributes into noisy_info / bad_channels dicts;
  • get_raw(stage=...) and the current_* getters;
  • dropping notch-filter methods other than spectrum_fit.

Tests / docs

  • New tests: staged == fit(); interpolate_bads=False path; the out-of-order warning fires/doesn't; Reference.interpolate_bads guards. Existing tests pass unchanged (the BC proof).
  • New gallery example examples/run_prep_in_stages.py.
  • Changelog entry under 0.7.0.

🤖 Generated with Claude Code

a-hurst and others added 6 commits June 13, 2026 13:00
Adapt the staged-pipeline methods cherry-picked from Austin Hurst's
work (gh #99) so that no existing public behavior changes:

- remove_line_noise: restore the EEG_new/EEG_clean/EEG attributes and
  the original filter_kwargs semantics (None -> spectrum_fit defaults,
  otherwise use the kwargs verbatim, so e.g. method="fir" still works).
  line_freqs now defaults to prep_params["line_freqs"].
- robust_reference: max_iterations defaults to prep_params; warn with a
  UserWarning if line-noise removal was not performed first. fit() marks
  the line-noise stage as done even when line_freqs is empty, so the
  default pipeline never emits the warning.
- Reference.interpolate_bads: use `is None`/`is not None` sentinels in the
  guards so that finding zero bad channels (an empty list) no longer
  raises during the default interpolating path.

Co-authored-by: Austin Hurst <[email protected]>
- test_prep_pipeline_stages_match_fit: manual remove_line_noise +
  robust_reference is identical to fit().
- test_prep_pipeline_skip_interpolation: interpolate_bads=False leaves the
  post-interpolation outputs uncomputed while still detecting bads.
- test_robust_reference_warns_without_line_noise / _without_line_freqs: the
  out-of-order UserWarning fires when expected and never during fit().
- test_interpolate_bads_guards: Reference.interpolate_bads raises before
  referencing and on a repeated call.

Co-authored-by: Austin Hurst <[email protected]>
Normalize formatting of the cherry-picked and newly added code to satisfy
the project's ruff-format gate.

Co-authored-by: Austin Hurst <[email protected]>
Add an examples/run_prep_in_stages.py gallery example demonstrating the new
remove_line_noise / robust_reference staged API and skipping interpolation
via interpolate_bads=False, plus a changelog entry under 0.7.0.

Co-authored-by: Austin Hurst <[email protected]>
@codecov

codecov Bot commented Jun 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.85%. Comparing base (fd11a2c) to head (39c1f2a).
⚠️ Report is 25 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #191      +/-   ##
==========================================
+ Coverage   97.75%   97.85%   +0.10%     
==========================================
  Files           7        7              
  Lines         802      840      +38     
==========================================
+ Hits          784      822      +38     
  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.

The changelog convention cites the PR number; the staged-PREP entry
mistakenly cited the issue number (#73) instead of the PR (#191).

Co-authored-by: Austin Hurst <[email protected]>
@sappelhoff
sappelhoff enabled auto-merge (squash) June 13, 2026 11:46
@sappelhoff
sappelhoff merged commit a80b371 into main Jun 13, 2026
12 of 13 checks passed
@sappelhoff
sappelhoff deleted the prep-stages-73 branch June 13, 2026 11:50
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.

Extend PrepPipeline to allow performing PREP in stages

2 participants