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

Add support for dictionary-type ref_channels in set_eeg_reference() #12366

Open
wants to merge 71 commits into
base: main
Choose a base branch
from

Conversation

qian-chu
Copy link
Contributor

@qian-chu qian-chu commented Jan 16, 2024

Implement #12283

To do list:

  • Change docdict["ref_channels_set_eeg_reference"] in mne/utils/docs.py to include dict
  • Change set_eeg_reference() to implement dict based re-referencing
    • Modify _check_before_reference or create new _check_before_dict_reference
    • Modify _apply_reference() or create new _apply_dict_reference()

@qian-chu
Copy link
Contributor Author

@AlexLepauvre I've come up with a short to-do list of things we may need to change to complete the PR. Feel free to modify it! My initial commit only changed the doc and slightly changed reference. Everything is subject to change as you see fit :)

qian-chu and others added 4 commits January 17, 2024 17:53
The current implementation is based on a dictionary mapping a list to each channel. The list contains the channels to average and take as reference for the respective channel.
I have also implemented an assertion to make sure that the channels in the dict correspond to the channels in the instance.
We might want to implement something about bad channels. If bad channels are present in the mapping, they should probably be skipped
mne/_fiff/reference.py Outdated Show resolved Hide resolved
mne/_fiff/reference.py Outdated Show resolved Hide resolved
@qian-chu
Copy link
Contributor Author

qian-chu commented May 3, 2024

Do you think we need a formal test for the addition (in test_reference)?

Copy link
Member

@drammock drammock left a comment

Choose a reason for hiding this comment

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

I've pushed a few commits to tidy up the implementation, and added some inline explanations below in a self-review (the commit messages should hopefully also be helpful in understanding what I did).

I didn't yet look closely at the adequacy of the tests, other than to ensure they all pass locally for me, and adapt them slightly to account for the change making the second return value be None (see comments below for reasoning).

One thing that I think is still missing is handling of SSP projectors, which may become invalid after referencing. The existing _check_before_reference helper has code to handle this; that should be refactored into another helper func and called from the new _check_before_dict_reference func too.

mne/_fiff/reference.py Show resolved Hide resolved
mne/_fiff/reference.py Show resolved Hide resolved
mne/_fiff/reference.py Show resolved Hide resolved
mne/utils/docs.py Show resolved Hide resolved
@qian-chu qian-chu requested a review from drammock July 29, 2024 18:56
@qian-chu
Copy link
Contributor Author

I've pushed a few commits to tidy up the implementation, and added some inline explanations below in a self-review (the commit messages should hopefully also be helpful in understanding what I did).

I didn't yet look closely at the adequacy of the tests, other than to ensure they all pass locally for me, and adapt them slightly to account for the change making the second return value be None (see comments below for reasoning).

One thing that I think is still missing is handling of SSP projectors, which may become invalid after referencing. The existing _check_before_reference helper has code to handle this; that should be refactored into another helper func and called from the new _check_before_dict_reference func too.

Just pushed a fix for the SSP projectors. Do you think we should also add a test for this in test_set_eeg_reference_dict?

@larsoner larsoner added this to the 1.8 milestone Jul 29, 2024
@larsoner larsoner modified the milestones: 1.8, 1.9 Aug 7, 2024
@qian-chu
Copy link
Contributor Author

@drammock When you have time would you mind re-reviewing? Thanks!

@qian-chu
Copy link
Contributor Author

@drammock @hoechenberger @larsoner @cbrnr Hi developers, how would you like us to proceed with this PR? Thanks!

@larsoner
Copy link
Member

Sorry for the slow response, I should be able to look in the coming days!

Copy link
Member

@larsoner larsoner left a comment

Choose a reason for hiding this comment

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

Just one tiny remaining comment which I'll push directly then mark for merge-when-green, thanks in advance @qian-chu !

@@ -74,6 +55,26 @@ def _check_before_reference(inst, ref_from, ref_to, ch_type):
# Need to call setup_proj after changing the projs:
inst._projector, _ = setup_proj(inst.info, add_eeg_ref=False, activate=False)


def _check_before_reference(inst, ref_from, ref_to, ch_type):
Copy link
Member

Choose a reason for hiding this comment

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

Can you move this back up? Moving it below _check_ssp will mess up git blame without any tangible benefit AFAICT?

@larsoner larsoner enabled auto-merge (squash) September 20, 2024 17:32
@larsoner larsoner enabled auto-merge (squash) September 20, 2024 17:33
@larsoner larsoner dismissed drammock’s stale review September 20, 2024 17:33

Comments addressed

@qian-chu
Copy link
Contributor Author

@larsoner Thanks! Glad to see this feature implemented :D

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.

6 participants