Generate a left cerebellum volume source space

Generate a volume source space of the left cerebellum and plot its vertices relative to the left cortical surface source space and the freesurfer segmentation file.

# Author: Alan Leggitt <alan.leggitt@ucsf.edu>
#
# License: BSD (3-clause)

import numpy as np
from scipy.spatial import ConvexHull
from mayavi import mlab
from mne import setup_source_space, setup_volume_source_space
from mne.datasets import sample

print(__doc__)

data_path = sample.data_path()
subjects_dir = data_path + '/subjects'
subj = 'sample'
aseg_fname = subjects_dir + '/sample/mri/aseg.mgz'

Out:

Successfully extracted to: [u'/home/ubuntu/mne_data/MNE-sample-data']

Setup the source spaces

# setup a cortical surface source space and extract left hemisphere
surf = setup_source_space(subj, subjects_dir=subjects_dir, add_dist=False)
lh_surf = surf[0]

# setup a volume source space of the left cerebellum cortex
volume_label = 'Left-Cerebellum-Cortex'
sphere = (0, 0, 0, 120)
lh_cereb = setup_volume_source_space(subj, mri=aseg_fname, sphere=sphere,
                                     volume_label=volume_label,
                                     subjects_dir=subjects_dir)

Out:

Setting up the source space with the following parameters:

SUBJECTS_DIR = /home/ubuntu/mne_data/MNE-sample-data/subjects
Subject      = sample
Surface      = white
Octahedron subdivision grade 6

>>> 1. Creating the source space...

Doing the octahedral vertex picking...
Loading /home/ubuntu/mne_data/MNE-sample-data/subjects/sample/surf/lh.white...
Mapping lh sample -> oct (6) ...
    Triangle neighbors and vertex normals...
Loading geometry from /home/ubuntu/mne_data/MNE-sample-data/subjects/sample/surf/lh.sphere...
    Triangle neighbors and vertex normals...
Setting up the triangulation for the decimated surface...
loaded lh.white 4098/155407 selected to source space (oct = 6)

Loading /home/ubuntu/mne_data/MNE-sample-data/subjects/sample/surf/rh.white...
Mapping rh sample -> oct (6) ...
    Triangle neighbors and vertex normals...
Loading geometry from /home/ubuntu/mne_data/MNE-sample-data/subjects/sample/surf/rh.sphere...
    Triangle neighbors and vertex normals...
Setting up the triangulation for the decimated surface...
loaded rh.white 4098/156866 selected to source space (oct = 6)

You are now one step closer to computing the gain matrix
Sphere                : origin at (0.0 0.0 0.0) mm
              radius  : 120.0 mm
grid                  : 5.0 mm
mindist               : 5.0 mm
MRI volume            : /home/ubuntu/mne_data/MNE-sample-data/subjects/sample/mri/aseg.mgz

Setting up the sphere...
Surface CM = (   0.0    0.0    0.0) mm
Surface fits inside a sphere with radius  120.0 mm
Surface extent:
    x = -120.0 ...  120.0 mm
    y = -120.0 ...  120.0 mm
    z = -120.0 ...  120.0 mm
Grid extent:
    x = -125.0 ...  125.0 mm
    y = -125.0 ...  125.0 mm
    z = -125.0 ...  125.0 mm
132651 sources before omitting any.
57769 sources after omitting infeasible sources.
50733 sources remaining after excluding the sources outside the surface and less than    5.0 mm inside.
Selecting voxels from Left-Cerebellum-Cortex
401 sources remaining after excluding sources too far from VOI voxels
Adjusting the neighborhood info...
Reading /home/ubuntu/mne_data/MNE-sample-data/subjects/sample/mri/aseg.mgz...
Source space : MRI voxel -> MRI (surface RAS)
     0.005000  0.000000  0.000000    -125.00 mm
     0.000000  0.005000  0.000000    -125.00 mm
     0.000000  0.000000  0.005000    -125.00 mm
     0.000000  0.000000  0.000000       1.00
MRI volume : MRI voxel -> MRI (surface RAS)
    -0.001000  0.000000  0.000000     128.00 mm
     0.000000  0.000000  0.001000    -128.00 mm
     0.000000 -0.001000  0.000000     128.00 mm
     0.000000  0.000000  0.000000       1.00
MRI volume : MRI (surface RAS) -> RAS (non-zero origin)
     1.000000 -0.000000 -0.000000      -5.27 mm
    -0.000000  1.000000 -0.000000       9.04 mm
    -0.000000  0.000000  1.000000     -27.29 mm
     0.000000  0.000000  0.000000       1.00
Setting up interpolation...
 794200/16777216 nonzero values [done]

Plot the positions of each source space

# extract left cortical surface vertices, triangle faces, and surface normals
x1, y1, z1 = lh_surf['rr'].T
faces = lh_surf['use_tris']
normals = lh_surf['nn']
# normalize for mayavi
normals /= np.sum(normals * normals, axis=1)[:, np.newaxis]

# extract left cerebellum cortex source positions
x2, y2, z2 = lh_cereb[0]['rr'][lh_cereb[0]['inuse'].astype(bool)].T

# open a 3d figure in mayavi
mlab.figure(1, bgcolor=(0, 0, 0))

# plot the left cortical surface
mesh = mlab.pipeline.triangular_mesh_source(x1, y1, z1, faces)
mesh.data.point_data.normals = normals
mlab.pipeline.surface(mesh, color=3 * (0.7,))

# plot the convex hull bounding the left cerebellum
hull = ConvexHull(np.c_[x2, y2, z2])
mlab.triangular_mesh(x2, y2, z2, hull.simplices, color=3 * (0.5,), opacity=0.3)

# plot the left cerebellum sources
mlab.points3d(x2, y2, z2, color=(1, 1, 0), scale_factor=0.001)

# adjust view parameters
mlab.view(173.78, 101.75, 0.30, np.array([-0.03, -0.01,  0.03]))
mlab.roll(85)
../../_images/sphx_glr_plot_left_cerebellum_volume_source_001.png

Compare volume source locations to segmentation file in freeview

# Export source positions to nift file
nii_fname = data_path + '/MEG/sample/mne_sample_lh-cerebellum-cortex.nii'

# Combine the source spaces
src = surf + lh_cereb

src.export_volume(nii_fname, mri_resolution=True)

# Uncomment the following lines to display source positions in freeview.
'''
# display image in freeview
from mne.utils import run_subprocess
mri_fname = subjects_dir + '/sample/mri/brain.mgz'
run_subprocess(['freeview', '-v', mri_fname, '-v',
                '%s:colormap=lut:opacity=0.5' % aseg_fname, '-v',
                '%s:colormap=jet:colorscale=0,2' % nii_fname, '-slice',
                '157 75 105'])
'''

Out:

Reading FreeSurfer lookup table

Total running time of the script: ( 0 minutes 10.978 seconds)

Gallery generated by Sphinx-Gallery