def calc_freesurfer_surf(subject, atlas): # Clean args = fmri.read_cmd_args( dict(subject=subject, atlas=atlas, function='clean_4d_data', fmri_file_template='rest.nii*', fsd='rest_linda', overwrite_4d_preproc=False)) pu.run_on_subjects(args, fmri.main) # save the surf files args = fmri.read_cmd_args( dict(subject=subject, atlas=atlas, function='load_surf_files', overwrite_surf_data=True, fmri_file_template=fs_surf_template.format(subject=subject, hemi='{hemi}'))) pu.run_on_subjects(args, fmri.main) # Renaming the files root_fol = op.join(fmri.MMVT_DIR, subject, 'fmri') for hemi in utils.HEMIS: os.rename( op.join(root_fol, 'fmri_rest_linda.sm6.{}.{}.npy'.format(subject, hemi)), op.join(root_fol, 'fmri_freesurfer_{}.npy'.format(hemi))) os.rename( op.join(root_fol, 'rest_linda.sm6.{}_minmax.pkl'.format(subject)), op.join(root_fol, 'freesurfer_minmax.pkl'))
def calc_hesheng_surf(subject, atlas): subject_fol = op.join(fmri.MMVT_DIR, subject, 'fmri') if not (utils.both_hemi_files_exist( op.join(subject_fol, 'fmri_hesheng_{hemi}.npy')) and op.isfile(op.join(subject_fol, 'hesheng_minmax.pkl'))): # Copy and rename Hesheng's files hesheng_fnames = glob.glob( op.join(hesheng_surf_fol.format(subject=subject), hesheng_template.format(subject=subject))) for fname in hesheng_fnames: hemi = lu.get_label_hemi_invariant_name(utils.namebase(fname)) target_file = op.join(fmri.FMRI_DIR, subject, 'hesheng_{}.nii.gz'.format(hemi)) mgz_target_file = utils.change_fname_extension(target_file, 'mgz') if not op.isfile(mgz_target_file): shutil.copy(fname, target_file) fu.nii_gz_to_mgz(target_file) os.remove(target_file) # Load Hesheng's files args = fmri.read_cmd_args( dict(subject=subject, atlas=atlas, function='load_surf_files', overwrite_surf_data=True, fmri_file_template='hesheng_{hemi}.mgz')) pu.run_on_subjects(args, fmri.main)
def calc_diff(subject, fmri_file_template='*linda_{hemi}*,*hesheng_{hemi}'): # Calc diff args = fmri.read_cmd_args( dict(subject=subject, function='calc_files_diff', fmri_file_template=fmri_file_template)) pu.run_on_subjects(args, fmri.main)
def project_and_calc_clusters(args): if not op.isdir(args.root_fol): print('You should first set args.root_fol!') return False img_files = [ f for f in glob.glob(op.join(args.root_fol, '*.img')) if op.isfile(f) ] for img_fname in img_files: mgz_fname = fu.mri_convert_to(img_fname, 'mgz', overwrite=False) if ' ' in utils.namebase(mgz_fname): mgz_new_fname = op.join( utils.get_parent_fol(mgz_fname), utils.namebase_with_ext(mgz_fname).replace(' ', '_').replace( ',', '').lower()) os.rename(mgz_fname, mgz_new_fname) nii_files = [ f for f in glob.glob(op.join(args.root_fol, '*')) if op.isfile(f) and utils.file_type(f) in ('nii', 'nii.gz', 'mgz') ] for fname in nii_files: fmri_args = fmri.read_cmd_args( dict(subject=args.subject, function='project_volume_to_surface,find_clusters', fmri_file_template=fname, threshold=args.cluster_threshold)) pu.run_on_subjects(fmri_args, fmri.main)
def calc_linda_surf(subject, atlas): # Check for Linda's output fname if not utils.both_hemi_files_exist(op.join(fmri.MMVT_DIR, subject, 'fmri', 'fmri_linda_{}.npy'.format('{hemi}'))) \ and not op.isfile(op.join(fmri.MMVT_DIR, subject, 'fmri', 'linda_minmax.pkl')): # Find Linda's files linda_volume_fnames = glob.glob(op.join( linda_fol.format(subject=subject), linda_vol_template.format(subject=subject))) linda_volume_folder = utils.get_parent_fol(linda_volume_fnames[0]) # project linda files on the surface args = fmri.read_cmd_args(dict( subject=subject, function='project_volume_to_surface', remote_fmri_dir=linda_volume_folder, fmri_file_template=linda_vol_template.format(subject=subject), overwrite_surf_data=True)) pu.run_on_subjects(args, fmri.main) # rename Linda's files linda_fnames = glob.glob(op.join(fmri.MMVT_DIR, subject, 'fmri', 'fmri_{}'.format( linda_template_npy.format(subject=subject)))) for fname in linda_fnames: hemi = lu.get_label_hemi(utils.namebase(fname)) target_file = op.join(fmri.MMVT_DIR, subject, 'fmri', 'fmri_linda_{}.npy'.format(hemi)) if not op.isfile(target_file): os.rename(fname, target_file) # rename minmax file linda_minmax_name = '{}.pkl'.format(utils.namebase(glob.glob(op.join( fmri.MMVT_DIR, subject, 'fmri', '{}_minmax.pkl'.format( utils.namebase(linda_vol_template.format(subject=subject)))))[0])) os.rename(op.join(fmri.MMVT_DIR, subject, 'fmri', linda_minmax_name), op.join(fmri.MMVT_DIR, subject, 'fmri', 'linda_minmax.pkl')) # delete mgz files mgz_files = glob.glob(op.join(fmri.MMVT_DIR, subject, 'fmri', 'fmri_{}_?h.mgz'.format( utils.namebase(linda_vol_template.format(subject=subject))))) for mgz_file in mgz_files: os.remove(mgz_file)
def pre_blender_call(args): from src.mmvt_addon import load_results_panel from src.preproc import fMRI as fmri from src.utils import preproc_utils as pu from src.utils import utils user_fol = op.join(pu.MMVT_DIR, args.subject) nii_fname = args.nii if not op.isfile(args.nii): args.nii = op.join(user_fol, 'fmri', nii_fname) if not op.isfile(args.nii): fmri_fol = op.join(utils.get_link_dir(utils.get_links_dir(), 'fMRI'), args.subject) args.nii = op.join(fmri_fol, nii_fname) if not op.isfile(args.nii): raise Exception("Can't find the nii file!") fmri_file_template, _, _ = load_results_panel.load_surf_files( args.nii, run_fmri_preproc=False, user_fol=user_fol) preproc_args = fmri.read_cmd_args( dict(subject=args.subject, atlas=args.atlas, function='load_surf_files', fmri_file_template=fmri_file_template, ignore_missing=True)) ret = pu.run_on_subjects(preproc_args, fmri.main) if ret: load_results_panel.clean_nii_temp_files(fmri_file_template, user_fol) args.fmri_file_template = fmri_file_template else: raise Exception("Couldn't load the surface files!") return args
def load_rest_to_colin(): args = fmri.read_cmd_args(['-s', subject]) args.task = 'REST' args.function = 'project_volume_to_surface,find_clusters' args.contrast = 'rest' args.volume_name = 'spmT_0001' fmri.main(subject, mri_subject, args)
def fsfast(): args = fmri.read_cmd_args(['-s', subject]) args.task = 'MSIT' args.function = 'fmri_pipeline' args.contrast_name = 'interference' args.atlas = 'laus250' fmri.main(subject, mri_subject, args)
def compare_connectivity(subject, atlas, n_jobs=6): for name, fol, template in zip(['hesheng', 'linda', 'freesurfer'], [hesheng_surf_fol, linda_surf_fol, fs_surf_fol], [hesheng_template, linda_hemi_template, fs_surf_template]): output_fname_template = op.join( fmri.MMVT_DIR, subject, 'fmri', '{}_labels_data_laus125_mean_{}.npz'.format(name, '{hemi}')) if not utils.both_hemi_files_exist(output_fname_template): args = fmri.read_cmd_args(dict( subject=subject, atlas=atlas, function='analyze_4d_data', fmri_file_template=template, remote_fmri_dir=fol, labels_extract_mode='mean', overwrite_labels_data=False)) pu.run_on_subjects(args, fmri.main) for hemi in utils.HEMIS: os.rename(op.join(fmri.MMVT_DIR, subject, 'fmri', 'labels_data_laus125_mean_{}.npz'.format(hemi)), output_fname_template.format(hemi=hemi)) args = con.read_cmd_args(dict( subject=subject, atlas='laus125', function='calc_lables_connectivity', connectivity_modality='fmri', connectivity_method='corr,cv', labels_extract_mode='mean', windows_length=34, windows_shift=4, save_mmvt_connectivity=False, calc_subs_connectivity=False, labels_name=name, recalc_connectivity=True, n_jobs=n_jobs)) pu.run_on_subjects(args, con.main) conn_fol = op.join(con.MMVT_DIR, subject, 'connectivity') coloring_fol = op.join(con.MMVT_DIR, subject, 'coloring') os.rename(op.join(conn_fol, 'fmri_corr.npy'), op.join(conn_fol, 'fmri_corr_{}.npy'.format(name))) os.rename(op.join(conn_fol, 'fmri_corr_cv_mean.npz'), op.join(conn_fol, 'mri_corr_cv_mean_{}.npz'.format(name))) os.rename(op.join(conn_fol, 'fmri_corr_cv_mean_mean.npz'), op.join(conn_fol, 'fmri_corr_cv_mean_mean_{}.npz'.format(name))) os.rename(op.join(coloring_fol, 'fmri_corr_cv_mean.csv'), op.join(coloring_fol, 'fmri_corr_cv_mean_{}.csv'.format(name)))
def pet(): args = fmri.read_cmd_args(['-s', subject]) args.threshold = 0 args.is_pet = True args.symetric_colors = False args.atlas = 'laus250' fmri.main(subject, mri_subject, args) '-s s02 --threshold 0 --is_pet 1 --symetric_colors 0 --overwrite_surf_data 1 --remote_subject_dir /local_mount/space/thibault/1/users/npeled/artur/recon_tese/{subject}'
def save_dynamic_activity_map(args): args = fmri.read_cmd_args( dict(subject=args.subject, atlas=args.atlas, function='save_dynamic_activity_map', fmri_file_template='fmcpr.up.sm6.{subject}.{hemi}.*', overwrite_activity_data=True)) pu.run_on_subjects(args, fmri.main)
def calc_subcorticals_activity(args): args = fmri.read_cmd_args( dict( subject=args.subject, function='calc_subcorticals_activity', # fmri_file_template='rest*', fmri_file_template='fmcpr.sm6.mni305.2mm.*', labels_extract_mode='mean', #,pca,pca_2,pca_4,pca_8', overwrite_subs_data=True)) pu.run_on_subjects(args, fmri.main)
def fmri_msit_pipeline(args): '-s pp009 -a laus250 -f fmri_pipeline -t MSIT --contrast_template "*Interference*"' for subject in args.subject: args = fmri.read_cmd_args( dict(subject=subject, atlas=args.atlas, function='fmri_pipeline', task='MSIT', contrast_template='*Interference*')) pu.run_on_subjects(args, fmri.main)
def project_volume_to_surface(args): args = fmri.read_cmd_args(dict( subject=args.subject, function='project_volume_to_surface', fmri_file_template='f*.gz', sftp_username=args.sftp_username, sftp_domain=args.sftp_domain, sftp=True, remote_subject_dir=args.remote_subject_dir, )) pu.run_on_subjects(args, fmri.main)
def get_subject_files_using_sftp(args): for subject in args.subject: args = fmri.read_cmd_args( dict(subject=subject, atlas=args.atlas, sftp_username=args.sftp_username, sftp_domain=args.sftp_domain, sftp=True, remote_subject_dir=args.remote_subject_dir, function='prepare_subject_folder')) pu.run_on_subjects(args, fmri.main)
def analyze_rest_fmri(args): for subject in args.mri_subject: remote_rest_fol = get_fMRI_rest_fol(subject, args.remote_fmri_dir) local_rest_fname = convert_rest_dicoms_to_mgz(subject, remote_rest_fol) if not op.isfile(local_rest_fname): print('{}: Can\'t find {}!'.format(subject, local_rest_fname)) continue args = fmri.read_cmd_args( dict( subject=subject, atlas=args.atlas, remote_subject_dir=args.remote_subject_dir, function='clean_4d_data', fmri_file_template=local_rest_fname, )) fmri.call_main(args) args = fmri.read_cmd_args( dict(subject=args.subject, atlas=args.atlas, function='analyze_4d_data', fmri_file_template='rest.sm6.{subject}.{hemi}.mgz', labels_extract_mode='mean', overwrite_labels_data=False)) fmri.call_main(args) args = connectivity.read_cmd_args( dict(subject=args.subject, atlas=args.atlas, function='calc_lables_connectivity', connectivity_modality='fmri', connectivity_method='corr', labels_extract_mode='mean', identifier='', save_mmvt_connectivity=True, calc_subs_connectivity=False, recalc_connectivity=True, n_jobs=args.n_jobs)) connectivity.call_main(args)
def load_labels_ts(args): args = fmri.read_cmd_args( dict(subject=args.subject, atlas=args.atlas, function='load_labels_ts', labels_order_fname=op.join(fmri.MMVT_DIR, 'labels_order', 'linda_laus125_order.txt'), labels_extract_mode='mean', excluded_labels='corpuscallosum,unknown', labels_indices_to_remove_from_data='0,4,113,117', st_template='{subject}_{atlas}_mri.txt', backup_existing_files=False, pick_the_first_one=True)) pu.run_on_subjects(args, fmri.main)
def clean_4d_data(args): ''' python -m src.preproc.fMRI -s nmr00474,nmr00502,nmr00515,nmr00603,nmr00609,nmr00626,nmr00629,nmr00650,nmr00657,nmr00669,nmr00674,nmr00681,nmr00683,nmr00692,nmr00698,nmr00710 -a laus125 -f clean_resting_state_data --template_brain fsaverage5 --fmri_file_template "f.nii*" --remote_subject_dir "/space/franklin/1/users/sx424/mem_flex/subjects/{subject}"' ''' args = fmri.read_cmd_args( dict(subject=args.subject, atlas=args.atlas, function='clean_4d_data', fmri_file_template='rest.nii*', fsd='rest_linda' # template_brain='fsaverage5', )) pu.run_on_subjects(args, fmri.main)
def project_and_calc_clusters(args): if not op.isdir(args.root_fol): print('You should first set args.root_fol!') return False nii_files = [ f for f in glob.glob(op.join(args.root_fol, '*')) if op.isfile(f) and utils.file_type(f) in ('nii', 'nii.gz', 'mgz') ] for fname in nii_files: args = fmri.read_cmd_args( dict(subject=args.subject, function='project_volume_to_surface,find_clusters', fmri_file_template=fname)) pu.run_on_subjects(args, fmri.main)
def analyze_4d_data(args): # '-s subject-name -a atlas-name -f analyze_4d_data --fmri_file_template {subject}*{morph_to_subject}.{hemi}.{format} --morph_labels_to_subject fsaverage' # '-f analyze_4d_data -a laus125 -s "file:/homes/5/npeled/space1/Documents/memory_task/subjects.txt"' args = fmri.read_cmd_args(dict( subject=args.subject, atlas=args.atlas, function='analyze_4d_data', # fmri_file_template='fmcpr.up.sm6.{subject}.{hemi}.nii.gz', fmri_file_template='{subject}_bld???_rest_reorient_skip_faln_mc_g1000000000_bpss_resid_{hemi}.mgz', # template_brain='fsaverage5', # template_brain='fsaverage6', # labels_extract_mode='mean,pca,pca_2,pca_4,pca_8', labels_extract_mode='mean', overwrite_labels_data=True )) pu.run_on_subjects(args, fmri.main)
def calc_labels_mean_freesurfer(args): ''' python -m src.preproc.fMRI -a laus125 -f calc_labels_mean_freesurfer --fmri_file_template "{hemi}.{subject}_bld014_rest_reorient_skip_faln_mc_g1000000000_bpss_resid_fsaverage6_sm6_fsaverage5*.mgz" --excluded_labels corpuscallosum,unknown --overwrite_labels_data 1 --remote_fmri_dir "/autofs/cluster/scratch/tuesday/noam/DataProcessed_memory/{subject}/surf" -s 'nmr00506','nmr00599','nmr00515','nmr00692','nmr00657','nmr00609','nmr00468','nmr00629','nmr00681','nmr00643','nmr00448','nmr00650','nmr00674','nmr00669','nmr00603','nmr00710','nmr00683','nmr00640','nmr00634','nmr00502','nmr00698' ''' args = fmri.read_cmd_args(dict( subject=args.subject, atlas=args.atlas, function='calc_labels_mean_freesurfer', fmri_file_template='{hemi}.{subject}_bld???_rest_reorient_skip_faln_mc_g1000000000_bpss_resid_fsaverage6_sm6_fsaverage5*.mgz', excluded_labels='corpuscallosum,unknown', remote_fmri_dir='/autofs/cluster/neuromind/douw/scans/adults/{subject}/surf', overwrite_labels_data=True, sftp_username=args.sftp_username, sftp_domain=args.sftp_domain, sftp=True, remote_subject_dir=args.remote_subject_dir, )) pu.run_on_subjects(args, fmri.main)
def language(args): # -f language -s nmr01361 --clinical_dir clin_4090354 # -s nmr01353 -f clean_4d_data --fsd sycabs --remote_fmri_dir "/space/megraid/clinical/MEG-MRI/seder/freesurfer" --nconditions 4 if args.clinical_dir == '': print('You should set the clinical_dir first. Example: clin_4090354') return clinical_root_dir = op.join(args.remote_fmri_dir, args.clinical_dir) if not op.isdir(clinical_root_dir): print('{} does not exist!'.format(clinical_root_dir)) task = 'sycabs' fwhm = 6 subject = args.subject[0] remote_mri_dir = args.remote_clinical_subjects_dir subject_mri_dir = op.join(remote_mri_dir, subject) mri_subject_task_dir = utils.make_dir(op.join(subject_mri_dir, task)) clinical_dirs = glob.glob(op.join(clinical_root_dir, '*')) clinical_dirs = [ d for d in clinical_dirs if utils.namebase(d) != 'mne_dcm' ] remote_fmri_dir = utils.select_one_file(clinical_dirs) fmri_fols = sorted(glob.glob(op.join(remote_fmri_dir, '*_SyCAbs'))) par_fol = utils.make_dir(op.join(remote_mri_dir, subject, 'par')) par_files = glob.glob(op.join(par_fol, '*.par')) sessions = sorted( [utils.find_num_in_str(utils.namebase(d))[0] for d in fmri_fols]) # Warning: You first need to put the original ones in the following folder: if len(par_files) == 0: print('\n *** Please put the original par files in {} and rerun ***'. format(op.join(remote_mri_dir, subject, 'par'))) return par_files.sort(key=lambda x: int(utils.namebase(x).split('_')[-1])) ret = input(''' Patient: {} MRI folder: {} fMRI root folder: {} fMRI sessions: {} Session and pars: {} Do you want to continue (y/n)? '''.format( subject, subject_mri_dir, remote_fmri_dir, [utils.namebase(d) for d in fmri_fols], list(zip([utils.namebase(f) for f in par_files], sessions)))) if not au.is_true(ret): return # You need first to run src.preproc.anatomy if not op.isfile(anat.get_blend_fname(subject, args.atlas)): args = anat.read_cmd_args( dict( subject=subject, remote_subject_dir=subject_mri_dir, ignore_missing=True, )) pu.run_on_subjects(args, anat.main) # convert the fMRI dicom files to nii for fmri_fol in fmri_fols: ses_num = utils.find_num_in_str(utils.namebase(fmri_fol))[0] ses_files = glob.glob(op.join(fmri_fol, '**', '*.*'), recursive=True) output_fname = op.join( utils.make_dir(op.join(mri_subject_task_dir, ses_num)), 'f.nii.gz') if not op.isfile(output_fname): fu.mri_convert(ses_files[0], output_fname) # Convert and arrange the par file from src.misc.fmri_scripts import convert_par for par_file, session in zip(par_files, sessions): fs_par_fname = op.join(mri_subject_task_dir, session, '{}.par'.format(task)) # if not op.isfile(fs_par_fname): warnings = convert_par.sycabs(par_file, fs_par_fname) if warnings != '': print( '\n *** Please fix the problems with the par convertion ({}) and rerun ***\n' .format(par_file)) return for hemi in utils.HEMIS: utils.delete_folder_files( op.join(remote_mri_dir, '{}_sm{}_{}'.format(task, fwhm, hemi))) # Run the FreeSurfer analysis args = fmri.read_cmd_args( dict(subject=subject, atlas=args.atlas, function='clean_4d_data', fsd=task, fwhm=fwhm, remote_fmri_dir=remote_mri_dir, nconditions=4, ignore_missing=True, print_only=False, overwrite_4d_preproc=False)) pu.run_on_subjects(args, fmri.main) # Load the fMRI results args = fmri.read_cmd_args( dict( subject=subject, atlas=args.atlas, function='load_surf_files', fmri_file_template=op.join(MMVT_DIR, subject, 'fmri', 'words_v_symbols_{hemi}.mgz'), )) pu.run_on_subjects(args, fmri.main)
def analyze_rest_fmri(gargs): good_subjects = [] for subject in gargs.mri_subject: # remote_rest_fol = get_fMRI_rest_fol(subject, gargs.remote_fmri_dir) # print('{}: {}'.format(subject, remote_rest_fol)) # if remote_rest_fol == '': # continue # local_rest_fname = convert_rest_dicoms_to_mgz(subject, remote_rest_fol, overwrite=True) # if local_rest_fname == '': # continue # if not op.isfile(local_rest_fname): # print('{}: Can\'t find {}!'.format(subject, local_rest_fname)) # continue # args = fmri.read_cmd_args(dict( # subject=subject, # atlas=gargs.atlas, # remote_subject_dir=gargs.remote_subject_dir, # function='clean_4d_data', # fmri_file_template=local_rest_fname, # overwrite_4d_preproc=True # )) # flags = fmri.call_main(args) # if subject not in flags or not flags[subject]['clean_4d_data']: # continue output_fname = op.join(MMVT_DIR, subject, 'connectivity', 'fmri_corr.npz') if op.isfile(output_fname): print('{} already exist!'.format(output_fname)) good_subjects.append(subject) continue remote_fname = op.join( gargs.remote_fmri_rest_dir, subject, 'rest_001', '001', 'fmcpr.siemens.sm6.{}.{}.nii.gz'.format(subject, '{hemi}')) if not utils.both_hemi_files_exist(remote_fname): print('Couldn\t find fMRI rest data for {} ({})'.format( subject, remote_fname)) continue local_fmri_fol = utils.make_dir(op.join(FMRI_DIR, subject)) local_fmri_template = op.join(local_fmri_fol, utils.namebase_with_ext(remote_fname)) if utils.both_hemi_files_exist(local_fmri_template): print('{} al') for hemi in utils.HEMIS: local_fmri_fname = op.join(local_fmri_fol, local_fmri_template.format(hemi=hemi)) if op.isfile(local_fmri_fname): os.remove(local_fmri_fname) utils.make_link(remote_fname.format(hemi=hemi), local_fmri_fname) args = fmri.read_cmd_args( dict(subject=subject, atlas=gargs.atlas, function='analyze_4d_data', fmri_file_template=local_fmri_template, labels_extract_mode='mean', overwrite_labels_data=True)) flags = fmri.call_main(args) if subject not in flags or not flags[subject]['analyze_4d_data']: continue args = connectivity.read_cmd_args( dict( subject=subject, atlas=gargs.atlas, function='calc_lables_connectivity', connectivity_modality='fmri', connectivity_method='corr', labels_extract_mode='mean', windows_length=34, # tr = 3 -> 100s windows_shift=4, # 12s save_mmvt_connectivity=True, calc_subs_connectivity=False, recalc_connectivity=True, n_jobs=gargs.n_jobs)) flags = connectivity.call_main(args) if subject in flags and flags[subject]['calc_lables_connectivity']: good_subjects.append(subject) print('{}/{} good subjects'.format(len(good_subjects), len(gargs.mri_subject))) print('Good subject: ', good_subjects) print('Bad subjects: ', set(gargs.mri_subject) - set(good_subjects))