You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Warn if event sample number is out of bound for raw when creating Epochs #12989
When creating Epochs by providing an events array that contains sample numbers outside of the data, the respective epochs are dropped, but no warning is given that sample numbers are actually out of bounds.
This can happen e.g. if events is in another sampling frequency as raw or if events contains sample numbers before first_sample.
importnumpyasnpimportmne# Generate random datadata=np.random.randn(5, 1000)
# Create MNE info structurech_names= [f'EEG {i+1}'foriinrange(5)] # Channel namesinfo=mne.create_info(ch_names=ch_names, sfreq=100)
# Create RawArrayraw=mne.io.RawArray(data, info, first_samp=150)
events=np.array([[50, 0, 1], # before first_samp
[500, 0, 2], # works
[1100, 0, 3]]) # outside of dataepochs=mne.Epochs(raw, events=events, preload=True)
Expected results
I would expect that MNE warns me that the events array contains sample numbers that are out of bounds, instead of marking them as bad epochs.
Actual results
Out of bounds epochs are dropped.
3 matching events found
Setting baseline interval to [-0.2, 0.0] s
Applying baseline correction (mode: mean)
0 projection items activated
Using data from preloaded Raw for 3 events and 71 original time points ...
2 bad epochs dropped
I agree that it is worthwhile to warn users about an events array containing out-of-bound samples!
I'd argue that warn + drop those epochs is the right approach, as opposed to warn instead of dropping those epochs (as you suggest). (EDIT: If I'm understanding your expected results correctly).
This preserves current behavior (just makes users more aware of it), and prevents an epoch from being created where there is no data (as is the case for the last event in your events array).
We could add a new on_event_bound="warn" | "raise" | "ignore" where "ignore" is essentially the old behavior and "warn" is the behavior proposed here.
We would have to make it clear that it only will emit a warning when the event sample in the events array is out of bounds (assuming that's what we indeed want). For example if I have equally spaced events starting at sample zero but tmin=-0.2, tmax=0.8 the first epoch will not exist / be dropped because to create that epoch you'd have to take from negative time in the raw instance. But that's less clearly an events-creation problem than having an event at sample 10000 when you only have 8000 samples (and first_samp=0).
@larsoner do you think there will be downstream side-effects in the case of on_event_bound="warn"? E.g. in your example, what happens when you do epochs.apply_baseline given that the first epoch won't have any data from -.2-0 s?
E.g. in your example, what happens when you do epochs.apply_baseline given that the first epoch won't have any data from -.2-0 s?
Good question, and not 100% sure. I'm inclined to say we shouldn't warn in this case, we should just warn in the case that the zeroth sample number is outside the bounds.
But as far as downstream side effects go emitting warnings is more of a dev problem than a user problem. Users are capable of ignoring such warnings easily :) For devs it will require some work to make it so that test suites that treat uncaught warnings as errors (like ours, mne-bids, mne-bids-pipeline, etc.) pass again, which will be annoying. But it seems worth it to help users not make mistakes.
Activity
[-]Warn[/-][+]Warn if event sample number is out of bound for raw when creating Epochs[/+]scott-huberty commentedon Nov 27, 2024
I agree that it is worthwhile to warn users about an events array containing out-of-bound samples!
I'd argue that warn + drop those epochs is the right approach, as opposed to warn instead of dropping those epochs (as you suggest). (EDIT: If I'm understanding your expected results correctly).
This preserves current behavior (just makes users more aware of it), and prevents an epoch from being created where there is no data (as is the case for the last event in your events array).
larsoner commentedon Dec 2, 2024
We could add a new
on_event_bound="warn" | "raise" | "ignore"where"ignore"is essentially the old behavior and"warn"is the behavior proposed here.We would have to make it clear that it only will emit a warning when the event sample in the events array is out of bounds (assuming that's what we indeed want). For example if I have equally spaced events starting at sample zero but
tmin=-0.2, tmax=0.8the first epoch will not exist / be dropped because to create that epoch you'd have to take from negative time in the raw instance. But that's less clearly an events-creation problem than having an event at sample 10000 when you only have 8000 samples (and first_samp=0).scott-huberty commentedon Dec 2, 2024
@larsoner do you think there will be downstream side-effects in the case of
on_event_bound="warn"? E.g. in your example, what happens when you doepochs.apply_baselinegiven that the first epoch won't have any data from -.2-0 s?larsoner commentedon Dec 4, 2024
Good question, and not 100% sure. I'm inclined to say we shouldn't warn in this case, we should just warn in the case that the zeroth sample number is outside the bounds.
But as far as downstream side effects go emitting warnings is more of a dev problem than a user problem. Users are capable of ignoring such warnings easily :) For devs it will require some work to make it so that test suites that treat uncaught warnings as errors (like ours, mne-bids, mne-bids-pipeline, etc.) pass again, which will be annoying. But it seems worth it to help users not make mistakes.
Warn when Epochs events fall outside the raw data range (#12989) (#14004
Merge remote-tracking branch 'upstream/main' into types
Merge remote-tracking branch 'upstream/main' into sharifhsn-perf-opt