event annotations flipped

hi everyone,

i’m trying to subselect epochs by condition. my event descriptions are pretty long because ideally i want to be able to subselect different parts of my data. a selection from the events dictionary below:
‘CW/fill/adv/close/mv/malin’: 1220,
‘CW/stim/adv/dist/g/furieux’: 1129,
‘onset/stim/adj/close/mg/luxuriante’: 49,

i’ve annotated my raw data and can see the annotations when i plot my data. however when i make my epochs and try to select a subsection with epochs['onset'] to take out only the epochs that fall into condition ‘onset’ i get the error message below.

it seems like it’s looking in the trigger codes and not the descriptions, but when i go back a few steps to where i created the annotations, using

event_annotations = mne.annotations_from_events(events=events, sfreq=rawmff.info['sfreq'], orig_time=rawmff.info['meas_date'], event_desc=eventsdict, verbose=True)
rawmff.set_annotations(rawmff.annotations + event_annotations)

and i try to FLIP the keys and the values in my event_desc=eventsdict dictionary, i get an error “TypeError: Event names must be an instance of str or None, got <class ‘int’> instead.”. so the structure of my dictionary seems to be correct - i create the annotations ok. it’s just after, when i try to use them in the epoched data, i can’t seem to get through to them.

mne v. 1.2.3, mac os 12.3.1.

many thanks for your help,

“Traceback (most recent call last):
File “/Users/ivanova/opt/anaconda3/envs/eegProc/lib/python3.9/site-packages/IPython/core/interactiveshell.py”, line 3433, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File “”, line 1, in
File “/Users/ivanova/opt/anaconda3/envs/eegProc/lib/python3.9/site-packages/mne/utils/mixin.py”, line 144, in getitem
return self._getitem(item)
File “/Users/ivanova/opt/anaconda3/envs/eegProc/lib/python3.9/site-packages/mne/utils/mixin.py”, line 195, in _getitem
select = inst._item_to_select(item)
File “/Users/ivanova/opt/anaconda3/envs/eegProc/lib/python3.9/site-packages/mne/utils/mixin.py”, line 153, in _item_to_select
select = self._keys_to_idx(item)
File “/Users/ivanova/opt/anaconda3/envs/eegProc/lib/python3.9/site-packages/mne/utils/mixin.py”, line 242, in _keys_to_idx
for k in match_event_names(self.event_id, keys)]),
File “/Users/ivanova/opt/anaconda3/envs/eegProc/lib/python3.9/site-packages/mne/event.py”, line 1493, in match_event_names
File “/Users/ivanova/opt/anaconda3/envs/eegProc/lib/python3.9/site-packages/mne/utils/check.py”, line 991, in _on_missing
raise error_klass(msg)
KeyError: ‘Event name “onset” could not be found. The following events are present in the data: 10, 11, 12, 13, 14, 15, MY HUNDRED TRIGGER CODES, 1219, 1220, 1221, 1222, 1223, 1224’
/Applications/DataSpell.app/Contents/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_utils.py:609: FutureWarning: iteritems is deprecated and will be removed in a future version. Use .items instead.
for item in s.iteritems():”

You have indeed specified your eventsdict correctly. Could you give us the code you are using to create the Epochs object?

I have a suspicion that what went wrong is that you don’t give it both the events matrix and the eventsdict when creating it, like this:

epochs = mne.Epochs(raw, events, eventsdict, tmin=-0.1, tmax=1.0)

You might expect the Epochs object to be clever enough to use the annotations you’ve created if you don’t specify them, but sadly, it isn’t and instead creates some default event names based on the trigger codes.


Hi Marijn,

Thanks for writing back! I create the epochs like so:

# load preprocessed data
loadrerefpath = os.path.join(eegdir + '/sub-' + subject + '_ses-' + session + '_ica_reref-eeg.fif')
reref = mne.io.read_raw_fif(loadrerefpath, allow_maxshield=True, preload=True, verbose=True)

# eventsdict for all stimuli
evdf = pd.read_excel(os.path.join(datadir,'trigDict.xlsx')) # xl sheet with two columns - condition and trigger code
eventsdict = {}
for index, row in evdf.iterrows():
    eventsdict[row['condition']] = row['code'] 
# here i tried flipping what's the key and what - the value, but if i do the below function doesn't work

onsetevents = mne.events_from_annotations(reref, event_id=eventsdict)
# this gives a tuple, so below i only use the events array which comes first in the tuple

# epoch
tmin = -0.2
tmax = 0.5
epochsreref = mne.Epochs(reref, events=onsetevents[0], tmin=tmin, tmax=tmax, on_missing='warn', reject=reject_criteria, preload=True, baseline=(-0.2,0))

Thanks for your help!

you should pass the event_id=eventsdict parameter here.


Hi both, thanks for your help, it’s now working!