Skip to content

[MRG+1] Glass brain. - #4408

Closed
jaeilepp wants to merge 10 commits into
mne-tools:masterfrom
jaeilepp:plot_glass_brain
Closed

jaeilepp wants to merge 10 commits into
mne-tools:masterfrom
jaeilepp:plot_glass_brain

Conversation

@jaeilepp

Copy link
Copy Markdown
Contributor

This is very much WIP.

I can't find proper documentation on the nifti images and their coordinate frames. Looks like switching from VolumeSourceEstimate it is quite straight forward, but for normal stcs it seems more difficult. Has anyone an idea what is the correct coordinate frame and how to get there or is it even possible with SourceEstimate?

@larsoner

Copy link
Copy Markdown
Member

All sources in VolumeSourceEstimate and SourceEstimate should be in FreeSurfer MRI Surface RAS coordinates. Why is it easy for VolumeSourceEstimate? If it's because there is the interpolator matrix, then you can look at how the transforms are done there in setup_volume_source_space. It also seems like you've already gone through some of this transform math with the dipole plotting stuff (which also used MRI slices).

@jaeilepp

Copy link
Copy Markdown
Contributor Author

Volume estimates have vox_mri_t available. Nifti images need the affine which is used under the hood in nilearn. I can't find documentation of that either.

@larsoner

Copy link
Copy Markdown
Member

Volume estimates have vox_mri_t available.

Yes but we create this in setup_volume_source_space, right?

I can't find documentation of that either.

Does this help?

http://nipy.org/nibabel/coordinate_systems.html

@codecov-io

codecov-io commented Jul 20, 2017 •

Copy link
Copy Markdown

Codecov Report

Merging #4408 into master will decrease coverage by <.01%.
The diff coverage is 96.11%.

@@            Coverage Diff            @@
##           master   #4408      +/-   ##
=========================================
- Coverage    82.7%   82.7%   -0.01%     
=========================================
  Files         349     349              
  Lines       64808   64817       +9     
  Branches    10023   10023              
=========================================
+ Hits        53598   53605       +7     
- Misses       8429    8431       +2     
  Partials     2781    2781

@jona-sassenhagen

Copy link
Copy Markdown
Contributor

I have code for mapping STC labels to the nilearn surface plots, though it's a bit hacky.

One perspective would be to say that glass brain plotting is inherently for volume STCs?

@larsoner

Copy link
Copy Markdown
Member

@jaeilepp

Copy link
Copy Markdown
Contributor Author

I can't figure out what I'm doing wrong here. I never get it to align:
figure_1

@agramfort

agramfort commented Jul 24, 2017 via email

Copy link
Copy Markdown
Member

@jaeilepp

Copy link
Copy Markdown
Contributor Author

I'm trying to transform to RAS+ as described here. Are you saying that the our translations are using some other ref space?

@agramfort

agramfort commented Jul 24, 2017 via email

Copy link
Copy Markdown
Member

@jaeilepp

Copy link
Copy Markdown
Contributor Author

@larsoner

Copy link
Copy Markdown
Member

@jaeilepp I have talked with the FreeSurfer folks about interpolating surface into volume. For FreeSurfer 6+ they now use ribbon.mgz, and for each voxel in the ribbon, take the value at the nearest surface vertex. We can do this same thing.

Basically you'd (conceptually but probably not in practice) upsample the surface STC to the full mesh, then do this nearest-neighbor mapping. Does that make sense? Then you can take this ribbon.mgz and resample it to the MNI space the standard way. WDYT?

In practice, you would probably:

  1. Compute sparse the mesh-upsampling matrix, shape ~ (200000, 10000) (high-res verts x low-res verts)
  2. Compute sparse the nearest-neighbor mapping from the high-resolution mesh onto ribbon.mgz, shape ~ (16777216, 200000) (voxels x high-res verts)
  3. Compute the dot product of these, which will give a matrix ~ (16777216, 10000) (voxels x low-res verts)
  4. Use the appropriate affine transformation to map this ribbon.mgz data to the nilearn plotter.

@larsoner

Copy link
Copy Markdown
Member

Even some very old structurals of mine have lh.ribbon.mgz and rh.ribbon.mgz so I think we should be okay to use them.

@jona-sassenhagen

Copy link
Copy Markdown
Contributor

I just want to say, I think this is a cool thing.

@jaeilepp

Copy link
Copy Markdown
Contributor Author

I have talked with the FreeSurfer folks about interpolating surface into volume

