def test_sort_tree_size(self): edges = igraph.Graph.Barabasi(n=500, m=3, directed=True).spanning_tree(None, True).get_edgelist() tree = Tree(0, edges) tree.set_subtree_size(edges) tree.sort_children_by_tree_size() children = tree.nodes[tree.root].children tmp_size = len(tree.nodes) for n in children: self.assertTrue(tree.nodes[n].tree_size <= tmp_size) tmp_size = tree.nodes[n].tree_size
def run(self): # compute a spanning tree of the input graph spanningTree = tlp.TreeTest.computeTree(self.graph) # get edges to use an input for the H3 layout implementation from buzzfeed # and reverse their orientation as it does not use the same direction as in Tulip graphs edges = [self.graph.ends(e)[::-1] for e in spanningTree.getEdges()] # compute the layout tree = Tree(edges) # copy result to Tulip layout property self.result.setAllEdgeValue([]) for n in self.graph.getNodes(): self.result[n] = tlp.Coord(tree.nodes[n].coord.x, tree.nodes[n].coord.y, tree.nodes[n].coord.z) # apply some scaling factor to the layout to get a correct rendering # in Tulip scaling = 1000 if self.dataSet: scaling = self.dataSet["layout scaling"] self.result.scale(tlp.Vec3f(scaling)) # cleanup computed spanning tree tlp.TreeTest.cleanComputedTree(self.graph, spanningTree) return True
def test_sort_tree_size(self): edges = igraph.Graph.Barabasi(n=500, m=3, directed=True).spanning_tree( None, True).get_edgelist() tree = Tree(0, edges) tree.set_subtree_size(edges) tree.sort_children_by_tree_size() children = tree.nodes[tree.root].children tmp_size = len(tree.nodes) for n in children: self.assertTrue(tree.nodes[n].tree_size <= tmp_size) tmp_size = tree.nodes[n].tree_size
def test_all_nodes_inserted(self): node_number = 500 edges = igraph.Graph.Barabasi(n=500, m=3, directed=True).spanning_tree( None, True).get_edgelist() tree = Tree(0, edges) self.assertEqual(node_number, len(tree.nodes))
import igraph import csv from h3.tree import Tree """ Export layout to csv: x, y, z """ if __name__ == '__main__': edges = igraph.Graph.Barabasi(n=1000, m=3, directed=True). \ spanning_tree(None, True).get_edgelist() tree = Tree(edges) with open('coordinates.csv', 'w+') as fp: csv_w = csv.writer(fp, delimiter=',') csv_w.writerow(['x', 'y', 'z']) for row in tree.get_coordinates(): csv_w.writerow(row)
from h3.tree import Tree """ Customized print tree to terminal Note: implemented by recursion, not scalable to a large number of nodes, only suitable for a small number of nodes """ def print_tree(tree, node_id, depth=0): children = tree.nodes[node_id].children total_children_area = 0 for child in children: total_children_area += tree.nodes[child.node_id].area print "\t" * tree.nodes[node_id].depth, \ "{0}, radius: {1}, parent: {2}, area: {3}, total_children_area: {4}" \ .format(node_id, tree.nodes[node_id].radius, tree.nodes[node_id].parent, tree.nodes[node_id].area, total_children_area) for child in children: print_tree(tree, child.node_id, depth) if __name__ == '__main__': edges = ((child, parent) for parent, child in igraph.Graph.Tree(40, 3).get_edgelist()) tree = Tree(edges) print_tree(tree, 0)
import igraph import collections import logging import json import csv from timeit import Timer from h3.tree import Tree """ Benchmark the performance of getting layout """ setup = """ import igraph from h3.tree import Tree edges = [(parent, child) for parent, child in igraph.Graph.Barabasi(n={0}, m=3, directed=True).\ spanning_tree(None, True).get_edgelist()] """ stmt = """ Tree(edges) """ if __name__ == '__main__': print "Nodes\tTime (s)" print "---------------" for n in xrange(1, 5): N = int(10**n) t = Timer(setup=setup.format(N), stmt=stmt).timeit(number=5) print "N={0}\tt={1:.4f}".format(N, t)
import igraph import collections import logging import json from h3.tree import Tree """ An example for drawing a random spanning tree with 500 nodes with tagging and equators while logging. """ if __name__ == '__main__': logging.basicConfig(level=logging.DEBUG, filename="log", filemode="w+", format="%(asctime)-10s \ %(levelname)-6s %(message)s") log = logging.getLogger() edges = igraph.Graph.Barabasi(n=500, m=3, directed=True).spanning_tree(None, True).get_edgelist() logging.info(edges) tree = Tree(edges) tree.scatter_plot()