Skip to content

fix: filter clean EEG intervals separately around BAD annotations - #224

Open
viranovskaya wants to merge 1 commit into
sappelhoff:mainfrom
viranovskaya:fix/bad-segments-223
Open

viranovskaya wants to merge 1 commit into
sappelhoff:mainfrom
viranovskaya:fix/bad-segments-223

Conversation

@viranovskaya

Copy link
Copy Markdown
Contributor

PR Description

Closes #223.

With reject_by_annotation="omit", detrending the entire recording could spread NaNs from a BAD interval into otherwise usable EEG channels before channel assessment. This change detrends the retained intervals separately in Reference and applies line-noise removal separately in PrepPipeline. BAD samples are left untouched by line-noise removal. If a clean interval is too short for the multitaper notch, it is retained and a warning is emitted rather than filtering across the gap.

The whole-recording path is preserved when there is no interval to split and the notch can run normally.

Runnable example: a two-second gap shared by 32 EEG channels

This example uses the same annotated NaN gap for both entry points. The EEG channels share signal components but have independent noise, so channel rejection is not driven by independent white noise. Explicit channel names work with both APIs.

import mne
import numpy as np
from pyprep import PrepPipeline, Reference

montage = mne.channels.make_standard_montage("spherical_1005")
names = montage.ch_names[:32]
t = np.arange(7500) / 250
phase = np.linspace(0, 2 * np.pi, len(names), endpoint=False)[:, None]
rng = np.random.default_rng(223)
data = 1e-5 * (
    np.sin(2 * np.pi * 9 * t + phase)
    + 0.5 * np.sin(2 * np.pi * 13 * t - phase)
    + 0.1 * rng.standard_normal((len(names), len(t)))
)
data[:, 3000:3500] = np.nan
raw = mne.io.RawArray(data, mne.create_info(names, 250, "eeg"))
raw.set_montage(montage)
raw.set_annotations(mne.preprocessing.annotate_nan(raw))

params = dict(ref_chs=names, reref_chs=names, line_freqs=[50])
opts = dict(ransac=False, reject_by_annotation="omit", random_state=223)
reference = Reference(raw.copy(), params.copy(), **opts).perform_reference()
pipeline = PrepPipeline(raw.copy(), params.copy(), montage, **opts).fit()
for result in (reference, pipeline):
    assert result.noisy_channels_original["bad_by_nan"] == []
    assert np.isfinite(result.raw.get_data(reject_by_annotation="omit")).all()
    assert np.isnan(result.raw.get_data(start=3000, stop=3500)).all()

AI assistance: OpenAI Codex assisted with implementation, regression tests, and review.

Merge Checklist

  • the PR has been reviewed and all comments are resolved
  • all CI checks pass
  • (if applicable): the PR description includes the phrase closes #<issue-number> to automatically close an issue
  • (if applicable): the changes are documented in the changelog changelog.rst
  • (if applicable): new contributors have added themselves to the authors list in the CITATION.cff file

@codecov

codecov Bot commented Nov 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.28%. Comparing base (9300503) to head (87ea203).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #224      +/-   ##
==========================================
+ Coverage   98.21%   98.28%   +0.07%     
==========================================
  Files           8        8              
  Lines         894      932      +38     
==========================================
+ Hits          878      916      +38     
  Misses         16       16              

☔ 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.

@viranovskaya
viranovskaya marked this pull request as ready for review November 29, 2026 14:25

This branch has not been deployed

No deployments
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.

Reference and PrepPipeline fail on NaNs in BAD segments with reject_by_annotation="omit"

1 participant