Repository: mne-tools/mne-python

Issue:
Empty epochs from valid annotation when Raw cropped from beginning
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

```python
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
```



Discussion:
this is a nasty one.

Apparently set_annotations is not doing the right thing when meas_date and
orig_time are None....

@larsoner do you have time to look? otherwise I'll try to find some time to
look into this before the
end of the week

>


Resolving patch:
diff --git a/mne/annotations.py b/mne/annotations.py
--- a/mne/annotations.py
+++ b/mne/annotations.py
@@ -1009,8 +1009,9 @@ def events_from_annotations(raw, event_id="auto",
 
     if chunk_duration is None:
         inds = raw.time_as_index(annotations.onset, use_rounding=use_rounding,
-                                 origin=annotations.orig_time) + raw.first_samp
-
+                                 origin=annotations.orig_time)
+        if annotations.orig_time is not None:
+            inds += raw.first_samp
         values = [event_id_[kk] for kk in annotations.description[event_sel]]
         inds = inds[event_sel]
     else:


Test patch:
diff --git a/mne/tests/test_annotations.py b/mne/tests/test_annotations.py
--- a/mne/tests/test_annotations.py
+++ b/mne/tests/test_annotations.py
@@ -278,6 +278,33 @@ def test_chunk_duration(first_samp):
     assert_array_equal(events, expected_events)
 
 
+def test_events_from_annotation_orig_time_none():
+    """Tests events_from_annotation with orig_time None and first_sampe > 0."""
+    # Create fake data
+    sfreq, duration_s = 100, 10
+    data = np.random.RandomState(42).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
+    assert_array_equal(epochs.get_data()[0], data[:, 800:901])
+
+
 def test_crop_more():
     """Test more cropping."""
     raw = mne.io.read_raw_fif(fif_fname).crop(0, 11).load_data()