Why do we need to do this?

@agramfort

agramfort commented Jul 25, 2017 via email

Copy link
Copy Markdown
Member

@agramfort

agramfort commented Jul 25, 2017 via email

Copy link
Copy Markdown
Member

@wmvanvliet

Copy link
Copy Markdown
Contributor

Not for this PR, but mne.viz._3d is growing huge. We should split it up into smaller files.

@jaeilepp

Copy link
Copy Markdown
Contributor Author

There's something weird with the glass brain plotter. This is what I get when I plot the data in plot_lcmv_beamformer_volume.
figure_2

@jaeilepp

Copy link
Copy Markdown
Contributor Author

Ok, it seems that the glass_brain requires MNI space and stat_map apparently doesn't (weird). Forgot to read the docs.

@jona-sassenhagen

jona-sassenhagen commented Jul 25, 2017 •

Copy link
Copy Markdown
Contributor

FYI nil earn stat_map now has an axes parameter, thanks to yours truly :D

@larsoner

Copy link
Copy Markdown
Member

I have talked with the FreeSurfer folks about interpolating surface into volume

Why do we need to do this?

the purpose here is to add a plotting routine for dipoles

I'm confused by what you're saying. The function in the code takes stc, which is an instance of SourceEstimate, as an argument. The two plots I see above are 1) surface-source-space vertices and 2) a volume STC. So it seems from code and plotting like this is meant to be a general-purpose plotting routine, if not specifically for surface-based source estimates (which is what SourceEstimate class is) to start.

If it's meant for dipole plotting, shouldn't it take Dipole as an argument, and shouldn't the plots have arrows?

@jaeilepp

Copy link
Copy Markdown
Contributor Author

But why would we want to interpolate surface estimates to volume? And how does the ribbon help in that case?

@larsoner

Copy link
Copy Markdown
Member

But why would we want to interpolate surface estimates to volume?

I assumed this was how you were going to plot everything in a volume. How were you planning to display the surface-based estimates here? Individual dipoles / dots (which I doubt would look very good)? I assumed the glass brain was meant to be like MRI slices, in which case volume interp would make sense.

how does the ribbon help in that case?

I think that the ribbon gives a binary mask of the cortical surface. The procedure I describe above gives you the surf->vol interpolation matrix.

@kingjr

kingjr commented Jul 25, 2017

Copy link
Copy Markdown
Member

Isn't there a problem with the left right lateralization on the first plot? The drawing suggests a frontal view (e.g. the cerebellum is hidden by the temporal cortices), which implicitly suggest the reverse left/right

@jaeilepp

Copy link
Copy Markdown
Contributor Author

The surface plotter actually kind of works already and with right thresholding it's possible to make ok looking figs:
figure_1

Now I'm struggling with the transforms again, as the volume stcs didn't work out of the box as I thought they would.

@larsoner

Copy link
Copy Markdown
Member

The surface plotter actually kind of works already

It puts individual points in, sure, but I don't think this is something we'd actually want to use. It is full of holes. The ribbon-based volume interpolation should be usable and have no holes, though.

@larsoner

Copy link
Copy Markdown
Member

Gist here to produce:
screenshot from 2017-07-25 11-06-12
figure_1

@mmagnuski

Copy link
Copy Markdown
Member

@kingjr
I agree that the front facing brain is confusing with respect to L - R. Its mirrored with respect to vertical axis most likely to save the user from mental rotation effort but it is surprising the way it is drawn.

@jaeilepp

Copy link
Copy Markdown
Contributor Author

Plus looks like the data was earlier being flipped too.

@agramfort

Copy link
Copy Markdown
Member

+1 for MRG

@agramfort agramfort changed the title [MRG] Glass brain. [MRG+1] Glass brain. Jul 28, 2017
Comment thread mne/viz/tests/test_3d.py Outdated
fwd_fname = op.join(data_dir, 'MEG', 'sample',
'sample_audvis_trunc-meg-vol-7-fwd.fif')
fname_vol = op.join(data_dir, 'MEG', 'sample',
'sample_audvis_trunc-grad-vol-7-fwd-sensmap-vol.w')

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.

these aren't used

@larsoner larsoner left a comment

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.

Otherwise LGTM

@jaeilepp
jaeilepp force-pushed the plot_glass_brain branch 2 times, most recently from 261ab51 to 029b46d Compare August 2, 2017 07:34
@larsoner larsoner added this to the 0.15 milestone Aug 4, 2017
@larsoner

