Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Questions about average reference #158

Open
scott-huberty opened this issue Feb 19, 2024 · 0 comments
Open

Questions about average reference #158

scott-huberty opened this issue Feb 19, 2024 · 0 comments

Comments

@scott-huberty
Copy link
Member

Hi @reispat

I'm migrating our conversation from Email to Github.

To sum up our email, you reached out with these questions:

Question 1

Per question 1, I previously told you that a robust average reference was applied to the raw object but I was mistaken. The average reference is currently not applied when applying the rejection policy.

To actually apply the the robust average reference I think we need to add the code here:

def apply(self, pipeline, return_ica=False):
"""Return a cleaned new raw object based on the rejection policy.
Parameters
----------
pipeline : LosslessPipeline
An instance of LosslessPipeline with after the pipeline has
been ran.
return_ica : bool
If ``True``, returns the :class:`~mne.preprocessing.ica` object used to
clean the :class:`~mne.io.Raw` object. Defaults to ``False``.
Returns
-------
mne.io.Raw
An :class:`~mne.io.Raw` instance with the appropriate channels and ICs
added to mne bads, interpolated, or dropped.
"""
# Get the raw object
raw = pipeline.raw.copy()
# Add channels to be rejected as bads
for key in self["ch_flags_to_reject"]:
if key in pipeline.flags["ch"]:
raw.info["bads"] += pipeline.flags["ch"][key].tolist()
# Clean the channels
if self["ch_cleaning_mode"] == "drop":
raw.drop_channels(raw.info["bads"])
elif self["ch_cleaning_mode"] == "interpolate":
raw.interpolate_bads(**self["interpolate_bads_kwargs"])
# Clean the epochs
# TODO: Not sure where we landed on having these prefixed as bad_
# or not by the pipeline. If not prefixed, this would be the
# step that add select types of flags as bad_ annotations.
# Clean the ics
ic_labels = pipeline.flags["ic"]
mask = np.array([False] * len(ic_labels["confidence"]))
for label in self["ic_flags_to_reject"]:
mask |= ic_labels["ic_type"] == label
mask &= ic_labels["confidence"] > self["ic_rejection_threshold"]
flagged_ics = ic_labels.loc[mask]
if not flagged_ics.empty:
flagged_ics = flagged_ics.index.tolist()
pipeline.ica2.exclude.extend(flagged_ics)
pipeline.ica2.apply(raw)
if return_ica:
return raw, pipeline.ica2
return raw

Something like

pipeline.flags["ch"].rereference(pipeline)
# as an aside - the circular reference from instance of LossLessPipeline - to an instance of FlaggedCh - back to the LosslessPipeline instance bothers me!

Would set the robust average reference.

I would also need to add it as a projector for source localization

However If you want RejectionPolicy.apply to compute the robust average reference as a projection without directly applying it to the data, we need to expose the projection kwarg of set_eeg_reference - by adding something like a set_eeg_reference_kwargs parameter to the function call of apply.

Of course, knowing that the average reference isn't currently being applied to the raw object by the pipeline, you could probably just apply it on your own after you are done with the pipeline?

If you'd like to be able to set the reference during the RejectionPolicy.apply method, I can probably implement that feature in the next two weeks - but if anyone else wants to do it, PR welcome!

Question 2

Per question 2 (applying an average reference to data that already have an average reference)

I’m not sure of the effects of applying the common reference again (should have no effect when it was done previously?)…

I don't think that applying an average reference again after it has already been applied will have any noticable effect. But I'll ping @jadesjardins and @christian-oreilly to chime in if i'm wrong here.

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

No branches or pull requests

1 participant