Skip to content

ENH: make compute_covariance work with Evoked objects - #14129

Open
BabaSanfour wants to merge 4 commits into
mne-tools:mainfrom
BabaSanfour:compute-cov-evoked
Open

ENH: make compute_covariance work with Evoked objects#14129
BabaSanfour wants to merge 4 commits into
mne-tools:mainfrom
BabaSanfour:compute-cov-evoked

Conversation

@BabaSanfour

@BabaSanfour BabaSanfour commented Aug 3, 2026

Copy link
Copy Markdown
Member

Reference issue (if any)

Closes #14115.

What does this implement/fix?

Accept Evoked through existing epochs argument and reject keep_sample_mean=False. With Evoked data we treat samples will be treated as observations and channels as variables: spatial covariance over time, not an estimate of trial-to-trial noise covariance

Possible alternatives/Improvements

  1. as an alternative we can wrap Evoked in Epoch object.
  2. for arguments we accept inst (for instance instead of epochs) but this will require 2-3 releases and some backward compatibility maintenance.

Additional information

With the help of claude Opus5; all lines added were read and double checked.

@BabaSanfour

Copy link
Copy Markdown
Member Author

@larsoner PR is ready; code works as intended, one improvement would be changing epochs to inst, this would require some deprecation cycles (?).

Comment thread mne/cov.py
The epochs.
epochs : instance of Epochs | Evoked | list of Epochs
The epochs or evoked response. For an evoked response, time samples
are treated as observations.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this can now also be Evoked, then the parameter should no longer be called epochs but rather inst (which is the name we use in other places when it can be several different things).

Comment thread mne/cov.py
"`keep_sample_mean=False` cannot be used when `epochs` is an "
"Evoked instance"
)
return [epochs], True

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to let the function do one thing: unpack the covariance inputs, and not also return whether the instance was an Evokeds or not. It's easy enough to do with isinstance(inst, Evoked).

Comment thread mne/cov.py
epochs = _unpack_epochs(epochs)
else:
epochs = sum([_unpack_epochs(epoch) for epoch in epochs], [])
epochs, is_evoked = _unpack_covariance_inputs(epochs, keep_sample_mean)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also here it should be inst instead of epochs

@wmvanvliet

Copy link
Copy Markdown
Contributor

This is useful, also for the upcoming dipole fitting GUI :)

@BabaSanfour

Copy link
Copy Markdown
Member Author

Thanks @wmvanvliet , i was going to make it an inst .. but not sure how i should handle deprecation here. Should i just change it ?

@wmvanvliet

Copy link
Copy Markdown
Contributor

it's only not compatible if someone explicitly does mne.compute_covariance(epochs=epochs) which I don't think is many. We could add a named parameter epochs that will raise a deprecation warning when used but otherwise just maps to inst.

Comment thread mne/cov.py
Comment on lines 898 to 904
epochs,
keep_sample_mean=True,
tmin=None,
tmax=None,
projs=None,
*,
on_few_samples="warn",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BabaSanfour something like this:

Suggested change
inst,
keep_sample_mean=True,
tmin=None,
tmax=None,
projs=None,
*,
epochs=None, # deprecated
on_few_samples="warn",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ENH: make compute_covariance work with Evoked objects

3 participants