def build_minimum_tree(g, root, terminals, edges, directed=True): """remove redundant edges from `edges` so that root can reach each node in terminals """ # build the tree t = Graph(directed=directed) for _ in range(g.num_vertices()): t.add_vertex() for (u, v) in edges: t.add_edge(u, v) # mask out redundant edges vis = init_visitor(t, root) pbfs_search(t, source=root, terminals=list(terminals), visitor=vis) minimum_edges = { e for u in terminals for e in extract_edges_from_pred(t, root, u, vis.pred) } # print(minimum_edges) efilt = t.new_edge_property('bool') efilt.a = False for u, v in minimum_edges: efilt[u, v] = True t.set_edge_filter(efilt) return filter_nodes_by_edges(t, minimum_edges)
def query_graph_s1(): """query s1""" G = Graph(directed=True) v0 = G.add_vertex() v1 = G.add_vertex() v2 = G.add_vertex() v3 = G.add_vertex() v4 = G.add_vertex() v5 = G.add_vertex() v6 = G.add_vertex() v7 = G.add_vertex() v8 = G.add_vertex() v9 = G.add_vertex() G.add_edge(v0, v1) # e0, gr:includes G.add_edge(v2, v0) # e1, gr:offers G.add_edge(v0, v3) # e2, gr:price G.add_edge(v0, v4) # e3, gr:serial_number G.add_edge(v0, v5) # e4, gr:validFrom G.add_edge(v0, v6) # e5, gr:validThrough G.add_edge(v0, v7) # e6, sorg:eligible_Region G.add_edge(v0, v8) # e7, sorg:eligible_Region G.add_edge(v0, v9) # e8, gr:priceValidUntil return G
def burtFig4(directed=False): ''' Returns the graph presented at Burt's "Role Equivalence" paper, at Figure_x ''' g = Graph(directed=directed) g.add_vertex(9) g.add_edge(0, 4) g.add_edge(1, 4) g.add_edge(2, 4) g.add_edge(3, 4) g.add_edge(4, 5) g.add_edge(4, 6) # 4-clique g.add_edge(5, 6) g.add_edge(5, 7) g.add_edge(5, 8) g.add_edge(6, 7) g.add_edge(6, 8) g.add_edge(7, 8) if directed: g.add_edge(6, 5) g.add_edge(7, 5) g.add_edge(8, 5) g.add_edge(7, 6) g.add_edge(8, 6) g.add_edge(8, 7) for ed in g.edges(): print ed return g
def build_minimum_tree(g, root, terminals, edges, directed=True): """remove redundant edges from `edges` so that root can reach each node in terminals """ # build the tree t = Graph(directed=directed) for _ in range(g.num_vertices()): t.add_vertex() for (u, v) in edges: t.add_edge(u, v) # mask out redundant edges vis = init_visitor(t, root) pbfs_search(t, source=root, terminals=list(terminals), visitor=vis) minimum_edges = {e for u in terminals for e in extract_edges_from_pred(t, root, u, vis.pred)} # print(minimum_edges) efilt = t.new_edge_property('bool') efilt.a = False for u, v in minimum_edges: efilt[u, v] = True t.set_edge_filter(efilt) return filter_nodes_by_edges(t, minimum_edges)
def test_fill_missing_time(): """simple chain graph test """ g = Graph(directed=False) g.add_vertex(4) g.add_edge_list([(0, 1), (1, 2), (2, 3)]) t = GraphView(g, directed=True) efilt = t.new_edge_property('bool') efilt.a = True efilt[t.edge(2, 3)] = False t.set_edge_filter(efilt) vfilt = t.new_vertex_property('bool') vfilt.a = True vfilt[3] = False t.set_vertex_filter(vfilt) root = 0 obs_nodes = {0, 2} infection_times = [0, 1.5, 3, -1] pt = fill_missing_time(g, t, root, obs_nodes, infection_times, debug=False) for i in range(4): assert pt[i] == infection_times[i]
def gen_cascade(g, p, source=None, stop_fraction=0.5): if source is None: source = random.choice(np.arange(g.num_vertices())) infected = {source} infection_times = np.ones(g.num_vertices()) * -1 infection_times[source] = 0 time = 0 edges = [] while np.count_nonzero( infection_times != -1) / g.num_vertices() <= stop_fraction: infected_nodes_until_t = copy(infected) time += 1 for i in infected_nodes_until_t: for j in g.vertex(i).all_neighbours(): j = int(j) if j not in infected and random.random() <= p: infected.add(j) infection_times[j] = time edges.append((i, j)) tree = Graph(directed=True) for _ in range(g.num_vertices()): tree.add_vertex() for u, v in edges: tree.add_edge(u, v) return source, infection_times, tree
def balancedBinaryTree(h, drawGraph=False): ''' h - the height of the tree ''' g = Graph(directed=False) g.add_vertex(2**h - 1) for i in xrange(1, 2**(h - 1)): lc = 2 * i - 1 rc = lc + 1 g.add_edge(i - 1, lc) g.add_edge(i - 1, rc) hIndex = g.new_vertex_property( "int") #associate with each node the height at which it lies k = 2 m = 1 for i in xrange(1, len(hIndex.a)): hIndex.a[i] = m k -= 1 if k == 0: m += 1 k = 2**m g.vp['height'] = hIndex if drawGraph == True: draw.graph_draw(g, vertex_text=g.vertex_index, edge_color="black", output="binaryTree_h_" + str(h) + "_.pdf") return g
def query_graph_f4(): """query f4""" G = Graph(directed=True) v0 = G.add_vertex() v1 = G.add_vertex() v2 = G.add_vertex() v3 = G.add_vertex() v4 = G.add_vertex() v5 = G.add_vertex() v6 = G.add_vertex() v7 = G.add_vertex() v8 = G.add_vertex() v9 = G.add_vertex() G.add_edge(v0, v1) # e0, foaf:homepage G.add_edge(v2, v0) # e1, gr:includes G.add_edge(v0, v3) # e2, og:title G.add_edge(v0, v4) # e3, sorg:description G.add_edge(v0, v8) # e4, sorg:contentSize G.add_edge(v1, v5) # e5, sorg:url G.add_edge(v1, v6) # e6, wsdbm:hits G.add_edge(v7, v1) # e7, wsdbm:likes G.add_edge(v1, v9) # e8, sorg:language return G
def build_closure(g, terminals, debug=False, verbose=False): terminals = list(terminals) # build closure gc = Graph(directed=False) gc.add_vertex(g.num_vertices()) edges_with_weight = set() r2pred = {} for r in terminals: if debug: print('root {}'.format(r)) vis = init_visitor(g, r) pbfs_search(g, source=r, terminals=terminals, visitor=vis) new_edges = set(get_edges(vis.dist, r, terminals)) if debug: print('new edges {}'.format(new_edges)) edges_with_weight |= new_edges r2pred[r] = vis.pred for u, v, c in edges_with_weight: gc.add_edge(u, v) eweight = gc.new_edge_property('int') weights = np.array([c for _, _, c in edges_with_weight]) eweight.set_2d_array(weights) vfilt = gc.new_vertex_property('bool') vfilt.a = False for v in terminals: vfilt[v] = True gc.set_vertex_filter(vfilt) return gc, eweight, r2pred
def create_true_graph(self): final_workflow = Graph() final_workflow.add_vertex(self.network_size) i = 0 while max([ shortest_distance(final_workflow, x, (self.network_size - 1)) for x in final_workflow.get_vertices() ]) > 100: i += 1 if i > 10000: raise RuntimeError('generating graph took too long') valid_source_nodes = [ index for index, in_degree in enumerate( final_workflow.get_in_degrees( final_workflow.get_vertices())) if ((in_degree > 0 or index < self.input_nodes) and index < (self.network_size - 1)) ] valid_to_nodes = [ index for index in final_workflow.get_vertices() if (index >= self.input_nodes) ] new_edge = final_workflow.add_edge( self.np_random.choice(valid_source_nodes), self.np_random.choice(valid_to_nodes)) if not is_DAG(final_workflow): final_workflow.remove_edge(new_edge) observation = adjacency(final_workflow).toarray().astype(int) if not self.observation_space.contains(observation): final_workflow.remove_edge(new_edge) return final_workflow
def gen_cascade(g, p, source=None, stop_fraction=0.5): if source is None: source = random.choice(np.arange(g.num_vertices())) infected = {source} infection_times = np.ones(g.num_vertices()) * -1 infection_times[source] = 0 time = 0 edges = [] while np.count_nonzero(infection_times != -1) / g.num_vertices() <= stop_fraction: infected_nodes_until_t = copy(infected) time += 1 for i in infected_nodes_until_t: for j in g.vertex(i).all_neighbours(): j = int(j) if j not in infected and random.random() <= p: infected.add(j) infection_times[j] = time edges.append((i, j)) tree = Graph(directed=True) for _ in range(g.num_vertices()): tree.add_vertex() for u, v in edges: tree.add_edge(u, v) return source, infection_times, tree
def burtFig5(directed=False): ''' Returns the graph presented at Burt's "Role Equivalence" paper, at Figure_x ''' g = Graph(directed=directed) g.add_vertex(10) if directed: addSymmetricEdge(g, 0, 1) addSymmetricEdge(g, 1, 2) addSymmetricEdge(g, 2, 3) addSymmetricEdge(g, 4, 6) addSymmetricEdge(g, 6, 5) addSymmetricEdge(g, 7, 9) addSymmetricEdge(g, 9, 8) g.add_edge(4, 0) g.add_edge(5, 0) g.add_edge(7, 3) g.add_edge(8, 3) else: g.add_edge(0, 1) g.add_edge(1, 2) g.add_edge(2, 3) g.add_edge(4, 6) g.add_edge(6, 5) g.add_edge(7, 9) g.add_edge(9, 8) g.add_edge(4, 0) g.add_edge(5, 0) g.add_edge(7, 3) g.add_edge(8, 3) return g
def _filter_short_branch(self, filter=False, short=30): """ filter out very short branches: do this maybe not right for some models, for models with flat part, it is right I will test how this effect the final matching results need to delete nodes, switch with the last one then delete last """ if filter == False: self.verts = self.verts_init self.edges = self.edges_init else: init_graph = Graph(directed=False) init_graph.add_vertex(len(self.verts_init)) for edge in self.edges_init: init_graph.add_edge(init_graph.vertex(edge[0]), init_graph.vertex(edge[1])) terminal_node = [] for v in init_graph.vertices(): if v.out_degree() == 1: terminal_node.append(v) visitor = DepthVisitor() short_nodes = [] for tn in terminal_node: search.dfs_search(init_graph, tn, visitor) tmp_node = visitor.get_short_branch(min_length=short) visitor.reset() for n in tmp_node: short_nodes.append(n) ## get edges on the short paths short_nodes = list(set(short_nodes)) short_edges = [] temp_verts = self.verts_init[:] v_num = len(self.verts_init) if len(short_nodes): for v in reversed(sorted(short_nodes)): for ve in init_graph.vertex(v).out_edges(): short_edges.append(ve) ## delete edges first, then vertex short_edges = list(set(short_edges)) for e in short_edges: init_graph.remove_edge(e) print 'deleting vertex', for v in reversed(sorted(short_nodes)): print v, temp_verts[int(v)] = temp_verts[v_num-1] init_graph.remove_vertex(v, fast=True) v_num -= 1 print '\ndeleting related edges' # already done above, just info user else: print 'no short branches' ######## new vertices and edges ######## self.verts = temp_verts[:v_num] self.edges = [] for e in init_graph.edges(): self.edges.append([int(e.source()), int(e.target())])
def edges2graph(g, edges): tree = Graph(directed=True) for _ in range(g.num_vertices()): tree.add_vertex() for u, v in edges: tree.add_edge(int(u), int(v)) return filter_nodes_by_edges(tree, edges)
def steiner_tree_greedy( g, root, infection_times, source, obs_nodes, debug=False, verbose=True): # root = min(obs_nodes, key=infection_times.__getitem__) sorted_obs = list(sorted(obs_nodes, key=infection_times.__getitem__))[1:] tree_nodes = {root} tree_edges = set() for u in sorted_obs: # connect u to the tree vis = init_visitor(g, u) if debug: print('connect {} to tree'.format(u)) print('nodes connectable: {}'.format(tree_nodes)) forbidden_nodes = list(set(obs_nodes) - tree_nodes) cpbfs_search(g, u, visitor=vis, terminals=list(tree_nodes), forbidden_nodes=forbidden_nodes, count_threshold=1) # add edge reachable_nodes = set(np.nonzero(vis.dist > 0)[0]).intersection(tree_nodes) if debug: print('reachable_nodes: {}'.format(reachable_nodes)) assert len(reachable_nodes) > 0 sorted_ancestors = sorted(reachable_nodes, key=vis.dist.__getitem__) ancestor = sorted_ancestors[0] if debug: print('ancestor: {}'.format(ancestor)) print('dist to reachable: {}'.format(vis.dist[sorted_ancestors])) new_edges = extract_edges_from_pred(g, u, ancestor, vis.pred) new_edges = {(v, u) for u, v in new_edges} # needs to reverse the order if debug: print('new_edges: {}'.format(new_edges)) tree_edges |= set(new_edges) tree_nodes |= {v for e in new_edges for v in e} t = Graph(directed=True) for _ in range(g.num_vertices()): t.add_vertex() vfilt = t.new_vertex_property('bool') vfilt.a = False for v in tree_nodes: vfilt[t.vertex(v)] = True for u, v in tree_edges: t.add_edge(t.vertex(u), t.vertex(v)) t.set_vertex_filter(vfilt) return t
def build_cooc_graph(coDic, posMap): ''' Converts a dictionary keeping word-word co-occurrences to a graph object where the edge weight between nodes (words) corresponds to their co-occurrence. Args: coOcDic: A dictionary where keys are tuples with 2 elements, (string1, string2) corresponding to the co-occurrence of two words. The corresponding value is an integer capturing the times the co-occurrence happened (e.g., through out a book). posMap: A dictionary from word to Part Of Speech. Returns: A graph object. ''' g = Graph(directed=False) wordToNodeID = dict( ) #maps a word to the ID of the node that it will be stored eWeight = g.new_edge_property( "int") #edges have weights capturing number of co-occurrences words = g.new_vertex_property( "object" ) #keep for each node the (potentially unicode) corresponding word as an attribute POS = g.new_vertex_property("string") #keep the Part Of Speech nodeID = 0 for word1, word2 in coDic.keys( ): #Each key is a (noun, noun) string. It will become an edge if word1 not in wordToNodeID: wordToNodeID[word1] = nodeID v = g.add_vertex() assert (str(v) == str(nodeID)) words[v] = word1 POS[v] = posMap[word1] nodeID += 1 if word2 not in wordToNodeID: wordToNodeID[word2] = nodeID v = g.add_vertex() assert (str(v) == str(nodeID)) words[v] = word2 POS[v] = posMap[word2] nodeID += 1 source = wordToNodeID[word1] target = wordToNodeID[word2] e = g.add_edge(source, target) eWeight[e] = coDic[(word1, word2)] g.edge_properties["co-occurrence"] = eWeight g.vertex_properties["word"] = words g.vertex_properties["partOfSpeach"] = POS #Encode the POS as a short number POS_encoded = g.new_vertex_property("short") posEncoder = part_of_speech_int_map(posToInt=True) for v in g.vertices(): POS_encoded[v] = posEncoder[POS[v][0]] g.vertex_properties["partOfSpeach_encoded"] = POS_encoded return g
def build_closure(g, terminals, p=None, debug=False, verbose=False): """build the transitive closure on terminals""" def get_edges(dist, root, terminals): """get adjacent edges to root with weight""" return {(root, t, dist[t]) for t in terminals if dist[t] != -1 and t != root} terminals = list(terminals) gc = Graph(directed=False) gc.add_vertex(g.num_vertices()) edges_with_weight = set() r2pred = {} # root to predecessor map (from bfs) # shortest path to all other nodes for r in terminals: if debug: print('root {}'.format(r)) targets = list(set(terminals) - {r}) dist_map, pred_map = shortest_distance(g, source=r, target=targets, weights=p, pred_map=True) dist_map = dict(zip(targets, dist_map)) # print(dist_map) # print(pred_map) new_edges = get_edges(dist_map, r, targets) # if p is None: # vis = init_visitor(g, r) # bfs_search(g, source=r, visitor=vis) # new_edges = set(get_edges(vis.dist, r, terminals)) # else: # print('weighted graph') if debug: print('new edges {}'.format(new_edges)) edges_with_weight |= new_edges # r2pred[r] = vis.pred r2pred[r] = pred_map for u, v, c in edges_with_weight: gc.add_edge(u, v) # edge weights eweight = gc.new_edge_property('int') weights = np.array([c for _, _, c in edges_with_weight]) eweight.set_2d_array(weights) vfilt = gc.new_vertex_property('bool') vfilt.a = False for v in terminals: vfilt[v] = True gc.set_vertex_filter(vfilt) return gc, eweight, r2pred
def input_data_gt(): g_nx = nx.karate_club_graph() g = Graph(directed=True) g.add_vertex(g_nx.number_of_nodes()) for u, v in g_nx.edges(): g.add_edge(u, v) g.add_edge(v, u) # the other direction return g, from_gt(g, None), g.num_vertices()
def components_graphtool(): g = GTGraph() # make sure all the nodes are in the graph if min_len <= 1: g.add_vertex(node_count) g.add_edge_list(edges) component_labels = label_components(g, directed=False)[0].a components = grouping.group(component_labels, min_len=min_len) return components
def split_gt(): g = GTGraph() if not only_watertight: # same as above, for single triangles with no adjacency g.add_vertex(len(mesh.faces)) g.add_edge_list(adjacency) component_labels = label_components(g, directed=False)[0].a components = group(component_labels) result = mesh.submesh(components, only_watertight=only_watertight) return result
def import_carribean_food_web_graph(save=True, export=True): saveLoadFolder = "Carribean_FoodWeb" graphFile = saveLoadPath + saveLoadFolder + "/Carribean_Adjacency_Matrix_raw.txt" g = Graph(directed=False) edgeWeights = g.new_edge_property("double") counter = -1 with open(graphFile, "r") as inF: for line in inF: if line[0] == "#": # This line is a header. Skip it. continue if counter == -1: # First non header line revels all the species/categories. categories = line.split() num_nodes = int(len(categories)) print num_nodes counter += 1 g.add_vertex(num_nodes) continue splitted = line.split() category = splitted[0] assert (category == categories[counter]) for neighbor, weight in enumerate(splitted[1:]): if weight != "0": e = g.add_edge(g.vertex(neighbor), g.vertex(counter)) # Neighbor eats him. edgeWeights[e] = float(weight) counter += 1 taxaToInt = {"D": 0, "A": 1, "I": 2, "F": 3, "R": 4, "B": 5} troClass = g.new_vertex_property("int") for i, categ in enumerate(categories): troClass.a[i] = taxaToInt[categ[0]] g.vp["trophic_class"] = troClass g.ep["edge_weight"] = edgeWeights g = make_simple_graph(g, undirected=True, gcc=True) graphName = saveLoadFolder if save: save_data( saveLoadPath + saveLoadFolder + "/" + graphName + ".GT.graph", g) g.save(saveLoadPath + saveLoadFolder + "/" + graphName + ".graph.xml", fmt="xml") if export: from_GT_To_Greach( g, saveLoadPath + saveLoadFolder + "/" + graphName + ".greach.graph") from_GT_To_Snap( g, saveLoadPath + saveLoadFolder + "/" + graphName + ".snap.graph") return g
def graph(self): try: from graph_tool import Graph except ImportError: return None g = Graph() g.add_vertex(len(self.nodes)) g.add_edge_list([(e.src_id, e.dest_id) for e in self.edges]) return g
def query_graph_l4(): G = Graph(directed=True) v0 = G.add_vertex() v1 = G.add_vertex() v2 = G.add_vertex() G.add_edge(v0, v1) # e0, og:tag G.add_edge(v0, v2) # e1, sorg:caption return G
def starGraph(numOfVertices): ''' numOfVertices = Number of vertices including the center node of the star. dependencies: graph_tool ''' g = Graph(directed=False) centerNode = g.add_vertex() for i in xrange(2, numOfVertices + 1): node = g.add_vertex() g.add_edge(centerNode, node) return g
def query_graph_l3(): G = Graph(directed=True) v0 = G.add_vertex() v1 = G.add_vertex() v2 = G.add_vertex() G.add_edge(v0, v1) # e0, wsdbm:likes G.add_edge(v0, v2) # e1, wsdbm:subscribes return G
def tree1(): g = Graph(directed=True) g.add_vertex(5) # one remaining singleton g.add_edge_list([(0, 1), (1, 2), (1, 3)]) # to test 4 is not included vfilt = g.new_vertex_property('bool') vfilt.set_value(True) vfilt[4] = False g.set_vertex_filter(vfilt) return g
def erdos_renyi_graph(n, e, directed=False, gcc=True): g = Graph(directed=directed) g.add_vertex(n) rint = np.random.randint edge_list = [[x, y] for x, y in zip(rint(0, n, size=e), rint(0, n, size=e))] g.add_edge_list(edge_list) random_rewire(g, model="erdos") g = make_simple_graph(g, undirected=1 - directed, gcc=gcc) return g
def generateEmptyGraph(width, height): graph = Graph() graph.add_vertex(width * height) pos = graph.new_vertex_property("vector<double>") for x in range(0, width): for y in range(0, height): pos[graph.vertex((x * width) + (y))] = (x, y) graph.vertex_properties["position"] = pos return graph
def co_graph_directed(): '''co_graph_directed ''' g = Graph(directed=True) g.add_vertex(2) edges = [(0, 1), (1, 0), (0, 2), (2, 0), (1, 2), (2, 1)] g.add_edge_list(edges) o = g.new_vertex_property('int') o.a = np.array([3, 4, 2]) co = g.new_edge_property('int') co.a = np.array([2, 2, 1, 1, 2, 2]) return g, o, co
def query_graph_l5(): G = Graph(directed=True) v0 = G.add_vertex() v1 = G.add_vertex() v2 = G.add_vertex() v3 = G.add_vertex() G.add_edge(v0, v1) # e0, sorg:jobTitle G.add_edge(v0, v3) # e1, sorg:nationality G.add_edge(v2, v3) # e2, gn:parentCountry return G
def query_graph_l1(): G = Graph(directed=True) v0 = G.add_vertex() v1 = G.add_vertex() v2 = G.add_vertex() v3 = G.add_vertex() G.add_edge(v0, v1) # e0 G.add_edge(v0, v2) # e1 G.add_edge(v2, v3) # e2 return G
def query_graph_l2(): G = Graph(directed=True) v0 = G.add_vertex() v1 = G.add_vertex() v2 = G.add_vertex() v3 = G.add_vertex() G.add_edge(v2, v3) # e0, wsdbm:likes G.add_edge(v2, v1) # e1, sorg:nationality G.add_edge(v0, v1) # e2, gn:parentCountry, switched directions return G
def gen_er(dicProperties): np.random.seed() # initialize graph graphER = Graph() nNodes = 0 nEdges = 0 rDens = 0.0 if "Nodes" in dicProperties.keys(): nNodes = dicProperties["Nodes"] graphER.add_vertex(nNodes) if "Edges" in dicProperties.keys(): nEdges = dicProperties["Edges"] rDens = nEdges / float(nNodes**2) dicProperties["Density"] = rDens else: rDens = dicProperties["Density"] nEdges = int(np.floor(rDens*nNodes**2)) dicProperties["Edges"] = nEdges else: nEdges = dicProperties["Edges"] rDens = dicProperties["Density"] nNodes = int(np.floor(np.sqrt(nEdges/rDens))) graphER.add_vertex(nNodes) dicProperties["Nodes"] = nNodes # generate edges numTest,numCurrentEdges = 0,0 while numCurrentEdges != nEdges and numTest < n_MAXTESTS: lstEdges = np.random.randint(0,nNodes,(nEdges-numCurrentEdges,2)) graphER.add_edge_list(lstEdges) # remove loops and duplicate edges remove_self_loops(graphER) remove_parallel_edges(graphER) numCurrentEdges = graphER.num_edges() numTest += 1 graphER.reindex_edges() nEdges = graphER.num_edges() rDens = nEdges / float(nNodes**2) # generate types rInhibFrac = dicProperties["InhibFrac"] lstTypesGen = np.random.uniform(0,1,nEdges) lstTypeLimit = np.full(nEdges,rInhibFrac) lstIsExcitatory = np.greater(lstTypesGen,lstTypeLimit) nExc = np.count_nonzero(lstIsExcitatory) epropType = graphER.new_edge_property("int",np.multiply(2,lstIsExcitatory)-np.repeat(1,nEdges)) # excitatory (True) or inhibitory (False) graphER.edge_properties["type"] = epropType # and weights if dicProperties["Weighted"]: lstWeights = dicGenWeights[dicProperties["Distribution"]](graphER,dicProperties,nEdges,nExc) # generate the weights epropW = graphER.new_edge_property("double",lstWeights) # crée la propriété pour stocker les poids graphER.edge_properties["weight"] = epropW return graphER
def query_graph_s7(): """query s7""" G = Graph(directed=True) v0 = G.add_vertex() v1 = G.add_vertex() v2 = G.add_vertex() v3 = G.add_vertex() G.add_edge(v0, v1) # e0, rdf:type G.add_edge(v0, v2) # e1, sorg:text G.add_edge(v3, v0) # e2, wsdbm:likes return G
def query_graph_s6(): """query s6""" G = Graph(directed=True) v0 = G.add_vertex() v1 = G.add_vertex() v2 = G.add_vertex() v3 = G.add_vertex() G.add_edge(v0, v1) # e0, mo:conductor G.add_edge(v0, v2) # e1, rdf:type G.add_edge(v0, v3) # e2, wsdbm:hasGenre return G
def to_directed(g, t, root): new_t = Graph(directed=True) all_edges = set() leaves = [v for v in t.vertices() if (v.out_degree() + v.in_degree()) == 1 and t != root] for target in leaves: path = shortest_path(t, source=root, target=target)[0] edges = set(zip(path[:-1], path[1:])) all_edges |= edges for _ in range(g.num_vertices()): new_t.add_vertex() for u, v in all_edges: new_t.add_edge(int(u), int(v)) return new_t
def test_sredni_wspolczynnik_klasteryzacji_na_sztywno_graf_pelny(self): # self.assertEqual(7. / 15, self.stat.sredni_wspolczynnik_klasteryzacji_moj()) # print self.stat.sredni_wspolczynnik_klasteryzacji_moj() g = Graph(directed=False) v0 = g.add_vertex() v1 = g.add_vertex() v2 = g.add_vertex() v3 = g.add_vertex() g.add_edge(v0, v1) g.add_edge(v0, v2) g.add_edge(v0, v3) g.add_edge(v1, v2) g.add_edge(v1, v3) g.add_edge(v2, v3) lc = local_clustering(g, undirected=True) self.assertEqual(1.0, vertex_average(g, lc)[0])
def ring(num_vtx=100, k=2, p=0.0): g = Graph(directed=False) vtx = list(g.add_vertex(num_vtx)) # connect neighbors for i in vtx: for j in xrange(1, k + 1): dest = g.vertex((g.vertex_index[i] - j) % num_vtx) if g.edge(i, dest) is None: g.add_edge(i, dest) # redirect edges # old_edges = list(g.edges()) old_edges = [(x.source(), x.target()) for x in g.edges()] for i in old_edges: n = random.random() if n < p: # redirect edge; choose random vertex as new destination vtx_tmp = vtx[:] vtx_tmp.remove(i[1]) if i[0] in vtx_tmp: vtx_tmp.remove(i[0]) dest = random.choice(vtx_tmp) while g.edge(i[0], dest) is not None: vtx_tmp.remove(dest) dest = random.choice(vtx_tmp) g.remove_edge(g.edge(i[0], i[1])) g.add_edge(i[0], dest) return g
class RoadMap(object): def __init__(self, mapfile): self._mapfile = mapfile self.DIRECTION_index = 6 self.PATHCLASS_index = 20 self.g = Graph() self.g.edge_properties["length"] = self.g.new_edge_property("double") self.g.edge_properties["level"] = self.g.new_edge_property("int") self.g.vertex_properties["pos"] = self.g.new_vertex_property("vector<double>") self.cross_pos_index = {} def load(self): if self._mapfile[-3:] != 'shp': self.g = load_graph(self._mapfile) return try: sf = shapefile.Reader(self._mapfile) except Exception as e: print(str(e)) return False roads_records = sf.shapeRecords() # 获取路段信息' for road_record in roads_records: cross_s_index = self.add_cross(road_record.shape.points[0]) cross_e_index = self.add_cross(road_record.shape.points[-1]) self.add_road_edge(cross_s_index, cross_e_index, road_record) if int(road_record.record[self.DIRECTION_index]) == 0: # 若路段是双向车道 self.add_road_edge(cross_e_index, cross_s_index, road_record) return True def has_edge(self, s_vertex, e_vertex): if self.g.num_vertices() >= max(s_vertex, e_vertex): return self.g.edge(s_vertex, e_vertex) else: return None def add_cross(self, cross_pos): if cross_pos in self.cross_pos_index: return self.cross_pos_index.get(cross_pos) else: cross_index = self.g.add_vertex() self.g.vp.pos[cross_index] = cross_pos self.cross_pos_index[cross_pos] = cross_index return cross_index def add_road_edge(self, s_vertex, e_vertex, road): if self.has_edge(s_vertex, e_vertex): return self.g.edge(s_vertex, e_vertex) else: edge = self.g.add_edge(s_vertex, e_vertex) self.g.ep.level[edge] = int(road.record[self.PATHCLASS_index]) self.g.ep.length[edge] = self.road_length(road) return edge @staticmethod def road_length(road): length = 0 for sub_road in zip(road.shape.points[:-1], road.shape.points[1:]): length += distance.euclidean(sub_road[0], sub_road[1]) return length
def edges_to_directed_tree(g, root, edges): t = Graph(directed=False) for _ in range(g.num_vertices()): t.add_vertex() for u, v in edges: t.add_edge(u, v) vis = EdgeCollectorVisitor() bfs_search(t, source=root, visitor=vis) t.clear_edges() t.set_directed(True) for u, v in vis.edges: t.add_edge(u, v) return filter_nodes_by_edges(t, edges)
def compose_graph(uid_pid_pairs): # set up graph g = Graph() g.vp['pid'] = v_pid_p = g.new_vertex_property('string') g.vp['count'] = v_count_p = g.new_vertex_property('int') g.ep['count'] = e_count_p = g.new_edge_property('int') pid_v_map = {} uid_last_v_map = {} vv_e_map = {} for uid, pid in uid_pid_pairs: # vertex v = pid_v_map.get(pid) if v is None: v = g.add_vertex() v_pid_p[v] = pid v_count_p[v] = 0 pid_v_map[pid] = v v_count_p[v] += 1 # edge last_v = uid_last_v_map.get(uid) uid_last_v_map[uid] = v if last_v is None: continue vv = (last_v, v) e = vv_e_map.get(vv) if e is None: e = g.add_edge(*vv) e_count_p[e] = 0 vv_e_map[vv] = e e_count_p[e] += 1 # calculate closeness g.vp['closeness'] = v_closeness_p = g.new_vertex_property('float') e_inverse_count_p = g.new_edge_property('int') e_inverse_count_p.a = e_count_p.a.max()-e_count_p.a debug('e_inverse_count_p.a: {}', e_inverse_count_p.a) closeness(g, weight=e_inverse_count_p, vprop=v_closeness_p) debug('v_closeness_p.a : {}', v_closeness_p.a) v_closeness_p.a = nan_to_num(v_closeness_p.a) debug('v_closeness_p.a : {}', v_closeness_p.a) # fillter g.vp['picked'] = v_picked_p = g.new_vertex_property('bool') debug('v_count_p.a.mean() : {}', v_count_p.a.mean()) v_picked_p.a = v_count_p.a > v_count_p.a.mean() debug('v_picked_p.a : {}', v_picked_p.a) g.set_vertex_filter(v_picked_p) g.set_vertex_filter(None) return g
def graph_from_dataframes(vertex_df, edge_df): '''Re-creates a Graph object with PropertyMaps taken from the vertex_df and edge_df DataFrames Paramters: ========== verex_df: a DataFrame with an index named 'vertex_index' edge_df: a DataFrame with a multi-index named ('source', 'target') Returns: ======== graph: a grah-tool Graph with PropertyMaps copied from the columns of the input DataFrames ''' graph = Graph(directed=True) vertex_index = vertex_df.index.get_level_values(level='vertex_index') vertices = graph.add_vertex(n=vertex_index.shape[0]) for col in vertex_df.columns: in_type = vertex_df[col].dtype.name try: dtype = ALIASES[in_type] except KeyError: log.info('Data type {} not supported'.format(in_type)) continue prop = graph.new_vertex_property(dtype) prop.fa = vertex_df[col] graph.vertex_properties[col] = prop src = edge_df.index.names.index('source') trgt = edge_df.index.names.index('target') ### TODO: use the list edge creation for tup in edge_df.index: source, target = tup[src], tup[trgt] try: edge = graph.add_edge(source, target) except ValueError: log.info('Invalid vertex in (source: {}, target: {})'.format(source, target)) for col in edge_df.columns: in_type = edge_df[col].dtype.name try: dtype = ALIASES[in_type] except KeyError: log.info('Data type {} not supported'.format(in_type)) continue prop = graph.new_edge_property(dtype) prop.fa = edge_df[col] graph.edge_properties[col] = prop return graph
def components_graphtool(): """ Find connected components using graphtool """ g = GTGraph() # make sure all the nodes are in the graph g.add_vertex(node_count) # add the edge list g.add_edge_list(edges) labels = np.array(label_components(g, directed=False)[0].a, dtype=np.int64)[:node_count] # we have to remove results that contain nodes outside # of the specified node set and reindex contained = np.zeros(node_count, dtype=np.bool) contained[nodes] = True index = np.arange(node_count, dtype=np.int64)[contained] components = grouping.group(labels[contained], min_len=min_len) components = np.array([index[c] for c in components]) return components
def build_closure(g, terminals, debug=False, verbose=False): terminals = list(terminals) # build closure gc = Graph(directed=False) for _ in range(g.num_vertices()): gc.add_vertex() edges_with_weight = set() r2pred = {} for r in terminals: if debug: print('root {}'.format(r)) vis = init_visitor(g, r) pbfs_search(g, source=r, terminals=terminals, visitor=vis) new_edges = set(get_edges(vis.dist, r, terminals)) if debug: print('new edges {}'.format(new_edges)) edges_with_weight |= new_edges r2pred[r] = vis.pred for u, v, c in edges_with_weight: gc.add_edge(u, v) eweight = gc.new_edge_property('int') weights = np.array([c for _, _, c in edges_with_weight]) eweight.set_2d_array(weights) vfilt = gc.new_vertex_property('bool') vfilt.a = False for v in terminals: vfilt[v] = True gc.set_vertex_filter(vfilt) return gc, eweight, r2pred
def load_graph(infile): inmatrix = np.loadtxt(infile, dtype=np.dtype('uint32'), delimiter=" ") numv = np.amax(inmatrix[:,0:2]) #print numv, inmatrix[:,0:2] g = Graph(directed=False) edge_weights = g.new_edge_property("double") g.edge_properties["weights"] = edge_weights vlist = list(g.add_vertex(numv)) for i in inmatrix: edge = g.add_edge(vlist[i[0]-1], vlist[i[1]-1]) # need to convert from 1-based index in file to 0-based edge_weights[edge] = i[2] remove_parallel_edges(g) return g
def graph_from_dataframes(vertex_df, edge_df): '''Re-creates a Graph object with PropertyMaps taken from the vertex_df and edge_df DataFrames Paramters: ========== verex_df: a DataFrame with an index named 'vertex_index' edge_df: a DataFrame with a multi-index named ('source', 'target') Returns: ======== graph: a grah-tool Graph with PropertyMaps copied from the columns of the input DataFrames ''' graph = Graph(directed=True) vertex_index = vertex_df.index.get_level_values(level='vertex_index') vertices = graph.add_vertex(n=vertex_index.shape[0]) for col in vertex_df.columns: dtype = ALIASES[vertex_df[col].dtype.name] prop = graph.new_vertex_property(dtype) prop.a = vertex_df[col] graph.vertex_properties[col] = prop src = edge_df.index.names.index('source') trgt = edge_df.index.names.index('target') ### TODO: use the list edge creation for tup in edge_df.index: source, target = tup[src], tup[trgt] edge = graph.add_edge(source, target) for col in edge_df.columns: dtype = ALIASES[edge_df[col].dtype.name] prop = graph.new_edge_property(dtype) prop.a = edge_df[col] graph.edge_properties[col] = prop return graph
def parse_graph_from_string(self, graphML_string): dom = minidom.parseString(graphML_string) root = dom.getElementsByTagName("graphml")[0] graph = root.getElementsByTagName("graph")[0] name = graph.getAttribute('id') g = Graph(directed=False) vpos=g.new_vertex_property("vector<double>") for node in graph.getElementsByTagName("node"): id=node.getAttribute('id') n = g.add_vertex() g.vertex_index[id] #right now only the positions are available for attr in node.getElementsByTagName("data"): if attr.firstChild: key=attr.getAttribute("key") #n[key] = attr.firstChild.data if(key=="x"): x=attr.firstChild.data elif(key=="y"): y=attr.firstChild.data vpos[id]=(x,y) g.vertex_properties["pos"]=vpos #have to workaround the directed graph written by the server for edge in graph.getElementsByTagName("edge"): source = edge.getAttribute('source') dest = edge.getAttribute('target') edge=g.edge(dest,source) if(edge==None): e = g.add_edge(source, dest) return g
def gen_fs(dicProperties): np.random.seed() graphFS = Graph() # on définit la fraction des arcs à utiliser la réciprocité f = dicProperties["Reciprocity"] rFracRecip = f/(2.0-f) # on définit toutes les grandeurs de base rInDeg = dicProperties["InDeg"] rOutDeg = dicProperties["OutDeg"] nNodes = 0 nEdges = 0 rDens = 0.0 if "Nodes" in dicProperties.keys(): nNodes = dicProperties["Nodes"] graphFS.add_vertex(nNodes) if "Edges" in dicProperties.keys(): nEdges = dicProperties["Edges"] rDens = nEdges / float(nNodes**2) dicProperties["Density"] = rDens else: rDens = dicProperties["Density"] nEdges = int(np.floor(rDens*nNodes**2)) dicProperties["Edges"] = nEdges else: nEdges = dicProperties["Edges"] rDens = dicProperties["Density"] nNodes = int(np.floor(np.sqrt(nEdges/rDens))) graphFS.add_vertex(nNodes) dicProperties["Nodes"] = nNodes # on définit le nombre d'arcs à créer nArcs = int(np.floor(rDens*nNodes**2)/(1+rFracRecip)) # on définit les paramètres fonctions de probabilité associées F(x) = A x^{-tau} Ai = nArcs*(rInDeg-1)/(nNodes) Ao = nArcs*(rOutDeg-1)/(nNodes) # on définit les moyennes des distributions de pareto 2 = lomax rMi = 1/(rInDeg-2.) rMo = 1/(rOutDeg-2.) # on définit les trois listes contenant les degrés sortant/entrant/bidirectionnels associés aux noeuds i in range(nNodes) lstInDeg = np.random.pareto(rInDeg,nNodes)+1 lstOutDeg = np.random.pareto(rOutDeg,nNodes)+1 lstInDeg = np.floor(np.multiply(Ai/np.mean(lstInDeg), lstInDeg)).astype(int) lstOutDeg = np.floor(np.multiply(Ao/np.mean(lstOutDeg), lstOutDeg)).astype(int) # on génère les stubs qui vont être nécessaires et on les compte nInStubs = int(np.sum(lstInDeg)) nOutStubs = int(np.sum(lstOutDeg)) lstInStubs = np.zeros(np.sum(lstInDeg)) lstOutStubs = np.zeros(np.sum(lstOutDeg)) nStartIn = 0 nStartOut = 0 for vert in range(nNodes): nInDegVert = lstInDeg[vert] nOutDegVert = lstOutDeg[vert] for j in range(np.max([nInDegVert,nOutDegVert])): if j < nInDegVert: lstInStubs[nStartIn+j] += vert if j < nOutDegVert: lstOutStubs[nStartOut+j] += vert nStartOut+=nOutDegVert nStartIn+=nInDegVert # on vérifie qu'on a à peu près le nombre voulu d'edges while nInStubs*(1+rFracRecip)/float(nArcs) < 0.95 : vert = np.random.randint(0,nNodes) nAddInStubs = int(np.floor(Ai/rMi*(np.random.pareto(rInDeg)+1))) lstInStubs = np.append(lstInStubs,np.repeat(vert,nAddInStubs)).astype(int) nInStubs+=nAddInStubs while nOutStubs*(1+rFracRecip)/float(nArcs) < 0.95 : nAddOutStubs = int(np.floor(Ao/rMo*(np.random.pareto(rOutDeg)+1))) lstOutStubs = np.append(lstOutStubs,np.repeat(vert,nAddOutStubs)).astype(int) nOutStubs+=nAddOutStubs # on s'assure d'avoir le même nombre de in et out stubs (1.13 is an experimental correction) nMaxStubs = int(1.13*(2.0*nArcs)/(2*(1+rFracRecip))) if nInStubs > nMaxStubs and nOutStubs > nMaxStubs: np.random.shuffle(lstInStubs) np.random.shuffle(lstOutStubs) lstOutStubs.resize(nMaxStubs) lstInStubs.resize(nMaxStubs) nOutStubs = nInStubs = nMaxStubs elif nInStubs < nOutStubs: np.random.shuffle(lstOutStubs) lstOutStubs.resize(nInStubs) nOutStubs = nInStubs else: np.random.shuffle(lstInStubs) lstInStubs.resize(nOutStubs) nInStubs = nOutStubs # on crée le graphe, les noeuds et les stubs nRecip = int(np.floor(nInStubs*rFracRecip)) nEdges = nInStubs + nRecip +1 # les stubs réciproques np.random.shuffle(lstInStubs) np.random.shuffle(lstOutStubs) lstInRecip = lstInStubs[0:nRecip] lstOutRecip = lstOutStubs[0:nRecip] lstEdges = np.array([np.concatenate((lstOutStubs,lstInRecip)),np.concatenate((lstInStubs,lstOutRecip))]).astype(int) # add edges graphFS.add_edge_list(np.transpose(lstEdges)) remove_self_loops(graphFS) remove_parallel_edges(graphFS) lstIsolatedVert = find_vertex(graphFS, graphFS.degree_property_map("total"), 0) graphFS.remove_vertex(lstIsolatedVert) graphFS.reindex_edges() nNodes = graphFS.num_vertices() nEdges = graphFS.num_edges() rDens = nEdges / float(nNodes**2) # generate types rInhibFrac = dicProperties["InhibFrac"] lstTypesGen = np.random.uniform(0,1,nEdges) lstTypeLimit = np.full(nEdges,rInhibFrac) lstIsExcitatory = np.greater(lstTypesGen,lstTypeLimit) nExc = np.count_nonzero(lstIsExcitatory) epropType = graphFS.new_edge_property("int",np.multiply(2,lstIsExcitatory)-np.repeat(1,nEdges)) # excitatory (True) or inhibitory (False) graphFS.edge_properties["type"] = epropType # and weights if dicProperties["Weighted"]: lstWeights = dicGenWeights[dicProperties["Distribution"]](graphFS,dicProperties,nEdges,nExc) # generate the weights epropW = graphFS.new_edge_property("double",lstWeights) # crée la propriété pour stocker les poids graphFS.edge_properties["weight"] = epropW return graphFS
class BoardGraphGraphtool(BoardGraphBase): def __init__(self, number_of_vertices, graph_type): super().__init__(number_of_vertices, graph_type) # Graph tool creates directed multigraph by default. self._graph = Graph() self._graph.add_vertex(number_of_vertices) self._graph.vertex_properties["cell"] = self._graph.new_vertex_property( "object", number_of_vertices * [BoardCell()] ) self._graph.edge_properties["direction" ] = self._graph.new_edge_property("object") self._graph.edge_properties["weight" ] = self._graph.new_edge_property("int") def __getitem__(self, position): return self._graph.vp.cell[self._graph.vertex(position)] def __setitem__(self, position, board_cell): self._graph.vp.cell[self._graph.vertex(position)] = board_cell def __contains__(self, position): return position in range(0, self.vertices_count()) def vertices_count(self): return self._graph.num_vertices() def edges_count(self): return self._graph.num_edges() def has_edge(self, source_vertice, target_vertice, direction): for e in self._graph.vertex(source_vertice).out_edges(): if ( int(e.target()) == target_vertice and self._graph.ep.direction[e] == direction ): return True return False def out_edges_count(self, source_vertice, target_vertice): return len([ 1 for e in self._graph.vertex(source_vertice).out_edges() if int(e.target()) == target_vertice ]) def reconfigure_edges(self, width, height, tessellation): """ Uses tessellation object to create all edges in graph. """ self._graph.clear_edges() for source_vertice in self._graph.vertices(): for direction in tessellation.legal_directions: neighbor_vertice = tessellation.neighbor_position( int(source_vertice), direction, board_width=width, board_height=height ) if neighbor_vertice is not None: e = self._graph.add_edge( source_vertice, neighbor_vertice, add_missing=False ) self._graph.ep.direction[e] = direction # TODO: Faster version? # def reconfigure_edges(self, width, height, tessellation): # """ # Uses tessellation object to create all edges in graph. # """ # self._graph.clear_edges() # edges_to_add = [] # directions_to_add = dict() # for source_vertice in self._graph.vertices(): # for direction in tessellation.legal_directions: # neighbor_vertice = tessellation.neighbor_position( # int(source_vertice), direction, # board_width=width, board_height=height # ) # if neighbor_vertice is not None: # edge = (int(source_vertice), neighbor_vertice,) # edges_to_add.append(edge) # if edge not in directions_to_add: # directions_to_add[edge] = deque() # directions_to_add[edge].append(direction) # self._graph.add_edge_list(edges_to_add) if edges_to_add else None # for e in edges_to_add: # e_descriptors = self._graph.edge( # s = self._graph.vertex(e[0]), # t = self._graph.vertex(e[1]), # all_edges = True # ) # for e_descriptor in e_descriptors: # if len(directions_to_add[e]) > 0: # self._graph.ep.direction[e_descriptor] = directions_to_add[e][0] # directions_to_add[e].popleft() def calculate_edge_weights(self): for e in self._graph.edges(): self._graph.ep.weight[e] = self.out_edge_weight(int(e.target())) def neighbor(self, from_position, direction): try: for e in self._graph.vertex(from_position).out_edges(): if self._graph.ep.direction[e] == direction: return int(e.target()) except ValueError as e: raise IndexError(e.args) return None def wall_neighbors(self, from_position): return [ int(n) for n in self._graph.vertex(from_position).out_neighbours() if self[int(n)].is_wall ] def all_neighbors(self, from_position): return [ int(n) for n in self._graph.vertex(from_position).out_neighbours() ] def shortest_path(self, start_position, end_position): try: return [ int(v) for v in shortest_path( g=self._graph, source=self._graph.vertex(start_position), target=self._graph.vertex(end_position), )[0] ] except ValueError: return [] def dijkstra_path(self, start_position, end_position): try: self.calculate_edge_weights() return [ int(v) for v in shortest_path( g=self._graph, source=self._graph.vertex(start_position), target=self._graph.vertex(end_position), weights=self._graph.ep.weight, )[0] ] except ValueError: return [] def position_path_to_direction_path(self, position_path): retv = [] src_vertice_index = 0 for target_vertice in position_path[1:]: source_vertice = position_path[src_vertice_index] src_vertice_index += 1 for out_edge in self._graph.vertex(source_vertice).out_edges(): if int(out_edge.target()) == target_vertice: retv.append(self._graph.ep.direction[out_edge]) return { 'source_position': position_path[0] if position_path else None, 'path': retv }
class SkeletonMatch(object): """ implement Oscar's skeleton matching alogrithm in this class """ def __init__(self, skel1, skel2, centricity=.5, length=.5, distorted=20.): if skel1 is not None and skel2 is not None : self.skel1 = skel1 self.skel2 = skel2 self.centricity_threhold = centricity self.length_threhold = length self.distorted_threhold = distorted self.skel1.calc_skel_properties() self.skel2.calc_skel_properties() # use index instead of real value skel1_index = np.arange(len(self.skel1.feature_node_index)) skel2_index = np.arange(len(self.skel2.feature_node_index)) junc1_num = len(skel1.junction_index) junc2_num = len(skel2.junction_index) #print 'skel1 normalized_verts\n', skel1.normalized_feature_verts #print 'skel2 normalized_verts\n', skel2.normalized_feature_verts #candidate matched pairs junction_pairs = [] junc_term_pairs = [] terminal_pairs = [] for i, j in itertools.product(skel1_index, skel2_index): if self.test_node_centricity(c1=i, c2=j): if i < junc1_num and j < junc2_num: # only junction nodes junction_pairs.append([i,j]) elif i >= junc1_num and j >= junc2_num: # only terminal nodes terminal_pairs.append([i,j]) else: junc_term_pairs.append([i,j]) self.junction_pairs = np.array(junction_pairs) self.terminal_pairs = np.array(terminal_pairs) self.junc_term_pairs = np.array(junc_term_pairs) #self.all_junc_pairs = np.vstack((self.junction_pairs, self.junc_term_pairs)) self.vote_tree = Graph(directed=False) self.node_pair = self.vote_tree.new_vertex_property("vector<short>") self._construct_voting_tree() else: print 'need input two skeleton to match' def _construct_voting_tree(self, prev_pairs=np.array([]), mix_junc_term=True): """ recursively consturct voting tree @param prev_pairs record that already on the path @param junc_pairs record that left part junction pairs (on current tree level) @param term_pairs record that left terminal pairs on current tree level now limits: at least one junction pair """ # root of the tree if len(prev_pairs) == 0: v1 = self.vote_tree.add_vertex() #first level, only junction pairs (both are junction) for n, pair in enumerate(self.junction_pairs): new_prev = pair.reshape(-1,2) # to use len(for level one), need to change shape print 'adding subtree', n+1, '/', len(self.junction_pairs) v2 = self._construct_voting_tree(prev_pairs=new_prev) self.vote_tree.add_edge(v1, v2) return v1 # return the root elif len(prev_pairs) == 1: # first level v1 = self.vote_tree.add_vertex() self.node_pair[v1] = prev_pairs.flatten() """ priority order: junction pairs, terminal pairs, junc-term pairs """ check_junc = True #prepare for next(second) level for n, pair in enumerate(self.junction_pairs): if pair[0] not in prev_pairs[:,0] and pair[1] not in prev_pairs[:,1]: new_prev = np.vstack((prev_pairs, pair)) v2 = self._construct_voting_tree(prev_pairs=new_prev) if v2 is not None: check_junc = False self.vote_tree.add_edge(v1, v2) # it is sure that that should be some terminal_pairs # but in case check_term = mix_junc_term # if True allow mix junc and term if check_junc: for n, pair in enumerate(self.terminal_pairs): new_prev = np.vstack((prev_pairs, pair)) v2 = self._construct_voting_tree(prev_pairs=new_prev, mix_junc_term=True) if v2 is not None: check_term = False self.vote_tree.add_edge(v1, v2) if check_junc and check_term: for n, pair in enumerate(self.junc_term_pairs): new_prev = np.vstack((prev_pairs, pair)) v2 = self._construct_voting_tree(prev_pairs=new_prev) if v2 is not None: self.vote_tree.add_edge(v1, v2) return v1 # return the first level of the tree elif 4 > len(prev_pairs) > 1: # above level two #if satisfy T2 (length and radius prune) if self.test_length_radius(n1=prev_pairs[-1,0], n2=prev_pairs[-1,1], matched_pairs=prev_pairs[:-1]) and self.test_topology_consistency(n1=prev_pairs[-1,0], n2=prev_pairs[-1,1], matched_pairs=prev_pairs[:-1]): v1 = self.vote_tree.add_vertex() self.node_pair[v1] = prev_pairs.flatten() check_junc = True for pair in self.junction_pairs: if pair[0] not in prev_pairs[:,0] and pair[1] not in prev_pairs[:,1]: new_prev = np.vstack((prev_pairs, pair)) v2 = self._construct_voting_tree(prev_pairs=new_prev) if v2 is not None: check_junc = False self.vote_tree.add_edge(v1, v2) check_term = mix_junc_term # if allow mix junc and term if check_junc: for pair in self.terminal_pairs: if pair[0] not in prev_pairs[:,0] and pair[1] not in prev_pairs[:,1]: new_prev = np.vstack((prev_pairs, pair)) v2 = self._construct_voting_tree(prev_pairs=new_prev, mix_junc_term=True) if v2 is not None: check_term = False self.vote_tree.add_edge(v1, v2) if check_junc and check_term: for pair in self.junc_term_pairs: if pair[0] not in prev_pairs[:,0] and pair[1] not in prev_pairs[:,1]: new_prev = np.vstack((prev_pairs, pair)) v2 = self._construct_voting_tree(prev_pairs=new_prev) if v2 is not None: self.vote_tree.add_edge(v1, v2) return v1 # return second and above level tree else: return None # fail to match elif len(prev_pairs) >= 4: if self.test_length_radius(n1=prev_pairs[-1,0], n2=prev_pairs[-1,1], matched_pairs=prev_pairs[:-1]) and self.test_topology_consistency(n1=prev_pairs[-1,0], n2=prev_pairs[-1,1], matched_pairs=prev_pairs[:-1]): #print 'len(prev_pairs) >= 4', #print 'current pairs\n', prev_pairs, if self.test_spatial_configuration(n1=prev_pairs[-1,0], n2=prev_pairs[-1,1], matched_pairs=prev_pairs[:-1]): v1 = self.vote_tree.add_vertex() self.node_pair[v1] = prev_pairs.flatten() #print 'succeed testing spatial', '[', prev_pairs[-1,0], prev_pairs[-1,1], ']', #print 'from\n', prev_pairs[:-1] #print 'current matched pairs\n', prev_pairs check_junc = True for pair in self.junction_pairs: if pair[0] not in prev_pairs[:,0] and pair[1] not in prev_pairs[:,1]: new_prev = np.vstack((prev_pairs, pair)) v2 = self._construct_voting_tree(prev_pairs=new_prev) if v2 is not None: check_junc = False self.vote_tree.add_edge(v1, v2) check_term = mix_junc_term # if True allow mix junction and terminal if check_junc: for pair in self.terminal_pairs: if pair[0] not in prev_pairs[:,0] and pair[1] not in prev_pairs[:,1]: new_prev = np.vstack((prev_pairs, pair)) v2 = self._construct_voting_tree(prev_pairs=new_prev) if v2 is not None: check_term = False self.vote_tree.add_edge(v1, v2) if check_junc and check_term: for pair in self.junc_term_pairs: if pair[0] not in prev_pairs[:,0] and pair[1] not in prev_pairs[:,1]: new_prev = np.vstack((prev_pairs, pair)) v2 = self._construct_voting_tree(prev_pairs=new_prev) if v2 is not None: self.vote_tree.add_edge(v1, v2) return v1 else: return None else: return None def test_node_centricity(self, c1, c2): """ match node centricity of the graph """ node_cent1 = self.skel1.node_centricity[c1] node_cent2 = self.skel2.node_centricity[c2] match_result = abs(node_cent1 - node_cent2) / (node_cent1 + node_cent2) threhold = self.centricity_threhold * .5 return match_result < threhold def test_length_radius(self, n1, n2, matched_pairs): """ match path length and radius from n1/n2 to nodes that already in matched_pairs """ path_len1 = self.skel1.path_length_ratio[n1, matched_pairs[:,0]] path_len2 = self.skel2.path_length_ratio[n2, matched_pairs[:,1]] length_match = abs(path_len1 - path_len2) / (path_len1 + path_len2) threhold = self.length_threhold * 0.5 #if all satisfied if np.all(length_match < threhold): path_rad1 = self.skel1.path_radius_ratio[n1, matched_pairs[:,0]] path_rad2 = self.skel2.path_radius_ratio[n2, matched_pairs[:,1]] radius_match = abs(path_rad1 - path_rad2) / (path_rad1 + path_rad2) if np.all(radius_match < threhold): return True else: return False else: return False def test_topology_consistency(self, n1, n2, matched_pairs): """ match skeleton topology consistency """ if len(matched_pairs) > 0: junct1 = matched_pairs[matched_pairs[:,0] < len(self.skel1.junction_index), 0] junct2 = matched_pairs[matched_pairs[:,1] < len(self.skel2.junction_index), 1] if len(junct1) < 1 or len(junct2) < 1: print 'no junction node in already matched pairs' return False else: idx1 = np.argmin(self.skel1.path_to_junction[n1, junct1]) idx2 = np.argmin(self.skel2.path_to_junction[n2, junct2]) """ print '\n[',n1,',',n2,']', print 'nearest pair[',junct1[idx1],',', junct2[idx2],']', if [junct1[idx1], junct2[idx2]] in matched_pairs.tolist(): print ' IN ', else: print ' NOT in ', for pair in matched_pairs: print pair, print '\n' """ return [junct1[idx1], junct2[idx2]] in matched_pairs.tolist() else: print 'none in matched_pairs' return False def test_spatial_configuration(self, n1, n2, matched_pairs): """ match spatial configuration """ threhold = self.distorted_threhold #need test if can be inverse skel1_vectors = self.skel1.normalized_feature_verts[matched_pairs[-3:,0]] - self.skel1.normalized_feature_verts[n1] skel2_vectors = self.skel2.normalized_feature_verts[matched_pairs[-3:,1]] - self.skel2.normalized_feature_verts[n2] #skel1_vectors = self.skel1.feature_node[matched_pairs[-3:,0]] - self.skel1.feature_node[n1] #skel2_vectors = self.skel2.feature_node[matched_pairs[-3:,1]] - self.skel2.feature_node[n2] """ for i in xrange(3): skel1_vectors[i] *= ( 1. / np.linalg.norm(skel1_vectors[i]) ) skel2_vectors[i] *= ( 1. / np.linalg.norm(skel2_vectors[i]) ) """ a = np.dot(skel2_vectors, np.linalg.inv(skel1_vectors)) u, s, v = np.linalg.svd(a) r = np.dot(u, v) if np.linalg.det(r) < 0: r *= -1.0 res1 = np.linalg.norm(a-r) #print 'res1', res1, if res1 > threhold: return False else: a = np.dot(skel1_vectors, np.linalg.inv(skel2_vectors)) u, s, v = np.linalg.svd(a) r = np.dot(u, v) if np.linalg.det(r) < 0: r *= -1.0 res2 = np.linalg.norm(a-r) if res2 > threhold: return False #print 'res2', res2 #return max(res1, res2) <= threhold return True def elector_vote(self): """ use elector vote to find better correspondence """ vote_matrix = np.zeros((len(self.skel1.feature_node_index), len(self.skel2.feature_node_index)), dtype=int) for v in self.vote_tree.vertices(): if v.out_degree() < 2: pairs = self.node_pair[v] if len(pairs) >= 8: temp_pairs = pairs.a.reshape(-1,2) for pair in temp_pairs: vote_matrix[pair[0], pair[1]] += 1 node_num_skel1 = len(self.skel1.feature_node_index) node_num_skel2 = len(self.skel2.feature_node_index) self.vote_matrix = vote_matrix.copy() #print 'original vote_matrix\n', vote_matrix if np.max(vote_matrix) == 0: final_corres = np.array([]) else: node_pair = np.unravel_index(vote_matrix.argmax(), vote_matrix.shape) vote_matrix[node_pair[0], :] = vote_matrix[:, node_pair[1]] = 0 final_corres = np.array(node_pair) final_corres.shape = (-1,2) #print 'correspondence\n', final_corres #print 'vote_matrix\n', vote_matrix while len(final_corres) < min(node_num_skel1, node_num_skel2): junct1 = final_corres[final_corres[:,0] < len(self.skel1.junction_index), 0] junct2 = final_corres[final_corres[:,1] < len(self.skel2.junction_index), 1] node_pair = np.unravel_index(vote_matrix.argmax(), vote_matrix.shape) if node_pair[0] not in final_corres[:,0] and node_pair[1] not in final_corres[:,1]: if len(junct1) < 3 or len(junct2) < 3: print 'less than 3 junction matched' final_corres = np.vstack((final_corres, node_pair)) vote_matrix[node_pair[0], :] = vote_matrix[:, node_pair[1]] = 0 else: idx1 = np.argmin(self.skel1.path_to_junction[node_pair[0], junct1]) idx2 = np.argmin(self.skel2.path_to_junction[node_pair[1], junct2]) if [junct1[idx1], junct2[idx2]] in final_corres.tolist(): final_corres = np.vstack((final_corres, node_pair)) vote_matrix[node_pair[0], :] = vote_matrix[:, node_pair[1]] = 0 #print 'added correspondence\n', final_corres #print 'vote_matrix\n', vote_matrix else: vote_matrix[node_pair[0], node_pair[1]] = 0 else: vote_matrix[node_pair[0], node_pair[1]] = 0 if np.all(vote_matrix == 0): break self.final_corres = final_corres
from main import number_requests #number_requests = 10 all_requests_with_primary_paths = {} all_requests_with_alternative_paths = {} all_requests_with_primary_frequency_channels = {} all_requests_with_alternative_frequency_channels = {} output = open(str(number_frequency_bands) + "_Channels_" + str(simulated_on_network) + "_Network_Blockings_log_Dijkstra.txt","a") if simulated_on_network == 1: ## Network is USIP ## ## Creating the USIP network ## g = Graph(directed=False) vertices_set = g.add_vertex(24) ''' e01 = g.add_edge(g.vertex_index[0], g.vertex_index[1]) e02 = g.add_edge(g.vertex_index[0], g.vertex_index[2]) e12 = g.add_edge(g.vertex_index[1], g.vertex_index[2]) e13 = g.add_edge(g.vertex_index[1], g.vertex_index[3]) e24 = g.add_edge(g.vertex_index[2], g.vertex_index[4]) e34 = g.add_edge(g.vertex_index[3], g.vertex_index[4]) e35 = g.add_edge(g.vertex_index[3], g.vertex_index[5]) e45 = g.add_edge(g.vertex_index[4], g.vertex_index[5]) ''' e01 = g.add_edge(g.vertex_index[0], g.vertex_index[1]) e05 = g.add_edge(g.vertex_index[0], g.vertex_index[5]) e12 = g.add_edge(g.vertex_index[1], g.vertex_index[2]) e15 = g.add_edge(g.vertex_index[1], g.vertex_index[5]) e23 = g.add_edge(g.vertex_index[2], g.vertex_index[3])
def makeGraphFast(self,img,dia,xScale,yScale): print('Building Graph Data Structure'), start=time.time() G = Graph(directed=False) sumAddVertices=0 vprop=G.new_vertex_property('object') eprop=G.new_edge_property('object') epropW=G.new_edge_property("float") h, w = np.shape(img) if xScale>0 and yScale>0: avgScale=(xScale+yScale)/2 else: avgScale=1. xScale=1. yScale=1. addedVerticesLine2=[] vListLine2=[] percentOld=0 counter=0 ''' Sweep over each line in the image except the last line ''' for idx,i in enumerate(img[:len(img)-2]): ''' Get foreground indices in the current line of the image and make vertices ''' counter+=1 percent=(float(counter)/float(h))*100 if percentOld+10< percent: print (str(np.round(percent,1))+'% '), percentOld=percent line1=np.where(i==True) if len(line1[0])>0: line1=set(line1[0]).difference(set(addedVerticesLine2)) vL=G.add_vertex(len(list(line1))) if len(line1)>1 : vList=vListLine2+list(vL) else: vList=vListLine2+[vL] line1=addedVerticesLine2+list(line1) for jdx,j in enumerate(line1): vprop[vList[jdx]]={'imgIdx':(j,idx),'coord': (float(j)*xScale,float(idx)*yScale), 'nrOfPaths':0, 'diameter':float(dia[idx][j])*avgScale} ''' keep order of the inserted vertices ''' sumAddVertices+=len(line1) addedVerticesLine2=[] vListLine2=[] ''' Connect foreground indices to neighbours in the next line ''' for v1 in line1: va=vList[line1.index(v1)] diagonalLeft = diagonalRight = True try: if img[idx][v1-1]==True: diagonalLeft=False vb=vList[line1.index(v1-1)] e=G.add_edge(va,vb) eprop[e]={'coord1':vprop[va]['coord'], 'coord2':vprop[vb]['coord'],'weight':((vprop[va]['diameter']+vprop[vb]['diameter'])/2),'RTP':False} epropW[e]=2./(eprop[e]['weight']**2) except: print 'Boundary vertex at: '+str([v1,idx-1])+' image size: '+ str([w,h]) pass try: if img[idx][v1+1]==True: diagonalRight=False vb=vList[line1.index(v1+1)] e=G.add_edge(va,vb) eprop[e]={'coord1':vprop[va]['coord'], 'coord2':vprop[vb]['coord'],'weight':((vprop[va]['diameter']+vprop[vb]['diameter'])/2),'RTP':False} epropW[e]=2./(eprop[e]['weight']**2) except: print 'Boundary vertex at: '+str([v1+1,idx])+' image size: '+ str([w,h]) pass # just if we are out of bounds try: if img[idx+1][v1]==True: diagonalRight=False diagonalLeft=False vNew=G.add_vertex() vprop[vNew]={'imgIdx':(v1,idx+1),'coord': (float(v1)*xScale,float(idx+1)*yScale), 'nrOfPaths':0, 'diameter':float(dia[idx+1][v1])*avgScale} vListLine2.append(vNew) e=G.add_edge(vList[line1.index(v1)],vNew) eprop[e]={'coord1':vprop[va]['coord'], 'coord2':vprop[vNew]['coord'],'weight':((vprop[va]['diameter']+vprop[vNew]['diameter'])/2),'RTP':False} epropW[e]=1./(eprop[e]['weight']**2) if v1 not in addedVerticesLine2: addedVerticesLine2.append(v1) except: print 'Boundary vertex at: '+str([v1,idx+1])+' image size: '+ str([w,h]) pass try: if diagonalRight == True and img[idx+1][v1+1]==True: vNew=G.add_vertex() vprop[vNew]={'imgIdx':(v1+1,idx+1),'coord': (float(v1+1)*xScale,float(idx+1)*yScale), 'nrOfPaths':0, 'diameter':float(dia[idx+1][v1+1])*avgScale} vListLine2.append(vNew) e=G.add_edge(vList[line1.index(v1)],vNew) eprop[e]={'coord1':vprop[va]['coord'], 'coord2':vprop[vNew]['coord'],'weight':((vprop[va]['diameter']+vprop[vNew]['diameter'])/2),'RTP':False} epropW[e]=1.41/(eprop[e]['weight']**2) if v1+1 not in addedVerticesLine2: addedVerticesLine2.append(v1+1) except: print 'Boundary vertex at: '+str([v1+1,idx+1])+' image size: '+ str([w,h]) pass try: if diagonalLeft == True and img[idx+1][v1-1]==True: vNew=G.add_vertex() vprop[vNew]={'imgIdx':(v1-1,idx+1),'coord': (float(v1-1)*xScale,float(idx+1)*yScale), 'nrOfPaths':0, 'diameter':float(dia[idx+1][v1-1])*avgScale} vListLine2.append(vNew) e=G.add_edge(vList[line1.index(v1)],vNew) eprop[e]={'coord1':vprop[va]['coord'], 'coord2':vprop[vNew]['coord'],'weight':((vprop[va]['diameter']+vprop[vNew]['diameter'])/2),'RTP':False} epropW[e]=1.41/(eprop[e]['weight']**2) if v1-1 not in addedVerticesLine2: addedVerticesLine2.append(v1-1) except: print 'Boundary vertex at: '+str([v1-1,idx+1])+' image size: '+ str([w,h]) pass try: if img[idx][v1+1]==False and img[idx][v1-1]==False and img[idx+1][v1]==False and diagonalLeft==False and diagonalRight==False: print 'tip detected' if img[idx-1][v1-1]==False and img[idx-1][v1+1]==False and img[idx-1][v1]==False: print 'floating pixel' except: pass print'done!' G.edge_properties["ep"] = eprop G.edge_properties["w"] = epropW G.vertex_properties["vp"] = vprop print 'graph build in '+str(time.time()-start) l = gt.label_largest_component(G) u = gt.GraphView(G, vfilt=l) print '# vertices' print(u.num_vertices()) print(G.num_vertices()) if u.num_vertices()!=G.num_vertices(): self.__fail=float((G.num_vertices()-u.num_vertices()))/float(G.num_vertices()) return u,u.num_vertices()
from Stack import Stack from AllConstants import * from main import number_requests #number_requests = 10 all_child_graphs = [] output = open(str(number_frequency_bands) + "_Channels_" + str(simulated_on_network) + "_Network_Blockings_log_BK.txt","a") if simulated_on_network == 1: ## Network is USIP ## ########### Creating as many layered graphs as there are channels ########### for i in range(number_frequency_bands): child_graph = Graph(); vertices_set = child_graph.add_vertex(24) ''' e01 = child_graph.add_edge(child_graph.vertex_index[0], child_graph.vertex_index[1]) e02 = child_graph.add_edge(child_graph.vertex_index[0], child_graph.vertex_index[2]) e12 = child_graph.add_edge(child_graph.vertex_index[1], child_graph.vertex_index[2]) e13 = child_graph.add_edge(child_graph.vertex_index[1], child_graph.vertex_index[3]) e24 = child_graph.add_edge(child_graph.vertex_index[2], child_graph.vertex_index[4]) e34 = child_graph.add_edge(child_graph.vertex_index[3], child_graph.vertex_index[4]) e35 = child_graph.add_edge(child_graph.vertex_index[3], child_graph.vertex_index[5]) e45 = child_graph.add_edge(child_graph.vertex_index[4], child_graph.vertex_index[5]) e10 = child_graph.add_edge(child_graph.vertex_index[1], child_graph.vertex_index[0]) e20 = child_graph.add_edge(child_graph.vertex_index[2], child_graph.vertex_index[0]) e21 = child_graph.add_edge(child_graph.vertex_index[2], child_graph.vertex_index[1]) e31 = child_graph.add_edge(child_graph.vertex_index[3], child_graph.vertex_index[1]) e42 = child_graph.add_edge(child_graph.vertex_index[4], child_graph.vertex_index[2])
def main(): conn = serial_interface.connect() cur_track = track.init_tracka() g = Graph() g.add_vertex(len(cur_track)) for (vi, node) in enumerate(cur_track): node.i = vi n_title = g.new_vertex_property("string") n_color = g.new_vertex_property("string") n_pos = g.new_vertex_property("vector<double>") e_title = g.new_edge_property("string") e_dist = g.new_edge_property("double") for node in cur_track: v = g.vertex(node.i) n_title[v] = node.name if node.typ == track.NODE_EXIT: # Invert points to match our ASCII display. n_pos[v] = (-node.reverse.coord_x, -node.reverse.coord_y) else: n_pos[v] = (-node.coord_x, -node.coord_y) e = g.add_edge(g.vertex(node.i), g.vertex(node.reverse.i)) if node.typ == track.NODE_SENSOR: n_color[v] = "blue" elif node.typ == track.NODE_BRANCH: n_color[v] = "orange" elif node.typ == track.NODE_MERGE: n_color[v] = "yellow" elif node.typ == track.NODE_ENTER: n_color[v] = "green" elif node.typ == track.NODE_EXIT: n_color[v] = "red" else: n_color[v] = "white" for edge in node.edge: if edge.src is None: continue e = g.add_edge(g.vertex(edge.src.i), g.vertex(edge.dest.i)) e_dist[e] = edge.dist e_title[e] = "%.2f" % (edge.dist) win = graph_tool.draw.GraphWindow(g, n_pos, (640, 480), edge_text=e_title, vertex_fill_color=n_color, vertex_text=n_title) win.show_all() def destroy_callback(*args, **kwargs): win.destroy() Gtk.main_quit() def set_switch(sw, d): for node in cur_track: if node.typ == track.NODE_BRANCH and node.num == sw: node.switch_direction = d return print "WARN: Could not find switch %d" % sw class Train(): num = -1 vel = 0. speed = 0. edge = cur_track[0].edge[0] edge_dist = 0 SPEEDX = 1. def __init__(self, num): self.num = num def update(self): # Super shitty deacceleration model self.vel = self.vel + (0.018/self.SPEEDX)*(self.speed - self.vel) self.edge_dist += self.vel while True: e = self.e() if self.edge_dist < e_dist[e]: break if self.edge.dest.typ == track.NODE_SENSOR: conn.set_sensor_tripped(self.edge.dest.num) self.edge = self.edge.dest.edge[self.edge.dest.switch_direction] self.edge_dist -= e_dist[e] def draw(self, n_pos, da, cr): e = self.e() start, end = np.array(n_pos[e.source()]), np.array(n_pos[e.target()]) alpha = self.edge_dist / e_dist[e] pos = start + alpha*(end - start) dp = win.graph.pos_to_device(pos) # dp: device position cr.rectangle(dp[0]-10, dp[1]-10, 20, 20) cr.set_source_rgb(102. / 256, 102. / 256, 102. / 256) cr.fill() cr.move_to(dp[0]-10, dp[1] + 10 - 12./2) cr.set_source_rgb(1., 1., 1.) cr.set_font_size(12) cr.show_text("%d" % self.num) cr.fill() def e(self): return g.edge(self.edge.src.i, self.edge.dest.i) def set_speed(self, speed): self.speed = speed/self.SPEEDX def toggle_reverse(self): self.edge = self.edge.reverse self.edge_dist = e_dist[self.e()] - self.edge_dist def find_train(train_number): for train in trains: if train.num == train_number: return train train = Train(train_number) trains.append(train) return train trains = [Train(12)] startup_time = time.time() accumulated_error = [0.] last_time = [time.time()] last_sensor_poll = [0] FPS = 30. def my_draw(da, cr): (typ, a1, a2) = conn.next_cmd() if typ is None: pass elif typ == 'set_speed': find_train(a1).set_speed(a2) elif typ == 'toggle_reverse': find_train(a1).toggle_reverse() elif typ == 'switch': set_switch(a1, a2) elif typ == 'sensor': last_sensor_poll[0] = round((time.time() - startup_time) * 1000)/1000 else: print "Ignoring command %s" % typ cur_time = time.time() dt = cur_time - last_time[0] + accumulated_error[0] num_steps = int(dt*FPS) accumulated_error[0] = dt - num_steps/FPS for train in trains: for _ in range(0, num_steps): train.update() train.draw(n_pos, da, cr) cr.move_to(10., 10.) cr.set_source_rgb(0., 0., 0.) cr.set_font_size(12) cr.show_text("Last polled at %.3f" % last_sensor_poll[0]) da.queue_draw() last_time[0] = cur_time win.connect("delete_event", destroy_callback) win.graph.connect("draw", my_draw) Gtk.main()
import graph_tool.all as gt from random import randint from Stack import Stack from AllConstants import * all_child_graphs = [] output = open("Blockings_log_BK.txt","a") if simulated_on_network == 1: ## Network is USIP ## ########### Creating as many layered graphs as there are channels ########### for i in range(number_frequency_bands): child_graph = Graph(); vertices_set = child_graph.add_vertex(24) e01 = child_graph.add_edge(child_graph.vertex_index[0], child_graph.vertex_index[1]) e05 = child_graph.add_edge(child_graph.vertex_index[0], child_graph.vertex_index[5]) e12 = child_graph.add_edge(child_graph.vertex_index[1], child_graph.vertex_index[2]) e15 = child_graph.add_edge(child_graph.vertex_index[1], child_graph.vertex_index[5]) e23 = child_graph.add_edge(child_graph.vertex_index[2], child_graph.vertex_index[3]) e24 = child_graph.add_edge(child_graph.vertex_index[2], child_graph.vertex_index[4]) e26 = child_graph.add_edge(child_graph.vertex_index[2], child_graph.vertex_index[6]) e36 = child_graph.add_edge(child_graph.vertex_index[3], child_graph.vertex_index[6]) e34 = child_graph.add_edge(child_graph.vertex_index[3], child_graph.vertex_index[4]) e47 = child_graph.add_edge(child_graph.vertex_index[4], child_graph.vertex_index[7]) e56 = child_graph.add_edge(child_graph.vertex_index[5], child_graph.vertex_index[6]) e58 = child_graph.add_edge(child_graph.vertex_index[5], child_graph.vertex_index[8]) e510 = child_graph.add_edge(child_graph.vertex_index[5], child_graph.vertex_index[10]) e67 = child_graph.add_edge(child_graph.vertex_index[6], child_graph.vertex_index[7]) e68 = child_graph.add_edge(child_graph.vertex_index[6], child_graph.vertex_index[8])
class SkeletonData(object): """ class to store and process skeleton data, like generated from starlab mean curvature skeleton """ def __init__(self, fname=None, mesh_name=None, filter_sb=False): """ @param filter_sb: if filter out Short Branch """ if fname != None: self.skel_name = fname self.read_skel_file(fname) self._filter_short_branch(filter=filter_sb, short=10) self._parse_data() self.mesh_name = mesh_name self.vert_radius = None def calc_skel_properties(self): """ calc all properties needed for matching """ self.calc_node_centricity() self.calc_skel_radius() self.calc_path_length_ratio() self.calc_path_radius_ratio() self.normalize_skeleton() def read_skel_file(self, fname, dim=3): if fname == None: print 'please input skeleton file name' sys.exit(0) elif os.path.isfile(fname): self.verts_init = [] self.edges_init = [] with open(fname) as sf: for line in sf: line = line.strip('\n') line = line.split(' ') if line[0] == '#': continue elif line[0] == 'v': self.verts_init.append([x for x in line[1:(dim+1)]]) #### attention!! verts of edge start from 1 in files #### elif line[0] == 'e': self.edges_init.append([int(x)-1 for x in line[1:3]]) else: print 'not support this format' sys.exit(0) else: print 'no such flie', fname sys.exit(0) def _filter_short_branch(self, filter=False, short=30): """ filter out very short branches: do this maybe not right for some models, for models with flat part, it is right I will test how this effect the final matching results need to delete nodes, switch with the last one then delete last """ if filter == False: self.verts = self.verts_init self.edges = self.edges_init else: init_graph = Graph(directed=False) init_graph.add_vertex(len(self.verts_init)) for edge in self.edges_init: init_graph.add_edge(init_graph.vertex(edge[0]), init_graph.vertex(edge[1])) terminal_node = [] for v in init_graph.vertices(): if v.out_degree() == 1: terminal_node.append(v) visitor = DepthVisitor() short_nodes = [] for tn in terminal_node: search.dfs_search(init_graph, tn, visitor) tmp_node = visitor.get_short_branch(min_length=short) visitor.reset() for n in tmp_node: short_nodes.append(n) ## get edges on the short paths short_nodes = list(set(short_nodes)) short_edges = [] temp_verts = self.verts_init[:] v_num = len(self.verts_init) if len(short_nodes): for v in reversed(sorted(short_nodes)): for ve in init_graph.vertex(v).out_edges(): short_edges.append(ve) ## delete edges first, then vertex short_edges = list(set(short_edges)) for e in short_edges: init_graph.remove_edge(e) print 'deleting vertex', for v in reversed(sorted(short_nodes)): print v, temp_verts[int(v)] = temp_verts[v_num-1] init_graph.remove_vertex(v, fast=True) v_num -= 1 print '\ndeleting related edges' # already done above, just info user else: print 'no short branches' ######## new vertices and edges ######## self.verts = temp_verts[:v_num] self.edges = [] for e in init_graph.edges(): self.edges.append([int(e.source()), int(e.target())]) def create_virtual_node(self): """ I am planning use this function to make virtual nodes for those feature nodes """ pass def _parse_data(self): """ extract interal points(degree>2) and endpoints(degree=1) extract segments """ if self.verts == None or self.edges == None: print 'please first call read_skel_file function' else: self.verts = np.array(self.verts, dtype=np.float) self.edges = np.array(self.edges, dtype=np.int) terminal_index = [] junction_index = [] self.skel_graph = Graph(directed=False) self.skel_graph.add_vertex(len(self.verts)) for edge in self.edges : self.skel_graph.add_edge(self.skel_graph.vertex(edge[0]), self.skel_graph.vertex(edge[1])) for v in self.skel_graph.vertices(): if v.out_degree() == 2 : continue elif v.out_degree() == 1 : terminal_index.append(int(v)) elif v.out_degree() > 2 : junction_index.append(int(v)) self.terminal = self.verts[terminal_index] self.junction = self.verts[junction_index] self.terminal_index = terminal_index self.junction_index = junction_index self.feature_node_index = junction_index + terminal_index self.feature_node = self.verts[self.feature_node_index] """ edge_vert_index = self.edges.flatten() print 'edge vertex index dtype', edge_vert_index.dtype if 0 in edge_vert_index: print 'vertex start from 0' else: print 'vertex start from 1' print 'skeleton vertex num', self.skel_graph.num_vertices() print 'skeleton edge num', self.skel_graph.num_edges() """ def _calc_edge_length(self): """ calc edge length and make it edge property map in graph-tool """ vec = self.verts[self.edges[:,0]] - self.verts[self.edges[:,1]] edge_length = np.sqrt(np.sum(vec**2, axis=-1)) self.edge_length_map = self.skel_graph.new_edge_property("double") self.edge_length_map.a = edge_length def calc_node_centricity(self): """ calc node centricity of feature nodes(terminal and junction nodes) T1 in Oscar's EG 2010 paper """ self._calc_edge_length() node_centricity = [] for n_idx in self.feature_node_index: dist = topology.shortest_distance(self.skel_graph, source=self.skel_graph.vertex(n_idx), weights=self.edge_length_map) node_centricity.append(dist.a.mean()) node_centricity = np.array(node_centricity) self.node_centricity = node_centricity / np.max(node_centricity) def calc_skel_radius(self, mesh_name=None, dim=3): """ calc nearest mesh vertex of skeleton vertex """ if mesh_name != None: self.mesh_name = mesh_name if self.mesh_name == None: print 'please set mesh_name before calc_skel_radius' elif os.path.isfile(self.mesh_name): mesh = om.TriMesh() assert om.read_mesh(mesh, self.mesh_name) mesh_vertices = np.zeros((mesh.n_vertices(), dim), dtype=float) for n, vh in enumerate(mesh.vertices()): for i in xrange(3): mesh_vertices[n, i] = mesh.point(vh)[i] nbrs = NearestNeighbors(n_neighbors=1, algorithm='ball_tree').fit(mesh_vertices) self.vert_radius, indices = nbrs.kneighbors(self.verts) else: print 'cannot find mesh file', self.mesh_name sys.exit(0) def calc_path_radius(self, start, end): """ utile function for other function calc skeleton **mean** vertex radius along some segment """ if self.vert_radius == None: print 'please call calc_skel_radius function first' return None elif start in self.feature_node_index and end in self.feature_node_index: v_list, e_list = topology.shortest_path(self.skel_graph, self.skel_graph.vertex(start), self.skel_graph.vertex(end), weights=self.edge_length_map) v_idx_list = [] for v in v_list: v_idx_list.append(int(v)) v_radius = self.vert_radius[v_idx_list] return v_radius.mean() else: print 'input vertex index is not feature node index' return None def calc_path_length_ratio(self): """ for each feature node pair segment, calculate path length ratio normalized, to make it scale invariant """ path_length = np.zeros((len(self.feature_node_index), len(self.feature_node_index)), dtype=float) for i, n_idx in enumerate(self.feature_node_index): for j, m_idx in enumerate(self.feature_node_index[i+1:], start=i+1): length = topology.shortest_distance(self.skel_graph, self.skel_graph.vertex(n_idx), self.skel_graph.vertex(m_idx), weights=self.edge_length_map) if length != None : path_length[i,j] = path_length[j,i] = length else: print 'compute path length ratio error' return None ### extract path length from each feature node to junction nodes ### ### Careful!! path_length MUST start from junction node self.path_to_junction = path_length[:,:len(self.junction_index)] self.path_length_ratio = path_length / path_length.max() return self.path_length_ratio def calc_path_radius_ratio(self): """ for each feature node pair segment, calculate path radius ratio normalized, to make it scale invariant """ path_radius = np.zeros((len(self.feature_node_index), len(self.feature_node_index)), dtype=float) for i, n_idx in enumerate(self.feature_node_index): for j, m_idx in enumerate(self.feature_node_index[i+1:], start=i+1): radius = self.calc_path_radius(n_idx, m_idx) if radius != None : path_radius[i, j] = path_radius[j, i] = radius else: print 'comptue path radius error' return None self.path_radius_ratio = path_radius / path_radius.max() return self.path_radius_ratio def normalize_skeleton(self): """ calc the pose-normalized skeleton to distinguish symmetric nodes using multidimensional scaling method(MDS) """ v_num = len(self.verts) geodesic_dist = np.zeros((v_num, v_num)) geodesic_dist_map = topology.shortest_distance(self.skel_graph, weights=self.edge_length_map) for i in xrange(v_num): geodesic_dist[i] = geodesic_dist_map[self.skel_graph.vertex(i)].a mds = manifold.MDS(n_components=3, max_iter=500, eps=1e-10, dissimilarity="precomputed", n_jobs=-2, n_init=1) verts_mean = self.verts - self.verts.mean(axis=0) normalized_verts = mds.fit(geodesic_dist, init=verts_mean).embedding_ #scale = np.sqrt((verts_mean ** 2).sum()) / np.sqrt((normalized_verts ** 2).sum()) #normalized_verts *= scale self.normalized_verts = normalized_verts self.normalized_feature_verts = normalized_verts[self.feature_node_index] return self.normalized_verts def write_file(self, file_path='./'): """ maybe need to save file after filter same as starlab mean curvature skeleton """ file_name = os.path.basename(self.skel_name) full_name = file_path + file_name v_num = len(self.verts) e_num = len(self.edges) first_line = '# D:3 ' + 'NV:' + str(v_num) + ' NE:' + str(e_num) + '\n' with open(full_name, 'w') as f: f.write(first_line) for v in self.verts: line = 'v ' + str(v[0]) + ' ' + str(v[1]) + ' ' + str(v[2]) + '\n' f.write(line) for e in self.edges: line = 'e ' + str(e[0]+1) + ' ' + str(e[1]+1) + '\n' f.write(line)
# graph-tool.skewed.de/ from graph_tool import Graph from graph_tool import draw g = Graph(directed=True) for i in range(5): g.add_vertex() v1 = g.add_vertex() v2 = g.add_vertex() v3 = g.vertex(2) e1 = g.add_edge(v1, v2) g.add_edge(v1, v3) draw.graph_draw(g)
def build_truncated_closure(g, cand_source, terminals, infection_times, k=-1, debug=False, verbose=False, **kawrgs): """ build a clojure graph in which cand_source + terminals are all connected to each other. the number of neighbors of each node is determined by k the larger the k, the denser the graph""" r2pred = {} edges = {} terminals = list(terminals) # from cand_source to terminals vis = init_visitor(g, cand_source) cpbfs_search(g, source=cand_source, visitor=vis, terminals=terminals, forbidden_nodes=terminals, count_threshold=-1) # k=-1 here because root connects to all other nodes r2pred[cand_source] = vis.pred for u, v, c in get_edges(vis.dist, cand_source, terminals): edges[(u, v)] = c if debug: print('cand_source: {}'.format(cand_source)) print('#terminals: {}'.format(len(terminals))) print('edges from cand_source: {}'.format(edges)) if verbose: terminals_iter = tqdm(terminals) print('building closure graph') else: terminals_iter = terminals # from terminal to other terminals # every temrinal should connetct to at least one earlier terminal # in this way, connectivity is ensured for root in terminals_iter: if root == cand_source: continue # connect from some earlier node to root # if it's earliest, can only connect to peers early_terminals = [t for t in terminals if infection_times[t] < infection_times[root]] same_time_terminals = [t for t in terminals if infection_times[t] == infection_times[root] if t != root] late_time_terminals = [t for t in terminals if infection_times[t] > infection_times[root]] if debug: print('root: {}'.format(root)) print('early_terminals: {}'.format(early_terminals)) print('same_time_terminals: {}'.format(same_time_terminals)) print('late_time_terminals: {}'.format(late_time_terminals)) if infection_times[root] == infection_times[terminals].min(): targets = early_terminals + same_time_terminals else: targets = early_terminals targets = list(set(targets) - {cand_source}) # no one can connect to cand_source if debug: print('targets: {}'.format(targets)) vis = init_visitor(g, root) cpbfs_search(g, source=root, visitor=vis, terminals=targets, forbidden_nodes=late_time_terminals, count_threshold=k) r2pred[root] = vis.pred for root, v, c in get_edges(vis.dist, root, early_terminals): if debug: print('edge ({}, {})'.format(v, root)) edges[(v, root)] = c # from earlier node to root if verbose: print('returning closure graph') gc = Graph(directed=True) for _ in range(g.num_vertices()): gc.add_vertex() for (u, v) in edges: gc.add_edge(u, v) eweight = gc.new_edge_property('int') eweight.set_2d_array(np.array(list(edges.values()))) return gc, eweight, r2pred
def makeGraph(self,img,dia,xScale,yScale): print 'Building Graph Data Structure' start=time.time() G = Graph(directed=False) vprop=G.new_vertex_property('object') eprop=G.new_edge_property('object') epropW=G.new_edge_property("int32_t") avgScale=(xScale+yScale)/2 test=np.where(img==True) ss = np.shape(test) cccc=0 percentOld=0.0 print str(np.round(percentOld,1))+'%' for (i,j) in zip(test[1],test[0]): cccc+=1 percent=(float(cccc)/float(ss[1]))*100 if percentOld+10< percent: print str(np.round(percent,1))+'%' percentOld=percent nodeNumber1 = (float(i)*yScale,float(j)*xScale) if gu.find_vertex(G, vprop, {'imgIdx':(j,i),'coord':nodeNumber1, 'nrOfPaths':0, 'diameter':float(dia[j][i])*avgScale}): v1=gu.find_vertex(G, vprop, {'imgIdx':(j,i),'coord':nodeNumber1, 'nrOfPaths':0, 'diameter':float(dia[j][i])*avgScale})[0] else: v1=G.add_vertex() vprop[G.vertex(v1)]={'imgIdx':(j,i),'coord':nodeNumber1, 'nrOfPaths':0, 'diameter':float(dia[j][i])*avgScale} try: if img[j,i+1] == True: nodeNumber2 = (float(i+1)*yScale,float(j)*xScale) if gu.find_vertex(G, vprop, {'imgIdx':(j,i+1),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j][i+1])*avgScale}): v2=gu.find_vertex(G, vprop, {'imgIdx':(j,i+1),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j][i+1])*avgScale})[0] if gu.find_edge(G, eprop, {'coord1':vprop[v2]['coord'], 'coord2':vprop[v1]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False}): pass else: e = G.add_edge(v1, v2) epropW[e]=(((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)/avgScale)**4 eprop[e]={'coord1':vprop[v1]['coord'], 'coord2':vprop[v2]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False} else: v2=G.add_vertex() vprop[G.vertex(v2)]={'imgIdx':(j,i+1),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j][i+1])*avgScale} e = G.add_edge(v1, v2) epropW[e]=(((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)/avgScale)**4 eprop[e]={'coord1':vprop[v1]['coord'], 'coord2':vprop[v2]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False} except: pass try: if img[j,i-1] == True: nodeNumber2 = (float(i-1)*yScale,float(j)*xScale) if gu.find_vertex(G, vprop, {'imgIdx':(j,i-1),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j][i-1])*avgScale}): v2=gu.find_vertex(G, vprop, {'imgIdx':(j,i-1),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j][i-1])*avgScale})[0] if gu.find_edge(G, eprop, {'coord1':vprop[v2]['coord'], 'coord2':vprop[v1]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False}): pass else: e = G.add_edge(v1, v2) epropW[e]=(((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)/avgScale)**4 eprop[e]={'coord1':vprop[v1]['coord'], 'coord2':vprop[v2]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False} else: v2=G.add_vertex() vprop[G.vertex(v2)]={'imgIdx':(j,i-1),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j][i-1])*avgScale} e = G.add_edge(v1, v2) epropW[e]=(((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)/avgScale)**4 eprop[e]={'coord1':vprop[v1]['coord'], 'coord2':vprop[v2]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False} except:pass try: if img[j + 1,i] == True: nodeNumber2 = (float(i)*yScale,float(j+1)*xScale) if gu.find_vertex(G, vprop, {'imgIdx':(j+1,i),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j+1][i])*avgScale}): v2=gu.find_vertex(G, vprop, {'imgIdx':(j+1,i),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j+1][i])*avgScale})[0] if gu.find_edge(G, eprop, {'coord1':vprop[v2]['coord'], 'coord2':vprop[v1]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False}): pass else: e = G.add_edge(v1, v2) epropW[e]=(((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)/avgScale)**4 eprop[e]={'coord1':vprop[v1]['coord'], 'coord2':vprop[v2]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False} else: v2=G.add_vertex() vprop[G.vertex(v2)]={'imgIdx':(j+1,i),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j+1][i])*avgScale} e = G.add_edge(v1, v2) epropW[e]=(((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)/avgScale)**4 eprop[e]={'coord1':vprop[v1]['coord'], 'coord2':vprop[v2]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False} except:pass try: if img[j - 1,i] == True: nodeNumber2 = (float(i)*yScale,float(j-1)*xScale) if gu.find_vertex(G, vprop, {'imgIdx':(j-1,i),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j-1][i])*avgScale}): v2=gu.find_vertex(G, vprop, {'imgIdx':(j-1,i),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j-1][i])*avgScale})[0] if gu.find_edge(G, eprop, {'coord1':vprop[v2]['coord'], 'coord2':vprop[v1]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False}): pass else: e = G.add_edge(v1, v2) epropW[e]=(((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)/avgScale)**4 eprop[e]={'coord1':vprop[v1]['coord'], 'coord2':vprop[v2]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False} else: v2=G.add_vertex() vprop[G.vertex(v2)]={'imgIdx':(j-1,i),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j-1][i])*avgScale} e = G.add_edge(v1, v2) epropW[e]=(((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)/avgScale)**4 eprop[e]={'coord1':vprop[v1]['coord'], 'coord2':vprop[v2]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False} except: pass # print '100.0%' print 'selecting largest connected component' G.edge_properties["ep"] = eprop G.edge_properties["w"] = epropW G.vertex_properties["vp"] = vprop l = gt.label_largest_component(G) print(l.a) u = gt.GraphView(G, vfilt=l) print '# vertices' print(u.num_vertices()) print(G.num_vertices()) print '# edges' print(u.num_edges()) print 'building graph finished in: '+str(time.time()-start)+'s' return u