### comparison env/lib/python3.9/site-packages/networkx/tests/test_convert.py @ 0:4f3585e2f14bdraftdefaulttip

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