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


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))
    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:


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['Stimulus/S 28','Stimulus/S 29'].average()
grand_average1 = mne.grand_average(data_CSplus_Remembered_avg)