# How to average epochs with the same event id

**URL:** <https://mne.discourse.group/t/how-to-average-epochs-with-the-same-event-id/2875>\
**Category:** Support & Discussions\
**Tags:** epochs\
**Created:** [March 24, 2021, 3:05am UTC](https://mne.discourse.group/t/how-to-average-epochs-with-the-same-event-id/2875 "2021-03-24T03:05:10Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![YuZhou](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/yuzhou/32/76_2.png) [@YuZhou](https://mne.discourse.group/u/YuZhou)\
**Post date:** [March 24, 2021, 3:05am UTC](https://mne.discourse.group/t/how-to-average-epochs-with-the-same-event-id/2875/1 "2021-03-24T03:05:10Z")

</div>

- MNE-Python version: 0.22.0
- operating system: linux

Hey guys,  
I’m wondering if there is a way to average epochs which is duplicate (the duplicated epochs have exactly the same event id) because, in my experiment, all the trials/pictures were repeated twice.

 ![image](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/94d83d4ed0bc7d1c8e5327755ff394e8490fed5f.jpeg)  
After epoching, I used the autoreject package to discard some epochs which are outliers. Is there a way or function to average epochs with the same event id? Or, is there a way to do this after applying `apply_inverse_epochs()`

Really need your help, thanks a lot!

Best

---

<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:** [March 25, 2021, 9:00am UTC](https://mne.discourse.group/t/how-to-average-epochs-with-the-same-event-id/2875/2 "2021-03-25T09:00:21Z")

</div>

> [@YuZhou](#):
>
> Is there a way or function to average epochs with the same event id?

Yes, simply select the epochs you wish to average by name, and then average them:

```python
evoked_cow = epochs['ab/cow'].average()

```

---

<div class="post-metadata">

**Author:** ![YuZhou](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/yuzhou/32/76_2.png) [@YuZhou](https://mne.discourse.group/u/YuZhou)\
**Post date:** [March 25, 2021, 11:45am UTC](https://mne.discourse.group/t/how-to-average-epochs-with-the-same-event-id/2875/3 "2021-03-25T11:45:24Z")

</div>

Hi! Richard,

Thank you very much for your reply. I have lots of duplicate trials, and I found some code in the tutorial to average them in a for loop:

```python
evokeds = dict()
query = 'event_name == {}'
for eve_name in epos_pic_corr.metadata['event_name'].unique():
    evokeds[str(eve_name)] = epos_pic_corr[query.format(eve_name)].average()

```

My question is the evokeds object created above already didn’t have the metadata like epochs have, right? I cannot query or select evoked data based on the metadata anymore, right?

---

<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:** [March 25, 2021, 2:10pm UTC](https://mne.discourse.group/t/how-to-average-epochs-with-the-same-event-id/2875/4 "2021-03-25T14:10:51Z")

</div>

> [@YuZhou](#):
>
> My question is the evokeds object created above already didn’t have the metadata like epochs have, right? I cannot query or select evoked data based on the metadata anymore, right?

Yes, the metadata gets lost when you average epochs. The problem here is that it would be difficult to decide which parts of the metadata to keep or how to merge metadata across trials. Do you have a specific use case that would require access to metadata on the `Evoked` level? What would you need, how would you imagine things to work?

Also, are you aware of `Evoked.comment`? There, we keep track of how epochs were combined to form the average.

---

<div class="post-metadata">

**Author:** ![YuZhou](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/yuzhou/32/76_2.png) [@YuZhou](https://mne.discourse.group/u/YuZhou)\
**Post date:** [March 26, 2021, 1:40am UTC](https://mne.discourse.group/t/how-to-average-epochs-with-the-same-event-id/2875/5 "2021-03-26T01:40:38Z")

</div>

I mainly have two goals, the first is to do the linear discriminant analysis at the sensor level. In my experiment, each picture was presented twice to increase the signal-to-noise ratio, so at first, I thought I should average the epochs with the same event name, and also, I modified the metadata obtained from the function that you write in the response to another MNE user, which is very handy by the way 👍 😊 Here is the example of the metadata I create, which was reordered for illustration:

 ![image](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/94be3e85f5663cb61ebc1b4801a79c1029c28c88.jpeg)  
You can see that each picture was repeated twice despite some pictures only have one record because of the rejection by the package ‘autoreject’. So naturally, it came to me that I should average the epochs with the same event name to increase the signal-to-noise ratio, but in another hand, if I do this, I will lose the metadata information which I need to do the linear discriminant analysis (LDA). So maybe not doing the average can also work in the LDA?

In the tutorial [Motor imagery decoding from EEG data using the Common Spatial Pattern (CSP) — MNE 0.22.0 documentation](https://mne.tools/stable/auto_examples/decoding/plot_decoding_csp_eeg.html#sphx-glr-auto-examples-decoding-plot-decoding-csp-eeg-py), which used LDA, I noticed that the label was created by the event id:

 ![image](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/9668b20da93b3b7a41959b118f398f63991e0153.png) but in my experiment, each picture had its own event id (trigger index), instead of each condition have its own event id (trigger index), so I prepared the metadata to mark the attributes and the conditions of the stimulus. In the metadata, you can see that the column of “feature\_judge” is the task type, the columns of “feature\_color”, “feaute\_animacy” and “feature\_size” are the attributes of the stimulus, what I want to do is to create 3 classifiers in this kind of the task to discriminate color (colorful vs. gray), animacy (animate vs. inanimate), size (small vs. big). Is there a way to create labels with metadata instead of the event\_id/trigger index?

---

<div class="post-metadata">

**Author:** ![agramfort](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@agramfort](https://mne.discourse.group/u/agramfort)\
**Post date:** [March 26, 2021, 7:43am UTC](https://mne.discourse.group/t/how-to-average-epochs-with-the-same-event-id/2875/6 "2021-03-26T07:43:22Z")

</div>

try something like this

`labels = pd.factorize(epochs.metadata['feature_color'])`  
or maybe  
`labels = epochs.metadata['feature_color']`

could even work

Alex

---

<div class="post-metadata">

**Author:** ![YuZhou](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/yuzhou/32/76_2.png) [@YuZhou](https://mne.discourse.group/u/YuZhou)\
**Post date:** [March 26, 2021, 10:20am UTC](https://mne.discourse.group/t/how-to-average-epochs-with-the-same-event-id/2875/7 "2021-03-26T10:20:08Z")

</div>

Hi! Alex,  
Thanks a lot for your suggestions 😊 both worked perfectly fine 👍 and I’m using the latter to try to do the LDA.

---

<div class="post-metadata">

**Author:** ![agramfort](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@agramfort](https://mne.discourse.group/u/agramfort)\
**Post date:** [March 27, 2021, 8:08pm UTC](https://mne.discourse.group/t/how-to-average-epochs-with-the-same-event-id/2875/9 "2021-03-27T20:08:35Z")

</div>

please share a full script from imports to the crash.

you can share .zip or dropbox folder or anything that allows to replicate the pb  
with:

python run\_script.py

Alex

---

<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:** [March 28, 2021, 4:06pm UTC](https://mne.discourse.group/t/how-to-average-epochs-with-the-same-event-id/2875/10 "2021-03-28T16:06:09Z")

</div>

A post was split to a new topic: [How to get the decision value of each time point in each epoch?](https://mne.discourse.group/t/how-to-get-the-decision-value-of-each-time-point-in-each-epoch/2895)

---

<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:** [March 28, 2021, 4:07pm UTC](https://mne.discourse.group/t/how-to-average-epochs-with-the-same-event-id/2875/11 "2021-03-28T16:07:23Z")

</div>

@YuZhou I have moved your question to a new topic. Please always open a separate topic for new questions.

---

<div class="post-metadata">

**Author:** ![YuZhou](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/yuzhou/32/76_2.png) [@YuZhou](https://mne.discourse.group/u/YuZhou)\
**Post date:** [March 29, 2021, 3:21am UTC](https://mne.discourse.group/t/how-to-average-epochs-with-the-same-event-id/2875/12 "2021-03-29T03:21:26Z")

</div>

Okay, sure, thanks a lot. I will pay attention next time 😌
