`mne.events_from_annotations` returns events that barely overlap with annotation
See original GitHub issueI noticed that mne.events_from_annotations
sometimes returns events that barely even overlap with an actual annotation when chunk_duration
is used. I think this might have been the actual reason behind the issue https://github.com/mne-tools/mne-python/issues/5684#issuecomment-438211200.
The issue seems to arise when an annotation has a duration that is only slightly over chunk_duration
. A first event will cover almost the entire annotation, but a second annotation will be created that might start within the annotation, but will barely overlap with it. Because of that, in some rare cases, the second event is identical to the next annotation - causing mne.Epochs
to throw an error due to duplicate events.
Steps and/or code to reproduce
import mne
import numpy as np
# Create fake data with annotations
data = np.random.randn(2, 16000)
info = mne.create_info(
ch_names=['EEG1', 'EEG2'], ch_types=['eeg', 'eeg'], sfreq=256)
raw = mne.io.RawArray(data, info)
onset = [0, 30]
duration = [30.1, 30]
description = ['0', '0']
annots = mne.Annotations(onset, duration, description)
raw = raw.set_annotations(annots)
# Extract epochs
events, event_ids = mne.events_from_annotations(raw, chunk_duration=30)
epochs = mne.Epochs(
raw, events, tmin=0, tmax=30, baseline=None, on_missing='warning')
Expected results
I would have expected events
to contain two events only: one for the first annotation [0, 30.1] and one for the second [30, 60]. An option would be to have an argument that lets the user decide whether events need to completely overlap with the annotation to be returned.
Actual results
events
is:
array([[ 0, 0, 1],
[7680, 0, 1],
[7680, 0, 1]])
Because of the duplicate row, mne.Epochs
returns RuntimeError: Event time samples were not unique
.
Additional information
Platform: Linux-4.17.13-041713-generic-x86_64-with-debian-buster-sid
Python: 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 17:14:51) [GCC 7.2.0]
Executable: /home/hubert/miniconda3/envs/dl-eeg/bin/python
CPU: x86_64: 4 cores
Memory: 15.6 GB
mne: 0.18.dev0
numpy: 1.16.0 {blas=openblas, lapack=openblas}
scipy: 1.1.0
matplotlib: 2.2.3 {backend=TkAgg}
sklearn: 0.20.2
nibabel: Not found
mayavi: 4.6.2 {qt_api=pyqt5, PyQt5=5.11.3}
cupy: Not found
pandas: 0.23.4
dipy: Not found
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
@mmagnuski @agramfort Thanks again!
Thanks for looking at this @mmagnuski!