def surf_convert(fsdir, t1files, surffiles, output_directory=None, rm_orig=False, fsconfig="/i2bm/local/freesurfer/SetUpFreeSurfer.sh"): """ Export FreeSurfer surfaces to the native space. Note that all the vetices are given in the index coordinate system. The subjecy id in the t1 and surf files must appear in the -3 position: xxx/subject_id/convert/t1.nii.gz <unit> <input name="fsdir" type="Directory" description="The freesurfer working directory with all the subjects."/> <input name="output_directory" type="Directory" description="The conversion destination folder."/> <input name="t1files" type="List_File" description="The t1 nifti files."/> <input name="surffiles" type="List_File" description="The surface to be converted."/> <input name="rm_orig" type="Bool" description="If true remove the input surfaces."/> <input name="fsconfig" type="File" description="The freesurfer configuration batch."/> <output name="csurffiles" type="List_File" description="The converted surfaces in the native space."/> </unit> """ # Create a t1 subject map t1map = {} for fname in t1files: subject_id = fname.split("/")[-3] if subject_id in t1map: raise ("Can't map two t1 for subject '{0}'.".format(subject_id)) t1map[subject_id] = fname # Convert all the surfaces csurffiles = [] for fname in surffiles: # Get the t1 reference image subject_id = fname.split("/")[-3] t1file = t1map[subject_id] t1_image = nibabel.load(t1file) # Compute the conformed space to the native anatomical deformation asegfile = os.path.join(fsdir, subject_id, "mri", "aseg.mgz") physical_to_index = numpy.linalg.inv(t1_image.get_affine()) translation = tkregister_translation(asegfile, fsconfig) deformation = numpy.dot(physical_to_index, translation) # Load and warp the mesh # The mesh: a 2-uplet with vertex (x, y, z) coordinates and # mesh triangles mesh = freesurfer.read_geometry(fname) surf = TriSurface(vertices=apply_affine_on_mesh(mesh[0], deformation), triangles=mesh[1]) # Save the mesh in the native space outputfile = fname + ".native" surf.save(os.path.dirname(outputfile), os.path.basename(outputfile)) csurffiles.append(outputfile) # Clean input surface if specified if rm_orig: os.remove(fname) return csurffiles
flirt(nodif_file, t1_file, omat=trf_file, out=reg_file, usesqform=False, cost="normmi", dof=6) """ Launch the tractography on the requested point of the cortical surface on the selected hemisphere """ # Load the white mesh in the diffusion space surface = TriSurface.load(whitefile) voxel_diff_to_t1 = flirt2aff(trf_file, nodif_file, t1_file) voxel_t1_to_diff = numpy.linalg.inv(voxel_diff_to_t1) white_diff_vertices = apply_affine_on_mesh(surface.vertices, voxel_t1_to_diff) # Select the vertices of interest if vertices_indices is None: vertices_indices = range(len(surface.vertices)) # Go through all the hemisphere vertices textures = {} for index in vertices_indices: # Select the seeding vertex point = white_diff_vertices[index] # Create a directory for each seeding vertex in order to avoid collision # Raise an exception if the directory has already been created vertexdir = os.path.join(connectdir, "{0}_{1}".format(hemi, index))
if trf_file is None: trf_file = os.path.join(connectdir, "dmri_to_t1.trf") reg_file = os.path.join(connectdir, "nodif_to_t1.nii.gz") flirt(nodif_file, t1_file, omat=trf_file, out=reg_file, usesqform=False, cost="normmi", dof=6) """ Launch the tractography on the requested point of the cortical surface on the selected hemisphere """ # Load the white mesh in the diffusion space surface = TriSurface.load(whitefile) voxel_diff_to_t1 = flirt2aff(trf_file, nodif_file, t1_file) voxel_t1_to_diff = numpy.linalg.inv(voxel_diff_to_t1) white_diff_vertices = apply_affine_on_mesh(surface.vertices, voxel_t1_to_diff) # Select the vertices of interest if vertices_indices is None: vertices_indices = range(len(surface.vertices)) # Go through all the hemisphere vertices textures = {} for index in vertices_indices: # Select the seeding vertex point = white_diff_vertices[index] # Create a directory for each seeding vertex in order to avoid collision # Raise an exception if the directory has already been created vertexdir = os.path.join(connectdir, "{0}_{1}".format(hemi, index))