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

[BUG] Fix bad channels error, better default #12382

Merged
merged 5 commits into from
Feb 8, 2024

Conversation

alexrockhill
Copy link
Contributor

Fixes #12351

@@ -3781,7 +3781,7 @@ def stc_near_sensors(
subjects_dir=None,
src=None,
picks=None,
surface="pial",
surface=None,
Copy link
Member

Choose a reason for hiding this comment

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

IIRC there was logic to the original choice -- source spaces are almost always created with white as the surface (for inverse modeling) but non-inverse-based projections like this should almost always use pial. So there was some logic to it. Not sure we should make this change

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why would you model with white and then project to pial?

Copy link
Member

Choose a reason for hiding this comment

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

So far when I've used this function I haven't used the source space for anything other than getting a nice decimation, so calling setup_source_space(subject, subjects_dir=subjects_dir) and everything works fine (things are projected to pial). With this change that code will be broken because now the projection will happen to white instead, since this is the default for setup_source_space. I agree that it's more consistent for people to do setup_source_space(subject, surface="white", subjects_dir=subjects_dir) then this function will behave as expected but I don't think the current implementation will make it clear that's what will be done. Hence why I think we maybe shouldn't bother changing it. If we do change it, I think we need to properly deprecate because people like me would otherwise silently get a different (wrong!) result, namely projection to the white surface whereas before it projected (correctly) to pial when I ask for a decimated version by passing src.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That seems very confusing. Maybe you shouldn't be allowed to pass a surface at all but should just use the surface in the source space?

Copy link
Member

Choose a reason for hiding this comment

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

That would have the same backward compat problem

Copy link
Member

Choose a reason for hiding this comment

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

For deprecations the policy is to preserve the old/current behavior for the upcoming release (1.7), but warn that the default will change in the next only if the behavior will actually change in the next release (1.8) given the current function inputs being passed.

So for example you would warn if someone did setup_source_space(subject) (which defaults to surface='white') and stc_near_sensors(evoked, src=src) (because in 1.7 the default is pial but in 1.8 the behavior under None would change to white); but you would not warn if they did setup_source_space(subject, surface='pial') then stc_near_sensors(evoked, src=src) because the behavior would be the same in 1.7 and 1.8. I think this can be accomplished with pseudocode:

def stc_near_sensors(..., surf=None):
    ...
    if surf is None:
        if src is not None:
            src_surf = ...
            if src_surf != "pial"
                warn(..., DeprecationWarning)
        surf = "pial"

then after 1.7 comes out we simplify to

    if surf is None:
        if src is None:
            surf = "pial"
        else:
            surf = ... # src_surf

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure that the surface is stored in the src instance. I looked and couldn't find it and it looks from setup_source_space that the surface is not stored as far as I can tell... so then you can't fill in the src_surf = .... line. Any ideas on how to handle that?

Copy link
Member

Choose a reason for hiding this comment

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

You could try/except to load the lh.pial surface. If it raises an exception (like file not found) or not np.allclose(src[0]['rr'], pial_rr) you could conclude it's not pial. Hopefully not too painful (5ish lines?) but would do the trick I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yikes okay well at least it will only in the codebase for a version

Copy link
Contributor Author

@alexrockhill alexrockhill Feb 7, 2024

Choose a reason for hiding this comment

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

Ok, I didn't end up doing it exactly how you suggested but I did it so that if you had no argument for surface and passed a src then you would get a warning that the behavior will change but in this version the behavior doesn't change which is what you asked for.

Comment on lines 1671 to 1673
import pdb

pdb.set_trace()
Copy link
Member

Choose a reason for hiding this comment

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

Remove cruft

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review, sorry for the bad mistake!

@@ -0,0 +1 @@
Fix bad channels not handled properly in :func:`mne.stc_near_sensors` as well as default ``surface`` handling for :class:`mne.VolSourceEstimate` inputs as ``src``, by `Alex Rockhill`_.
Copy link
Member

Choose a reason for hiding this comment

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

Not just a bugfix because it's involves a deprecation cycle. So the more important naming and change I think would be 12382.apichange.rst describing how behavior is being changed in stc_near_sensors. Then you can have a 12382.bugfix.rst to describe the bug fix separately if you want (but less critical than calling out the API change via deprecation in the right place in the changelog)

@alexrockhill
Copy link
Contributor Author

Ok I good by me

@larsoner larsoner merged commit 6305bd1 into mne-tools:main Feb 8, 2024
30 checks passed
@larsoner
Copy link
Member

larsoner commented Feb 8, 2024

Thanks @alexrockhill !

FYI to avoid the pre-commit bot having to do stuff you can do locall

pre-commit install --install-hooks

@alexrockhill alexrockhill deleted the seeg_stc branch February 11, 2024 15:50
snwnde pushed a commit to snwnde/mne-python that referenced this pull request Mar 20, 2024
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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.

[BUG] stc_near_sensors unexpected behavior
2 participants