Skip to content

MRG [+1]: Add scale by distance, orient to surface for mne coreg - #4966

Merged
larsoner merged 7 commits into
mne-tools:masterfrom
larsoner:coreg
Mar 4, 2018
Merged

larsoner merged 7 commits into
mne-tools:masterfrom
larsoner:coreg

Conversation

@larsoner

@larsoner larsoner commented Feb 24, 2018 •

Copy link
Copy Markdown
Member

This PR:

  1. Always loads low-res and high-res surfaces; always uses low-res for geometrical calculations (nice speedups when viewing high-res head), computes a low-res mesh if only a high-res is available (should be very rare)
  2. Adds "orient-to-surface" that turns spheres to cylinders oriented toward the surface.
  3. Adds "scale-by-distance" that scales the points by their distance from the surface.
  4. Adds "mark-inside" that changes the color of points that are inside the head.
  5. Adds HPI points

Still todo:

  • add tests
  • actually add high res->low res decimation step

Using:

mne coreg --subjects-dir=~/mne_data/MNE-sample-data/subjects/ --subject sample --fif ~/mne_data/MNE-sample-data/MEG/sample/sample_audvis_raw.fif --trans ~/mne_data/MNE-sample-data/MEG/sample/sample_audvis_raw-trans.fif --head-opacity=0.5

On current master:

master

On this PR with options --scale-by-distance --orient-to-surface:

pr

@larsoner larsoner mentioned this pull request Feb 24, 2018
26 of 29 tasks
@larsoner

Copy link
Copy Markdown
Member Author

And adding coloration based on points inside (yikes):

inside

This is consistent with mne_analyze:

screen shot 2018-02-23 at 22 55 46

After hitting "Fit using headshape" we get a bit better:

headshape

I'm guessing that the fitting was done with ICP with no constraint on inside/outside. I bet we can get a better head<->MRI trans once we add such a constraint. Although looking at it, we will definitely need to add a limited version (e.g., permit up to N mm inside the head, might need a few mm for some datasets to be tolerant of MRI imperfections and so forth).

@codecov

codecov Bot commented Feb 24, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #4966 into master will increase coverage by 0.05%.
The diff coverage is 88.67%.

@@            Coverage Diff             @@
##           master    #4966      +/-   ##
==========================================
+ Coverage   87.84%   87.89%   +0.05%     
==========================================
  Files         353      353              
  Lines       63068    63435     +367     
  Branches    10795    10853      +58     
==========================================
+ Hits        55401    55758     +357     
- Misses       4916     4919       +3     
- Partials     2751     2758       +7

@larsoner

Copy link
Copy Markdown
Member Author

@christianbrodbeck @agramfort ready for review/merge from my end. You can test with:

$ mne coreg --subjects-dir=~/mne_data/MNE-sample-data/subjects/ \
    --subject sample --fif ~/mne_data/MNE-sample-data/MEG/sample/sample_audvis_raw.fif \
    --trans ~/mne_data/MNE-sample-data/MEG/sample/sample_audvis_raw-trans.fif \
    --head-opacity=0.5 --orient-to-surface --scale-by-distance --mark-inside

@larsoner larsoner changed the title WIP: Add scale by distance, orient to surface for mne coreg MRG: Add scale by distance, orient to surface for mne coreg Feb 26, 2018
@agramfort

Copy link
Copy Markdown
Member

the trans file seems to be ignored for me. Even if I load it manually the plot does not seem to update. Do I miss something?

@larsoner

Copy link
Copy Markdown
Member Author

If I run the code snippet I pasted above I see this:

wtrans

If I run it without the --trans line as:

$ mne coreg --subjects-dir=~/mne_data/MNE-sample-data/subjects/ \
    --subject sample --fif ~/mne_data/MNE-sample-data/MEG/sample/sample_audvis_raw.fif \
    --head-opacity=0.5 --orient-to-surface --scale-by-distance --mark-inside

I see this:

notrans

What do you see? Does everything work for you on master?

@agramfort

Copy link
Copy Markdown
Member

when I run the command you shared I see this:

screen shot 2018-02-27 at 09 31 58

when then I click on lock and back to edit I see this:

screen shot 2018-02-27 at 09 32 12

when I click on load trans, nothing happens. I also see this in master. Can someone else replicate?

I see the same thing with my old 2.7 env.

@larsoner

Copy link
Copy Markdown
Member Author

This is probably caused by the subject not having MRI fiducials defined. In principle this should only disable fitting to fiducials and similar operations. Let's fix it separately because it's unrelated to these changes.

In the meantime, you can fix it by actually defining the LPA/Nasion/RPA (by clicking edit, then LPA, click on image, click Nasion, click image, click RPA, click image, click lock then maybe save).

@larsoner

Copy link
Copy Markdown
Member Author

@agramfort I can replicate but let's fix this problem separately. I have added this to #3934. In the meantime you can use the procedure I outlined above or just extract this sample-fiducials.fif file into your subjects/sample/bem/ directory (an inexact file I produced):

