def full_cost(tree_adj, tree, gt_graph, X): import graph_tool.centrality as gt from bidict import bidict vbetween, ebetween = gt.betweenness(gt_graph) info, prt = augmented_ancestor(tree_adj, X) root = None for u, parent in prt.items(): if parent is None: root = u break assert root is not None Xbar = set(tree_adj.keys()) - X edge_to_vertex = bidict({}) raw_edge_features = {} for edge in tree: tail = edge_tail(edge, prt)[0] edge_to_vertex[edge] = tail raw_edge_features[edge] = [ebetween[gt_graph.edge(*edge)], vbetween[gt_graph.vertex(tail)], info[tail][1]/len(tree_adj), info[tail][2]/len(X), [], []] distances = [] for i in X: for j in Xbar: path = actual_tree_path(i, j, prt) path_len = len(path) distances.append(path_len) if j != root: raw_edge_features[edge_to_vertex[:j]][4].append(path_len) for edge in path: raw_edge_features[edge][5].append(path_len) return raw_edge_features, sum(distances)/len(distances)
def calculate_measures(g, tmp_measures=None, measure_list=['BC', 'T', 'E']): if tmp_measures is None: tmp_measures = dict((k, []) for k in measure_list) tmp_measures['BC'].append(np.mean(gtc.betweenness(g)[0].get_array())) tmp_measures['T'].append(gtclust.global_clustering(g)[0]) tmp_measures['E'].append(np.mean(gtc.closeness(g,harmonic=True).get_array())) return tmp_measures
def calculate_betweenness(map_object, original_data_source, filtered_name_string): betweenness_property_map=betweenness(map_object.graph_object)[0] betweennes_list=[] for station_name_string in original_data_source.data["station_names"]: if station_name_string==filtered_name_string: betweennes_list.append(-1) else: vertex=map_object.station_name_index[station_name_string] betweennes_list.append(betweenness_property_map[vertex]) return betweennes_list
def calculate_measures(g, tmp_measures=None, measure_list=['BC', 'T', 'E']): if tmp_measures is None: tmp_measures = dict((k, []) for k in measure_list) tmp_measures['BC'].append(np.mean(gtc.betweenness(g)[0].get_array())) tmp_measures['T'].append(gtclust.global_clustering(g)[0]) tmp_measures['E'].append( np.mean(gtc.closeness(g, harmonic=True).get_array())) return tmp_measures
def set_properties(self, subgraph): v_betweenness, e_betweenness = betweenness(subgraph) subgraph.vertex_properties["vertex_betweenness"] = v_betweenness subgraph.edge_properties["edge_betweenness"] = e_betweenness v_closeness = closeness(subgraph) subgraph.vertex_properties["closeness"] = v_closeness l_clustering = local_clustering(subgraph) subgraph.vertex_properties["local_clustering"] = l_clustering bicomp, articulation, nc = label_biconnected_components(subgraph) subgraph.vertex_properties["articulation"] = articulation return subgraph
def plot_centralities(network, title="Centrality measures"): g = network.graph comm_size = g.num_vertices() closeness = centrality.closeness(g).get_array().tolist() max_eigenval, eigenvec = centrality.eigenvector(g) # eigenvector = [x/max_eigenval for x in eigenvec.get_array().tolist()] #normalize! eigenvector = eigenvec.get_array().tolist() betw, _edges = centrality.betweenness(g, norm=True) betweenness = betw.get_array().tolist() P.suptitle(title) # P.figure() print "nans", len([x for x in closeness if isnan(x)]) closeness = [0 if isnan(x) else x for x in closeness] # closeness = [x for x in closeness if not isnan(x)] closeness = _filtered(closeness, comm_size) print "closeness", closeness print "non zeros", len([x for x in closeness if x != 0]) P.subplot(2, 2, 1) plot_hist(closeness) P.xlabel("Closeness centrality") P.ylabel("Number of nodes (total={})".format(len(closeness))) counts, degrees = stats.vertex_hist(g, "in", float_count=False) print "counts : ", len(counts), counts print "degrees: ", len(degrees), degrees counts = list(counts) counts.append(0) P.subplot(2, 2, 2) P.bar(degrees, counts, align='center', color="#348ABD") # P.hist(counts, bins=degrees, ) P.xlabel("Degree centrality (in)") P.ylabel("Number of nodes (total={})".format(sum(counts))) P.xlim(0, max(degrees)) betweenness = _filtered(betweenness, comm_size) print "betweenness", betweenness P.subplot(2, 2, 3) plot_hist(betweenness) P.xlabel("Betweenness centrality") P.ylabel("Number of nodes (total={})".format(len(betweenness))) eigenvector = _filtered(eigenvector, comm_size) print "eigenvector", eigenvector P.subplot(2, 2, 4) plot_hist(eigenvector) P.xlabel("Eigenvector centrality") P.ylabel("Number of nodes (total={})".format(len(eigenvector))) P.show()
def get_dataframe_all_topolog_metrics(self): graph = self.get_graph() eprop_trust = graph.new_edge_property('double') start_time = time.time() for e in graph.edges(): v_name_s = graph.vertex_properties['name_proteins'][e.source()] v_number_s = self.dict_genes[v_name_s] v_name_t = graph.vertex_properties['name_proteins'][e.target()] v_number_t = self.dict_genes[v_name_t] eprop_trust[e] = self.adjacency_matrix[v_number_s, v_number_t] graph.edge_properties['trust'] = eprop_trust print('confidence score за :', '--- %s seconds ---' % (time.time() - start_time)) list_metrics = [ 'betweenness', 'pagerank', 'closeness', 'katz', 'hits_authority', 'hits_hub', 'eigenvector', 'eigentrust' ] # 'trust_transitivity' dict_map = {} start_time = time.time() dict_map['betweenness'] = ct.betweenness(graph)[0] dict_map['pagerank'] = ct.pagerank(graph) dict_map['closeness'] = ct.closeness(graph) dict_map['katz'] = ct.katz(graph) dict_map['hits_authority'] = ct.hits(graph)[1] dict_map['hits_hub'] = ct.hits(graph)[2] dict_map['eigenvector'] = ct.eigenvector(graph)[1] #print('trust_transitivity') #"dict_map['trust_transitivity'] = ct.trust_transitivity(graph, graph.edge_properties["trust"]) print('все метрики кроме eigentrust за :', '--- %s seconds ---' % (time.time() - start_time)) start_time = time.time() dict_map['eigentrust'] = ct.eigentrust(graph, graph.edge_properties['trust'], max_iter=10**6) print('eigentrust за :', '--- %s seconds ---' % (time.time() - start_time)) start_time = time.time() dict_metrics = {} for key in list_metrics: dict_metrics[key] = [] for v in graph.vertices(): for metric in list_metrics: dict_metrics[metric].append(dict_map[metric][v]) dataframe_all_topolog_metrics = pd.DataFrame(dict_metrics) dataframe_all_topolog_metrics.index = graph.vertex_properties[ 'name_proteins'] print('получила датафрейм с метриками за :', '--- %s seconds ---' % (time.time() - start_time)) return dataframe_all_topolog_metrics
def betweenness_influent_nodes(graph): vertex_prop, edge_prop = gc.betweenness(graph, weight=graph.edge_properties["weight"]) gd.graph_draw(graph, vertex_fill_color=vertex_prop, vertex_size=gd.prop_to_size(vertex_prop, mi=5, ma=15), edge_pen_width=gd.prop_to_size(edge_prop, mi=0.5, ma=5), vcmap=matplotlib.cm.gist_heat, vorder=vertex_prop, output_size=(1920, 1080), output=FIGURES_PATH + BETWEENNESS_VIZ) print("Visualization finished and saved...")
def graph_betweenness_centrality(graph): nodes, edges = betweenness(graph) nodes_dataframe = pandas.DataFrame(nodes.get_array()) edges_dataframe = pandas.DataFrame(edges.get_array()) nodes_dataframe.columns = [betweenness_centrality_key] edges_dataframe.columns = [betweenness_centrality_key] nodes_dataframe = sort_for_betweenness_centrality(nodes_dataframe) edges_dataframe = sort_for_betweenness_centrality(edges_dataframe) return nodes_dataframe, edges_dataframe
def betweenness_correlated_weights(graph,dicProperties,nEdges,nExc): lstWeights = np.zeros(nEdges) rMin = dicProperties["Min"] rMax = dicProperties["Max"] vpropBetw,epropBetw = betweenness(graph) arrBetw = epropBetw.a.copy() arrLogBetw = np.log10(arrBetw) rMaxLogBetw = arrLogBetw.max() rMinLogBetw = arrLogBetw.min() arrLogBetw = -5 + 2 * (arrLogBetw - rMinLogBetw ) / (rMaxLogBetw - rMinLogBetw) arrBetw = np.exp(np.log(10) * arrLogBetw) rMaxBetw = arrBetw.max() rMinBetw = arrBetw.min() lstWeights = np.multiply(arrBetw-rMinBetw,rMax/rMaxBetw) + rMin return lstWeights
def execute(now): session = store.get_session() topology = NetworkTopology(session, now) (node_betweenness, link_betweenness) = gt.betweenness(topology.topology) closeness = gt.closeness(topology.topology) for v in topology.nodes: topology.node_information[v].degree = topology.nodes[v].out_degree() topology.node_information[v].closeness = closeness[topology.nodes[v]] topology.node_information[v].betweenness = node_betweenness[topology.nodes[v]] for l in topology.links: topology.link_information[l].betweenness = link_betweenness[topology.links[l]] session.commit()
def build_pivot_generator(N, graph, shared_edges, pivot_strategy, pivot_gen): """Return a vertex generator according to the chosen strategy""" if shared_edges: non_shared = non_shared_vertices(N, shared_edges) return iter(lambda: r.choice(non_shared), -1) if pivot_strategy is PivotStrategy.weighted: assert pivot_gen, "provide your own generator" return pivot_gen if pivot_strategy in [PivotStrategy.uniform, PivotStrategy.no_pivot]: return None if pivot_strategy is PivotStrategy.by_degree: degs = graph.degree_property_map('total').a weights = np.exp(-degs)/np.sum(np.exp(-degs)) return WeightedRandomGenerator(weights) if pivot_strategy is PivotStrategy.by_betweenness: vb, _ = centrality.betweenness(graph) vb = N*vb.a/5 weights = np.exp(-vb)/np.sum(np.exp(-vb)) return WeightedRandomGenerator(weights) raise ValueError('check build_pivot_generator call')
def analyze(dir_name): g = gload.load_graph(dir_name) with open(os.path.join(dir_name, GRAPH_FILE), 'wb') as f: pickle.dump(g, f) sim = galg.simrank(g) with open(os.path.join(dir_name, SIMRANK_FILE), 'wb') as f: pickle.dump(sim, f) prank = galg.psimrank(g) with open(os.path.join(dir_name, PRANK_FILE), 'wb') as f: pickle.dump(prank, f) gr = grdf.calculate_taxonomy2(g) with open(os.path.join(dir_name, GR_FILE), 'wb') as f: pickle.dump(gr, f) vp, ep = centrality.betweenness(analsis_dict['graph']) with open(os.path.join(dir_name, BET_FILE), 'wb') as f: pickle.dump(vp, f)
def calculate_cen_b(pre_u, type, pos_type): # if type == "DeC": if type == "BetC": v_be = pre_u.new_vertex_property("float") v_betweenness = gtc.betweenness(pre_u, vprop=v_be) pre_u.vertex_properties['betweenness'] = v_be if type == "CloC": v_clo = pre_u.new_vertex_property("float") v_closeness = gtc.closeness(pre_u, vprop=v_clo) pre_u.vertex_properties['closeness'] = v_clo # if type == "EigenveC": # if type == "KatzC": # if type == "EigentrustC": if pos_type == "FD": pos = gt.sfdp_layout(pre_u, p=2.6, K=40, C=1) if pos_type == "FR": pos = gt.fruchterman_reingold_layout(pre_u, n_iter=100, r=100, a=10) nodes = [] convert = [] for v in pre_u.vertices(): s_node = {} s_node["name"] = v_userid[v]["userid"] s_node['race'] = v_race[v]["race"] s_node['cohort'] = v_cohort[v]['cohort'] s_node['gender'] = v_gender[v]['gender'] s_node['c1net'] = v_c1net[v]['c1net'] s_node['c2net'] = v_c2net[v]['c2net'] s_node['c3net'] = v_c3net[v]['c3net'] s_node['c4net'] = v_c4net[v]['c4net'] s_node['c5net'] = v_c5net[v]['c5net'] s_node['c6net'] = v_c6net[v]['c6net'] s_node['c7net'] = v_c7net[v]['c7net'] s_node["out-degree"] = v.out_degree() s_node["in-degree"] = v.in_degree() s_node["cx"] = (pos[v][0]) s_node["cy"] = (pos[v][1]) if 'v_be' in locals().keys(): s_node["BetC"] = v_be[v] if 'v_clo' in locals().keys(): if math.isnan(v_clo[v]): v_clo[v] = 0 s_node["CloC"] = v_clo[v] convert.append(v_userid[v]["userid"]) # o_node={} # o_node[v_userid[v]["userid"]]=s_node nodes.append(s_node) all_data = {} all_data['nodes'] = nodes links = [] for e in pre_u.edges(): s_edge = {} s_edge['source'] = convert.index(str(e_source[e])) s_edge['target'] = convert.index(str(e_target[e])) s_edge['type'] = e_type[e]['type'] s_edge['year'] = e_year[e]['year'] s_edge['weight'] = len(pre_u.edge(e.target(), e.source(), all_edges=True, add_missing=False)) + \ len(pre_u.edge(e.source(), e.target(), all_edges=True, add_missing=False)) links.append(s_edge) all_data['links'] = links return (all_data)
def _get_edge_centrality(graph): _, centrality = betweenness(graph, norm=False) return centrality
def save_centrality(g, node_out_fname, edge_out_fname, weight=None): """ :param g: `Graph` instance :return: None """ df = pd.DataFrame() df['node'] = pd.Series(np.array([int(v) for v in g.vertices()])) # degree print('Degree') num_nodes = len(g.get_vertices()) denom = num_nodes - 1 if g.is_directed(): unnormalized_in_degree = np.array([v.in_degree() for v in g.vertices()]) unnormalized_out_degree = np.array([v.out_degree() for v in g.vertices()]) df['unnormalized_in_degree'] = unnormalized_in_degree df['unnormalized_out_degree'] = unnormalized_out_degree df['in_degree'] = unnormalized_in_degree / denom df['out_degree'] = unnormalized_out_degree / denom else: # check whether weighted graph or not if weight: unnormalized_degree = np.zeros(num_nodes) edge_weights = np.array(weight.get_array()) for edge, w in zip(g.get_edges(), edge_weights): for node in edge[:2]: unnormalized_degree[node] += w df['unnormalized_degree'] = unnormalized_degree df['degree'] = unnormalized_degree / denom else: unnormalized_degree = np.array([v.out_degree() for v in g.vertices()]) df['unnormalized_degree'] = unnormalized_degree df['degree'] = unnormalized_degree / denom # closeness print('Closeness') df['unnormalized_closeness'] = np.array(closeness(g, weight=weight, norm=False).get_array()) df['closeness'] = np.array(closeness(g, weight=weight, norm=True).get_array()) # pageRank print('PageRank') df['pagerank'] = np.array(pagerank(g, weight=weight).get_array()) # betweenness print('Betweenness') un_node_between, un_edge_between = betweenness(g, weight=weight, norm=False) node_between, edge_between = betweenness(g, weight=weight, norm=True) df['unnormalized_betweenness'] = np.array(un_node_between.get_array()) df['betweenness'] = np.array(node_between.get_array()) df.to_csv(node_out_fname, index=False) # edge sources = [] targets = [] for e in g.edges(): source, target = list(map(int, [e.source(), e.target()])) sources.append(source) targets.append(target) df = pd.DataFrame() df['source'] = pd.Series(np.array(sources)) df['target'] = np.array(targets) # betweenness df['unnormalized_betweenness'] = np.array(un_edge_between.get_array()) df['betweenness'] = np.array(edge_between.get_array()) df.to_csv(edge_out_fname, index=False)
#!/usr/bin/env python3 # This file is part of Project Ruprecht. See COPYING for license. import sys import graph_tool as gt import graph_tool.centrality as centr def print_top_e(g, eprops): names = g.vertex_properties['name'] ee = list(g.edges()) ee.sort(key=lambda a: eprops[a], reverse=True) for e in ee: print(names[e.source()], names[e.target()]) g = gt.load_graph_from_csv(sys.argv[1], csv_options={'delimiter': "\t"}) btwv, btwe = centr.betweenness(g) print_top_e(g, btwe)
def betweenness_centrality(g: Graph): return centrality.betweenness(g, weight=g.edge_properties['weight'])[0]
def calculate_cen(pre_u, type, pos_type): # if type == "DeC": if type == "BetC": v_be = pre_u.new_vertex_property("float") v_betweenness = gtc.betweenness(pre_u, vprop=v_be) pre_u.vertex_properties['betweenness'] = v_be if type == "CloC": v_clo = pre_u.new_vertex_property("float") v_closeness = gtc.closeness(pre_u, vprop=v_clo) pre_u.vertex_properties['closeness'] = v_clo # if type == "EigenveC": # if type == "KatzC": # if type == "EigentrustC": if pos_type == "FD": pos = gt.sfdp_layout(pre_u, p=2.6, K=40, C=1) if pos_type == "FR": pos = gt.fruchterman_reingold_layout(pre_u, n_iter=100, r=100, a=10) nodes = [] convert = [] for v in pre_u.vertices(): s_node = {} s_node["name"] = v_userid[v]["userid"] s_node['gender'] = v_gender[v]["gender"] s_node['schregion'] = v_schregion[v]['schregion'] s_node['school'] = v_school[v]['school'] s_node['major'] = v_major[v]['major'] s_node['marriage'] = v_marriage[v]['marriage'] s_node['grade'] = v_grade[v]['grade'] s_node['liveplace'] = v_liveplace[v]['liveplace'] s_node['height'] = v_height[v]['height'] s_node['weight'] = v_weight[v]['weight'] s_node['scorelevel'] = v_scorelevel[v]['scorelevel'] s_node['birthyear'] = v_birthyear[v]['birthyear'] s_node["out-degree"] = v.out_degree() s_node["in-degree"] = v.in_degree() s_node["cx"] = pos[v][0] s_node["cy"] = pos[v][1] if 'v_be' in locals().keys(): s_node["BetC"] = v_be[v] if 'v_clo' in locals().keys(): if math.isnan(v_clo[v]): v_clo[v] = 0 s_node["CloC"] = v_clo[v] # print(v_clo[v]) convert.append(v_userid[v]["userid"]) # o_node={} # o_node[v_userid[v]["userid"]]=s_node nodes.append(s_node) all_data = {} all_data['nodes'] = nodes links = [] for e in pre_u.edges(): s_edge = {} s_edge['source'] = convert.index(str(e_source[e])) s_edge['target'] = convert.index(str(e_target[e])) s_edge['date'] = e_date[e]['date'] s_edge['dailycount'] = e_dailycount[e]['dailycount'] s_edge['weight'] = len(pre_u.edge(e.target(), e.source(), all_edges=True, add_missing=False)) + \ len(pre_u.edge(e.source(), e.target(), all_edges=True, add_missing=False)) links.append(s_edge) all_data['links'] = links return(all_data)
def _get_vertex_centrality(graph): centrality, _ = betweenness(graph, weight=graph.ep.weight, norm=False) return centrality