test_convert_scipy.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import pytest
  2. np = pytest.importorskip("numpy")
  3. sp = pytest.importorskip("scipy")
  4. import scipy.sparse # call as sp.sparse
  5. import networkx as nx
  6. from networkx.generators.classic import barbell_graph, cycle_graph, path_graph
  7. from networkx.utils import graphs_equal
  8. class TestConvertScipy:
  9. def setup_method(self):
  10. self.G1 = barbell_graph(10, 3)
  11. self.G2 = cycle_graph(10, create_using=nx.DiGraph)
  12. self.G3 = self.create_weighted(nx.Graph())
  13. self.G4 = self.create_weighted(nx.DiGraph())
  14. def test_exceptions(self):
  15. class G:
  16. format = None
  17. pytest.raises(nx.NetworkXError, nx.to_networkx_graph, G)
  18. def create_weighted(self, G):
  19. g = cycle_graph(4)
  20. e = list(g.edges())
  21. source = [u for u, v in e]
  22. dest = [v for u, v in e]
  23. weight = [s + 10 for s in source]
  24. ex = zip(source, dest, weight)
  25. G.add_weighted_edges_from(ex)
  26. return G
  27. def identity_conversion(self, G, A, create_using):
  28. GG = nx.from_scipy_sparse_array(A, create_using=create_using)
  29. assert nx.is_isomorphic(G, GG)
  30. GW = nx.to_networkx_graph(A, create_using=create_using)
  31. assert nx.is_isomorphic(G, GW)
  32. GI = nx.empty_graph(0, create_using).__class__(A)
  33. assert nx.is_isomorphic(G, GI)
  34. ACSR = A.tocsr()
  35. GI = nx.empty_graph(0, create_using).__class__(ACSR)
  36. assert nx.is_isomorphic(G, GI)
  37. ACOO = A.tocoo()
  38. GI = nx.empty_graph(0, create_using).__class__(ACOO)
  39. assert nx.is_isomorphic(G, GI)
  40. ACSC = A.tocsc()
  41. GI = nx.empty_graph(0, create_using).__class__(ACSC)
  42. assert nx.is_isomorphic(G, GI)
  43. AD = A.todense()
  44. GI = nx.empty_graph(0, create_using).__class__(AD)
  45. assert nx.is_isomorphic(G, GI)
  46. AA = A.toarray()
  47. GI = nx.empty_graph(0, create_using).__class__(AA)
  48. assert nx.is_isomorphic(G, GI)
  49. def test_shape(self):
  50. "Conversion from non-square sparse array."
  51. A = sp.sparse.lil_array([[1, 2, 3], [4, 5, 6]])
  52. pytest.raises(nx.NetworkXError, nx.from_scipy_sparse_array, A)
  53. def test_identity_graph_matrix(self):
  54. "Conversion from graph to sparse matrix to graph."
  55. A = nx.to_scipy_sparse_array(self.G1)
  56. self.identity_conversion(self.G1, A, nx.Graph())
  57. def test_identity_digraph_matrix(self):
  58. "Conversion from digraph to sparse matrix to digraph."
  59. A = nx.to_scipy_sparse_array(self.G2)
  60. self.identity_conversion(self.G2, A, nx.DiGraph())
  61. def test_identity_weighted_graph_matrix(self):
  62. """Conversion from weighted graph to sparse matrix to weighted graph."""
  63. A = nx.to_scipy_sparse_array(self.G3)
  64. self.identity_conversion(self.G3, A, nx.Graph())
  65. def test_identity_weighted_digraph_matrix(self):
  66. """Conversion from weighted digraph to sparse matrix to weighted digraph."""
  67. A = nx.to_scipy_sparse_array(self.G4)
  68. self.identity_conversion(self.G4, A, nx.DiGraph())
  69. def test_nodelist(self):
  70. """Conversion from graph to sparse matrix to graph with nodelist."""
  71. P4 = path_graph(4)
  72. P3 = path_graph(3)
  73. nodelist = list(P3.nodes())
  74. A = nx.to_scipy_sparse_array(P4, nodelist=nodelist)
  75. GA = nx.Graph(A)
  76. assert nx.is_isomorphic(GA, P3)
  77. pytest.raises(nx.NetworkXError, nx.to_scipy_sparse_array, P3, nodelist=[])
  78. # Test nodelist duplicates.
  79. long_nl = nodelist + [0]
  80. pytest.raises(nx.NetworkXError, nx.to_scipy_sparse_array, P3, nodelist=long_nl)
  81. # Test nodelist contains non-nodes
  82. non_nl = [-1, 0, 1, 2]
  83. pytest.raises(nx.NetworkXError, nx.to_scipy_sparse_array, P3, nodelist=non_nl)
  84. def test_weight_keyword(self):
  85. WP4 = nx.Graph()
  86. WP4.add_edges_from((n, n + 1, {"weight": 0.5, "other": 0.3}) for n in range(3))
  87. P4 = path_graph(4)
  88. A = nx.to_scipy_sparse_array(P4)
  89. np.testing.assert_equal(
  90. A.todense(), nx.to_scipy_sparse_array(WP4, weight=None).todense()
  91. )
  92. np.testing.assert_equal(
  93. 0.5 * A.todense(), nx.to_scipy_sparse_array(WP4).todense()
  94. )
  95. np.testing.assert_equal(
  96. 0.3 * A.todense(), nx.to_scipy_sparse_array(WP4, weight="other").todense()
  97. )
  98. def test_format_keyword(self):
  99. WP4 = nx.Graph()
  100. WP4.add_edges_from((n, n + 1, {"weight": 0.5, "other": 0.3}) for n in range(3))
  101. P4 = path_graph(4)
  102. A = nx.to_scipy_sparse_array(P4, format="csr")
  103. np.testing.assert_equal(
  104. A.todense(), nx.to_scipy_sparse_array(WP4, weight=None).todense()
  105. )
  106. A = nx.to_scipy_sparse_array(P4, format="csc")
  107. np.testing.assert_equal(
  108. A.todense(), nx.to_scipy_sparse_array(WP4, weight=None).todense()
  109. )
  110. A = nx.to_scipy_sparse_array(P4, format="coo")
  111. np.testing.assert_equal(
  112. A.todense(), nx.to_scipy_sparse_array(WP4, weight=None).todense()
  113. )
  114. A = nx.to_scipy_sparse_array(P4, format="bsr")
  115. np.testing.assert_equal(
  116. A.todense(), nx.to_scipy_sparse_array(WP4, weight=None).todense()
  117. )
  118. A = nx.to_scipy_sparse_array(P4, format="lil")
  119. np.testing.assert_equal(
  120. A.todense(), nx.to_scipy_sparse_array(WP4, weight=None).todense()
  121. )
  122. A = nx.to_scipy_sparse_array(P4, format="dia")
  123. np.testing.assert_equal(
  124. A.todense(), nx.to_scipy_sparse_array(WP4, weight=None).todense()
  125. )
  126. A = nx.to_scipy_sparse_array(P4, format="dok")
  127. np.testing.assert_equal(
  128. A.todense(), nx.to_scipy_sparse_array(WP4, weight=None).todense()
  129. )
  130. def test_format_keyword_raise(self):
  131. with pytest.raises(nx.NetworkXError):
  132. WP4 = nx.Graph()
  133. WP4.add_edges_from(
  134. (n, n + 1, {"weight": 0.5, "other": 0.3}) for n in range(3)
  135. )
  136. P4 = path_graph(4)
  137. nx.to_scipy_sparse_array(P4, format="any_other")
  138. def test_null_raise(self):
  139. with pytest.raises(nx.NetworkXError):
  140. nx.to_scipy_sparse_array(nx.Graph())
  141. def test_empty(self):
  142. G = nx.Graph()
  143. G.add_node(1)
  144. M = nx.to_scipy_sparse_array(G)
  145. np.testing.assert_equal(M.toarray(), np.array([[0]]))
  146. def test_ordering(self):
  147. G = nx.DiGraph()
  148. G.add_edge(1, 2)
  149. G.add_edge(2, 3)
  150. G.add_edge(3, 1)
  151. M = nx.to_scipy_sparse_array(G, nodelist=[3, 2, 1])
  152. np.testing.assert_equal(
  153. M.toarray(), np.array([[0, 0, 1], [1, 0, 0], [0, 1, 0]])
  154. )
  155. def test_selfloop_graph(self):
  156. G = nx.Graph([(1, 1)])
  157. M = nx.to_scipy_sparse_array(G)
  158. np.testing.assert_equal(M.toarray(), np.array([[1]]))
  159. G.add_edges_from([(2, 3), (3, 4)])
  160. M = nx.to_scipy_sparse_array(G, nodelist=[2, 3, 4])
  161. np.testing.assert_equal(
  162. M.toarray(), np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]])
  163. )
  164. def test_selfloop_digraph(self):
  165. G = nx.DiGraph([(1, 1)])
  166. M = nx.to_scipy_sparse_array(G)
  167. np.testing.assert_equal(M.toarray(), np.array([[1]]))
  168. G.add_edges_from([(2, 3), (3, 4)])
  169. M = nx.to_scipy_sparse_array(G, nodelist=[2, 3, 4])
  170. np.testing.assert_equal(
  171. M.toarray(), np.array([[0, 1, 0], [0, 0, 1], [0, 0, 0]])
  172. )
  173. def test_from_scipy_sparse_array_parallel_edges(self):
  174. """Tests that the :func:`networkx.from_scipy_sparse_array` function
  175. interprets integer weights as the number of parallel edges when
  176. creating a multigraph.
  177. """
  178. A = sp.sparse.csr_array([[1, 1], [1, 2]])
  179. # First, with a simple graph, each integer entry in the adjacency
  180. # matrix is interpreted as the weight of a single edge in the graph.
  181. expected = nx.DiGraph()
  182. edges = [(0, 0), (0, 1), (1, 0)]
  183. expected.add_weighted_edges_from([(u, v, 1) for (u, v) in edges])
  184. expected.add_edge(1, 1, weight=2)
  185. actual = nx.from_scipy_sparse_array(
  186. A, parallel_edges=True, create_using=nx.DiGraph
  187. )
  188. assert graphs_equal(actual, expected)
  189. actual = nx.from_scipy_sparse_array(
  190. A, parallel_edges=False, create_using=nx.DiGraph
  191. )
  192. assert graphs_equal(actual, expected)
  193. # Now each integer entry in the adjacency matrix is interpreted as the
  194. # number of parallel edges in the graph if the appropriate keyword
  195. # argument is specified.
  196. edges = [(0, 0), (0, 1), (1, 0), (1, 1), (1, 1)]
  197. expected = nx.MultiDiGraph()
  198. expected.add_weighted_edges_from([(u, v, 1) for (u, v) in edges])
  199. actual = nx.from_scipy_sparse_array(
  200. A, parallel_edges=True, create_using=nx.MultiDiGraph
  201. )
  202. assert graphs_equal(actual, expected)
  203. expected = nx.MultiDiGraph()
  204. expected.add_edges_from(set(edges), weight=1)
  205. # The sole self-loop (edge 0) on vertex 1 should have weight 2.
  206. expected[1][1][0]["weight"] = 2
  207. actual = nx.from_scipy_sparse_array(
  208. A, parallel_edges=False, create_using=nx.MultiDiGraph
  209. )
  210. assert graphs_equal(actual, expected)
  211. def test_symmetric(self):
  212. """Tests that a symmetric matrix has edges added only once to an
  213. undirected multigraph when using
  214. :func:`networkx.from_scipy_sparse_array`.
  215. """
  216. A = sp.sparse.csr_array([[0, 1], [1, 0]])
  217. G = nx.from_scipy_sparse_array(A, create_using=nx.MultiGraph)
  218. expected = nx.MultiGraph()
  219. expected.add_edge(0, 1, weight=1)
  220. assert graphs_equal(G, expected)
  221. @pytest.mark.parametrize("sparse_format", ("csr", "csc", "dok"))
  222. def test_from_scipy_sparse_array_formats(sparse_format):
  223. """Test all formats supported by _generate_weighted_edges."""
  224. # trinode complete graph with non-uniform edge weights
  225. expected = nx.Graph()
  226. expected.add_edges_from(
  227. [
  228. (0, 1, {"weight": 3}),
  229. (0, 2, {"weight": 2}),
  230. (1, 0, {"weight": 3}),
  231. (1, 2, {"weight": 1}),
  232. (2, 0, {"weight": 2}),
  233. (2, 1, {"weight": 1}),
  234. ]
  235. )
  236. A = sp.sparse.coo_array([[0, 3, 2], [3, 0, 1], [2, 1, 0]]).asformat(sparse_format)
  237. assert graphs_equal(expected, nx.from_scipy_sparse_array(A))