summaryrefslogtreecommitdiff
path: root/networkx/linalg
diff options
context:
space:
mode:
authorJarrod Millman <jarrod.millman@gmail.com>2020-07-09 22:37:40 -0700
committerJarrod Millman <jarrod.millman@gmail.com>2020-07-10 09:34:39 -0700
commitf30e9392bef0dccbcfd1b73ccb934064f6200fa3 (patch)
tree21b7905bf57228859d6df16c073eeb894437ab6a /networkx/linalg
parentcea08c3bb8ca5aa2e167d534b0c5629205733762 (diff)
downloadnetworkx-f30e9392bef0dccbcfd1b73ccb934064f6200fa3.tar.gz
Tell psf/black to ignore specific np.arrays
Diffstat (limited to 'networkx/linalg')
-rw-r--r--networkx/linalg/tests/test_attrmatrix.py10
-rw-r--r--networkx/linalg/tests/test_bethehessian.py2
-rw-r--r--networkx/linalg/tests/test_graphmatrix.py10
-rw-r--r--networkx/linalg/tests/test_laplacian.py18
-rw-r--r--networkx/linalg/tests/test_modularity.py6
5 files changed, 46 insertions, 0 deletions
diff --git a/networkx/linalg/tests/test_attrmatrix.py b/networkx/linalg/tests/test_attrmatrix.py
index 1fe2412e..1e87f2cb 100644
--- a/networkx/linalg/tests/test_attrmatrix.py
+++ b/networkx/linalg/tests/test_attrmatrix.py
@@ -31,11 +31,13 @@ def test_attr_matrix_directed():
G.add_edge(0, 2, thickness=2)
G.add_edge(1, 2, thickness=3)
M = nx.attr_matrix(G, rc_order=[0, 1, 2])
+ # fmt: off
data = np.array(
[[0., 1., 1.],
[0., 0., 1.],
[0., 0., 0.]]
)
+ # fmt: on
npt.assert_equal(M, np.array(data))
@@ -47,25 +49,31 @@ def test_attr_matrix_multigraph():
G.add_edge(0, 2, thickness=2)
G.add_edge(1, 2, thickness=3)
M = nx.attr_matrix(G, rc_order=[0, 1, 2])
+ # fmt: off
data = np.array(
[[0., 3., 1.],
[3., 0., 1.],
[1., 1., 0.]]
)
+ # fmt: on
npt.assert_equal(M, np.array(data))
M = nx.attr_matrix(G, edge_attr="weight", rc_order=[0, 1, 2])
+ # fmt: off
data = np.array(
[[0., 9., 1.],
[9., 0., 1.],
[1., 1., 0.]]
)
+ # fmt: on
npt.assert_equal(M, np.array(data))
M = nx.attr_matrix(G, edge_attr="thickness", rc_order=[0, 1, 2])
+ # fmt: off
data = np.array(
[[0., 3., 2.],
[3., 0., 3.],
[2., 3., 0.]]
)
+ # fmt: on
npt.assert_equal(M, np.array(data))
@@ -90,9 +98,11 @@ def test_attr_sparse_matrix_directed():
G.add_edge(0, 2, thickness=2)
G.add_edge(1, 2, thickness=3)
M = nx.attr_sparse_matrix(G, rc_order=[0, 1, 2])
+ # fmt: off
data = np.array(
[[0., 1., 1.],
[0., 0., 1.],
[0., 0., 0.]]
)
+ # fmt: on
npt.assert_equal(M.todense(), np.array(data))
diff --git a/networkx/linalg/tests/test_bethehessian.py b/networkx/linalg/tests/test_bethehessian.py
index 61fd1f65..64644ba4 100644
--- a/networkx/linalg/tests/test_bethehessian.py
+++ b/networkx/linalg/tests/test_bethehessian.py
@@ -17,9 +17,11 @@ class TestBetheHessian:
def test_bethe_hessian(self):
"Bethe Hessian matrix"
+ # fmt: off
H = np.array([[4, -2, 0],
[-2, 5, -2],
[0, -2, 4]])
+ # fmt: on
permutation = [2, 0, 1]
# Bethe Hessian gives expected form
npt.assert_equal(nx.bethe_hessian_matrix(self.P, r=2).todense(), H)
diff --git a/networkx/linalg/tests/test_graphmatrix.py b/networkx/linalg/tests/test_graphmatrix.py
index 28f96acc..fdf3c640 100644
--- a/networkx/linalg/tests/test_graphmatrix.py
+++ b/networkx/linalg/tests/test_graphmatrix.py
@@ -16,6 +16,7 @@ def test_incidence_matrix_simple():
MG = nx.random_clustered_graph(deg, seed=42)
I = nx.incidence_matrix(G).todense().astype(int)
+ # fmt: off
expected = np.array(
[[1, 1, 1, 0],
[0, 1, 0, 1],
@@ -23,9 +24,11 @@ def test_incidence_matrix_simple():
[0, 0, 1, 0],
[0, 0, 0, 0]]
)
+ # fmt: on
npt.assert_equal(I, expected)
I = nx.incidence_matrix(MG).todense().astype(int)
+ # fmt: off
expected = np.array(
[[1, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0],
@@ -36,6 +39,7 @@ def test_incidence_matrix_simple():
[0, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 1, 0, 1]]
)
+ # fmt: on
npt.assert_equal(I, expected)
with pytest.raises(NetworkXError):
@@ -47,6 +51,7 @@ class TestGraphMatrix:
def setup_class(cls):
deg = [3, 2, 2, 1, 0]
cls.G = havel_hakimi_graph(deg)
+ # fmt: off
cls.OI = np.array(
[[-1, -1, -1, 0],
[1, 0, 0, -1],
@@ -61,10 +66,12 @@ class TestGraphMatrix:
[1, 0, 0, 0, 0],
[0, 0, 0, 0, 0]]
)
+ # fmt: on
cls.WG = havel_hakimi_graph(deg)
cls.WG.add_edges_from(
(u, v, {"weight": 0.5, "other": 0.3}) for (u, v) in cls.G.edges()
)
+ # fmt: off
cls.WA = np.array(
[[0, 0.5, 0.5, 0.5, 0],
[0.5, 0, 0.5, 0, 0],
@@ -72,9 +79,11 @@ class TestGraphMatrix:
[0.5, 0, 0, 0, 0],
[0, 0, 0, 0, 0]]
)
+ # fmt: on
cls.MG = nx.MultiGraph(cls.G)
cls.MG2 = cls.MG.copy()
cls.MG2.add_edge(0, 1)
+ # fmt: off
cls.MG2A = np.array(
[[0, 2, 1, 1, 0],
[2, 0, 1, 0, 0],
@@ -89,6 +98,7 @@ class TestGraphMatrix:
[0, 0, 0, 1, 0],
[0, 0, 0, 0, 0]]
)
+ # fmt: on
cls.no_edges_G = nx.Graph([(1, 2), (3, 2, {"weight": 8})])
cls.no_edges_A = np.array([[0, 0], [0, 0]])
diff --git a/networkx/linalg/tests/test_laplacian.py b/networkx/linalg/tests/test_laplacian.py
index 7d76fdf7..16dac8c6 100644
--- a/networkx/linalg/tests/test_laplacian.py
+++ b/networkx/linalg/tests/test_laplacian.py
@@ -28,11 +28,13 @@ class TestLaplacian:
def test_laplacian(self):
"Graph Laplacian"
+ # fmt: off
NL = np.array([[3, -1, -1, -1, 0],
[-1, 2, -1, 0, 0],
[-1, -1, 2, 0, 0],
[-1, 0, 0, 1, 0],
[0, 0, 0, 0, 0]])
+ # fmt: on
WL = 0.5 * NL
OL = 0.3 * NL
npt.assert_equal(nx.laplacian_matrix(self.G).todense(), NL)
@@ -47,6 +49,7 @@ class TestLaplacian:
def test_normalized_laplacian(self):
"Generalized Graph Laplacian"
+ # fmt: off
G = np.array([[ 1. , -0.408, -0.408, -0.577, 0.],
[-0.408, 1. , -0.5 , 0. , 0.],
[-0.408, -0.5 , 1. , 0. , 0.],
@@ -62,6 +65,7 @@ class TestLaplacian:
[-0.2887, -0.3333, 0.6667, 0., 0.],
[-0.3536, 0., 0., 0.5, 0.],
[0., 0., 0., 0., 0.]])
+ # fmt: on
npt.assert_almost_equal(
nx.normalized_laplacian_matrix(self.G, nodelist=range(5)).todense(), G, decimal=3
@@ -102,32 +106,38 @@ class TestLaplacian:
(6, 4),
)
)
+ # fmt: off
GL = np.array([[0.9833, -0.2941, -0.3882, -0.0291, -0.0231, -0.0261],
[-0.2941, 0.8333, -0.2339, -0.0536, -0.0589, -0.0554],
[-0.3882, -0.2339, 0.9833, -0.0278, -0.0896, -0.0251],
[-0.0291, -0.0536, -0.0278, 0.9833, -0.4878, -0.6675],
[-0.0231, -0.0589, -0.0896, -0.4878, 0.9833, -0.2078],
[-0.0261, -0.0554, -0.0251, -0.6675, -0.2078, 0.9833]])
+ # fmt: on
L = nx.directed_laplacian_matrix(G, alpha=0.9, nodelist=sorted(G))
npt.assert_almost_equal(L, GL, decimal=3)
# Make the graph strongly connected, so we can use a random and lazy walk
G.add_edges_from(((2, 5), (6, 1)))
+ # fmt: off
GL = np.array([[1., -0.3062, -0.4714, 0., 0., -0.3227],
[-0.3062, 1., -0.1443, 0., -0.3162, 0.],
[-0.4714, -0.1443, 1., 0., -0.0913, 0.],
[0., 0., 0., 1., -0.5, -0.5],
[0., -0.3162, -0.0913, -0.5, 1., -0.25],
[-0.3227, 0., 0., -0.5, -0.25, 1.]])
+ # fmt: on
L = nx.directed_laplacian_matrix(G, alpha=0.9, nodelist=sorted(G), walk_type="random")
npt.assert_almost_equal(L, GL, decimal=3)
+ # fmt: off
GL = np.array([[0.5, -0.1531, -0.2357, 0., 0., -0.1614],
[-0.1531, 0.5, -0.0722, 0., -0.1581, 0.],
[-0.2357, -0.0722, 0.5, 0., -0.0456, 0.],
[0., 0., 0., 0.5, -0.25, -0.25],
[0., -0.1581, -0.0456, -0.25, 0.5, -0.125],
[-0.1614, 0., 0., -0.25, -0.125, 0.5]])
+ # fmt: on
L = nx.directed_laplacian_matrix(G, alpha=0.9, nodelist=sorted(G), walk_type="lazy")
npt.assert_almost_equal(L, GL, decimal=3)
@@ -151,12 +161,14 @@ class TestLaplacian:
(6, 4),
)
)
+ # fmt: off
GL = np.array([[0.0366, -0.0132, -0.0153, -0.0034, -0.0020, -0.0027],
[-0.0132, 0.0450, -0.0111, -0.0076, -0.0062, -0.0069],
[-0.0153, -0.0111, 0.0408, -0.0035, -0.0083, -0.0027],
[-0.0034, -0.0076, -0.0035, 0.3688, -0.1356, -0.2187],
[-0.0020, -0.0062, -0.0083, -0.1356, 0.2026, -0.0505],
[-0.0027, -0.0069, -0.0027, -0.2187, -0.0505, 0.2815]])
+ # fmt: on
L = nx.directed_combinatorial_laplacian_matrix(G, alpha=0.9, nodelist=sorted(G))
npt.assert_almost_equal(L, GL, decimal=3)
@@ -164,24 +176,28 @@ class TestLaplacian:
# Make the graph strongly connected, so we can use a random and lazy walk
G.add_edges_from(((2, 5), (6, 1)))
+ # fmt: off
GL = np.array([[0.1395, -0.0349, -0.0465, 0, 0, -0.0581],
[-0.0349, 0.0930, -0.0116, 0, -0.0465, 0],
[-0.0465, -0.0116, 0.0698, 0, -0.0116, 0],
[0, 0, 0, 0.2326, -0.1163, -0.1163],
[0, -0.0465, -0.0116, -0.1163, 0.2326, -0.0581],
[-0.0581, 0, 0, -0.1163, -0.0581, 0.2326]])
+ # fmt: on
L = nx.directed_combinatorial_laplacian_matrix(
G, alpha=0.9, nodelist=sorted(G), walk_type="random"
)
npt.assert_almost_equal(L, GL, decimal=3)
+ # fmt: off
GL = np.array([[0.0698, -0.0174, -0.0233, 0, 0, -0.0291],
[-0.0174, 0.0465, -0.0058, 0, -0.0233, 0],
[-0.0233, -0.0058, 0.0349, 0, -0.0058, 0],
[0, 0, 0, 0.1163, -0.0581, -0.0581],
[0, -0.0233, -0.0058, -0.0581, 0.1163, -0.0291],
[-0.0291, 0, 0, -0.0581, -0.0291, 0.1163]])
+ # fmt: on
L = nx.directed_combinatorial_laplacian_matrix(
G, alpha=0.9, nodelist=sorted(G), walk_type="lazy"
@@ -190,12 +206,14 @@ class TestLaplacian:
E = nx.DiGraph(margulis_gabber_galil_graph(2))
L = nx.directed_combinatorial_laplacian_matrix(E)
+ # fmt: off
expected = np.array(
[[ 0.16666667, -0.08333333, -0.08333333, 0. ],
[-0.08333333, 0.16666667, 0. , -0.08333333],
[-0.08333333, 0. , 0.16666667, -0.08333333],
[ 0. , -0.08333333, -0.08333333, 0.16666667]]
)
+ # fmt: on
npt.assert_almost_equal(L, expected, decimal=6)
with pytest.raises(nx.NetworkXError):
diff --git a/networkx/linalg/tests/test_modularity.py b/networkx/linalg/tests/test_modularity.py
index f791dcea..02cf1d64 100644
--- a/networkx/linalg/tests/test_modularity.py
+++ b/networkx/linalg/tests/test_modularity.py
@@ -33,11 +33,13 @@ class TestModularity:
def test_modularity(self):
"Modularity matrix"
+ # fmt: off
B = np.array([[-1.125, 0.25, 0.25, 0.625, 0.],
[0.25, -0.5, 0.5, -0.25, 0.],
[0.25, 0.5, -0.5, -0.25, 0.],
[0.625, -0.25, -0.25, -0.125, 0.],
[0., 0., 0., 0., 0.]])
+ # fmt: on
permutation = [4, 0, 1, 2, 3]
npt.assert_equal(nx.modularity_matrix(self.G), B)
@@ -48,11 +50,13 @@ class TestModularity:
def test_modularity_weight(self):
"Modularity matrix with weights"
+ # fmt: off
B = np.array([[-1.125, 0.25, 0.25, 0.625, 0.],
[0.25, -0.5, 0.5, -0.25, 0.],
[0.25, 0.5, -0.5, -0.25, 0.],
[0.625, -0.25, -0.25, -0.125, 0.],
[0., 0., 0., 0., 0.]])
+ # fmt: on
G_weighted = self.G.copy()
for n1, n2 in G_weighted.edges():
@@ -64,12 +68,14 @@ class TestModularity:
def test_directed_modularity(self):
"Directed Modularity matrix"
+ # fmt: off
B = np.array([[-0.2, 0.6, 0.8, -0.4, -0.4, -0.4],
[0., 0., 0., 0., 0., 0.],
[0.7, 0.4, -0.3, -0.6, 0.4, -0.6],
[-0.2, -0.4, -0.2, -0.4, 0.6, 0.6],
[-0.2, -0.4, -0.2, 0.6, -0.4, 0.6],
[-0.1, -0.2, -0.1, 0.8, -0.2, -0.2]])
+ # fmt: on
node_permutation = [5, 1, 2, 3, 4, 6]
idx_permutation = [4, 0, 1, 2, 3, 5]
mm = nx.directed_modularity_matrix(self.DG, nodelist=sorted(self.DG))