Grand average in MNE-python

Hi MNE community,

Is there a way to make a grand average of evoked files, and of source
estimations in MNE-python?

best,
mads

Hi Mads,

with stc objects and evoked objects in Python you can use numpy
functions to accumulate.

grand_ave = np.mean([evoked1, evoked2, evoked3])

which will produce another evoked.

The same works for stcs.

HTH,
Denis

Sorry Mads,

too fast, it only works with np.sum or sum from the standardlib.

In [7]: np.sum([evoked, evoked]).nave
Out[7]: 104

You might then want to divide by .nave

Denis

Sorry Mads,

too fast, it only works with np.sum or sum from the standardlib.

In [7]: np.sum([evoked, evoked]).nave
Out[7]: 104

You might then want to divide by .nave

Dividing by nave isn't necessary, the Evoked class takes care of
appropriate scaling. I.e., if you have

#print evoked1.nave
60
#print evoked2.nave
55

#evoked3 = evoked1 + evoked2 # same as using np.sum in Denis' example
#print evoked3.nave
115

The data in evoked3 is the same if the evoked response had been
calculated over 115 trials from one Epochs object. For SourceEstimates,
the number of averages isn't considered, i.e., if you use

stc3 = stc1 + stc2

The stc3.data is simply "stc1.data + stc2.data". So, you will need to
divide the stc yourself, i.e., use

stc3 = (stc1 + stc2) / 2.

The nice thing about using these overloaded operators is that you will
get an error if you try to combine objects which are not compatible,
e.g., evoked responses with different time intervals, source estimates
with different vertices etc.

I hope this helps.

Martin

PS: In case you are curious, the magic for combining evoked responses
happens in the function "merge_evoked" in mne/fiff/evoked.py

Sorry Mads,

too fast, it only works with np.sum or sum from the standardlib.

In [7]: np.sum([evoked, evoked]).nave
Out[7]: 104

You might then want to divide by .nave

Dividing by nave isn't necessary, the Evoked class takes care of
appropriate scaling. I.e., if you have

Good to know :wink:

#print evoked1.nave
60
#print evoked2.nave
55

#evoked3 = evoked1 + evoked2 # same as using np.sum in Denis' example
#print evoked3.nave
115

The data in evoked3 is the same if the evoked response had been
calculated over 115 trials from one Epochs object. For SourceEstimates,
the number of averages isn't considered, i.e., if you use

stc3 = stc1 + stc2

The stc3.data is simply "stc1.data + stc2.data". So, you will need to
divide the stc yourself, i.e., use

stc3 = (stc1 + stc2) / 2.

The nice thing about using these overloaded operators is that you will
get an error if you try to combine objects which are not compatible,
e.g., evoked responses with different time intervals, source estimates
with different vertices etc.

just to avoid confusion, the operator overloading is also used when
using a sum function on a list of evoked objects.
The manual way would be to directly access `evoked.data`

Best,
Denis

Hi,

thanks for all the explanations and help. Very useful.

- mads