comparison env/lib/python3.9/site-packages/networkx/linalg/tests/test_graphmatrix.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 np = pytest.importorskip("numpy")
4 npt = pytest.importorskip("numpy.testing")
5 scipy = pytest.importorskip("scipy")
6
7 import networkx as nx
8 from networkx.generators.degree_seq import havel_hakimi_graph
9 from networkx.exception import NetworkXError
10
11
12 def test_incidence_matrix_simple():
13 deg = [3, 2, 2, 1, 0]
14 G = havel_hakimi_graph(deg)
15 deg = [(1, 0), (1, 0), (1, 0), (2, 0), (1, 0), (2, 1), (0, 1), (0, 1)]
16 MG = nx.random_clustered_graph(deg, seed=42)
17
18 I = nx.incidence_matrix(G).todense().astype(int)
19 # fmt: off
20 expected = np.array(
21 [[1, 1, 1, 0],
22 [0, 1, 0, 1],
23 [1, 0, 0, 1],
24 [0, 0, 1, 0],
25 [0, 0, 0, 0]]
26 )
27 # fmt: on
28 npt.assert_equal(I, expected)
29
30 I = nx.incidence_matrix(MG).todense().astype(int)
31 # fmt: off
32 expected = np.array(
33 [[1, 0, 0, 0, 0, 0, 0],
34 [1, 0, 0, 0, 0, 0, 0],
35 [0, 1, 0, 0, 0, 0, 0],
36 [0, 0, 0, 0, 0, 0, 0],
37 [0, 1, 0, 0, 0, 0, 0],
38 [0, 0, 0, 0, 1, 1, 0],
39 [0, 0, 0, 0, 0, 1, 1],
40 [0, 0, 0, 0, 1, 0, 1]]
41 )
42 # fmt: on
43 npt.assert_equal(I, expected)
44
45 with pytest.raises(NetworkXError):
46 nx.incidence_matrix(G, nodelist=[0, 1])
47
48
49 class TestGraphMatrix:
50 @classmethod
51 def setup_class(cls):
52 deg = [3, 2, 2, 1, 0]
53 cls.G = havel_hakimi_graph(deg)
54 # fmt: off
55 cls.OI = np.array(
56 [[-1, -1, -1, 0],
57 [1, 0, 0, -1],
58 [0, 1, 0, 1],
59 [0, 0, 1, 0],
60 [0, 0, 0, 0]]
61 )
62 cls.A = np.array(
63 [[0, 1, 1, 1, 0],
64 [1, 0, 1, 0, 0],
65 [1, 1, 0, 0, 0],
66 [1, 0, 0, 0, 0],
67 [0, 0, 0, 0, 0]]
68 )
69 # fmt: on
70 cls.WG = havel_hakimi_graph(deg)
71 cls.WG.add_edges_from(
72 (u, v, {"weight": 0.5, "other": 0.3}) for (u, v) in cls.G.edges()
73 )
74 # fmt: off
75 cls.WA = np.array(
76 [[0, 0.5, 0.5, 0.5, 0],
77 [0.5, 0, 0.5, 0, 0],
78 [0.5, 0.5, 0, 0, 0],
79 [0.5, 0, 0, 0, 0],
80 [0, 0, 0, 0, 0]]
81 )
82 # fmt: on
83 cls.MG = nx.MultiGraph(cls.G)
84 cls.MG2 = cls.MG.copy()
85 cls.MG2.add_edge(0, 1)
86 # fmt: off
87 cls.MG2A = np.array(
88 [[0, 2, 1, 1, 0],
89 [2, 0, 1, 0, 0],
90 [1, 1, 0, 0, 0],
91 [1, 0, 0, 0, 0],
92 [0, 0, 0, 0, 0]]
93 )
94 cls.MGOI = np.array(
95 [[-1, -1, -1, -1, 0],
96 [1, 1, 0, 0, -1],
97 [0, 0, 1, 0, 1],
98 [0, 0, 0, 1, 0],
99 [0, 0, 0, 0, 0]]
100 )
101 # fmt: on
102 cls.no_edges_G = nx.Graph([(1, 2), (3, 2, {"weight": 8})])
103 cls.no_edges_A = np.array([[0, 0], [0, 0]])
104
105 def test_incidence_matrix(self):
106 "Conversion to incidence matrix"
107 I = (
108 nx.incidence_matrix(
109 self.G,
110 nodelist=sorted(self.G),
111 edgelist=sorted(self.G.edges()),
112 oriented=True,
113 )
114 .todense()
115 .astype(int)
116 )
117 npt.assert_equal(I, self.OI)
118
119 I = (
120 nx.incidence_matrix(
121 self.G,
122 nodelist=sorted(self.G),
123 edgelist=sorted(self.G.edges()),
124 oriented=False,
125 )
126 .todense()
127 .astype(int)
128 )
129 npt.assert_equal(I, np.abs(self.OI))
130
131 I = (
132 nx.incidence_matrix(
133 self.MG,
134 nodelist=sorted(self.MG),
135 edgelist=sorted(self.MG.edges()),
136 oriented=True,
137 )
138 .todense()
139 .astype(int)
140 )
141 npt.assert_equal(I, self.OI)
142
143 I = (
144 nx.incidence_matrix(
145 self.MG,
146 nodelist=sorted(self.MG),
147 edgelist=sorted(self.MG.edges()),
148 oriented=False,
149 )
150 .todense()
151 .astype(int)
152 )
153 npt.assert_equal(I, np.abs(self.OI))
154
155 I = (
156 nx.incidence_matrix(
157 self.MG2,
158 nodelist=sorted(self.MG2),
159 edgelist=sorted(self.MG2.edges()),
160 oriented=True,
161 )
162 .todense()
163 .astype(int)
164 )
165 npt.assert_equal(I, self.MGOI)
166
167 I = (
168 nx.incidence_matrix(
169 self.MG2,
170 nodelist=sorted(self.MG),
171 edgelist=sorted(self.MG2.edges()),
172 oriented=False,
173 )
174 .todense()
175 .astype(int)
176 )
177 npt.assert_equal(I, np.abs(self.MGOI))
178
179 def test_weighted_incidence_matrix(self):
180 I = (
181 nx.incidence_matrix(
182 self.WG,
183 nodelist=sorted(self.WG),
184 edgelist=sorted(self.WG.edges()),
185 oriented=True,
186 )
187 .todense()
188 .astype(int)
189 )
190 npt.assert_equal(I, self.OI)
191
192 I = (
193 nx.incidence_matrix(
194 self.WG,
195 nodelist=sorted(self.WG),
196 edgelist=sorted(self.WG.edges()),
197 oriented=False,
198 )
199 .todense()
200 .astype(int)
201 )
202 npt.assert_equal(I, np.abs(self.OI))
203
204 # npt.assert_equal(nx.incidence_matrix(self.WG,oriented=True,
205 # weight='weight').todense(),0.5*self.OI)
206 # npt.assert_equal(nx.incidence_matrix(self.WG,weight='weight').todense(),
207 # np.abs(0.5*self.OI))
208 # npt.assert_equal(nx.incidence_matrix(self.WG,oriented=True,weight='other').todense(),
209 # 0.3*self.OI)
210
211 I = nx.incidence_matrix(
212 self.WG,
213 nodelist=sorted(self.WG),
214 edgelist=sorted(self.WG.edges()),
215 oriented=True,
216 weight="weight",
217 ).todense()
218 npt.assert_equal(I, 0.5 * self.OI)
219
220 I = nx.incidence_matrix(
221 self.WG,
222 nodelist=sorted(self.WG),
223 edgelist=sorted(self.WG.edges()),
224 oriented=False,
225 weight="weight",
226 ).todense()
227 npt.assert_equal(I, np.abs(0.5 * self.OI))
228
229 I = nx.incidence_matrix(
230 self.WG,
231 nodelist=sorted(self.WG),
232 edgelist=sorted(self.WG.edges()),
233 oriented=True,
234 weight="other",
235 ).todense()
236 npt.assert_equal(I, 0.3 * self.OI)
237
238 # WMG=nx.MultiGraph(self.WG)
239 # WMG.add_edge(0,1,weight=0.5,other=0.3)
240 # npt.assert_equal(nx.incidence_matrix(WMG,weight='weight').todense(),
241 # np.abs(0.5*self.MGOI))
242 # npt.assert_equal(nx.incidence_matrix(WMG,weight='weight',oriented=True).todense(),
243 # 0.5*self.MGOI)
244 # npt.assert_equal(nx.incidence_matrix(WMG,weight='other',oriented=True).todense(),
245 # 0.3*self.MGOI)
246
247 WMG = nx.MultiGraph(self.WG)
248 WMG.add_edge(0, 1, weight=0.5, other=0.3)
249
250 I = nx.incidence_matrix(
251 WMG,
252 nodelist=sorted(WMG),
253 edgelist=sorted(WMG.edges(keys=True)),
254 oriented=True,
255 weight="weight",
256 ).todense()
257 npt.assert_equal(I, 0.5 * self.MGOI)
258
259 I = nx.incidence_matrix(
260 WMG,
261 nodelist=sorted(WMG),
262 edgelist=sorted(WMG.edges(keys=True)),
263 oriented=False,
264 weight="weight",
265 ).todense()
266 npt.assert_equal(I, np.abs(0.5 * self.MGOI))
267
268 I = nx.incidence_matrix(
269 WMG,
270 nodelist=sorted(WMG),
271 edgelist=sorted(WMG.edges(keys=True)),
272 oriented=True,
273 weight="other",
274 ).todense()
275 npt.assert_equal(I, 0.3 * self.MGOI)
276
277 def test_adjacency_matrix(self):
278 "Conversion to adjacency matrix"
279 npt.assert_equal(nx.adj_matrix(self.G).todense(), self.A)
280 npt.assert_equal(nx.adj_matrix(self.MG).todense(), self.A)
281 npt.assert_equal(nx.adj_matrix(self.MG2).todense(), self.MG2A)
282 npt.assert_equal(
283 nx.adj_matrix(self.G, nodelist=[0, 1]).todense(), self.A[:2, :2]
284 )
285 npt.assert_equal(nx.adj_matrix(self.WG).todense(), self.WA)
286 npt.assert_equal(nx.adj_matrix(self.WG, weight=None).todense(), self.A)
287 npt.assert_equal(nx.adj_matrix(self.MG2, weight=None).todense(), self.MG2A)
288 npt.assert_equal(
289 nx.adj_matrix(self.WG, weight="other").todense(), 0.6 * self.WA
290 )
291 npt.assert_equal(
292 nx.adj_matrix(self.no_edges_G, nodelist=[1, 3]).todense(), self.no_edges_A
293 )