sample-fiducials.zip

Then the commands I pasted above should work.

@agramfort

agramfort commented Feb 27, 2018 via email

Copy link
Copy Markdown
Member

@larsoner

Copy link
Copy Markdown
Member Author

I just wiped my sample dataset and mne-python config, and the procedure (mark points, click save, then do transforms, maybe after resetting) worked fine over here. The GUI should not pop up with "Edit" mode for the fiducials, this means that your sample-fiducials.fif file is somehow not being read / used. Maybe it is corrupted, you could try mine.

Also I realized that this requirement of having MRI fiducials is brought on by the code relying on the transforms being nasion-centric. This can thus be fixed in #4971, where I make the transforms origins the standard Neuromag head coord origin and stardand Freesurfer surface RAS origin (rather than their respective nasion locations).

@larsoner

Copy link
Copy Markdown
Member Author

@cjayb I noticed on the listserv you're using mne coreg. Feel free to give this a try (and #4971) if you want to see some recent possible enhancements.

@christianbrodbeck

Copy link
Copy Markdown
Member

Currently when I toggle "project" for EEG it "orients" them instead.
screen shot 2018-03-02 at 12 02 20 pm

Comment thread mne/gui/_coreg_gui.py Outdated
label="MRI"),
Item('hsp_obj', style='custom',
label="Head shape"),
label="Extra"),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't find "Extra" intuitive – we've been calling them head shape points because they come from the head shape recording, is that misleading in some way?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Some places we call these "head shape", in others we call them "extra" because they are extra points during digitization. At some point we should standardize. I was moving toward standardizing around the Elekta standard but maybe we should stick with "head shape".

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I can def. see how 'extra' is not intuitive and in this context head shape is clearly more so. There is the common case however of EEG electrodes being the only 'head shape' information we have; here whatever points may be acquired (on an Elekta system at least) are likely to be on the face, and consequently more 'extra'-like. +1 for keeping 'head shape' from me though, it's the most natural in this context.

Comment thread mne/gui/_coreg_gui.py Outdated
self._initial_project_eeg = project_eeg
self._initial_orient_to_surface = orient_to_surface
self._initial_scale_by_distance = scale_by_distance
self._initial_mark_inside = mark_inside

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could we make this self._initial_kwargs?

Comment thread mne/gui/_viewer.py Outdated
project_to_surface = Bool(False, label='Project')
orient_to_surface = Bool(False, label='Orient')
scale_by_distance = Bool(False, label='Dist.')
mark_inside = Bool(False, label='Mark')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you could add a desc parameter here to add fly-over help in the GUI?

@christianbrodbeck

Copy link
Copy Markdown
Member

On the PR I also get a lot of these which I don't get on master:

ERROR: In /Users/ilan/minonda/conda-bld/work/VTK-6.3.0/Common/Core/vtkLookupTable.cxx, line 144
vtkLookupTable (0x7fc3f4584230): Bad table range: [1e+299, -1e+299]

ERROR: In /Users/ilan/minonda/conda-bld/work/VTK-6.3.0/Common/Core/vtkLookupTable.cxx, line 144
vtkLookupTable (0x7fc3f4584230): Bad table range: [1e+299, -1e+299]

ERROR: In /Users/ilan/minonda/conda-bld/work/VTK-6.3.0/Common/Core/vtkLookupTable.cxx, line 144
vtkLookupTable (0x7fc3f1491510): Bad table range: [1e+299, -1e+299]

ERROR: In /Users/ilan/minonda/conda-bld/work/VTK-6.3.0/Common/Core/vtkLookupTable.cxx, line 144
vtkLookupTable (0x7fc3f1491510): Bad table range: [1e+299, -1e+299]

ERROR: In /Users/ilan/minonda/conda-bld/work/VTK-6.3.0/Common/Core/vtkLookupTable.cxx, line 144
vtkLookupTable (0x7fc3f1411c10): Bad table range: [1e+299, -1e+299]

ERROR: In /Users/ilan/minonda/conda-bld/work/VTK-6.3.0/Common/Core/vtkLookupTable.cxx, line 144
vtkLookupTable (0x7fc3f1411c10): Bad table range: [1e+299, -1e+299]

@larsoner

larsoner commented Mar 2, 2018

Copy link
Copy Markdown
Member Author

That is probably a VTK6 bug, it shouldn't happen with newer ones

@cjayb

cjayb commented Mar 2, 2018

Copy link
Copy Markdown
Contributor

That is probably a VTK6 bug, it shouldn't happen with newer ones

