def connect_subgraphs_by_spanning_trees(self): subgraphs = {} labels = graph_tool.label_components(self.g)[0].a for i in range(len(labels)): label = labels[i] if label in subgraphs: subgraphs[label].append(self.g.vertex(i)) else: subgraphs[label] = [self.g.vertex(i)] vertices_idx_to_connect = [] print(len(subgraphs.keys())) for l, s in tqdm(subgraphs.items()): # print(l) subgraph = graph_tool.GraphView(self.g, vfilt=labels == l, directed=True) tree_edges = graph_tool.min_spanning_tree(subgraph) tree = graph_tool.GraphView(subgraph, efilt=tree_edges, directed=True) sort = graph_tool.topological_sort(tree) vertices_idx_to_connect.append(sort[0]) self.v_root = self.g.add_vertex() for i in vertices_idx_to_connect: self.g.add_edge(self.v_root, i)
def extract_max_pg(ball_view, qgraph, w, S_w, d_Q): ''' Extract the maximum perfect graph of qgraph in the ball. ''' if valid_sim_w(S_w, w, qgraph) == False: return None vertex_matchset = set(v for u in qgraph.vertices() for v in S_w[u]) # edge_matchset = set(e for e in ball_view.edges() for (u, v) in qgraph.edges() if e.source() in S_w[u] and e.target() in S_w[v]) edge_matchset = set() for e in qgraph.edges(): source = e.source() target = e.target() for sim_v1 in S_w[source]: for sim_v2 in S_w[target]: eg = ball_view.edge(sim_v1, sim_v2) if eg: edge_matchset.add(eg) pg_view = gt.GraphView(ball_view, vfilt=lambda v: v in vertex_matchset, efilt=lambda e: e in edge_matchset) dist = gt.shortest_distance(pg_view, w, None, None, None, None, False) maxPGC = gt.GraphView(pg_view, vfilt=lambda v: dist.a[int(v)] <= d_Q) for u in qgraph.vertices(): S_w[u] = set(v for v in maxPGC.vertices() if v in S_w[u]) if len(S_w[u]) == 0: maxPGC = None return maxPGC
def loop(self): for m in range(1, self.seq_length+1): print ("begin step: ", m) tree_prop = self.update_edge_weights() tree_cost = self.cost_of_tree(tree_prop) self.current_tree = gt.GraphView(self.graph, efilt=tree_prop) # draw the best spanning tree yet found every 125 iterations if m % 125 == 0: position = self.graph_class.position self.graph_class.draw(self.best_tree, position) self.prev_degree = self.degree.copy() self.degree = self.current_tree.degree_property_map("total") vertex_sum = self.sum_of_vertex_weights() bound = tree_cost - 2*vertex_sum print("current best bound: ", self.bound) if m != self.seq_length: print( "new bound: ", bound) if bound > self.bound: self.bound = bound self.best_tree = self.current_tree self.step = self.next_step(m) self.update_vertex_weights() else: print self.bound gt.graph_draw(gt.GraphView(self.graph, efilt=tree_prop), self.graph_class.position)
def connectivity_prune(ball, w, sim, d_Q, Qgraph): ''' Use the matching relation sim to prune the ball ''' #输出的结果是 以w为球心,d_Q为半径的球,满足数据图dual simulation约束的球 #if write as follow then the result will be wrong # tmp = set(v for u in Qgraph.vertices() for v in sim[u] if v in ball.vertices()) # view_1 = gt.GraphView(ball, vfilt = lambda v: v in tmp) vertex_matchset = set(v for u in Qgraph.vertices() for v in sim[u] if v in ball.vertices()) edge_matchset = set() for e in Qgraph.edges(): source = e.source() target = e.target() for sim_v1 in sim[source]: for sim_v2 in sim[target]: eg = ball.edge(sim_v1, sim_v2) if eg: edge_matchset.add(eg) view_1 = gt.GraphView(ball, vfilt=lambda v: v in vertex_matchset, efilt=lambda e: e in edge_matchset) dist = gt.shortest_distance(view_1, w, None, None, None, None, False) view_2 = gt.GraphView(view_1, vfilt=lambda v: dist.a[int(v)] <= d_Q) return view_2
def graph_sequence(g, ibin, init_foot, fbin, init_step, max_iter, K): dist_array = np.array(g.ep.dist.a) filter_array = dist_array <= ibin gv = gt.GraphView(g, efilt=filter_array) ng = gt.Graph(gv, prune=True) pos = gt.sfdp_layout(ng, pos=ng.vp.pos, K=K) ng.vp.pos = pos graphs = [ng] cbin = ibin + init_foot # pos = gt.sfdp_layout(g, pos=pos, K=1.5) while cbin <= fbin: filter_array = dist_array <= cbin gv = gt.GraphView(g, efilt=filter_array) ng = gt.Graph(gv, prune=True) ng.copy_property(graphs[-1].vp.pos, tgt=ng.vp.pos) # print(ng.num_edges()) ng.vp.pos = gt.sfdp_layout( ng, pos=ng.vp.pos, max_iter=max_iter, init_step=init_step, K=K ) graphs.append(ng) cbin += init_foot # step += init_foot * 3 return graphs
def split_graph(g, first=None, second=None): if first is None: first = get_first() if second is None: second = get_second() g_first = gt.GraphView(g, vfilt=first) g_second = gt.GraphView(g, vfilt=second) return g_first, g_second
def strong_paraller(self, Qgraph, Dgraph): d_Q = self.cal_diameter_qgraph(Qgraph) self.dual_paraller(Qgraph, Dgraph) for u in Qgraph.vertices(): for v in self.sim[int(u)]: self.sim_node_set.add(v) self.sim_edge_set = self.sim_edge(Qgraph, Dgraph, self.sim) dual_graph = gt.GraphView(Dgraph, vfilt=lambda v: v in self.sim_node_set, efilt=lambda e: e in self.sim_edge_set) gl.comm.Barrier() all_sim_node = set() all_result = None if gl.worker_id == 0: all_result = gl.comm.gather(self.sim_node_set, root=0) else: gl.comm.gather(self.sim_node_set, root=0) if gl.worker_id == 0: for node_set in all_result: all_sim_node |= node_set all_sim_node = gl.comm.bcast( all_sim_node if gl.worker_id == 0 else None, root=0) # print all_sim_node gl.comm.Barrier() for v in all_sim_node: bfs_work = bfs.BfsWorker() bfs_work.bfs_paraller(Dgraph, v, d_Q) self.d_hop_node[v] = set(bfs_work.result_node & self.sim_node_set) gl.comm.Barrier() gl.comm.Barrier() all_strong_node = set() for node in all_sim_node: tmp_view = gt.GraphView(dual_graph, vfilt=lambda v: v in self.d_hop_node[node]) dwork = dsp.DualWorker() dwork.dual_paraller(Qgraph, tmp_view) for u in Qgraph.vertices(): all_strong_node |= dwork.sim[int(u)] gl.comm.Barrier() all_paraller_strong_node = set() all_result = None if gl.worker_id == 0: all_result = gl.comm.gather(all_strong_node, root=0) else: gl.comm.gather(all_strong_node, root=0) if gl.worker_id == 0: for node_set in all_result: all_paraller_strong_node |= node_set if gl.worker_id == 0: direct_node_set = set() balllistsim = ss.strong_simulation_ball(Qgraph, Dgraph) for ball in balllistsim: for v in ball.vertices(): direct_node_set.add(int(v)) print self.set_is_same_set(all_paraller_strong_node, direct_node_set)
def main(): # The description about the data is available at # <https://graph-tool.skewed.de/static/doc/collection.html> for name in ['karate', 'lesmis', 'football', 'dolphins', 'netscience']: g = gt.collection.data[name] g = gt.GraphView(g, directed=False) if name == 'netscience': # Use only the largest component in the netscience data l = gt.label_largest_component(g) g = gt.Graph(gt.GraphView(g, vfilt=l), prune=True) process(name, g)
def write_classes_hierarchical(filename, graph, state): levels = state.get_levels() b = levels[0].get_blocks() bcc = {} for i in np.unique(b.a): b_filter = (b.a == i) u = gt.GraphView(graph, vfilt=b_filter) comp, hist = gt.label_components(u) for v in u.vertices(): bcc[int(v)] = str(i)+'_'+str(comp[v]) f = open(filename, "w") header = "Name\tRealClass\tCComp\tBlockCC" for l in range(len(levels)): header = header + "\tBlock" + str(l) f.write(header+"\n") for v in graph.vertices(): Name = str(graph.vp.Name[v]) RealClass = str(graph.vp.RealClass[v]) CComp = str(graph.vp.CComp[v]) BlockCC = str(bcc[v]) Block_list = list() r = v for l in range(len(levels)): r = levels[l].get_blocks()[r] Block_list.append(str(r)) f.write(Name+"\t"+RealClass+"\t"+CComp+"\t"+BlockCC+"\t"+"\t".join(Block_list)+"\n") f.close()
def graphml_to_json(network_dir): """Converts full GraphML file to JSON subgraph""" labels = [] nodes_list = [] edges_list = [] full_graph = gt.load_graph( os.path.join(network_dir, os.path.basename(network_dir) + "_cytoscape.graphml")) components = gt.label_components(full_graph)[0].a subgraph = gt.GraphView(full_graph, vfilt=(components == components[-1])) subgraph = gt.Graph(subgraph, prune=True) subgraph.save(os.path.join(network_dir, "subgraph.graphml")) G = nx.read_graphml(os.path.join(network_dir, "subgraph.graphml")) for value in G.nodes.values(): labels.append(value['id']) data = json_graph.node_link_data(G) for k, v in data.items(): if k == "nodes": for node in range(len(v)): node_attr = v[node] node_attr['label'] = labels[node] nodes_list.append({'data': node_attr}) elif k == "links": for edge in range(len(v)): edge_attr = v[edge] edges_list.append({'data': edge_attr}) network_dict = {'elements': {'nodes': nodes_list, 'edges': edges_list}} return network_dict
def graph_view(g, vfilt=None, efilt=None): view = gt.GraphView(g, vfilt=vfilt, efilt=efilt) view.vertex_name = g.vertex_name view.vertex_taxid = g.vertex_taxid view.edge_in_taxonomy = g.edge_in_taxonomy view.vertex_in_taxonomy = g.vertex_in_taxonomy ## view.dubious = g.dubious view.incertae_sedis = g.incertae_sedis view.taxid_vertex = g.taxid_vertex view.hindex = g.hindex view.edge_strees = g.edge_strees view.vertex_snode = g.vertex_snode view.vertex_strees = g.vertex_strees view.vertex_stem_cdef = g.vertex_stem_cdef view.stem_cdef_vertex = g.stem_cdef_vertex _attach_funcs(view) r = [ x for x in view.vertices() if x.in_degree()==0 ] ## assert len(r)==1 ## assert r if not r: print '!!! no root?' if len(r) > 1: print '!!! disconnected view' view.root = r[0] view.rootv = r return view
def block_annotation(graph, state): levels = state.get_levels() # Find the informative hierarchical levels (i.e. the non-redundant levels, those with non-equivalent block assignment) def check_level_redundancy(l): x = state.project_partition(l, 0).a y = state.project_partition(l + 1, 0).a return gt.partition_overlap(x, y, norm=True) == 1 L = len(levels) - 1 redundant_levels = [False] + list( map(check_level_redundancy, reversed(range(L)))) nr_levels = [L - i for i, x in enumerate(redundant_levels) if not x] b = levels[0].get_blocks() bcc = graph.new_vertex_property('string') bcc4 = graph.new_vertex_property('string') for i in np.unique(b.a): b_filter = (b.a == i) u = gt.GraphView(graph, vfilt=b_filter) tmp = [] r = u.get_vertices()[0] for l in range(len(levels)): r = levels[l].get_blocks()[r] if l in nr_levels: tmp.append(str(r)) tmp.reverse() comp, hist = gt.label_components(u) for v in u.vertices(): tag = '_'.join(tmp + [str(comp[v])]) bcc[v] = tag bcc4[v] = tag if (hist[comp[int(v)]] >= 4) else '-' return ((b, bcc, bcc4))
def get_related_plasmids(graph, vfilt): ref = [] new = [] ref_index = {} new_index = {} u = gt.GraphView(graph, vfilt=vfilt) for v in u.vertices(): if u.vp.AccessionVersion[v] == qry_acc: # As the query is not present in the reference network, we get rid of it to calculate the fit of both partitions continue if u.vp.sHSBMRef2[v] not in ref_index: if len(list(ref_index.values())) > 0: ref_index[u.vp.sHSBMRef2[v]] = np.max(list( ref_index.values())) + 1 else: ref_index[u.vp.sHSBMRef2[v]] = 0 ref.append(ref_index[u.vp.sHSBMRef2[v]]) if u.vp.sHSBM2[v] not in new_index: if len(list(new_index.values())) > 0: new_index[u.vp.sHSBM2[v]] = np.max(list( new_index.values())) + 1 else: new_index[u.vp.sHSBM2[v]] = 0 new.append(new_index[u.vp.sHSBM2[v]]) return (ref, new)
def cost_of_tree(self, tree_prop): tree = gt.GraphView(self.graph, efilt=tree_prop) tree_cost = 0 for e in tree.edges(): tree_cost += self.costs[e] return tree_cost
def write_classes_hierarchical(filename, graph, state): levels = state.get_levels() b = levels[0].get_blocks() bcc = {} for i in np.unique(b.a): b_filter = (b.a == i) u = gt.GraphView(graph, vfilt=b_filter) comp, hist = gt.label_components(u) for v in u.vertices(): bcc[int(v)] = str(i) + '_' + str(comp[v]) f = open(filename, "w") header = "AccessionVersion\tPTU\tCComp\tBlockCC" for l in range(len(levels)): header = header + "\tBlock" + str(l) f.write(header + "\n") for v in graph.vertices(): AccVer = graph.vp.AccessionVersion[v] PtuManual = graph.vp.PtuManual[v] CComp = str(graph.vp.CComp[v]) BlockCC = bcc[v] Block_list = list() r = v for l in range(len(levels)): r = levels[l].get_blocks()[ r] # The tutorial seems to be wrong here Block_list.append(str(r)) f.write(AccVer + "\t" + PtuManual + "\t" + CComp + "\t" + BlockCC + "\t" + "\t".join(Block_list) + "\n") f.close()
def get_blocksCC(graph, blocks): v_Bcc = graph.new_vertex_property("string") for i in np.unique(blocks.a): b_filter = (blocks.a == i) u = gt.GraphView(graph, vfilt=b_filter) comp, hist = gt.label_components(u) for v in u.vertices(): v_Bcc[v] = str(i)+'_'+str(comp[v]) return v_Bcc
def _refinement(graph, threshold): vertex_betweenness_value = gt.betweenness(graph)[0].get_array() d = np.abs(vertex_betweenness_value - np.median(vertex_betweenness_value)) mdev = np.median(d) s = d / mdev if mdev else np.zeros_like(d) vfilt = s < threshold graph = gt.GraphView(graph, vfilt=vfilt) comp, hist = gt.label_components(graph) temp = [] for i in range(len(hist)): if hist[i] > 1: temp.append( gt.Graph(gt.GraphView(graph, vfilt=(comp.a == i)), prune=True, directed=False)) return temp
def produce_answer(self, entry, prune): qc_fltr = self.graph.new_vertex_property("bool") qc_fltr = self.make_filter(entry.qc, qc_fltr) qg = gtall.GraphView(self.graph, qc_fltr) qg = gtall.Graph(qg, prune=prune) best_score = 0.0 answer = -1 for i, ac in enumerate(entry.ac): ac_fltr = self.graph.new_vertex_property("bool") ac_fltr = None # make_filter(g, ac, ac_fltr) ag = gtall.GraphView(self.graph, ac_fltr) ag = gtall.Graph(ag, prune=prune) score = self.reason_over_paths(qg, ag) if score > best_score: best_score = score answer = i del ac_fltr del qc_fltr return answer
def least_edges(self): v0 = self.graph.vertex(0) subgraph = gt.GraphView(self.graph, efilt=self.neighborhoods[0]) sorted_edges = sorted(subgraph.edges(), key=lambda e: self.costs[e]) e1 = sorted_edges[0] e2 = sorted_edges[1] s1 = e1.source() s2 = e2.source() t1 = e1.target() t2 = e2.target() return (s1,t1),(s2,t2)
def initialize_step(self): tree_prop = gt.min_spanning_tree(self.graph, weights = self.e_weights) tree = gt.GraphView(self.graph, efilt=tree_prop) gt.graph_draw(tree, self.graph_class.position) self.best_tree = tree tree_cost = 0 for e in tree.edges(): tree_cost += self.e_weights[e] t = (1/(2.0*self.graph_class.size)) * tree_cost return t
def create_ball_view(w, d_Q, Dgraph): ''' Create a ball [w, d_Q] view on top of data graph ''' #global Dgraph dist = gt.shortest_distance(Dgraph, w, None, None, None, None, False) ball_view = gt.GraphView(Dgraph, vfilt=lambda v: dist.a[int(v)] <= d_Q) # print "ball------------------------->" # for e in ball_view.edges(): # print ball_view.vertex_properties["label"][e.source()],"-->",ball_view.vertex_properties["label"][e.target()] return ball_view
def worker(Graph, current_count): U = gt.GraphView(Graph, vfilt=lambda v: Graph.vertex_properties['alive'][v]) gt.graph_draw(U, U.vertex_properties['position'], vertex_shape=U.vertex_properties['shape'], vertex_fill_color=U.vertex_properties['fillcolor'], output=frame_path + 'taxi%06d.png' % count, bg_color=(1, 1, 1, 1), output_size=resolution)
def need_data_b(received, g, hir): re = received.keys() need = set() for i in re: need = need.union(hir[i]) fil = [] vfilt = g.new_vertex_property("bool") for v in g.vertices(): if v_userid[v]['userid'] in need: vfilt[v] = True u = gt.GraphView(g, vfilt) u = gt.Graph(u) return (u)
def subgraph_source2target(self, sources, targets): # Below code is copied from graphNX.py # H, S, T = self.multiple2single_st(sources, targets) # subgraph_vertices = nx.descendants(H, S) & nx.(H, T) # def vertex_filter(v): # return v in subgraph_vertices G = gt.GraphView(H, vfilt=vertex_filter, directed=True) G = H.subgraph(nx.descendants(H, S) & nx.ancestors(H, T)) return self.__class__(G)
def output_step_data(self,dgraph,path): vmatch_set = set() for key in self.sim.keys(): for v in self.sim[key]: vmatch_set.add(v) gtview = gt.GraphView(dgraph, vfilt = lambda v: v in vmatch_set) gtview.vertex_properties["show"] = gtview.new_vertex_property("string") for v in gtview.vertices(): if v in self.border_node: tmpstr = "o"+str(v)+gtview.vertex_properties["label"][v] else: tmpstr = str(v)+gtview.vertex_properties["label"][v] gtview.vertex_properties["show"][v] =tmpstr gt.graph_draw(gtview, vertex_text = gtview.vertex_properties["show"],output_size=(800, 800),output = path) del gtview.vertex_properties["show"]
def mapping_2_util_vec_general(groups, network): num_people = len(network.get_vertices()) G = [[False for i in range(num_people)] for j in range(len(groups))] for i,group in enumerate(groups): for person in group: G[i][person] = True Group_graphs = [gt.GraphView(network, G[i]) for i in range(len(G))] util = [util_func_vec(g) for g in Group_graphs] ret = [] for person in range(num_people): p = network.vertex(person) #for i in range(len(G)): # print(network.vp["pref"][p][i]) p_util = [util[i][p] + network.vp.pref[p][i] for i in range(len(G))] ret.append(max(p_util)) return ret
def removeUnimportantResources(self, unimportant, resources): unimportant_vertices = list() for u in reversed(sorted(unimportant)): #Never delete grandmother and grandfather, even if they become insignificant if u > 1: unimportant_vertices.append(self.stateGraph.vertex(u)) gv = gt.GraphView(self.stateGraph, vfilt=lambda x: x not in unimportant_vertices) #stateGraph = gt.Graph(gv,prune=True) #for u in unimportant_vertices: # stateGraph.remove_vertex(u) #print (stateGraph.vertex(0)) #print (stateGraph.vertex(1)) return gv
def all_license_subgraphs(g, licenses, quota=1, proportion=0): """Takes a graph and returns the subgraphs induced by different licenses types. Parameters: g: either a gt.Graph or a nx.Graph licenses: list of keys to be used for the return dict quota: an int for the minimum number of licenses of a given type to appear in that licenses subgraph proportion: a float for the proportion of licenses on the domain that should be the given license type Returns: a dict mapping string license names to subgraphs """ if isinstance(g, gt.Graph): subgraph_by_license = dict() for license in licenses: nodes = g.new_vp('bool') for v in g.vertices(): cc_licenses = g.vp['cc_licenses'][v] if isinstance(cc_licenses, dict): total_licenses = sum(cc_licenses.values()) if (license in cc_licenses and cc_licenses[license] >= proportion * total_licenses and cc_licenses[license] >= quota): nodes[v] = True else: nodes[v] = False subgraph_by_license[license] = gt.GraphView(g, vfilt=nodes) return subgraph_by_license elif isinstance(g, nx.Graph): subgraph_by_license = dict() for license in licenses: nodes = set() for v, data in g.nodes(data=True): cc_licenses = data['cc_licenses'] if isinstance(cc_licenses, dict): total_licenses = sum(cc_licenses.values()) if (license in cc_licenses and cc_licenses[license] >= proportion * total_licenses and cc_licenses[license] >= quota): nodes.add(v) subgraph_by_license[license] = nx.induced_subgraph(g, nodes) return subgraph_by_license else: raise TypeError('graph format not recognized, must be nx or gt')
def plot_initialize(): color = G.new_vertex_property('vector<double>') shape = G.new_vertex_property('string') alive = G.new_vertex_property('bool') for v in G.vertices(): color[v] = [254. / 255, 238. / 255, 107. / 255, 1] shape[v] = 'circle' alive[v] = True G.vertex_properties['fillcolor'] = color G.vertex_properties['shape'] = shape G.vertex_properties['alive'] = alive global G_no_moving G_no_moving = gt.GraphView( G, vfilt=lambda v: G.vertex_properties['shape'] == 'circle')
def adaptivethresh(in_mat, thr, mlib, N, use_gt=False): """ Counts number of motifs with a given absolute threshold. Parameters ---------- in_mat : ndarray M x M Connectivity matrix thr : float Absolute threshold [0, 1]. mlib : list List of motif classes. Returns ------- mf : ndarray 1D vector listing the total motifs of size N for each class of mlib. References ---------- .. [1] Battiston, F., Nicosia, V., Chavez, M., & Latora, V. (2017). Multilayer motif analysis of brain networks. Chaos. https://doi.org/10.1063/1.4979282 """ if use_gt is True: try: import graph_tool.all as gt from pynets.stats.netstats import np2gt g = np2gt((in_mat > thr).astype(int)) mlib, mf = gt.motifs(gt.GraphView(g, directed=False), k=N) except ImportError as e: print(e, "graph_tool not installed!") else: from pynets.stats.netmotifs import countmotifs mf = countmotifs((in_mat > thr).astype(int), N=N) try: mf = np.array([mf[k] for k in mlib]) except BaseException: print('0 motifs...') mf = np.zeros(len(mlib)) return mf