How to average epochs with the same event id

  • 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.


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

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

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

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:

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?

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.

1 Like

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 :+1: :blush: Here is the example of the metadata I create, which was reordered for illustration:


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, which used LDA, I noticed that the label was created by the event id:

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?

try something like this

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

could even work

Alex

1 Like

Hi! Alex,
Thanks a lot for your suggestions :blush: both worked perfectly fine :+1: and Iā€™m using the latter to try to do the LDA.

1 Like

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

2 Likes

A post was split to a new topic: How to get the decision value of each time point in each epoch?

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

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

1 Like