Grand average of all subjects per condition

MNE-Python version: 0.23.0
Operating system: McOS BigSur 11.4

Dear MNE forum,

First of all, thank you very much for your valuable feedback here as well as for the 8-hour YouTube tutorial on MNE. I am sorry in advanced for probably asking trivial things. I’m not advanced in Python and a beginner in MNE.

I have created the epochs, and then since I have 2 conditions, for each subject I created evokeds. That is, I have 2 evokeds per subject. Now I want to create the grand average across all subjects for each condition. As I see it, I should thus end up having 2 evoked objects across subjects (for each of the two conditions). When I use the function mne.grand_average, I get an error.

The documentation requires all_inst [ list of Evoked ] or [ AverageTFR ]. So, I created a list of all evoked objects (per subject) for the two conditions.

all_evokeds_col = [ev_col_1, ev_col_2, ev_col_3, ...]
all_evokeds_non_col = [ev_non_col_1, ev_non_col_2, ev_non_col_3,...]

grand_average_col = mne.grand_average(all_evokeds_col)
grand_average_non_col = mne.grand_average(all_evokeds_non_col)

But the grand_average function throws the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-72-50206ec155ec> in <module>
----> 1 grand_average_col = mne.grand_average(all_evokeds_col)

~/opt/anaconda3/envs/mne/lib/python3.9/site-packages/mne/utils/numerics.py in grand_average(all_inst, interpolate_bads, drop_bads)
    574 
    575     inst_type = type(all_inst[0])
--> 576     _validate_type(all_inst[0], (Evoked, AverageTFR), 'All elements')
    577     for inst in all_inst:
    578         _validate_type(inst, inst_type, 'All elements', 'of the same type')

~/opt/anaconda3/envs/mne/lib/python3.9/site-packages/mne/utils/check.py in _validate_type(item, types, item_name, type_name)
    423                 type_name = ', '.join(type_name)
    424         _item_name = 'Item' if item_name is None else item_name
--> 425         raise TypeError(f"{_item_name} must be an instance of {type_name}, "
    426                         f"got {type(item)} instead")
    427 

TypeError: All elements must be an instance of Evoked or AverageTFR, got <class 'list'> instead

I think I might be doing something fundamentally wrong, probably it is the list of evokeds (all_inst). So, I would be extremely grateful if someone could tell me. I’ve seen all examples on the MNE website and read the documentation but I’m struggling to compute the average across all subjects per condition.

All the best,
Armine

it seems that at least one of ev_col_1, ev_col_2, etc is a list (probably with an evoked inside it). You’ll need to figure out why that is. For example, this could easily happen if you had saved the evokeds to disk after creating them, then re-loaded before doing the grand average, because mne.read_evokeds will return a (possibly length-one) list when reading from file. (the reason it does that is that the fif format can store multiple evokeds in a single file, if desired).

If you can’t figure out why, you’ll need to share more of your code (the part that creates the variables ev_col_1 etc).

1 Like

Dear Dan,

Thanks a lot for your feedback! You were right, I indeed saved all the averaged epochs and then loaded them from a disk, that’s why the error emerged. But now following your recommendation, I create the evokeds and then immediately create the grand average, and everything works as I said. Thank you so much!