ERP Average - Code overwrites data

Hello everyone :slight_smile:

I try to get the average data on one condition for every participant and subsequently grand average over all participants.
My problem is that python overwrites the previous average when looping through the participant list and subsequently only forwards the averaged data for the last participant into the grand average. Of course I then get a notfication: Only a single dataset was passed to mne.grand_average()

import numpy as np
import matplotlib.pyplot as plt

import mne
from mne.time_frequency import tfr_morlet, psd_multitaper, tfr_multitaper




participant_list=[
                  'sub-04','sub-06','sub-07','sub-09','sub-10','sub-11',
                  'sub-13','sub-14','sub-15','sub-16','sub-20','sub-21',
                  'sub-22','sub-24','sub-25','sub-26','sub-27','sub-28','sub-29','sub-30',
                  'sub-31','sub-33','sub-34','sub-35','sub-37','sub-39','sub-43',
                  'sub-44','sub-45','sub-46','sub-47','sub-48','sub-49','sub-50',
                  'sub-51','sub-52','sub-53','sub-54','sub-56']

for subject in participant_list:
    
    # Load data (after ICA)
    session = 'ses-Expo'
    bids_root ='C:\\Users\\Admin\\Desktop\\Projekt_Franziska\Studie 3\\Analyse\\EEG\\BIDS'
    mne_data_folder=bids_root + '/' + subject + '/' +  session + '/' +  'eeg/' 
    data=mne.read_epochs(mne_data_folder + subject  +  '_' + session + '_run01_' + 'data_clean-epo.fif', verbose=False)
    
    
    data_CSplus_Remembered=data['Stimulus/S 28','Stimulus/S 29']
    
    data_CSplus_Forgotten=data['Stimulus/S 21','Stimulus/S 22',
                               'Stimulus/S 23','Stimulus/S 24',
                               'Stimulus/S 26', 'Stimulus/S 27']
    
    
    data_CSplus_Remembered_avg =  data_CSplus_Remembered['Stimulus/S 28','Stimulus/S 29'].average()
    
    evoked_CSPdata_Remembered= data_CSplus_Remembered.average(method='mean').filter(0.1, 20).crop(-0.2,1.0)
    data_CSplus_Remembered.apply_baseline(baseline=(-0.2, 0))
    data_CSplus_Remembered.plot_image(picks=['Pz'],vmin=-500,vmax=500);
    
    
    grand_average1 = mne.grand_average([data_CSplus_Remembered_avg])

I am not sure what to do about it…do I have to save all the averaged data sets and then upload all again to do the grand average?

Thank you :slight_smile:

Best,
Franziska

For starters, your grand average is inside the loop and is called at every iteration. Secondly, data_CSplus_Remembered_avg has a new value assigned at every iteration, while it should really be a list to which you append a new value.

data_CSplus_Remembered_avg = list()
for subject in participant_list:
    ....
    data_CSplus_Remembered_avg.append(
        data_CSplus_Remembered['Stimulus/S 28','Stimulus/S 29'].average()
    )
grand_average1 = mne.grand_average(data_CSplus_Remembered_avg)
3 Likes