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

[MRG] Adding get_montage for montage to BaseRaw objects #7667

Merged
merged 40 commits into from
Sep 8, 2020

Conversation

adam2392
Copy link
Member

Reference issue

Fixes: #7652

What does this implement/fix?

Adds get_montage() and get_ch_positions for ContainsMixin mixin class, which can be used by all instances to get the montage or channel positions.

@adam2392 adam2392 changed the title [WIP] Adding additional containsmixin for montage and ch pos. [WIP] Adding get_montage and get_ch_positions for montage and ch pos. Apr 24, 2020
@adam2392
Copy link
Member Author

@agramfort lmk if you think there's any issues still?

make it work with testing data as it contains all kinds of dig points you don't want to loose with get_montage

the test should do:
m = inst.get_montage()
inst.set_montage()
and inst should be unchanged

I used both the: fif_fname = op.join(io_dir, 'tests', 'data', 'test_raw.fif') and the bv_fname = op.join(io_dir, 'brainvision', 'tests', 'data', 'test.vhdr') along w/ the fif_dig_montage_fname = op.join(data_path, 'montage', 'eeganes07.fif') test datasets. I used a standard montage template, as well as one loaded from data. Note that the on_missing is required to allow full roundtrip of the montage setting.

FYI some issues w/ montage ordering?
I tried testing the actual montage received, but this doesn't work and I still am not sure why, but I think it's because the raw.info['dig'] does not maintain any ordering, so when you try to compare using _get_dig_montage_pos, the assertion doesn't work nicely.

    # XXX: dig montage doesn't maintain ordering...
    assert_array_equal(_get_dig_montage_pos(montage),
                        _get_dig_montage_pos(test_montage))

@codecov
Copy link

codecov bot commented May 12, 2020

Codecov Report

Merging #7667 into master will increase coverage by 0.01%.
The diff coverage is 96.77%.

@@            Coverage Diff             @@
##           master    #7667      +/-   ##
==========================================
+ Coverage   90.36%   90.37%   +0.01%     
==========================================
  Files         455      455              
  Lines       84722    84780      +58     
  Branches    13425    13433       +8     
==========================================
+ Hits        76557    76621      +64     
+ Misses       5309     5300       -9     
- Partials     2856     2859       +3     

@adam2392 adam2392 changed the title [WIP] Adding get_montage and get_ch_positions for montage and ch pos. [MRG] Adding get_montage and get_ch_positions for montage and ch pos. May 13, 2020
@adam2392 adam2392 requested a review from agramfort May 14, 2020 19:05
@agramfort
Copy link
Member

@adam2392 can you see why CIs complain? I'll review when it's green. thanks

@adam2392
Copy link
Member Author

@adam2392 can you see why CIs complain? I'll review when it's green. thanks

Sorry I totally dropped the ball on this, but will ping you when the CIs pass. Doing a check rn.

@adam2392
Copy link
Member Author

adam2392 commented Sep 2, 2020

Okay I just fixed the codecov issue and now I think that once CI is green this is good to go assuming the changes I made addressed your comments sufficiently. (i.e. using the test dataset)

@larsoner larsoner added this to the 0.21 milestone Sep 2, 2020
@agramfort
Copy link
Member

I woudl suggest we limit this PR to get_montage that returns channels that it make sense to use in set_montage (EEG, seeg, ecog, fnirs). I would leave the MEG out of this.

@adam2392
Copy link
Member Author

adam2392 commented Sep 2, 2020

I woudl suggest we limit this PR to get_montage that returns channels that it make sense to use in set_montage (EEG, seeg, ecog, fnirs). I would leave the MEG out of this.

Sorry for the stupid questions (mne-python codebase is still a big one for me to understand), but... does what I have rn meet that? Since @larsoner suggested I just drop the orientations, I don't think I am doing it anymore(?) Or do I have to add an explicit picks somewhere in the get_ch_positions() function?

@larsoner
Copy link
Member

larsoner commented Sep 2, 2020

I think the idea is to get rid of get_ch_positions entirely and just have get_montage which returns positions only for the same set of channels that set_montage can set (EEG, sEEG, ECoG, not sure about fNIRS...)

@agramfort
Copy link
Member

agramfort commented Sep 2, 2020 via email

@adam2392
Copy link
Member Author

adam2392 commented Sep 3, 2020

Okay got rid of get_ch_positions and just left get_montage along w/ the related tests.

Copy link
Member

@agramfort agramfort left a comment

Choose a reason for hiding this comment

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

I am really not sure why tests are so complicated.

I would read in the testing fif file. do a get_montage and set_montage and assert with objecf_diff that chs and dig are the same.

I would then do a set_montage to an EEG file without montage and then do a get_montage and assert it's also consistent with object_diff

@adam2392
Copy link
Member Author

adam2392 commented Sep 3, 2020

Not sure why the round trip for this test dataset is failing: fif_fname = op.join(io_dir, 'tests', 'data', 'test_raw.fif')

I get a mismatch say for example:

