Skip to content

Empty epochs from valid annotation when Raw cropped from beginning #8201

@hubertjb

Description

@hubertjb

While working with a cropped raw file I came across a case where mne.Epochs did not output what I expected. This happens when the Raw object contains an annotation toward the end of the recording and is cropped from the beginning, such that raw.first_samp is now close to the onset of the annotation. When calling mne.Epochs, the resulting epoch is empty (if I understand correctly, because the event now falls outside of the recording) although it should still be possible to extract this epoch.

Code to reproduce

import mne
import numpy as np


# Create fake data
sfreq = 100
duration_s = 10
data = np.random.randn(1, sfreq * duration_s)
info = mne.create_info(ch_names=['EEG1'], ch_types=['eeg'], sfreq=sfreq)
raw = mne.io.RawArray(data, info)

# Add annotation toward the end
onset = [8]
duration = [1]
description = ['0']
annots = mne.Annotations(onset, duration, description)
raw = raw.set_annotations(annots)

# Crop start of raw
raw.crop(tmin=7)

# Extract epochs
events, event_ids = mne.events_from_annotations(raw)
epochs = mne.Epochs(
    raw, events, tmin=0, tmax=1, baseline=None, on_missing='warning')

# epochs is empty
print(epochs.get_data())

Expected results

I would have expected mne.Epochs to return a valid epoch that starts 1 s after the beginning of the cropped raw and ends 1 s after that.

Actual results

The resulting epochs object is empty.

Additional information

Platform:      Linux-5.4.0-45-generic-x86_64-with-glibc2.10
Python:        3.8.5 (default, Aug  3 2020, 16:08:50)  [GCC 7.3.0]
Executable:    /home/hubert/miniconda3/envs/braindecode/bin/python
CPU:           x86_64: 4 cores
Memory:        7.7 GB

mne:           0.20.7
numpy:         1.18.5 {blas=openblas, lapack=openblas}
scipy:         1.4.1
matplotlib:    3.3.0 {backend=module://ipykernel.pylab.backend_inline}

sklearn:       0.23.2
numba:         Not found
nibabel:       Not found
cupy:          Not found
pandas:        1.1.0
dipy:          Not found
mayavi:        Not found
pyvista:       Not found
vtk:           Not found

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions