def test_K4(self): """Betweenness centrality: K4""" G=networkx.complete_graph(4) b=networkx.current_flow_betweenness_centrality_subset(G, list(G), list(G), normalized=True) b_answer=networkx.current_flow_betweenness_centrality(G,normalized=True) for n in sorted(G): assert_almost_equal(b[n],b_answer[n]) # test weighted network G.add_edge(0,1,{'weight':0.5,'other':0.3}) b=networkx.current_flow_betweenness_centrality_subset(G, list(G), list(G), normalized=True, weight=None) for n in sorted(G): assert_almost_equal(b[n],b_answer[n]) b=networkx.current_flow_betweenness_centrality_subset(G, list(G), list(G), normalized=True) b_answer=networkx.current_flow_betweenness_centrality(G,normalized=True) for n in sorted(G): assert_almost_equal(b[n],b_answer[n]) b=networkx.current_flow_betweenness_centrality_subset(G, list(G), list(G), normalized=True, weight='other') b_answer=networkx.current_flow_betweenness_centrality(G,normalized=True,weight='other') for n in sorted(G): assert_almost_equal(b[n],b_answer[n])
def getNodeSpecBetweenness(): global outfile global data global sources print sources global sinks print sinks weight = [] G = nx.from_numpy_matrix(data) for i in xrange(len(data)): for j in xrange(i, len(data)): if (data[i, j] != 0): weight.append(data[i, j]) net = nx.current_flow_betweenness_centrality_subset(G, sources=sources, targets=sinks, normalized=False, weight="weight") f = open(outfile, 'w') with open(outfile, 'w') as f: for key, value in net.items(): if value > 0.0: f.write('%s %s\n' % (key, value)) root = os.path.splitext(outfile)[0] outfile2 = root + '_res_VMD.dat' f2 = open(outfile2, 'w') with open(outfile2, 'w') as f2: for key, value in net.items(): if value > 0.0: f2.write('%s ' % key)
def test_P4(self): """Betweenness centrality: P4""" G = nx.path_graph(4) b = nx.current_flow_betweenness_centrality_subset(G, list(G), list(G), normalized=True) b_answer = nx.current_flow_betweenness_centrality(G, normalized=True) for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
def test_K4_normalized(self): """Betweenness centrality: K4""" G = networkx.complete_graph(4) b = networkx.current_flow_betweenness_centrality_subset(G, G.nodes(), G.nodes(), normalized=True) b_answer = networkx.current_flow_betweenness_centrality(G, normalized=True) for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
def test_star(self): """Betweenness centrality: star """ G = nx.Graph() nx.add_star(G, ["a", "b", "c", "d"]) b = nx.current_flow_betweenness_centrality_subset(G, list(G), list(G), normalized=True) b_answer = nx.current_flow_betweenness_centrality(G, normalized=True) for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
def get_wire_centrality(network, this_TimeStamp=0, mode = 'betweenness'): edgeList = network.connectivity.edge_list conMat = np.zeros((network.numOfWires, network.numOfWires)) conMat[edgeList[:,0], edgeList[:,1]] = network.junctionConductance[this_TimeStamp,:] conG = nx.from_numpy_array(conMat) if mode == 'betweenness': return np.array(list(nx.current_flow_betweenness_centrality_subset(conG, network.sources, network.drains, weight = 'weight').values())) elif mode == 'closeness': return np.array(list(nx.current_flow_closeness_centrality(conG, weight = 'weight').values()))
def test_P4(self): """Betweenness centrality: P4""" G = networkx.path_graph(4) b = networkx.current_flow_betweenness_centrality_subset( G, G.nodes(), G.nodes(), normalized=True) b_answer = networkx.current_flow_betweenness_centrality( G, normalized=True) for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
def test_P4_normalized(self): """Betweenness centrality: P4 normalized""" G = nx.path_graph(4) b = nx.current_flow_betweenness_centrality_subset(G, list(G), list(G), normalized=True) b_answer = nx.current_flow_betweenness_centrality(G, normalized=True) for n in sorted(G): assert almost_equal(b[n], b_answer[n])
def test_K4_normalized(self): """Betweenness centrality: K4""" G=networkx.complete_graph(4) b=networkx.current_flow_betweenness_centrality_subset(G, list(G), list(G), normalized=True) b_answer=networkx.current_flow_betweenness_centrality(G,normalized=True) for n in sorted(G): assert_almost_equal(b[n],b_answer[n])
def test_star(self): """Betweenness centrality: star """ G = networkx.Graph() G.add_star(['a', 'b', 'c', 'd']) b = networkx.current_flow_betweenness_centrality_subset( G, G.nodes(), G.nodes(), normalized=True) b_answer = networkx.current_flow_betweenness_centrality( G, normalized=True) for n in sorted(G): assert_almost_equal(b[n], b_answer[n])
def test_P4(self): """Betweenness centrality: P4""" G = nx.path_graph(4) b = nx.current_flow_betweenness_centrality_subset(G, list(G), list(G), normalized=True) b_answer = nx.current_flow_betweenness_centrality(G, normalized=True) for n in sorted(G): assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def test_star(self): """Betweenness centrality: star """ G = nx.Graph() nx.add_star(G, ["a", "b", "c", "d"]) b = nx.current_flow_betweenness_centrality_subset(G, list(G), list(G), normalized=True) b_answer = nx.current_flow_betweenness_centrality(G, normalized=True) for n in sorted(G): assert almost_equal(b[n], b_answer[n])
def test_star(self): """Betweenness centrality: star """ G=networkx.Graph() G.add_star(['a','b','c','d']) b=networkx.current_flow_betweenness_centrality_subset(G, G.nodes(), G.nodes(), normalized=True) b_answer=networkx.current_flow_betweenness_centrality(G,normalized=True) for n in sorted(G): assert_almost_equal(b[n],b_answer[n])
def test_star(self): """Betweenness centrality: star""" G = nx.Graph() nx.add_star(G, ["a", "b", "c", "d"]) b = nx.current_flow_betweenness_centrality_subset(G, list(G), list(G), normalized=True) b_answer = nx.current_flow_betweenness_centrality(G, normalized=True) for n in sorted(G): assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def test_K4(self): """Betweenness centrality: K4""" G = nx.complete_graph(4) b = nx.current_flow_betweenness_centrality_subset(G, list(G), list(G), normalized=True) b_answer = nx.current_flow_betweenness_centrality(G, normalized=True) for n in sorted(G): assert b[n] == pytest.approx(b_answer[n], abs=1e-7) # test weighted network G.add_edge(0, 1, weight=0.5, other=0.3) b = nx.current_flow_betweenness_centrality_subset(G, list(G), list(G), normalized=True, weight=None) for n in sorted(G): assert b[n] == pytest.approx(b_answer[n], abs=1e-7) b = nx.current_flow_betweenness_centrality_subset(G, list(G), list(G), normalized=True) b_answer = nx.current_flow_betweenness_centrality(G, normalized=True) for n in sorted(G): assert b[n] == pytest.approx(b_answer[n], abs=1e-7) b = nx.current_flow_betweenness_centrality_subset(G, list(G), list(G), normalized=True, weight="other") b_answer = nx.current_flow_betweenness_centrality(G, normalized=True, weight="other") for n in sorted(G): assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def compute_subgraph_center(self, subgraph): if self.method == 'betweenness_centrality': d = nx.betweenness_centrality(subgraph, weight='weight') center = max(d, key=d.get) elif self.method == 'betweenness_centrality_subset': d = nx.betweenness_centrality_subset(subgraph, weight='weight') center = max(d, key=d.get) elif self.method == 'information_centrality': d = nx.information_centrality(subgraph, weight='weight') center = max(d, key=d.get) elif self.method == 'local_reaching_centrality': d = {} for n in self.G.nodes(): d[n] = nx.local_reaching_centrality(self.G, n, weight='weight') center = max(d, key=d.get) elif self.method == 'voterank': d = nx.voterank(subgraph) center = max(d, key=d.get) elif self.method == 'percolation_centrality': d = nx.percolation_centrality(subgraph, weight='weight') center = max(d, key=d.get) elif self.method == 'subgraph_centrality': d = nx.subgraph_centrality(subgraph) center = max(d, key=d.get) elif self.method == 'subgraph_centrality_exp': d = nx.subgraph_centrality_exp(subgraph) center = max(d, key=d.get) elif self.method == 'estrada_index': d = nx.estrada_index(subgraph) center = max(d, key=d.get) elif self.method == 'second_order_centrality': d = nx.second_order_centrality(subgraph) center = max(d, key=d.get) elif self.method == 'eigenvector_centrality': d = nx.eigenvector_centrality(subgraph, weight='weight') center = max(d, key=d.get) elif self.method == 'load_centrality': d = nx.load_centrality(subgraph, weight='weight') center = max(d, key=d.get) elif self.method == 'closeness_centrality': d = nx.closeness_centrality(subgraph) center = max(d, key=d.get) elif self.method == 'current_flow_closeness_centrality': d = nx.current_flow_closeness_centrality(subgraph, weight='weight') center = max(d, key=d.get) elif self.method == 'current_flow_betweenness_centrality': d = nx.current_flow_betweenness_centrality(subgraph, weight='weight') center = max(d, key=d.get) elif self.method == 'current_flow_betweenness_centrality_subset': d = nx.current_flow_betweenness_centrality_subset(subgraph, weight='weight') center = max(d, key=d.get) elif self.method == 'approximate_current_flow_betweenness_centrality': d = nx.approximate_current_flow_betweenness_centrality( subgraph, weight='weight') center = max(d, key=d.get) elif self.method == 'harmonic_centrality': d = nx.harmonic_centrality(subgraph) center = max(d, key=d.get) elif self.method == 'page_rank': d = nx.pagerank(subgraph, weight='weight') center = max(d, key=d.get) elif self.method == 'hits': d = nx.hits(subgraph) center = max(d, key=d.get) elif self.method == 'katz_centrality': d = nx.katz_centrality(subgraph, weight='weight') center = max(d, key=d.get) else: new_centers = nx.center(subgraph) # new_centers gives a list of centers and here we just pick one randomly --not good for stability # to do : find a better way to choose the center--make it stable index = random.randint(0, len(new_centers) - 1) center = new_centers[index] return center