def evaluate_costs(crag_path, merges, ends, iterations, starting, ending, tmp=None): """ Evaluates the assignation of the given merges and end scores and returns the corresponding stats. Assumes node features have already been assigned :param crag_path: CRAG file path :param merges: Merge score to assign :param ends: End weight to assign :param iterations: Maximum number of iterations :param folder: Folder where to generate solution :param starting: Starting section to evaluate. Set to None for all :param ending: Ending section to evaluate. Set to None for all """ # Manage temporary file creation if tmp is None: tmp_path = tempfile.mkdtemp() else: dataio.create_dir(tmp) tmp_path = tmp # Copy crag into temporary file copy_path = os.path.join(tmp_path, 'crag.h5') shutil.copyfile(crag_path, copy_path) # Generate and evaluate solution cg = generate_solution(copy_path, merges, ends, iterations, tmp_path) rand_score, voi_split, voi_merge = cg.evaluate_solution(init=starting, end=ending) # Clean temporary data shutil.rmtree(tmp_path) return {'rand': rand_score, 'voi_split': voi_split, 'voi_merge': voi_merge}
def generate_solution(crag_path, merges, ends, iterations, model, folder, logs=None, edge_feat_value=1, nthreads=3): """ Solves the inference problem of the CRAG after assigning the corresponding features and weights to edges (No assignment edges) and nodes (Slice and assignment nodes). Slice nodes are assigned the descriptor provided by the model. Given the difference between the descriptors of the slices they connect, assignment nodes are set by the ratio between the distance and the maximum distance considering the other assignment they 'compete' with (outliers are identified as well). The other values assigned here are constant values. :param crag_path: CRAG file path :param merges: Merge score to assign :param ends: End weight to assign :param iterations: Maximum number of iterations :param model: Folder where the trained network is :param folder: Folder where to generate solution :param logs: Path where to store the logs. Set to None to disable :param edge_feat_value: Feature value to assign to the no-assignment edges :param nthreads: Number of threads to use Returns ------- cg: Crag class containing the solution """ print('Generating solution for merge score %f and end score %f' % (merges, ends)) if not os.path.isdir(model): raise ValueError('Model folder %s does not exist' % model) # Modify weights costs = cc.CragCostManager(crag_path) costs.update_node_weights(merges, weights=None) costs.update_edge_weights(ends) # Modify features costs.update_node_features(model) costs.update_edge_features(edge_feat_value) costs.save() # Evaluate resulting crag dataio.create_dir(folder) aux_crag = os.path.join(folder, 'project') cg = CragGenerator(aux_crag) cg.read_crag(crag_path) cg.solve(merges, ends, folder, iterations, logs=logs, threads=nthreads) return cg
def extract(self, outp): """ Extracts the merge trees in the given folder """ # Prepare temporary folder for extracting H5 files self.tmp = tempfile.mkdtemp() print('Processing data ...') self.parse_data() print('Extracting trees ...') dataio.create_dir(outp) self.extract_trees(outp) # Cleaning environment self.clean()
def merge_dataset(folder, histories, thresh, outp): """ Thresholds images by merging all supervoxels up to a given threshold :param folder: Folder containing superpixel images :param histories: Folder containing the corresponding merge tree histories :param thresh: Merge threshold up to which merge regions :param outp: Folder where to store resulting images """ dataio.create_dir(outp) # Read superpixels and histories sps = dataio.FileReader(folder).extract_files() hists = dataio.FileReader(histories, exts=['.txt', '.dat', '.data']).extract_files() for (s, h) in zip(sps, hists): img = merge_superpixels(s, h, thresh) name, ext = os.path.splitext(s) mh.imsave(os.path.join(outp, os.path.basename(name) + ext), img.astype(float))
def save_data(self, out_p, group='data/labels', min_digit=5, overwrite=True, int_data=False): """ Saves processed data. If path to HDF file provided, a dataset is created inside the given group. Otherwise, images are dumped into the folder specified in the path. Params: --------- out_p: string Output path. If it corresponds to a valid HDF5 it is stored as a HDF5 dataset. Otherwise it is stored in a file. group: string In case the output corresponds to a HDF5 file, it is the path inside the dataset where data needs to be stored. Subgroups must be separated by a /. Not used uf dumping into a folder. min_digit: integer Images are named, in order, with its position in the input volume. This number specifies the minimum amount of digits to use in the labeling if dumping data into a folder. Not used for HDF5 files. overwrite: boolean Whether to overwrite existing datasets in the destination path int_data: boolean By default data is stored as float. If this field is True, it is stored as unsigned integer in .png files. Only used if data path is a folder. """ if os.path.exists(out_p) and not overwrite: return self._check_data() if dataio.valid_volume_path(out_p): with h5py.File(out_p, 'w') as f: dataio.store_hf_group(f, group, self.imgs) else: dataio.create_dir(out_p) if int_data is True: dataio.volume_to_folder(self.imgs, out_p, min_digit=min_digit, typ='uint8', ext='.png') else: dataio.volume_to_folder(self.imgs, out_p, min_digit=min_digit)
def solve(self, node_bias, edge_bias, outp, iterations, logs=None, threads=3): """ Solves the inference problem given the costs assigned Params --------- node_bias: double Bias to be added to each node weight edge_bias: double Bias to be added to each edge weight outp: string Folder where to extract the solution found iterations: integer Number of iterations for the solver logs: string Path where to store the process logs. Set to None to disable threads: int Number of threads to use """ if not os.path.isdir(outp): dataio.create_dir(outp) if self.project_file is None: raise IOError('No CRAG project has been read/generated') args = [ "cmc_solve", "-f", str(node_bias), "-b", str(edge_bias), "-p", self.project_file, "--exportSolution=" + str(outp), "--numIterations=" + str(iterations), "--readOnly" ] # "--numThreads=" + str(threads) # ] logs = None if logs is None else os.path.join(logs, 'solve.log') p = Process(args, logs) p.execute(verbose=True) self.solution = outp