示例#1
0
    def costdiff(self) -> float:
        new_synth = self.synthimage.copy()
        new_cellmap = self.cellmap.copy()
        region = self.node.children[0].cell.region.union(self.s1.region).union(self.s2.region)
        self.node.children[0].cell.draw(new_synth, new_cellmap, optimization.is_background, self.config['simulation'])
        self.s1.draw(new_synth, new_cellmap, optimization.is_cell, self.config['simulation'])
        self.s2.draw(new_synth, new_cellmap, optimization.is_cell, self.config['simulation'])

        if useDistanceObjective:
            start_cost = optimization.dist_objective(self.realimage[region.top:region.bottom, region.left:region.right],
                                                     self.synthimage[region.top:region.bottom, region.left:region.right],
                                                     self.distmap[region.top:region.bottom, region.left:region.right],
                                                     self.cellmap[region.top:region.bottom, region.left:region.right])
            end_cost = optimization.dist_objective(self.realimage[region.top:region.bottom, region.left:region.right],
                                                   new_synth[region.top:region.bottom, region.left:region.right],
                                                   self.distmap[region.top:region.bottom, region.left:region.right],
                                                   new_cellmap[region.top:region.bottom, region.left:region.right])
        else:
            start_cost = optimization.objective(self.realimage[region.top:region.bottom, region.left:region.right],
                                self.synthimage[region.top:region.bottom, region.left:region.right],
                                self.cellmap[region.top:region.bottom, region.left:region.right])
            end_cost = optimization.objective(self.realimage[region.top:region.bottom, region.left:region.right],
                              new_synth[region.top:region.bottom, region.left:region.right],
                              new_cellmap[region.top:region.bottom, region.left:region.right])

        return end_cost - start_cost
示例#2
0
    def costdiff(self) -> float:
        overlap_cost = self.config["overlap.cost"]
        new_synth = self.synthimage.copy()
        new_cellmap = self.cellmap.copy()
        region = self.combination.simulated_region(
            self.frame.simulation_config)

        for child in self.node.children:
            region = region.union(
                child.cell.simulated_region(self.frame.simulation_config))

        for child in self.node.children:
            child.cell.draw(new_synth, new_cellmap, optimization.is_background,
                            self.frame.simulation_config)

        self.combination.draw(new_synth, new_cellmap, optimization.is_cell,
                              self.frame.simulation_config)

        if useDistanceObjective:
            start_cost = optimization.dist_objective(
                self.realimage[region.top:region.bottom,
                               region.left:region.right],
                self.synthimage[region.top:region.bottom,
                                region.left:region.right],
                self.distmap[region.top:region.bottom,
                             region.left:region.right],
                self.cellmap[region.top:region.bottom,
                             region.left:region.right], overlap_cost)
            end_cost = optimization.dist_objective(
                self.realimage[region.top:region.bottom,
                               region.left:region.right],
                new_synth[region.top:region.bottom, region.left:region.right],
                self.distmap[region.top:region.bottom,
                             region.left:region.right],
                new_cellmap[region.top:region.bottom,
                            region.left:region.right], overlap_cost)
        else:
            start_cost = optimization.objective(
                self.realimage[region.top:region.bottom,
                               region.left:region.right],
                self.synthimage[region.top:region.bottom,
                                region.left:region.right],
                self.cellmap[region.top:region.bottom,
                             region.left:region.right], overlap_cost,
                self.config["cell.importance"])
            end_cost = optimization.objective(
                self.realimage[region.top:region.bottom,
                               region.left:region.right],
                new_synth[region.top:region.bottom, region.left:region.right],
                new_cellmap[region.top:region.bottom,
                            region.left:region.right], overlap_cost,
                self.config["cell.importance"])

        return end_cost - start_cost - self.config["split.cost"]
    def costdiff(self):
        overlap_cost = self.config["overlap.cost"]
        new_synth = self.synthimage.copy()
        new_cellmap = self.cellmap.copy()
        region = self.node.cell.simulated_region(self.frame.simulation_config).\
            union(self.replacement_cell.simulated_region(self.frame.simulation_config))
        self.node.cell.draw(new_synth, new_cellmap, optimization.is_background,
                            self.frame.simulation_config)
        self.replacement_cell.draw(new_synth, new_cellmap,
                                   optimization.is_cell,
                                   self.frame.simulation_config)

        if useDistanceObjective:
            start_cost = optimization.dist_objective(
                self.realimage[region.top:region.bottom,
                               region.left:region.right],
                self.synthimage[region.top:region.bottom,
                                region.left:region.right],
                self.distmap[region.top:region.bottom,
                             region.left:region.right],
                self.cellmap[region.top:region.bottom,
                             region.left:region.right], overlap_cost)
            end_cost = optimization.dist_objective(
                self.realimage[region.top:region.bottom,
                               region.left:region.right],
                new_synth[region.top:region.bottom, region.left:region.right],
                self.distmap[region.top:region.bottom,
                             region.left:region.right],
                new_cellmap[region.top:region.bottom,
                            region.left:region.right], overlap_cost)
        else:
            start_cost = optimization.objective(
                self.realimage[region.top:region.bottom,
                               region.left:region.right],
                self.synthimage[region.top:region.bottom,
                                region.left:region.right],
                self.cellmap[region.top:region.bottom,
                             region.left:region.right], overlap_cost,
                self.config["cell.importance"])
            end_cost = optimization.objective(
                self.realimage[region.top:region.bottom,
                               region.left:region.right],
                new_synth[region.top:region.bottom, region.left:region.right],
                new_cellmap[region.top:region.bottom,
                            region.left:region.right], overlap_cost,
                self.config["cell.importance"])

        return end_cost - start_cost + \
              0 * (np.sqrt((self.node.cell.x - self.replacement_cell.x)**2 + (self.node.cell.y - self.replacement_cell.y)**2)
            + 0 * abs(self.node.cell.rotation - self.replacement_cell.rotation))