# original location of info['chs'][316]['loc']
[-0.01491282  0.11079782  0.07650087  0.          0.          0.
  0.          1.          0.          0.          0.          1.        ]

# new location of info['chs'][316]['loc']
[-0.01491282  0.11079782  0.07650087  0.00235201  0.11096951 -0.03500458
  0.          1.          0.          0.          0.          1.        ]

So I'm not sure if that's coming from the fact that there is a orientation change due to _get_data_as_dict_from_dig? However, the coordinates are the same...

I pushed up the changes so you can see locally. Sorry for the print statements in comments. I'll axe those when this is sorted out.

@larsoner
Copy link
Member

larsoner commented Sep 3, 2020

For EEG, first three entries in loc are the electrode pos, next three are the ref electrode pos (if present)

@adam2392
Copy link
Member Author

adam2392 commented Sep 6, 2020

AFAIK we don't differentiate between different electrode types (EEG, ECoG, sEEG) in dig. In theory just the channel names matching in the montage instance should be good enough for those use cases.

Yes the standard order is fiducials/HPI (if present)/EEG/extra (headshape) points. So I think all that should need to be tweaked here really is ensuring that the ref gets ident==0 and is first in the list of EEG points...

Got it working by adding a "sorting" algorithm applied to the _format_dig_points function.

Copy link
Member

@agramfort agramfort left a comment

Choose a reason for hiding this comment

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

@larsoner merge if you're happy

@larsoner larsoner merged commit 01750aa into mne-tools:master Sep 8, 2020
@larsoner
Copy link
Member

larsoner commented Sep 8, 2020

Thanks @adam2392 !

larsoner added a commit to libertyh/mne-python that referenced this pull request Sep 9, 2020
* upstream/master: (489 commits)
  MRG, DOC: Fix ICA docstring, add whitening (mne-tools#8227)
  MRG: Extract measurement date and age for NIRX files (mne-tools#7891)
  Nihon Kohden EEG file reader WIP (mne-tools#6017)
  BUG: Fix scaling for src_mri_t in coreg (mne-tools#8223)
  MRG: Set pyvista as default 3d backend (mne-tools#8220)
  MRG: Recreate our helmet graphic (mne-tools#8116)
  [MRG] Adding get_montage for montage to BaseRaw objects (mne-tools#7667)
  ENH: Allow setting tqdm backend (mne-tools#8177)
  [MRG, IO] Persyst reader into Raw object (mne-tools#8176)
  MRG, BUG: Fix errors in IO/loading/projectors (mne-tools#8210)
  MAINT: vectorize _read_annotations_edf (mne-tools#8214)
  FIX : events_from_annotation when annotations.orig_time is None and f… (mne-tools#8209)
  FIX: do not project to sphere; DOC - explain how to get EEGLAB-like topoplots (mne-tools#7455)
  [MRG, DOC] Added linear algebra of transform to doc (mne-tools#7087)
  FIX: Travis failure on python3.8.1 (mne-tools#8207)
  BF: String formatting in exception message (mne-tools#8206)
  BUG: Fix STC limit bug (mne-tools#8202)
  MRG, DOC: fix ica tutorial (mne-tools#8175)
  CSP component order selection (mne-tools#8151)
  MRG, ENH: Add on_missing to plot_events (mne-tools#8198)
  ...
marsipu pushed a commit to marsipu/mne-python that referenced this pull request Oct 14, 2020
* Adding additional containsmixin for montage and ch pos.

* Removing unnecessary code.

* Adding up fixes for comments from agram.

* Adding updated.

* Adding updated test montage.

* Adding updated docstring.

* Adding updated docstring.

* Adding change log.

* Adding get_montage doc.

* Fixing pydoc.

* Update mne/channels/channels.py

Co-authored-by: Alexandre Gramfort <[email protected]>

* Wip.

* Adding unit tests for ch positions.

* Coord frame from chs.

* Adding the documentation.

* Deleting script.

* Removing diff.

* Fix flake8.

* Fixing unit test for codecov.

* Update mne/channels/channels.py

Co-authored-by: Eric Larson <[email protected]>

* Adding updated digitatization.

* Adding updated digitatization.

* Adding channels.

* FIX: Ref

* Fixing unit tests.

* Removing get_ch_positions.

* Fix latest.

* trying.

* Merging.

* Update mne/channels/tests/test_montage.py

Co-authored-by: Alexandre Gramfort <[email protected]>

* Fixing reference location.

* Fixing reference location.

* Merging.

* Show test montage breaks.

* Adding updated unit tests.

* Adding updated montage.

* Fixing order.

* Fixing unit tests for fieldtrip.

* Fixing unit tests for fieldtrip.

* Adding fix.

Co-authored-by: Alexandre Gramfort <[email protected]>
Co-authored-by: Eric Larson <[email protected]>
@adam2392 adam2392 deleted the get_ch_coords branch October 30, 2020 16:06
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.

get_montage and get_coordinates for Raw and Montage objects
3 participants