Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/networkx/tests/test_convert.py @ 1:56ad4e20f292 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author | guerler |
---|---|
date | Fri, 31 Jul 2020 00:32:28 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:d30785e31577 | 1:56ad4e20f292 |
---|---|
1 #!/usr/bin/env python | |
2 import pytest | |
3 | |
4 import networkx as nx | |
5 from networkx.testing import assert_nodes_equal, assert_edges_equal, assert_graphs_equal | |
6 from networkx.convert import (to_networkx_graph, | |
7 to_dict_of_dicts, | |
8 from_dict_of_dicts, | |
9 to_dict_of_lists, | |
10 from_dict_of_lists) | |
11 from networkx.generators.classic import barbell_graph, cycle_graph | |
12 | |
13 | |
14 class TestConvert(): | |
15 def edgelists_equal(self, e1, e2): | |
16 return sorted(sorted(e) for e in e1) == sorted(sorted(e) for e in e2) | |
17 | |
18 def test_simple_graphs(self): | |
19 for dest, source in [(to_dict_of_dicts, from_dict_of_dicts), | |
20 (to_dict_of_lists, from_dict_of_lists)]: | |
21 G = barbell_graph(10, 3) | |
22 G.graph = {} | |
23 dod = dest(G) | |
24 | |
25 # Dict of [dicts, lists] | |
26 GG = source(dod) | |
27 assert_graphs_equal(G, GG) | |
28 GW = to_networkx_graph(dod) | |
29 assert_graphs_equal(G, GW) | |
30 GI = nx.Graph(dod) | |
31 assert_graphs_equal(G, GI) | |
32 | |
33 # With nodelist keyword | |
34 P4 = nx.path_graph(4) | |
35 P3 = nx.path_graph(3) | |
36 P4.graph = {} | |
37 P3.graph = {} | |
38 dod = dest(P4, nodelist=[0, 1, 2]) | |
39 Gdod = nx.Graph(dod) | |
40 assert_graphs_equal(Gdod, P3) | |
41 | |
42 def test_exceptions(self): | |
43 # NX graph | |
44 class G(object): | |
45 adj = None | |
46 | |
47 pytest.raises(nx.NetworkXError, to_networkx_graph, G) | |
48 | |
49 # pygraphviz agraph | |
50 class G(object): | |
51 is_strict = None | |
52 | |
53 pytest.raises(nx.NetworkXError, to_networkx_graph, G) | |
54 | |
55 # Dict of [dicts, lists] | |
56 G = {"a": 0} | |
57 pytest.raises(TypeError, to_networkx_graph, G) | |
58 | |
59 # list or generator of edges | |
60 class G(object): | |
61 next = None | |
62 | |
63 pytest.raises(nx.NetworkXError, to_networkx_graph, G) | |
64 | |
65 # no match | |
66 pytest.raises(nx.NetworkXError, to_networkx_graph, "a") | |
67 | |
68 def test_digraphs(self): | |
69 for dest, source in [(to_dict_of_dicts, from_dict_of_dicts), | |
70 (to_dict_of_lists, from_dict_of_lists)]: | |
71 G = cycle_graph(10) | |
72 | |
73 # Dict of [dicts, lists] | |
74 dod = dest(G) | |
75 GG = source(dod) | |
76 assert_nodes_equal(sorted(G.nodes()), sorted(GG.nodes())) | |
77 assert_edges_equal(sorted(G.edges()), sorted(GG.edges())) | |
78 GW = to_networkx_graph(dod) | |
79 assert_nodes_equal(sorted(G.nodes()), sorted(GW.nodes())) | |
80 assert_edges_equal(sorted(G.edges()), sorted(GW.edges())) | |
81 GI = nx.Graph(dod) | |
82 assert_nodes_equal(sorted(G.nodes()), sorted(GI.nodes())) | |
83 assert_edges_equal(sorted(G.edges()), sorted(GI.edges())) | |
84 | |
85 G = cycle_graph(10, create_using=nx.DiGraph) | |
86 dod = dest(G) | |
87 GG = source(dod, create_using=nx.DiGraph) | |
88 assert sorted(G.nodes()) == sorted(GG.nodes()) | |
89 assert sorted(G.edges()) == sorted(GG.edges()) | |
90 GW = to_networkx_graph(dod, create_using=nx.DiGraph) | |
91 assert sorted(G.nodes()) == sorted(GW.nodes()) | |
92 assert sorted(G.edges()) == sorted(GW.edges()) | |
93 GI = nx.DiGraph(dod) | |
94 assert sorted(G.nodes()) == sorted(GI.nodes()) | |
95 assert sorted(G.edges()) == sorted(GI.edges()) | |
96 | |
97 def test_graph(self): | |
98 g = nx.cycle_graph(10) | |
99 G = nx.Graph() | |
100 G.add_nodes_from(g) | |
101 G.add_weighted_edges_from((u, v, u) for u, v in g.edges()) | |
102 | |
103 # Dict of dicts | |
104 dod = to_dict_of_dicts(G) | |
105 GG = from_dict_of_dicts(dod, create_using=nx.Graph) | |
106 assert_nodes_equal(sorted(G.nodes()), sorted(GG.nodes())) | |
107 assert_edges_equal(sorted(G.edges()), sorted(GG.edges())) | |
108 GW = to_networkx_graph(dod, create_using=nx.Graph) | |
109 assert_nodes_equal(sorted(G.nodes()), sorted(GW.nodes())) | |
110 assert_edges_equal(sorted(G.edges()), sorted(GW.edges())) | |
111 GI = nx.Graph(dod) | |
112 assert sorted(G.nodes()) == sorted(GI.nodes()) | |
113 assert sorted(G.edges()) == sorted(GI.edges()) | |
114 | |
115 # Dict of lists | |
116 dol = to_dict_of_lists(G) | |
117 GG = from_dict_of_lists(dol, create_using=nx.Graph) | |
118 # dict of lists throws away edge data so set it to none | |
119 enone = [(u, v, {}) for (u, v, d) in G.edges(data=True)] | |
120 assert_nodes_equal(sorted(G.nodes()), sorted(GG.nodes())) | |
121 assert_edges_equal(enone, sorted(GG.edges(data=True))) | |
122 GW = to_networkx_graph(dol, create_using=nx.Graph) | |
123 assert_nodes_equal(sorted(G.nodes()), sorted(GW.nodes())) | |
124 assert_edges_equal(enone, sorted(GW.edges(data=True))) | |
125 GI = nx.Graph(dol) | |
126 assert_nodes_equal(sorted(G.nodes()), sorted(GI.nodes())) | |
127 assert_edges_equal(enone, sorted(GI.edges(data=True))) | |
128 | |
129 def test_with_multiedges_self_loops(self): | |
130 G = cycle_graph(10) | |
131 XG = nx.Graph() | |
132 XG.add_nodes_from(G) | |
133 XG.add_weighted_edges_from((u, v, u) for u, v in G.edges()) | |
134 XGM = nx.MultiGraph() | |
135 XGM.add_nodes_from(G) | |
136 XGM.add_weighted_edges_from((u, v, u) for u, v in G.edges()) | |
137 XGM.add_edge(0, 1, weight=2) # multiedge | |
138 XGS = nx.Graph() | |
139 XGS.add_nodes_from(G) | |
140 XGS.add_weighted_edges_from((u, v, u) for u, v in G.edges()) | |
141 XGS.add_edge(0, 0, weight=100) # self loop | |
142 | |
143 # Dict of dicts | |
144 # with self loops, OK | |
145 dod = to_dict_of_dicts(XGS) | |
146 GG = from_dict_of_dicts(dod, create_using=nx.Graph) | |
147 assert_nodes_equal(XGS.nodes(), GG.nodes()) | |
148 assert_edges_equal(XGS.edges(), GG.edges()) | |
149 GW = to_networkx_graph(dod, create_using=nx.Graph) | |
150 assert_nodes_equal(XGS.nodes(), GW.nodes()) | |
151 assert_edges_equal(XGS.edges(), GW.edges()) | |
152 GI = nx.Graph(dod) | |
153 assert_nodes_equal(XGS.nodes(), GI.nodes()) | |
154 assert_edges_equal(XGS.edges(), GI.edges()) | |
155 | |
156 # Dict of lists | |
157 # with self loops, OK | |
158 dol = to_dict_of_lists(XGS) | |
159 GG = from_dict_of_lists(dol, create_using=nx.Graph) | |
160 # dict of lists throws away edge data so set it to none | |
161 enone = [(u, v, {}) for (u, v, d) in XGS.edges(data=True)] | |
162 assert_nodes_equal(sorted(XGS.nodes()), sorted(GG.nodes())) | |
163 assert_edges_equal(enone, sorted(GG.edges(data=True))) | |
164 GW = to_networkx_graph(dol, create_using=nx.Graph) | |
165 assert_nodes_equal(sorted(XGS.nodes()), sorted(GW.nodes())) | |
166 assert_edges_equal(enone, sorted(GW.edges(data=True))) | |
167 GI = nx.Graph(dol) | |
168 assert_nodes_equal(sorted(XGS.nodes()), sorted(GI.nodes())) | |
169 assert_edges_equal(enone, sorted(GI.edges(data=True))) | |
170 | |
171 # Dict of dicts | |
172 # with multiedges, OK | |
173 dod = to_dict_of_dicts(XGM) | |
174 GG = from_dict_of_dicts(dod, create_using=nx.MultiGraph, | |
175 multigraph_input=True) | |
176 assert_nodes_equal(sorted(XGM.nodes()), sorted(GG.nodes())) | |
177 assert_edges_equal(sorted(XGM.edges()), sorted(GG.edges())) | |
178 GW = to_networkx_graph(dod, create_using=nx.MultiGraph, multigraph_input=True) | |
179 assert_nodes_equal(sorted(XGM.nodes()), sorted(GW.nodes())) | |
180 assert_edges_equal(sorted(XGM.edges()), sorted(GW.edges())) | |
181 GI = nx.MultiGraph(dod) # convert can't tell whether to duplicate edges! | |
182 assert_nodes_equal(sorted(XGM.nodes()), sorted(GI.nodes())) | |
183 #assert_not_equal(sorted(XGM.edges()), sorted(GI.edges())) | |
184 assert not sorted(XGM.edges()) == sorted(GI.edges()) | |
185 GE = from_dict_of_dicts(dod, create_using=nx.MultiGraph, | |
186 multigraph_input=False) | |
187 assert_nodes_equal(sorted(XGM.nodes()), sorted(GE.nodes())) | |
188 assert sorted(XGM.edges()) != sorted(GE.edges()) | |
189 GI = nx.MultiGraph(XGM) | |
190 assert_nodes_equal(sorted(XGM.nodes()), sorted(GI.nodes())) | |
191 assert_edges_equal(sorted(XGM.edges()), sorted(GI.edges())) | |
192 GM = nx.MultiGraph(G) | |
193 assert_nodes_equal(sorted(GM.nodes()), sorted(G.nodes())) | |
194 assert_edges_equal(sorted(GM.edges()), sorted(G.edges())) | |
195 | |
196 # Dict of lists | |
197 # with multiedges, OK, but better write as DiGraph else you'll | |
198 # get double edges | |
199 dol = to_dict_of_lists(G) | |
200 GG = from_dict_of_lists(dol, create_using=nx.MultiGraph) | |
201 assert_nodes_equal(sorted(G.nodes()), sorted(GG.nodes())) | |
202 assert_edges_equal(sorted(G.edges()), sorted(GG.edges())) | |
203 GW = to_networkx_graph(dol, create_using=nx.MultiGraph) | |
204 assert_nodes_equal(sorted(G.nodes()), sorted(GW.nodes())) | |
205 assert_edges_equal(sorted(G.edges()), sorted(GW.edges())) | |
206 GI = nx.MultiGraph(dol) | |
207 assert_nodes_equal(sorted(G.nodes()), sorted(GI.nodes())) | |
208 assert_edges_equal(sorted(G.edges()), sorted(GI.edges())) | |
209 | |
210 def test_edgelists(self): | |
211 P = nx.path_graph(4) | |
212 e = [(0, 1), (1, 2), (2, 3)] | |
213 G = nx.Graph(e) | |
214 assert_nodes_equal(sorted(G.nodes()), sorted(P.nodes())) | |
215 assert_edges_equal(sorted(G.edges()), sorted(P.edges())) | |
216 assert_edges_equal(sorted(G.edges(data=True)), sorted(P.edges(data=True))) | |
217 | |
218 e = [(0, 1, {}), (1, 2, {}), (2, 3, {})] | |
219 G = nx.Graph(e) | |
220 assert_nodes_equal(sorted(G.nodes()), sorted(P.nodes())) | |
221 assert_edges_equal(sorted(G.edges()), sorted(P.edges())) | |
222 assert_edges_equal(sorted(G.edges(data=True)), sorted(P.edges(data=True))) | |
223 | |
224 e = ((n, n + 1) for n in range(3)) | |
225 G = nx.Graph(e) | |
226 assert_nodes_equal(sorted(G.nodes()), sorted(P.nodes())) | |
227 assert_edges_equal(sorted(G.edges()), sorted(P.edges())) | |
228 assert_edges_equal(sorted(G.edges(data=True)), sorted(P.edges(data=True))) | |
229 | |
230 def test_directed_to_undirected(self): | |
231 edges1 = [(0, 1), (1, 2), (2, 0)] | |
232 edges2 = [(0, 1), (1, 2), (0, 2)] | |
233 assert self.edgelists_equal(nx.Graph(nx.DiGraph(edges1)).edges(), edges1) | |
234 assert self.edgelists_equal(nx.Graph(nx.DiGraph(edges2)).edges(), edges1) | |
235 assert self.edgelists_equal(nx.MultiGraph(nx.DiGraph(edges1)).edges(), edges1) | |
236 assert self.edgelists_equal(nx.MultiGraph(nx.DiGraph(edges2)).edges(), edges1) | |
237 | |
238 assert self.edgelists_equal(nx.MultiGraph(nx.MultiDiGraph(edges1)).edges(), | |
239 edges1) | |
240 assert self.edgelists_equal(nx.MultiGraph(nx.MultiDiGraph(edges2)).edges(), | |
241 edges1) | |
242 | |
243 assert self.edgelists_equal(nx.Graph(nx.MultiDiGraph(edges1)).edges(), edges1) | |
244 assert self.edgelists_equal(nx.Graph(nx.MultiDiGraph(edges2)).edges(), edges1) | |
245 | |
246 def test_attribute_dict_integrity(self): | |
247 # we must not replace dict-like graph data structures with dicts | |
248 G = nx.OrderedGraph() | |
249 G.add_nodes_from("abc") | |
250 H = to_networkx_graph(G, create_using=nx.OrderedGraph) | |
251 assert list(H.nodes) == list(G.nodes) | |
252 H = nx.OrderedDiGraph(G) | |
253 assert list(H.nodes) == list(G.nodes) | |
254 | |
255 def test_to_edgelist(self): | |
256 G = nx.Graph([(1, 1)]) | |
257 elist = nx.to_edgelist(G, nodelist=list(G)) | |
258 assert_edges_equal(G.edges(data=True), elist) | |
259 | |
260 def test_custom_node_attr_dict_safekeeping(self): | |
261 class custom_dict(dict): | |
262 pass | |
263 | |
264 class Custom(nx.Graph): | |
265 node_attr_dict_factory = custom_dict | |
266 | |
267 g = nx.Graph() | |
268 g.add_node(1, weight=1) | |
269 | |
270 h = Custom(g) | |
271 assert isinstance(g._node[1], dict) | |
272 assert isinstance(h._node[1], custom_dict) | |
273 | |
274 # this raise exception | |
275 # h._node.update((n, dd.copy()) for n, dd in g.nodes.items()) | |
276 # assert isinstance(h._node[1], custom_dict) |