Skip to content

Instantly share code, notes, and snippets.

@dokato
Last active January 11, 2018 16:43
Show Gist options
  • Save dokato/7420b25ed913e10a934999fbd2944e2c to your computer and use it in GitHub Desktop.
Save dokato/7420b25ed913e10a934999fbd2944e2c to your computer and use it in GitHub Desktop.
Visualization of brain regions after FreeSurfer parcellation
from __future__ import print_function
import os
import numpy as np
from surfer import Brain
from mayavi import mlab
import nibabel as nib
subjects_folder = os.environ["SUBJECTS_DIR"]
subject_id = 'fsaverage'
hemi = "both"
surf = "smoothwm"
parcel_method = 'aparc.annot'
brain = Brain(subject_id, hemi, surf)
if hemi != 'both':
aparc_filename = os.path.join(subjects_folder, subject_id, 'label', '.'.join([hemi,parcel_method]))
labels, ctab, names = nib.freesurfer.read_annot(aparc_filename)
else:
aparc_fn_lh = os.path.join(subjects_folder, subject_id, 'label', '.'.join(['lh',parcel_method]))
aparc_fn_rh = os.path.join(subjects_folder, subject_id, 'label', '.'.join(['rh',parcel_method]))
labels_lh, ctab_lh, names_lh = nib.freesurfer.read_annot(aparc_fn_lh)
labels_rh, ctab_rh, names_rh = nib.freesurfer.read_annot(aparc_fn_rh)
assert names_lh == names_rh, 'rh not eq lh'
names = names_lh
print('Parcellation regions:')
for i in names:
print(i)
reg_name = raw_input('If you want to select parcellation region, specify its name here:')
if not reg_name in names:
vals = np.arange(0,1,1./len(names))
else:
vals = np.zeros(len(names)) - 1
vals[names.index(reg_name)] = 1
if hemi != 'both':
vtx = vals[labels]
vtx[labels==-1] = -1
brain.add_data(vtx, 0, 1, thresh=0, colormap='jet', alpha=0.8)
else:
vtx_lh = vals[labels_lh]
vtx_rh = vals[labels_rh]
vtx_lh[labels_lh == -1] = -1
vtx_rh[labels_rh == -1] = -1
#import pdb; pdb.set_trace()
brain.add_data(vtx_lh, 0, 1, thresh=0, colormap='jet', alpha=0.8, hemi='lh')
brain.add_data(vtx_rh, 0, 1, thresh=0, colormap='jet', alpha=0.8, hemi='rh')
mlab.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment