Skip to content

MRG: Refactor coreg transformations and fits - #4971

Merged
larsoner merged 40 commits into
mne-tools:masterfrom
larsoner:coreg2
Mar 30, 2018
Merged

larsoner merged 40 commits into
mne-tools:masterfrom
larsoner:coreg2

Conversation

@larsoner

@larsoner larsoner commented Feb 26, 2018 •

Copy link
Copy Markdown
Member

This PR builds on #4966 so that must be merged first.

This PR:

  1. Refactors the transforms to only ever transform the MRI points. be done in the _coreg_gui module instead of the _viewer.py module / visuals.
  2. Speeds plotting / interaction substantially by making transform parameters in a list that can be updated at once (previously, updating 3 rot + 3 trans params caused 6 plot updates, now it causes 1)
  3. Refactors the transforms to respect Neuromag head coordinates (rather than being naison-centric)
  4. Adds weighting factors for nasion/LPA/RPA/EEG/HPI. For example, setting the nasion factor to 100 weights the nasion 100x more than any dig point (i.e., pretty much fixes it in place)
  5. Changes the fitting method from fit_point_cloud-based (which uses nearest calculations on every objective function evaluation) to iterative-closest-point (ICP; computes nearest neighbors and does the fit using those matched pairs). This should replicate what people are presumably more accustomed to from mne_analyze and in the MEG literature. It is fairly quick, so pressing it multiple times to see/watch fit convergence isn't bad and also has some pedagogical value I think.
  6. Adds coordinate frame axes to the display
  7. Make display more compact
  8. Save and restore scene scale factor and size
  9. Adds status bar to monitor changes / convergence
  10. Sets head opaque when editing MRI fiducials, sets back to transparent afterward
  11. Sets background color black by default
  12. Fixes popup parenting (closing window will close options dialogs)
  13. Changes units to most readable / natural (mm, deg, percent)
  14. Allows ICP fitting only if >= 10 points are available
  15. Switches to doing nearest-neighbor calculations using high res surface by using a nearest_calc class-based scheme that is sufficiently fast

Also:

  • fixes (by disabling) scaling of sphere surfaces
  • removes Save trans button that was errantly added
  • does not require MRI fiducials for transforms to take effect
  • allows weights for EEG electrodes and HPI coils (default to zero)
  • adds "reset" buttons for scaling and trans
  • changes "Save subject as..." to "Save as..." for more generic term
  • allow setting view coordinate frame to head- or MRI-based
  • fix bug with point orientation updates
  • fix bug where excluding outliers (exclude 10mm, then exclude 5 mm) causes an error
  • fix OSX HiDPI size saving
  • make SCENE_SCALE persist
  • save scene size
  • fix bug with saving old source spaces (negative distance)
  • fix layout on OSX (make left panel scrollable)
  • fix switching terrain->arcball
  • add ICP fitting options (n_iterations, convergence criteria)
  • use high-res mesh for ICP
  • improve correspondence between mne_analyze and mne coreg ICP
  • fix scaling single param source space distances
  • MRI fiducials should be in mm not m
  • add nearest-fid-mode for ICP
  • fix double window popup?
  • see if latest libs avoid warnings
  • add tests for _DistanceQuery
  • query and set window size rather than scene size
  • ensure KIT guis work

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

codecov Bot commented Feb 26, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #4971 into master will increase coverage by 0.01%.
The diff coverage is 86.65%.

@@            Coverage Diff             @@
##           master    #4971      +/-   ##
==========================================
+ Coverage      88%   88.01%   +0.01%     
==========================================
  Files         356      356              
  Lines       64462    65128     +666     
  Branches    11018    11146     +128     
==========================================
+ Hits        56729    57324     +595     
- Misses       4950     4975      +25     
- Partials     2783     2829      +46

@cjayb

cjayb commented Mar 1, 2018

Copy link
Copy Markdown
Contributor

Looks really good, I much enjoy the didactic twist of moving the MRI into the head point cloud, completely agree on moving away from nasion-centered transforms. Thank you for finally figuring out the EEG electrode situation (my failed attempt a few years back is still nagging :)

I guess the "Save trans"-button is WIP?

@larsoner

larsoner commented Mar 1, 2018

Copy link
Copy Markdown
Member Author

I errantly added that one, actually. The "Save as..." button saves only the trans if you did no scaling, and saves the surrogate + trans if you did scaling. I didn't realize it did the former until I had added the button, then forgot to remove it...

