{ "nbformat_minor": 0, "nbformat": 4, "cells": [ { "execution_count": null, "cell_type": "code", "source": [ "%matplotlib inline" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "\n# Generate a left cerebellum volume source space\n\n\nGenerate a volume source space of the left cerebellum and plot its vertices\nrelative to the left cortical surface source space and the freesurfer\nsegmentation file.\n\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# Author: Alan Leggitt \n#\n# License: BSD (3-clause)\n\nimport numpy as np\nfrom scipy.spatial import ConvexHull\nfrom mayavi import mlab\nfrom mne import setup_source_space, setup_volume_source_space\nfrom mne.datasets import sample\n\nprint(__doc__)\n\ndata_path = sample.data_path()\nsubjects_dir = data_path + '/subjects'\nsubj = 'sample'\naseg_fname = subjects_dir + '/sample/mri/aseg.mgz'" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Setup the source spaces\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# setup a cortical surface source space and extract left hemisphere\nsurf = setup_source_space(subj, fname=None, subjects_dir=subjects_dir,\n add_dist=False)\nlh_surf = surf[0]\n\n# setup a volume source space of the left cerebellum cortex\nvolume_label = 'Left-Cerebellum-Cortex'\nsphere = (0, 0, 0, 120)\nlh_cereb = setup_volume_source_space(subj, mri=aseg_fname, sphere=sphere,\n volume_label=volume_label,\n subjects_dir=subjects_dir)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Plot the positions of each source space\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# extract left cortical surface vertices, triangle faces, and surface normals\nx1, y1, z1 = lh_surf['rr'].T\nfaces = lh_surf['use_tris']\nnormals = lh_surf['nn']\n# normalize for mayavi\nnormals /= np.sum(normals * normals, axis=1)[:, np.newaxis]\n\n# extract left cerebellum cortex source positions\nx2, y2, z2 = lh_cereb[0]['rr'][lh_cereb[0]['inuse'].astype(bool)].T\n\n# open a 3d figure in mayavi\nmlab.figure(1, bgcolor=(0, 0, 0))\n\n# plot the left cortical surface\nmesh = mlab.pipeline.triangular_mesh_source(x1, y1, z1, faces)\nmesh.data.point_data.normals = normals\nmlab.pipeline.surface(mesh, color=3 * (0.7,))\n\n# plot the convex hull bounding the left cerebellum\nhull = ConvexHull(np.c_[x2, y2, z2])\nmlab.triangular_mesh(x2, y2, z2, hull.simplices, color=3 * (0.5,), opacity=0.3)\n\n# plot the left cerebellum sources\nmlab.points3d(x2, y2, z2, color=(1, 1, 0), scale_factor=0.001)\n\n# adjust view parameters\nmlab.view(173.78, 101.75, 0.30, np.array([-0.03, -0.01, 0.03]))\nmlab.roll(85)" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "Compare volume source locations to segmentation file in freeview\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# Export source positions to nift file\nnii_fname = data_path + '/MEG/sample/mne_sample_lh-cerebellum-cortex.nii'\n\n# Combine the source spaces\nsrc = surf + lh_cereb\n\nsrc.export_volume(nii_fname, mri_resolution=True)\n\n# Uncomment the following lines to display source positions in freeview.\n'''\n# display image in freeview\nfrom mne.utils import run_subprocess\nmri_fname = subjects_dir + '/sample/mri/brain.mgz'\nrun_subprocess(['freeview', '-v', mri_fname, '-v',\n '%s:colormap=lut:opacity=0.5' % aseg_fname, '-v',\n '%s:colormap=jet:colorscale=0,2' % nii_fname, '-slice',\n '157 75 105'])\n'''" ], "outputs": [], "metadata": { "collapsed": false } } ], "metadata": { "kernelspec": { "display_name": "Python 2", "name": "python2", "language": "python" }, "language_info": { "mimetype": "text/x-python", "nbconvert_exporter": "python", "name": "python", "file_extension": ".py", "version": "2.7.13", "pygments_lexer": "ipython2", "codemirror_mode": { "version": 2, "name": "ipython" } } } }