You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Document that add_reference_channel should be used with set_eeg_reference for average reference #13618
Hey!
We (mostly @jschepers) might have diagnosed an issue in MNE:
We often want to have a different reference e.g. average reference in EEG.
But also often, the reference channel is not included in the data-set (because it is flat / removed during recording etc.).
Then, when naively applying the average reference with set_eeg_reference, one can very easily get the wrong result, as the calculation in mne boils down to (we think, the projection-code makes tracing the actual calculation difficult to us): sum(over_channels) / n_channels
"ignoring" the forgotten reference channel, and not sum(over_channels) / (n_channels+1) - including the "forgotten" reference channel.
The (for us) seemingly correct way to calculate average reference is to add_reference_channel before, then calculating the average reference (optional: drop the reference channel again for fullrank data).
We assumed, MNE does this by default, but we were wrong. We think this could be documented, that if the reference channel is not included in the data, one should add_reference_channel before calculating average reference.
We are happy to provide some example code if necessary to understand. We can also propose some documentation in the tutorial + reference-function. One could also think to check if a reference channel is included in the data (we don't know how), and if not - either calculate sum(...)/(n+1). or add a channel prior to re-referencing, or throw a warning
We hope we didn't miss anything too obvious! Thanks a ton for the hard work.
I haven't thought much about this issue, but have a couple of quick thoughts. First the practice of including the zeroed-out channel differs across manufacturers / formats -- to take two that come to mind immediately:
Neuromag EEG / .fif -- the electrode is typically on the nose and serves as the reference, but there is no channel for it in the data. You could in principle add a zero-filled channel for it, apply the avg reference as you say, etc., but in practice nobody does this.
EGI MFF -- the electrode that serves as a reference is added as a channel in the data that is all zeros.
For projectors, we take them into account this reference electrode properly during forward / inverse calculation etc. (its contribution is subtracted out... even though it will cancel later during avg ref proj) so I think things are okay in those pipelines. And for sensor analysis, projectors always bias the data slightly (hence the proj="reconstruct"option in evoked.plot), avg ref projectors included. (For source-space analyses, this bias is compensated for by applying the same projector to the forward model that you do to the data.) So I think in the case of an average reference projector, things behave as expected, and we maybe need to document things better somewhere. I'm guessing this was a conscious choice during development of Neuromag pipelines to treat EEG this way, knowing the end goal is source imaging...
But for sensor-space analyses without using / understanding / accepting projector bias, when people do set_eeg_reference(ref_channels="average", projection=False) (which is the default!) I suspect the n vs n+1 bug is probably in there. At the very least we should document it, and suggest that people add a reference channel if it's missing and they intend to rely on sensor-space analyses.
This looks like a documentation gap rather than a bug. A short note in the set_eeg_reference documentation (average reference section) could mention that when the reference electrode is not present as a channel, users should consider calling add_reference_channel before applying an average reference, especially for sensor-space analyses.
If that placement sounds right, I can open a small documentation PR adding this note.
Hi! I'm a GSoC applicant and I'd like to work on this. Since I'm familiar with the documentation structure now, I can handle the updates. May I take this up?
@Dpereaptkhamur-13 There is already a PR open in #13664 addressing this, although it hasn't seen activity in a while. Let's see if the author there still plans on working on it first. If not, then you'd be very welcome to pick that up, and you could use the existing work in #13664 as a starting point.
Hi @tsbinns, thank you for the feedback! That makes sense. I'm happy to wait and see if the original author wants to continue. If not, I'd be more than glad to pick it up and integrate the existing work from #13664 into this PR. Just let me know whenever you'd like me to proceed!
@Dpereaptkhamur-13 It's been a week and I haven't heard back from the author of #13664. If you are still interested, you could open a new PR, building on the existing work and unresolved review comments.
Hi @tsbinns, thank you for the update! Yes, I am definitely still interested and would be happy to take this over. I'll open a new PR shortly that incorporates the existing work from #13664 along with the necessary fixes for the unresolved review comments. Thanks for the opportunity!
Hi @tsbinns, I have taken over the work from #13664 as discussed. I've addressed the documentation requirements for #13618 and fixed the towncrier naming issue that was causing check failures in the previous PR. Ready for review!
Activity
larsoner commentedon Jan 28, 2026
I haven't thought much about this issue, but have a couple of quick thoughts. First the practice of including the zeroed-out channel differs across manufacturers / formats -- to take two that come to mind immediately:
For projectors, we take them into account this reference electrode properly during forward / inverse calculation etc. (its contribution is subtracted out... even though it will cancel later during avg ref proj) so I think things are okay in those pipelines. And for sensor analysis, projectors always bias the data slightly (hence the
proj="reconstruct"option inevoked.plot), avg ref projectors included. (For source-space analyses, this bias is compensated for by applying the same projector to the forward model that you do to the data.) So I think in the case of an average reference projector, things behave as expected, and we maybe need to document things better somewhere. I'm guessing this was a conscious choice during development of Neuromag pipelines to treat EEG this way, knowing the end goal is source imaging...But for sensor-space analyses without using / understanding / accepting projector bias, when people do
set_eeg_reference(ref_channels="average", projection=False)(which is the default!) I suspect thenvsn+1bug is probably in there. At the very least we should document it, and suggest that people add a reference channel if it's missing and they intend to rely on sensor-space analyses.Farzah11 commentedon Feb 6, 2026
This looks like a documentation gap rather than a bug. A short note in the
set_eeg_referencedocumentation (average reference section) could mention that when the reference electrode is not present as a channel, users should consider callingadd_reference_channelbefore applying an average reference, especially for sensor-space analyses.If that placement sounds right, I can open a small documentation PR adding this note.
larsoner commentedon Feb 7, 2026
@Farzah11 yeah I think that would be a good start!
Farzah11 commentedon Feb 16, 2026
Hi
I wanted to work on this Issue, Could you please assign it to me @larsoner
tsbinns commentedon Feb 16, 2026
@Farzah11 Issues aren't typically assigned to individuals here. Feel free to open a PR whenever you have something.
Dpereaptkhamur-13 commentedon Apr 8, 2026
Hi! I'm a GSoC applicant and I'd like to work on this. Since I'm familiar with the documentation structure now, I can handle the updates. May I take this up?
tsbinns commentedon Apr 8, 2026
@Dpereaptkhamur-13 There is already a PR open in #13664 addressing this, although it hasn't seen activity in a while. Let's see if the author there still plans on working on it first. If not, then you'd be very welcome to pick that up, and you could use the existing work in #13664 as a starting point.
Dpereaptkhamur-13 commentedon Apr 8, 2026
Hi @tsbinns, thank you for the feedback! That makes sense. I'm happy to wait and see if the original author wants to continue. If not, I'd be more than glad to pick it up and integrate the existing work from #13664 into this PR. Just let me know whenever you'd like me to proceed!
tsbinns commentedon Apr 15, 2026
@Dpereaptkhamur-13 It's been a week and I haven't heard back from the author of #13664. If you are still interested, you could open a new PR, building on the existing work and unresolved review comments.
Dpereaptkhamur-13 commentedon Apr 16, 2026
Hi @tsbinns, thank you for the update! Yes, I am definitely still interested and would be happy to take this over. I'll open a new PR shortly that incorporates the existing work from #13664 along with the necessary fixes for the unresolved review comments. Thanks for the opportunity!
Dpereaptkhamur-13 commentedon Apr 16, 2026
Hi @tsbinns, I have taken over the work from #13664 as discussed. I've addressed the documentation requirements for #13618 and fixed the towncrier naming issue that was causing check failures in the previous PR. Ready for review!
tsbinns commentedon Jun 26, 2026
Fixed in #13846