def test_p4_edge_load(self): G=self.P4 c = nx.edge_load_centrality(G) d={(0, 1): 6.000, (1, 2): 8.000, (2, 3): 6.000} for n in G.edges(): assert_almost_equal(c[n],d[n],places=3)
def test_p4_edge_load(self): G = self.P4 c = nx.edge_load_centrality(G) d = {(0, 1): 6.000, (1, 2): 8.000, (2, 3): 6.000} for n in G.edges(): assert almost_equal(c[n], d[n], places=3)
def test_tree_edge_load(self): G=self.T c = nx.edge_load_centrality(G) d={(0, 1): 24.000, (0, 2): 24.000, (1, 3): 12.000, (1, 4): 12.000, (2, 5): 12.000, (2, 6): 12.000} for n in G.edges(): assert_almost_equal(c[n],d[n],places=3)
def test_tree_edge_load(self): G = self.T c = nx.edge_load_centrality(G) d = {(0, 1): 24.000, (0, 2): 24.000, (1, 3): 12.000, (1, 4): 12.000, (2, 5): 12.000, (2, 6): 12.000} for n in G.edges(): assert almost_equal(c[n], d[n], places=3)
def test_tree_edge_load(self): G = self.T c = nx.edge_load_centrality(G) d = { (0, 1): 24.000, (0, 2): 24.000, (1, 3): 12.000, (1, 4): 12.000, (2, 5): 12.000, (2, 6): 12.000, } for n in G.edges(): assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_k5_edge_load(self): G=self.K5 c = nx.edge_load_centrality(G) d={(0, 1): 5.000, (0, 2): 5.000, (0, 3): 5.000, (0, 4): 5.000, (1, 2): 5.000, (1, 3): 5.000, (1, 4): 5.000, (2, 3): 5.000, (2, 4): 5.000, (3, 4): 5.000} for n in G.edges(): assert_almost_equal(c[n],d[n],places=3)
def test_k5_edge_load(self): G = self.K5 c = nx.edge_load_centrality(G) d = {(0, 1): 5.000, (0, 2): 5.000, (0, 3): 5.000, (0, 4): 5.000, (1, 2): 5.000, (1, 3): 5.000, (1, 4): 5.000, (2, 3): 5.000, (2, 4): 5.000, (3, 4): 5.000} for n in G.edges(): assert almost_equal(c[n], d[n], places=3)
def test_k5_edge_load(self): G = self.K5 c = nx.edge_load_centrality(G) d = { (0, 1): 5.000, (0, 2): 5.000, (0, 3): 5.000, (0, 4): 5.000, (1, 2): 5.000, (1, 3): 5.000, (1, 4): 5.000, (2, 3): 5.000, (2, 4): 5.000, (3, 4): 5.000, } for n in G.edges(): assert c[n] == pytest.approx(d[n], abs=1e-3)
def get_central_edges(g, allowed, n=20, method="load_centrality"): nx_g = pyzx2nx(g.copy()) if method == "load_centrality": centralities = nx.edge_load_centrality(nx_g) elif method == "betweenness_centrality": centralities = nx.edge_betweenness_centrality(nx_g) elif method == "pos_dispersion": centralities = {(s, t): nx.dispersion(nx_g, u=s, v=t) for (s, t) in g.edges()} elif method == "neg_dispersion": centralities = {(s, t): -nx.dispersion(nx_g, u=s, v=t) for (s, t) in g.edges()} elif method == "degree": dgs = nx_g.degree() centralities = {(s, t): dgs[s] + dgs[t] for (s, t) in g.edges()} elif method == "sum_neighb_degree": dgs = nx_g.degree() all_neighbors = { (s, t): list(set(list(nx_g.neighbors(s)) + list(nx_g.neighbors(t)))) for (s, t) in g.edges() } centralities = {(s, t): sum([dgs[n] for n in ns]) for ((s, t), ns) in all_neighbors.items()} elif method == "avg_neighb_degree": dgs = nx_g.degree() all_neighbors = { (s, t): list(set(list(nx_g.neighbors(s)) + list(nx_g.neighbors(t)))) for (s, t) in g.edges() } centralities = {(s, t): np.mean([dgs[n] for n in ns]) for ((s, t), ns) in all_neighbors.items()} else: raise RuntimeError(f"[get_central_edges] Unrecognized method {method}") allowed_centralities = {e: centralities[e] for e in allowed} sum_allowed = sum(allowed_centralities.values()) weights = [float(v) / sum_allowed for v in allowed_centralities.values()] # ranked_edges = sorted(allowed_centralities.items(), key=lambda item: -item[1]) # top_n = [k for (k, _) in ranked_edges[:n]] # return top_n return weights
def test_p4_edge_load(self): G = self.P4 c = nx.edge_load_centrality(G) d = {(0, 1): 6.000, (1, 2): 8.000, (2, 3): 6.000} for n in G.edges(): assert c[n] == pytest.approx(d[n], abs=1e-3)
def set_edge_weight(self, edge_weight_method='weight'): if edge_weight_method == 'weight': return # Centrality based methods elif edge_weight_method == 'edge_betweenness_centrality': print("comptuing edge_betweenness_centrality..") C = nx.edge_betweenness_centrality(self.G, weight='weight') print("done!") elif edge_weight_method == 'edge_betweenness_centrality_subset': print("comptuing edge_betweenness_centrality_subset..") C = nx.edge_current_flow_betweenness_centrality(self.G, weight='weight') print('done') elif edge_weight_method == 'edge_current_flow_betweenness_centrality_subset': print( "comptuing edge_current_flow_betweenness_centrality_subset..") C = nx.edge_current_flow_betweenness_centrality_subset( self.G, weight='weight') print('done') elif edge_weight_method == 'edge_load_centrality': print("comptuing edge_load_centrality..") C = nx.edge_load_centrality(self.G) print('done!') # Link Prediction based methods elif edge_weight_method == 'adamic_adar_index': print("comptuing adamic_adar_index ..") preds = nx.adamic_adar_index(self.G, self.G.edges()) C = {} for u, v, p in preds: C[(u, v)] = p elif edge_weight_method == 'ra_index_soundarajan_hopcroft': print("comptuing ra_index_soundarajan_hopcroft ..") preds = nx.ra_index_soundarajan_hopcroft(self.G, self.G.edges()) C = {} for u, v, p in preds: C[(u, v)] = p elif edge_weight_method == 'preferential_attachment': print("comptuing preferential_attachment ..") preds = nx.preferential_attachment(self.G, self.G.edges()) C = {} for u, v, p in preds: C[(u, v)] = p #elif edge_weight_method=='cn_soundarajan_hopcroft': # print("comptuing cn_soundarajan_hopcroft ..") # preds=nx.cn_soundarajan_hopcroft(self.G,self.G.edges()) # C={} # for u, v, p in preds: # C[(u,v)]=p elif edge_weight_method == 'within_inter_cluster': print("comptuing within_inter_cluster ..") preds = nx.within_inter_cluster(self.G, self.G.edges()) C = {} for u, v, p in preds: C[(u, v)] = p elif edge_weight_method == 'resource_allocation_index': print("comptuing resource allocation index ..") preds = nx.resource_allocation_index(self.G, self.G.edges()) C = {} for u, v, p in preds: C[(u, v)] = p elif edge_weight_method == 'jaccard_coefficient': print("comptuing jaccard_coefficient..") preds = nx.jaccard_coefficient(self.G, self.G.edges()) C = {} for u, v, p in preds: C[(u, v)] = p print('done!') for u, v, d in self.G.edges(data=True): if edge_weight_method == None: d['weight'] = 1 else: d['weight'] = C[(u, v)] return 1
for i in range(n): if (str(int(t_val[i,0])),str(int(t_val[i,1]))) in edge_cu.keys(): t_val[i,6]=edge_cu[(str(int(t_val[i,0])),str(int(t_val[i,1])))] if (str(int(t_val[i,1])),str(int(t_val[i,0]))) in edge_cu.keys(): t_val[i,6]=edge_cu[(str(int(t_val[i,1])),str(int(t_val[i,0])))] sp_cu=stats.spearmanr(t_val[:,2],t_val[:,6]) print("edge_current_flow_betweenness_centrality",sp_cu) # In[100]: #########task 7 - edge_load_centrality edge_lo=nx.edge_load_centrality(net) for i in range(n): if (str(int(t_val[i,0])),str(int(t_val[i,1]))) in edge_lo.keys(): t_val[i,7]=edge_lo[(str(int(t_val[i,0])),str(int(t_val[i,1])))] if (str(int(t_val[i,1])),str(int(t_val[i,0]))) in edge_lo.keys(): t_val[i,7]=edge_lo[(str(int(t_val[i,1])),str(int(t_val[i,0])))] sp_lo=stats.spearmanr(t_val[:,2],t_val[:,7]) print("edge_load_centrality",sp_lo) # In[116]: #########task 7 - thresholded networks edges = net.edges.data()
def edge_load_centrality(G, scale): return remove_transition(G, nx.edge_load_centrality(G), scale)