def convert_rest_dicoms_to_mgz(subject, rest_fol): output_fname = op.join(FMRI_DIR, subject, '{}_rest.mgz'.format(subject)) if op.isfile(output_fname): return output_fname dicom_files = glob.glob(op.join(rest_fol, 'MR*')) dicom_files.sort(key=op.getmtime) fu.mri_convert(dicom_files[0], output_fname) if op.isfile(output_fname): return output_fname else: raise Exception('Can\'t find {}!'.format(output_fname))
def convert_ct_to_mgz(subject, ct_raw_input_fol, ct_fol='', output_name='ct_org.mgz', overwrite=False, print_only=False, ask_before=False): if not op.isdir(ct_fol): ct_fol = utils.make_dir(op.join(MMVT_DIR, subject, 'ct')) if op.isfile(op.join(ct_fol, 'ct_reg_to_mr.mgz')) and not overwrite: return True if op.isfile(op.join(ct_fol, output_name)): return True ct_fname = utils.select_one_file(glob.glob(op.join(ct_fol, '*.mgz'))) if op.isfile(ct_fname): if utils.namebase(ct_fname) != 'ct_org': utils.make_link(ct_fname, op.join(ct_fol, 'ct_org.mgz')) return True output_fname = op.join(ct_fol, output_name) if op.isfile(output_fname): if not overwrite: return True else: os.remove(output_fname) if op.isfile(op.join(SUBJECTS_DIR, subject, 'ct', 'ct.nii.gz')): ct_files = [op.join(SUBJECTS_DIR, subject, 'ct', 'ct.nii.gz')] elif op.isfile(op.join(SUBJECTS_DIR, subject, 'mri', 'ct.nii.gz')): ct_files = [op.join(SUBJECTS_DIR, subject, 'mri', 'ct.nii.gz')] else: if not op.isdir(ct_raw_input_fol): print('{} does not exist!'.format(ct_fol)) return False ct_files = glob.glob(op.join(ct_raw_input_fol, '*.dcm')) if len(ct_files) == 0: sub_folders = [ d for d in glob.glob(op.join(ct_raw_input_fol, '*')) if op.isdir(d) ] if len(sub_folders) == 0: print('Cannot find CT files in {}!'.format(ct_raw_input_fol)) return False fol = utils.select_one_file(sub_folders, '', 'CT', is_dir=True) ct_files = glob.glob(op.join(fol, '*.dcm')) if len(ct_files) == 0: print('Cannot find CT files in {}!'.format(fol)) return False ct_files.sort(key=op.getmtime) if ask_before: ret = input('convert {} to {}? '.format(ct_files[0], output_fname)) if not au.is_true(ret): return False fu.mri_convert(ct_files[0], output_fname, print_only=print_only) return True if print_only else op.isfile(output_fname)
def convert_rest_dicoms_to_mgz(subject, rest_fol, overwrite=False): try: root = utils.make_dir(op.join(FMRI_DIR, subject)) output_fname = op.join(root, '{}_rest.mgz'.format(subject)) if op.isfile(output_fname): if not overwrite: return output_fname if overwrite: os.remove(output_fname) dicom_files = glob.glob(op.join(rest_fol, 'MR*')) dicom_files.sort(key=op.getmtime) fu.mri_convert(dicom_files[0], output_fname) if op.isfile(output_fname): return output_fname else: print('Can\'t find {}!'.format(output_fname)) return '' except: utils.print_last_error_line() return ''
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)