From fec20e0a6767eca1b40f19e1cf06387cdaea0f13 Mon Sep 17 00:00:00 2001 From: Jarrod Millman Date: Thu, 27 May 2021 14:17:28 -0700 Subject: Add dtype argument to adjacency_matrix (#4850) and deprecate adj_matrix --- doc/developer/deprecations.rst | 3 ++- doc/release/release_dev.rst | 4 ++++ networkx/algorithms/centrality/katz.py | 2 +- networkx/linalg/graphmatrix.py | 24 +++++++++++++++++++++--- networkx/linalg/tests/test_graphmatrix.py | 21 ++++++++++++--------- networkx/tests/test_convert_numpy.py | 2 +- 6 files changed, 41 insertions(+), 15 deletions(-) diff --git a/doc/developer/deprecations.rst b/doc/developer/deprecations.rst index 619e3281..4cc4a2c1 100644 --- a/doc/developer/deprecations.rst +++ b/doc/developer/deprecations.rst @@ -86,4 +86,5 @@ Version 3.0 classes defined therein. * In ``utils/decorators.py`` remove ``preserve_random_state``. * In ``algorithms/community/quality.py`` remove ``coverage`` and ``performance``. -* Remove ``testing``. \ No newline at end of file +* Remove ``testing``. +* In ``linalg/graphmatrix.py`` remove ``adj_matrix``. diff --git a/doc/release/release_dev.rst b/doc/release/release_dev.rst index 5cc0bc1c..cd646c12 100644 --- a/doc/release/release_dev.rst +++ b/doc/release/release_dev.rst @@ -179,6 +179,8 @@ API Changes - [`#4843 `_] The unused ``normalized`` parameter has been removed from ``communicability_betweeness_centrality`` +- [`#4850 `_] + Added ``dtype`` parameter to adjacency_matrix Deprecations ------------ @@ -233,6 +235,8 @@ Deprecations Deprecate ``run``. - [`#4829 `_] Deprecate ``assert_nodes_equal``, ``assert_edges_equal``, and ``assert_graphs_equal``. +- [`#4850 `_] + Deprecate ``adj_matrix``. Contributors ------------ diff --git a/networkx/algorithms/centrality/katz.py b/networkx/algorithms/centrality/katz.py index 811837fe..c528bafc 100644 --- a/networkx/algorithms/centrality/katz.py +++ b/networkx/algorithms/centrality/katz.py @@ -322,7 +322,7 @@ def katz_centrality_numpy(G, alpha=0.1, beta=1.0, normalized=True, weight=None): except (TypeError, ValueError, AttributeError) as e: raise nx.NetworkXError("beta must be a number") from e - A = nx.adj_matrix(G, nodelist=nodelist, weight=weight).todense().T + A = nx.adjacency_matrix(G, nodelist=nodelist, weight=weight).todense().T n = A.shape[0] centrality = np.linalg.solve(np.eye(n, n) - (alpha * A), b) if normalized: diff --git a/networkx/linalg/graphmatrix.py b/networkx/linalg/graphmatrix.py index 73147857..ab9460e3 100644 --- a/networkx/linalg/graphmatrix.py +++ b/networkx/linalg/graphmatrix.py @@ -96,7 +96,7 @@ def incidence_matrix(G, nodelist=None, edgelist=None, oriented=False, weight=Non return A.asformat("csc") -def adjacency_matrix(G, nodelist=None, weight="weight"): +def adjacency_matrix(G, nodelist=None, dtype=None, weight="weight"): """Returns adjacency matrix of G. Parameters @@ -108,6 +108,10 @@ def adjacency_matrix(G, nodelist=None, weight="weight"): The rows and columns are ordered according to the nodes in nodelist. If nodelist is None, then the ordering is produced by G.nodes(). + dtype : NumPy data-type, optional + The desired data-type for the array. + If None, then the NumPy default is used. + weight : string or None, optional (default='weight') The edge data key used to provide each value in the matrix. If None, then each edge has weight 1. @@ -150,7 +154,21 @@ def adjacency_matrix(G, nodelist=None, weight="weight"): to_dict_of_dicts adjacency_spectrum """ - return nx.to_scipy_sparse_matrix(G, nodelist=nodelist, weight=weight) + return nx.to_scipy_sparse_matrix(G, nodelist=nodelist, dtype=dtype, weight=weight) + + +def _adj_matrix_warning(G, nodelist=None, dtype=None, weight="weight"): + import warnings + + warnings.warn( + ( + "adj_matrix is deprecated and will be removed in version 3.0.\n" + "Use `adjacency_matrix` instead\n" + ), + DeprecationWarning, + stacklevel=2, + ) + return adjacency_matrix(G, nodelist, dtype, weight) -adj_matrix = adjacency_matrix +adj_matrix = _adj_matrix_warning diff --git a/networkx/linalg/tests/test_graphmatrix.py b/networkx/linalg/tests/test_graphmatrix.py index 5a9c70ca..206fb4e2 100644 --- a/networkx/linalg/tests/test_graphmatrix.py +++ b/networkx/linalg/tests/test_graphmatrix.py @@ -275,20 +275,23 @@ class TestGraphMatrix: def test_adjacency_matrix(self): "Conversion to adjacency matrix" - np.testing.assert_equal(nx.adj_matrix(self.G).todense(), self.A) - np.testing.assert_equal(nx.adj_matrix(self.MG).todense(), self.A) - np.testing.assert_equal(nx.adj_matrix(self.MG2).todense(), self.MG2A) + np.testing.assert_equal(nx.adjacency_matrix(self.G).todense(), self.A) + np.testing.assert_equal(nx.adjacency_matrix(self.MG).todense(), self.A) + np.testing.assert_equal(nx.adjacency_matrix(self.MG2).todense(), self.MG2A) np.testing.assert_equal( - nx.adj_matrix(self.G, nodelist=[0, 1]).todense(), self.A[:2, :2] + nx.adjacency_matrix(self.G, nodelist=[0, 1]).todense(), self.A[:2, :2] ) - np.testing.assert_equal(nx.adj_matrix(self.WG).todense(), self.WA) - np.testing.assert_equal(nx.adj_matrix(self.WG, weight=None).todense(), self.A) + np.testing.assert_equal(nx.adjacency_matrix(self.WG).todense(), self.WA) np.testing.assert_equal( - nx.adj_matrix(self.MG2, weight=None).todense(), self.MG2A + nx.adjacency_matrix(self.WG, weight=None).todense(), self.A ) np.testing.assert_equal( - nx.adj_matrix(self.WG, weight="other").todense(), 0.6 * self.WA + nx.adjacency_matrix(self.MG2, weight=None).todense(), self.MG2A ) np.testing.assert_equal( - nx.adj_matrix(self.no_edges_G, nodelist=[1, 3]).todense(), self.no_edges_A + nx.adjacency_matrix(self.WG, weight="other").todense(), 0.6 * self.WA + ) + np.testing.assert_equal( + nx.adjacency_matrix(self.no_edges_G, nodelist=[1, 3]).todense(), + self.no_edges_A, ) diff --git a/networkx/tests/test_convert_numpy.py b/networkx/tests/test_convert_numpy.py index f155de35..a14f4e7e 100644 --- a/networkx/tests/test_convert_numpy.py +++ b/networkx/tests/test_convert_numpy.py @@ -151,7 +151,7 @@ class TestConvertNumpyMatrix: pytest.raises(TypeError, nx.from_numpy_matrix, A) G = nx.cycle_graph(3) - A = nx.adj_matrix(G).todense() + A = nx.adjacency_matrix(G).todense() H = nx.from_numpy_matrix(A) assert all(type(m) == int and type(n) == int for m, n in H.edges()) H = nx.from_numpy_array(A) -- cgit v1.2.1