summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJarrod Millman <jarrod.millman@gmail.com>2021-05-21 10:17:38 -0700
committerGitHub <noreply@github.com>2021-05-21 10:17:38 -0700
commite7a38bdc2874ec697ec28fc048a4d3204190c002 (patch)
tree538da844c2eeb2726774724169508a72d10e2a7d
parent1f1fb4245fd02cb414bfedd2880c7d85a02004ab (diff)
downloadnetworkx-e7a38bdc2874ec697ec28fc048a4d3204190c002.tar.gz
Use pytest.approx (#4827)
* Use pytest.approx * Deprecate almost_equal
-rw-r--r--doc/developer/deprecations.rst1
-rw-r--r--doc/release/release_dev.rst2
-rw-r--r--networkx/algorithms/assortativity/tests/test_connectivity.py5
-rw-r--r--networkx/algorithms/assortativity/tests/test_neighbor_degree.py4
-rw-r--r--networkx/algorithms/bipartite/tests/test_centrality.py8
-rw-r--r--networkx/algorithms/bipartite/tests/test_spectral_bipartivity.py29
-rw-r--r--networkx/algorithms/centrality/tests/test_betweenness_centrality.py82
-rw-r--r--networkx/algorithms/centrality/tests/test_betweenness_centrality_subset.py36
-rw-r--r--networkx/algorithms/centrality/tests/test_closeness_centrality.py21
-rw-r--r--networkx/algorithms/centrality/tests/test_current_flow_betweenness_centrality.py27
-rw-r--r--networkx/algorithms/centrality/tests/test_current_flow_betweenness_centrality_subset.py31
-rw-r--r--networkx/algorithms/centrality/tests/test_current_flow_closeness.py7
-rw-r--r--networkx/algorithms/centrality/tests/test_degree_centrality.py15
-rw-r--r--networkx/algorithms/centrality/tests/test_eigenvector_centrality.py21
-rw-r--r--networkx/algorithms/centrality/tests/test_harmonic_centrality.py24
-rw-r--r--networkx/algorithms/centrality/tests/test_katz_centrality.py43
-rw-r--r--networkx/algorithms/centrality/tests/test_load_centrality.py40
-rw-r--r--networkx/algorithms/centrality/tests/test_percolation_centrality.py10
-rw-r--r--networkx/algorithms/centrality/tests/test_reaching.py3
-rw-r--r--networkx/algorithms/centrality/tests/test_second_order_centrality.py7
-rw-r--r--networkx/algorithms/centrality/tests/test_subgraph.py13
-rw-r--r--networkx/algorithms/centrality/tests/test_trophic.py39
-rw-r--r--networkx/algorithms/community/tests/test_quality.py27
-rw-r--r--networkx/algorithms/flow/tests/test_maxflow_large_graph.py3
-rw-r--r--networkx/algorithms/link_analysis/tests/test_hits.py14
-rw-r--r--networkx/algorithms/link_analysis/tests/test_pagerank.py31
-rw-r--r--networkx/algorithms/shortest_paths/tests/test_generic.py21
-rw-r--r--networkx/algorithms/tests/test_boundary.py14
-rw-r--r--networkx/algorithms/tests/test_communicability.py7
-rw-r--r--networkx/algorithms/tests/test_link_prediction.py2
-rw-r--r--networkx/algorithms/tests/test_structuralholes.py56
-rw-r--r--networkx/algorithms/tests/test_threshold.py3
-rw-r--r--networkx/conftest.py3
-rw-r--r--networkx/drawing/tests/test_layout.py17
-rw-r--r--networkx/generators/tests/test_internet_as_graphs.py22
-rw-r--r--networkx/linalg/tests/test_algebraic_connectivity.py36
-rw-r--r--networkx/readwrite/tests/test_graphml.py5
-rw-r--r--networkx/testing/utils.py9
38 files changed, 364 insertions, 374 deletions
diff --git a/doc/developer/deprecations.rst b/doc/developer/deprecations.rst
index 5105bdd5..1b99e81e 100644
--- a/doc/developer/deprecations.rst
+++ b/doc/developer/deprecations.rst
@@ -85,3 +85,4 @@ Version 3.0
* In ``networkx.classes`` remove the ``ordered`` module and the four ``Ordered``
classes defined therein.
* In ``utils/decorators.py`` remove ``preserve_random_state``.
+* In ``testing/utils.py`` remove ``almost_equal``.
diff --git a/doc/release/release_dev.rst b/doc/release/release_dev.rst
index 11ee9f0e..fec41b3b 100644
--- a/doc/release/release_dev.rst
+++ b/doc/release/release_dev.rst
@@ -113,6 +113,8 @@ Deprecations
Deprecate the ``Ordered`` graph classes.
- [`#4826 <https://github.com/networkx/networkx/pull/4826>`_]
Deprecate ``preserve_random_state``.
+- [`#4827 <https://github.com/networkx/networkx/pull/4827>`_]
+ Deprecate ``almost_equal``.
Contributors
diff --git a/networkx/algorithms/assortativity/tests/test_connectivity.py b/networkx/algorithms/assortativity/tests/test_connectivity.py
index b1b0ac81..dd12d9bb 100644
--- a/networkx/algorithms/assortativity/tests/test_connectivity.py
+++ b/networkx/algorithms/assortativity/tests/test_connectivity.py
@@ -3,7 +3,6 @@ from itertools import permutations
import pytest
import networkx as nx
-from networkx.testing import almost_equal
class TestNeighborConnectivity:
@@ -86,9 +85,9 @@ class TestNeighborConnectivity:
nd = nx.average_degree_connectivity(G)[5]
assert nd == 1.8
nd = nx.average_degree_connectivity(G, weight="weight")[5]
- assert almost_equal(nd, 3.222222, places=5)
+ assert nd == pytest.approx(3.222222, abs=1e-5)
nd = nx.k_nearest_neighbors(G, weight="weight")[5]
- assert almost_equal(nd, 3.222222, places=5)
+ assert nd == pytest.approx(3.222222, abs=1e-5)
def test_zero_deg(self):
G = nx.DiGraph()
diff --git a/networkx/algorithms/assortativity/tests/test_neighbor_degree.py b/networkx/algorithms/assortativity/tests/test_neighbor_degree.py
index 51b4aa86..65827da1 100644
--- a/networkx/algorithms/assortativity/tests/test_neighbor_degree.py
+++ b/networkx/algorithms/assortativity/tests/test_neighbor_degree.py
@@ -1,5 +1,5 @@
+import pytest
import networkx as nx
-from networkx.testing import almost_equal
class TestAverageNeighbor:
@@ -73,4 +73,4 @@ class TestAverageNeighbor:
nd = nx.average_neighbor_degree(G)[5]
assert nd == 1.8
nd = nx.average_neighbor_degree(G, weight="weight")[5]
- assert almost_equal(nd, 3.222222, places=5)
+ assert nd == pytest.approx(3.222222, abs=1e-5)
diff --git a/networkx/algorithms/bipartite/tests/test_centrality.py b/networkx/algorithms/bipartite/tests/test_centrality.py
index 48a5abde..a9ad0b15 100644
--- a/networkx/algorithms/bipartite/tests/test_centrality.py
+++ b/networkx/algorithms/bipartite/tests/test_centrality.py
@@ -1,6 +1,6 @@
+import pytest
import networkx as nx
from networkx.algorithms import bipartite
-from networkx.testing import almost_equal
class TestBipartiteCentrality:
@@ -92,7 +92,7 @@ class TestBipartiteCentrality:
"E14": 0.17,
}
for node, value in answer.items():
- assert almost_equal(value, deg[node], places=2)
+ assert value == pytest.approx(deg[node], abs=1e-2)
def test_davis_betweenness_centrality(self):
G = self.davis
@@ -132,7 +132,7 @@ class TestBipartiteCentrality:
"E14": 0.00,
}
for node, value in answer.items():
- assert almost_equal(value, bet[node], places=2)
+ assert value == pytest.approx(bet[node], abs=1e-2)
def test_davis_closeness_centrality(self):
G = self.davis
@@ -172,4 +172,4 @@ class TestBipartiteCentrality:
"E14": 0.52,
}
for node, value in answer.items():
- assert almost_equal(value, clos[node], places=2)
+ assert value == pytest.approx(clos[node], abs=1e-2)
diff --git a/networkx/algorithms/bipartite/tests/test_spectral_bipartivity.py b/networkx/algorithms/bipartite/tests/test_spectral_bipartivity.py
index 16bab3c0..0cdc2d60 100644
--- a/networkx/algorithms/bipartite/tests/test_spectral_bipartivity.py
+++ b/networkx/algorithms/bipartite/tests/test_spectral_bipartivity.py
@@ -4,7 +4,6 @@ pytest.importorskip("scipy")
import networkx as nx
from networkx.algorithms.bipartite import spectral_bipartivity as sb
-from networkx.testing import almost_equal
# Examples from Figure 1
# E. Estrada and J. A. Rodríguez-Velázquez, "Spectral measures of
@@ -17,54 +16,54 @@ class TestSpectralBipartivity:
G = nx.star_graph(2)
G.add_edge(1, 2)
- assert almost_equal(sb(G), 0.843, places=3)
+ assert sb(G) == pytest.approx(0.843, abs=1e-3)
G = nx.star_graph(3)
G.add_edge(1, 2)
- assert almost_equal(sb(G), 0.871, places=3)
+ assert sb(G) == pytest.approx(0.871, abs=1e-3)
G = nx.star_graph(4)
G.add_edge(1, 2)
- assert almost_equal(sb(G), 0.890, places=3)
+ assert sb(G) == pytest.approx(0.890, abs=1e-3)
def test_k23_like(self):
# K2,3-like
G = nx.complete_bipartite_graph(2, 3)
G.add_edge(0, 1)
- assert almost_equal(sb(G), 0.769, places=3)
+ assert sb(G) == pytest.approx(0.769, abs=1e-3)
G = nx.complete_bipartite_graph(2, 3)
G.add_edge(2, 4)
- assert almost_equal(sb(G), 0.829, places=3)
+ assert sb(G) == pytest.approx(0.829, abs=1e-3)
G = nx.complete_bipartite_graph(2, 3)
G.add_edge(2, 4)
G.add_edge(3, 4)
- assert almost_equal(sb(G), 0.731, places=3)
+ assert sb(G) == pytest.approx(0.731, abs=1e-3)
G = nx.complete_bipartite_graph(2, 3)
G.add_edge(0, 1)
G.add_edge(2, 4)
- assert almost_equal(sb(G), 0.692, places=3)
+ assert sb(G) == pytest.approx(0.692, abs=1e-3)
G = nx.complete_bipartite_graph(2, 3)
G.add_edge(2, 4)
G.add_edge(3, 4)
G.add_edge(0, 1)
- assert almost_equal(sb(G), 0.645, places=3)
+ assert sb(G) == pytest.approx(0.645, abs=1e-3)
G = nx.complete_bipartite_graph(2, 3)
G.add_edge(2, 4)
G.add_edge(3, 4)
G.add_edge(2, 3)
- assert almost_equal(sb(G), 0.645, places=3)
+ assert sb(G) == pytest.approx(0.645, abs=1e-3)
G = nx.complete_bipartite_graph(2, 3)
G.add_edge(2, 4)
G.add_edge(3, 4)
G.add_edge(2, 3)
G.add_edge(0, 1)
- assert almost_equal(sb(G), 0.597, places=3)
+ assert sb(G) == pytest.approx(0.597, abs=1e-3)
def test_single_nodes(self):
@@ -72,11 +71,11 @@ class TestSpectralBipartivity:
G = nx.complete_bipartite_graph(2, 3)
G.add_edge(2, 4)
sbn = sb(G, nodes=[1, 2])
- assert almost_equal(sbn[1], 0.85, places=2)
- assert almost_equal(sbn[2], 0.77, places=2)
+ assert sbn[1] == pytest.approx(0.85, abs=1e-2)
+ assert sbn[2] == pytest.approx(0.77, abs=1e-2)
G = nx.complete_bipartite_graph(2, 3)
G.add_edge(0, 1)
sbn = sb(G, nodes=[1, 2])
- assert almost_equal(sbn[1], 0.73, places=2)
- assert almost_equal(sbn[2], 0.82, places=2)
+ assert sbn[1] == pytest.approx(0.73, abs=1e-2)
+ assert sbn[2] == pytest.approx(0.82, abs=1e-2)
diff --git a/networkx/algorithms/centrality/tests/test_betweenness_centrality.py b/networkx/algorithms/centrality/tests/test_betweenness_centrality.py
index f827427b..d563db91 100644
--- a/networkx/algorithms/centrality/tests/test_betweenness_centrality.py
+++ b/networkx/algorithms/centrality/tests/test_betweenness_centrality.py
@@ -1,5 +1,5 @@
+import pytest
import networkx as nx
-from networkx.testing import almost_equal
def weighted_G():
@@ -24,7 +24,7 @@ class TestBetweennessCentrality:
b = nx.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])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def test_K5_endpoints(self):
"""Betweenness centrality: K5 endpoints"""
@@ -32,12 +32,12 @@ class TestBetweennessCentrality:
b = nx.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])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
# normalized = True case
b = nx.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])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def test_P3_normalized(self):
"""Betweenness centrality: P3 normalized"""
@@ -45,7 +45,7 @@ class TestBetweennessCentrality:
b = nx.betweenness_centrality(G, weight=None, normalized=True)
b_answer = {0: 0.0, 1: 1.0, 2: 0.0}
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):
"""Betweenness centrality: P3"""
@@ -53,14 +53,14 @@ class TestBetweennessCentrality:
b_answer = {0: 0.0, 1: 1.0, 2: 0.0}
b = nx.betweenness_centrality(G, weight=None, normalized=False)
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_sample_from_P3(self):
G = nx.path_graph(3)
b_answer = {0: 0.0, 1: 1.0, 2: 0.0}
b = nx.betweenness_centrality(G, k=3, weight=None, normalized=False, seed=1)
for n in sorted(G):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
b = nx.betweenness_centrality(G, k=2, weight=None, normalized=False, seed=1)
# python versions give different results with same seed
b_approx1 = {0: 0.0, 1: 1.5, 2: 0.0}
@@ -74,12 +74,12 @@ class TestBetweennessCentrality:
b_answer = {0: 2.0, 1: 3.0, 2: 2.0}
b = nx.betweenness_centrality(G, weight=None, normalized=False, endpoints=True)
for n in sorted(G):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
# normalized = True case
b_answer = {0: 2 / 3, 1: 1.0, 2: 2 / 3}
b = nx.betweenness_centrality(G, weight=None, normalized=True, endpoints=True)
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_krackhardt_kite_graph(self):
"""Betweenness centrality: Krackhardt kite graph"""
@@ -100,7 +100,7 @@ class TestBetweennessCentrality:
b_answer[b] /= 2
b = nx.betweenness_centrality(G, weight=None, normalized=False)
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_krackhardt_kite_graph_normalized(self):
"""Betweenness centrality: Krackhardt kite graph normalized"""
@@ -119,7 +119,7 @@ class TestBetweennessCentrality:
}
b = nx.betweenness_centrality(G, weight=None, normalized=True)
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_florentine_families_graph(self):
"""Betweenness centrality: Florentine families graph"""
@@ -144,7 +144,7 @@ class TestBetweennessCentrality:
b = nx.betweenness_centrality(G, weight=None, normalized=True)
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_les_miserables_graph(self):
"""Betweenness centrality: Les Miserables graph"""
@@ -231,7 +231,7 @@ class TestBetweennessCentrality:
b = nx.betweenness_centrality(G, weight=None, normalized=True)
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_ladder_graph(self):
"""Betweenness centrality: Ladder graph"""
@@ -242,7 +242,7 @@ class TestBetweennessCentrality:
b_answer[b] /= 2
b = nx.betweenness_centrality(G, weight=None, normalized=False)
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_disconnected_path(self):
"""Betweenness centrality: disconnected path"""
@@ -252,7 +252,7 @@ class TestBetweennessCentrality:
b_answer = {0: 0, 1: 1, 2: 0, 3: 0, 4: 2, 5: 2, 6: 0}
b = nx.betweenness_centrality(G, weight=None, normalized=False)
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_disconnected_path_endpoints(self):
"""Betweenness centrality: disconnected path endpoints"""
@@ -262,11 +262,11 @@ class TestBetweennessCentrality:
b_answer = {0: 2, 1: 3, 2: 2, 3: 3, 4: 5, 5: 5, 6: 3}
b = nx.betweenness_centrality(G, weight=None, normalized=False, endpoints=True)
for n in sorted(G):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
# normalized = True case
b = nx.betweenness_centrality(G, weight=None, normalized=True, endpoints=True)
for n in sorted(G):
- assert almost_equal(b[n], b_answer[n] / 21)
+ assert b[n] == pytest.approx(b_answer[n] / 21, abs=1e-7)
def test_directed_path(self):
"""Betweenness centrality: directed path"""
@@ -275,7 +275,7 @@ class TestBetweennessCentrality:
b = nx.betweenness_centrality(G, weight=None, normalized=False)
b_answer = {0: 0.0, 1: 1.0, 2: 0.0}
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_directed_path_normalized(self):
"""Betweenness centrality: directed path normalized"""
@@ -284,7 +284,7 @@ class TestBetweennessCentrality:
b = nx.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])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
class TestWeightedBetweennessCentrality:
@@ -294,7 +294,7 @@ class TestWeightedBetweennessCentrality:
b = nx.betweenness_centrality(G, weight="weight", 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])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def test_P3_normalized(self):
"""Weighted betweenness centrality: P3 normalized"""
@@ -302,7 +302,7 @@ class TestWeightedBetweennessCentrality:
b = nx.betweenness_centrality(G, weight="weight", normalized=True)
b_answer = {0: 0.0, 1: 1.0, 2: 0.0}
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):
"""Weighted betweenness centrality: P3"""
@@ -310,7 +310,7 @@ class TestWeightedBetweennessCentrality:
b_answer = {0: 0.0, 1: 1.0, 2: 0.0}
b = nx.betweenness_centrality(G, weight="weight", normalized=False)
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_krackhardt_kite_graph(self):
"""Weighted betweenness centrality: Krackhardt kite graph"""
@@ -333,7 +333,7 @@ class TestWeightedBetweennessCentrality:
b = nx.betweenness_centrality(G, weight="weight", normalized=False)
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_krackhardt_kite_graph_normalized(self):
"""Weighted betweenness centrality:
@@ -355,7 +355,7 @@ class TestWeightedBetweennessCentrality:
b = nx.betweenness_centrality(G, weight="weight", normalized=True)
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_florentine_families_graph(self):
"""Weighted betweenness centrality:
@@ -381,7 +381,7 @@ class TestWeightedBetweennessCentrality:
b = nx.betweenness_centrality(G, weight="weight", normalized=True)
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_les_miserables_graph(self):
"""Weighted betweenness centrality: Les Miserables graph"""
@@ -468,7 +468,7 @@ class TestWeightedBetweennessCentrality:
b = nx.betweenness_centrality(G, weight="weight", normalized=True)
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_ladder_graph(self):
"""Weighted betweenness centrality: Ladder graph"""
@@ -479,7 +479,7 @@ class TestWeightedBetweennessCentrality:
b_answer[b] /= 2
b = nx.betweenness_centrality(G, weight="weight", normalized=False)
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_G(self):
"""Weighted betweenness centrality: G"""
@@ -487,7 +487,7 @@ class TestWeightedBetweennessCentrality:
b_answer = {0: 2.0, 1: 0.0, 2: 4.0, 3: 3.0, 4: 4.0, 5: 0.0}
b = nx.betweenness_centrality(G, weight="weight", normalized=False)
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_G2(self):
"""Weighted betweenness centrality: G2"""
@@ -511,7 +511,7 @@ class TestWeightedBetweennessCentrality:
b = nx.betweenness_centrality(G, weight="weight", normalized=False)
for n in sorted(G):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
class TestEdgeBetweennessCentrality:
@@ -521,7 +521,7 @@ class TestEdgeBetweennessCentrality:
b = nx.edge_betweenness_centrality(G, weight=None, normalized=False)
b_answer = dict.fromkeys(G.edges(), 1)
for n in sorted(G.edges()):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def test_normalized_K5(self):
"""Edge betweenness centrality: K5"""
@@ -529,7 +529,7 @@ class TestEdgeBetweennessCentrality:
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])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def test_C4(self):
"""Edge betweenness centrality: C4"""
@@ -537,7 +537,7 @@ class TestEdgeBetweennessCentrality:
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] / 6)
+ assert b[n] == pytest.approx(b_answer[n] / 6, abs=1e-7)
def test_P4(self):
"""Edge betweenness centrality: P4"""
@@ -545,7 +545,7 @@ class TestEdgeBetweennessCentrality:
b = nx.edge_betweenness_centrality(G, weight=None, normalized=False)
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])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def test_normalized_P4(self):
"""Edge betweenness centrality: P4"""
@@ -553,7 +553,7 @@ class TestEdgeBetweennessCentrality:
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] / 6)
+ assert b[n] == pytest.approx(b_answer[n] / 6, abs=1e-7)
def test_balanced_tree(self):
"""Edge betweenness centrality: balanced tree"""
@@ -561,7 +561,7 @@ class TestEdgeBetweennessCentrality:
b = nx.edge_betweenness_centrality(G, weight=None, normalized=False)
b_answer = {(0, 1): 12, (0, 2): 12, (1, 3): 6, (1, 4): 6, (2, 5): 6, (2, 6): 6}
for n in sorted(G.edges()):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
class TestWeightedEdgeBetweennessCentrality:
@@ -571,7 +571,7 @@ class TestWeightedEdgeBetweennessCentrality:
b = nx.edge_betweenness_centrality(G, weight="weight", normalized=False)
b_answer = dict.fromkeys(G.edges(), 1)
for n in sorted(G.edges()):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def test_C4(self):
"""Edge betweenness centrality: C4"""
@@ -579,7 +579,7 @@ class TestWeightedEdgeBetweennessCentrality:
b = nx.edge_betweenness_centrality(G, weight="weight", normalized=False)
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])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def test_P4(self):
"""Edge betweenness centrality: P4"""
@@ -587,7 +587,7 @@ class TestWeightedEdgeBetweennessCentrality:
b = nx.edge_betweenness_centrality(G, weight="weight", normalized=False)
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])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def test_balanced_tree(self):
"""Edge betweenness centrality: balanced tree"""
@@ -595,7 +595,7 @@ class TestWeightedEdgeBetweennessCentrality:
b = nx.edge_betweenness_centrality(G, weight="weight", normalized=False)
b_answer = {(0, 1): 12, (0, 2): 12, (1, 3): 6, (1, 4): 6, (2, 5): 6, (2, 6): 6}
for n in sorted(G.edges()):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def test_weighted_graph(self):
eList = [
@@ -624,7 +624,7 @@ class TestWeightedEdgeBetweennessCentrality:
(3, 4): 0.5,
}
for n in sorted(G.edges()):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def test_normalized_weighted_graph(self):
eList = [
@@ -654,4 +654,4 @@ class TestWeightedEdgeBetweennessCentrality:
}
norm = len(G) * (len(G) - 1) / 2
for n in sorted(G.edges()):
- assert almost_equal(b[n], b_answer[n] / norm)
+ assert b[n] == pytest.approx(b_answer[n] / norm, abs=1e-7)
diff --git a/networkx/algorithms/centrality/tests/test_betweenness_centrality_subset.py b/networkx/algorithms/centrality/tests/test_betweenness_centrality_subset.py
index 9c770fe0..58e44011 100644
--- a/networkx/algorithms/centrality/tests/test_betweenness_centrality_subset.py
+++ b/networkx/algorithms/centrality/tests/test_betweenness_centrality_subset.py
@@ -1,5 +1,5 @@
+import pytest
import networkx as nx
-from networkx.testing import almost_equal
class TestSubsetBetweennessCentrality:
@@ -11,7 +11,7 @@ class TestSubsetBetweennessCentrality:
)
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])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def test_P5_directed(self):
"""Betweenness Centrality Subset: P5 directed"""
@@ -20,7 +20,7 @@ class TestSubsetBetweennessCentrality:
b_answer = {0: 0, 1: 1, 2: 1, 3: 0, 4: 0, 5: 0}
b = nx.betweenness_centrality_subset(G, sources=[0], targets=[3], weight=None)
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_P5(self):
"""Betweenness Centrality Subset: P5"""
@@ -29,7 +29,7 @@ class TestSubsetBetweennessCentrality:
b_answer = {0: 0, 1: 0.5, 2: 0.5, 3: 0, 4: 0, 5: 0}
b = nx.betweenness_centrality_subset(G, sources=[0], targets=[3], weight=None)
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_P5_multiple_target(self):
"""Betweenness Centrality Subset: P5 multiple target"""
@@ -40,7 +40,7 @@ class TestSubsetBetweennessCentrality:
G, sources=[0], targets=[3, 4], weight=None
)
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_box(self):
"""Betweenness Centrality Subset: box"""
@@ -49,7 +49,7 @@ class TestSubsetBetweennessCentrality:
b_answer = {0: 0, 1: 0.25, 2: 0.25, 3: 0}
b = nx.betweenness_centrality_subset(G, sources=[0], targets=[3], weight=None)
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_box_and_path(self):
"""Betweenness Centrality Subset: box and path"""
@@ -60,7 +60,7 @@ class TestSubsetBetweennessCentrality:
G, sources=[0], targets=[3, 4], weight=None
)
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_box_and_path2(self):
"""Betweenness Centrality Subset: box and path multiple target"""
@@ -71,7 +71,7 @@ class TestSubsetBetweennessCentrality:
G, sources=[0], targets=[3, 4], weight=None
)
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_diamond_multi_path(self):
"""Betweenness Centrality Subset: Diamond Multi Path"""
@@ -113,7 +113,7 @@ class TestSubsetBetweennessCentrality:
}
for n in sorted(G):
- assert almost_equal(b[n], expected_b[n])
+ assert b[n] == pytest.approx(expected_b[n], abs=1e-7)
class TestBetweennessCentralitySources:
@@ -123,7 +123,7 @@ class TestBetweennessCentralitySources:
b = nx.betweenness_centrality_source(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])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def test_P3(self):
"""Betweenness Centrality Sources: P3"""
@@ -131,7 +131,7 @@ class TestBetweennessCentralitySources:
b_answer = {0: 0.0, 1: 1.0, 2: 0.0}
b = nx.betweenness_centrality_source(G, weight=None, normalized=True)
for n in sorted(G):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
class TestEdgeSubsetBetweennessCentrality:
@@ -144,7 +144,7 @@ class TestEdgeSubsetBetweennessCentrality:
b_answer = dict.fromkeys(G.edges(), 0)
b_answer[(0, 3)] = b_answer[(0, 1)] = 0.5
for n in sorted(G.edges()):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def test_P5_directed(self):
"""Edge betweenness subset centrality: P5 directed"""
@@ -156,7 +156,7 @@ class TestEdgeSubsetBetweennessCentrality:
G, sources=[0], targets=[3], weight=None
)
for n in sorted(G.edges()):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def test_P5(self):
"""Edge betweenness subset centrality: P5"""
@@ -168,7 +168,7 @@ class TestEdgeSubsetBetweennessCentrality:
G, sources=[0], targets=[3], weight=None
)
for n in sorted(G.edges()):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def test_P5_multiple_target(self):
"""Edge betweenness subset centrality: P5 multiple target"""
@@ -181,7 +181,7 @@ class TestEdgeSubsetBetweennessCentrality:
G, sources=[0], targets=[3, 4], weight=None
)
for n in sorted(G.edges()):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def test_box(self):
"""Edge betweenness subset centrality: box"""
@@ -194,7 +194,7 @@ class TestEdgeSubsetBetweennessCentrality:
G, sources=[0], targets=[3], weight=None
)
for n in sorted(G.edges()):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def test_box_and_path(self):
"""Edge betweenness subset centrality: box and path"""
@@ -208,7 +208,7 @@ class TestEdgeSubsetBetweennessCentrality:
G, sources=[0], targets=[3, 4], weight=None
)
for n in sorted(G.edges()):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
def test_box_and_path2(self):
"""Edge betweenness subset centrality: box and path multiple target"""
@@ -223,4 +223,4 @@ class TestEdgeSubsetBetweennessCentrality:
G, sources=[0], targets=[3, 4], weight=None
)
for n in sorted(G.edges()):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
diff --git a/networkx/algorithms/centrality/tests/test_closeness_centrality.py b/networkx/algorithms/centrality/tests/test_closeness_centrality.py
index b3f883b8..a59b2698 100644
--- a/networkx/algorithms/centrality/tests/test_closeness_centrality.py
+++ b/networkx/algorithms/centrality/tests/test_closeness_centrality.py
@@ -3,7 +3,6 @@ Tests for closeness centrality.
"""
import pytest
import networkx as nx
-from networkx.testing import almost_equal
class TestClosenessCentrality:
@@ -35,8 +34,8 @@ class TestClosenessCentrality:
res = {0: 0.25, 1: 0.375, 2: 0.375, 3: 0.25, 4: 0.222, 5: 0.333, 6: 0.222}
wf_res = {0: 0.5, 1: 0.75, 2: 0.75, 3: 0.5, 4: 0.667, 5: 1.0, 6: 0.667}
for n in G:
- assert almost_equal(c[n], res[n], places=3)
- assert almost_equal(cwf[n], wf_res[n], places=3)
+ assert c[n] == pytest.approx(res[n], abs=1e-3)
+ assert cwf[n] == pytest.approx(wf_res[n], abs=1e-3)
def test_digraph(self):
G = nx.path_graph(3, create_using=nx.DiGraph())
@@ -45,20 +44,20 @@ class TestClosenessCentrality:
d = {0: 0.0, 1: 0.500, 2: 0.667}
dr = {0: 0.667, 1: 0.500, 2: 0.0}
for n in sorted(self.P3):
- assert almost_equal(c[n], d[n], places=3)
- assert almost_equal(cr[n], dr[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
+ assert cr[n] == pytest.approx(dr[n], abs=1e-3)
def test_k5_closeness(self):
c = nx.closeness_centrality(self.K5)
d = {0: 1.000, 1: 1.000, 2: 1.000, 3: 1.000, 4: 1.000}
for n in sorted(self.K5):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_p3_closeness(self):
c = nx.closeness_centrality(self.P3)
d = {0: 0.667, 1: 1.000, 2: 0.667}
for n in sorted(self.P3):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_krackhardt_closeness(self):
c = nx.closeness_centrality(self.K)
@@ -75,7 +74,7 @@ class TestClosenessCentrality:
9: 0.310,
}
for n in sorted(self.K):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_florentine_families_closeness(self):
c = nx.closeness_centrality(self.F)
@@ -97,7 +96,7 @@ class TestClosenessCentrality:
"Tornabuoni": 0.483,
}
for n in sorted(self.F):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_les_miserables_closeness(self):
c = nx.closeness_centrality(self.LM)
@@ -181,7 +180,7 @@ class TestClosenessCentrality:
"MmeHucheloup": 0.353,
}
for n in sorted(self.LM):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_weighted_closeness(self):
edges = [
@@ -201,7 +200,7 @@ class TestClosenessCentrality:
c = nx.closeness_centrality(XG, distance="weight")
d = {"y": 0.200, "x": 0.286, "s": 0.138, "u": 0.235, "v": 0.200}
for n in sorted(XG):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
#
# Tests for incremental closeness centrality.
diff --git a/networkx/algorithms/centrality/tests/test_current_flow_betweenness_centrality.py b/networkx/algorithms/centrality/tests/test_current_flow_betweenness_centrality.py
index 69b06cb9..eea05f43 100644
--- a/networkx/algorithms/centrality/tests/test_current_flow_betweenness_centrality.py
+++ b/networkx/algorithms/centrality/tests/test_current_flow_betweenness_centrality.py
@@ -1,7 +1,6 @@
import pytest
import networkx as nx
-from networkx.testing import almost_equal
from networkx import edge_current_flow_betweenness_centrality as edge_current_flow
from networkx import approximate_current_flow_betweenness_centrality as approximate_cfbc
@@ -17,19 +16,19 @@ class TestFlowBetweennessCentrality:
b = nx.current_flow_betweenness_centrality(G, normalized=True)
b_answer = {0: 0.25, 1: 0.25, 2: 0.25, 3: 0.25}
for n in sorted(G):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
G.add_edge(0, 1, weight=0.5, other=0.3)
b = nx.current_flow_betweenness_centrality(G, normalized=True, weight=None)
for n in sorted(G):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
wb_answer = {0: 0.2222222, 1: 0.2222222, 2: 0.30555555, 3: 0.30555555}
b = nx.current_flow_betweenness_centrality(G, normalized=True, weight="weight")
for n in sorted(G):
- assert almost_equal(b[n], wb_answer[n])
+ assert b[n] == pytest.approx(wb_answer[n], abs=1e-7)
wb_answer = {0: 0.2051282, 1: 0.2051282, 2: 0.33974358, 3: 0.33974358}
b = nx.current_flow_betweenness_centrality(G, normalized=True, weight="other")
for n in sorted(G):
- assert almost_equal(b[n], wb_answer[n])
+ assert b[n] == pytest.approx(wb_answer[n], abs=1e-7)
def test_K4(self):
"""Betweenness centrality: K4"""
@@ -40,7 +39,7 @@ class TestFlowBetweennessCentrality:
)
b_answer = {0: 0.75, 1: 0.75, 2: 0.75, 3: 0.75}
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_P4_normalized(self):
"""Betweenness centrality: P4 normalized"""
@@ -48,7 +47,7 @@ class TestFlowBetweennessCentrality:
b = nx.current_flow_betweenness_centrality(G, normalized=True)
b_answer = {0: 0, 1: 2.0 / 3, 2: 2.0 / 3, 3: 0}
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_P4(self):
"""Betweenness centrality: P4"""
@@ -56,7 +55,7 @@ class TestFlowBetweennessCentrality:
b = nx.current_flow_betweenness_centrality(G, normalized=False)
b_answer = {0: 0, 1: 2, 2: 2, 3: 0}
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_star(self):
"""Betweenness centrality: star"""
@@ -65,7 +64,7 @@ class TestFlowBetweennessCentrality:
b = nx.current_flow_betweenness_centrality(G, normalized=True)
b_answer = {"a": 1.0, "b": 0.0, "c": 0.0, "d": 0.0}
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_solvers2(self):
"""Betweenness centrality: alternate solvers"""
@@ -76,7 +75,7 @@ class TestFlowBetweennessCentrality:
)
b_answer = {0: 0.75, 1: 0.75, 2: 0.75, 3: 0.75}
for n in sorted(G):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
class TestApproximateFlowBetweennessCentrality:
@@ -149,7 +148,7 @@ class TestEdgeFlowBetweennessCentrality:
b_answer = dict.fromkeys(G.edges(), 0.25)
for (s, t), v1 in b_answer.items():
v2 = b.get((s, t), b.get((t, s)))
- assert almost_equal(v1, v2)
+ assert v1 == pytest.approx(v2, abs=1e-7)
def test_K4_normalized(self):
"""Edge flow betweenness centrality: K4"""
@@ -158,7 +157,7 @@ class TestEdgeFlowBetweennessCentrality:
b_answer = dict.fromkeys(G.edges(), 0.75)
for (s, t), v1 in b_answer.items():
v2 = b.get((s, t), b.get((t, s)))
- assert almost_equal(v1, v2)
+ assert v1 == pytest.approx(v2, abs=1e-7)
def test_C4(self):
"""Edge flow betweenness centrality: C4"""
@@ -167,7 +166,7 @@ class TestEdgeFlowBetweennessCentrality:
b_answer = {(0, 1): 1.25, (0, 3): 1.25, (1, 2): 1.25, (2, 3): 1.25}
for (s, t), v1 in b_answer.items():
v2 = b.get((s, t), b.get((t, s)))
- assert almost_equal(v1, v2)
+ assert v1 == pytest.approx(v2, abs=1e-7)
def test_P4(self):
"""Edge betweenness centrality: P4"""
@@ -176,4 +175,4 @@ class TestEdgeFlowBetweennessCentrality:
b_answer = {(0, 1): 1.5, (1, 2): 2.0, (2, 3): 1.5}
for (s, t), v1 in b_answer.items():
v2 = b.get((s, t), b.get((t, s)))
- assert almost_equal(v1, v2)
+ assert v1 == pytest.approx(v2, abs=1e-7)
diff --git a/networkx/algorithms/centrality/tests/test_current_flow_betweenness_centrality_subset.py b/networkx/algorithms/centrality/tests/test_current_flow_betweenness_centrality_subset.py
index 47e41b3f..ae5a730f 100644
--- a/networkx/algorithms/centrality/tests/test_current_flow_betweenness_centrality_subset.py
+++ b/networkx/algorithms/centrality/tests/test_current_flow_betweenness_centrality_subset.py
@@ -4,7 +4,6 @@ pytest.importorskip("numpy")
pytest.importorskip("scipy")
import networkx as nx
-from networkx.testing import almost_equal
from networkx import edge_current_flow_betweenness_centrality as edge_current_flow
@@ -22,7 +21,7 @@ class TestFlowBetweennessCentrality:
)
b_answer = nx.current_flow_betweenness_centrality(G, normalized=True)
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_K4(self):
"""Betweenness centrality: K4"""
@@ -32,20 +31,20 @@ class TestFlowBetweennessCentrality:
)
b_answer = nx.current_flow_betweenness_centrality(G, normalized=True)
for n in sorted(G):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
# test weighted network
G.add_edge(0, 1, weight=0.5, other=0.3)
b = nx.current_flow_betweenness_centrality_subset(
G, list(G), list(G), normalized=True, weight=None
)
for n in sorted(G):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
b = nx.current_flow_betweenness_centrality_subset(
G, list(G), list(G), normalized=True
)
b_answer = nx.current_flow_betweenness_centrality(G, normalized=True)
for n in sorted(G):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
b = nx.current_flow_betweenness_centrality_subset(
G, list(G), list(G), normalized=True, weight="other"
)
@@ -53,7 +52,7 @@ class TestFlowBetweennessCentrality:
G, normalized=True, weight="other"
)
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_P4_normalized(self):
"""Betweenness centrality: P4 normalized"""
@@ -63,7 +62,7 @@ class TestFlowBetweennessCentrality:
)
b_answer = nx.current_flow_betweenness_centrality(G, normalized=True)
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_P4(self):
"""Betweenness centrality: P4"""
@@ -73,7 +72,7 @@ class TestFlowBetweennessCentrality:
)
b_answer = nx.current_flow_betweenness_centrality(G, normalized=True)
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_star(self):
"""Betweenness centrality: star"""
@@ -84,7 +83,7 @@ class TestFlowBetweennessCentrality:
)
b_answer = nx.current_flow_betweenness_centrality(G, normalized=True)
for n in sorted(G):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
# class TestWeightedFlowBetweennessCentrality():
@@ -99,7 +98,7 @@ class TestEdgeFlowBetweennessCentrality:
b_answer = edge_current_flow(G, normalized=True)
for (s, t), v1 in b_answer.items():
v2 = b.get((s, t), b.get((t, s)))
- assert almost_equal(v1, v2)
+ assert v1 == pytest.approx(v2, abs=1e-7)
def test_K4(self):
"""Betweenness centrality: K4"""
@@ -108,20 +107,20 @@ class TestEdgeFlowBetweennessCentrality:
b_answer = edge_current_flow(G, normalized=False)
for (s, t), v1 in b_answer.items():
v2 = b.get((s, t), b.get((t, s)))
- assert almost_equal(v1, v2)
+ assert v1 == pytest.approx(v2, abs=1e-7)
# test weighted network
G.add_edge(0, 1, weight=0.5, other=0.3)
b = edge_current_flow_subset(G, list(G), list(G), normalized=False, weight=None)
# weight is None => same as unweighted network
for (s, t), v1 in b_answer.items():
v2 = b.get((s, t), b.get((t, s)))
- assert almost_equal(v1, v2)
+ assert v1 == pytest.approx(v2, abs=1e-7)
b = edge_current_flow_subset(G, list(G), list(G), normalized=False)
b_answer = edge_current_flow(G, normalized=False)
for (s, t), v1 in b_answer.items():
v2 = b.get((s, t), b.get((t, s)))
- assert almost_equal(v1, v2)
+ assert v1 == pytest.approx(v2, abs=1e-7)
b = edge_current_flow_subset(
G, list(G), list(G), normalized=False, weight="other"
@@ -129,7 +128,7 @@ class TestEdgeFlowBetweennessCentrality:
b_answer = edge_current_flow(G, normalized=False, weight="other")
for (s, t), v1 in b_answer.items():
v2 = b.get((s, t), b.get((t, s)))
- assert almost_equal(v1, v2)
+ assert v1 == pytest.approx(v2, abs=1e-7)
def test_C4(self):
"""Edge betweenness centrality: C4"""
@@ -138,7 +137,7 @@ class TestEdgeFlowBetweennessCentrality:
b_answer = edge_current_flow(G, normalized=True)
for (s, t), v1 in b_answer.items():
v2 = b.get((s, t), b.get((t, s)))
- assert almost_equal(v1, v2)
+ assert v1 == pytest.approx(v2, abs=1e-7)
def test_P4(self):
"""Edge betweenness centrality: P4"""
@@ -147,4 +146,4 @@ class TestEdgeFlowBetweennessCentrality:
b_answer = edge_current_flow(G, normalized=True)
for (s, t), v1 in b_answer.items():
v2 = b.get((s, t), b.get((t, s)))
- assert almost_equal(v1, v2)
+ assert v1 == pytest.approx(v2, abs=1e-7)
diff --git a/networkx/algorithms/centrality/tests/test_current_flow_closeness.py b/networkx/algorithms/centrality/tests/test_current_flow_closeness.py
index c2fa4ec0..24a916a4 100644
--- a/networkx/algorithms/centrality/tests/test_current_flow_closeness.py
+++ b/networkx/algorithms/centrality/tests/test_current_flow_closeness.py
@@ -4,7 +4,6 @@ pytest.importorskip("numpy")
pytest.importorskip("scipy")
import networkx as nx
-from networkx.testing import almost_equal
class TestFlowClosenessCentrality:
@@ -14,7 +13,7 @@ class TestFlowClosenessCentrality:
b = nx.current_flow_closeness_centrality(G)
b_answer = {0: 2.0 / 3, 1: 2.0 / 3, 2: 2.0 / 3, 3: 2.0 / 3}
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_P4(self):
"""Closeness centrality: P4"""
@@ -22,7 +21,7 @@ class TestFlowClosenessCentrality:
b = nx.current_flow_closeness_centrality(G)
b_answer = {0: 1.0 / 6, 1: 1.0 / 4, 2: 1.0 / 4, 3: 1.0 / 6}
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_star(self):
"""Closeness centrality: star"""
@@ -31,7 +30,7 @@ class TestFlowClosenessCentrality:
b = nx.current_flow_closeness_centrality(G)
b_answer = {"a": 1.0 / 3, "b": 0.6 / 3, "c": 0.6 / 3, "d": 0.6 / 3}
for n in sorted(G):
- assert almost_equal(b[n], b_answer[n])
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-7)
class TestWeightedFlowClosenessCentrality:
diff --git a/networkx/algorithms/centrality/tests/test_degree_centrality.py b/networkx/algorithms/centrality/tests/test_degree_centrality.py
index 2b204cfb..f5f0ca70 100644
--- a/networkx/algorithms/centrality/tests/test_degree_centrality.py
+++ b/networkx/algorithms/centrality/tests/test_degree_centrality.py
@@ -2,9 +2,8 @@
Unit tests for degree centrality.
"""
-
+import pytest
import networkx as nx
-from networkx.testing import almost_equal
class TestDegreeCentrality:
@@ -52,13 +51,13 @@ class TestDegreeCentrality:
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)
+ assert exact[n] == pytest.approx(dc, abs=1e-7)
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)
+ assert exact[n] == pytest.approx(dc, abs=1e-7)
def test_degree_centrality_3(self):
d = nx.degree_centrality(self.K)
@@ -75,7 +74,7 @@ class TestDegreeCentrality:
9: 0.111,
}
for n, dc in d.items():
- assert almost_equal(exact[n], float(f"{dc:.3f}"))
+ assert exact[n] == pytest.approx(float(f"{dc:.3f}"), abs=1e-7)
def test_degree_centrality_4(self):
d = nx.degree_centrality(self.F)
@@ -99,7 +98,7 @@ class TestDegreeCentrality:
]
exact = dict(zip(names, dcs))
for n, dc in d.items():
- assert almost_equal(exact[n], float(f"{dc:.3f}"))
+ assert exact[n] == pytest.approx(float(f"{dc:.3f}"), abs=1e-7)
def test_indegree_centrality(self):
d = nx.in_degree_centrality(self.G)
@@ -115,7 +114,7 @@ class TestDegreeCentrality:
8: 0.125,
}
for n, dc in d.items():
- assert almost_equal(exact[n], dc)
+ assert exact[n] == pytest.approx(dc, abs=1e-7)
def test_outdegree_centrality(self):
d = nx.out_degree_centrality(self.G)
@@ -131,7 +130,7 @@ class TestDegreeCentrality:
8: 0.0,
}
for n, dc in d.items():
- assert almost_equal(exact[n], dc)
+ assert exact[n] == pytest.approx(dc, abs=1e-7)
def test_small_graph_centrality(self):
G = nx.empty_graph(create_using=nx.DiGraph)
diff --git a/networkx/algorithms/centrality/tests/test_eigenvector_centrality.py b/networkx/algorithms/centrality/tests/test_eigenvector_centrality.py
index 133f4ffd..383d4565 100644
--- a/networkx/algorithms/centrality/tests/test_eigenvector_centrality.py
+++ b/networkx/algorithms/centrality/tests/test_eigenvector_centrality.py
@@ -6,7 +6,6 @@ pytest.importorskip("scipy")
import networkx as nx
-from networkx.testing import almost_equal
class TestEigenvectorCentrality:
@@ -17,15 +16,15 @@ class TestEigenvectorCentrality:
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(G, 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)
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):
"""Eigenvector centrality: P3"""
@@ -33,10 +32,10 @@ class TestEigenvectorCentrality:
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)
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-4)
b = nx.eigenvector_centrality(G)
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_P3_unweighted(self):
"""Eigenvector centrality: P3"""
@@ -44,7 +43,7 @@ class TestEigenvectorCentrality:
b_answer = {0: 0.5, 1: 0.7071, 2: 0.5}
b = nx.eigenvector_centrality_numpy(G, 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)
def test_maxiter(self):
with pytest.raises(nx.PowerIterationFailedConvergence):
@@ -129,25 +128,25 @@ class TestEigenvectorCentralityDirected:
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)
+ assert a == pytest.approx(b, abs=1e-4)
def test_eigenvector_centrality_weighted_numpy(self):
G = self.G
p = nx.eigenvector_centrality_numpy(G)
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_eigenvector_centrality_unweighted(self):
G = self.H
p = nx.eigenvector_centrality(G)
for (a, b) in zip(list(p.values()), self.G.evc):
- assert almost_equal(a, b, places=4)
+ assert a == pytest.approx(b, abs=1e-4)
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)
+ assert a == pytest.approx(b, abs=1e-7)
class TestEigenvectorCentralityExceptions:
diff --git a/networkx/algorithms/centrality/tests/test_harmonic_centrality.py b/networkx/algorithms/centrality/tests/test_harmonic_centrality.py
index 8048508b..ae2bad7d 100644
--- a/networkx/algorithms/centrality/tests/test_harmonic_centrality.py
+++ b/networkx/algorithms/centrality/tests/test_harmonic_centrality.py
@@ -1,9 +1,9 @@
"""
Tests for degree centrality.
"""
+import pytest
import networkx as nx
from networkx.algorithms.centrality import harmonic_centrality
-from networkx.testing import almost_equal
class TestClosenessCentrality:
@@ -27,43 +27,43 @@ class TestClosenessCentrality:
c = harmonic_centrality(self.P3)
d = {0: 1.5, 1: 2, 2: 1.5}
for n in sorted(self.P3):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_p4_harmonic(self):
c = harmonic_centrality(self.P4)
d = {0: 1.8333333, 1: 2.5, 2: 2.5, 3: 1.8333333}
for n in sorted(self.P4):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_clique_complete(self):
c = harmonic_centrality(self.K5)
d = {0: 4, 1: 4, 2: 4, 3: 4, 4: 4}
for n in sorted(self.P3):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_cycle_C4(self):
c = harmonic_centrality(self.C4)
d = {0: 2.5, 1: 2.5, 2: 2.5, 3: 2.5}
for n in sorted(self.C4):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_cycle_C5(self):
c = harmonic_centrality(self.C5)
d = {0: 3, 1: 3, 2: 3, 3: 3, 4: 3, 5: 4}
for n in sorted(self.C5):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_bal_tree(self):
c = harmonic_centrality(self.T)
d = {0: 4.0, 1: 4.1666, 2: 4.1666, 3: 2.8333, 4: 2.8333, 5: 2.8333, 6: 2.8333}
for n in sorted(self.T):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_exampleGraph(self):
c = harmonic_centrality(self.Gb)
d = {0: 0, 1: 2, 2: 1, 3: 2.5, 4: 1}
for n in sorted(self.Gb):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_weighted_harmonic(self):
XG = nx.DiGraph()
@@ -80,7 +80,7 @@ class TestClosenessCentrality:
c = harmonic_centrality(XG, distance="weight")
d = {"a": 0, "b": 0.1, "c": 2.533, "d": 0, "e": 0, "f": 0.83333}
for n in sorted(XG):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_empty(self):
G = nx.DiGraph()
@@ -99,16 +99,16 @@ class TestClosenessCentrality:
c = harmonic_centrality(self.C4_directed, nbunch=[0, 1], sources=[1, 2])
d = {0: 0.833, 1: 0.333}
for n in [0, 1]:
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_p3_harmonic_subset(self):
c = harmonic_centrality(self.P3, sources=[0, 1])
d = {0: 1, 1: 1, 2: 1.5}
for n in self.P3:
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_p4_harmonic_subset(self):
c = harmonic_centrality(self.P4, nbunch=[2, 3], sources=[0, 1])
d = {2: 1.5, 3: 0.8333333}
for n in [2, 3]:
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
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)
diff --git a/networkx/algorithms/centrality/tests/test_load_centrality.py b/networkx/algorithms/centrality/tests/test_load_centrality.py
index 66d0ea56..5d69884a 100644
--- a/networkx/algorithms/centrality/tests/test_load_centrality.py
+++ b/networkx/algorithms/centrality/tests/test_load_centrality.py
@@ -1,5 +1,5 @@
+import pytest
import networkx as nx
-from networkx.testing import almost_equal
class TestLoadCentrality:
@@ -37,8 +37,8 @@ class TestLoadCentrality:
b = nx.load_centrality(self.D)
result = {0: 5.0 / 12, 1: 1.0 / 4, 2: 1.0 / 12, 3: 1.0 / 4, 4: 0.000}
for n in sorted(self.D):
- assert almost_equal(result[n], b[n], places=3)
- assert almost_equal(result[n], nx.load_centrality(self.D, n), places=3)
+ assert result[n] == pytest.approx(b[n], abs=1e-3)
+ assert result[n] == pytest.approx(nx.load_centrality(self.D, n), abs=1e-3)
def test_weighted_load(self):
b = nx.load_centrality(self.G, weight="weight", normalized=False)
@@ -50,25 +50,25 @@ class TestLoadCentrality:
c = nx.load_centrality(G)
d = {0: 0.000, 1: 0.000, 2: 0.000, 3: 0.000, 4: 0.000}
for n in sorted(G):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_p3_load(self):
G = self.P3
c = nx.load_centrality(G)
d = {0: 0.000, 1: 1.000, 2: 0.000}
for n in sorted(G):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
c = nx.load_centrality(G, v=1)
- assert almost_equal(c, 1.0)
+ assert c == pytest.approx(1.0, abs=1e-7)
c = nx.load_centrality(G, v=1, normalized=True)
- assert almost_equal(c, 1.0)
+ assert c == pytest.approx(1.0, abs=1e-7)
def test_p2_load(self):
G = nx.path_graph(2)
c = nx.load_centrality(G)
d = {0: 0.000, 1: 0.000}
for n in sorted(G):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_krackhardt_load(self):
G = self.K
@@ -86,7 +86,7 @@ class TestLoadCentrality:
9: 0.000,
}
for n in sorted(G):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_florentine_families_load(self):
G = self.F
@@ -109,7 +109,7 @@ class TestLoadCentrality:
"Tornabuoni": 0.090,
}
for n in sorted(G):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_les_miserables_load(self):
G = self.LM
@@ -194,21 +194,21 @@ class TestLoadCentrality:
"MmeHucheloup": 0.000,
}
for n in sorted(G):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_unnormalized_k5_load(self):
G = self.K5
c = nx.load_centrality(G, normalized=False)
d = {0: 0.000, 1: 0.000, 2: 0.000, 3: 0.000, 4: 0.000}
for n in sorted(G):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_unnormalized_p3_load(self):
G = self.P3
c = nx.load_centrality(G, normalized=False)
d = {0: 0.000, 1: 2.000, 2: 0.000}
for n in sorted(G):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_unnormalized_krackhardt_load(self):
G = self.K
@@ -227,7 +227,7 @@ class TestLoadCentrality:
}
for n in sorted(G):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_unnormalized_florentine_families_load(self):
G = self.F
@@ -251,7 +251,7 @@ class TestLoadCentrality:
"Tornabuoni": 16.333,
}
for n in sorted(G):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_load_betweenness_difference(self):
# Difference Between Load and Betweenness
@@ -287,21 +287,21 @@ class TestLoadCentrality:
c = nx.load_centrality(B, normalized=False)
d = {0: 1.750, 1: 1.750, 2: 6.500, 3: 6.500, 4: 1.750, 5: 1.750}
for n in sorted(B):
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_c4_edge_load(self):
G = self.C4
c = nx.edge_load_centrality(G)
d = {(0, 1): 6.000, (0, 3): 6.000, (1, 2): 6.000, (2, 3): 6.000}
for n in G.edges():
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_p4_edge_load(self):
G = self.P4
c = nx.edge_load_centrality(G)
d = {(0, 1): 6.000, (1, 2): 8.000, (2, 3): 6.000}
for n in G.edges():
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_k5_edge_load(self):
G = self.K5
@@ -319,7 +319,7 @@ class TestLoadCentrality:
(3, 4): 5.000,
}
for n in G.edges():
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
def test_tree_edge_load(self):
G = self.T
@@ -333,4 +333,4 @@ class TestLoadCentrality:
(2, 6): 12.000,
}
for n in G.edges():
- assert almost_equal(c[n], d[n], places=3)
+ assert c[n] == pytest.approx(d[n], abs=1e-3)
diff --git a/networkx/algorithms/centrality/tests/test_percolation_centrality.py b/networkx/algorithms/centrality/tests/test_percolation_centrality.py
index 4311edb9..ad9f243d 100644
--- a/networkx/algorithms/centrality/tests/test_percolation_centrality.py
+++ b/networkx/algorithms/centrality/tests/test_percolation_centrality.py
@@ -1,5 +1,5 @@
+import pytest
import networkx as nx
-from networkx.testing import almost_equal
def example1a_G():
@@ -37,7 +37,7 @@ class TestPercolationCentrality:
p = nx.percolation_centrality(G)
p_answer = {4: 0.625, 6: 0.667}
for n in p_answer:
- assert almost_equal(p[n], p_answer[n], places=3)
+ assert p[n] == pytest.approx(p_answer[n], abs=1e-3)
def test_percolation_example1b(self):
"""percolation centrality: example 1a"""
@@ -45,7 +45,7 @@ class TestPercolationCentrality:
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)
+ assert p[n] == pytest.approx(p_answer[n], abs=1e-3)
def test_converge_to_betweenness(self):
"""percolation centrality: should converge to betweenness
@@ -73,9 +73,9 @@ class TestPercolationCentrality:
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)
+ assert p_answer[n] == pytest.approx(b_answer[n], abs=1e-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)
+ assert p_answer[n] == pytest.approx(b_answer[n], abs=1e-3)
diff --git a/networkx/algorithms/centrality/tests/test_reaching.py b/networkx/algorithms/centrality/tests/test_reaching.py
index 8587776b..86ae8a93 100644
--- a/networkx/algorithms/centrality/tests/test_reaching.py
+++ b/networkx/algorithms/centrality/tests/test_reaching.py
@@ -2,7 +2,6 @@
import pytest
import networkx as nx
-from networkx.testing import almost_equal
class TestGlobalReachingCentrality:
@@ -78,7 +77,7 @@ class TestGlobalReachingCentrality:
expected = sum(max_local - lrc for lrc in local_reach_ctrs) / denom
grc = nx.global_reaching_centrality
actual = grc(G, normalized=False, weight="weight")
- assert almost_equal(expected, actual, places=7)
+ assert expected == pytest.approx(actual, abs=1e-7)
class TestLocalReachingCentrality:
diff --git a/networkx/algorithms/centrality/tests/test_second_order_centrality.py b/networkx/algorithms/centrality/tests/test_second_order_centrality.py
index 385593b6..903bbe9a 100644
--- a/networkx/algorithms/centrality/tests/test_second_order_centrality.py
+++ b/networkx/algorithms/centrality/tests/test_second_order_centrality.py
@@ -8,7 +8,6 @@ pytest.importorskip("numpy")
pytest.importorskip("scipy")
import networkx as nx
-from networkx.testing import almost_equal
class TestSecondOrderCentrality:
@@ -45,7 +44,7 @@ class TestSecondOrderCentrality:
b = nx.second_order_centrality(G)
for n in sorted(G):
- assert almost_equal(b[n], b_answer[n], places=2)
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-2)
def test_K3(self):
"""Second order centrality: complete graph, as defined in paper"""
@@ -55,7 +54,7 @@ class TestSecondOrderCentrality:
b = nx.second_order_centrality(G)
for n in sorted(G):
- assert almost_equal(b[n], b_answer[n], places=2)
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-2)
def test_ring_graph(self):
"""Second order centrality: ring graph, as defined in paper"""
@@ -65,4 +64,4 @@ class TestSecondOrderCentrality:
b = nx.second_order_centrality(G)
for n in sorted(G):
- assert almost_equal(b[n], b_answer[n], places=2)
+ assert b[n] == pytest.approx(b_answer[n], abs=1e-2)
diff --git a/networkx/algorithms/centrality/tests/test_subgraph.py b/networkx/algorithms/centrality/tests/test_subgraph.py
index e5e0210a..bb1a8112 100644
--- a/networkx/algorithms/centrality/tests/test_subgraph.py
+++ b/networkx/algorithms/centrality/tests/test_subgraph.py
@@ -10,7 +10,6 @@ from networkx.algorithms.centrality.subgraph_alg import (
subgraph_centrality,
subgraph_centrality_exp,
)
-from networkx.testing import almost_equal
class TestSubgraph:
@@ -18,7 +17,7 @@ class TestSubgraph:
answer = {0: 1.5430806348152433, 1: 1.5430806348152433}
result = subgraph_centrality(nx.path_graph(2))
for k, v in result.items():
- assert almost_equal(answer[k], result[k], places=7)
+ assert answer[k] == pytest.approx(result[k], abs=1e-7)
answer1 = {
"1": 1.6445956054135658,
@@ -39,10 +38,10 @@ class TestSubgraph:
)
result1 = subgraph_centrality(G1)
for k, v in result1.items():
- assert almost_equal(answer1[k], result1[k], places=7)
+ assert answer1[k] == pytest.approx(result1[k], abs=1e-7)
result1 = subgraph_centrality_exp(G1)
for k, v in result1.items():
- assert almost_equal(answer1[k], result1[k], places=7)
+ assert answer1[k] == pytest.approx(result1[k], abs=1e-7)
def test_subgraph_centrality_big_graph(self):
g199 = nx.complete_graph(199)
@@ -63,7 +62,7 @@ class TestSubgraph:
}
result = communicability_betweenness_centrality(nx.path_graph(4))
for k, v in result.items():
- assert almost_equal(answer[k], result[k], places=7)
+ assert answer[k] == pytest.approx(result[k], abs=1e-7)
answer1 = {
"1": 0.060039074193949521,
@@ -84,9 +83,9 @@ class TestSubgraph:
)
result1 = communicability_betweenness_centrality(G1)
for k, v in result1.items():
- assert almost_equal(answer1[k], result1[k], places=7)
+ assert answer1[k] == pytest.approx(result1[k], abs=1e-7)
def test_estrada_index(self):
answer = 1041.2470334195475
result = estrada_index(nx.karate_club_graph())
- assert almost_equal(answer, result, places=7)
+ assert answer == pytest.approx(result, abs=1e-7)
diff --git a/networkx/algorithms/centrality/tests/test_trophic.py b/networkx/algorithms/centrality/tests/test_trophic.py
index 18e2d74a..f1d68131 100644
--- a/networkx/algorithms/centrality/tests/test_trophic.py
+++ b/networkx/algorithms/centrality/tests/test_trophic.py
@@ -6,7 +6,6 @@ np = pytest.importorskip("numpy")
pytest.importorskip("scipy")
import networkx as nx
-from networkx.testing import almost_equal
def test_trophic_levels():
@@ -74,15 +73,15 @@ def test_trophic_levels_levine():
for nid, level in d.items():
expected_level = expected_d[nid]
- assert almost_equal(expected_level, level)
+ assert expected_level == pytest.approx(level, abs=1e-7)
def test_trophic_levels_simple():
matrix_a = np.array([[0, 0], [1, 0]])
G = nx.from_numpy_array(matrix_a, create_using=nx.DiGraph)
d = nx.trophic_levels(G)
- assert almost_equal(d[0], 2)
- assert almost_equal(d[1], 1)
+ assert d[0] == pytest.approx(2, abs=1e-7)
+ assert d[1] == pytest.approx(1, abs=1e-7)
def test_trophic_levels_more_complex():
@@ -98,7 +97,7 @@ def test_trophic_levels_more_complex():
d = nx.trophic_levels(G)
expected_result = [1, 2, 3, 4]
for ind in range(4):
- assert almost_equal(d[ind], expected_result[ind])
+ assert d[ind] == pytest.approx(expected_result[ind], abs=1e-7)
# fmt: off
matrix = np.array([
@@ -116,7 +115,7 @@ def test_trophic_levels_more_complex():
print("Expected Result: ", expected_result)
for ind in range(4):
- assert almost_equal(d[ind], expected_result[ind])
+ assert d[ind] == pytest.approx(expected_result[ind], abs=1e-7)
def test_trophic_levels_even_more_complex():
@@ -143,7 +142,7 @@ def test_trophic_levels_even_more_complex():
result_2 = nx.trophic_levels(G)
for ind in range(5):
- assert almost_equal(result_1[ind], result_2[ind])
+ assert result_1[ind] == pytest.approx(result_2[ind], abs=1e-7)
def test_trophic_levels_singular_matrix():
@@ -204,7 +203,7 @@ def test_trophic_differences():
matrix_a = np.array([[0, 1], [0, 0]])
G = nx.from_numpy_array(matrix_a, create_using=nx.DiGraph)
diffs = nx.trophic_differences(G)
- assert almost_equal(diffs[(0, 1)], 1)
+ assert diffs[(0, 1)] == pytest.approx(1, abs=1e-7)
# fmt: off
matrix_b = np.array([
@@ -217,18 +216,18 @@ def test_trophic_differences():
G = nx.from_numpy_array(matrix_b, create_using=nx.DiGraph)
diffs = nx.trophic_differences(G)
- assert almost_equal(diffs[(0, 1)], 1)
- assert almost_equal(diffs[(0, 2)], 1.5)
- assert almost_equal(diffs[(1, 2)], 0.5)
- assert almost_equal(diffs[(1, 3)], 1.25)
- assert almost_equal(diffs[(2, 3)], 0.75)
+ assert diffs[(0, 1)] == pytest.approx(1, abs=1e-7)
+ assert diffs[(0, 2)] == pytest.approx(1.5, abs=1e-7)
+ assert diffs[(1, 2)] == pytest.approx(0.5, abs=1e-7)
+ assert diffs[(1, 3)] == pytest.approx(1.25, abs=1e-7)
+ assert diffs[(2, 3)] == pytest.approx(0.75, abs=1e-7)
def test_trophic_incoherence_parameter_no_cannibalism():
matrix_a = np.array([[0, 1], [0, 0]])
G = nx.from_numpy_array(matrix_a, create_using=nx.DiGraph)
q = nx.trophic_incoherence_parameter(G, cannibalism=False)
- assert almost_equal(q, 0)
+ assert q == pytest.approx(0, abs=1e-7)
# fmt: off
matrix_b = np.array([
@@ -240,7 +239,7 @@ def test_trophic_incoherence_parameter_no_cannibalism():
# fmt: on
G = nx.from_numpy_array(matrix_b, create_using=nx.DiGraph)
q = nx.trophic_incoherence_parameter(G, cannibalism=False)
- assert almost_equal(q, np.std([1, 1.5, 0.5, 0.75, 1.25]))
+ assert q == pytest.approx(np.std([1, 1.5, 0.5, 0.75, 1.25]), abs=1e-7)
# fmt: off
matrix_c = np.array([
@@ -253,7 +252,7 @@ def test_trophic_incoherence_parameter_no_cannibalism():
G = nx.from_numpy_array(matrix_c, create_using=nx.DiGraph)
q = nx.trophic_incoherence_parameter(G, cannibalism=False)
# Ignore the -link
- assert almost_equal(q, np.std([1, 1.5, 0.5, 0.75, 1.25]))
+ assert q == pytest.approx(np.std([1, 1.5, 0.5, 0.75, 1.25]), abs=1e-7)
# no self-loops case
# fmt: off
@@ -267,14 +266,14 @@ def test_trophic_incoherence_parameter_no_cannibalism():
G = nx.from_numpy_array(matrix_d, create_using=nx.DiGraph)
q = nx.trophic_incoherence_parameter(G, cannibalism=False)
# Ignore the -link
- assert almost_equal(q, np.std([1, 1.5, 0.5, 0.75, 1.25]))
+ assert q == pytest.approx(np.std([1, 1.5, 0.5, 0.75, 1.25]), abs=1e-7)
def test_trophic_incoherence_parameter_cannibalism():
matrix_a = np.array([[0, 1], [0, 0]])
G = nx.from_numpy_array(matrix_a, create_using=nx.DiGraph)
q = nx.trophic_incoherence_parameter(G, cannibalism=True)
- assert almost_equal(q, 0)
+ assert q == pytest.approx(0, abs=1e-7)
# fmt: off
matrix_b = np.array([
@@ -287,7 +286,7 @@ def test_trophic_incoherence_parameter_cannibalism():
# fmt: on
G = nx.from_numpy_array(matrix_b, create_using=nx.DiGraph)
q = nx.trophic_incoherence_parameter(G, cannibalism=True)
- assert almost_equal(q, 2)
+ assert q == pytest.approx(2, abs=1e-7)
# fmt: off
matrix_c = np.array([
@@ -300,4 +299,4 @@ def test_trophic_incoherence_parameter_cannibalism():
G = nx.from_numpy_array(matrix_c, create_using=nx.DiGraph)
q = nx.trophic_incoherence_parameter(G, cannibalism=True)
# Ignore the -link
- assert almost_equal(q, np.std([1, 1.5, 0.5, 0.75, 1.25]))
+ assert q == pytest.approx(np.std([1, 1.5, 0.5, 0.75, 1.25]), abs=1e-7)
diff --git a/networkx/algorithms/community/tests/test_quality.py b/networkx/algorithms/community/tests/test_quality.py
index 79d60e7f..e45d7555 100644
--- a/networkx/algorithms/community/tests/test_quality.py
+++ b/networkx/algorithms/community/tests/test_quality.py
@@ -11,7 +11,6 @@ from networkx.algorithms.community import modularity
from networkx.algorithms.community import performance
from networkx.algorithms.community import partition_quality
from networkx.algorithms.community.quality import inter_community_edges
-from networkx.testing import almost_equal
class TestPerformance:
@@ -21,15 +20,15 @@ class TestPerformance:
"""Tests that a poor partition has a low performance measure."""
G = barbell_graph(3, 0)
partition = [{0, 1, 4}, {2, 3, 5}]
- assert almost_equal(8 / 15, performance(G, partition))
- assert almost_equal(8 / 15, partition_quality(G, partition)[1])
+ assert 8 / 15 == pytest.approx(performance(G, partition), abs=1e-7)
+ assert 8 / 15 == pytest.approx(partition_quality(G, partition)[1], abs=1e-7)
def test_good_partition(self):
"""Tests that a good partition has a high performance measure."""
G = barbell_graph(3, 0)
partition = [{0, 1, 2}, {3, 4, 5}]
- assert almost_equal(14 / 15, performance(G, partition))
- assert almost_equal(14 / 15, partition_quality(G, partition)[1])
+ assert 14 / 15 == pytest.approx(performance(G, partition), abs=1e-7)
+ assert 14 / 15 == pytest.approx(partition_quality(G, partition)[1], abs=1e-7)
class TestCoverage:
@@ -39,39 +38,39 @@ class TestCoverage:
"""Tests that a poor partition has a low coverage measure."""
G = barbell_graph(3, 0)
partition = [{0, 1, 4}, {2, 3, 5}]
- assert almost_equal(3 / 7, coverage(G, partition))
- assert almost_equal(3 / 7, partition_quality(G, partition)[0])
+ assert 3 / 7 == pytest.approx(coverage(G, partition), abs=1e-7)
+ assert 3 / 7 == pytest.approx(partition_quality(G, partition)[0], abs=1e-7)
def test_good_partition(self):
"""Tests that a good partition has a high coverage measure."""
G = barbell_graph(3, 0)
partition = [{0, 1, 2}, {3, 4, 5}]
- assert almost_equal(6 / 7, coverage(G, partition))
- assert almost_equal(6 / 7, partition_quality(G, partition)[0])
+ assert 6 / 7 == pytest.approx(coverage(G, partition), abs=1e-7)
+ assert 6 / 7 == pytest.approx(partition_quality(G, partition)[0], abs=1e-7)
def test_modularity():
G = nx.barbell_graph(3, 0)
C = [{0, 1, 4}, {2, 3, 5}]
- assert almost_equal(-16 / (14 ** 2), modularity(G, C))
+ assert (-16 / (14 ** 2)) == pytest.approx(modularity(G, C), abs=1e-7)
C = [{0, 1, 2}, {3, 4, 5}]
- assert almost_equal((35 * 2) / (14 ** 2), modularity(G, C))
+ assert (35 * 2) / (14 ** 2) == pytest.approx(modularity(G, C), abs=1e-7)
n = 1000
G = nx.erdos_renyi_graph(n, 0.09, seed=42, directed=True)
C = [set(range(n // 2)), set(range(n // 2, n))]
- assert almost_equal(0.00017154251389292754, modularity(G, C))
+ assert 0.00017154251389292754 == pytest.approx(modularity(G, C), abs=1e-7)
G = nx.margulis_gabber_galil_graph(10)
mid_value = G.number_of_nodes() // 2
nodes = list(G.nodes)
C = [set(nodes[:mid_value]), set(nodes[mid_value:])]
- assert almost_equal(0.13, modularity(G, C))
+ assert 0.13 == pytest.approx(modularity(G, C), abs=1e-7)
G = nx.DiGraph()
G.add_edges_from([(2, 1), (2, 3), (3, 4)])
C = [{1, 2}, {3, 4}]
- assert almost_equal(2 / 9, modularity(G, C))
+ assert 2 / 9 == pytest.approx(modularity(G, C), abs=1e-7)
def test_modularity_resolution():
diff --git a/networkx/algorithms/flow/tests/test_maxflow_large_graph.py b/networkx/algorithms/flow/tests/test_maxflow_large_graph.py
index 73f2e0a5..c86aaad4 100644
--- a/networkx/algorithms/flow/tests/test_maxflow_large_graph.py
+++ b/networkx/algorithms/flow/tests/test_maxflow_large_graph.py
@@ -11,7 +11,6 @@ from networkx.algorithms.flow import dinitz
from networkx.algorithms.flow import edmonds_karp
from networkx.algorithms.flow import preflow_push
from networkx.algorithms.flow import shortest_augmenting_path
-from networkx.testing import almost_equal
flow_funcs = [
boykov_kolmogorov,
@@ -97,7 +96,7 @@ class TestMaxflowLargeGraph:
kwargs["flow_func"] = flow_func
errmsg = f"Assertion failed in function: {flow_func.__name__}"
flow_value = nx.maximum_flow_value(G, (0, 0), "t", **kwargs)
- assert almost_equal(flow_value, 1.0), errmsg
+ assert flow_value == pytest.approx(1.0, abs=1e-7)
def test_gl1(self):
G = read_graph("gl1")
diff --git a/networkx/algorithms/link_analysis/tests/test_hits.py b/networkx/algorithms/link_analysis/tests/test_hits.py
index be354086..53a13ab9 100644
--- a/networkx/algorithms/link_analysis/tests/test_hits.py
+++ b/networkx/algorithms/link_analysis/tests/test_hits.py
@@ -4,8 +4,6 @@ import networkx as nx
np = pytest.importorskip("numpy")
pytest.importorskip("scipy")
-from networkx.testing import almost_equal
-
from networkx.algorithms.link_analysis.hits_alg import _hits_python
# Example from
@@ -34,9 +32,9 @@ class TestHITS:
G = self.G
h, a = nx.hits_numpy(G)
for n in G:
- assert almost_equal(h[n], G.h[n], places=4)
+ assert h[n] == pytest.approx(G.h[n], abs=1e-4)
for n in G:
- assert almost_equal(a[n], G.a[n], places=4)
+ assert a[n] == pytest.approx(G.a[n], abs=1e-4)
@pytest.mark.parametrize(
"hits_alg",
@@ -46,15 +44,15 @@ class TestHITS:
G = self.G
h, a = hits_alg(G, tol=1.0e-08)
for n in G:
- assert almost_equal(h[n], G.h[n], places=4)
+ assert h[n] == pytest.approx(G.h[n], abs=1e-4)
for n in G:
- assert almost_equal(a[n], G.a[n], places=4)
+ assert a[n] == pytest.approx(G.a[n], abs=1e-4)
nstart = {i: 1.0 / 2 for i in G}
h, a = hits_alg(G, nstart=nstart)
for n in G:
- assert almost_equal(h[n], G.h[n], places=4)
+ assert h[n] == pytest.approx(G.h[n], abs=1e-4)
for n in G:
- assert almost_equal(a[n], G.a[n], places=4)
+ assert a[n] == pytest.approx(G.a[n], abs=1e-4)
def test_empty(self):
G = nx.Graph()
diff --git a/networkx/algorithms/link_analysis/tests/test_pagerank.py b/networkx/algorithms/link_analysis/tests/test_pagerank.py
index e655db0a..e6a2154c 100644
--- a/networkx/algorithms/link_analysis/tests/test_pagerank.py
+++ b/networkx/algorithms/link_analysis/tests/test_pagerank.py
@@ -6,7 +6,6 @@ import pytest
np = pytest.importorskip("numpy")
pytest.importorskip("scipy")
-from networkx.testing import almost_equal
from networkx.algorithms.link_analysis.pagerank_alg import _pagerank_python
# Example from
@@ -60,12 +59,12 @@ class TestPageRank:
G = self.G
p = alg(G, alpha=0.9, tol=1.0e-08)
for n in G:
- assert almost_equal(p[n], G.pagerank[n], places=4)
+ assert p[n] == pytest.approx(G.pagerank[n], abs=1e-4)
nstart = {n: random.random() for n in G}
p = alg(G, alpha=0.9, tol=1.0e-08, nstart=nstart)
for n in G:
- assert almost_equal(p[n], G.pagerank[n], places=4)
+ assert p[n] == pytest.approx(G.pagerank[n], abs=1e-4)
@pytest.mark.parametrize("alg", (nx.pagerank, _pagerank_python))
def test_pagerank_max_iter(self, alg):
@@ -76,7 +75,7 @@ class TestPageRank:
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)
+ assert p[n] == pytest.approx(G.pagerank[n], abs=1e-4)
def test_google_matrix(self):
G = self.G
@@ -84,7 +83,7 @@ class TestPageRank:
e, ev = np.linalg.eig(M.T)
p = np.array(ev[:, 0] / ev[:, 0].sum())[:, 0]
for (a, b) in zip(p, self.G.pagerank.values()):
- assert almost_equal(a, b)
+ assert a == pytest.approx(b, abs=1e-7)
@pytest.mark.parametrize("alg", (nx.pagerank, _pagerank_python, nx.pagerank_numpy))
def test_personalization(self, alg):
@@ -98,7 +97,7 @@ class TestPageRank:
}
p = alg(G, alpha=0.85, personalization=personalize)
for n in G:
- assert almost_equal(p[n], answer[n], places=4)
+ assert p[n] == pytest.approx(answer[n], abs=1e-4)
@pytest.mark.parametrize("alg", (nx.pagerank, _pagerank_python, nx.google_matrix))
def test_zero_personalization_vector(self, alg):
@@ -118,7 +117,7 @@ class TestPageRank:
}
p = alg(G, alpha=0.85, personalization=personalize)
for n in G:
- assert almost_equal(p[n], answer[n], places=4)
+ assert p[n] == pytest.approx(answer[n], abs=1e-4)
@pytest.mark.parametrize("alg", (nx.pagerank, _pagerank_python))
def test_incomplete_personalization(self, alg):
@@ -132,7 +131,7 @@ class TestPageRank:
}
p = alg(G, alpha=0.85, personalization=personalize)
for n in G:
- assert almost_equal(p[n], answer[n], places=4)
+ assert p[n] == pytest.approx(answer[n], abs=1e-4)
def test_dangling_matrix(self):
"""
@@ -147,17 +146,17 @@ class TestPageRank:
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
+ assert M2[i, j] == pytest.approx(
+ dangling[j + 1] / dangling_sum, abs=1e-4
)
else:
- assert almost_equal(M2[i, j], M1[i, j], places=4)
+ assert M2[i, j] == pytest.approx(M1[i, j], abs=1e-4)
@pytest.mark.parametrize("alg", (nx.pagerank, _pagerank_python, nx.pagerank_numpy))
def test_dangling_pagerank(self, alg):
pr = alg(self.G, dangling=self.dangling_edges)
for n in self.G:
- assert almost_equal(pr[n], self.G.dangling_pagerank[n], places=4)
+ assert pr[n] == pytest.approx(self.G.dangling_pagerank[n], abs=1e-4)
def test_empty(self):
G = nx.Graph()
@@ -178,7 +177,7 @@ class TestPageRank:
}
p = alg(G)
for n in G:
- assert almost_equal(p[n], answer[n], places=4)
+ assert p[n] == pytest.approx(answer[n], abs=1e-4)
class TestPageRankScipy(TestPageRank):
@@ -186,14 +185,14 @@ class TestPageRankScipy(TestPageRank):
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)
+ assert p[n] == pytest.approx(G.pagerank[n], abs=1e-4)
personalize = {n: random.random() for n in G}
p = nx.pagerank_scipy(G, alpha=0.9, tol=1.0e-08, personalization=personalize)
nstart = {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)
+ assert p[n] == pytest.approx(G.pagerank[n], abs=1e-4)
def test_scipy_pagerank_max_iter(self):
with pytest.raises(nx.PowerIterationFailedConvergence):
@@ -202,7 +201,7 @@ class TestPageRankScipy(TestPageRank):
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)
+ assert pr[n] == pytest.approx(self.G.dangling_pagerank[n], abs=1e-4)
def test_empty_scipy(self):
G = nx.Graph()
diff --git a/networkx/algorithms/shortest_paths/tests/test_generic.py b/networkx/algorithms/shortest_paths/tests/test_generic.py
index 2a7b15ff..92902174 100644
--- a/networkx/algorithms/shortest_paths/tests/test_generic.py
+++ b/networkx/algorithms/shortest_paths/tests/test_generic.py
@@ -2,7 +2,6 @@ import pytest
import networkx as nx
-from networkx.testing import almost_equal
def validate_grid_path(r, c, s, t, p):
@@ -287,44 +286,44 @@ class TestGenericPath:
class TestAverageShortestPathLength:
def test_cycle_graph(self):
ans = nx.average_shortest_path_length(nx.cycle_graph(7))
- assert almost_equal(ans, 2)
+ assert ans == pytest.approx(2, abs=1e-7)
def test_path_graph(self):
ans = nx.average_shortest_path_length(nx.path_graph(5))
- assert almost_equal(ans, 2)
+ assert ans == pytest.approx(2, abs=1e-7)
def test_weighted(self):
G = nx.Graph()
nx.add_cycle(G, range(7), weight=2)
ans = nx.average_shortest_path_length(G, weight="weight")
- assert almost_equal(ans, 4)
+ assert ans == pytest.approx(4, abs=1e-7)
G = nx.Graph()
nx.add_path(G, range(5), weight=2)
ans = nx.average_shortest_path_length(G, weight="weight")
- assert almost_equal(ans, 4)
+ assert ans == pytest.approx(4, abs=1e-7)
def test_specified_methods(self):
G = nx.Graph()
nx.add_cycle(G, range(7), weight=2)
ans = nx.average_shortest_path_length(G, weight="weight", method="dijkstra")
- assert almost_equal(ans, 4)
+ assert ans == pytest.approx(4, abs=1e-7)
ans = nx.average_shortest_path_length(G, weight="weight", method="bellman-ford")
- assert almost_equal(ans, 4)
+ assert ans == pytest.approx(4, abs=1e-7)
ans = nx.average_shortest_path_length(
G, weight="weight", method="floyd-warshall"
)
- assert almost_equal(ans, 4)
+ assert ans == pytest.approx(4, abs=1e-7)
G = nx.Graph()
nx.add_path(G, range(5), weight=2)
ans = nx.average_shortest_path_length(G, weight="weight", method="dijkstra")
- assert almost_equal(ans, 4)
+ assert ans == pytest.approx(4, abs=1e-7)
ans = nx.average_shortest_path_length(G, weight="weight", method="bellman-ford")
- assert almost_equal(ans, 4)
+ assert ans == pytest.approx(4, abs=1e-7)
ans = nx.average_shortest_path_length(
G, weight="weight", method="floyd-warshall"
)
- assert almost_equal(ans, 4)
+ assert ans == pytest.approx(4, abs=1e-7)
def test_disconnected(self):
g = nx.Graph()
diff --git a/networkx/algorithms/tests/test_boundary.py b/networkx/algorithms/tests/test_boundary.py
index 46dd0c52..16ac8eab 100644
--- a/networkx/algorithms/tests/test_boundary.py
+++ b/networkx/algorithms/tests/test_boundary.py
@@ -1,9 +1,9 @@
"""Unit tests for the :mod:`networkx.algorithms.boundary` module."""
from itertools import combinations
-
+import pytest
import networkx as nx
-from networkx.testing import almost_equal, assert_edges_equal
+from networkx.testing import assert_edges_equal
from networkx import convert_node_labels_to_integers as cnlti
@@ -51,11 +51,11 @@ class TestNodeBoundary:
return min(len(nx.node_boundary(G, nn)) / k for nn in combinations(G, k))
P = nx.petersen_graph()
- assert almost_equal(cheeger(P, 1), 3.00, places=2)
- assert almost_equal(cheeger(P, 2), 2.00, places=2)
- assert almost_equal(cheeger(P, 3), 1.67, places=2)
- assert almost_equal(cheeger(P, 4), 1.00, places=2)
- assert almost_equal(cheeger(P, 5), 0.80, places=2)
+ assert cheeger(P, 1) == pytest.approx(3.00, abs=1e-2)
+ assert cheeger(P, 2) == pytest.approx(2.00, abs=1e-2)
+ assert cheeger(P, 3) == pytest.approx(1.67, abs=1e-2)
+ assert cheeger(P, 4) == pytest.approx(1.00, abs=1e-2)
+ assert cheeger(P, 5) == pytest.approx(0.80, abs=1e-2)
def test_directed(self):
"""Tests the node boundary of a directed graph."""
diff --git a/networkx/algorithms/tests/test_communicability.py b/networkx/algorithms/tests/test_communicability.py
index 00d30136..bf219888 100644
--- a/networkx/algorithms/tests/test_communicability.py
+++ b/networkx/algorithms/tests/test_communicability.py
@@ -6,7 +6,6 @@ pytest.importorskip("numpy")
pytest.importorskip("scipy")
import networkx as nx
-from networkx.testing import almost_equal
from networkx.algorithms.communicability_alg import communicability, communicability_exp
@@ -24,7 +23,7 @@ class TestCommunicability:
result = communicability(nx.path_graph(2))
for k1, val in result.items():
for k2 in val:
- assert almost_equal(answer[k1][k2], result[k1][k2], places=7)
+ assert answer[k1][k2] == pytest.approx(result[k1][k2], abs=1e-7)
def test_communicability2(self):
@@ -74,9 +73,9 @@ class TestCommunicability:
result = communicability(G1)
for k1, val in result.items():
for k2 in val:
- assert almost_equal(answer[k1][k2], result[k1][k2], places=7)
+ assert answer[k1][k2] == pytest.approx(result[k1][k2], abs=1e-7)
result = communicability_exp(G1)
for k1, val in result.items():
for k2 in val:
- assert almost_equal(answer[k1][k2], result[k1][k2], places=7)
+ assert answer[k1][k2] == pytest.approx(result[k1][k2], abs=1e-7)
diff --git a/networkx/algorithms/tests/test_link_prediction.py b/networkx/algorithms/tests/test_link_prediction.py
index cb3e58c0..34b58c4c 100644
--- a/networkx/algorithms/tests/test_link_prediction.py
+++ b/networkx/algorithms/tests/test_link_prediction.py
@@ -13,7 +13,7 @@ def _test_func(G, ebunch, expected, predict_func, **kwargs):
assert len(exp_dict) == len(res_dict)
for p in exp_dict:
- assert nx.testing.almost_equal(exp_dict[p], res_dict[p])
+ assert nx.testing.almost_equal(exp_dict[p], res_dict[p], places=7)
class TestResourceAllocationIndex:
diff --git a/networkx/algorithms/tests/test_structuralholes.py b/networkx/algorithms/tests/test_structuralholes.py
index a0499e65..31f62416 100644
--- a/networkx/algorithms/tests/test_structuralholes.py
+++ b/networkx/algorithms/tests/test_structuralholes.py
@@ -1,7 +1,7 @@
"""Unit tests for the :mod:`networkx.algorithms.structuralholes` module."""
import math
+import pytest
import networkx as nx
-from networkx.testing import almost_equal
class TestStructuralHoles:
@@ -51,67 +51,67 @@ class TestStructuralHoles:
def test_constraint_directed(self):
constraint = nx.constraint(self.D)
- assert almost_equal(constraint[0], 1.003, places=3)
- assert almost_equal(constraint[1], 1.003, places=3)
- assert almost_equal(constraint[2], 1.389, places=3)
+ assert constraint[0] == pytest.approx(1.003, abs=1e-3)
+ assert constraint[1] == pytest.approx(1.003, abs=1e-3)
+ assert constraint[2] == pytest.approx(1.389, abs=1e-3)
def test_effective_size_directed(self):
effective_size = nx.effective_size(self.D)
- assert almost_equal(effective_size[0], 1.167, places=3)
- assert almost_equal(effective_size[1], 1.167, places=3)
- assert almost_equal(effective_size[2], 1, places=3)
+ assert effective_size[0] == pytest.approx(1.167, abs=1e-3)
+ assert effective_size[1] == pytest.approx(1.167, abs=1e-3)
+ assert effective_size[2] == pytest.approx(1, abs=1e-3)
def test_constraint_weighted_directed(self):
D = self.D.copy()
nx.set_edge_attributes(D, self.D_weights, "weight")
constraint = nx.constraint(D, weight="weight")
- assert almost_equal(constraint[0], 0.840, places=3)
- assert almost_equal(constraint[1], 1.143, places=3)
- assert almost_equal(constraint[2], 1.378, places=3)
+ assert constraint[0] == pytest.approx(0.840, abs=1e-3)
+ assert constraint[1] == pytest.approx(1.143, abs=1e-3)
+ assert constraint[2] == pytest.approx(1.378, abs=1e-3)
def test_effective_size_weighted_directed(self):
D = self.D.copy()
nx.set_edge_attributes(D, self.D_weights, "weight")
effective_size = nx.effective_size(D, weight="weight")
- assert almost_equal(effective_size[0], 1.567, places=3)
- assert almost_equal(effective_size[1], 1.083, places=3)
- assert almost_equal(effective_size[2], 1, places=3)
+ assert effective_size[0] == pytest.approx(1.567, abs=1e-3)
+ assert effective_size[1] == pytest.approx(1.083, abs=1e-3)
+ assert effective_size[2] == pytest.approx(1, abs=1e-3)
def test_constraint_undirected(self):
constraint = nx.constraint(self.G)
- assert almost_equal(constraint["G"], 0.400, places=3)
- assert almost_equal(constraint["A"], 0.595, places=3)
- assert almost_equal(constraint["C"], 1, places=3)
+ assert constraint["G"] == pytest.approx(0.400, abs=1e-3)
+ assert constraint["A"] == pytest.approx(0.595, abs=1e-3)
+ assert constraint["C"] == pytest.approx(1, abs=1e-3)
def test_effective_size_undirected_borgatti(self):
effective_size = nx.effective_size(self.G)
- assert almost_equal(effective_size["G"], 4.67, places=2)
- assert almost_equal(effective_size["A"], 2.50, places=2)
- assert almost_equal(effective_size["C"], 1, places=2)
+ assert effective_size["G"] == pytest.approx(4.67, abs=1e-2)
+ assert effective_size["A"] == pytest.approx(2.50, abs=1e-2)
+ assert effective_size["C"] == pytest.approx(1, abs=1e-2)
def test_effective_size_undirected(self):
G = self.G.copy()
nx.set_edge_attributes(G, 1, "weight")
effective_size = nx.effective_size(G, weight="weight")
- assert almost_equal(effective_size["G"], 4.67, places=2)
- assert almost_equal(effective_size["A"], 2.50, places=2)
- assert almost_equal(effective_size["C"], 1, places=2)
+ assert effective_size["G"] == pytest.approx(4.67, abs=1e-2)
+ assert effective_size["A"] == pytest.approx(2.50, abs=1e-2)
+ assert effective_size["C"] == pytest.approx(1, abs=1e-2)
def test_constraint_weighted_undirected(self):
G = self.G.copy()
nx.set_edge_attributes(G, self.G_weights, "weight")
constraint = nx.constraint(G, weight="weight")
- assert almost_equal(constraint["G"], 0.299, places=3)
- assert almost_equal(constraint["A"], 0.795, places=3)
- assert almost_equal(constraint["C"], 1, places=3)
+ assert constraint["G"] == pytest.approx(0.299, abs=1e-3)
+ assert constraint["A"] == pytest.approx(0.795, abs=1e-3)
+ assert constraint["C"] == pytest.approx(1, abs=1e-3)
def test_effective_size_weighted_undirected(self):
G = self.G.copy()
nx.set_edge_attributes(G, self.G_weights, "weight")
effective_size = nx.effective_size(G, weight="weight")
- assert almost_equal(effective_size["G"], 5.47, places=2)
- assert almost_equal(effective_size["A"], 2.47, places=2)
- assert almost_equal(effective_size["C"], 1, places=2)
+ assert effective_size["G"] == pytest.approx(5.47, abs=1e-2)
+ assert effective_size["A"] == pytest.approx(2.47, abs=1e-2)
+ assert effective_size["C"] == pytest.approx(1, abs=1e-2)
def test_constraint_isolated(self):
G = self.G.copy()
diff --git a/networkx/algorithms/tests/test_threshold.py b/networkx/algorithms/tests/test_threshold.py
index 8bba956c..fdd03c19 100644
--- a/networkx/algorithms/tests/test_threshold.py
+++ b/networkx/algorithms/tests/test_threshold.py
@@ -8,7 +8,6 @@ import pytest
import networkx as nx
import networkx.algorithms.threshold as nxt
from networkx.algorithms.isomorphism.isomorph import graph_could_be_isomorphic
-from networkx.testing import almost_equal
cnlti = nx.convert_node_labels_to_integers
@@ -224,7 +223,7 @@ class TestGeneratorThreshold:
c1 = nxt.cluster_sequence(cs)
c2 = list(nx.clustering(G).values())
- assert almost_equal(sum([abs(c - d) for c, d in zip(c1, c2)]), 0)
+ assert sum([abs(c - d) for c, d in zip(c1, c2)]) == pytest.approx(0, abs=1e-7)
b1 = nx.betweenness_centrality(G).values()
b2 = nxt.betweenness_sequence(cs)
diff --git a/networkx/conftest.py b/networkx/conftest.py
index 8b2bad9c..0d39779c 100644
--- a/networkx/conftest.py
+++ b/networkx/conftest.py
@@ -136,6 +136,9 @@ def set_warnings():
warnings.filterwarnings(
"ignore", category=DeprecationWarning, message="preserve_random_state"
)
+ warnings.filterwarnings(
+ "ignore", category=DeprecationWarning, message="`almost_equal`"
+ )
@pytest.fixture(autouse=True)
diff --git a/networkx/drawing/tests/test_layout.py b/networkx/drawing/tests/test_layout.py
index c232ace9..e878f5e7 100644
--- a/networkx/drawing/tests/test_layout.py
+++ b/networkx/drawing/tests/test_layout.py
@@ -1,6 +1,5 @@
"""Unit tests for layout functions."""
import networkx as nx
-from networkx.testing import almost_equal
import pytest
@@ -196,7 +195,7 @@ class TestLayout:
pos = nx.circular_layout(self.bigG)
npos = nx.spring_layout(self.bigG, pos=pos, fixed=[(0, 0)])
for axis in range(2):
- assert almost_equal(pos[(0, 0)][axis], npos[(0, 0)][axis])
+ assert pos[(0, 0)][axis] == pytest.approx(npos[(0, 0)][axis], abs=1e-7)
def test_center_parameter(self):
G = nx.path_graph(1)
@@ -315,9 +314,9 @@ class TestLayout:
cost, grad = costfn(pos, np, invdist, meanweight=0, dim=1)
- assert almost_equal(cost, ((3 / 2.0 - 1) ** 2))
- assert almost_equal(grad[0], -0.5)
- assert almost_equal(grad[1], 0.5)
+ assert cost == pytest.approx(((3 / 2.0 - 1) ** 2), abs=1e-7)
+ assert grad[0] == pytest.approx((-0.5), abs=1e-7)
+ assert grad[1] == pytest.approx(0.5, abs=1e-7)
def check_kamada_kawai_costfn(self, pos, invdist, meanwt, dim):
costfn = nx.drawing.layout._kamada_kawai_costfn
@@ -330,7 +329,7 @@ class TestLayout:
diff = np.linalg.norm(pos[i] - pos[j])
expected_cost += (diff * invdist[i][j] - 1.0) ** 2
- assert almost_equal(cost, expected_cost)
+ assert cost == pytest.approx(expected_cost, abs=1e-7)
dx = 1e-4
for nd in range(pos.shape[0]):
@@ -344,7 +343,7 @@ class TestLayout:
ps[idx] -= 2 * dx
cminus = costfn(ps, np, invdist, meanweight=meanwt, dim=pos.shape[1])[0]
- assert almost_equal(grad[idx], (cplus - cminus) / (2 * dx), places=5)
+ assert grad[idx] == pytest.approx((cplus - cminus) / (2 * dx), abs=1e-5)
def test_kamada_kawai_costfn(self):
invdist = 1 / np.array([[0.1, 2.1, 1.7], [2.1, 0.2, 0.6], [1.7, 0.6, 0.3]])
@@ -379,8 +378,8 @@ class TestLayout:
distances_equidistant = self.collect_node_distances(pos_equidistant)
for d in range(1, len(distances_equidistant) - 1):
# test similarity to two decimal places
- assert almost_equal(
- distances_equidistant[d], distances_equidistant[d + 1], 2
+ assert distances_equidistant[d] == pytest.approx(
+ distances_equidistant[d + 1], abs=1e-2
)
def test_rescale_layout_dict(self):
diff --git a/networkx/generators/tests/test_internet_as_graphs.py b/networkx/generators/tests/test_internet_as_graphs.py
index 5eae15ba..08e54dd7 100644
--- a/networkx/generators/tests/test_internet_as_graphs.py
+++ b/networkx/generators/tests/test_internet_as_graphs.py
@@ -1,6 +1,6 @@
+from pytest import approx
from networkx import is_connected, neighbors
from networkx.generators.internet_as_graphs import random_internet_as_graph
-from networkx.testing import almost_equal
class TestInternetASTopology:
@@ -174,16 +174,16 @@ class TestInternetASTopology:
attributes"
)
- assert almost_equal(d_m / len(self.M), 2 + (2.5 * self.n) / 10000, places=0)
- assert almost_equal(d_cp / len(self.CP), 2 + (1.5 * self.n) / 10000, places=0)
- assert almost_equal(d_c / len(self.C), 1 + (5 * self.n) / 100000, places=0)
+ assert d_m / len(self.M) == approx((2 + (2.5 * self.n) / 10000), abs=1e-0)
+ assert d_cp / len(self.CP) == approx((2 + (1.5 * self.n) / 10000), abs=1e-0)
+ assert d_c / len(self.C) == approx((1 + (5 * self.n) / 100000), abs=1e-0)
- assert almost_equal(p_m_m / len(self.M), 1 + (2 * self.n) / 10000, places=0)
- assert almost_equal(p_cp_m / len(self.CP), 0.2 + (2 * self.n) / 10000, places=0)
- assert almost_equal(
- p_cp_cp / len(self.CP), 0.05 + (2 * self.n) / 100000, places=0
+ assert p_m_m / len(self.M) == approx((1 + (2 * self.n) / 10000), abs=1e-0)
+ assert p_cp_m / len(self.CP) == approx((0.2 + (2 * self.n) / 10000), abs=1e-0)
+ assert p_cp_cp / len(self.CP) == approx(
+ (0.05 + (2 * self.n) / 100000), abs=1e-0
)
- assert almost_equal(t_m / d_m, 0.375, places=1)
- assert almost_equal(t_cp / d_cp, 0.375, places=1)
- assert almost_equal(t_c / d_c, 0.125, places=1)
+ assert t_m / d_m == approx(0.375, abs=1e-1)
+ assert t_cp / d_cp == approx(0.375, abs=1e-1)
+ assert t_c / d_c == approx(0.125, abs=1e-1)
diff --git a/networkx/linalg/tests/test_algebraic_connectivity.py b/networkx/linalg/tests/test_algebraic_connectivity.py
index e95d0cff..ddfe8f53 100644
--- a/networkx/linalg/tests/test_algebraic_connectivity.py
+++ b/networkx/linalg/tests/test_algebraic_connectivity.py
@@ -6,7 +6,6 @@ np = pytest.importorskip("numpy")
import networkx as nx
-from networkx.testing import almost_equal
methods = ("tracemin_pcg", "tracemin_lu", "lanczos", "lobpcg")
@@ -50,13 +49,13 @@ def test_fiedler_vector_tracemin_unknown():
def check_eigenvector(A, l, x):
nx = np.linalg.norm(x)
# Check zeroness.
- assert not almost_equal(nx, 0)
+ assert not nx == pytest.approx(0, abs=1e-7)
y = A * x
ny = np.linalg.norm(y)
# Check collinearity.
- assert almost_equal(x @ y, nx * ny)
+ assert x @ y == pytest.approx(nx * ny, abs=1e-7)
# Check eigenvalue.
- assert almost_equal(ny, l * nx)
+ assert ny == pytest.approx(l * nx, abs=1e-7)
class TestAlgebraicConnectivity:
@@ -99,7 +98,9 @@ class TestAlgebraicConnectivity:
G = nx.Graph()
G.add_edge(0, 1, weight=1)
A = nx.laplacian_matrix(G)
- assert almost_equal(nx.algebraic_connectivity(G, tol=1e-12, method=method), 2)
+ assert nx.algebraic_connectivity(G, tol=1e-12, method=method) == pytest.approx(
+ 2, abs=1e-7
+ )
x = nx.fiedler_vector(G, tol=1e-12, method=method)
check_eigenvector(A, 2, x)
@@ -111,9 +112,9 @@ class TestAlgebraicConnectivity:
G.add_edge(0, 1, spam=1)
G.add_edge(0, 1, spam=-2)
A = -3 * nx.laplacian_matrix(G, weight="spam")
- assert almost_equal(
- nx.algebraic_connectivity(G, weight="spam", tol=1e-12, method=method), 6
- )
+ assert nx.algebraic_connectivity(
+ G, weight="spam", tol=1e-12, method=method
+ ) == pytest.approx(6, abs=1e-7)
x = nx.fiedler_vector(G, weight="spam", tol=1e-12, method=method)
check_eigenvector(A, 6, x)
@@ -123,7 +124,7 @@ class TestAlgebraicConnectivity:
A = nx.laplacian_matrix(G)
sigma = 2 - sqrt(2 + sqrt(2))
ac = nx.algebraic_connectivity(G, tol=1e-12, method="tracemin")
- assert almost_equal(ac, sigma)
+ assert ac == pytest.approx(sigma, abs=1e-7)
x = nx.fiedler_vector(G, tol=1e-12, method="tracemin")
check_eigenvector(A, sigma, x)
@@ -134,7 +135,7 @@ class TestAlgebraicConnectivity:
A = nx.laplacian_matrix(G)
sigma = 2 - sqrt(2 + sqrt(2))
ac = nx.algebraic_connectivity(G, tol=1e-12, method=method)
- assert almost_equal(ac, sigma)
+ assert ac == pytest.approx(sigma, abs=1e-7)
x = nx.fiedler_vector(G, tol=1e-12, method=method)
check_eigenvector(A, sigma, x)
@@ -146,7 +147,7 @@ class TestAlgebraicConnectivity:
A = nx.laplacian_matrix(G)
sigma = 0.438447187191
ac = nx.algebraic_connectivity(G, tol=1e-12, method=method)
- assert almost_equal(ac, sigma)
+ assert ac == pytest.approx(sigma, abs=1e-7)
x = nx.fiedler_vector(G, tol=1e-12, method=method)
check_eigenvector(A, sigma, x)
@@ -157,7 +158,7 @@ class TestAlgebraicConnectivity:
A = nx.laplacian_matrix(G)
sigma = 2 - sqrt(2)
ac = nx.algebraic_connectivity(G, tol=1e-12, method=method)
- assert almost_equal(ac, sigma)
+ assert ac == pytest.approx(sigma, abs=1e-7)
x = nx.fiedler_vector(G, tol=1e-12, method=method)
check_eigenvector(A, sigma, x)
@@ -168,7 +169,7 @@ class TestAlgebraicConnectivity:
A = nx.laplacian_matrix(G)
sigma = 2 - sqrt(2)
ac = nx.algebraic_connectivity(G, tol=1e-12, method=method, seed=1)
- assert almost_equal(ac, sigma)
+ assert ac == pytest.approx(sigma, abs=1e-7)
x = nx.fiedler_vector(G, tol=1e-12, method=method, seed=1)
check_eigenvector(A, sigma, x)
@@ -278,12 +279,9 @@ class TestAlgebraicConnectivity:
)
A = laplacian_fn(G)
try:
- assert almost_equal(
- nx.algebraic_connectivity(
- G, normalized=normalized, tol=1e-12, method=method
- ),
- sigma,
- )
+ assert nx.algebraic_connectivity(
+ G, normalized=normalized, tol=1e-12, method=method
+ ) == pytest.approx(sigma, abs=1e-7)
x = nx.fiedler_vector(G, normalized=normalized, tol=1e-12, method=method)
check_eigenvector(A, sigma, x)
except nx.NetworkXError as e:
diff --git a/networkx/readwrite/tests/test_graphml.py b/networkx/readwrite/tests/test_graphml.py
index 019a7e5c..75f1a91b 100644
--- a/networkx/readwrite/tests/test_graphml.py
+++ b/networkx/readwrite/tests/test_graphml.py
@@ -5,7 +5,6 @@ from networkx.readwrite.graphml import GraphMLWriter
import io
import tempfile
import os
-from networkx.testing import almost_equal
class BaseGraphML:
@@ -1258,7 +1257,7 @@ class TestWriteGraphML(BaseGraphML):
assert G.edges == H.edges
wtG = G[1][2]["weight"]
wtH = H[1][2]["weight"]
- assert almost_equal(wtG, wtH, places=6)
+ assert wtG == pytest.approx(wtH, abs=1e-6)
assert type(wtG) == np.float64
assert type(wtH) == float
os.close(fd)
@@ -1274,7 +1273,7 @@ class TestWriteGraphML(BaseGraphML):
assert G.edges == H.edges
wtG = G[1][2]["weight"]
wtH = H[1][2]["weight"]
- assert almost_equal(wtG, wtH, places=6)
+ assert wtG == pytest.approx(wtH, abs=1e-6)
assert type(wtG) == np.float32
assert type(wtH) == float
os.close(fd)
diff --git a/networkx/testing/utils.py b/networkx/testing/utils.py
index 795cefad..68faf230 100644
--- a/networkx/testing/utils.py
+++ b/networkx/testing/utils.py
@@ -7,6 +7,15 @@ __all__ = [
def almost_equal(x, y, places=7):
+ import warnings
+
+ warnings.warn(
+ (
+ "`almost_equal` is deprecated and will be removed in version 3.0.\n"
+ "Use `pytest.approx` instead.\n"
+ ),
+ DeprecationWarning,
+ )
return round(abs(x - y), places) == 0