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.eigenvector_centrality(G) for n in sorted(G): assert almost_equal(b[n], b_answer[n], places=4)
def test_pagerank(self): G = self.G p = nx.pagerank(G, alpha=0.9, tol=1.0e-08) for n in G: assert almost_equal(p[n], G.pagerank[n], places=4) nstart = dict((n, random.random()) for n in G) p = nx.pagerank(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_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): """Katz centrality: K5""" G = nx.complete_graph(5) alpha = 0.1 b = nx.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]) nstart = dict([(n, 1) for n in G]) b = nx.katz_centrality(G, alpha, nstart=nstart) for n in sorted(G): assert almost_equal(b[n], b_answer[n])
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_K5(self): """Eigenvector centrality: K5""" G = nx.complete_graph(5) b = nx.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(G, nstart=nstart) for n in sorted(G): assert almost_equal(b[n], b_answer[n]) b = nx.eigenvector_centrality_numpy(G) for n in sorted(G): assert almost_equal(b[n], b_answer[n], places=3)
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_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_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_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.katz_centrality(G, alpha) 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(G, alpha, beta) for n in sorted(G): assert almost_equal(b[n], b_answer[n], places=4)
def test_incomplete_personalization(self): G = nx.complete_graph(4) personalize = {3: 1} answer = { 0: 0.22077931820379187, 1: 0.22077931820379187, 2: 0.22077931820379187, 3: 0.3376620453886241, } p = nx.pagerank(G, alpha=0.85, personalization=personalize) for n in G: assert almost_equal(p[n], answer[n], places=4)
def test_personalization(self): G = nx.complete_graph(4) personalize = {0: 1, 1: 1, 2: 4, 3: 4} answer = { 0: 0.23246732615667579, 1: 0.23246732615667579, 2: 0.267532673843324, 3: 0.2675326738433241, } p = nx.pagerank(G, alpha=0.85, personalization=personalize) for n in G: assert almost_equal(p[n], answer[n], places=4)
def test_outdegree_centrality(self): d = nx.out_degree_centrality(self.G) exact = { 0: 0.125, 1: 0.125, 2: 0.125, 3: 0.125, 4: 0.125, 5: 0.375, 6: 0.0, 7: 0.0, 8: 0.0, } for n, dc in d.items(): assert almost_equal(exact[n], dc)
def test_indegree_centrality(self): d = nx.in_degree_centrality(self.G) exact = { 0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.625, 6: 0.125, 7: 0.125, 8: 0.125, } for n, dc in d.items(): assert almost_equal(exact[n], dc)
def test_degree_centrality_3(self): d = nx.degree_centrality(self.K) exact = { 0: 0.444, 1: 0.444, 2: 0.333, 3: 0.667, 4: 0.333, 5: 0.556, 6: 0.556, 7: 0.333, 8: 0.222, 9: 0.111, } for n, dc in d.items(): assert almost_equal(exact[n], float("%5.3f" % dc))
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_degree_centrality_4(self): d = nx.degree_centrality(self.F) names = sorted(self.F.nodes()) dcs = [ 0.071, 0.214, 0.143, 0.214, 0.214, 0.071, 0.286, 0.071, 0.429, 0.071, 0.214, 0.214, 0.143, 0.286, 0.214, ] exact = dict(zip(names, dcs)) for n, dc in d.items(): assert almost_equal(exact[n], float("%5.3f" % dc))
def test_eigenvector_centrality_weighted(self): G = self.G p = nx.eigenvector_centrality(G) for (a, b) in zip(list(p.values()), self.G.evc): assert almost_equal(a, b, places=4)
def test_degree_centrality_2(self): d = nx.degree_centrality(self.P3) exact = {0: 0.5, 1: 1, 2: 0.5} for n, dc in d.items(): assert almost_equal(exact[n], dc)
def test_degree_centrality_1(self): d = nx.degree_centrality(self.K5) exact = dict(zip(range(5), [1] * 5)) for n, dc in d.items(): assert almost_equal(exact[n], dc)
def test_dangling_scipy_pagerank(self): pr = nx.pagerank_scipy(self.G, dangling=self.dangling_edges) for n in self.G: assert almost_equal(pr[n], self.G.dangling_pagerank[n], places=4)
def assert_result_almost_equal(self, r1, r2): assert len(r1) == len(r2) for k in r1.keys(): assert almost_equal(r1[k], r2[k])
def test_eigenvector_centrality_unweighted_numpy(self): G = self.H p = nx.eigenvector_centrality_numpy(G) for (a, b) in zip(list(p.values()), self.G.evc): assert almost_equal(a, b)
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_katz_centrality_weighted(self): G = self.G alpha = self.G.alpha p = nx.katz_centrality(G, alpha, weight="weight") for (a, b) in zip(list(p.values()), self.G.evc): assert almost_equal(a, b)