Warn when Epochs events fall outside the raw data range (#12989)#14004
Conversation
|
Thanks for opening this PR @CedricConday! I agree with the discussion in the issue that there should be an option to control whether a warning is emitted. That would also be in line with how we handle the similar situation for setting annotations in Lines 714 to 719 in 90b8cee |
Mirrors the on_missing pattern used for out-of-range Raw annotations (_on_missing helper): 'raise' | 'warn' (default) | 'ignore'. Addresses maintainer review on mne-toolsgh-14004.
|
Thanks @tsbinns! Added an |
tsbinns
left a comment
There was a problem hiding this comment.
Thanks for adding the extra control @CedricConday!
Just a few comments.
|
@CedricConday Please also link your GitHub account to CircleCI so that the documentation build will run, and we can verify that looks good. |
Events whose epoch window lies (partly) outside the raw data are silently dropped, which is surprising e.g. when events come from a different sampling frequency or contain samples before first_samp. Warn at construction listing how many events are out of bounds. Adds a regression test.
Warn only when an event's sample number falls outside the recorded data ([first_samp, first_samp + n_times)) — the issue's actual scenario (events at a different sampling frequency, or before first_samp). The earlier check on the epoch *window* also fired for in-range events whose window merely clips the edge (wide tmax / negative tmin on short data), which is normal and broke several existing tests. Full test_epochs.py is green under -W error.
for more information, see https://pre-commit.ci
Mirrors the on_missing pattern used for out-of-range Raw annotations (_on_missing helper): 'raise' | 'warn' (default) | 'ignore'. Addresses maintainer review on mne-toolsgh-14004.
…x test - on_outside made keyword-only in both Epochs and EpochsArray - Warning logic moved from Epochs to BaseEpochs._oob_check for reuse - EpochsArray now accepts and forwards on_outside to BaseEpochs - Test uses pytest.raises instead of pytest.warns per mne warnings-as-errors policy
…-as-errors, add missing BaseEpochs on_outside doc
…event file (mne-toolsgh-12989) test_warnings and test_n_components_none pass the complete event file against a shorter/cropped raw, so some events fall past the data and are dropped — expected here. Pass on_outside='ignore' at those call sites so the new default warning does not trip warnings-as-errors. No behavior change to the tests' actual assertions.
ed3a816 to
11f6a3b
Compare
for more information, see https://pre-commit.ci
|
Thanks for the review @tsbinns — I've addressed all of it: |
…ne-toolsgh-12989) test_n_components_none crops raw to (1.5, stop) but reads the full event file, so some events fall past the cropped data and are dropped as expected. Pass on_outside='ignore' at that call site so the new default warning does not trip warnings-as-errors. A prior commit fixed the two sibling ICA tests but missed this one. Test-only; no behavior change.
|
@CedricConday Thanks for updates. Just to let you know that I haven't forgotten about this, but I won't be able to review until the weekend. |
There was a problem hiding this comment.
@CedricConday Just a few small things.
This comment remains unadressed, so please have a look at that: #14004 (comment)
| self.detrend = detrend | ||
|
|
||
| self._raw = raw | ||
| self._oob_check(on_outside) |
There was a problem hiding this comment.
This can be moved into the if events is not None block above, since it is only relevant in this case.
- Changelog: mark as newfeature (not bugfix) and reword - Move the out-of-bounds check into the events-not-None branch, after selection has been applied, and pass raw explicitly - Reword the warning to describe the data range rather than sample numbers, and say the epoch cannot be created (not "dropped") - Rename _oob_check -> _check_events_outside_data - DRY the on_outside docstring via a new docdict entry (on_outside_epochs) - Make on_outside keyword-only in BaseEpochs - Test: match the new wording and drop the redundant catch_warnings, since warnings are already treated as errors in the suite
|
I addressed all 6 issues you mentioned. |
test_docdict_order requires docdict keys to appear in sorted order in the source; place on_outside_epochs between on_missing_montage and on_rank_mismatch.
|
Yes it seems reasonable! But I think you should push a commit with |
|
Thanks @CedricConday! |
* upstream/main: Make scrollbar handlers draggable (mne-tools#14040) [pre-commit.ci] pre-commit autoupdate (mne-tools#14052) Warn when Epochs events fall outside the raw data range (mne-tools#12989) (mne-tools#14004) Ensure epochs being concatenated have compatible event ids (mne-tools#14051) Widen main content area (mne-tools#14015) MAINT: remove dead gain/bits/value_range fields from _read_header in … (mne-tools#14047) ENH: replace `_get_blocks` binary reader with mffpy Reader API (mne-tools#14043) MAINT: Update dependency specifiers (mne-tools#14048) [dependabot]: Bump the actions group with 2 updates (mne-tools#14049) Simplify doc building with more refleak (mne-tools#14045) ENH: add overlay Brain GUI (mne-tools#14031) ENH: support multiple simultaneous overlays in Brain.add_data (mne-tools#13995) Add option to show a zero line in browser (mne-tools#14018) FIX: pass cmap name string not tuple to interactive topomap slider kwargs (mne-tools#14039) Doc/add ai policy pointer (mne-tools#14037) Allow subclasses of FigureClass to be passed to plot_raw/plot_epochs (mne-tools#13979) MAINT: Replace manual PNS binary block reader in `_read_segment_file` with `mffpy` (mne-tools#14030)
Fixes #12989
What was wrong
When
mne.Epochsis created from aneventsarray containing sample numbers outside the data, the corresponding epochs are silently dropped with no indication that the events were out of bounds. This is easy to hit wheneventswas generated at a different sampling frequency, or contains samples beforefirst_samp.Fix
At construction, compute each epoch's sample window (the same
start/stopmath used by_get_epoch_from_raw) and warn if any fall before the start or past the end of the raw data, reporting how many.Test
Adds
test_epochs_warn_out_of_bounds_events: an event past the end of the data now raises aRuntimeWarning. Verified it does not warn onmain(the silent-drop behavior) and warns with this change.AI-assisted, human-reviewed — I'm an AI engineer; I find, fix, and test with AI (Claude Code), then review and verify before opening.