@cjayb

cjayb commented Mar 1, 2018

Copy link
Copy Markdown
Contributor

Ah! Suggest going with "Save as..." then, instead of the current "Save Subject As..."; the latter implied to me that a scaled subject would be saved to subjects_dir... Otherwise +1 to have a single button for saving that works in either scenario.

BTW: Any idea how the HPI coil orientation is determined? I happen to get this one funky one on the rhs (looks like it's 90 deg rotated, pretty sure it wasn't during recording, I think I would have felt it!)

image

@larsoner

larsoner commented Mar 1, 2018 via email

Copy link
Copy Markdown
Member Author

@larsoner larsoner changed the title WIP: Refactor coreg transformations and fits MRG: Refactor coreg transformations and fits Mar 5, 2018
@larsoner

larsoner commented Mar 5, 2018

Copy link
Copy Markdown
Member Author

Okay @cjayb @christianbrodbeck I think this fixes everything we talked about from the previous PR and adds a bunch of functionality (see top comment). Feel free to give it a try.

@agramfort this might even fix things on your system! A good starting command is:

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

@larsoner

larsoner commented Mar 6, 2018

Copy link
Copy Markdown
Member Author

I refactored a bunch of display params on Linux and now OSX can be unhappy. I'll set back to WIP and let people know when it's fixed. In the meantime you can still test it.

@larsoner larsoner changed the title MRG: Refactor coreg transformations and fits WIP: Refactor coreg transformations and fits Mar 6, 2018
@cjayb

cjayb commented Mar 7, 2018

Copy link
Copy Markdown
Contributor

Some initial thoughts on GUI elements:

  • I'm not crazy about the drop-down list for fiducials: adds some clicking; IMHO the previous radio buttons were nicer
  • the Omit-button works, yay!
  • how's the ICP supposed to work? Is the intention to let it run once per click, and leave it to the user to decide when to stop? It /is/ fast, but... But would you consider running a few iterations based on some stopping criterion (delta-trans or delta-rot)?

