Averaging evoked objects across subjects

Hey everyone,

for my data analysis, I want to compare the EEG signal between two conditions averaged across participants using the mne.grand_average() function. However, this returns the following error: “All elements must be an instance of Evoked or AverageTFR, got <class ‘list’> instead.”

The evoked .fif objects come from a folder which contains evokeds from multiple participants. I loaded the evoked objects in the folder into one list with the following code:

file_names = fnmatch.filter(os.listdir(evoked_folder), "*condition1_ave.fif")

all_evoked = []

for file_counter in file_names[0:len(file_names)]:
    #file_counter = file_names[1]
    #participant_name = file_counter.split("_")[0] 
    temp = mne.read_evokeds(evoked_folder + file_counter, condition= None)
    all_evoked.append(temp)

However, this seems to create a list of 16 lists of an evoked object, instead of one list with 16 evoked objects. Thus, when I run mne.grand_average(all_evoked), I get the errror message: “All elements must be an instance of Evoked or AverageTFR, got <class ‘list’> instead.”

I should also mention that the evoked objects contain epochs with two different event IDs. The number of epochs is not the same in every evoked function due to rejection of trials during the preprocessing.

However, I am not sure how I can get the list to be only made up of the evoked objects? I would be super grateful for some help!

  • MNE version:1.2.1_0
  • operating system: Windows 11
1 Like

Hello @NoahM and welcome to the forum!

If you don’t pass a condition name to read_evokeds(), it will return a list of evoked objects.

In your specific case, I suppose the easiest solution is to simply pass the condition name. You will then get back a single evoked (for that specific condition).

Best wishes,
Richard

1 Like

Hey @richard, thank you for the quick reply!

Unfortunately, the way you suggested it does not work for me because for the evoked objects, I aggregated epochs from two conditions and the ratio of the 2 event IDs slightly deviates for each participant. Thus, the condition function would differ for every itertation of the for loop.

How many evoked objects does each evoked file contain? Only one?

1 Like

Yes, exactly. After preprocessing the epochs for every participant separately, I used their respective epochs to create two evoked objects for each participant separately as well (based on their performance in a behavioral task). So now, I have 2 (conditions) x 20 (participants) evoked files which each contain one evoked object.

read_evokeds() allows condition to be an integer referring to the “index” of the the evoked you want to load. If there’s only a single evoked in your files, the corresponding index would always be 0.

So you can read the evoked via read_evokeds(…, condition=0). This will return that one evoked object, and not a list.

Best wishes,
Richard

1 Like

Thank you so much for your quick and competent help, Richard!

I did exactly that and like you said, an evoked object was returned. Now, I can run the grand_average() function on the list of evokeds.

Kind regards,
Noah