def freesurfer_surface_to_blender_surface(subject, hemi='both', overwrite=False): for hemi in utils.get_hemis(hemi): utils.make_dir(op.join(MMVT_DIR, subject, 'surf')) for surf_type in ['inflated', 'pial']: surf_name = op.join(SUBJECTS_DIR, subject, 'surf', '{}.{}'.format(hemi, surf_type)) surf_wavefront_name = '{}.asc'.format(surf_name) surf_new_name = '{}.srf'.format(surf_name) hemi_ply_fname = '{}.ply'.format(surf_name) mmvt_hemi_ply_fname = op.join(MMVT_DIR, subject, 'surf', '{}.{}.ply'.format(hemi, surf_type)) mmvt_hemi_npz_fname = op.join(MMVT_DIR, subject, 'surf', '{}.{}.npz'.format(hemi, surf_type)) if overwrite or not op.isfile(mmvt_hemi_ply_fname) and not op.isfile(mmvt_hemi_npz_fname): print('{} {}: convert srf to asc'.format(hemi, surf_type)) utils.run_script('mris_convert {} {}'.format(surf_name, surf_wavefront_name)) os.rename(surf_wavefront_name, surf_new_name) print('{} {}: convert asc to ply'.format(hemi, surf_type)) convert_hemis_srf_to_ply(subject, hemi, surf_type) if op.isfile(mmvt_hemi_ply_fname): os.remove(mmvt_hemi_ply_fname) shutil.copy(hemi_ply_fname, mmvt_hemi_ply_fname) ply_fname = op.join(MMVT_DIR, subject, 'surf', '{}.{}.ply'.format(hemi, surf_type)) if not op.isfile(mmvt_hemi_npz_fname): verts, faces = utils.read_ply_file(ply_fname) np.savez(mmvt_hemi_npz_fname, verts=verts, faces=faces) return utils.both_hemi_files_exist(op.join(MMVT_DIR, subject, 'surf', '{hemi}.pial.ply')) and \ utils.both_hemi_files_exist(op.join(MMVT_DIR, subject, 'surf', '{hemi}.pial.npz')) and \ utils.both_hemi_files_exist(op.join(MMVT_DIR, subject, 'surf', '{hemi}.inflated.ply')) and \ utils.both_hemi_files_exist(op.join(MMVT_DIR, subject, 'surf', '{hemi}.inflated.npz'))
def freesurfer_surface_to_blender_surface(subject, hemi='both', overwrite=False): for hemi in utils.get_hemis(hemi): surf_name = op.join(SUBJECTS_DIR, subject, 'surf', '{}.pial'.format(hemi)) surf_wavefront_name = '{}.asc'.format(surf_name) surf_new_name = '{}.srf'.format(surf_name) hemi_ply_fname = '{}.ply'.format(surf_name) mmvt_hemi_ply_fname = op.join(MMVT_DIR, subject, '{}.pial.ply'.format(hemi)) if overwrite or not op.isfile(hemi_ply_fname): print('{}: convert srf to asc'.format(hemi)) utils.run_script('mris_convert {} {}'.format(surf_name, surf_wavefront_name)) os.rename(surf_wavefront_name, surf_new_name) print('{}: convert asc to ply'.format(hemi)) convert_hemis_srf_to_ply(subject, hemi) for hemi in utils.get_hemis(hemi): if not op.isfile(mmvt_hemi_ply_fname): shutil.copy(hemi_ply_fname, mmvt_hemi_ply_fname) ply_fname = op.join(SUBJECTS_DIR, subject, 'surf', '{}.pial.ply'.format(hemi)) verts, faces = utils.read_ply_file(ply_fname) np.savez(op.join(SUBJECTS_DIR, subject, 'mmvt', '{}.pial'.format(hemi)), verts=verts, faces=faces) shutil.copyfile(op.join(SUBJECTS_DIR, subject, 'mmvt', '{}.pial.npz'.format(hemi)), op.join(MMVT_DIR, subject, '{}.pial.npz'.format(hemi))) return utils.both_hemi_files_exist(op.join(SUBJECTS_DIR, subject, 'surf', '{hemi}.pial.ply'))
def convert_perecelated_cortex(subject, aparc_name, overwrite_ply_files=False, hemi='both'): lookup = {} for hemi in utils.get_hemis(hemi): lookup[hemi] = create_labels_lookup(subject, hemi, aparc_name) if len(lookup[hemi]) == 0: continue srf_fol = op.join(SUBJECTS_DIR, subject,'{}.pial.{}'.format(aparc_name, hemi)) ply_fol = op.join(SUBJECTS_DIR, subject,'{}_{}_ply'.format(aparc_name, hemi)) blender_fol = op.join(MMVT_DIR, subject,'{}.pial.{}'.format(aparc_name, hemi)) utils.convert_srf_files_to_ply(srf_fol, overwrite_ply_files) rename_cortical(lookup, srf_fol, ply_fol) utils.rmtree(blender_fol) shutil.copytree(ply_fol, blender_fol) return lookup
def convert_perecelated_cortex(subject, aparc_name, surf_type='pial', overwrite_ply_files=False, hemi='both'): lookup = {} for hemi in utils.get_hemis(hemi): lookup[hemi] = create_labels_lookup(subject, hemi, aparc_name) if len(lookup[hemi]) == 0: continue srf_fol = op.join(SUBJECTS_DIR, subject,'{}.{}.{}'.format(aparc_name, surf_type, hemi)) ply_fol = op.join(SUBJECTS_DIR, subject,'{}_{}_{}_ply'.format(aparc_name, surf_type, hemi)) blender_fol = op.join(MMVT_DIR, subject,'{}.{}.{}'.format(aparc_name, surf_type, hemi)) utils.convert_srf_files_to_ply(srf_fol, overwrite_ply_files) rename_cortical(lookup, srf_fol, ply_fol) if surf_type == 'inflated': for ply_fname in glob.glob(op.join(ply_fol, '*.ply')): verts, faces = utils.read_ply_file(ply_fname) verts_offset = 5.5 if hemi == 'rh' else -5.5 verts[:, 0] = verts[:, 0] + verts_offset utils.write_ply_file(verts, faces, ply_fname) utils.rmtree(blender_fol) shutil.copytree(ply_fol, blender_fol) return lookup
def freesurfer_surface_to_blender_surface(subject, hemi='both', overwrite=False): # verts, faces = {}, {} for hemi in utils.get_hemis(hemi): utils.make_dir(op.join(MMVT_DIR, subject, 'surf')) for surf_type in ['inflated', 'pial']: surf_name = op.join(SUBJECTS_DIR, subject, 'surf', '{}.{}'.format(hemi, surf_type)) surf_wavefront_name = '{}.asc'.format(surf_name) surf_new_name = '{}.srf'.format(surf_name) hemi_ply_fname = '{}.ply'.format(surf_name) mmvt_hemi_ply_fname = op.join(MMVT_DIR, subject, 'surf', '{}.{}.ply'.format(hemi, surf_type)) mmvt_hemi_npz_fname = op.join(MMVT_DIR, subject, 'surf', '{}.{}.npz'.format(hemi, surf_type)) if overwrite or not op.isfile(mmvt_hemi_ply_fname) and not op.isfile(mmvt_hemi_npz_fname): print('{}: convert srf to asc'.format(hemi)) utils.run_script('mris_convert {} {}'.format(surf_name, surf_wavefront_name)) os.rename(surf_wavefront_name, surf_new_name) print('{}: convert asc to ply'.format(hemi)) convert_hemis_srf_to_ply(subject, hemi, surf_type) # if surf_type == 'inflated': # verts, faces = utils.read_ply_file(hemi_ply_fname) # verts_offset = 5.5 if hemi == 'rh' else -5.5 # verts[:, 0] = verts[:, 0] + verts_offset # utils.write_ply_file(verts, faces, '{}_offset.ply'.format(surf_name)) if op.isfile(mmvt_hemi_ply_fname): os.remove(mmvt_hemi_ply_fname) shutil.copy(hemi_ply_fname, mmvt_hemi_ply_fname) ply_fname = op.join(MMVT_DIR, subject, 'surf', '{}.{}.ply'.format(hemi, surf_type)) if not op.isfile(mmvt_hemi_npz_fname): verts, faces = utils.read_ply_file(ply_fname) np.savez(mmvt_hemi_npz_fname, verts=verts, faces=faces) # verts[hemi], faces[hemi] = utils.read_ply_file(mmvt_hemi_npz_fname) # if not op.isfile(op.join(MMVT_DIR, subject, 'cortex.pial.npz')): # faces['rh'] += np.max(faces['lh']) + 1 # verts_cortex = np.vstack((verts['lh'], verts['rh'])) # faces_cortex = np.vstack((faces['lh'], faces['rh'])) # utils.write_ply_file(verts_cortex, faces_cortex, op.join(MMVT_DIR, subject, 'cortex.pial.ply')) # np.savez(op.join(MMVT_DIR, subject, 'cortex.pial.npz'), verts=verts_cortex, faces=faces_cortex) return utils.both_hemi_files_exist(op.join(MMVT_DIR, subject, 'surf', '{hemi}.pial.ply')) and \ utils.both_hemi_files_exist(op.join(MMVT_DIR, subject, 'surf', '{hemi}.pial.npz')) and \ utils.both_hemi_files_exist(op.join(MMVT_DIR, subject, 'surf', '{hemi}.inflated.ply')) and \ utils.both_hemi_files_exist(op.join(MMVT_DIR, subject, 'surf', '{hemi}.inflated.npz'))
def convert_hemis_srf_to_ply(subject, hemi='both', surf_type='pial'): for hemi in utils.get_hemis(hemi): ply_file = utils.srf2ply(op.join(SUBJECTS_DIR, subject, 'surf', '{}.{}.srf'.format(hemi, surf_type)), op.join(SUBJECTS_DIR, subject, 'surf', '{}.{}.ply'.format(hemi, surf_type))) utils.make_dir(op.join(MMVT_DIR, subject))
def convert_hemis_srf_to_ply(subject, hemi='both'): for hemi in utils.get_hemis(hemi): ply_file = utils.srf2ply(op.join(SUBJECTS_DIR, subject, 'surf', '{}.pial.srf'.format(hemi)), op.join(SUBJECTS_DIR, subject, 'surf', '{}.pial.ply'.format(hemi))) utils.make_dir(op.join(MMVT_DIR, subject)) shutil.copyfile(ply_file, op.join(MMVT_DIR, subject, '{}.pial.ply'.format(hemi)))