def process(image, brain_mask, args, logger): img = io.open_nii(image) mask = io.open_nii(brain_mask) dirname, base, ext = io.split_filename(image) if args.output_dir is not None: dirname = args.output_dir if not os.path.exists(dirname): logger.info('Making output directory: {}'.format(dirname)) os.mkdir(dirname) if args.find_background_mask: bg_mask = background_mask(img) bgfile = os.path.join(dirname, base + '_bgmask' + ext) io.save_nii(bg_mask, bgfile, is_nii=True) if args.wm_peak is not None: logger.info('Loading WM peak: ', args.wm_peak) peak = float(np.load(args.wm_peak)) else: peak = gmm_class_mask(img, brain_mask=mask, contrast=args.contrast) if args.save_wm_peak: np.save(os.path.join(dirname, base + '_wmpeak.npy'), peak) normalized = gmm.gmm_normalize(img, mask, args.norm_value, args.contrast, args.background_mask, peak) outfile = os.path.join(dirname, base + '_gmm' + ext) logger.info('Normalized image saved: {}'.format(outfile)) io.save_nii(normalized, outfile, is_nii=True)
def test_gmm_normalization(self): from intensity_normalization.normalize import gmm normalized = gmm.gmm_normalize(self.img, self.brain_mask, norm_value=self.norm_val) self.assertAlmostEqual( normalized.get_data()[self.wm_mask.get_data()].mean(), self.norm_val, delta=20)
def run_intensity_gmm(infile, outfolder): from intensity_normalization.normalize import gmm from intensity_normalization.utilities import io try: if not exists(join(outfolder, "Robex")): makedirs(join(outfolder, "Robex")) filename = infile.split(sep)[-1].split(".")[0] i = io.open_nii(join(outfolder, infile)) b_mask = io.open_nii( join(outfolder, "robex_masks", filename + "_mask.nii.gz")) normalized = gmm.gmm_normalize(i, b_mask) io.save_nii(normalized, join(outfolder, filename + "_gmm.nii.gz")) shutil.move(join(outfolder, infile), join(outfolder, "Robex")) except: e = sys.exc_info() print("Error: ", str(e[0])) print("Error: ", str(e[1])) print("Error: executing gmm method") sys.exit(2)
def gmm_normalisation(self): agent = nib.load(self.registration_path) new_agent = gmm.gmm_normalize(agent) nib.save(new_agent, self.normalisation_path)