def test_hits(self): G = self.G h, a = nx.hits(G, tol=1.0e-08) for n in G: assert almost_equal(h[n], G.h[n], places=4) for n in G: assert almost_equal(a[n], G.a[n], places=4)
def test_hits_numpy(self): numpy = pytest.importorskip("numpy") G = self.G h, a = nx.hits_numpy(G) for n in G: assert almost_equal(h[n], G.h[n], places=4) for n in G: assert almost_equal(a[n], G.a[n], places=4)
def test_hits_scipy(self): sp = pytest.importorskip("scipy") G = self.G h, a = nx.hits_scipy(G, tol=1.0e-08) for n in G: assert almost_equal(h[n], G.h[n], places=4) for n in G: assert almost_equal(a[n], G.a[n], places=4)
def test_P3(self): """Eigenvector centrality: P3""" G = nx.path_graph(3) b_answer = {0: 0.5, 1: 0.7071, 2: 0.5} b = nx.eigenvector_centrality_numpy(G) for n in sorted(G): assert almost_equal(b[n], b_answer[n], places=4) b = nx.builtin.eigenvector_centrality(G) for n in sorted(G): assert almost_equal(b[n], b_answer[n], places=4)
def test_K5_unweighted(self): """Katz centrality: K5""" G = nx.complete_graph(5) alpha = 0.1 b = nx.katz_centrality(G, alpha, weight=None) v = math.sqrt(1 / 5.0) b_answer = dict.fromkeys(G, v) for n in sorted(G): assert almost_equal(b[n], b_answer[n]) nstart = dict([(n, 1) for n in G]) b = nx.eigenvector_centrality_numpy(G, weight=None) for n in sorted(G): assert almost_equal(b[n], b_answer[n], places=3)
def test_K5(self): """Eigenvector centrality: K5""" G = nx.complete_graph(5) b = nx.builtin.eigenvector_centrality(G) v = math.sqrt(1 / 5.0) b_answer = dict.fromkeys(G, v) for n in sorted(G): assert almost_equal(b[n], b_answer[n]) nstart = dict([(n, 1) for n in G]) b = nx.eigenvector_centrality_numpy(G) for n in sorted(G): assert almost_equal(b[n], b_answer[n], places=3)
def test_scipy_pagerank(self): G = self.G p = nx.pagerank_scipy(G, alpha=0.9, tol=1.0e-08) for n in G: assert almost_equal(p[n], G.pagerank[n], places=4) personalize = dict((n, random.random()) for n in G) p = nx.pagerank_scipy(G, alpha=0.9, tol=1.0e-08, personalization=personalize) nstart = dict((n, random.random()) for n in G) p = nx.pagerank_scipy(G, alpha=0.9, tol=1.0e-08, nstart=nstart) for n in G: assert almost_equal(p[n], G.pagerank[n], places=4)
def test_normalized_weighted_graph(self): eList = [ (0, 1, 5), (0, 2, 4), (0, 3, 3), (0, 4, 2), (1, 2, 4), (1, 3, 1), (1, 4, 3), (2, 4, 5), (3, 4, 4), ] G = nx.Graph() G.add_weighted_edges_from(eList) b = nx.edge_betweenness_centrality(G, weight="weight", normalized=True) b_answer = { (0, 1): 0.0, (0, 2): 1.0, (0, 3): 2.0, (0, 4): 1.0, (1, 2): 2.0, (1, 3): 3.5, (1, 4): 1.5, (2, 4): 1.0, (3, 4): 0.5, } norm = len(G) * (len(G) - 1) / 2 for n in sorted(G.edges()): assert almost_equal(b[n], b_answer[n])
def test_normalized_P4(self): """Edge betweenness centrality: P4""" G = nx.path_graph(4) b = nx.edge_betweenness_centrality(G, weight=None, normalized=True) b_answer = {(0, 1): 3, (1, 2): 4, (2, 3): 3} for n in sorted(G.edges()): assert almost_equal(b[n], b_answer[n])
def test_C4(self): """Edge betweenness centrality: C4""" G = nx.cycle_graph(4) b = nx.edge_betweenness_centrality(G, weight=None, normalized=True) b_answer = {(0, 1): 2, (0, 3): 2, (1, 2): 2, (2, 3): 2} for n in sorted(G.edges()): assert almost_equal(b[n], b_answer[n])
def test_normalized_K5(self): """Edge betweenness centrality: K5""" G = nx.complete_graph(5) b = nx.edge_betweenness_centrality(G, weight=None, normalized=True) b_answer = dict.fromkeys(G.edges(), 1 / 10) for n in sorted(G.edges()): assert almost_equal(b[n], b_answer[n])
def test_K5(self): """Betweenness centrality: K5""" G = nx.complete_graph(5) b = nx.builtin.betweenness_centrality(G, weight=None, normalized=False) b_answer = {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0} for n in sorted(G): assert almost_equal(b[n], b_answer[n])
def test_florentine_families_graph(self): """Weighted betweenness centrality: Florentine families graph""" G = nx.florentine_families_graph() for e in G.edges: G.edges[e]["weight"] = 1 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, } b = nx.builtin.betweenness_centrality(G, weight="weight", normalized=True) for n in sorted(G): assert almost_equal(b[n], b_answer[n], 3)
def test_krackhardt_kite_graph_normalized(self): """Weighted betweenness centrality: Krackhardt kite graph normalized """ G = nx.krackhardt_kite_graph() G = nx.Graph(G) for e in G.edges: G.edges[e]["weight"] = 1 b_answer = { 0: 0.023, 1: 0.023, 2: 0.000, 3: 0.102, 4: 0.000, 5: 0.231, 6: 0.231, 7: 0.389, 8: 0.222, 9: 0.000, } b = nx.builtin.betweenness_centrality(G, weight="weight", normalized=True) for n in sorted(G): assert almost_equal(b[n], b_answer[n], 3)
def test_krackhardt_kite_graph(self): """Weighted betweenness centrality: Krackhardt kite graph""" G = nx.krackhardt_kite_graph() G = nx.Graph(G) for e in G.edges: G.edges[e]["weight"] = 1 b_answer = { 0: 1.667, 1: 1.667, 2: 0.000, 3: 7.333, 4: 0.000, 5: 16.667, 6: 16.667, 7: 28.000, 8: 16.000, 9: 0.000, } for b in b_answer: b_answer[b] /= 2 b = nx.builtin.betweenness_centrality(G, weight="weight", normalized=False) for n in sorted(G): assert almost_equal(b[n], b_answer[n], 3)
def test_P3(self): """Betweenness centrality: P3""" G = nx.path_graph(3) b_answer = {0: 0.0, 1: 1.0, 2: 0.0} b = nx.builtin.betweenness_centrality(G, weight=None, normalized=False) for n in sorted(G): assert almost_equal(b[n], b_answer[n])
def test_google_matrix(self): G = self.G M = nx.google_matrix(G, alpha=0.9, nodelist=sorted(G)) e, ev = numpy.linalg.eig(M.T) p = numpy.array(ev[:, 0] / ev[:, 0].sum())[:, 0] for (a, b) in zip(p, self.G.pagerank.values()): assert almost_equal(a, b)
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_numpy_pagerank(self): G = self.G p = nx.pagerank_numpy(G, alpha=0.9) for n in G: assert almost_equal(p[n], G.pagerank[n], places=4) personalize = dict((n, random.random()) for n in G) p = nx.pagerank_numpy(G, alpha=0.9, personalization=personalize)
def test_directed_path_normalized(self): """Betweenness centrality: directed path normalized""" G = nx.DiGraph() nx.add_path(G, [0, 1, 2]) b = nx.builtin.betweenness_centrality(G, weight=None, normalized=True) b_answer = {0: 0.0, 1: 0.5, 2: 0.0} for n in sorted(G): assert almost_equal(b[n], b_answer[n])
def test_P3_endpoints(self): """Betweenness centrality: P3 endpoints""" G = nx.path_graph(3) b_answer = {0: 2.0, 1: 3.0, 2: 2.0} b = nx.builtin.betweenness_centrality(G, weight=None, normalized=False, endpoints=True) for n in sorted(G): assert almost_equal(b[n], b_answer[n]) # normalized = True case b_answer = {0: 2 / 3, 1: 1.0, 2: 2 / 3} b = nx.builtin.betweenness_centrality(G, weight=None, normalized=True, endpoints=True) for n in sorted(G): assert almost_equal(b[n], b_answer[n])
def test_disconnected_path(self): """Betweenness centrality: disconnected path""" G = nx.Graph() nx.add_path(G, [0, 1, 2]) nx.add_path(G, [3, 4, 5, 6]) b_answer = {0: 0, 1: 1, 2: 0, 3: 0, 4: 2, 5: 2, 6: 0} b = nx.builtin.betweenness_centrality(G, weight=None, normalized=False) for n in sorted(G): assert almost_equal(b[n], b_answer[n])
def test_K5(self): """Katz centrality: K5""" G = nx.complete_graph(5) alpha = 0.1 b = nx.builtin.katz_centrality(G, alpha) v = math.sqrt(1 / 5.0) b_answer = dict.fromkeys(G, v) for n in sorted(G): assert almost_equal(b[n], b_answer[n])
def test_dangling_matrix(self): """ Tests that the google_matrix doesn't change except for the dangling nodes. """ G = self.G dangling = self.dangling_edges dangling_sum = float(sum(dangling.values())) M1 = nx.google_matrix(G, personalization=dangling) M2 = nx.google_matrix(G, personalization=dangling, dangling=dangling) for i in range(len(G)): for j in range(len(G)): if i == self.dangling_node_index and (j + 1) in dangling: assert almost_equal(M2[i, j], dangling[j + 1] / dangling_sum, places=4) else: assert almost_equal(M2[i, j], M1[i, j], places=4)
def test_K5_endpoints(self): """Betweenness centrality: K5 endpoints""" G = nx.complete_graph(5) b = nx.builtin.betweenness_centrality(G, weight=None, normalized=False, endpoints=True) b_answer = {0: 4.0, 1: 4.0, 2: 4.0, 3: 4.0, 4: 4.0} for n in sorted(G): assert almost_equal(b[n], b_answer[n]) # normalized = True case b = nx.builtin.betweenness_centrality(G, weight=None, normalized=True, endpoints=True) b_answer = {0: 0.4, 1: 0.4, 2: 0.4, 3: 0.4, 4: 0.4} for n in sorted(G): assert almost_equal(b[n], b_answer[n])
def test_G(self): """Weighted betweenness centrality: G""" G = weighted_G() b_answer = {0: 2.0, 1: 0.0, 2: 4.0, 3: 3.0, 4: 4.0, 5: 0.0} b = nx.builtin.betweenness_centrality(G, weight="weight", normalized=False) for n in sorted(G): assert almost_equal(b[n], b_answer[n])
def test_disconnected_path_endpoints(self): """Betweenness centrality: disconnected path endpoints""" G = nx.Graph() nx.add_path(G, [0, 1, 2]) nx.add_path(G, [3, 4, 5, 6]) b_answer = {0: 2, 1: 3, 2: 2, 3: 3, 4: 5, 5: 5, 6: 3} b = nx.builtin.betweenness_centrality(G, weight=None, normalized=False, endpoints=True) for n in sorted(G): assert almost_equal(b[n], b_answer[n]) # normalized = True case b = nx.builtin.betweenness_centrality(G, weight=None, normalized=True, endpoints=True) for n in sorted(G): assert almost_equal(b[n], b_answer[n] / 21)
def test_ladder_graph(self): """Betweenness centrality: Ladder graph""" G = nx.Graph() # ladder_graph(3) G.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3), (2, 4), (4, 5), (3, 5)]) b_answer = {0: 1.667, 1: 1.667, 2: 6.667, 3: 6.667, 4: 1.667, 5: 1.667} for b in b_answer: b_answer[b] /= 2 b = nx.builtin.betweenness_centrality(G, weight=None, normalized=False) for n in sorted(G): assert almost_equal(b[n], b_answer[n], 3)
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.builtin.katz_centrality(G, alpha, beta) for n in sorted(G): assert almost_equal(b[n], b_answer[n], places=4)
def test_P3(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.builtin.katz_centrality(G, alpha) for n in sorted(G): assert almost_equal(b[n], b_answer[n], places=4)