def save_output(imagefiles, realimages, lineage: LineageM, args, lineagefile):
    shape = realimages[0].shape
    for frame_index in range(len(lineage.frames)):
        realimage = realimages[frame_index]
        cellnodes = lineage.frames[frame_index].nodes
        synthimage = optimization.generate_synthetic_image(
            cellnodes, realimage.shape, grey_synthetic_image)
        cost = optimization.objective(realimage, synthimage)
        print('Final Cost:', cost)

        frame = np.empty((shape[0], shape[1], 3))
        frame[..., 0] = realimage
        frame[..., 1] = frame[..., 0]
        frame[..., 2] = frame[..., 0]

        if not args.output.is_dir():
            args.output.mkdir()

        for node in cellnodes:
            node.cell.drawoutline(frame, (1, 0, 0))
            properties = [imagefiles[frame_index].name, node.cell.name]
            properties.extend([
                str(node.cell.x),
                str(node.cell.y),
                str(node.cell.width),
                str(node.cell.length),
                str(node.cell.rotation)
            ])
            print(','.join(properties), file=lineagefile)

        frame = np.clip(frame, 0, 1)

        debugimage = Image.fromarray((255 * frame).astype(np.uint8))
        debugimage.save(args.output / imagefiles[frame_index].name)
示例#5
0
 def costdiff(self) -> float:
     overlap_cost = self.config["overlap.cost"]
     if useDistanceObjective:
         start_cost = optimization.dist_objective(self.realimage,
                                                  self.old_synthimage,
                                                  self.distmap,
                                                  self.cellmap,
                                                  overlap_cost)
         end_cost = optimization.dist_objective(self.realimage,
                                                self.new_synthimage,
                                                self.distmap, self.cellmap,
                                                overlap_cost)
     else:
         start_cost = optimization.objective(self.realimage,
                                             self.old_synthimage,
                                             self.cellmap, overlap_cost,
                                             self.config["cell.importance"])
         end_cost = optimization.objective(self.realimage,
                                           self.new_synthimage,
                                           self.cellmap, overlap_cost,
                                           self.config["cell.importance"])
     return end_cost - start_cost
示例#6
0
def save_output(imagefiles, realimages, synthimages, cellmaps,
                lineage: LineageM, args, lineagefile, config):
    for frame_index in range(len(lineage.frames)):
        realimage = realimages[frame_index]
        cellnodes = lineage.frames[frame_index].nodes
        cellmap = cellmaps[frame_index]
        synthimage = synthimages[frame_index]
        cost = optimization.objective(realimage, synthimage, cellmap,
                                      config["overlap.cost"],
                                      config["cell.importance"])
        print('Final Cost:', cost)
        for node in cellnodes:
            properties = [imagefiles[frame_index].name, node.cell.name]
            properties.extend([
                str(node.cell.x),
                str(node.cell.y),
                str(node.cell.width),
                str(node.cell.length),
                str(node.cell.rotation)
            ])
            print(','.join(properties), file=lineagefile)
def save_output(imagefiles, realimages, lineage: LineageM, args):
    shape = realimages[0].shape
    for frame_index in range(len(lineage.frames)):
        realimage = realimages[frame_index]
        cellnodes = lineage.frames[frame_index].nodes
        synthimage = optimization.generate_synthetic_image(
            cellnodes, realimage.shape, grey_synthetic_image)
        cost = optimization.objective(realimage, synthimage)
        print('Final Cost:', cost)

        frame = np.empty((shape[0], shape[1], 3))
        frame[..., 0] = realimage
        frame[..., 1] = frame[..., 0]
        frame[..., 2] = frame[..., 0]

        for node in cellnodes:
            node.cell.drawoutline(frame, (1, 0, 0))

        frame = np.clip(frame, 0, 1)

        debugimage = Image.fromarray((255 * frame).astype(np.uint8))
        debugimage.save(args.output / imagefiles[frame_index].name)
示例#8
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