def test_converge_to_betweenness(self): """percolation centrality: should converge to betweenness centrality when all nodes are percolated the same""" # taken from betweenness test test_florentine_families_graph G = nx.florentine_families_graph() b_answer = { "Acciaiuoli": 0.000, "Albizzi": 0.212, "Barbadori": 0.093, "Bischeri": 0.104, "Castellani": 0.055, "Ginori": 0.000, "Guadagni": 0.255, "Lamberteschi": 0.000, "Medici": 0.522, "Pazzi": 0.000, "Peruzzi": 0.022, "Ridolfi": 0.114, "Salviati": 0.143, "Strozzi": 0.103, "Tornabuoni": 0.092, } p_states = {k: 1.0 for k, v in b_answer.items()} p_answer = nx.percolation_centrality(G, states=p_states) for n in sorted(G): assert almost_equal(p_answer[n], b_answer[n], places=3) p_states = {k: 0.3 for k, v in b_answer.items()} p_answer = nx.percolation_centrality(G, states=p_states) for n in sorted(G): assert almost_equal(p_answer[n], b_answer[n], places=3)
def test_converge_to_betweenness(self): """percolation centrality: should converge to betweenness centrality when all nodes are percolated the same""" # taken from betweenness test test_florentine_families_graph G = nx.florentine_families_graph() b_answer =\ {'Acciaiuoli': 0.000, 'Albizzi': 0.212, 'Barbadori': 0.093, 'Bischeri': 0.104, 'Castellani': 0.055, 'Ginori': 0.000, 'Guadagni': 0.255, 'Lamberteschi': 0.000, 'Medici': 0.522, 'Pazzi': 0.000, 'Peruzzi': 0.022, 'Ridolfi': 0.114, 'Salviati': 0.143, 'Strozzi': 0.103, 'Tornabuoni': 0.092} p_states = {k: 1.0 for k, v in b_answer.items()} p_answer = nx.percolation_centrality(G, states=p_states) for n in sorted(G): assert_almost_equal(p_answer[n], b_answer[n], places=3) p_states = {k: 0.3 for k, v in b_answer.items()} p_answer = nx.percolation_centrality(G, states=p_states) for n in sorted(G): assert_almost_equal(p_answer[n], b_answer[n], places=3)
def test_percolation_example1b(self): """percolation centrality: example 1a""" G = example1b_G() p = nx.percolation_centrality(G) p_answer = {4: 0.825, 6: 0.4} for n in p_answer: assert almost_equal(p[n], p_answer[n], places=3)
def test_percolation_example1b(self): """percolation centrality: example 1a""" G = example1b_G() p = nx.percolation_centrality(G) p_answer = {4: 0.825, 6: 0.4} for n in p_answer: assert p[n] == pytest.approx(p_answer[n], abs=1e-3)
def test_percolation_example1b(self): """percolation centrality: example 1a""" G = example1b_G() p = nx.percolation_centrality(G) p_answer = {4: 0.825, 6: 0.4} for n in p_answer: assert_almost_equal(p[n], p_answer[n], places=3)
def percolation(G): balldict = {} for node in G.nodes: balldict[node] = G.nodes[node]['superUrn'].Um[0] centrality = nx.percolation_centrality(G, states=balldict) return centrality
def Centrality(G, N=10, method='katz', outliers=False, Label=True, layOut='shells'): if method.lower() == 'katz': phi = 1.618033988749895 # largest eigenvalue of adj matrix ranking = nx.katz_centrality_numpy(G, 1 / phi) elif method.lower() == 'degree': ranking = nx.degree_centrality(G) elif method.lower() == 'eigen': ranking = nx.eigenvector_centrality_numpy(G) elif method.lower() == 'closeness': ranking = nx.closeness_centrality(G) elif method.lower() == 'betweeness': ranking = nx.betweenness_centrality(G) elif method.lower() == 'harmonic': ranking = nx.harmonic_centrality(G) elif method.lower() == 'percolation': ranking = nx.percolation_centrality(G) else: print('Error, Unsupported Method.') return None important_nodes = sorted(ranking.items(), key=operator.itemgetter(1))[::-1] #[0:Nimportant] data = np.array([n[1] for n in important_nodes]) dnodes = [n[0] for n in important_nodes][:N] if outliers: m = 1 # 1 standard Deviation CI data = data[:N] out = len(data[abs(data - np.mean(data)) > m * np.std(data)]) # outlier within m stDev interval if out < N: dnodes = [n for n in dnodes[:out]] print('Influencial Users: {0}'.format(str(dnodes))) print('Influencial Users Scores: {0}'.format(str(data[:len(dnodes)]))) Gt = G.subgraph(dnodes) return Gt
def percolation_centrality(G): return nx.percolation_centrality(G)
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
def percolation_centrality(G): G2 = nx.percolation_centrality(G, attribute='percolation', states=None, weight=None) return G2