I get it on vtk 8.1.0 too :(

ERROR: In /Users/prabhu/src/git/VTKPythonPackage/standalone-build/VTK-source/Common/Core/vtkLookupTable.cxx, line 143 vtkLookupTable (0x7f87a46cc550): Bad table range: [1e+299, -1e+299]

@larsoner

larsoner commented Mar 3, 2018

Copy link
Copy Markdown
Member Author

I don't get it on OSX (8.1) or Linux (7.1). What version of Mayavi are you using? What is the command-line to reproduce? (I think I know a workaround, but it would be nice if I could reproduce locally instead of pinging you again to test.)

@cjayb

cjayb commented Mar 3, 2018

Copy link
Copy Markdown
Contributor

Don't hesitate to ping, a tiny contribution to get this great tool polished!

mayavi: Version: 4.5.1.dev0 (git master as on March 1st)

mne coreg --subject=sample --subjects-dir=~/data/templates/MNE-sample-data/subjects # ERRORs

spews the ERROR (specifically: 6 times) into the terminal (tested earlier by starting the gui in an ipython shell). However, just adding the info source fif file make the errors go away:

mne coreg --subject=sample --subjects-dir=~/data/templates/MNE-sample-data/subjects --fif ~/data/templates/MNE-sample-data/MEG/sample/sample_audvis_raw.fif # NO ERRORs

@christianbrodbeck

Copy link
Copy Markdown
Member

What version of Mayavi are you using?

That should be a benefit of an all conda env :) can you reproduce with this?
mne-dev-spec-file.txt

What is the command-line to reproduce?

I just used $ mne coreg and it appears fairly often, just from starting it, from setting the raw file (and none of the new view options are selected)

@larsoner

larsoner commented Mar 3, 2018

Copy link
Copy Markdown
Member Author

Thanks for the tips on how to test. This has been fixed along with other comments in the latest commit. @christianbrodbeck @cjayb feel free to try again.

There is the common case however of EEG electrodes being the only 'head shape' information we have

In master and this PR we only use extra/HSP points plus fiducials (sometimes), but in #4971 this will be changed to allow EEG and HPI points to be used, too (an not to necessarily require fiducials).

@christianbrodbeck

Copy link
Copy Markdown
Member

I have a small layout suggestion in larsoner#28, otherwise I think it looks great and we'll do more thorough testing for #4971

@christianbrodbeck christianbrodbeck changed the title MRG: Add scale by distance, orient to surface for mne coreg MRG [+1]: Add scale by distance, orient to surface for mne coreg Mar 3, 2018
@larsoner

larsoner commented Mar 3, 2018

Copy link
Copy Markdown
Member Author

Great thanks @christianbrodbeck I've merged that nice tweak.

@cjayb will you have time to look again in the next couple of days? @agramfort can't test so I need to get one more +1 from someone else to merge :)

Hopefully in #4971 we can also fix @agramfort's problem.

@cjayb

cjayb commented Mar 4, 2018

Copy link
Copy Markdown
Contributor

I can confirm that the weird table range error is now gone. But it seems the Omit-button really is my nemesis (killed my PR a few years back):

  • exclude the worst outliers (>10mm)
  • fit head shape
  • exclude with a more conservative setting (5 mm)

The error below basically what snagged me back then: after the first omit, the list is no longer consistent with info. I recently thought that the way vertices as included into a source space, using an indexing list, might be a way to fix this, WDYT?

This could also go into #4971 , maybe makes more sense with the new weights-implementation, your call, so a provisional +1 from me on this one!

Coregistration: Excluding 5 head shape points with distance >= 0.010 m.
Coregistration: Excluding 4 head shape points with distance >= 0.005 m.
Exception occurred in traits notification handler for object: <mne.gui._coreg_gui.CoregFrame object at 0x8228b9048>, trait: omit_points, old value: <undefined>, new value: 0
Traceback (most recent call last):
  File "/Users/cjb/miniconda_envs/mne3/lib/python3.6/site-packages/traits/trait_notifiers.py", line 359, in __call__
    self.handler( *args )
  File "/Users/cjb/src/git/mne-python/mne/gui/_coreg_gui.py", line 1563, in _omit_points_fired
    self.model.omit_hsp_points(distance)
  File "/Users/cjb/src/git/mne-python/mne/gui/_coreg_gui.py", line 439, in omit_hsp_points
    new_filter[old_filter] = new_sub_filter
IndexError: boolean index did not match indexed array along dimension 0; dimension is 73 but corresponding boolean dimension is 78

@larsoner

larsoner commented Mar 4, 2018

Copy link
Copy Markdown
Member Author

But it seems the Omit-button really is my nemesis (killed my PR a few years back):

I can confirm that this also happens on master. I'll add it to the TODO list for #4971 then.

@larsoner
larsoner merged commit 24668f4 into mne-tools:master Mar 4, 2018
@larsoner
larsoner deleted the coreg branch March 4, 2018 18:47
britta-wstnr pushed a commit to britta-wstnr/mne-python that referenced this pull request Jul 5, 2018
…-tools#4966)

* WIP: Add distance scaling and orientation

* FIX: Proper parameterization

* ENH: Add HPI points and color of inside points

* FIX: Add tests

* FIX: Fix test

* FIX: Many fixes

* view options layout
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