def gen_tissue(self): """ A function to segment and threshold tissue types from T1w. """ # Segment the t1w brain into probability maps maps = regutils.segment_t1w(self.t1w_brain, self.map_path) self.wm_mask = maps['wm_prob'] self.gm_mask = maps['gm_prob'] self.csf_mask = maps['csf_prob'] self.t1w_brain = regutils.check_orient_and_dims(self.t1w_brain, self.vox_size) self.wm_mask = regutils.check_orient_and_dims(self.wm_mask, self.vox_size) self.gm_mask = regutils.check_orient_and_dims(self.gm_mask, self.vox_size) self.csf_mask = regutils.check_orient_and_dims(self.csf_mask, self.vox_size) # Threshold WM to binary in dwi space t_img = load_img(self.wm_mask) mask = math_img('img > 0.2', img=t_img) mask.to_filename(self.wm_mask_thr) # Threshold T1w brain to binary in anat space t_img = load_img(self.t1w_brain) mask = math_img('img > 0.0', img=t_img) mask.to_filename(self.t1w_brain_mask) # Extract wm edge os.system("fslmaths {} -edge -bin -mas {} {}".format(self.wm_mask_thr, self.wm_mask_thr, self.wm_edge)) return
def gen_tissue( self, wm_mask_existing, gm_mask_existing, csf_mask_existing, overwrite ): """ A function to segment and threshold tissue types from T1w. """ import time import shutil # Segment the t1w brain into probability maps if ( wm_mask_existing is not None and gm_mask_existing is not None and csf_mask_existing is not None and overwrite is False ): print("Existing segmentations detected...") wm_mask = regutils.check_orient_and_dims( wm_mask_existing, self.basedir_path, self.vox_size, overwrite=False) gm_mask = regutils.check_orient_and_dims( gm_mask_existing, self.basedir_path, self.vox_size, overwrite=False) csf_mask = regutils.check_orient_and_dims( csf_mask_existing, self.basedir_path, self.vox_size, overwrite=False) else: try: maps = regutils.segment_t1w(self.t1w_brain, self.map_name) time.sleep(0.5) wm_mask = maps["wm_prob"] gm_mask = maps["gm_prob"] csf_mask = maps["csf_prob"] except RuntimeError as e: import sys print(e, "Segmentation failed. Does the input anatomical image " "still contained skull?" ) sys.exit(1) # Threshold WM to binary in dwi space t_img = nib.load(wm_mask) mask = math_img("img > 0.20", img=t_img) mask.to_filename(self.wm_mask_thr) # Extract wm edge self.wm_edge = regutils.get_wm_contour(wm_mask, self.wm_mask_thr, self.wm_edge) time.sleep(0.5) shutil.copyfile(wm_mask, self.wm_mask) shutil.copyfile(gm_mask, self.gm_mask) shutil.copyfile(csf_mask, self.csf_mask) return
def test_segment_t1w(): """ Test segment_t1w functionality """ base_dir = str(Path(__file__).parent/"examples") anat_dir = f"{base_dir}/003/anat" t1w = f"{anat_dir}/sub-003_T1w.nii.gz" basename = f"{anat_dir}/test_segment_t1w" out = reg_utils.segment_t1w(t1w, basename, opts='') print(out) assert out is not None
def gen_tissue(self, wm_mask_existing, gm_mask_existing, overwrite): """ A function to segment and threshold tissue types from T1w. """ # Segment the t1w brain into probability maps if (wm_mask_existing is not None and gm_mask_existing is not None and overwrite is False): print("Existing segmentations detected...") gm_mask = regutils.check_orient_and_dims(gm_mask_existing, self.basedir_path, self.vox_size, overwrite=False) wm_mask = regutils.check_orient_and_dims(wm_mask_existing, self.basedir_path, self.vox_size, overwrite=False) else: try: maps = regutils.segment_t1w(self.t1w_brain, self.map_name) gm_mask = maps["gm_prob"] wm_mask = maps["wm_prob"] except RuntimeError: print( "Segmentation failed. Does the input anatomical image still contained skull?" ) # Threshold GM to binary in func space t_img = nib.load(gm_mask) mask = math_img("img > 0.02", img=t_img) mask.to_filename(self.gm_mask_thr) os.system( f"fslmaths {gm_mask} -mas {self.gm_mask_thr} {self.gm_mask} 2>/dev/null" ) # Threshold WM to binary in dwi space t_img = nib.load(wm_mask) mask = math_img("img > 0.50", img=t_img) mask.to_filename(self.wm_mask_thr) # Extract wm edge os.system( f"fslmaths {wm_mask} -edge -bin -mas {self.wm_mask_thr} {self.wm_edge} 2>/dev/null" ) return
def gen_tissue(self): """ A function to segment and threshold tissue types from T1w. """ # Segment the t1w brain into probability maps maps = regutils.segment_t1w(self.t1w_brain, self.map_path) self.gm_mask = maps['gm_prob'] self.t1w_brain = regutils.check_orient_and_dims(self.t1w_brain, self.vox_size) self.gm_mask = regutils.check_orient_and_dims(self.gm_mask, self.vox_size) # Threshold WM to binary in dwi space t_img = load_img(self.gm_mask) mask = math_img('img > 0.1', img=t_img) mask.to_filename(self.gm_mask_thr) # Threshold T1w brain to binary in anat space t_img = load_img(self.t1w_brain) mask = math_img('img > 0.0', img=t_img) mask.to_filename(self.t1w_brain_mask) return
def gen_tissue(self): # Segment the t1w brain into probability maps self.maps = mgru.segment_t1w(self.t1w_brain, self.map_path) self.wm_mask = self.maps['wm_prob'] self.gm_mask = self.maps['gm_prob'] self.csf_mask = self.maps['csf_prob'] self.t1w_brain = utils.match_target_vox_res(self.t1w_brain, self.vox_size, self.anat_path, sens='anat') self.wm_mask = utils.match_target_vox_res(self.wm_mask, self.vox_size, self.anat_path, sens='anat') self.gm_mask = utils.match_target_vox_res(self.gm_mask, self.vox_size, self.anat_path, sens='anat') self.csf_mask = utils.match_target_vox_res(self.csf_mask, self.vox_size, self.anat_path, sens='anat') # Threshold WM to binary in dwi space self.t_img = load_img(self.wm_mask) self.mask = math_img('img > 0.2', img=self.t_img) self.mask.to_filename(self.wm_mask_thr) # Threshold T1w brain to binary in anat space self.t_img = load_img(self.t1w_brain) self.mask = math_img('img > 0.0', img=self.t_img) self.mask.to_filename(self.t1w_brain_mask) # Extract wm edge cmd = 'fslmaths ' + self.wm_mask_thr + ' -edge -bin -mas ' + self.wm_mask_thr + ' ' + self.wm_edge os.system(cmd) return