def main(args): """Main function of cellanneal.""" if (args.start_temp is not None or args.end_temp is not None) and args.auto_temp == 1: raise Exception( "when auto_temp is set to 1(default value), starting temperature or ending temperature should not be set manually" ) if not args.no_parallel: import dask from dask.distributed import Client, LocalCluster if not args.cluster: cluster = LocalCluster( n_workers=args.workers, local_dir="/tmp/CellUniverse/dask-worker-space") else: cluster = args.cluster client = Client(cluster) else: client = None lineagefile = None start = time.time() try: if args.debug: debugmode = True config = load_config(args.config) celltype = config['global.cellType'].lower() # setup the colony from a file with the initial properties lineageframes = LineageFrames() colony = lineageframes.forward() load_colony(colony, args.initial, config) # open the lineage file for writing lineagefile = open(args.output / 'lineage.csv', 'w') header = ['file', 'name'] if celltype == 'bacilli': header.extend(['x', 'y', 'width', 'length', 'rotation']) print(','.join(header), file=lineagefile) if args.global_optimization: import global_optimization global_optimization.optimize(get_inputfiles(args), lineageframes, lineagefile, args, config) return 0 if args.auto_temp == 1: temperature, end_temperature = auto_temp_schedule( get_inputfiles(args)[0], lineageframes.forward(), args, config) setattr(args, 'start_temp', temperature) setattr(args, 'end_temp', end_temperature) print("auto temperature schedule finished") print("starting temperature is ", args.start_temp, "ending temperature is ", args.end_temp) for imagefile in get_inputfiles(args): colony = optimize(imagefile, lineageframes, args, config, client) # flatten modifications and save cell properties colony.flatten() for cellnode in colony: properties = [imagefile.name, cellnode.cell.name] if celltype == 'bacilli': properties.extend([ str(cellnode.cell.x), str(cellnode.cell.y), str(cellnode.cell.width), str(cellnode.cell.length), str(cellnode.cell.rotation) ]) print(','.join(properties), file=lineagefile) except KeyboardInterrupt as error: raise error finally: if lineagefile: lineagefile.close() print(f'{time.time() - start} seconds') return 0
def main(args): """Main function of cellanneal.""" if (args.start_temp is not None or args.end_temp is not None) and args.auto_temp == 1: raise Exception("when auto_temp is set to 1(default value), starting temperature or ending temperature should not be set manually") # if not args.no_parallel: # import dask # from dask.distributed import Client, LocalCluster # if not args.cluster: # cluster = LocalCluster( # n_workers=args.workers,local_dir="/tmp/CellUniverse/dask-worker-space" # ) # else: # cluster = args.cluster # # client = Client(cluster) # else: client = None lineagefile = None start = time.time() try: config = load_config(args.config) simulation_config = config["simulation"] #Maybe better to store the image type in the config file in the first place, instead of using cmd? if args.graySynthetic == True: simulation_config["image.type"] = "graySynthetic" elif args.phaseContrast == True: simulation_config["image.type"] = "phaseContrastImage" elif args.binary == True: simulation_config["image.type"] = "binary" else: raise ValueError("Invalid Command: Synthetic image type must be specified") if not args.output.is_dir(): args.output.mkdir() if not args.bestfit.is_dir(): args.bestfit.mkdir() if args.residual and not args.residual.is_dir(): args.residual.mkdir() seed = int(start * 1000) % (2**32) if args.seed != None: seed = args.seed np.random.seed(seed) print("Seed: {}".format(seed)) celltype = config['global.cellType'].lower() # setup the colony from a file with the initial properties lineageframes = LineageFrames() colony = lineageframes.forward() imagefiles = get_inputfiles(args) if args.lineage_file: load_colony(colony, args.lineage_file, config, initial_frame = imagefiles[0].name) else: load_colony(colony, args.initial, config) cost_diff = (-1, -1) # open the lineage file for writing lineagefile = open(args.output/'lineage.csv', 'w') header = ['file', 'name'] if celltype == 'bacilli': header.extend(['x', 'y', 'width', 'length', 'rotation', "split_alpha", "opacity"]) print(','.join(header), file=lineagefile) if args.debug: with open(args.debug/'debug.csv', 'w') as debugfile: print(','.join(['window_start', 'window_end', 'pbad_total', 'bad_count', 'temperature', 'total_cost_diff', 'current_iteration', 'total_iterations']), file=debugfile) if args.global_optimization: global useDistanceObjective useDistanceObjective = args.dist realimages = [optimization.load_image(imagefile) for imagefile in imagefiles] window = config["global_optimizer.window_size"] if args.lineage_file: lineage = global_optimization.build_initial_lineage(imagefiles, args.lineage_file, args.continue_from, config["simulation"]) else: lineage = global_optimization.build_initial_lineage(imagefiles, args.initial, args.continue_from, config["simulation"]) lineage = global_optimization.find_optimal_simulation_confs(imagefiles, lineage, realimages, args.continue_from) sim_start = args.continue_from - args.frame_first print(sim_start) shape = realimages[0].shape synthimages = [] cellmaps = [] distmaps = [] iteration_per_cell = config["iteration_per_cell"] if not useDistanceObjective: distmaps = [None] * len(realimages) for window_start in range(1 - window, len(realimages)): window_end = window_start + window print(window_start, window_end) if window_end <= len(realimages): # get initial estimate if window_start >= sim_start: if window_end > 1: lineage.copy_forward() realimage = realimages[window_end - 1] synthimage, cellmap = optimization.generate_synthetic_image(lineage.frames[window_end - 1].nodes, shape, lineage.frames[window_end - 1].simulation_config) synthimages.append(synthimage) cellmaps.append(cellmap) if useDistanceObjective: distmap = distance_transform_edt(realimage < .5) distmap /= config[f'{config["global.cellType"].lower()}.distanceCostDivisor'] * config[ 'global.pixelsPerMicron'] distmap += 1 distmaps.append(distmap) if args.auto_temp == 1 and window_end == 1: print("auto temperature schedule started") args.start_temp, args.end_temp = \ global_optimization.auto_temp_schedule(imagefiles, lineage, realimages, synthimages, cellmaps, distmaps, 0, 1, lineagefile, args, config) print("auto temperature schedule finished") print("starting temperature is ", args.start_temp, "ending temperature is ", args.end_temp) if args.auto_meth == "frame" and optimization.auto_temp_schedule_frame(window_end, 3): print("auto temperature schedule restarted") args.start_temp, args.end_temp = \ global_optimization.auto_temp_schedule(imagefiles, lineage, realimages, synthimages, cellmaps, distmaps, window_start, window_end, lineagefile, args, config) print("auto temperature schedule finished") print("starting temperature is ", args.start_temp, "ending temperature is ", args.end_temp) if window_start >= sim_start: if useDistanceObjective: global_optimization.totalCostDiff = optimization.dist_objective(realimage, synthimage, distmap, cellmap, config["overlap.cost"]) else: global_optimization.totalCostDiff = optimization.objective(realimage, synthimage, cellmap, config["overlap.cost"], config["cell.importance"]) global_optimization.optimize(imagefiles, lineage, realimages, synthimages, cellmaps, distmaps, window_start, window_end, lineagefile, args, config, iteration_per_cell) if window_start >= 0: global_optimization.save_lineage(imagefiles[window_start].name, lineage.frames[window_start].nodes, lineagefile) global_optimization.save_output(imagefiles[window_start].name, synthimages[window_start], realimages[window_start], lineage.frames[window_start].nodes, args, config) return 0 config["simulation"] = optimization.find_optimal_simulation_conf(config["simulation"], optimization.load_image(imagefiles[0]), list(colony)) if args.auto_temp == 1: print("auto temperature schedule started") args.start_temp, args.end_temp = optimization.auto_temp_schedule(imagefiles[0], lineageframes.forward(), args, config) print("auto temperature schedule finished") print("starting temperature is ", args.start_temp, "ending temperature is ", args.end_temp) frame_num = 0 prev_cell_num = len(colony) for imagefile in imagefiles: # Recomputing temperature when needed frame_num += 1 if args.auto_meth == "frame": if optimization.auto_temp_schedule_frame(frame_num, 8): print("auto temperature schedule started (recomputed)") args.start_temp, args.end_temp = optimization.auto_temp_schedule(imagefile, colony, args, config) print("auto temperature schedule finished") print("starting temperature is ", args.start_temp, "ending temperature is ", args.end_temp) elif args.auto_meth == "factor": if optimization.auto_temp_schedule_factor(len(colony), prev_cell_num, 1.1): print("auto temperature schedule started (recomputed)") args.start_temp, args.end_temp = optimization.auto_temp_schedule(imagefile, colony, args, config) print("auto temperature schedule finished") print("starting temperature is ", args.start_temp, "ending temperature is ", args.end_temp) prev_cell_num = len(colony) elif args.auto_meth == "const": if optimization.auto_temp_schedule_const(len(colony), prev_cell_num, 10): print("auto temperature schedule started (recomputed)") args.start_temp, args.end_temp = optimization.auto_temp_schedule(imagefile, colony, args, config) print("auto temperature schedule finished") print("starting temperature is ", args.start_temp, "ending temperature is ", args.end_temp) prev_cell_num = len(colony) elif args.auto_meth == "cost": print(cost_diff, frame_num, optimization.auto_temp_shcedule_cost(cost_diff)) if frame_num >= 2 and optimization.auto_temp_shcedule_cost(cost_diff): print("auto temperature schedule started cost_diff (recomputed)") args.start_temp, args.end_temp = optimization.auto_temp_schedule(imagefile, colony, args, config) print("auto temperature schedule finished") print("starting temperature is ", args.start_temp, "ending temperature is ", args.end_temp) colony = optimize(imagefile, lineageframes, args, config, client) cost_diff = optimization.update_cost_diff(colony, cost_diff) # flatten modifications and save cell properties colony.flatten() for cellnode in colony: properties = [imagefile.name, cellnode.cell.name] if celltype == 'bacilli': properties.extend([ str(cellnode.cell.x), str(cellnode.cell.y), str(cellnode.cell.width), str(cellnode.cell.length), str(cellnode.cell.rotation)]) print(','.join(properties), file=lineagefile) except KeyboardInterrupt as error: raise error finally: if lineagefile: lineagefile.close() print(f'{time.time() - start} seconds') return 0
def main(args): """Main function of cellanneal.""" if (args.start_temp is not None or args.end_temp is not None) and args.auto_temp == 1: raise Exception( "when auto_temp is set to 1(default value), starting temperature or ending temperature should not be set manually" ) # if not args.no_parallel: # import dask # from dask.distributed import Client, LocalCluster # if not args.cluster: # cluster = LocalCluster( # n_workers=args.workers,local_dir="/tmp/CellUniverse/dask-worker-space" # ) # else: # cluster = args.cluster # # client = Client(cluster) # else: client = None lineagefile = None start = time.time() try: config = load_config(args.config) simulation_config = config["simulation"] #Maybe better to store the image type in the config file in the first place, instead of using cmd? if args.graySynthetic == True: simulation_config["image.type"] = "graySynthetic" elif args.phaseContrast == True: simulation_config["image.type"] = "phaseContrastImage" elif args.binary == True: simulation_config["image.type"] = "binary" else: raise ValueError( "Invalid Command: Synthetic image type must be specified") if args.debug: debugmode = True celltype = config['global.cellType'].lower() # setup the colony from a file with the initial properties lineageframes = LineageFrames() colony = lineageframes.forward() load_colony(colony, args.initial, config) cost_diff = (-1, -1) # open the lineage file for writing lineagefile = open(args.output / 'lineage.csv', 'w') header = ['file', 'name'] if celltype == 'bacilli': header.extend(['x', 'y', 'width', 'length', 'rotation']) print(','.join(header), file=lineagefile) #optimze configuration config["simulation"] = find_optimal_simulation_conf( config["simulation"], load_image(get_inputfiles(args)[0]), list(colony)) if args.global_optimization: import global_optimization if args.auto_temp == 1: print("auto temperature schedule started") args.start_temp, args.end_temp = \ global_optimization.auto_temp_schedule(get_inputfiles(args), lineageframes, lineagefile, args, config) print("auto temperature schedule finished") print("starting temperature is ", args.start_temp, "ending temperature is ", args.end_temp) global_optimization.optimize(get_inputfiles(args), lineageframes, lineagefile, args, config) return 0 if args.auto_temp == 1: print("auto temperature schedule started") args.start_temp, args.end_temp = auto_temp_schedule( get_inputfiles(args)[0], lineageframes.forward(), args, config) print("auto temperature schedule finished") print("starting temperature is ", args.start_temp, "ending temperature is ", args.end_temp) frame_num = 0 prev_cell_num = len(colony) for imagefile in get_inputfiles( args): # Recomputing temperature when needed frame_num += 1 if args.auto_meth == "frame": if auto_temp_schedule_frame(frame_num, 8): print("auto temperature schedule started (recomputed)") args.start_temp, args.end_temp = auto_temp_schedule( imagefile, colony, args, config) print("auto temperature schedule finished") print("starting temperature is ", args.start_temp, "ending temperature is ", args.end_temp) elif args.auto_meth == "factor": if auto_temp_schedule_factor(len(colony), prev_cell_num, 1.1): print("auto temperature schedule started (recomputed)") args.start_temp, args.end_temp = auto_temp_schedule( imagefile, colony, args, config) print("auto temperature schedule finished") print("starting temperature is ", args.start_temp, "ending temperature is ", args.end_temp) prev_cell_num = len(colony) elif args.auto_meth == "const": if auto_temp_schedule_const(len(colony), prev_cell_num, 10): print("auto temperature schedule started (recomputed)") args.start_temp, args.end_temp = auto_temp_schedule( imagefile, colony, args, config) print("auto temperature schedule finished") print("starting temperature is ", args.start_temp, "ending temperature is ", args.end_temp) prev_cell_num = len(colony) elif args.auto_meth == "cost": print(cost_diff, frame_num, auto_temp_shcedule_cost(cost_diff)) if frame_num >= 2 and auto_temp_shcedule_cost(cost_diff): print( "auto temperature schedule started cost_diff (recomputed)" ) args.start_temp, args.end_temp = auto_temp_schedule( imagefile, colony, args, config) print("auto temperature schedule finished") print("starting temperature is ", args.start_temp, "ending temperature is ", args.end_temp) colony = optimize(imagefile, lineageframes, args, config, client) cost_diff = update_cost_diff(colony, cost_diff) # flatten modifications and save cell properties colony.flatten() for cellnode in colony: properties = [imagefile.name, cellnode.cell.name] if celltype == 'bacilli': properties.extend([ str(cellnode.cell.x), str(cellnode.cell.y), str(cellnode.cell.width), str(cellnode.cell.length), str(cellnode.cell.rotation) ]) print(','.join(properties), file=lineagefile) except KeyboardInterrupt as error: raise error finally: if lineagefile: lineagefile.close() print(f'{time.time() - start} seconds') return 0
def main(args): """Main function of cellanneal.""" import dask from dask.distributed import Client, LocalCluster if not args.cluster: cluster = LocalCluster(n_workers=args.workers, ) else: cluster = args.cluster client = Client(cluster) lineagefile = None start = time.time() try: if args.debug: debugmode = True config = load_config(args.config) celltype = config['global.cellType'].lower() # setup the colony from a file with the initial properties lineageframes = LineageFrames() colony = lineageframes.forward() load_colony(colony, args.initial, config) # open the lineage file for writing lineagefile = open(args.output / 'lineage.csv', 'w') header = ['file', 'name'] if celltype == 'bacilli': header.extend(['x', 'y', 'width', 'length', 'rotation']) print(','.join(header), file=lineagefile) for imagefile in get_inputfiles(args): colony = optimize(imagefile, lineageframes, args, config, client) # flatten modifications and save cell properties colony.flatten() for cellnode in colony: properties = [imagefile.name, cellnode.cell.name] if celltype == 'bacilli': properties.extend([ str(cellnode.cell.x), str(cellnode.cell.y), str(cellnode.cell.width), str(cellnode.cell.length), str(cellnode.cell.rotation) ]) print(','.join(properties), file=lineagefile) except KeyboardInterrupt as error: raise error finally: if lineagefile: lineagefile.close() print(f'{time.time() - start} seconds') return 0