def test_patch(subject, **kargs): from src.utils import preproc_utils as pu SUBJECTS_DIR, MMVT_DIR, FREESURFER_HOME = pu.get_links() flat_patch_cut_vertices = utils.load(op.join(MMVT_DIR, subject, 'flat_patch_cut_vertices.pkl')) for hemi in utils.HEMIS: # verts, faces = read_patch(hemi, SUBJECTS_DIR) # write_patch(op.join(MMVT_DIR, 'fsaverage', 'surf', '{}.flat.test.pial'.format(hemi)), verts, faces) # fs_verts, fs_faces = utils.read_pial('fsaverage', MMVT_DIR, hemi, surface_type='inflated') print('Reading inflated surf') fs_verts, fs_faces = nib.freesurfer.read_geometry(op.join(SUBJECTS_DIR, subject, 'surf', '{}.inflated'.format(hemi))) # flat_verts, flat_faces = read_patch(hemi, SUBJECTS_DIR, surface_type='inflated') # good_verts = set(np.unique(flat_faces)) # bad_verts = np.setdiff1d(np.arange(0, flat_verts.shape[0]), good_verts) # bad_faces_inds = set(utils.flat_list_of_lists([verts_faces_lookup[hemi][v] for v in bad_verts])) patch_fname = op.join(SUBJECTS_DIR, subject, 'surf', '{}.inflated.patch'.format(hemi)) print('Writing patch') flat_patch_cut_vertices_hemi = set(flat_patch_cut_vertices[hemi]) write_patch(patch_fname, [(ind, v) for ind, v in enumerate(fs_verts) if ind not in flat_patch_cut_vertices_hemi], set())#flat_faces) print('Reading patch') patch_verts, patch_faces = read_patch(subject, hemi, SUBJECTS_DIR, surface_type='inflated', patch_fname=patch_fname) print('Writing ply') patch_verts *= 0.1 utils.write_ply_file(patch_verts, patch_faces, op.join(MMVT_DIR, subject, 'surf', '{}.flat.pial.test.ply').format(hemi)) print('Finish!')
import os.path as op import time import nibabel as nib import numpy as np import scipy.ndimage from skimage import measure from src.utils import utils from src.utils import preproc_utils as pu from src.preproc import anatomy as anat SUBJECTS_DIR, MMVT_DIR, FREESURFER_HOME = pu.get_links() def create_surf(subject, subcortical_code, subcortical_name): aseg = nib.load(op.join(SUBJECTS_DIR, subject, 'mri', 'aseg.mgz')).get_data() t1_header = nib.load(op.join(SUBJECTS_DIR, subject, 'mri', 'T1.mgz')).header aseg[aseg != subcortical_code] = 255 aseg[aseg == subcortical_code] = 10 aseg = np.array(aseg, dtype=np.float) aseg_smooth = scipy.ndimage.gaussian_filter(aseg, sigma=1) verts_vox, faces, _, _ = measure.marching_cubes(aseg_smooth, 100) # Doesn't seem to fix the normals directions that should be out... faces = measure.correct_mesh_orientation(aseg_smooth, verts_vox, faces) verts = utils.apply_trans(t1_header.get_vox2ras_tkr(), verts_vox) fol = utils.make_dir(op.join(MMVT_DIR, subject, 'subcortical_test')) ply_file_name = op.join(fol, '{}.ply'.format(subcortical_name)) utils.write_ply_file(verts, faces, ply_file_name, False)
import os import os.path as op import numpy as np import shutil import time import csv from mne.label import _read_annot from src.utils import utils from src.utils import preproc_utils as pu SUBJECTS_DIR, MMVT_DIR, FREESURFER_HOME = pu.get_links() APARC2ASEG = 'mri_aparc2aseg --s {subject} --annot {atlas} --o {atlas}+aseg.mgz' def create_freeview_cmd(subject, args):#, atlas, bipolar, create_points_files=True, way_points=False): blender_freeview_fol = op.join(MMVT_DIR, subject, 'freeview') freeview_command = 'freeview -v T1.mgz:opacity=0.3 ' + \ '{0}+aseg.mgz:opacity=0.05:colormap=lut:lut={0}ColorLUT.txt '.format(args.atlas) if args.elecs_names: groups = set([utils.elec_group(name, args.bipolar) for name in args.elecs_names]) freeview_command += '-w ' if args.way_points else '-c ' postfix = '.label' if args.way_points else '.dat' for group in groups: freeview_command += group + postfix + ' ' utils.make_dir(blender_freeview_fol) with open(op.join(blender_freeview_fol, 'run_freeview.sh'), 'w') as sh_file: sh_file.write(freeview_command) print(freeview_command)