# How to string codified epochs

**URL:** <https://mne.discourse.group/t/how-to-string-codified-epochs/4716>\
**Category:** Support & Discussions\
**Tags:** epochs\
**Created:** [April 15, 2022, 3:18pm UTC](https://mne.discourse.group/t/how-to-string-codified-epochs/4716 "2022-04-15T15:18:01Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![balandongiv](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/balandongiv/32/104_2.png) [@balandongiv](https://mne.discourse.group/u/balandongiv)\
**Post date:** [April 15, 2022, 3:18pm UTC](https://mne.discourse.group/t/how-to-string-codified-epochs/4716/1 "2022-04-15T15:18:01Z")

</div>

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

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

```python

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

---

<div class="post-metadata">

**Author:** ![balandongiv](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/balandongiv/32/104_2.png) [@balandongiv](https://mne.discourse.group/u/balandongiv)\
**Post date:** [April 15, 2022, 3:26pm UTC](https://mne.discourse.group/t/how-to-string-codified-epochs/4716/2 "2022-04-15T15:26:16Z")

</div>

Ahaa, It is simply

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

```

---

<div class="post-metadata">

**Author:** ![mmagnuski](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mmagnuski/32/153_2.png) [@mmagnuski](https://mne.discourse.group/u/mmagnuski)\
**Post date:** [April 16, 2022, 8:52am UTC](https://mne.discourse.group/t/how-to-string-codified-epochs/4716/3 "2022-04-16T08:52:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mmagnuski](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mmagnuski/32/153_2.png) [@mmagnuski](https://mne.discourse.group/u/mmagnuski)\
**Post date:** [April 16, 2022, 8:54am UTC](https://mne.discourse.group/t/how-to-string-codified-epochs/4716/4 "2022-04-16T08:54:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mmagnuski](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mmagnuski/32/153_2.png) [@mmagnuski](https://mne.discourse.group/u/mmagnuski)\
**Post date:** [April 16, 2022, 9:02am UTC](https://mne.discourse.group/t/how-to-string-codified-epochs/4716/5 "2022-04-16T09:02:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![balandongiv](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/balandongiv/32/104_2.png) [@balandongiv](https://mne.discourse.group/u/balandongiv)\
**Post date:** [April 16, 2022, 9:21am UTC](https://mne.discourse.group/t/how-to-string-codified-epochs/4716/6 "2022-04-16T09:21:54Z")

</div>

@mmagnuski , you are saying to assign directly here?

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

```

---

<div class="post-metadata">

**Author:** ![mmagnuski](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mmagnuski/32/153_2.png) [@mmagnuski](https://mne.discourse.group/u/mmagnuski)\
**Post date:** [April 16, 2022, 10:44am UTC](https://mne.discourse.group/t/how-to-string-codified-epochs/4716/7 "2022-04-16T10:44:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![balandongiv](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/balandongiv/32/104_2.png) [@balandongiv](https://mne.discourse.group/u/balandongiv)\
**Post date:** [April 16, 2022, 11:42am UTC](https://mne.discourse.group/t/how-to-string-codified-epochs/4716/8 "2022-04-16T11:42:46Z")

</div>

I tried

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

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [April 16, 2022, 1:55pm UTC](https://mne.discourse.group/t/how-to-string-codified-epochs/4716/9 "2022-04-16T13:55:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![balandongiv](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/balandongiv/32/104_2.png) [@balandongiv](https://mne.discourse.group/u/balandongiv)\
**Post date:** [April 16, 2022, 4:04pm UTC](https://mne.discourse.group/t/how-to-string-codified-epochs/4716/10 "2022-04-16T16:04:55Z")

</div>

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

> [@How to string codified epochs](https://mne.discourse.group/t/how-to-string-codified-epochs/4716/2):
>
> Ahaa, It is simply event\_ids ={'r0': 0, 'r1': 1,'r2': 2} epochs.event\_id=event\_ids

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [April 16, 2022, 5:15pm UTC](https://mne.discourse.group/t/how-to-string-codified-epochs/4716/11 "2022-04-16T17:15:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mmagnuski](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mmagnuski/32/153_2.png) [@mmagnuski](https://mne.discourse.group/u/mmagnuski)\
**Post date:** [April 16, 2022, 7:50pm UTC](https://mne.discourse.group/t/how-to-string-codified-epochs/4716/12 "2022-04-16T19:50:42Z")

</div>

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:

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

---

<div class="post-metadata">

**Author:** ![balandongiv](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/balandongiv/32/104_2.png) [@balandongiv](https://mne.discourse.group/u/balandongiv)\
**Post date:** [April 17, 2022, 5:07am UTC](https://mne.discourse.group/t/how-to-string-codified-epochs/4716/13 "2022-04-17T05:07:50Z")

</div>

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

```python

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)

```
