Creating epochs with variable event latencies

  • MNE-Python version: 0.23.4
  • operating system: Windows
fname = "/EEG_data/subj1.set"
raw = mne.io.read_raw_eeglab(fname, preload=True)
raw_events, raw_event_id = mne.events_from_annotations(raw) #Q1
events_stim_to_resp = mne.pick_events(raw_events,exclude=[13,51,52,53,54])
epochs = mne.Epochs(raw, events=events_stim_to_resp , event_id=raw_event_id,  tmin=- 0.2, tmax=0.5, baseline = (-0.4,0), preload=True) #Q2
print(raw_event_id)
Used Annotations descriptions: ['11', '12', '13', '14', '21', '22', '23', '24', '51', '52', '53', '54', 'boundary']
{'11': 1, '12': 2, '13': 3, '14': 4, '21': 5, '22': 6, '23': 7, '24': 8, '51': 9, '52': 10, '53': 11, '54': 12, 'boundary': 13}

events_stim_to_resp[0:9]
array([[  467,     0,     3],
       [ 1068,     0,     7],
       [ 7256,     0,     2],
       [ 7545,     0,     6],
       [ 9851,     0,     3],
       [10207,     0,     7],
       [10686,     0,     1],
       [10986,     0,     5],
       [11528,     0,     3]])

How to read all fields from EEG.event / any workaround to import them from csv and then using it?
Reference: support user-defined event fields in mne.read_epochs_eeglab() · Issue #3837 · mne-tools/mne-python · GitHub
In the discussion, it can be seen that - “For legacy reasons, the events array will not be extented beyond a 3-column integer array.”

Q1. If I wanted to pick a field other than the "0"s column, is it possible?

Q2. Is there a way to use a variable latencies for event epoching instead of the fixed tmin-tmax duration?

I want to split the continuous data from events 3-7, 2-6 . . . and use the tmin-tmax values as 467-1068, 7256-7545 . . . Is there a way to do this directly? If not, any suggestion for workaround is also appreciated :slight_smile:

Custom EEGLAB events are currently not supported (the issue you linked is still open), but you could use the code that @mmagnuski posted there: support user-defined event fields in mne.read_epochs_eeglab() · Issue #3837 · mne-tools/mne-python · GitHub

Regarding variable-length epochs, this is not possible with rectangular NumPy arrays. As a workaround, you could put your epochs into a 2D array (one epoch per row, the number of columns corresponds to the number of samples of the longest epoch) and fill up samples for shorter epochs with np.nan.

1 Like

BTW - some standard mne operations (like averaging to get ERP) are expected to fail when there are NaN’s in the data, so you should be careful.

Thanks for the solution. But I couldn’t do it with NumPy arrays. I converted the epochs into dataframe, and then removed the additional time samples from the dataframe. Handling them with dataframe seemed easier to understand (and visualize!) and implement :slight_smile:

@mmagnuski I am not going to do averaging. But will keep in mind the uneven time duration while handling them. Thanks!