def test_apply_warp(): import pkg_resources # Warp original anat to standard space using warp img (had to # invwarp first) and linear mats base_dir = os.path.abspath( pkg_resources.resource_filename("pynets", "../data/examples")) anat_dir = f"{base_dir}/003/anat" ref = pkg_resources.resource_filename( "pynets", f"templates/MNI152_T1_brain_" f"2mm.nii.gz") inp = f"{anat_dir}/sub-003_T1w.nii.gz" out = f"{anat_dir}/highres2standard_test_apply_warp.nii.gz" warp = f"{anat_dir}/highres2standard_warp.nii.gz" xfm = f"{anat_dir}/highres2standard.mat" utils.apply_warp(ref, inp, out, warp, xfm=xfm, mask=None, interp=None, sup=False) # highres2standard_apply_warp = f"{anat_dir}/highres2standard_test_apply_warp.nii.gz" # highres2standard_apply_warp = nib.load(highres2standard_apply_warp) # highres2standard_apply_warp = highres2standard_apply_warp.get_data() # # highres2standard_align_nonlinear = nib.load(f"{anat_dir}/highres2standard_nonlinear.nii.gz") # highres2standard_align_nonlinear = highres2standard_align_nonlinear.get_data() # check_eq_arrays = np.allclose(highres2standard_apply_warp.astype('float32'), # highres2standard_align_nonlinear.astype('float32')) # assert check_eq_arrays is True assert os.path.isfile(out)
def roi2t1w_align(roi, t1w_brain, mni2t1_xfm, mni2t1w_warp, roi_in_t1w, template, simple): """ A function to perform alignment of a roi from MNI space --> T1w. """ import time from pynets.registration import utils as regutils from nilearn.image import resample_to_img roi_img = nib.load(roi) template_img = nib.load(template) roi_img_res = resample_to_img(roi_img, template_img, interpolation="nearest") roi_res = f"{roi.split('.nii')[0]}_res.nii.gz" nib.save(roi_img_res, roi_res) # Apply warp or transformer resulting from the inverse MNI->T1w created # earlier if simple is False: regutils.apply_warp(t1w_brain, roi_res, roi_in_t1w, warp=mni2t1w_warp) else: regutils.applyxfm(t1w_brain, roi_res, mni2t1_xfm, roi_in_t1w) time.sleep(0.5) return roi_in_t1w
def waymask2dwi_align( waymask, t1w_brain, ap_path, mni2t1w_warp, mni2t1_xfm, t1wtissue2dwi_xfm, waymask_in_t1w, waymask_in_dwi, B0_mask_tmp_path, template, simple, ): """ A function to perform alignment of a waymask from MNI space --> T1w --> dwi. """ import time from pynets.registration import utils as regutils from nilearn.image import resample_to_img # Apply warp or transformer resulting from the inverse MNI->T1w created # earlier waymask_img = nib.load(waymask) template_img = nib.load(template) waymask_img_res = resample_to_img( waymask_img, template_img, ) waymask_res = f"{waymask.split('.nii')[0]}_res.nii.gz" nib.save(waymask_img_res, waymask_res) if simple is False: regutils.apply_warp(t1w_brain, waymask_res, waymask_in_t1w, warp=mni2t1w_warp) else: regutils.applyxfm(t1w_brain, waymask_res, mni2t1_xfm, waymask_in_t1w) time.sleep(0.5) # Apply transform from t1w to native dwi space regutils.applyxfm(ap_path, waymask_in_t1w, t1wtissue2dwi_xfm, waymask_in_dwi) time.sleep(0.5) waymask_in_dwi = regutils.apply_mask_to_image(waymask_in_dwi, B0_mask_tmp_path, waymask_in_dwi) return waymask_in_dwi
def atlas2t1w2dwi_align( uatlas, uatlas_parcels, atlas, t1w_brain, t1w_brain_mask, mni2t1w_warp, t1_aligned_mni, ap_path, mni2t1_xfm, t1w2dwi_xfm, wm_gm_int_in_dwi, aligned_atlas_t1mni, aligned_atlas_skull, dwi_aligned_atlas, dwi_aligned_atlas_wmgm_int, B0_mask, mni2dwi_xfm, simple, ): """ A function to perform atlas alignment atlas --> T1 --> dwi. Tries nonlinear registration first, and if that fails, does a linear registration instead. For this to succeed, must first have called t1w2dwi_align. """ import time from nilearn.image import resample_to_img from pynets.core.utils import checkConsecutive from pynets.registration import utils as regutils from nilearn.image import math_img from nilearn.masking import intersect_masks template_img = nib.load(t1_aligned_mni) if uatlas_parcels: atlas_img_orig = nib.load(uatlas_parcels) else: atlas_img_orig = nib.load(uatlas) old_count = len(np.unique(np.asarray(atlas_img_orig.dataobj))) uatlas_res_template = resample_to_img(atlas_img_orig, template_img, interpolation="nearest") uatlas_res_template = nib.Nifti1Image( np.asarray(uatlas_res_template.dataobj).astype('uint16'), affine=uatlas_res_template.affine, header=uatlas_res_template.header, ) nib.save(uatlas_res_template, aligned_atlas_t1mni) if simple is False: try: regutils.apply_warp( t1w_brain, aligned_atlas_t1mni, aligned_atlas_skull, warp=mni2t1w_warp, interp="nn", sup=True, mask=t1w_brain_mask, ) time.sleep(0.5) # Apply linear transformation from template to dwi space regutils.applyxfm(ap_path, aligned_atlas_skull, t1w2dwi_xfm, dwi_aligned_atlas, interp="nearestneighbour") time.sleep(0.5) except BaseException: print( "Warning: Atlas is not in correct dimensions, or input is low" " quality,\nusing linear template registration.") regutils.applyxfm(t1w_brain, aligned_atlas_t1mni, mni2t1_xfm, aligned_atlas_skull, interp="nearestneighbour") time.sleep(0.5) combine_xfms(mni2t1_xfm, t1w2dwi_xfm, mni2dwi_xfm) time.sleep(0.5) regutils.applyxfm(ap_path, aligned_atlas_t1mni, mni2dwi_xfm, dwi_aligned_atlas, interp="nearestneighbour") time.sleep(0.5) else: regutils.applyxfm(t1w_brain, aligned_atlas_t1mni, mni2t1_xfm, aligned_atlas_skull, interp="nearestneighbour") time.sleep(0.5) combine_xfms(mni2t1_xfm, t1w2dwi_xfm, mni2dwi_xfm) time.sleep(0.5) regutils.applyxfm(ap_path, aligned_atlas_t1mni, mni2dwi_xfm, dwi_aligned_atlas, interp="nearestneighbour") time.sleep(0.5) atlas_img = nib.load(dwi_aligned_atlas) wm_gm_img = nib.load(wm_gm_int_in_dwi) wm_gm_mask_img = math_img("img > 0", img=wm_gm_img) atlas_mask_img = math_img("img > 0", img=atlas_img) atlas_img_corr = nib.Nifti1Image( np.asarray(atlas_img.dataobj).astype('uint16'), affine=atlas_img.affine, header=atlas_img.header, ) # Get the union of masks dwi_aligned_atlas_wmgm_int_img = intersect_masks( [wm_gm_mask_img, atlas_mask_img], threshold=0, connected=False) nib.save(atlas_img_corr, dwi_aligned_atlas) nib.save(dwi_aligned_atlas_wmgm_int_img, dwi_aligned_atlas_wmgm_int) dwi_aligned_atlas = regutils.apply_mask_to_image(dwi_aligned_atlas, B0_mask, dwi_aligned_atlas) time.sleep(0.5) dwi_aligned_atlas_wmgm_int = regutils.apply_mask_to_image( dwi_aligned_atlas_wmgm_int, B0_mask, dwi_aligned_atlas_wmgm_int) time.sleep(0.5) final_dat = atlas_img_corr.get_fdata() unique_a = sorted(set(np.array(final_dat.flatten().tolist()))) if not checkConsecutive(unique_a): print("Warning! Non-consecutive integers found in parcellation...") new_count = len(unique_a) diff = np.abs(np.int(float(new_count) - float(old_count))) print(f"Previous label count: {old_count}") print(f"New label count: {new_count}") print(f"Labels dropped: {diff}") atlas_img.uncache() atlas_img_corr.uncache() atlas_img_orig.uncache() atlas_mask_img.uncache() wm_gm_img.uncache() wm_gm_mask_img.uncache() return dwi_aligned_atlas_wmgm_int, dwi_aligned_atlas, aligned_atlas_skull
def atlas2t1w_align(uatlas, uatlas_parcels, atlas, t1w_brain, t1w_brain_mask, t1_aligned_mni, mni2t1w_warp, mni2t1_xfm, gm_mask, aligned_atlas_t1mni, aligned_atlas_skull, aligned_atlas_gm, simple, gm_fail_tol=5): """ A function to perform atlas alignment from atlas --> T1w. """ import time from pynets.registration import utils as regutils from nilearn.image import resample_to_img # from pynets.core.utils import checkConsecutive template_img = nib.load(t1_aligned_mni) if uatlas_parcels: atlas_img_orig = nib.load(uatlas_parcels) else: atlas_img_orig = nib.load(uatlas) # old_count = len(np.unique(np.asarray(atlas_img_orig.dataobj))) uatlas_res_template = resample_to_img(atlas_img_orig, template_img, interpolation="nearest") uatlas_res_template = nib.Nifti1Image( np.asarray(uatlas_res_template.dataobj).astype('uint16'), affine=uatlas_res_template.affine, header=uatlas_res_template.header, ) nib.save(uatlas_res_template, aligned_atlas_t1mni) if simple is False: try: regutils.apply_warp( t1w_brain, aligned_atlas_t1mni, aligned_atlas_skull, warp=mni2t1w_warp, interp="nn", sup=True, mask=t1w_brain_mask, ) time.sleep(0.5) except BaseException: print( "Warning: Atlas is not in correct dimensions, or input is low " "quality,\nusing linear template registration.") regutils.applyxfm(t1w_brain, aligned_atlas_t1mni, mni2t1_xfm, aligned_atlas_skull, interp="nearestneighbour") time.sleep(0.5) else: regutils.applyxfm(t1w_brain, aligned_atlas_t1mni, mni2t1_xfm, aligned_atlas_skull, interp="nearestneighbour") time.sleep(0.5) # aligned_atlas_gm = regutils.apply_mask_to_image(aligned_atlas_skull, # gm_mask, # aligned_atlas_gm) aligned_atlas_gm = regutils.apply_mask_to_image(aligned_atlas_skull, t1w_brain_mask, aligned_atlas_gm) time.sleep(0.5) atlas_img = nib.load(aligned_atlas_gm) atlas_img_corr = nib.Nifti1Image( np.asarray(atlas_img.dataobj).astype('uint16'), affine=atlas_img.affine, header=atlas_img.header, ) nib.save(atlas_img_corr, aligned_atlas_gm) # final_dat = atlas_img_corr.get_fdata() # unique_a = sorted(set(np.array(final_dat.flatten().tolist()))) # # if not checkConsecutive(unique_a): # print("\nWarning! non-consecutive integers found in parcellation...") # new_count = len(unique_a) # diff = np.abs(np.int(float(new_count) - float(old_count))) # print(f"Previous label count: {old_count}") # print(f"New label count: {new_count}") # print(f"Labels dropped: {diff}") # if diff > gm_fail_tol: # print(f"Grey-Matter mask too restrictive >{str(gm_fail_tol)} for this " # f"parcellation. Falling back to the T1w mask...") # aligned_atlas_gm = regutils.apply_mask_to_image(aligned_atlas_skull, # t1w_brain_mask, # aligned_atlas_gm) # time.sleep(5) template_img.uncache() atlas_img_orig.uncache() atlas_img.uncache() atlas_img_corr.uncache() return aligned_atlas_gm, aligned_atlas_skull
def RegisterParcellation2MNIFunc_align(uatlas, template, template_mask, t1w_brain, t1w2mni_xfm, aligned_atlas_t1w, aligned_atlas_mni, t1w2mni_warp, simple): """ A function to perform atlas alignment from T1w atlas --> MNI. """ import time from pynets.registration import utils as regutils from nilearn.image import resample_to_img atlas_img = nib.load(uatlas) t1w_brain_img = nib.load(t1w_brain) uatlas_res_template = resample_to_img(atlas_img, t1w_brain_img, interpolation="nearest") uatlas_res_template = nib.Nifti1Image( np.asarray(uatlas_res_template.dataobj).astype('uint16'), affine=uatlas_res_template.affine, header=uatlas_res_template.header, ) nib.save(uatlas_res_template, aligned_atlas_t1w) if simple is False: try: regutils.apply_warp( template, aligned_atlas_t1w, aligned_atlas_mni, warp=t1w2mni_warp, interp="nn", sup=True, ) time.sleep(0.5) except BaseException: print("Warning: Atlas is not in correct dimensions, or input is " "low quality,\nusing linear template registration.") regutils.align( aligned_atlas_t1w, template, init=t1w2mni_xfm, out=aligned_atlas_mni, dof=6, searchrad=True, interp="nearestneighbour", cost="mutualinfo", ) time.sleep(0.5) else: regutils.align( aligned_atlas_t1w, template, init=t1w2mni_xfm, out=aligned_atlas_mni, dof=6, searchrad=True, interp="nearestneighbour", cost="mutualinfo", ) time.sleep(0.5) return aligned_atlas_mni
def tissue2dwi_align(self): """ A function to perform alignment of ventricle ROI's from MNI space --> dwi and CSF from T1w space --> dwi. First generates and performs dwi space alignment of avoidance/waypoint masks for tractography. First creates ventricle ROI. Then creates transforms from stock MNI template to dwi space. For this to succeed, must first have called both t1w2dwi_align. """ import sys import time import os.path as op import pkg_resources from pynets.core.utils import load_runconfig from nilearn.image import resample_to_img from nipype.utils.filemanip import fname_presuffix, copyfile from pynets.core.nodemaker import three_to_four_parcellation from nilearn.image import math_img, index_img hardcoded_params = load_runconfig() tiss_class = hardcoded_params['tracking']["tissue_classifier"][0] fa_template_path = pkg_resources.resource_filename( "pynets", f"templates/standard/FA_{self.vox_size}.nii.gz") if sys.platform.startswith('win') is False: try: fa_template_img = nib.load(fa_template_path) except indexed_gzip.ZranError as e: print( e, f"\nCannot load FA template. Do you have git-lfs " f"installed?") else: try: fa_template_img = nib.load(fa_template_path) except ImportError as e: print(e, f"\nCannot load FA template. Do you have git-lfs ") mni_template_img = nib.load(self.input_mni_brain) if not np.allclose(fa_template_img.affine, mni_template_img.affine) \ or not \ np.allclose(fa_template_img.shape, mni_template_img.shape): fa_template_img_res = resample_to_img(fa_template_img, mni_template_img) nib.save(fa_template_img_res, self.fa_template_res) else: self.fa_template_res = fname_presuffix(fa_template_path, suffix="_tmp", newpath=op.dirname( self.reg_path_img)) copyfile(fa_template_path, self.fa_template_res, copy=True, use_hardlink=False) # Register Lateral Ventricles and Corpus Callosum rois to t1w resample_to_img(nib.load(self.mni_atlas), nib.load(self.input_mni_brain), interpolation='nearest').to_filename(self.mni_roi_ref) roi_parcels = three_to_four_parcellation(self.mni_roi_ref) ventricle_roi = math_img("img1 + img2", img1=index_img(roi_parcels, 2), img2=index_img(roi_parcels, 13)) self.mni_vent_loc = fname_presuffix(self.mni_vent_loc, suffix="_tmp", newpath=op.dirname( self.reg_path_img)) ventricle_roi.to_filename(self.mni_vent_loc) del roi_parcels, ventricle_roi # Create transform from the HarvardOxford atlas in MNI to T1w. # This will be used to transform the ventricles to dwi space. regutils.align( self.mni_roi_ref, self.input_mni_brain, xfm=self.xfm_roi2mni_init, init=None, bins=None, dof=6, cost="mutualinfo", searchrad=True, interp="spline", out=None, ) # Create transform to align roi to mni and T1w using flirt regutils.applyxfm( self.input_mni_brain, self.mni_vent_loc, self.xfm_roi2mni_init, self.vent_mask_mni, ) time.sleep(0.5) if self.simple is False: # Apply warp resulting from the inverse MNI->T1w created earlier regutils.apply_warp( self.t1w_brain, self.vent_mask_mni, self.vent_mask_t1w, warp=self.mni2t1w_warp, interp="nn", sup=True, ) time.sleep(0.5) if sys.platform.startswith('win') is False: try: nib.load(self.corpuscallosum) except indexed_gzip.ZranError as e: print( e, f"\nCannot load Corpus Callosum ROI. " f"Do you have git-lfs installed?") else: try: nib.load(self.corpuscallosum) except ImportError as e: print( e, f"\nCannot load Corpus Callosum ROI. " f"Do you have git-lfs installed?") regutils.apply_warp( self.t1w_brain, self.corpuscallosum, self.corpuscallosum_mask_t1w, warp=self.mni2t1w_warp, interp="nn", sup=True, ) else: regutils.applyxfm(self.vent_mask_mni, self.t1w_brain, self.mni2t1_xfm, self.vent_mask_t1w) time.sleep(0.5) regutils.applyxfm( self.corpuscallosum, self.t1w_brain, self.mni2t1_xfm, self.corpuscallosum_mask_t1w, ) time.sleep(0.5) # Applyxfm to map FA template image to T1w space regutils.applyxfm(self.t1w_brain, self.fa_template_res, self.mni2t1_xfm, self.fa_template_t1w) time.sleep(0.5) # Applyxfm tissue maps to dwi space if self.t1w_brain_mask is not None: regutils.applyxfm( self.ap_path, self.t1w_brain_mask, self.t1wtissue2dwi_xfm, self.t1w_brain_mask_in_dwi, ) time.sleep(0.5) regutils.applyxfm(self.ap_path, self.vent_mask_t1w, self.t1wtissue2dwi_xfm, self.vent_mask_dwi) time.sleep(0.5) regutils.applyxfm(self.ap_path, self.csf_mask, self.t1wtissue2dwi_xfm, self.csf_mask_dwi) time.sleep(0.5) regutils.applyxfm(self.ap_path, self.gm_mask, self.t1wtissue2dwi_xfm, self.gm_in_dwi) time.sleep(0.5) regutils.applyxfm(self.ap_path, self.wm_mask, self.t1wtissue2dwi_xfm, self.wm_in_dwi) time.sleep(0.5) regutils.applyxfm( self.ap_path, self.corpuscallosum_mask_t1w, self.t1wtissue2dwi_xfm, self.corpuscallosum_dwi, ) time.sleep(0.5) csf_thr = 0.95 wm_thr = 0.05 gm_thr = 0.05 # Threshold WM to binary in dwi space nib.save(math_img(f"img > {wm_thr}", img=nib.load(self.wm_in_dwi)), self.wm_in_dwi_bin) # Threshold GM to binary in dwi space nib.save(math_img(f"img > {gm_thr}", img=nib.load(self.gm_in_dwi)), self.gm_in_dwi_bin) # Threshold CSF to binary in dwi space nib.save(math_img(f"img > {csf_thr}", img=nib.load(self.csf_mask_dwi)), self.csf_mask_dwi_bin) # Threshold WM to binary in dwi space self.wm_in_dwi = regutils.apply_mask_to_image(self.wm_in_dwi, self.wm_in_dwi_bin, self.wm_in_dwi) # Threshold GM to binary in dwi space self.gm_in_dwi = regutils.apply_mask_to_image(self.gm_in_dwi, self.gm_in_dwi_bin, self.gm_in_dwi) # Threshold CSF to binary in dwi space self.csf_mask = regutils.apply_mask_to_image(self.csf_mask_dwi, self.csf_mask_dwi_bin, self.csf_mask_dwi) # Create ventricular CSF mask print("Creating Ventricular CSF mask...") math_img("(img1 + img2) > 0.0001", img1=nib.load(self.csf_mask_dwi), img2=nib.load(self.vent_mask_dwi)).to_filename( self.vent_csf_in_dwi) print("Creating Corpus Callosum mask...") math_img("(img1*img2 - img3) > 0.0001", img1=nib.load(self.corpuscallosum_dwi), img2=nib.load(self.wm_in_dwi_bin), img3=nib.load(self.vent_csf_in_dwi)).to_filename( self.corpuscallosum_dwi) # Create GM-WM interface image math_img("((img1*img2 + img3)*img4) > 0.0001", img1=nib.load(self.gm_in_dwi_bin), img2=nib.load(self.wm_in_dwi_bin), img3=nib.load(self.corpuscallosum_dwi), img4=nib.load(self.B0_mask)).to_filename( self.wm_gm_int_in_dwi) return
def tissue2dwi_align(self): """ A function to perform alignment of ventricle ROI's from MNI space --> dwi and CSF from T1w space --> dwi. First generates and performs dwi space alignment of avoidance/waypoint masks for tractography. First creates ventricle ROI. Then creates transforms from stock MNI template to dwi space. For this to succeed, must first have called both t1w2dwi_align. """ import sys import time import os.path as op import pkg_resources from pynets.core.utils import load_runconfig from nilearn.image import resample_to_img hardcoded_params = load_runconfig() tiss_class = hardcoded_params['tracking']["tissue_classifier"][0] fa_template_path = pkg_resources.resource_filename( "pynets", f"templates/FA_{self.vox_size}.nii.gz") if sys.platform.startswith('win') is False: try: fa_template_img = nib.load(fa_template_path) except indexed_gzip.ZranError as e: print( e, f"\nCannot load FA template. Do you have git-lfs " f"installed?") else: try: fa_template_img = nib.load(fa_template_path) except ImportError as e: print(e, f"\nCannot load FA template. Do you have git-lfs ") mni_template_img = nib.load(self.input_mni_brain) fa_template_img_res = resample_to_img(fa_template_img, mni_template_img) nib.save(fa_template_img_res, self.fa_template_res) # Register Lateral Ventricles and Corpus Callosum rois to t1w if not op.isfile(self.mni_atlas): raise FileNotFoundError("FSL atlas for ventricle reference not" " found!") # Create transform to MNI atlas to T1w using flirt. This will be use to # transform the ventricles to dwi space. regutils.align( self.mni_atlas, self.input_mni_brain, xfm=self.xfm_roi2mni_init, init=None, bins=None, dof=6, cost="mutualinfo", searchrad=True, interp="spline", out=None, ) time.sleep(0.5) if sys.platform.startswith('win') is False: try: nib.load(self.mni_vent_loc) except indexed_gzip.ZranError as e: print( e, f"\nCannot load ventricle ROI. Do you have git-lfs " f"installed?") else: try: nib.load(self.mni_vent_loc) except ImportError as e: print( e, f"\nCannot load ventricle ROI. Do you have git-lfs " f"installed?") # Create transform to align roi to mni and T1w using flirt regutils.applyxfm( self.input_mni_brain, self.mni_vent_loc, self.xfm_roi2mni_init, self.vent_mask_mni, ) time.sleep(0.5) if self.simple is False: # Apply warp resulting from the inverse MNI->T1w created earlier regutils.apply_warp( self.t1w_brain, self.vent_mask_mni, self.vent_mask_t1w, warp=self.mni2t1w_warp, interp="nn", sup=True, ) time.sleep(0.5) if sys.platform.startswith('win') is False: try: nib.load(self.corpuscallosum) except indexed_gzip.ZranError as e: print( e, f"\nCannot load Corpus Callosum ROI. " f"Do you have git-lfs installed?") else: try: nib.load(self.corpuscallosum) except ImportError as e: print( e, f"\nCannot load Corpus Callosum ROI. " f"Do you have git-lfs installed?") regutils.apply_warp( self.t1w_brain, self.corpuscallosum, self.corpuscallosum_mask_t1w, warp=self.mni2t1w_warp, interp="nn", sup=True, ) else: regutils.applyxfm(self.vent_mask_mni, self.t1w_brain, self.mni2t1_xfm, self.vent_mask_t1w) time.sleep(0.5) regutils.applyxfm( self.corpuscallosum, self.t1w_brain, self.mni2t1_xfm, self.corpuscallosum_mask_t1w, ) time.sleep(0.5) # Applyxfm to map FA template image to T1w space regutils.applyxfm(self.t1w_brain, self.fa_template_res, self.mni2t1_xfm, self.fa_template_t1w) time.sleep(0.5) # Applyxfm tissue maps to dwi space if self.t1w_brain_mask is not None: regutils.applyxfm( self.ap_path, self.t1w_brain_mask, self.t1wtissue2dwi_xfm, self.t1w_brain_mask_in_dwi, ) time.sleep(0.5) regutils.applyxfm(self.ap_path, self.vent_mask_t1w, self.t1wtissue2dwi_xfm, self.vent_mask_dwi) time.sleep(0.5) regutils.applyxfm(self.ap_path, self.csf_mask, self.t1wtissue2dwi_xfm, self.csf_mask_dwi) time.sleep(0.5) regutils.applyxfm(self.ap_path, self.gm_mask, self.t1wtissue2dwi_xfm, self.gm_in_dwi) time.sleep(0.5) regutils.applyxfm(self.ap_path, self.wm_mask, self.t1wtissue2dwi_xfm, self.wm_in_dwi) time.sleep(0.5) regutils.applyxfm( self.ap_path, self.corpuscallosum_mask_t1w, self.t1wtissue2dwi_xfm, self.corpuscallosum_dwi, ) time.sleep(0.5) if tiss_class == 'wb' or tiss_class == 'cmc': csf_thr = 0.50 wm_thr = 0.15 gm_thr = 0.10 else: csf_thr = 0.99 wm_thr = 0.10 gm_thr = 0.075 # Threshold WM to binary in dwi space thr_img = nib.load(self.wm_in_dwi) thr_img = math_img(f"img > {wm_thr}", img=thr_img) nib.save(thr_img, self.wm_in_dwi_bin) # Threshold GM to binary in dwi space thr_img = nib.load(self.gm_in_dwi) thr_img = math_img(f"img > {gm_thr}", img=thr_img) nib.save(thr_img, self.gm_in_dwi_bin) # Threshold CSF to binary in dwi space thr_img = nib.load(self.csf_mask_dwi) thr_img = math_img(f"img > {csf_thr}", img=thr_img) nib.save(thr_img, self.csf_mask_dwi_bin) # Threshold WM to binary in dwi space self.wm_in_dwi = regutils.apply_mask_to_image(self.wm_in_dwi, self.wm_in_dwi_bin, self.wm_in_dwi) time.sleep(0.5) # Threshold GM to binary in dwi space self.gm_in_dwi = regutils.apply_mask_to_image(self.gm_in_dwi, self.gm_in_dwi_bin, self.gm_in_dwi) time.sleep(0.5) # Threshold CSF to binary in dwi space self.csf_mask = regutils.apply_mask_to_image(self.csf_mask_dwi, self.csf_mask_dwi_bin, self.csf_mask_dwi) time.sleep(0.5) # Create ventricular CSF mask print("Creating Ventricular CSF mask...") os.system(f"fslmaths {self.vent_mask_dwi} -kernel sphere 10 -ero " f"-bin {self.vent_mask_dwi}") time.sleep(1) os.system(f"fslmaths {self.csf_mask_dwi} -add {self.vent_mask_dwi} " f"-bin {self.vent_csf_in_dwi}") time.sleep(1) print("Creating Corpus Callosum mask...") os.system( f"fslmaths {self.corpuscallosum_dwi} -mas {self.wm_in_dwi_bin} " f"-sub {self.vent_csf_in_dwi} " f"-bin {self.corpuscallosum_dwi}") time.sleep(1) # Create gm-wm interface image os.system(f"fslmaths {self.gm_in_dwi_bin} -mul {self.wm_in_dwi_bin} " f"-add {self.corpuscallosum_dwi} " f"-mas {self.B0_mask} -bin {self.wm_gm_int_in_dwi}") time.sleep(1) return