def main(argv=None): '''Run the driver script for this module. This code only runs if we're being run as a script. Otherwise, it's silent and just exposes methods.''' args = process_command_line(argv) basename = os.path.basename(args.image) sha = basename[:basename.rfind('.')] # this only gets run the first time around, so if >1 image is # specified, they will ALL be in the file named by the sha of the first # one. But grepping around isn't too hard. logging.basicConfig(filename=log_name_gen(sha, args.log), level=logging.DEBUG, format='%(asctime)s %(message)s') logging.info("Beginning image %s", args.image) try: run_info = run_img(sitkstrats.read(args.image), sha, args.nseeds, args.media_root, args.seed) except Exception as exc: # pylint: disable=W0703 logging.critical("Encountered critical exception:\n%s", exc) raise write_info(run_info, filename=os.path.join(args.log, sha + "-seg.json")) return 0
def main(argv=None): '''Run the driver script for this module. This code only runs if we're being run as a script. Otherwise, it's silent and just exposes methods.''' args = process_command_line(argv) basename = os.path.basename(args.image) sha = basename[:basename.rfind('.')] # this only gets run the first time around, so if >1 image is # specified, they will ALL be in the file named by the sha of the first # one. But grepping around isn't too hard. logging.basicConfig(filename=log_name_gen(sha, args.log), level=logging.DEBUG, format='%(asctime)s %(message)s') logging.info("Beginning image %s", args.image) try: run_info = run_img(sitkstrats.read(args.image), sha, args.nseeds, args.media_root, args.seed) except Exception as exc: # pylint: disable=W0703 logging.critical("Encountered critical exception:\n%s", exc) raise write_info(run_info, filename=os.path.join(args.log, sha+"-seg.json")) return 0
def run_img(img, sha, nseeds, root_dir, addl_seed): # pylint: disable=C0111 '''Run the entire protocol on a particular image starting with sha hash''' img_info = {} lung_img, lung_info = debug_log(sitkstrats.segment_lung, (img, { 'probe_size': 7 }), root_dir, sha) img_info['lungseg'] = lung_info # (img, tmp_info) = debug_log(sitkstrats.crop_to_segmentation, # (img_in, lung_img), # root_dir, sha, subdir="crop") # lung_img = sitkstrats.crop_to_segmentation(lung_img, lung_img)[0] # img_info['crop'] = tmp_info segstrats = configure_strats() seed_indep_imgs = {} seed_indep_info = {} seeds = [] for (sname, strat) in [(strnam, segstrats[strnam]['seed-independent']) for strnam in segstrats]: try: optha = opthash(strat['opts']) fname = os.path.join(root_dir, strat['strategy'].__name__, sha + "-" + optha + ".nii") tmp_img = sitkstrats.read(fname) tmp_info = strat['opts'] tmp_info['file'] = os.path.join(fname) logging.info( "Loaded seed-independent image for '%s', '%s' from file", sname, fname) except RuntimeError: logging.debug("Building seed-independent image '%s', '%s'.", sname, fname) (tmp_img, tmp_info) = debug_log(strat['strategy'], (img, strat['opts']), root_dir, sha) logging.info("Built seed-independent image '%s', '%s' in %s", sname, fname, tmp_info['time']) seed_indep_imgs[sname] = tmp_img seed_indep_info[sname] = tmp_info # compute seeds, first by taking the centers of mass of a bunch of the # watershed segemented regions, then by adding a bunch of random ones that # are inside the lung field. if nseeds > 1 or addl_seed is None: (seeds, tmp_info) = sitkstrats.com_calc(img=seed_indep_imgs['watershed'], max_size=0.05, min_size=1e-5, lung_img=lung_img) img_info['deterministic-seeds'] = tmp_info seeds.extend(sitkstrats.distribute_seeds(lung_img, nseeds - len(seeds))) else: print("Provided seed only.") img_info['deterministic-seeds'] = None if addl_seed is not None: # additional seed is given in terms of the input image, and must # be corrected for cropping. if 'crop' in img_info: origin = img_info['crop']['origin'] assert len(addl_seed) == len(origin) addl_seed = [ addl_seed[i] - origin[i] for i in range(len(addl_seed)) ] seeds.insert(0, addl_seed) if len(seeds) > nseeds: logging.warning( "The number of seeds generated in the deterministic " + "phase (%s) is greater than the allowed number of " + "seeds (%s). The list of seeds is being truncated.", len(seeds), nseeds) # with many deterministic seeds, this list can be longer than nseeds. seeds = seeds[0:nseeds] seg_info = seeddep(seed_indep_imgs, seeds, root_dir, sha, segstrats, img_info['lungseg']['size'], img) img_info['noduleseg'] = {} for seed in seg_info: for segstrat in seg_info[seed]: combined_info = {'seed-dependent': seg_info[seed][segstrat]} if segstrat in seed_indep_info: combined_info['seed-independent'] = seed_indep_info[segstrat] img_info['noduleseg'].setdefault(seed, {})[segstrat] = combined_info return img_info
def run_img(img, sha, nseeds, root_dir, addl_seed): # pylint: disable=C0111 '''Run the entire protocol on a particular image starting with sha hash''' img_info = {} lung_img, lung_info = debug_log(sitkstrats.segment_lung, (img, {'probe_size': 7}), root_dir, sha) img_info['lungseg'] = lung_info # (img, tmp_info) = debug_log(sitkstrats.crop_to_segmentation, # (img_in, lung_img), # root_dir, sha, subdir="crop") # lung_img = sitkstrats.crop_to_segmentation(lung_img, lung_img)[0] # img_info['crop'] = tmp_info segstrats = configure_strats() seed_indep_imgs = {} seed_indep_info = {} for (sname, strat) in [(strnam, segstrats[strnam]['seed-independent']) for strnam in segstrats]: try: optha = opthash(strat['opts']) fname = os.path.join(root_dir, strat['strategy'].__name__, sha + "-" + optha + ".nii") tmp_img = sitkstrats.read(fname) tmp_info = strat['opts'] tmp_info['file'] = os.path.join(fname) logging.info( "Loaded seed-independent image for '%s', '%s' from file", sname, fname) except RuntimeError: logging.debug( "Building seed-independent image '%s', '%s'.", sname, fname) (tmp_img, tmp_info) = debug_log(strat['strategy'], (img, strat['opts']), root_dir, sha) logging.info( "Built seed-independent image '%s', '%s' in %s", sname, fname, tmp_info['time']) seed_indep_imgs[sname] = tmp_img seed_indep_info[sname] = tmp_info # compute seeds, first by taking the centers of mass of a bunch of the # watershed segemented regions, then by adding a bunch of random ones that # are inside the lung field. (seeds, tmp_info) = sitkstrats.com_calc(img=seed_indep_imgs['watershed'], max_size=0.05, min_size=1e-5, lung_img=lung_img) img_info['deterministic-seeds'] = tmp_info seeds.extend(sitkstrats.distribute_seeds(lung_img, nseeds-len(seeds))) if addl_seed is not None: # additional seed is given in terms of the input image, and must # be corrected for cropping. origin = img_info['crop']['origin'] assert len(addl_seed) == len(origin) addl_seed = [addl_seed[i] - origin[i] for i in range(len(addl_seed))] seeds.insert(0, addl_seed) if len(seeds) > nseeds: logging.warning("The number of seeds generated in the deterministic " + "phase (%s) is greater than the allowed number of " + "seeds (%s). The list of seeds is being truncated.", len(seeds), nseeds) # with many deterministic seeds, this list can be longer than nseeds. seeds = seeds[0:nseeds] seg_info = seeddep(seed_indep_imgs, seeds, root_dir, sha, segstrats, img_info['lungseg']['size'], img) img_info['noduleseg'] = {} for seed in seg_info: for segstrat in seg_info[seed]: combined_info = {'seed-dependent': seg_info[seed][segstrat]} if segstrat in seed_indep_info: combined_info['seed-independent'] = seed_indep_info[segstrat] img_info['noduleseg'].setdefault( seed, {})[segstrat] = combined_info return img_info