def test_functions(self): """ Test the helper functions in csv_related """ fn = self.get_input_file_name("csv.txt") rows = csv_related.read_csv(fn) self.assertEqual(len(rows), 6) rows = csv_related.read_csv_keyword(fn, "id") self.assertEqual(len(rows), 3)
def read_merge_tree(self, fn): """ Read and create a merge tree from a file. The format of the file is the format written by write merge_tree() It does not matter the order of the indices in the file, as they are sorted by this function. The only condition is that the index for the vertex that is the root of the tree MUST be the largest. This is guaranteed when creating a merge tree with create_merge_tree() @param fn File containing the merge tree """ log.info("Reading merge tree from %s", fn) ps = self.rb_states_table.get_particles() log.debug("particles") for p in ps: log.debug("%s", p.get_name()) rows = csv_related.read_csv(fn, delimiter=field_delim) for i in range(len(rows)): row = rows[i] rows[i] = [int(row[0]), int(row[1]), int(row[2]), row[3]] # Sort rows by vertice index rows.sort() subsets = [] # build subsets from the rows text for row in rows: names = row[3].split(unit_delim) log.debug("row %s names %s", row, names) # get particles in the subset particles = [] for name in names: l = [p for p in ps if p.get_name() == name] if (len(l) > 1): ValueError("More than one particle with same name" % names) particles.extend(l) s = domino.Subset(particles) subset_names = [p.get_name() for p in particles] log.debug("Merge tree Subset %s. Particles %s ", s, subset_names) subsets.append(s) # The vertex with the largest index is the root. # trick: get the merge tree object from a tree with only one node ... jt = domino.SubsetGraph() jt.add_vertex(subsets[0]) mt = domino.get_merge_tree(jt) # ... and add the rest of the vertices for i in range(1, len(subsets)): mt.add_vertex(subsets[i]) # the name of the vertex is a subset # set edges for row in rows: vid = row[0] child_left = row[1] child_right = row[2] if child_left != -1: mt.add_edge(vid, child_left) if child_right != -1: mt.add_edge(vid, child_right) self.merge_tree = mt log.info("%s", self.merge_tree.show_graphviz())
def read_reference_frames(fn, n=10): """ Read the reference frames contained in a solutions file from sampling n is the maximum number of ref frames to read. NOTE: Currently the function returns only the reference frames and discards the score, the first element of a row """ rows = csv_related.read_csv(fn, delimiter="/") x = min(n, len(rows)) all_refs = [] for i, row in enumerate(rows[0:x]): refs = [TextToReferenceFrame(t).get_reference_frame() for t in row[1:]] all_refs.append(refs) return all_refs