Repository: mne-tools/mne-python

Issue:
event.py define_target_events error
I brought this up on mne_analysis_request mailing list, then looked at the code, and wasn't sure the best way to point out the issue, so here it is:

Line 141:         lag[lag != fill_na] *= tsample

If fill_na is None, then lag != fill_na returns True, so this only multiplies lag[1] by tsample, leaving all other entries in lag as number of samples rather than number of milliseconds. Not sure the most elegant way to fix it - maybe just if fill_na is None, do lag *= tsample, else do the current line 141. But it also seems risky to index this way; what if the true lag is 47 and my fill_na is 47 as well?

Thanks,

Natalie



Discussion:
can you paste here 10 lines of python that reproduces the bug and that one
could use for a test?

thanks for looking into it.

This code reproduces the bug - you can see it only multiplies lag[1] by tsample rather than all non-NA elements by inspecting new_events[1], as all of these lags should be exactly 1500ms:

```
events = np.array([[0, 0, 1], [375, 0, 2], [500, 0, 1], [875, 0, 3], [1000, 0, 1], [1375, 0, 3], [1100, 0, 1], [1475, 0, 2], [1500, 0, 1], [1875, 0, 2]])                              

new_events = mne.event.define_target_events(events=events, reference_id=1, target_id=2, sfreq=250., tmin=1.4, tmax=1.6, new_id=5)
```

Thank you!

@natalieklein sorry for the delay, I'm currently traveling, I'll get back to you ASAP.

@dengemann No problem! I used the code from your function without the lag for now as I don't really need the lag, so no rush at all, I simply wanted to share so it could be fixed for other users. I mentioned this in the mne analysis list, but as a reminder, this problem only occurs when fill_na is None.

Thanks again!

@natalieklein do you think you can open a PR to fix this? It should come with a non-regression test that you already have here.

@agramfort I'll try to do it today!


Resolving patch:
diff --git a/mne/event.py b/mne/event.py
--- a/mne/event.py
+++ b/mne/event.py
@@ -132,13 +132,13 @@ def define_target_events(events, reference_id, target_id, sfreq, tmin, tmax,
             elif fill_na is not None:
                 event[2] = fill_na
                 new_events += [event]
-                lag += [fill_na]
+                lag.append(None)
 
     new_events = np.array(new_events)
 
     lag = np.abs(lag, dtype='f8')
     if lag.any():
-        lag[lag != fill_na] *= tsample
+        lag *= tsample
     else:
         lag = np.array([])
 


Test patch:
diff --git a/mne/tests/test_event.py b/mne/tests/test_event.py
--- a/mne/tests/test_event.py
+++ b/mne/tests/test_event.py
@@ -317,3 +317,21 @@ def test_define_events():
     n_target_ = events_[events_[:, 2] == 42].shape[0]
 
     assert_true(n_target_ == (n_target - n_miss))
+
+    events = np.array([[0, 0, 1],
+                       [375, 0, 2],
+                       [500, 0, 1],
+                       [875, 0, 3],
+                       [1000, 0, 1],
+                       [1375, 0, 3],
+                       [1100, 0, 1],
+                       [1475, 0, 2],
+                       [1500, 0, 1],
+                       [1875, 0, 2]])
+    true_lag_nofill = [1500., 1500., 1500.]
+    true_lag_fill = [1500., np.nan, np.nan, 1500., 1500.]
+    n, lag_nofill = define_target_events(events, 1, 2, 250., 1.4, 1.6, 5)
+    n, lag_fill = define_target_events(events, 1, 2, 250., 1.4, 1.6, 5, 99)
+
+    assert_array_equal(true_lag_fill, lag_fill)
+    assert_array_equal(true_lag_nofill, lag_nofill)