def test_eigenvector_v_katz_random(self): G = nx.gnp_random_graph(10, 0.5, seed=1234) l = float(max(eigvals(nx.adjacency_matrix(G).todense()))) e = nx.eigenvector_centrality_numpy(G) k = nx.katz_centrality_numpy(G, 1.0 / l) for n in G: assert almost_equal(e[n], k[n])
def test_P3_unweighted(self): """Katz centrality: P3""" alpha = 0.1 G = nx.path_graph(3) b_answer = { 0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162 } b = nx.katz_centrality_numpy(G, alpha, weight=None) for n in sorted(G): assert almost_equal(b[n], b_answer[n], places=4)
def test_beta_as_dict(self): alpha = 0.1 beta = {0: 1.0, 1: 1.0, 2: 1.0} b_answer = { 0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162 } G = nx.path_graph(3) b = nx.katz_centrality_numpy(G, alpha, beta) for n in sorted(G): assert almost_equal(b[n], b_answer[n], places=4)
def test_multiple_alpha(self): alpha_list = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6] for alpha in alpha_list: b_answer = { 0.1: { 0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162, }, 0.2: { 0: 0.5454545454545454, 1: 0.6363636363636365, 2: 0.5454545454545454, }, 0.3: { 0: 0.5333964609104419, 1: 0.6564879518897746, 2: 0.5333964609104419, }, 0.4: { 0: 0.5232045649263551, 1: 0.6726915834767423, 2: 0.5232045649263551, }, 0.5: { 0: 0.5144957746691622, 1: 0.6859943117075809, 2: 0.5144957746691622, }, 0.6: { 0: 0.5069794004195823, 1: 0.6970966755769258, 2: 0.5069794004195823, }, } G = nx.path_graph(3) b = nx.katz_centrality_numpy(G, alpha) for n in sorted(G): assert almost_equal(b[n], b_answer[alpha][n], places=4)
def test_katz_centrality_unweighted(self): H = self.H alpha = self.H.alpha p = nx.katz_centrality_numpy(H, alpha, weight="weight") for (a, b) in zip(list(p.values()), self.H.evc): assert almost_equal(a, b)
def test_bad_beta_numbe(self): with pytest.raises(nx.NetworkXException): G = nx.Graph([(0, 1)]) e = nx.katz_centrality_numpy(G, 0.1, beta="foo")
def test_bad_beta(self): with pytest.raises(nx.NetworkXException): G = nx.Graph([(0, 1)]) beta = {0: 77} e = nx.katz_centrality_numpy(G, 0.1, beta=beta)