larsoner commented Aug 4, 2017

Copy link
Copy Markdown
Member
  • This test takes 20 sec on my machine. Can you make it faster?

  • The test fails on my system with:

======================================================================
ERROR: Test plotting stc with nilearn.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/larsoner/anaconda/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/Users/larsoner/python/mne-python/mne/utils.py", line 843, in dec
    return function(*args, **kwargs)
  File "/Users/larsoner/anaconda/lib/python2.7/site-packages/numpy/testing/decorators.py", line 147, in skipper_func
    return f(*args, **kwargs)
  File "/Users/larsoner/anaconda/lib/python2.7/site-packages/numpy/testing/decorators.py", line 147, in skipper_func
    return f(*args, **kwargs)
  File "/Users/larsoner/python/mne-python/mne/viz/tests/test_source_estimate.py", line 42, in test_plot_glass_brain
    stc.plot_glass_brain('sample', subjects_dir, src=src)
  File "/Users/larsoner/python/mne-python/mne/source_estimate.py", line 980, in plot_glass_brain
    symmetric_cbar=symmetric_cbar, show=show)
  File "/Users/larsoner/python/mne-python/mne/viz/source_estimate.py", line 121, in plot_stc_glass_brain
    img = stc.as_volume(src)
  File "/Users/larsoner/python/mne-python/mne/source_estimate.py", line 1722, in as_volume
    mri_resolution=mri_resolution)
  File "/Users/larsoner/python/mne-python/mne/source_estimate.py", line 2710, in save_stc_as_volume
    shape = src[0]['shape']
KeyError: 'shape'

@larsoner

larsoner commented Aug 8, 2017

Copy link
Copy Markdown
Member

Also this could now use a rebase

@kingjr

kingjr commented Aug 9, 2017

Copy link
Copy Markdown
Member

The plotting seems very slow. Is this because there is a morphing applied? Can we pass a precomputed morph somehow?

@larsoner

larsoner commented Aug 9, 2017

Copy link
Copy Markdown
Member

There is a nearest-neighbor computation in volume space (different from our morphing routines) that could be slow, but someone needs to line profile to determine if it is the bottleneck.

@kingjr

kingjr commented Aug 9, 2017 via email

Copy link
Copy Markdown
Member

@larsoner

larsoner commented Aug 9, 2017

Copy link
Copy Markdown
Member

Is it common to plot a glass brain is that hasn't been normalized ?

The nearest-neighbor approach is the preferred method to map surface into volume space using Freesurfer as of 6.0. They are normalized in the sense that they are transformed to MNI space. However no smoothing is performed, which is a bit different from what we're used to, so some documentation could help.

@kingjr

kingjr commented Aug 9, 2017

Copy link
Copy Markdown
Member

The nearest-neighbor approach is the preferred method to map surface into volume space using Freesurfer as of 6.0. They are normalized in the sense that they are transformed to MNI space.

Can we precompute the nearest neighbor?

Additionally, plotting seems to crash when you give vertices from a single hemisphere:

# this works
vertices = [np.array([12460]), np.array([12460])]
src = SourceEstimate(np.array([1, 1])[:, None], vertices=vertices,
                                   subject='sample', tstep=1, tmin=0)
plot_stc_glass_brain(src, subject='sample', subjects_dir=subjects_dir)
# this does'nt
vertices = [np.array([12460]), np.array([])]
src = SourceEstimate(np.array([1,])[:, None], vertices=vertices,
                                   subject='sample', tstep=1, tmin=0)
plot_stc_glass_brain(src, subject='sample', subjects_dir=subjects_dir)

@larsoner

larsoner commented Aug 9, 2017

Copy link
Copy Markdown
Member

Can we precompute the nearest neighbor?

First someone needs to profile to see if this is indeed limiting the speed. It could be some other step.

Additionally, plotting seems to crash when you give vertices from a single hemisphere

Good catch, this should also be fixed before merge

@larsoner

larsoner commented Aug 16, 2017 •

Copy link
Copy Markdown
Member

Can we precompute the nearest neighbor?

First someone needs to profile to see if this is indeed limiting the speed. It could be some other step.

In the tests at least, 80% of the time is spent in nilearn.plotting.plot_glass_brain.

Closing to open separate PR.

@larsoner larsoner closed this Aug 16, 2017
@larsoner larsoner mentioned this pull request Aug 16, 2017
1 of 4 tasks
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.

10 participants