# event annotations flipped

**URL:** <https://mne.discourse.group/t/event-annotations-flipped/6228>\
**Category:** Support & Discussions\
**Tags:** eeg, epochs, annotations\
**Created:** [January 23, 2023, 5:28pm UTC](https://mne.discourse.group/t/event-annotations-flipped/6228 "2023-01-23T17:28:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bissera](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/bissera/32/1962_2.png) [@bissera](https://mne.discourse.group/u/bissera)\
**Post date:** [January 23, 2023, 5:28pm UTC](https://mne.discourse.group/t/event-annotations-flipped/6228/1 "2023-01-23T17:28:09Z")

</div>

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

```python
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,  
bissera

“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   
epochsreref[‘onset’]  
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  
\_on\_missing(  
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():”

---

<div class="post-metadata">

**Author:** ![wmvanvliet](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/wmvanvliet/32/100_2.png) [@wmvanvliet](https://mne.discourse.group/u/wmvanvliet)\
**Post date:** [January 25, 2023, 7:41am UTC](https://mne.discourse.group/t/event-annotations-flipped/6228/2 "2023-01-25T07:41:56Z")

</div>

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:

```python
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.

---

<div class="post-metadata">

**Author:** ![bissera](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/bissera/32/1962_2.png) [@bissera](https://mne.discourse.group/u/bissera)\
**Post date:** [January 25, 2023, 1:21pm UTC](https://mne.discourse.group/t/event-annotations-flipped/6228/3 "2023-01-25T13:21:53Z")

</div>

Hi Marijn,

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

```python
# 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!  
bissera

---

<div class="post-metadata">

**Author:** ![drammock](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/drammock/32/4_2.png) [@drammock](https://mne.discourse.group/u/drammock)\
**Post date:** [January 25, 2023, 3:34pm UTC](https://mne.discourse.group/t/event-annotations-flipped/6228/4 "2023-01-25T15:34:27Z")

</div>

> [@bissera](#):
>
> ```python
> epochsreref = mne.Epochs(reref, events=onsetevents[0], tmin=tmin, tmax=tmax, on_missing='warn', reject=reject_criteria, preload=True, baseline=(-0.2,0))
> 
> ```

you should pass the `event_id=eventsdict` parameter here.

---

<div class="post-metadata">

**Author:** ![bissera](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/bissera/32/1962_2.png) [@bissera](https://mne.discourse.group/u/bissera)\
**Post date:** [January 26, 2023, 2:33pm UTC](https://mne.discourse.group/t/event-annotations-flipped/6228/5 "2023-01-26T14:33:05Z")

</div>

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