def stats(self): if self.graph.num_vertices() == 0: print(self.graph.num_vertices(),'vertices',self.graph.num_edges(),'edges') return avgdeg, stddevdeg = gt.vertex_average(self.graph, 'total') avgwt, stddevwt = gt.edge_average(self.graph, self.graph.ep['distance']) print(str(self.graph.num_vertices()) + ' vertices and ' + str(self.graph.num_edges()) + ' edges') print('Average vertex degree',avgdeg,'standard deviation',stddevdeg) print('Average edge weight',avgwt,'standard deviation',stddevwt)
def degreeStats(g): avg, std = vertex_average(g, "total" if not g.is_directed() else "in") total_degrees = g.get_out_degrees(g.get_vertices()) print("Graus") stats(total_degrees) print("\tDesvio padrão (graphtools): ", std) distribution = vertex_hist(g, "total" if not g.is_directed() else "in") histogram(distribution, "Distribuição de graus", "$k_{total}$", "$NP(k_{in})$", sys.argv[1][:-8] + ".graus")
def sredni_wspolczynnik_klasteryzacji(self): '''The local clustering coefficient [watts-collective-1998] ci is defined as ci=|{ejk}| / (ki(ki−1)) :vj,vk∈Ni,ejk∈E where ki is the out-degree of vertex i, and Ni={vj:eij∈E} is the set of out-neighbours of vertex i. For undirected graphs the value of ci is normalized as c′i=2ci. The implemented algorithm runs in O(|V|⟨k⟩2) time, where ⟨k⟩ is the average out-degree https://en.wikipedia.org/wiki/Clustering_coefficient''' lc = local_clustering(self.graph, undirected=True) return vertex_average(self.graph, lc)[0]
def degree_distribution(self, g, name): total_hist = gt.vertex_hist(g, "total", float_count=False) self.__plot_degree(total_hist, self.name + ' ' + name + ' ' + "totaldegdist.pdf", "total node degree") # in_hist = gt.vertex_hist(g, "in", float_count=False) # self.__plot_degree(in_hist, self.name + ' ' + name + ' ' + "indegdistloglog.pdf", "in node degree") # # out_hist = gt.vertex_hist(g, "out", float_count=False) # self.__plot_degree(out_hist, self.name + ' ' + name + ' ' + "outdegdistloglog.pdf", "out node degree") [atot, stdm] = gt.vertex_average(g, "total") stdtot = stdm * np.sqrt(g.num_vertices()) return atot, stdtot, stdm
def useGraphTool(pd): # Extract the graphml representation of the planner data graphml = pd.printGraphML() f = open("graph.graphml", 'w') f.write(graphml) f.close() # Load the graphml data using graph-tool graph = gt.load_graph("graph.graphml", fmt="xml") edgeweights = graph.edge_properties["weight"] # Write some interesting statistics avgdeg, stddevdeg = gt.vertex_average(graph, "total") avgwt, stddevwt = gt.edge_average(graph, edgeweights) print("---- PLANNER DATA STATISTICS ----") print( str(graph.num_vertices()) + " vertices and " + str(graph.num_edges()) + " edges") print("Average vertex degree (in+out) = " + str(avgdeg) + " St. Dev = " + str(stddevdeg)) print("Average edge weight = " + str(avgwt) + " St. Dev = " + str(stddevwt)) _, hist = gt.label_components(graph) print("Strongly connected components: " + str(len(hist))) # Make the graph undirected (for weak components, and a simpler drawing) graph.set_directed(False) _, hist = gt.label_components(graph) print("Weakly connected components: " + str(len(hist))) # Plotting the graph gt.remove_parallel_edges(graph) # Removing any superfluous edges edgeweights = graph.edge_properties["weight"] colorprops = graph.new_vertex_property("string") vertexsize = graph.new_vertex_property("double") start = -1 goal = -1 for v in range(graph.num_vertices()): # Color and size vertices by type: start, goal, other if pd.isStartVertex(v): start = v colorprops[graph.vertex(v)] = "cyan" vertexsize[graph.vertex(v)] = 10 elif pd.isGoalVertex(v): goal = v colorprops[graph.vertex(v)] = "green" vertexsize[graph.vertex(v)] = 10 else: colorprops[graph.vertex(v)] = "yellow" vertexsize[graph.vertex(v)] = 5 # default edge color is black with size 0.5: edgecolor = graph.new_edge_property("string") edgesize = graph.new_edge_property("double") for e in graph.edges(): edgecolor[e] = "black" edgesize[e] = 0.5 # using A* to find shortest path in planner data if start != -1 and goal != -1: _, pred = gt.astar_search(graph, graph.vertex(start), edgeweights) # Color edges along shortest path red with size 3.0 v = graph.vertex(goal) while v != graph.vertex(start): p = graph.vertex(pred[v]) for e in p.out_edges(): if e.target() == v: edgecolor[e] = "red" edgesize[e] = 2.0 v = p pos = graph.new_vertex_property("vector<double>") for v in range(graph.num_vertices()): vtx = pd.getVertex(v) st = vtx.getState() pos[graph.vertex(v)] = [st[0], st[1]] # Writing graph to file: # pos indicates the desired vertex positions, and pin=True says that we # really REALLY want the vertices at those positions # gt.graph_draw(graph, pos=pos, vertex_size=vertexsize, vertex_fill_color=colorprops, # edge_pen_width=edgesize, edge_color=edgecolor, # output="graph.pdf") gt.graph_draw(graph, pos=pos, output="graph.pdf") print('\nGraph written to graph.pdf') graph.vertex_properties["pos"] = pos graph.vertex_properties["vsize"] = vertexsize graph.vertex_properties["vcolor"] = colorprops graph.edge_properties["esize"] = edgesize graph.edge_properties["ecolor"] = edgecolor graph.save("mgraph.graphml") print('\nGraph saved to mgraph.graphml')
with open("{}".format(args_main.graph), 'r') as f: for l in f: edges.append((l.split("\t")[0], l.split("\t")[2].rstrip("\n"))) g.add_edge_list(edges, hashed=True) number_of_vertices = len(list(g.vertices())) number_of_edges = len(list(g.edges())) print("### STATISTICS ###") print("\tNumber of vertices: {}".format(number_of_vertices)) print("\tNumber of edges: {}".format(number_of_edges)) clustering_coeff, std_error = gt.global_clustering(g) avg_degree, avg_degree_std = gt.vertex_average(g, "total") avg_in_degree, avg_in_degree_std = gt.vertex_average(g, "in") avg_out_degree, avg_out_degree_std = gt.vertex_average(g, "out") zero_in_deg = 0 zero_out_deg = 0 isolated_entities = 0 for v in g.vertices(): if v.in_degree() == 0: zero_in_deg += 1 if v.out_degree() == 0: zero_out_deg += 1 if v.in_degree() == 0 and v.out_degree() == 0: isolated_entities += 1 print("\n\tClustering Coefficient: {}".format(clustering_coeff))
def useGraphTool(pd, space): # Extract the graphml representation of the planner data graphml = pd.printGraphML() f = open("graph.xml", 'w') f.write(graphml) f.close() # Load the graphml data using graph-tool graph = gt.load_graph("graph.xml") edgeweights = graph.edge_properties["weight"] # Write some interesting statistics avgdeg, stddevdeg = gt.vertex_average(graph, "total") avgwt, stddevwt = gt.edge_average(graph, edgeweights) print "---- PLANNER DATA STATISTICS ----" print str(graph.num_vertices()) + " vertices and " + str(graph.num_edges()) + " edges" print "Average vertex degree (in+out) = " + str(avgdeg) + " St. Dev = " + str(stddevdeg) print "Average edge weight = " + str(avgwt) + " St. Dev = " + str(stddevwt) comps, hist = gt.label_components(graph) print "Strongly connected components: " + str(len(hist)) graph.set_directed(False) # Make the graph undirected (for weak components, and a simpler drawing) comps, hist = gt.label_components(graph) print "Weakly connected components: " + str(len(hist)) # Plotting the graph gt.remove_parallel_edges(graph) # Removing any superfluous edges edgeweights = graph.edge_properties["weight"] colorprops = graph.new_vertex_property("string") vertexsize = graph.new_vertex_property("double") start = -1 goal = -1 for v in range(graph.num_vertices()): # Color and size vertices by type: start, goal, other if (pd.isStartVertex(v)): start = v colorprops[graph.vertex(v)] = "cyan" vertexsize[graph.vertex(v)] = 10 elif (pd.isGoalVertex(v)): goal = v colorprops[graph.vertex(v)] = "green" vertexsize[graph.vertex(v)] = 10 else: colorprops[graph.vertex(v)] = "yellow" vertexsize[graph.vertex(v)] = 5 # default edge color is black with size 0.5: edgecolor = graph.new_edge_property("string") edgesize = graph.new_edge_property("double") for e in graph.edges(): edgecolor[e] = "black" edgesize[e] = 0.5 # using A* to find shortest path in planner data if start != -1 and goal != -1: dist, pred = gt.astar_search(graph, graph.vertex(start), edgeweights) # Color edges along shortest path red with size 3.0 v = graph.vertex(goal) while v != graph.vertex(start): p = graph.vertex(pred[v]) for e in p.out_edges(): if e.target() == v: edgecolor[e] = "red" edgesize[e] = 2.0 v = p # Writing graph to file: # pos indicates the desired vertex positions, and pin=True says that we # really REALLY want the vertices at those positions gt.graph_draw (graph, vertex_size=vertexsize, vertex_fill_color=colorprops, edge_pen_width=edgesize, edge_color=edgecolor, output="graph.png") print print 'Graph written to graph.png'
#!/usr/bin/python3 import os import statistics as stats import graph_tool.all as gt os.chdir("/home/jen/Documents/School/GradSchool/Thesis/Images/") g_link = gt.load_graph("Examples/ToyLinked.xml.gz") g_bran = gt.load_graph("Examples/ToyBranching.xml.gz") #Misc Stats link_deg_avg, link_deg_std = gt.vertex_average(g_link, deg="total") bran_deg_avg, bran_deg_std = gt.vertex_average(g_bran, deg="total") #Centrality vp_btwn_link, ep_btwn_link = gt.betweenness(g_link) link_btwn = [vp_btwn_link[v] for v in g_link.vertices()] vp_btwn_bran, ep_btwn_bran = gt.betweenness(g_bran) bran_btwn = [vp_btwn_bran[v] for v in g_bran.vertices()] link_btwn_avg = stats.mean(link_btwn) link_btwn_std = stats.stdev(link_btwn) bran_btwn_avg = stats.mean(bran_btwn) bran_btwn_std = stats.stdev(bran_btwn) #Cost and efficiency link_mst = gt.min_spanning_tree(g_link) bran_mst = gt.min_spanning_tree(g_bran) link_shortest = [x for vector in gt.shortest_distance(g_link) for x in vector]
pds.load(args.plannerdata, pd) pd.computeEdgeWeights() # Extract the graphml representation of the planner data graphml = pd.printGraphML() f = open("graph.graphml", 'w') f.write(graphml) f.close() # Load the graphml data using graph-tool graph = gt.load_graph("graph.graphml", fmt="xml") os.remove("graph.graphml") edgeweights = graph.edge_properties["weight"] # Write some interesting statistics avgdeg, stddevdeg = gt.vertex_average(graph, "total") avgwt, stddevwt = gt.edge_average(graph, edgeweights) print("---- PLANNER DATA STATISTICS ----") print( str(graph.num_vertices()) + " vertices and " + str(graph.num_edges()) + " edges") print("Average vertex degree (in+out) = " + str(avgdeg) + " St. Dev = " + str(stddevdeg)) print("Average edge weight = " + str(avgwt) + " St. Dev = " + str(stddevwt)) _, hist = gt.label_components(graph) print("Strongly connected components: " + str(len(hist))) # Make the graph undirected (for weak components, and a simpler drawing)