test_convert_numpy.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. import itertools
  2. import pytest
  3. import networkx as nx
  4. from networkx.utils import graphs_equal
  5. np = pytest.importorskip("numpy")
  6. npt = pytest.importorskip("numpy.testing")
  7. class TestConvertNumpyArray:
  8. def setup_method(self):
  9. self.G1 = nx.barbell_graph(10, 3)
  10. self.G2 = nx.cycle_graph(10, create_using=nx.DiGraph)
  11. self.G3 = self.create_weighted(nx.Graph())
  12. self.G4 = self.create_weighted(nx.DiGraph())
  13. def create_weighted(self, G):
  14. g = nx.cycle_graph(4)
  15. G.add_nodes_from(g)
  16. G.add_weighted_edges_from((u, v, 10 + u) for u, v in g.edges())
  17. return G
  18. def assert_equal(self, G1, G2):
  19. assert sorted(G1.nodes()) == sorted(G2.nodes())
  20. assert sorted(G1.edges()) == sorted(G2.edges())
  21. def identity_conversion(self, G, A, create_using):
  22. assert A.sum() > 0
  23. GG = nx.from_numpy_array(A, create_using=create_using)
  24. self.assert_equal(G, GG)
  25. GW = nx.to_networkx_graph(A, create_using=create_using)
  26. self.assert_equal(G, GW)
  27. GI = nx.empty_graph(0, create_using).__class__(A)
  28. self.assert_equal(G, GI)
  29. def test_shape(self):
  30. "Conversion from non-square array."
  31. A = np.array([[1, 2, 3], [4, 5, 6]])
  32. pytest.raises(nx.NetworkXError, nx.from_numpy_array, A)
  33. def test_identity_graph_array(self):
  34. "Conversion from graph to array to graph."
  35. A = nx.to_numpy_array(self.G1)
  36. self.identity_conversion(self.G1, A, nx.Graph())
  37. def test_identity_digraph_array(self):
  38. """Conversion from digraph to array to digraph."""
  39. A = nx.to_numpy_array(self.G2)
  40. self.identity_conversion(self.G2, A, nx.DiGraph())
  41. def test_identity_weighted_graph_array(self):
  42. """Conversion from weighted graph to array to weighted graph."""
  43. A = nx.to_numpy_array(self.G3)
  44. self.identity_conversion(self.G3, A, nx.Graph())
  45. def test_identity_weighted_digraph_array(self):
  46. """Conversion from weighted digraph to array to weighted digraph."""
  47. A = nx.to_numpy_array(self.G4)
  48. self.identity_conversion(self.G4, A, nx.DiGraph())
  49. def test_nodelist(self):
  50. """Conversion from graph to array to graph with nodelist."""
  51. P4 = nx.path_graph(4)
  52. P3 = nx.path_graph(3)
  53. nodelist = list(P3)
  54. A = nx.to_numpy_array(P4, nodelist=nodelist)
  55. GA = nx.Graph(A)
  56. self.assert_equal(GA, P3)
  57. # Make nodelist ambiguous by containing duplicates.
  58. nodelist += [nodelist[0]]
  59. pytest.raises(nx.NetworkXError, nx.to_numpy_array, P3, nodelist=nodelist)
  60. # Make nodelist invalid by including nonexistent nodes
  61. nodelist = [-1, 0, 1]
  62. with pytest.raises(
  63. nx.NetworkXError,
  64. match=f"Nodes {nodelist - P3.nodes} in nodelist is not in G",
  65. ):
  66. nx.to_numpy_array(P3, nodelist=nodelist)
  67. def test_weight_keyword(self):
  68. WP4 = nx.Graph()
  69. WP4.add_edges_from((n, n + 1, {"weight": 0.5, "other": 0.3}) for n in range(3))
  70. P4 = nx.path_graph(4)
  71. A = nx.to_numpy_array(P4)
  72. np.testing.assert_equal(A, nx.to_numpy_array(WP4, weight=None))
  73. np.testing.assert_equal(0.5 * A, nx.to_numpy_array(WP4))
  74. np.testing.assert_equal(0.3 * A, nx.to_numpy_array(WP4, weight="other"))
  75. def test_from_numpy_array_type(self):
  76. A = np.array([[1]])
  77. G = nx.from_numpy_array(A)
  78. assert isinstance(G[0][0]["weight"], int)
  79. A = np.array([[1]]).astype(float)
  80. G = nx.from_numpy_array(A)
  81. assert isinstance(G[0][0]["weight"], float)
  82. A = np.array([[1]]).astype(str)
  83. G = nx.from_numpy_array(A)
  84. assert isinstance(G[0][0]["weight"], str)
  85. A = np.array([[1]]).astype(bool)
  86. G = nx.from_numpy_array(A)
  87. assert isinstance(G[0][0]["weight"], bool)
  88. A = np.array([[1]]).astype(complex)
  89. G = nx.from_numpy_array(A)
  90. assert isinstance(G[0][0]["weight"], complex)
  91. A = np.array([[1]]).astype(object)
  92. pytest.raises(TypeError, nx.from_numpy_array, A)
  93. A = np.array([[[1, 1, 1], [1, 1, 1]], [[1, 1, 1], [1, 1, 1]]])
  94. with pytest.raises(
  95. nx.NetworkXError, match=f"Input array must be 2D, not {A.ndim}"
  96. ):
  97. g = nx.from_numpy_array(A)
  98. def test_from_numpy_array_dtype(self):
  99. dt = [("weight", float), ("cost", int)]
  100. A = np.array([[(1.0, 2)]], dtype=dt)
  101. G = nx.from_numpy_array(A)
  102. assert isinstance(G[0][0]["weight"], float)
  103. assert isinstance(G[0][0]["cost"], int)
  104. assert G[0][0]["cost"] == 2
  105. assert G[0][0]["weight"] == 1.0
  106. def test_from_numpy_array_parallel_edges(self):
  107. """Tests that the :func:`networkx.from_numpy_array` function
  108. interprets integer weights as the number of parallel edges when
  109. creating a multigraph.
  110. """
  111. A = np.array([[1, 1], [1, 2]])
  112. # First, with a simple graph, each integer entry in the adjacency
  113. # matrix is interpreted as the weight of a single edge in the graph.
  114. expected = nx.DiGraph()
  115. edges = [(0, 0), (0, 1), (1, 0)]
  116. expected.add_weighted_edges_from([(u, v, 1) for (u, v) in edges])
  117. expected.add_edge(1, 1, weight=2)
  118. actual = nx.from_numpy_array(A, parallel_edges=True, create_using=nx.DiGraph)
  119. assert graphs_equal(actual, expected)
  120. actual = nx.from_numpy_array(A, parallel_edges=False, create_using=nx.DiGraph)
  121. assert graphs_equal(actual, expected)
  122. # Now each integer entry in the adjacency matrix is interpreted as the
  123. # number of parallel edges in the graph if the appropriate keyword
  124. # argument is specified.
  125. edges = [(0, 0), (0, 1), (1, 0), (1, 1), (1, 1)]
  126. expected = nx.MultiDiGraph()
  127. expected.add_weighted_edges_from([(u, v, 1) for (u, v) in edges])
  128. actual = nx.from_numpy_array(
  129. A, parallel_edges=True, create_using=nx.MultiDiGraph
  130. )
  131. assert graphs_equal(actual, expected)
  132. expected = nx.MultiDiGraph()
  133. expected.add_edges_from(set(edges), weight=1)
  134. # The sole self-loop (edge 0) on vertex 1 should have weight 2.
  135. expected[1][1][0]["weight"] = 2
  136. actual = nx.from_numpy_array(
  137. A, parallel_edges=False, create_using=nx.MultiDiGraph
  138. )
  139. assert graphs_equal(actual, expected)
  140. @pytest.mark.parametrize(
  141. "dt",
  142. (
  143. None, # default
  144. int, # integer dtype
  145. np.dtype(
  146. [("weight", "f8"), ("color", "i1")]
  147. ), # Structured dtype with named fields
  148. ),
  149. )
  150. def test_from_numpy_array_no_edge_attr(self, dt):
  151. A = np.array([[0, 1], [1, 0]], dtype=dt)
  152. G = nx.from_numpy_array(A, edge_attr=None)
  153. assert "weight" not in G.edges[0, 1]
  154. assert len(G.edges[0, 1]) == 0
  155. def test_from_numpy_array_multiedge_no_edge_attr(self):
  156. A = np.array([[0, 2], [2, 0]])
  157. G = nx.from_numpy_array(A, create_using=nx.MultiDiGraph, edge_attr=None)
  158. assert all("weight" not in e for _, e in G[0][1].items())
  159. assert len(G[0][1][0]) == 0
  160. def test_from_numpy_array_custom_edge_attr(self):
  161. A = np.array([[0, 2], [3, 0]])
  162. G = nx.from_numpy_array(A, edge_attr="cost")
  163. assert "weight" not in G.edges[0, 1]
  164. assert G.edges[0, 1]["cost"] == 3
  165. def test_symmetric(self):
  166. """Tests that a symmetric array has edges added only once to an
  167. undirected multigraph when using :func:`networkx.from_numpy_array`.
  168. """
  169. A = np.array([[0, 1], [1, 0]])
  170. G = nx.from_numpy_array(A, create_using=nx.MultiGraph)
  171. expected = nx.MultiGraph()
  172. expected.add_edge(0, 1, weight=1)
  173. assert graphs_equal(G, expected)
  174. def test_dtype_int_graph(self):
  175. """Test that setting dtype int actually gives an integer array.
  176. For more information, see GitHub pull request #1363.
  177. """
  178. G = nx.complete_graph(3)
  179. A = nx.to_numpy_array(G, dtype=int)
  180. assert A.dtype == int
  181. def test_dtype_int_multigraph(self):
  182. """Test that setting dtype int actually gives an integer array.
  183. For more information, see GitHub pull request #1363.
  184. """
  185. G = nx.MultiGraph(nx.complete_graph(3))
  186. A = nx.to_numpy_array(G, dtype=int)
  187. assert A.dtype == int
  188. @pytest.fixture
  189. def multigraph_test_graph():
  190. G = nx.MultiGraph()
  191. G.add_edge(1, 2, weight=7)
  192. G.add_edge(1, 2, weight=70)
  193. return G
  194. @pytest.mark.parametrize(("operator", "expected"), ((sum, 77), (min, 7), (max, 70)))
  195. def test_numpy_multigraph(multigraph_test_graph, operator, expected):
  196. A = nx.to_numpy_array(multigraph_test_graph, multigraph_weight=operator)
  197. assert A[1, 0] == expected
  198. def test_to_numpy_array_multigraph_nodelist(multigraph_test_graph):
  199. G = multigraph_test_graph
  200. G.add_edge(0, 1, weight=3)
  201. A = nx.to_numpy_array(G, nodelist=[1, 2])
  202. assert A.shape == (2, 2)
  203. assert A[1, 0] == 77
  204. @pytest.mark.parametrize(
  205. "G, expected",
  206. [
  207. (nx.Graph(), np.array([[0, 1 + 2j], [1 + 2j, 0]], dtype=complex)),
  208. (nx.DiGraph(), np.array([[0, 1 + 2j], [0, 0]], dtype=complex)),
  209. ],
  210. )
  211. def test_to_numpy_array_complex_weights(G, expected):
  212. G.add_edge(0, 1, weight=1 + 2j)
  213. A = nx.to_numpy_array(G, dtype=complex)
  214. npt.assert_array_equal(A, expected)
  215. def test_to_numpy_array_arbitrary_weights():
  216. G = nx.DiGraph()
  217. w = 922337203685477580102 # Out of range for int64
  218. G.add_edge(0, 1, weight=922337203685477580102) # val not representable by int64
  219. A = nx.to_numpy_array(G, dtype=object)
  220. expected = np.array([[0, w], [0, 0]], dtype=object)
  221. npt.assert_array_equal(A, expected)
  222. # Undirected
  223. A = nx.to_numpy_array(G.to_undirected(), dtype=object)
  224. expected = np.array([[0, w], [w, 0]], dtype=object)
  225. npt.assert_array_equal(A, expected)
  226. @pytest.mark.parametrize(
  227. "func, expected",
  228. ((min, -1), (max, 10), (sum, 11), (np.mean, 11 / 3), (np.median, 2)),
  229. )
  230. def test_to_numpy_array_multiweight_reduction(func, expected):
  231. """Test various functions for reducing multiedge weights."""
  232. G = nx.MultiDiGraph()
  233. weights = [-1, 2, 10.0]
  234. for w in weights:
  235. G.add_edge(0, 1, weight=w)
  236. A = nx.to_numpy_array(G, multigraph_weight=func, dtype=float)
  237. assert np.allclose(A, [[0, expected], [0, 0]])
  238. # Undirected case
  239. A = nx.to_numpy_array(G.to_undirected(), multigraph_weight=func, dtype=float)
  240. assert np.allclose(A, [[0, expected], [expected, 0]])
  241. @pytest.mark.parametrize(
  242. ("G, expected"),
  243. [
  244. (nx.Graph(), [[(0, 0), (10, 5)], [(10, 5), (0, 0)]]),
  245. (nx.DiGraph(), [[(0, 0), (10, 5)], [(0, 0), (0, 0)]]),
  246. ],
  247. )
  248. def test_to_numpy_array_structured_dtype_attrs_from_fields(G, expected):
  249. """When `dtype` is structured (i.e. has names) and `weight` is None, use
  250. the named fields of the dtype to look up edge attributes."""
  251. G.add_edge(0, 1, weight=10, cost=5.0)
  252. dtype = np.dtype([("weight", int), ("cost", int)])
  253. A = nx.to_numpy_array(G, dtype=dtype, weight=None)
  254. expected = np.asarray(expected, dtype=dtype)
  255. npt.assert_array_equal(A, expected)
  256. def test_to_numpy_array_structured_dtype_single_attr_default():
  257. G = nx.path_graph(3)
  258. dtype = np.dtype([("weight", float)]) # A single named field
  259. A = nx.to_numpy_array(G, dtype=dtype, weight=None)
  260. expected = np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]], dtype=float)
  261. npt.assert_array_equal(A["weight"], expected)
  262. @pytest.mark.parametrize(
  263. ("field_name", "expected_attr_val"),
  264. [
  265. ("weight", 1),
  266. ("cost", 3),
  267. ],
  268. )
  269. def test_to_numpy_array_structured_dtype_single_attr(field_name, expected_attr_val):
  270. G = nx.Graph()
  271. G.add_edge(0, 1, cost=3)
  272. dtype = np.dtype([(field_name, float)])
  273. A = nx.to_numpy_array(G, dtype=dtype, weight=None)
  274. expected = np.array([[0, expected_attr_val], [expected_attr_val, 0]], dtype=float)
  275. npt.assert_array_equal(A[field_name], expected)
  276. @pytest.mark.parametrize("graph_type", (nx.Graph, nx.DiGraph))
  277. @pytest.mark.parametrize(
  278. "edge",
  279. [
  280. (0, 1), # No edge attributes
  281. (0, 1, {"weight": 10}), # One edge attr
  282. (0, 1, {"weight": 5, "flow": -4}), # Multiple but not all edge attrs
  283. (0, 1, {"weight": 2.0, "cost": 10, "flow": -45}), # All attrs
  284. ],
  285. )
  286. def test_to_numpy_array_structured_dtype_multiple_fields(graph_type, edge):
  287. G = graph_type([edge])
  288. dtype = np.dtype([("weight", float), ("cost", float), ("flow", float)])
  289. A = nx.to_numpy_array(G, dtype=dtype, weight=None)
  290. for attr in dtype.names:
  291. expected = nx.to_numpy_array(G, dtype=float, weight=attr)
  292. npt.assert_array_equal(A[attr], expected)
  293. @pytest.mark.parametrize("G", (nx.Graph(), nx.DiGraph()))
  294. def test_to_numpy_array_structured_dtype_scalar_nonedge(G):
  295. G.add_edge(0, 1, weight=10)
  296. dtype = np.dtype([("weight", float), ("cost", float)])
  297. A = nx.to_numpy_array(G, dtype=dtype, weight=None, nonedge=np.nan)
  298. for attr in dtype.names:
  299. expected = nx.to_numpy_array(G, dtype=float, weight=attr, nonedge=np.nan)
  300. npt.assert_array_equal(A[attr], expected)
  301. @pytest.mark.parametrize("G", (nx.Graph(), nx.DiGraph()))
  302. def test_to_numpy_array_structured_dtype_nonedge_ary(G):
  303. """Similar to the scalar case, except has a different non-edge value for
  304. each named field."""
  305. G.add_edge(0, 1, weight=10)
  306. dtype = np.dtype([("weight", float), ("cost", float)])
  307. nonedges = np.array([(0, np.inf)], dtype=dtype)
  308. A = nx.to_numpy_array(G, dtype=dtype, weight=None, nonedge=nonedges)
  309. for attr in dtype.names:
  310. nonedge = nonedges[attr]
  311. expected = nx.to_numpy_array(G, dtype=float, weight=attr, nonedge=nonedge)
  312. npt.assert_array_equal(A[attr], expected)
  313. def test_to_numpy_array_structured_dtype_with_weight_raises():
  314. """Using both a structured dtype (with named fields) and specifying a `weight`
  315. parameter is ambiguous."""
  316. G = nx.path_graph(3)
  317. dtype = np.dtype([("weight", int), ("cost", int)])
  318. exception_msg = "Specifying `weight` not supported for structured dtypes"
  319. with pytest.raises(ValueError, match=exception_msg):
  320. nx.to_numpy_array(G, dtype=dtype) # Default is weight="weight"
  321. with pytest.raises(ValueError, match=exception_msg):
  322. nx.to_numpy_array(G, dtype=dtype, weight="cost")
  323. @pytest.mark.parametrize("graph_type", (nx.MultiGraph, nx.MultiDiGraph))
  324. def test_to_numpy_array_structured_multigraph_raises(graph_type):
  325. G = nx.path_graph(3, create_using=graph_type)
  326. dtype = np.dtype([("weight", int), ("cost", int)])
  327. with pytest.raises(nx.NetworkXError, match="Structured arrays are not supported"):
  328. nx.to_numpy_array(G, dtype=dtype, weight=None)
  329. def test_from_numpy_array_nodelist_bad_size():
  330. """An exception is raised when `len(nodelist) != A.shape[0]`."""
  331. n = 5 # Number of nodes
  332. A = np.diag(np.ones(n - 1), k=1) # Adj. matrix for P_n
  333. expected = nx.path_graph(n)
  334. assert graphs_equal(nx.from_numpy_array(A, edge_attr=None), expected)
  335. nodes = list(range(n))
  336. assert graphs_equal(
  337. nx.from_numpy_array(A, edge_attr=None, nodelist=nodes), expected
  338. )
  339. # Too many node labels
  340. nodes = list(range(n + 1))
  341. with pytest.raises(ValueError, match="nodelist must have the same length as A"):
  342. nx.from_numpy_array(A, nodelist=nodes)
  343. # Too few node labels
  344. nodes = list(range(n - 1))
  345. with pytest.raises(ValueError, match="nodelist must have the same length as A"):
  346. nx.from_numpy_array(A, nodelist=nodes)
  347. @pytest.mark.parametrize(
  348. "nodes",
  349. (
  350. [4, 3, 2, 1, 0],
  351. [9, 7, 1, 2, 8],
  352. ["a", "b", "c", "d", "e"],
  353. [(0, 0), (1, 1), (2, 3), (0, 2), (3, 1)],
  354. ["A", 2, 7, "spam", (1, 3)],
  355. ),
  356. )
  357. def test_from_numpy_array_nodelist(nodes):
  358. A = np.diag(np.ones(4), k=1)
  359. # Without edge attributes
  360. expected = nx.relabel_nodes(
  361. nx.path_graph(5), mapping=dict(enumerate(nodes)), copy=True
  362. )
  363. G = nx.from_numpy_array(A, edge_attr=None, nodelist=nodes)
  364. assert graphs_equal(G, expected)
  365. # With edge attributes
  366. nx.set_edge_attributes(expected, 1.0, name="weight")
  367. G = nx.from_numpy_array(A, nodelist=nodes)
  368. assert graphs_equal(G, expected)
  369. @pytest.mark.parametrize(
  370. "nodes",
  371. (
  372. [4, 3, 2, 1, 0],
  373. [9, 7, 1, 2, 8],
  374. ["a", "b", "c", "d", "e"],
  375. [(0, 0), (1, 1), (2, 3), (0, 2), (3, 1)],
  376. ["A", 2, 7, "spam", (1, 3)],
  377. ),
  378. )
  379. def test_from_numpy_array_nodelist_directed(nodes):
  380. A = np.diag(np.ones(4), k=1)
  381. # Without edge attributes
  382. H = nx.DiGraph([(0, 1), (1, 2), (2, 3), (3, 4)])
  383. expected = nx.relabel_nodes(H, mapping=dict(enumerate(nodes)), copy=True)
  384. G = nx.from_numpy_array(A, create_using=nx.DiGraph, edge_attr=None, nodelist=nodes)
  385. assert graphs_equal(G, expected)
  386. # With edge attributes
  387. nx.set_edge_attributes(expected, 1.0, name="weight")
  388. G = nx.from_numpy_array(A, create_using=nx.DiGraph, nodelist=nodes)
  389. assert graphs_equal(G, expected)
  390. @pytest.mark.parametrize(
  391. "nodes",
  392. (
  393. [4, 3, 2, 1, 0],
  394. [9, 7, 1, 2, 8],
  395. ["a", "b", "c", "d", "e"],
  396. [(0, 0), (1, 1), (2, 3), (0, 2), (3, 1)],
  397. ["A", 2, 7, "spam", (1, 3)],
  398. ),
  399. )
  400. def test_from_numpy_array_nodelist_multigraph(nodes):
  401. A = np.array(
  402. [
  403. [0, 1, 0, 0, 0],
  404. [1, 0, 2, 0, 0],
  405. [0, 2, 0, 3, 0],
  406. [0, 0, 3, 0, 4],
  407. [0, 0, 0, 4, 0],
  408. ]
  409. )
  410. H = nx.MultiGraph()
  411. for i, edge in enumerate(((0, 1), (1, 2), (2, 3), (3, 4))):
  412. H.add_edges_from(itertools.repeat(edge, i + 1))
  413. expected = nx.relabel_nodes(H, mapping=dict(enumerate(nodes)), copy=True)
  414. G = nx.from_numpy_array(
  415. A,
  416. parallel_edges=True,
  417. create_using=nx.MultiGraph,
  418. edge_attr=None,
  419. nodelist=nodes,
  420. )
  421. assert graphs_equal(G, expected)
  422. @pytest.mark.parametrize(
  423. "nodes",
  424. (
  425. [4, 3, 2, 1, 0],
  426. [9, 7, 1, 2, 8],
  427. ["a", "b", "c", "d", "e"],
  428. [(0, 0), (1, 1), (2, 3), (0, 2), (3, 1)],
  429. ["A", 2, 7, "spam", (1, 3)],
  430. ),
  431. )
  432. @pytest.mark.parametrize("graph", (nx.complete_graph, nx.cycle_graph, nx.wheel_graph))
  433. def test_from_numpy_array_nodelist_rountrip(graph, nodes):
  434. G = graph(5)
  435. A = nx.to_numpy_array(G)
  436. expected = nx.relabel_nodes(G, mapping=dict(enumerate(nodes)), copy=True)
  437. H = nx.from_numpy_array(A, edge_attr=None, nodelist=nodes)
  438. assert graphs_equal(H, expected)
  439. # With an isolated node
  440. G = graph(4)
  441. G.add_node("foo")
  442. A = nx.to_numpy_array(G)
  443. expected = nx.relabel_nodes(G, mapping=dict(zip(G.nodes, nodes)), copy=True)
  444. H = nx.from_numpy_array(A, edge_attr=None, nodelist=nodes)
  445. assert graphs_equal(H, expected)