Indeed my immediate trouble is with displaying (qt5, pyqt5, vtk8, os x 10.13.3)

  • initially started the gui (mne coreg ... sample ...) while connected to a large external screen: resizing the window works ok
  • unplug: window no longer fits on the 13" screen (retina display in the default 1280x800 configuration)
    • resizing from top corners doesn't work, bottom corners outside of view
    • going fullscreen doesn't help (hidpi-related?)
  • close gui, restart it (only main 13" screen attached): window still larger than screen, and additionally the below exception on startup (is there an old config file I need to delete somewhere?)
cjb@hathor $ mne coreg --subject sample --subjects-dir=~/data/templates/MNE-sample-data/subjects --fif ~/data/templates/MNE-sample-data/MEG/sample/sample_audvis_raw.fif --trans ~/data/templates/MNE-sample-data/MEG/sample/sample_audvis_raw-trans.fif
/Users/cjb/miniconda_envs/mne3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
    Triangle neighbors and vertex normals...
    Triangle neighbors and vertex normals...
Exception occurred in traits notification handler.
Please check the log file for details.
Exception occurred in traits notification handler for object: <mne.gui._viewer.HeadViewController object at 0x1c2921a570>, trait: interaction, old value: trackball, new value: terrain
Traceback (most recent call last):
  File "/Users/cjb/miniconda_envs/mne3/lib/python3.6/site-packages/traits/trait_notifiers.py", line 538, in _dispatch_change_event
    self.dispatch( handler, *args )
  File "/Users/cjb/miniconda_envs/mne3/lib/python3.6/site-packages/traits/trait_notifiers.py", line 501, in dispatch
    handler( *args )
  File "/Users/cjb/src/git/mne-python/mne/gui/_viewer.py", line 114, in on_set_interaction
    self.scene.interactor.interactor_style
AttributeError: 'NoneType' object has no attribute 'interactor_style'

@larsoner

larsoner commented Mar 7, 2018

Copy link
Copy Markdown
Member Author

I'm not crazy about the drop-down list for fiducials: adds some clicking; IMHO the previous radio buttons were nicer

I agree, but it was necessary to save space (shaved over 100 pixels from the left and right columns). The other problem I had before was that LPA/Nasion/RPA lined up with the X/Y/Z, which makes it visually confusing (they seem vertically related but aren't). These two things made it seem worth the switch even for extra clicks.

window no longer fits on the 13" screen

Yes this is a HiDPI resolution problem and on the todo list above. I need to refactor the there panels on the left into a single panel so that it, too, can scroll like the right one.

close gui, restart it

I ended up getting rid of the scene-size-saving step because of some TraitsUI limitations, so this bug is already "fixed". (You'll need to manually set your mne config values to fix it, but users of the final PR won't have that problem.)

AttributeError: 'NoneType' object has no attribute 'interactor_style'

This is a weird one, I don't get this on OSX or Linux. But it should be fixable by delaying the interactor-setting step.

@larsoner

larsoner commented Mar 7, 2018

Copy link
Copy Markdown
Member Author

how's the ICP supposed to work? Is the intention to let it run once per click, and leave it to the user to decide when to stop?

Yes, that's more or less how it's done in mne_analyze for example. We could add a number-of-iterations parameter but usually don't see people use it much in mne_analyze. We could try to automate the procedure, too, but I worry about degenerate cases somehow breaking things (e.g., if the dig is bad or points aren't omitted properly and parameters start to explode, especially during rescaling steps), so I was going to omit that for simplicity for now.

@christianbrodbeck

Copy link
Copy Markdown
Member

I'm not crazy about the drop-down list for fiducials

I agree that the drop-down menu for this seems clumsy

it was necessary to save space

Maybe some less frequently used items like "interaction" and "Display options" could go into a menu bar to save space? I think it should be possible with traitsui: http://docs.enthought.com/traitsui/traitsui_user_manual/handler.html#menus-and-menu-bars

@cjayb

cjayb commented Mar 7, 2018

Copy link
Copy Markdown
Contributor

You'll need to manually set your mne config values to fix it

Sorry, which parameter is it exactly?

I worry about degenerate cases somehow breaking things

Agree, the risk of automating the ICP iteration-process is risky, fine to let it be user-controlled

@christianbrodbeck

Copy link
Copy Markdown
Member

Maybe even better would be to replace the mayavi toolbar that sits on top of the scene? I don't know if it's possible to modify it so that the x/y/z buttons line up with our coordinate system?

@larsoner

larsoner commented Mar 7, 2018

Copy link
Copy Markdown
Member Author

Maybe even better would be to replace the mayavi toolbar that sits on top of the scene?

I thought about removing this but that toolbar is pretty useful actually. You can tweak viz and save very easily which is nice.

Sorry, which parameter is it exactly?

MNE_COREG_SCENE_WIDTH / HEIGHT

Maybe some less frequently used items like "interaction" and "Display options" could go into a menu bar

I can move the scale and interaction mode into "Display options", and move that button somewhere that it doesn't add space (maybe to the right of top/right/front/left). I need to refactor that panel anyway.

I agree that the drop-down menu for this seems clumsy

To me the bigger concern than space (though I listed this second) was the false impression that the L/N/R were related to X/Y/Z due to visual vertical alignment. I'll try to find some way to avoid this while putting buttons back.

@christianbrodbeck

Copy link
Copy Markdown
Member

(toolbar) I thought about removing this but that toolbar is pretty useful actually

Yeah that's why I was wondering whether it could be modified so that x/y/z work properly, or just by adding additional buttons...

@cjayb

cjayb commented Mar 7, 2018

Copy link
Copy Markdown
Contributor

MNE_COREG_SCENE_WIDTH / HEIGHT

I basically get random behaviour setting these in mne-python.json :( No combination (even halving the value to accommodate hidpi) seems to make the window fit onto my (blasted) screen.

@larsoner

larsoner commented Mar 7, 2018

Copy link
Copy Markdown
Member Author

Yes there is a height limitation due to the concatenation of three panels on the left. I'm going to fix it by:

  1. trimming vertical space on the elements
  2. embedding them in a single panel (what is done on the right side) to allow using a scrollbar when necessary, but this will require a lot of refactoring

@larsoner

larsoner commented Mar 7, 2018

Copy link
Copy Markdown
Member Author

Okay, layout works on HiDPI OSX now by tightening up the layout and relying a bit more on tooltips and implicit labels rather than explicit labels. Here is master on OSX:

screen shot 2018-03-07 at 12 41 59

This PR:

screen shot 2018-03-07 at 12 42 26

And when in MRI fiducial editing mode master:

screen shot 2018-03-07 at 12 42 05

And this PR:

screen shot 2018-03-07 at 12 42 32

I think it's ready for another look. @cjayb @christianbrodbeck you might need to set MNE_COREG_SCENE_WIDTH/HEIGHT in ~/.mne/mne-python.json. In particular height should be sufficiently small (700 or less) on 13" HiDPI OSX.

@cjayb

cjayb commented Mar 7, 2018 •

Copy link
Copy Markdown
Contributor
  • previous AssertionError Re: 'interactor_style' fixed
  • something not working with my config, no effect on scene height
cjb@hathor $ grep SCENE_HEIGHT ~/.mne/mne-python.json
"MNE_COREG_SCENE_HEIGHT": "400",

Here's a shot of my whole screen. If I Grab the window, the parts not show on-screen are visible on the screenshot :-/ Any suggestions on which config to flush...? Edit: Also, the scrolling works in the right pane but not left.

coreg_gui_screen

@larsoner

larsoner commented Mar 7, 2018

Copy link
Copy Markdown
Member Author

@cjayb argh okay. I'll need to actually do the scrolling refactoring I mentioned (I hoped to avoid it by saving space). It's probably worthwhile anyway.

BTW command-shift-4 then "space" on OSX will allow you to capture a single window.

@christianbrodbeck

Copy link
Copy Markdown
Member

I'll need to actually do the scrolling refactoring

What if you put the view panel back below the scene? You could make it less tall by moving the view options to the side (or into a menu bar)?

@christianbrodbeck

Copy link
Copy Markdown
Member

Or change the coordinate system to be consistent with Mayavi's x/y/z? Then the view buttons would not even be needed because the the Mayavi toolbar x/y/z buttons could be used instead?

@larsoner larsoner changed the title WIP: Refactor coreg transformations and fits MRG: Refactor coreg transformations and fits Mar 30, 2018
@larsoner

Copy link
Copy Markdown
Member Author

Okay everything should be working and I think everyone is happy with this. I'll go ahead and merge if CIs come back happy!

@agramfort

agramfort commented Mar 30, 2018 via email

Copy link
Copy Markdown
Member

@larsoner
larsoner merged commit c51705e into mne-tools:master Mar 30, 2018
@larsoner

Copy link
Copy Markdown
Member Author

Nothing like a +2,086 −1,572 PR to send us into the weekend :)

@larsoner
larsoner deleted the coreg2 branch March 30, 2018 19:40
britta-wstnr pushed a commit to britta-wstnr/mne-python that referenced this pull request Jul 5, 2018
* ENH: Refactor fitting and origins

* FIX: Many fixes

* FIX: Fix style

* FIX: Dont save size

* FIX: Multiple fixes

* FIX: Fix highlighting

* ENH: Save window size and add status bar

* ENH: Refactor

* FIX: Fix tests

* FIX: Many fixes

* FIX: Revert silly change

* FIX: Tweak button size

* FIX: Text and sizes

* FIX: Opacity

* ENH: Refactor options

* ENH: ICP iterations

* FIX: Tweak sizes

* FIX: Fix scaling and units

* FIX: Aesthetics

* FIX: Allow 1-element scale

* FIX?: Fix logic

* FIX: Fix checks

* ENH: Allow matching to surface

* FIX: Tweak nasion [ci skip]

* ENH: Better status updates

* FIX: Add tests

* fix status bar queue info

* use Enum for coord_frame

* sync bidirectionally with view options dialog

(in case two dialogs are open)

* Prevent double view options dialog

* [WIP] FIX kit2fiff GUI (mne-tools#30)

* TEST GUIs:  raise exceptions in change handlers

* RF use decorator for lazy import

* fix

* TEST:  raise exceptions in change handlers in all mayavi tests

* FIX: Dont scale sphere surfaces

* FIX: kit2fiff

* FIX: Fix parenting

* FIX: Flake

* FIX: Fix size

* FIX: Actually copy reg files

* [WIP] fix kit2fiff (mne-tools#31)

* remove gui.combine_kit_markers()  (it is redundant with gui.kit2fiff())

* TEST kit2fiff-gui:  set marker file

* FIX kit2fiff gui point visualization & view

* test_kit2fiff:  update assert statements

* STY

* FIX: Migrate to pytest

* FIX: Fix test
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.

5 participants