def init_func_folder(self, folder_name): func_folder = MRT_Methods.init_mrt_folder(folder_name, "func") anat_file, func_file, sbref_file, AP, PA = MRT_Methods.load_initial_files( folder_name, "func") func_file, func_mask = self.MotionCorrection_and_Mask( func_file, func_folder) tmp_dir = f"{func_folder}/tmp" if not os.path.isdir(tmp_dir): os.mkdir(tmp_dir) files = [anat_file, sbref_file, AP, PA] for i, file in enumerate(files): files[i] = shutil.copy(file, f"{func_folder}/{file.split(os.sep)[-1]}") anat_file, sbref_file, AP, PA = files return anat_file, func_file, func_mask, sbref_file, tmp_dir, AP, PA
def fibre_orientation(self, dwi_file: str, dwi_mask: str, wm_resp: str, gm_resp: str, csf_resp: str): fod_wm, fod_gm, fod_csf = [ resp.replace("response", "FOD") for resp in [wm_resp, gm_resp, csf_resp] ] fod_wm, fod_gm, fod_csf = [ odf.replace("txt", "mif") for odf in [fod_wm, fod_gm, fod_csf] ] tissues = f"{os.path.dirname(dwi_file)}/tissues.mif" if not os.path.isfile(tissues): print("Estimating Fibre Orientation Distributions") fod_wm, fod_gm, fod_csf = mrt_methods.calc_fibre_orientation( dwi_file, dwi_mask, wm_resp, gm_resp, csf_resp) tissues = mrt_methods.gen_tissues_orient(fod_wm, fod_gm, fod_csf) return fod_wm, fod_gm, fod_csf, tissues
def Gen_FA(self, dwi_file: str, mask_file: str): dti_file = f"{os.path.dirname(dwi_file)}/dti.mif" fa_file = f"{os.path.dirname(dwi_file)}/fa.mif" if not os.path.isfile(dti_file) or not os.path.isfile(fa_file): print("Generating FA image for group-level analysis") fa_file = mrt_methods.fit_tensors(dwi_file, mask_file, dti_file) return fa_file
def gen_5tt(self, t1_registered: str, t1_mask_regisitered: str): out_vis = f"{os.path.dirname(t1_registered)}/vis.mif" out_5tt = f"{os.path.dirname(t1_registered)}/5TT.mif" if not os.path.isfile(out_vis): print( "Generating five-tissue-type (5TT) image for Anatomically-Constrained Tractography (ACT)" ) out_vis, out_5tt = mrt_methods.five_tissue(t1_registered, t1_mask_regisitered) return out_vis, out_5tt
def T1_correction(self, anat: str): anat_dir = f"{os.path.dirname(anat)}/T1.anat" if not os.path.isdir(anat_dir): print( "Performing brain extraction and B1 bias field correction of T1 image" ) bias_corr_brain, bias_corr_mask = MRT_Methods.T1_correction(anat) else: corr_list = glob.glob(f"{anat_dir}/T1_biascorr_") corr_list.sort() bias_corr_brain, bias_corr_mask = corr_list return bias_corr_brain, bias_corr_mask
def gen_dwi_to_T1_contrast(self, dwi_file: str, dwi_mask: str, t1_file: str, t1_mask: str): meanbzero = dwi_file.replace(".mif", "_meanbzero.mif") dwi_pseudoT1 = dwi_file.replace(".mif", "_pseudoT1.mif") T1_pseudobzero = f"{os.path.dirname(dwi_file)}/T1_pseudobzero.mif" if not os.path.isfile(T1_pseudobzero): print( "Generating contrast-matched images for inter-modal registration between DWIs and T1" ) meanbzero, dwi_pseudoT1, T1_pseudobzero = mrt_methods.DWI_to_T1_cont( dwi_file, dwi_mask, t1_file, t1_mask) return meanbzero, dwi_pseudoT1, T1_pseudobzero
def sort_files(self, folder_name): connectome, dwi, tractogram, anat = [ f"{folder_name}/{sub_dir}" for sub_dir in ["connectome", "dwi", "tractogram", "anat"] ] for sub_dir in [connectome, dwi, tractogram, anat]: if not os.path.isdir(sub_dir): os.makedirs(sub_dir) new_file = None for file in glob.glob(f"{folder_name}/*"): if not os.path.isdir(file): header = file.split(os.sep)[-1] if ("dwi" in header or "AP" in header or "b0" in header or "tissues" in header or "bzero" in header): new_file = f'{dwi}/{header.replace("mif","nii")}' if "mif" in file and not os.path.isfile(new_file): new_file = mrt_methods.convert_to_mif(file, new_file) else: shutil.copy(file, f"{dwi}/{header}") elif "T1" in file or "vis" in file or "5TT" in file: new_file = f'{anat}/{file.split(os.sep)[-1].replace("mif", "nii")}' if "mif" in file and not os.path.isfile(new_file): new_file = mrt_methods.convert_to_mif(file, new_file) else: shutil.copy(file, f"{anat}/{file.split(os.sep)[-1]}") else: new_file = ( f'{tractogram}/{file.split(os.sep)[-1].replace("mif", "nii")}' ) if "mif" in file and not os.path.isfile(new_file): new_file = mrt_methods.convert_to_mif(file, new_file) else: shutil.copy(file, f"{tractogram}/{file.split(os.sep)[-1]}") if new_file: if os.path.isfile(new_file): print( f"Moved {file.split(os.sep)[-1]} to {os.sep.join(new_file.split(os.sep)[-2:])}" )
def init_func_folder(self, folder_name): mrt_folder = MRT_Methods.init_mrt_folder(folder_name, "func") anat_file, func, sbref, AP, PA, bvec, bval = MRT_Methods.load_initial_files( folder_name, "func") dwi, mask = self.MotionCorrection_and_Mask(AP, mrt_folder) file_list = [anat_file, func, sbref, AP, PA, mask] print("Converting files into .mif format...") for f in file_list: f_name = f.split(os.sep)[-1] new_f = f'{mrt_folder}/{f_name.replace(".nii.gz",".mif")}' if not os.path.isfile(new_f): if "T1" in f: print("Importing T1 image into temporary directory") new_anat = MRT_Methods.convert_to_mif(f, new_f) elif "mask" in f: print("Importing mask image into temporary directory") new_mask = MRT_Methods.convert_to_mif(f, new_f) else: if "AP" in f: print("Importing DWI data into temporary directory") new_dwi = MRT_Methods.convert_to_mif( f, new_f, bvec, bval) elif "PA" in f: print( "Importing reversed phased encode data into temporary directory" ) new_PA = MRT_Methods.convert_to_mif(f, new_f) elif "rest_bold" in f: print( "Importing rs-fMRI data into temporary directory") new_func = MRT_Methods.convert_to_mif(f, new_f) elif "rest_sbref" in f: print("Importing rs reference to temporary directory") new_sbref = MRT_Methods.convert_to_mif(f, new_f) else: if "T1" in f: new_anat = new_f elif "mask" in f: new_mask = new_f else: if "AP" in f: new_dwi = new_f elif "PA" in f: new_PA = new_f elif "rest_bold" in f: new_func = new_f elif "rest_sbref" in f: new_sbref = new_f return new_anat, new_func, new_sbref, new_dwi, new_mask, new_PA
def gen_Response(self, dwi_file: str, dwi_mask: str): working_dir = os.path.dirname(dwi_file) wm_resp, gm_resp, csf_resp = [ f"{working_dir}/response_{tissue}.txt" for tissue in ["wm", "gm", "csf"] ] if not os.path.isfile(wm_resp): print( "Estimating tissue response functions for spherical deconvolution" ) wm_resp, gm_resp, csf_resp = mrt_methods.gen_response( dwi_file, dwi_mask, working_dir) return wm_resp, gm_resp, csf_resp
def reg_dwi_and_t1( self, dwi_file: str, t1_brain: str, dwi_pseudoT1: str, T1_pseudobzero: str, meanbzero: str, t1_mask: str, dwi_mask: str, ): working_dir = os.path.dirname(dwi_file) t1_registered = f"{working_dir}/T1_registered.mif" t1_mask_registered = f"{working_dir}/T1_mask_registered.mif" if not os.path.isfile(t1_registered) or not os.path.isfile( t1_mask_registered): print("Performing registration between DWIs and T1") t1_registered, t1_mask_registered = mrt_methods.reg_dwi_T1( dwi_file, t1_brain, dwi_pseudoT1, T1_pseudobzero, meanbzero, t1_mask, dwi_mask, True, ) else: t1_registered, t1_mask_registered = mrt_methods.reg_dwi_T1( dwi_file, t1_brain, dwi_pseudoT1, T1_pseudobzero, meanbzero, t1_mask, dwi_mask, False, ) return t1_registered, t1_mask_registered
def run(self): if "dwi" in self.data_type: for i, folder_name in enumerate(self.subjects_dirs): subj = self.subjects[i] print(f"Analyzing {subj}...") anat, dwi, mask, PA = self.init_mrt_folder(folder_name) denoised = self.denoise(dwi_file=dwi, dwi_mask=mask) degibbs = self.unring(denoised) preprocessed = self.DWI_preproc(degibbs, PA) dwi_file = self.bias_correct(preprocessed, subj=subj) bias_corr_brain, bias_corr_mask = MRT_Methods.T1_correction( anat) elif "func" in self.data_type: for i, folder_name in enumerate(self.subjects_dirs): subj = self.subjects[i] print(f"Analyzing {subj}...") anat, func, sbref, AP, mask, PA = self.init_func_folder( folder_name) denoised = self.denoise(dwi_file=AP, dwi_mask=mask) degibbs = self.unring(denoised) preprocessed = self.DWI_preproc(degibbs, PA, func) dwi_file = self.bias_correct(preprocessed, subj=subj) bias_corr_brain, bias_corr_mask = MRT_Methods.T1_correction( anat)
def DWI_preproc(self, degibbs: str, PA: str, func: str = None): if not func: data_type = "dwi" func = None out_file = f"{os.path.dirname(degibbs)}/dwi1_preprocessed.mif" else: data_type = "func" func = func out_file = f"{os.path.dirname(degibbs)}/func_preprocessed.mif" if not os.path.isfile(out_file): print("Performing various geometric corrections of DWIs") out_file = MRT_Methods.DWI_prep( degibbs=degibbs, PA=PA, out_file=out_file, data_type=self.data_type, func=func, ) return out_file
def convert_tck_to_trk(self, tck_file: str, dwi_file: str): trk_file = tck_file.replace("tck", "trk") if not os.path.isfile(trk_file): print("Converting tractography file from .tck format to .trk") trk_file = mrt_methods.convert_tck_to_trk(tck_file, dwi_file) return trk_file
def calc_tracts(self, fod_wm: str, seg_5tt: str): Tracts = f"{os.path.dirname(fod_wm)}/tractogram.tck" if not os.path.isfile(Tracts): print("Performing whole-brain fibre-tracking") Tracts = mrt_methods.generate_tracks(fod_wm, seg_5tt) return Tracts
def unring(self, denoised: str): out_file = denoised.replace(".mif", "_degibbs.mif") if not os.path.isfile(out_file): print("Performing Gibbs ringing removal for DWI data") MRT_Methods.Unring(in_file=denoised, out_file=out_file) return out_file
def bias_correct(self, preprocessed: str, subj: str): out_file = f"{os.path.dirname(preprocessed)}/{subj}_dwi.mif" if not os.path.isfile(out_file): print("Performing initial B1 bias field correction of DWIs") out_file = MRT_Methods.bias_correct(preprocessed, out_file) return out_file
def denoise(self, dwi_file: str, dwi_mask: str): out_file = dwi_file.replace(".mif", "_denoised.mif") if not os.path.isfile(out_file): print("Performing MP-PCA denoising of DWI data") out_file = MRT_Methods.Denoise(dwi_file, dwi_mask, out_file) return out_file