def create_annotation_from_fsaverage(subject, aparc_name='aparc250', fsaverage='fsaverage', overwrite_annotation=False, overwrite_morphing=False, do_solve_labels_collisions=False, morph_labels_from_fsaverage=True, fs_labels_fol='', n_jobs=6): annotations_exist = np.all([op.isfile(op.join(SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format(hemi, aparc_name))) for hemi in HEMIS]) existing_freesurfer_annotations = ['aparc.DKTatlas40.annot', 'aparc.annot', 'aparc.a2009s.annot'] if '{}.annot'.format(aparc_name) in existing_freesurfer_annotations: morph_labels_from_fsaverage = False do_solve_labels_collisions = False if not annotations_exist: utils.make_dir(op.join(SUBJECTS_DIR, subject, 'label')) annotations_exist = fu.create_annotation_file(subject, aparc_name, subjects_dir=SUBJECTS_DIR, freesurfer_home=FREE_SURFER_HOME) if morph_labels_from_fsaverage: utils.morph_labels_from_fsaverage(subject, SUBJECTS_DIR, aparc_name, n_jobs=n_jobs, fsaverage=fsaverage, overwrite=overwrite_morphing, fs_labels_fol=fs_labels_fol) if do_solve_labels_collisions: solve_labels_collisions(subject, aparc_name, fsaverage, n_jobs) # Note that using the current mne version this code won't work, because of collissions between hemis # You need to change the mne.write_labels_to_annot code for that. if overwrite_annotation or not annotations_exist: try: utils.labels_to_annot(subject, SUBJECTS_DIR, aparc_name, overwrite=overwrite_annotation) except: print("Can't write labels to annotation! Trying to solve labels collision") solve_labels_collisions(subject, aparc_name, fsaverage, n_jobs) try: utils.labels_to_annot(subject, SUBJECTS_DIR, aparc_name, overwrite=overwrite_annotation) except: print("Can't write labels to annotation! Solving the labels collision didn't help...") print(traceback.format_exc()) return utils.both_hemi_files_exist(op.join( SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format('{hemi}', aparc_name)))
def create_annotation_from_fsaverage(subject, aparc_name='aparc250', fsaverage='fsaverage', remote_subject_dir='', overwrite_annotation=False, overwrite_morphing=False, do_solve_labels_collisions=False, morph_labels_from_fsaverage=True, fs_labels_fol='', n_jobs=6): annotations_exist = np.all([op.isfile(op.join(SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format(hemi, aparc_name))) for hemi in HEMIS]) if not annotations_exist: utils.make_dir(op.join(SUBJECTS_DIR, subject, 'label')) remote_annotations_exist = np.all([op.isfile(op.join(remote_subject_dir, 'label', '{}.{}.annot'.format( hemi, aparc_name))) for hemi in HEMIS]) if remote_annotations_exist: for hemi in HEMIS: shutil.copy(op.join(remote_subject_dir, 'label', '{}.{}.annot'.format(hemi, aparc_name)), op.join(SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format(hemi, aparc_name))) return True existing_freesurfer_annotations = ['aparc.DKTatlas40.annot', 'aparc.annot', 'aparc.a2009s.annot'] if '{}.annot'.format(aparc_name) in existing_freesurfer_annotations: morph_labels_from_fsaverage = False do_solve_labels_collisions = False if not annotations_exist: utils.make_dir(op.join(SUBJECTS_DIR, subject, 'label')) annotations_exist = fu.create_annotation_file(subject, aparc_name, subjects_dir=SUBJECTS_DIR, freesurfer_home=FREE_SURFER_HOME) if morph_labels_from_fsaverage: lu.morph_labels_from_fsaverage(subject, SUBJECTS_DIR, MMVT_DIR, aparc_name, n_jobs=n_jobs, fsaverage=fsaverage, overwrite=overwrite_morphing, fs_labels_fol=fs_labels_fol) if do_solve_labels_collisions: solve_labels_collisions(subject, aparc_name, fsaverage, n_jobs) # Note that using the current mne version this code won't work, because of collissions between hemis # You need to change the mne.write_labels_to_annot code for that. if overwrite_annotation or not annotations_exist: try: utils.labels_to_annot(subject, SUBJECTS_DIR, aparc_name, overwrite=overwrite_annotation) except: print("Can't write labels to annotation! Trying to solve labels collision") print(traceback.format_exc()) solve_labels_collisions(subject, aparc_name, fsaverage, n_jobs) try: utils.labels_to_annot(subject, SUBJECTS_DIR, aparc_name, overwrite=overwrite_annotation) except: print("Can't write labels to annotation! Solving the labels collision didn't help...") print(traceback.format_exc()) return utils.both_hemi_files_exist(op.join( SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format('{hemi}', aparc_name)))
def parcelate_cortex(subject, aparc_name, overwrite=False, overwrite_ply_files=False, minimum_labels_num=50): dont_do_anything = True ret = {'pial':True, 'inflated':True} utils.labels_to_annot(subject, SUBJECTS_DIR, aparc_name, overwrite=False) for surface_type in ['pial', 'inflated']: files_exist = True for hemi in HEMIS: blender_labels_fol = op.join(MMVT_DIR, subject,'{}.{}.{}'.format(aparc_name, surface_type, hemi)) files_exist = files_exist and op.isdir(blender_labels_fol) and \ len(glob.glob(op.join(blender_labels_fol, '*.ply'))) > minimum_labels_num # if surface_type == 'inflated': if overwrite or not files_exist: dont_do_anything = False matlab_command = op.join(BRAINDER_SCRIPTS_DIR, 'splitting_cortical.m') matlab_command = "'{}'".format(matlab_command) sio.savemat(op.join(BRAINDER_SCRIPTS_DIR, 'params.mat'), mdict={'subject': subject, 'aparc':aparc_name, 'subjects_dir': SUBJECTS_DIR, 'scripts_dir': BRAINDER_SCRIPTS_DIR, 'freesurfer_home': FREE_SURFER_HOME, 'surface_type': surface_type}) cmd = 'matlab -nodisplay -nosplash -nodesktop -r "run({}); exit;"'.format(matlab_command) script_ret = utils.run_script(cmd) if script_ret == '': return False # convert the obj files to ply lookup = convert_perecelated_cortex(subject, aparc_name, surface_type, overwrite_ply_files) matlab_labels_vertices = True if surface_type == 'pial': matlab_labels_vertices = save_matlab_labels_vertices(subject, aparc_name) labels_num = sum([len(lookup[hemi]) for hemi in HEMIS]) labels_files_num = sum([len(glob.glob(op.join(MMVT_DIR, subject,'{}.{}.{}'.format( aparc_name, surface_type, hemi), '*.ply'))) for hemi in HEMIS]) labels_dic_fname = op.join(MMVT_DIR, subject,'labels_dic_{}_{}.pkl'.format(aparc_name, hemi)) print('labels_files_num == labels_num: {}'.format(labels_files_num == labels_num)) print('isfile(labels_dic_fname): {}'.format(op.isfile(labels_dic_fname))) print('matlab_labels_vertices files: {}'.format(matlab_labels_vertices)) ret[surface_type] = labels_files_num == labels_num and op.isfile(labels_dic_fname) and matlab_labels_vertices if dont_do_anything: return True else: return ret['pial'] and ret['inflated']