# Grand average in MNE-python

**URL:** <https://mne.discourse.group/t/grand-average-in-mne-python/662>\
**Category:** Mailing List Archive (read-only)\
**Tags:** list-archive\
**Created:** [February 11, 2014, 10:01pm UTC](https://mne.discourse.group/t/grand-average-in-mne-python/662 "2014-02-11T22:01:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [February 11, 2014, 10:01pm UTC](https://mne.discourse.group/t/grand-average-in-mne-python/662/1 "2014-02-11T22:01:59Z")

</div>

Hi MNE community,

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

best,  
mads

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [February 11, 2014, 10:08pm UTC](https://mne.discourse.group/t/grand-average-in-mne-python/662/2 "2014-02-11T22:08:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [February 11, 2014, 10:13pm UTC](https://mne.discourse.group/t/grand-average-in-mne-python/662/3 "2014-02-11T22:13:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [February 11, 2014, 10:30pm UTC](https://mne.discourse.group/t/grand-average-in-mne-python/662/4 "2014-02-11T22:30:21Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [February 11, 2014, 10:35pm UTC](https://mne.discourse.group/t/grand-average-in-mne-python/662/5 "2014-02-11T22:35:21Z")

</div>

> > 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 😉

> #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

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)\
**Post date:** [February 12, 2014, 10:23am UTC](https://mne.discourse.group/t/grand-average-in-mne-python/662/6 "2014-02-12T10:23:40Z")

</div>

Hi,

thanks for all the explanations and help. Very useful.

- mads
