def mri_convert(volume_fname, from_format='nii.gz', to_format='mgz'): try: print('convert {} to {}'.format(volume_fname.format(format=from_format), volume_fname.format(format=to_format))) utils.run_script('mri_convert {} {}'.format(volume_fname.format(format=from_format), volume_fname.format(format=to_format))) except: print('Error running mri_convert!')
def combine_movies(fol, movie_name, parts=(), movie_type='mp4'): # First convert the part to avi, because mp4 cannot be concat cmd = 'ffmpeg -i concat:"' if len(parts) == 0: parts = sorted( glob.glob(op.join(fol, '{}_*.{}'.format(movie_name, movie_type)))) else: parts = [op.join(fol, p) for p in parts] for part_fname in parts: part_name, _ = op.splitext(part_fname) cmd = '{}{}.avi|'.format(cmd, op.join(fol, part_name)) utils.remove_file('{}.avi'.format(part_name)) utils.run_script('ffmpeg -i {} -codec copy {}.avi'.format( part_fname, op.join(fol, part_name))) # cmd = '{}" -c copy -bsf:a aac_adtstoasc {}'.format(cmd[:-1], op.join(fol, '{}.{}'.format(movie_name, movie_type))) cmd = '{}" -c copy {}'.format( cmd[:-1], op.join(fol, '{}.{}'.format(movie_name, movie_type))) print(cmd) utils.remove_file('{}.{}'.format(op.join(fol, movie_name), movie_type)) utils.run_script(cmd) # clean up # todo: doesn't clean the part filess utils.remove_file('{}.avi'.format(op.join(fol, movie_name))) for part_fname in parts: part_name, _ = op.splitext(part_fname) utils.remove_file('{}.avi'.format(part_name))
def mri_convert(org_fname, new_fname, overwrite=False): if not op.isfile(new_fname) or overwrite: utils.run_script('mri_convert {} {}'.format(org_fname, new_fname)) if op.isfile(new_fname): return new_fname else: raise Exception("mri_convert: {} wan't created".format(new_fname))
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 subcortical_segmentation(subject, overwrite_subcortical_objs=False): # Must source freesurfer. You need to have write permissions on SUBJECTS_DIR script_fname = op.join(BRAINDER_SCRIPTS_DIR, 'aseg2srf') if not op.isfile(script_fname): raise Exception('The subcortical segmentation script is missing! {}'.format(script_fname)) if not utils.is_exe(script_fname): utils.set_exe_permissions(script_fname) # aseg2srf: For every subcortical region (8 10 11 12 13 16 17 18 26 47 49 50 51 52 53 54 58): # 1) mri_pretess: Changes region segmentation so that the neighbors of all voxels have a face in common # 2) mri_tessellate: Creates surface by tessellating # 3) mris_smooth: Smooth the new surface # 4) mris_convert: Convert the new surface into srf format function_output_fol = op.join(SUBJECTS_DIR, subject, 'mmvt', 'subcortical_objs') renamed_output_fol = op.join(SUBJECTS_DIR, subject, 'mmvt', 'subcortical') lookup = load_subcortical_lookup_table() obj_files = glob.glob(op.join(function_output_fol, '*.srf')) if len(obj_files) < len(lookup) or overwrite_subcortical_objs: utils.delete_folder_files(function_output_fol) # utils.delete_folder_files(aseg_to_srf_output_fol) utils.delete_folder_files(renamed_output_fol) print('Trying to write into {}'.format(function_output_fol)) utils.run_script(ASEG_TO_SRF.format(subject)) # os.rename(aseg_to_srf_output_fol, function_output_fol) ply_files = glob.glob(op.join(renamed_output_fol, '*.ply')) if len(ply_files) < len(lookup) or overwrite_subcortical_objs: convert_and_rename_subcortical_files(subject, function_output_fol, renamed_output_fol, lookup) flag_ok = len(glob.glob(op.join(renamed_output_fol, '*.ply'))) == len(lookup) and \ len(glob.glob(op.join(renamed_output_fol, '*.npz'))) == len(lookup) return flag_ok
def parcelate_cortex(subject, aparc_name, overwrite=False, overwrite_ply_files=False, minimum_labels_num=50): files_exist = True for hemi in HEMIS: blender_labels_fol = op.join(MMVT_DIR, subject,'{}.pial.{}'.format(aparc_name, 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 overwrite or not files_exist: 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}) cmd = 'matlab -nodisplay -nosplash -nodesktop -r "run({}); exit;"'.format(matlab_command) utils.run_script(cmd) # convert the obj files to ply lookup = convert_perecelated_cortex(subject, aparc_name, overwrite_ply_files) 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,'{}.pial.{}'.format( aparc_name, 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)) return labels_files_num == labels_num and op.isfile(labels_dic_fname) and matlab_labels_vertices
def mri_convert(volume_fname, from_format='nii.gz', to_format='mgz'): try: print('convert {} to {}'.format( volume_fname.format(format=from_format), volume_fname.format(format=to_format))) utils.run_script('mri_convert {} {}'.format( volume_fname.format(format=from_format), volume_fname.format(format=to_format))) except: print('Error running mri_convert!')
def mri_convert(org_fname, new_fname, overwrite=False, print_only=False, **kargs): cmd = 'mri_convert "{}" "{}"'.format(org_fname, new_fname) if print_only: print(cmd) return if not op.isfile(new_fname) or overwrite: utils.run_script(cmd) if op.isfile(new_fname): return new_fname else: raise Exception("mri_convert: {} wan't created".format(new_fname))
def read_surface(surf_name): print('Reading {}'.format(surf_name)) try: verts, faces = nib.freesurfer.read_geometry(surf_name) except: from src.utils import utils import shutil utils.print_last_error_line() print('Trying to recreate the surface') surf_wavefront_name = '{}.asc'.format(surf_name) print('mris_convert {} {}'.format(surf_name, surf_wavefront_name)) utils.run_script('mris_convert {} {}'.format(surf_name, surf_wavefront_name)) ply_fname = '{}.ply'.format(surf_name) verts, faces = utils.srf2ply(surf_wavefront_name, ply_fname) shutil.copyfile(surf_name, '{}.org'.format(surf_name)) nib.freesurfer.write_geometry(surf_name, verts, faces) return verts, faces
def combine_movies(fol, movie_name, movie_type='mp4'): # First convert the part to avi, because mp4 cannot be concat cmd = 'ffmpeg -i concat:"' parts = sorted(glob.glob(op.join(fol, '{}_*.{}'.format(movie_name, movie_type)))) for part_fname in parts: part_name, _ = op.splitext(part_fname) cmd = '{}{}.avi|'.format(cmd, op.join(fol, part_name)) utils.remove_file('{}.avi'.format(part_name)) utils.run_script('ffmpeg -i {} -codec copy {}.avi'.format(part_fname, op.join(fol, part_name))) # cmd = '{}" -c copy -bsf:a aac_adtstoasc {}'.format(cmd[:-1], op.join(fol, '{}.{}'.format(movie_name, movie_type))) cmd = '{}" -c copy {}'.format(cmd[:-1], op.join(fol, '{}.{}'.format(movie_name, movie_type))) print(cmd) utils.remove_file('{}.{}'.format(op.join(fol, movie_name), movie_type)) utils.run_script(cmd) # clean up # todo: doesn't clean the part filess utils.remove_file('{}.avi'.format(op.join(fol, movie_name))) for part_fname in parts: part_name, _ = op.splitext(part_fname) utils.remove_file('{}.avi'.format(part_name))
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 call_script(script_fname, args, log_name='', blend_fname=None, call_args=None): if args.blender_fol == '': args.blender_fol = get_blender_dir() if not op.isdir(args.blender_fol): print('No Blender folder!') return logs_fol = utils.make_dir( op.join(utils.get_parent_fol(__file__, 4), 'logs')) if log_name == '': log_name = utils.namebase(script_fname) if len(args.subjects) == 0: args.subjects = [args.subject] for subject in args.subjects: args.subject = subject args.subjects = '' print('*********** {} ***********'.format(subject)) if blend_fname is None: blend_fname = get_subject_fname(args) else: blend_fname = op.join(get_mmvt_dir(), blend_fname) if call_args is None: call_args = create_call_args(args) log_fname = op.join(logs_fol, '{}.log'.format(log_name)) cmd = '{blender_exe} {blend_fname} --background --python "{script_fname}" {call_args}'.format( # > {log_fname} blender_exe=op.join(args.blender_fol, 'blender'), blend_fname=blend_fname, script_fname=script_fname, call_args=call_args, log_fname=log_fname) mmvt_addon_fol = utils.get_parent_fol(__file__, 2) os.chdir(mmvt_addon_fol) print(cmd) utils.run_script(cmd) print('Finish! For more details look in {}'.format(log_fname))
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 parcelate_cortex(subject, aparc_name, overwrite=False, overwrite_ply_files=False, minimum_labels_num=50): dont_do_anything = True ret = {'pial':True, 'inflated':True} 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) utils.run_script(cmd) # 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']
def fs_stat(subject, atlas, surface='pial', excluded=('corpuscallosum', 'unknown'), overwrite=False): labels_area = [] for hemi in utils.HEMIS: output_fname = op.join(SUBJECTS_DIR, subject, 'label', '{}.{}.stat'.format(hemi, atlas)) if not op.isfile(output_fname) or overwrite: utils.run_script( 'mris_anatomical_stats -a {} {} {} {} > {}'.format( atlas, subject, hemi, surface, output_fname)) hemi_labels_names, hemi_labels_area = parse_fs_stat( output_fname, excluded) labels_area.extend(hemi_labels_area) labels_area = np.array(labels_area) print('{}: areas: min {}, max {}, mean {}, std {}'.format( subject, np.min(labels_area), np.max(labels_area), np.mean(labels_area), np.std(labels_area))) fol = utils.make_dir(op.join(SUBJECTS_DIR, subject, 'label', atlas)) output_fname = op.join(fol, 'labels_fs_stat.npz') np.savez(output_fname, labels_area=labels_area)
def get_tr(fmri_fname, **kargs): try: img = nib.load(fmri_fname) hdr = img.get_header() tr = float(hdr._header_data.tolist()[-1][0]) / 1000.0 # To sec return tr except: output = utils.run_script('mri_info --tr {}'.format(fmri_fname)) try: output = output.decode('ascii') except: pass output = output.replace('\n', '') tr = float(output) / 1000 return tr
def subcortical_segmentation(subject, overwrite_subcortical_objs=False): # Must source freesurfer. You need to have write permissions on SUBJECTS_DIR if not op.isfile(op.join(SUBJECTS_DIR, subject, 'mri', 'norm.mgz')): return False script_fname = op.join(BRAINDER_SCRIPTS_DIR, 'aseg2srf') if not op.isfile(script_fname): raise Exception('The subcortical segmentation script is missing! {}'.format(script_fname)) if not utils.is_exe(script_fname): utils.set_exe_permissions(script_fname) # aseg2srf: For every subcortical region (8 10 11 12 13 16 17 18 26 47 49 50 51 52 53 54 58): # 1) mri_pretess: Changes region segmentation so that the neighbors of all voxels have a face in common # 2) mri_tessellate: Creates surface by tessellating # 3) mris_smooth: Smooth the new surface # 4) mris_convert: Convert the new surface into srf format function_output_fol = op.join(SUBJECTS_DIR, subject, 'mmvt', 'subcortical_objs') renamed_output_fol = op.join(SUBJECTS_DIR, subject, 'mmvt', 'subcortical') lookup = load_subcortical_lookup_table() obj_files = glob.glob(op.join(function_output_fol, '*.srf')) if len(obj_files) < len(lookup) or overwrite_subcortical_objs: utils.delete_folder_files(function_output_fol) # utils.delete_folder_files(aseg_to_srf_output_fol) utils.delete_folder_files(renamed_output_fol) print('Trying to write into {}'.format(function_output_fol)) utils.run_script(ASEG_TO_SRF.format(subject)) # os.rename(aseg_to_srf_output_fol, function_output_fol) ply_files = glob.glob(op.join(renamed_output_fol, '*.ply')) if len(ply_files) < len(lookup) or overwrite_subcortical_objs: convert_and_rename_subcortical_files(subject, function_output_fol, renamed_output_fol, lookup) blender_dir = op.join(MMVT_DIR, subject, 'subcortical') if not op.isdir(blender_dir) or len(glob.glob(op.join(blender_dir, '*.ply'))) < len(ply_files): copy_subcorticals_to_blender(renamed_output_fol, subject) flag_ok = len(glob.glob(op.join(blender_dir, '*.ply'))) == len(lookup) and \ len(glob.glob(op.join(blender_dir, '*.npz'))) == len(lookup) return flag_ok
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']
def get_vox2ras_tkr(fname): output = utils.run_script('mri_info --vox2ras-tkr {}'.format(fname)) return read_transform_matrix_from_output(output)
def which_matlab(): ret = utils.run_script('which matlab') ret = ret.replace('\n', '') return ret
def get_vox2ras(fname): output = utils.run_script('mri_info --vox2ras {}'.format(fname)) return read_transform_matrix_from_output(output)
def run_matlab(): matlab_cmd = get_matlab_cmd() print('matlab: {}'.format(matlab_cmd)) utils.run_script(matlab_cmd)
def arrange_data(subjects): files_list = [] for sub in subjects: utils.make_dir(op.join(fmri_root_data, sub)) sub_root_anat_fol = utils.make_dir(op.join(fmri_root_data, sub, 'anat')) sub_root_bold_fol = utils.make_dir(op.join(fmri_root_data, sub, 'bold')) anat_fols = [ f for f in glob.glob(op.join(subject_fmri_fold, sub, 'anat', '*')) if op.isdir(f) ] if len(anat_fols) == 0: print('No anat folder for {}!'.format(sub)) continue elif len(anat_fols) > 1: print('More than one anat folder for {}!'.format(sub)) continue anat_fol = anat_fols[0] anat_number = int(utils.namebase(anat_fol)) anat_files = glob.glob(op.join(anat_fol, '*nii.gz')) if len(anat_files) == 1: sub_anat_fol = utils.make_dir( op.join(sub_root_anat_fol, utils.namebase(anat_fol))) utils.make_link( anat_files[0], op.join(sub_anat_fol, '{}.nii.gz'.format(utils.namebase(anat_files[0])))) elif len(anat_files) == 0: print('No nii.gz files were found in {}!'.format(anat_fol)) continue else: print('Too many nii.gz files were found in {}!'.format(anat_fol)) continue bold_fols = [ f for f in glob.glob(op.join(subject_fmri_fold, sub, 'bold', '*')) if op.isdir(f) ] bolds_numbers = ','.join( [str(int(utils.namebase(bold_fol))) for bold_fol in bold_fols]) for bold_fol in bold_fols: fmri_files = glob.glob( op.join(bold_fol, '*.nii') ) # '{}_bld{}_rest.nii'.format(sub, utils.namebase(bold_fol)))) if len(fmri_files) == 1: sub_fmri_fol = utils.make_dir( op.join(sub_root_bold_fol, utils.namebase(bold_fol))) # utils.delete_folder_files(sub_fmri_fol) # target_file = op.join(sub_fmri_fol, '{}_bld{}_rest_reorient.nii.gz'.format(sub, utils.namebase(bold_fol))) target_file = op.join( sub_fmri_fol, '{}_bld{}_rest.nii'.format(sub, utils.namebase(bold_fol))) utils.make_link(fmri_files[0], target_file) new_target_file = op.join( sub_fmri_fol, '{}_bld{}_rest_reorient.nii.gz'.format( sub, utils.namebase(bold_fol))) if not op.isfile(new_target_file): output = utils.run_script('mri_convert {} {}'.format( target_file, new_target_file)) # utils.remove_link(target_file) # if not op.isfile(target_file): # utils.copy_file(fmri_files[0], target_file) elif len(fmri_files) == 0: print( 'No *_rest_reorient_skip_faln_mc.nii.gz files were found in {}!' .format(bold_fol)) continue else: print( 'Too many *_rest_reorient_skip_faln_mc.nii.gz files were found in {}!' .format(bold_fol)) continue tr = int(fu.get_tr(target_file) * 1000) / 1000.0 files_list.append('{} {} {} {}'.format( sub, bolds_numbers, str(anat_number).zfill(3), tr)) #int(tr))) # int(trs[sub]))) utils.make_dir(op.join(fmri_root_data, sub, 'scripts')) params_fname = op.join(fmri_root_data, sub, 'scripts', '{}.params'.format(sub)) with open(params_fname, 'w') as f: f.write( params_file_text.format(subject=sub, bolds=bolds_numbers.replace(',', ' '), tr=tr)) return files_list