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, MRG] Fix coordinate frames #982

Merged
merged 7 commits into from
Mar 9, 2022
Merged

Conversation

alexrockhill
Copy link
Contributor

@alexrockhill alexrockhill commented Mar 8, 2022

Fixes leftovers from #980.

  • The fiducials were not removed before setting the montage, causing an unwanted transformation, now they are removed and then put back which is good because we want them in the sidecar
  • There was inefficiency in mne_bids.utils._get_landmarks because the tests for the same coordinate frame are done for raw.get_montage so it's better not to duplicate these
  • Whew, the end of the tutorial that I finished is long winded, but I think if you are given a template, there is no way to know if it's in surface RAS, voxels or scanner RAS, it's not in the definition, so if we are supporting this, I think it's a requirement to walk users through it

I think this cleans up a lot of loose threads but there is a lot of added narrative so if @hoechenberger or @agramfort or both want to read through it and make sure it's all necessary that would be really helpful. Other than that, I am much more confident that everything works now.

cc @adam2392 also because you work with this a lot too

Merge checklist

Maintainer, please confirm the following before merging:

  • All comments resolved
  • This is not your own PR
  • All CIs are happy
  • PR title starts with [MRG]
  • whats_new.rst is updated
  • PR description includes phrase "closes <#issue-number>"

@codecov
Copy link

codecov bot commented Mar 8, 2022

Codecov Report

Merging #982 (455d488) into main (5798e49) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #982   +/-   ##
=======================================
  Coverage   94.99%   95.00%           
=======================================
  Files          25       25           
  Lines        3678     3665   -13     
=======================================
- Hits         3494     3482   -12     
+ Misses        184      183    -1     
Impacted Files Coverage Δ
mne_bids/utils.py 95.74% <ø> (-0.36%) ⬇️
mne_bids/dig.py 98.08% <100.00%> (+0.50%) ⬆️
mne_bids/read.py 95.93% <100.00%> (+0.02%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5798e49...455d488. Read the comment docs.

@alexrockhill
Copy link
Contributor Author

Ok, everything should pass and that's all I wanted to change.

I just added an error for consistency where previously there would have been a cryptic empty dictionary error if there were not meg landmarks in the raw file since I was changing that part of the code already.

Comment on lines +121 to +123
# MNE-Python, if written with :func:`mne_bids.write_raw_bids`, must have
# an :attr:`mne_bids.BIDSPath.space` specified, and will be read in with
# the montage channel locations set to the coordinate frame 'unknown'.
Copy link
Member

Choose a reason for hiding this comment

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

Is this true? Or perhaps it can just be made a little bit more clear.

Maybe it would also help to provide a direct link to the specification for that matter?

https://bids-specification.readthedocs.io/en/stable/04-modality-specific-files/04-intracranial-electroencephalography.html#electrode-description-_electrodestsv

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If it's not in MNE_TO_BIDS_FRAMES and there is no BIDSPath.space then it won't be written. It could default to 'Other' that's a -1 from me, I don't think Other should be in BIDS.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You get

warn("Coordinate frame could not be inferred from the raw object "
                 "and the BIDSPath.space was none, skipping the writing of "
                 "channel positions")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's linked at the top, we could add it there, but the whole narrative is trying to get them not to save data in a template coordinate frame if possible so handing a list of template coordinate frames that close to the beginning of the example seems counterproductive

mne_bids/dig.py Outdated
raise ValueError('All HPI, electrodes, and fiducials must be in the '
'same coordinate frame. Found: "{}"'
.format(coord_frame))
if raw.get_montage() is None or raw.get_montage().get_positions() is None:
Copy link
Member

Choose a reason for hiding this comment

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

Why would montage not be None, but positions are None?

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 got an error in the tests that positions were none I thought, let me double check

Copy link
Member

Choose a reason for hiding this comment

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

Okay I might be wrong. Maybe it's a downstream consequence of mne

Copy link
Member

@adam2392 adam2392 left a comment

Choose a reason for hiding this comment

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

Minor changes, but otw lgtm

@agramfort
Copy link
Member

tell me when I should look here again.

@alexrockhill
Copy link
Contributor Author

Ok, I added @adam2392 's comments, if you could review @agramfort that would be really helpful

@alexrockhill
Copy link
Contributor Author

The narrative and code is really lengthy at the end to describe all the coordinate frame transforms. I think in the future we could refactor mne_bids.read.get_head_mri_trans to work with iEEG data but that would require the template being in BIDS format with fiducials in the sidecar. Since we don't have fsaverage that way in the sample data, maybe this isn't worth doing.

What might be worth doing eventually is to download all the templates, find fiducials on their T1s and save them somewhere in mne_bids. That way, montage could be transformed to mri by mne_bids.dig._read_dig_bids and then they could just use mne.compute_native_head_t to get the trans. The only thing is that we'd have to do the same circumventing the transform to head by raw.set_montage so that the data could be returned in mri so that they could compute the trans. That or read_raw_bids could return a trans as well which actually might be a nice convenience for EEG and MEG too.

@adam2392
Copy link
Member

adam2392 commented Mar 8, 2022

What might be worth doing eventually is to download all the templates, find fiducials on their T1s and save them somewhere in mne_bids.

Seems like a good GSoC issue :p. Would be a pretty valuable contribution.

@alexrockhill
Copy link
Contributor Author

I guess it wouldn't be too hard but the only thing is that you'd have to infer whether the channels.tsv coordinates were in surface RAS, voxels or scanner RAS. You could also have a keyword to specific with auto as the default.

@alexrockhill
Copy link
Contributor Author

@adam2392 look okay now?

@alexrockhill
Copy link
Contributor Author

I opened an issue on bids-specification which is really the most upstream point where this should be solved, maybe we'll get a bit of help

@alexrockhill
Copy link
Contributor Author

Good to go? It would be nice to get this in as main is actually a bit broken right now (in the case of a montage with non-head coordinates being passed)

Copy link
Member

@hoechenberger hoechenberger left a comment

Choose a reason for hiding this comment

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

Anyone, feel free to merge once CI goes green!

@alexrockhill
Copy link
Contributor Author

Ok, all green

@hoechenberger hoechenberger merged commit 78e8de8 into mne-tools:main Mar 9, 2022
@hoechenberger
Copy link
Member

Thanks, @alexrockhill!

@alexrockhill alexrockhill deleted the fix branch March 9, 2022 18:50
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.

4 participants