How to string codified epochs

Given an epochs with the following event create using the mne.make_fixed_length_events

[[ 6450 0 1]
[ 7350 0 1]
[ 8251 0 1]
[ 9152 0 1]
[10053 0 1]
[10954 0 1]
[11855 0 1]
[12756 0 1]
[13657 0 1]
[14558 0 1]]

Then, along the way, I redefine the integer Event IDs into the following.

[[ 6450 0 0]
[ 7350 0 0]
[ 8251 0 0]
[ 9152 0 0]
[10053 0 0]
[10954 0 0]
[11855 0 1]
[12756 0 1]
[13657 0 1]
[14558 0 1]]

Is it possible to map the Integer Event IDs onto meaningful description.
Such that, I would like create an event dictionary

event_ids ={'r0': 0, 'r1': 1,'r2': 2}

The full code to reproduce the above epoch creation is as below

import mne
import numpy as np
import pandas as pd

sample_data_raw_file='sample_audvis_filt-0-40_raw.fif'
raw = mne.io.read_raw_fif(sample_data_raw_file)
raw.crop(tmax=250)

tmin, tmax = 0, 6
event_id = 1  # This is used to identify the events.


events = mne.make_fixed_length_events(raw, event_id, start=0, stop=None, duration=tmax)
epochs = mne.Epochs(raw, events=events, event_id=event_id, baseline=None,
                    verbose=True, tmin=tmin, tmax=tmax, preload=True)


list_time = [int(x / tmax) for x in [40,80,150]]

df = pd.DataFrame(epochs.events, columns=['time_point', 'duration', 'event']).reset_index()

conditions = [
    (df['index'].between(list_time[0], list_time[1])),
    (df['index'].between(list_time[0], len(df)))]

df['event'] = np.select(conditions, [1, 2], default=0)
df.drop(columns=['index'], inplace=True)

epochs.events = df.to_numpy()

event_ids ={'r0': 0, 'r1': 1,'r2': 2}

I tried something like


epochs = mne.Epochs(raw, events=events, event_id={'r0': 0, 'r1': 1,'r2': 2}, baseline=None,
                    verbose=True, tmin=tmin, tmax=tmax, preload=True)

notice the event_id referencing

but as expected, the compiler return ValueError

ValueError: No matching events found for r0 (event id 0)

Appreciate for any hints

Ahaa, It is simply

event_ids ={'r0': 0, 'r1': 1,'r2': 2}
epochs.event_id=event_ids

I think the event_id dictionary should map from trigger values to names not the other way (so {0: 'r0', ...}).

No, sorry, I was wrong. :slight_smile: I think the problem is the trigger value of 0 , which is likely ignored.

So you could for example not change any trigger values to zero, but some of the ones to twos. And the event _id would be then for example {'r0': 1, 'r1': 2}.

@mmagnuski , you are saying to assign directly here?

epochs = mne.Epochs(raw, events=events, event_id={'r0': 0, 'r1': 1,'r2': 2}, baseline=None,
                    verbose=True, tmin=tmin, tmax=tmax, preload=True)

Yes, but you shouldn’t have a zero trigger, because a value of zero is no trigger. This is at least how I understand the error you get. But you should be able to assign event_id at epoch creation. Assigning it later (as in your solution) does not guarantee you will not face related errors later on.

I tried

    event_id = 1  # This is used to identify the events.
    event_ids ={'r0': 1, 'r1': 2,'r2': 3}
    # Divide continous signal into an epoch of tmax seconds
    events = mne.make_fixed_length_events(raw, event_id, start=0, stop=None, duration=tmax)
    epochs = mne.Epochs(raw, events=events, event_id=event_ids, baseline=None,
                        verbose=True, tmin=tmin, tmax=tmax, preload=True)

The compiler return an error

ValueError: No matching events found for r1 (event id 2)

Your epochs only contain events with code 1, so MNE complains about you passing a mapping to codes 2 and 3 as well.

You should just use make_fixed_length_epochs()

Or alter the event codes in the array you get from make_fixed_length_events() such that you have at least one occurrence of 2 and 3

Correct me if I am wrong @richard , in that case, I can simply assign the event as per this OP

You shouldn’t do this. You should pass the desired events and mapping between event names and codes (event_id) when instantiating the Epochs class.

You need to have events of given type (trigger value) when you try to epoch with respect to them and pass their string identifiers in event_id dict. Sorry if I led you astray - I thought that your problem stemmed from having event with trigger value of zero, thats why I suggested to replace zeros with ones and ones with twos, but that was not a good suggestion because trigger values of zero are not a problem to mne:

import numpy as np
import mne

n_channels, n_samples = 4, 1000
data = np.random.random((n_channels, n_samples))
events = np.array([[100, 0, 0], [250, 0, 0], [500, 0, 1], [650, 0, 1]])

info = mne.create_info(list('abcd'), sfreq=100, ch_types='eeg')
raw = mne.io.RawArray(data, info)
epochs = mne.Epochs(raw, events, preload=True, event_id={'a': 0, 'b': 1})

so in your case you just need to make sure that events of given values that are used in event_id dict are present in the events array (last column).

1 Like

Since it is critical to pass the desired events and mapping between event names and codes ( event_id ) when instantiating the Epochs class,

Then, it is better to reshuffle some of the step into


events = mne.make_fixed_length_events(raw, event_id, start=0, stop=None, duration=tmax)

list_time = [int(x / tmax) for x in [40,80,150]]

df = pd.DataFrame(events, columns=['time_point', 'duration', 'event']).reset_index()

conditions = [
    (df['index'].between(list_time[0], list_time[1])),
    (df['index'].between(list_time[0], len(df)))]

df['event'] = np.select(conditions, [2, 3], default=1)
df.drop(columns=['index'], inplace=True)

events = df.to_numpy()

event_ids ={'r0': 1, 'r1': 2,'r2': 3}


epochs = mne.Epochs(raw, events=events, event_id=event_ids, baseline=None,
                    verbose=True, tmin=tmin, tmax=tmax, preload=True)
1 Like