question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

with weights = 'equal', mne.combine_evoked computes sum, not mean

See original GitHub issue

Following up on a strange variation in the scale of my evokeds, I realized that mne.combine_evoked computes the sum, rather than the mean when weights = ‘equal’ (mne version: ‘0.21.dev0’):

evoked.py, line 876:

naves = np.array([evk.nave for evk in all_evoked], float)
  if isinstance(weights, str):
      _check_option('weights', weights, ['nave', 'equal'])
      if weights == 'nave':
          weights = naves / naves.sum()
      else:
          weights = np.ones_like(naves)
  else:
      weights = np.array(weights, float)

Shouldn’t it be weights = np.ones_like(naves) / len(all_evoked) instead of weights = np.ones_like(naves) ?

The documentation says

The weights to apply to the data of each evoked instance. Can also be ‘nave’ to weight according to evoked.nave, or “equal” to use equal weighting (each weighted as 1/N).

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
drammockcommented, Jun 5, 2020

it seems to me like the definitive question here is “what is a more common use case: equally-weighted sum, or equally-weighted average?” Both are possible, it’s a question of which we want to make easy for users with a string argument to weights. It is my impression that equally-weighted average is more common, and is also slightly more difficult for users to implement themselves:

equal_weighted_sum = combine_evokeds(evks, weights=np.ones(len(evks)))
equal_weighted_avg = combine_evokeds(evks, weights=np.ones(len(evks)) / len(evks))

Therefore I’m preparing a PR to make the code align with the docstring for weights='equal' and also to change the docstring’s recommendation about how to do a subtraction, since it will no longer be correct to use weights='equal' for subtraction after that change.

1reaction
drammockcommented, Jun 5, 2020

There is definitely something wrong here. If I recall correctly the discussion among myself, @jasmainak, @jona-sassenhagen, and @agramfort, it was decided that weights='equal' should work the way it does in order for subtractions of evokeds to work sensibly. I think between that concern and trying hard to get the nave computation correct, we lost sight of the common use case where users want 1/N weighting. For a quick work-around you could pass an array of 1/N values as weights, but then nave will be wrong. I think it’s time to re-think this implementation so that the most common use cases are correct, obvious, and hard to mess up.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mne.combine_evoked — MNE 1.3.dev0 documentation
Merge evoked data by weighted addition or subtraction. Each Evoked in all_evoked should have the same channels and the same time instants.
Read more >
The Evoked data structure: evoked/averaged data
For this reason, combining :class: ~mne.Evoked objects with either weights='equal' or by providing custom numeric weights should usually not be done if you ......
Read more >
Averaging ERPs: Creating MNE Evoked objects
Since averaging across trials is typically the end goal of an ERP experiment, MNE has a distinct class, Evoked , for ERP data...
Read more >
A Reproducible MEG/EEG Group Study With the MNE Software
While the unprocessed data do not show a clear evoked response, ... MNE computes the PSD of raw data using the standard Welch's...
Read more >
Evoked - Mainak Jas
In MNE the evoked objects are usually created by averaging epochs data with mne. ... Applying baseline correction (mode: mean) Read a total...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found