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