diff options
Diffstat (limited to 'networkx/algorithms/centrality/tests/test_katz_centrality.py')
-rw-r--r-- | networkx/algorithms/centrality/tests/test_katz_centrality.py | 43 |
1 files changed, 21 insertions, 22 deletions
diff --git a/networkx/algorithms/centrality/tests/test_katz_centrality.py b/networkx/algorithms/centrality/tests/test_katz_centrality.py index 97dc86bd..8f00df5f 100644 --- a/networkx/algorithms/centrality/tests/test_katz_centrality.py +++ b/networkx/algorithms/centrality/tests/test_katz_centrality.py @@ -1,8 +1,7 @@ import math -import networkx as nx -from networkx.testing import almost_equal import pytest +import networkx as nx class TestKatzCentrality: @@ -14,11 +13,11 @@ class TestKatzCentrality: 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]) + assert b[n] == pytest.approx(b_answer[n], abs=1e-7) nstart = {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]) + assert b[n] == pytest.approx(b_answer[n], abs=1e-7) def test_P3(self): """Katz centrality: P3""" @@ -27,7 +26,7 @@ class TestKatzCentrality: 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) + assert b[n] == pytest.approx(b_answer[n], abs=1e-4) def test_maxiter(self): with pytest.raises(nx.PowerIterationFailedConvergence): @@ -40,7 +39,7 @@ class TestKatzCentrality: 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) + assert b[n] == pytest.approx(b_answer[n], abs=1e-4) def test_beta_as_dict(self): alpha = 0.1 @@ -49,7 +48,7 @@ class TestKatzCentrality: 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) + assert b[n] == pytest.approx(b_answer[n], abs=1e-4) def test_multiple_alpha(self): alpha_list = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6] @@ -89,7 +88,7 @@ class TestKatzCentrality: G = nx.path_graph(3) b = nx.katz_centrality(G, alpha) for n in sorted(G): - assert almost_equal(b[n], b_answer[alpha][n], places=4) + assert b[n] == pytest.approx(b_answer[alpha][n], abs=1e-4) def test_multigraph(self): with pytest.raises(nx.NetworkXException): @@ -126,11 +125,11 @@ class TestKatzCentralityNumpy: 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]) + assert b[n] == pytest.approx(b_answer[n], abs=1e-7) nstart = {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) + assert b[n] == pytest.approx(b_answer[n], abs=1e-3) def test_P3(self): """Katz centrality: P3""" @@ -139,7 +138,7 @@ class TestKatzCentralityNumpy: b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449, 2: 0.5598852584152162} b = nx.katz_centrality_numpy(G, alpha) for n in sorted(G): - assert almost_equal(b[n], b_answer[n], places=4) + assert b[n] == pytest.approx(b_answer[n], abs=1e-4) def test_beta_as_scalar(self): alpha = 0.1 @@ -148,7 +147,7 @@ class TestKatzCentralityNumpy: 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) + assert b[n] == pytest.approx(b_answer[n], abs=1e-4) def test_beta_as_dict(self): alpha = 0.1 @@ -157,7 +156,7 @@ class TestKatzCentralityNumpy: 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) + assert b[n] == pytest.approx(b_answer[n], abs=1e-4) def test_multiple_alpha(self): alpha_list = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6] @@ -197,7 +196,7 @@ class TestKatzCentralityNumpy: 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) + assert b[n] == pytest.approx(b_answer[alpha][n], abs=1e-4) def test_multigraph(self): with pytest.raises(nx.NetworkXException): @@ -226,11 +225,11 @@ class TestKatzCentralityNumpy: 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]) + assert b[n] == pytest.approx(b_answer[n], abs=1e-7) nstart = {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) + assert b[n] == pytest.approx(b_answer[n], abs=1e-3) def test_P3_unweighted(self): """Katz centrality: P3""" @@ -239,7 +238,7 @@ class TestKatzCentralityNumpy: 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) + assert b[n] == pytest.approx(b_answer[n], abs=1e-4) class TestKatzCentralityDirected: @@ -298,14 +297,14 @@ class TestKatzCentralityDirected: 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) + assert a == pytest.approx(b, abs=1e-7) def test_katz_centrality_unweighted(self): H = self.H alpha = self.H.alpha p = nx.katz_centrality(H, alpha, weight="weight") for (a, b) in zip(list(p.values()), self.H.evc): - assert almost_equal(a, b) + assert a == pytest.approx(b, abs=1e-7) class TestKatzCentralityDirectedNumpy(TestKatzCentralityDirected): @@ -321,14 +320,14 @@ class TestKatzCentralityDirectedNumpy(TestKatzCentralityDirected): alpha = self.G.alpha p = nx.katz_centrality_numpy(G, alpha, weight="weight") for (a, b) in zip(list(p.values()), self.G.evc): - assert almost_equal(a, b) + assert a == pytest.approx(b, abs=1e-7) 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) + assert a == pytest.approx(b, abs=1e-7) class TestKatzEigenvectorVKatz: @@ -344,4 +343,4 @@ class TestKatzEigenvectorVKatz: 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]) + assert e[n] == pytest.approx(k[n], abs=1e-7) |