comparison env/lib/python3.9/site-packages/networkx/algorithms/operators/tests/test_all.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 import networkx as nx
3 from networkx.testing import assert_edges_equal
4
5
6 def test_union_all_attributes():
7 g = nx.Graph()
8 g.add_node(0, x=4)
9 g.add_node(1, x=5)
10 g.add_edge(0, 1, size=5)
11 g.graph["name"] = "g"
12
13 h = g.copy()
14 h.graph["name"] = "h"
15 h.graph["attr"] = "attr"
16 h.nodes[0]["x"] = 7
17
18 j = g.copy()
19 j.graph["name"] = "j"
20 j.graph["attr"] = "attr"
21 j.nodes[0]["x"] = 7
22
23 ghj = nx.union_all([g, h, j], rename=("g", "h", "j"))
24 assert set(ghj.nodes()) == {"h0", "h1", "g0", "g1", "j0", "j1"}
25 for n in ghj:
26 graph, node = n
27 assert ghj.nodes[n] == eval(graph).nodes[int(node)]
28
29 assert ghj.graph["attr"] == "attr"
30 assert ghj.graph["name"] == "j" # j graph attributes take precendent
31
32
33 def test_intersection_all():
34 G = nx.Graph()
35 H = nx.Graph()
36 R = nx.Graph()
37 G.add_nodes_from([1, 2, 3, 4])
38 G.add_edge(1, 2)
39 G.add_edge(2, 3)
40 H.add_nodes_from([1, 2, 3, 4])
41 H.add_edge(2, 3)
42 H.add_edge(3, 4)
43 R.add_nodes_from([1, 2, 3, 4])
44 R.add_edge(2, 3)
45 R.add_edge(4, 1)
46 I = nx.intersection_all([G, H, R])
47 assert set(I.nodes()) == {1, 2, 3, 4}
48 assert sorted(I.edges()) == [(2, 3)]
49
50
51 def test_intersection_all_attributes():
52 g = nx.Graph()
53 g.add_node(0, x=4)
54 g.add_node(1, x=5)
55 g.add_edge(0, 1, size=5)
56 g.graph["name"] = "g"
57
58 h = g.copy()
59 h.graph["name"] = "h"
60 h.graph["attr"] = "attr"
61 h.nodes[0]["x"] = 7
62
63 gh = nx.intersection_all([g, h])
64 assert set(gh.nodes()) == set(g.nodes())
65 assert set(gh.nodes()) == set(h.nodes())
66 assert sorted(gh.edges()) == sorted(g.edges())
67
68 h.remove_node(0)
69 pytest.raises(nx.NetworkXError, nx.intersection, g, h)
70
71
72 def test_intersection_all_multigraph_attributes():
73 g = nx.MultiGraph()
74 g.add_edge(0, 1, key=0)
75 g.add_edge(0, 1, key=1)
76 g.add_edge(0, 1, key=2)
77 h = nx.MultiGraph()
78 h.add_edge(0, 1, key=0)
79 h.add_edge(0, 1, key=3)
80 gh = nx.intersection_all([g, h])
81 assert set(gh.nodes()) == set(g.nodes())
82 assert set(gh.nodes()) == set(h.nodes())
83 assert sorted(gh.edges()) == [(0, 1)]
84 assert sorted(gh.edges(keys=True)) == [(0, 1, 0)]
85
86
87 def test_union_all_and_compose_all():
88 K3 = nx.complete_graph(3)
89 P3 = nx.path_graph(3)
90
91 G1 = nx.DiGraph()
92 G1.add_edge("A", "B")
93 G1.add_edge("A", "C")
94 G1.add_edge("A", "D")
95 G2 = nx.DiGraph()
96 G2.add_edge("1", "2")
97 G2.add_edge("1", "3")
98 G2.add_edge("1", "4")
99
100 G = nx.union_all([G1, G2])
101 H = nx.compose_all([G1, G2])
102 assert_edges_equal(G.edges(), H.edges())
103 assert not G.has_edge("A", "1")
104 pytest.raises(nx.NetworkXError, nx.union, K3, P3)
105 H1 = nx.union_all([H, G1], rename=("H", "G1"))
106 assert sorted(H1.nodes()) == [
107 "G1A",
108 "G1B",
109 "G1C",
110 "G1D",
111 "H1",
112 "H2",
113 "H3",
114 "H4",
115 "HA",
116 "HB",
117 "HC",
118 "HD",
119 ]
120
121 H2 = nx.union_all([H, G2], rename=("H", ""))
122 assert sorted(H2.nodes()) == [
123 "1",
124 "2",
125 "3",
126 "4",
127 "H1",
128 "H2",
129 "H3",
130 "H4",
131 "HA",
132 "HB",
133 "HC",
134 "HD",
135 ]
136
137 assert not H1.has_edge("NB", "NA")
138
139 G = nx.compose_all([G, G])
140 assert_edges_equal(G.edges(), H.edges())
141
142 G2 = nx.union_all([G2, G2], rename=("", "copy"))
143 assert sorted(G2.nodes()) == [
144 "1",
145 "2",
146 "3",
147 "4",
148 "copy1",
149 "copy2",
150 "copy3",
151 "copy4",
152 ]
153
154 assert sorted(G2.neighbors("copy4")) == []
155 assert sorted(G2.neighbors("copy1")) == ["copy2", "copy3", "copy4"]
156 assert len(G) == 8
157 assert nx.number_of_edges(G) == 6
158
159 E = nx.disjoint_union_all([G, G])
160 assert len(E) == 16
161 assert nx.number_of_edges(E) == 12
162
163 E = nx.disjoint_union_all([G1, G2])
164 assert sorted(E.nodes()) == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
165
166 G1 = nx.DiGraph()
167 G1.add_edge("A", "B")
168 G2 = nx.DiGraph()
169 G2.add_edge(1, 2)
170 G3 = nx.DiGraph()
171 G3.add_edge(11, 22)
172 G4 = nx.union_all([G1, G2, G3], rename=("G1", "G2", "G3"))
173 assert sorted(G4.nodes()) == ["G1A", "G1B", "G21", "G22", "G311", "G322"]
174
175
176 def test_union_all_multigraph():
177 G = nx.MultiGraph()
178 G.add_edge(1, 2, key=0)
179 G.add_edge(1, 2, key=1)
180 H = nx.MultiGraph()
181 H.add_edge(3, 4, key=0)
182 H.add_edge(3, 4, key=1)
183 GH = nx.union_all([G, H])
184 assert set(GH) == set(G) | set(H)
185 assert set(GH.edges(keys=True)) == set(G.edges(keys=True)) | set(H.edges(keys=True))
186
187
188 def test_input_output():
189 l = [nx.Graph([(1, 2)]), nx.Graph([(3, 4)])]
190 U = nx.disjoint_union_all(l)
191 assert len(l) == 2
192 C = nx.compose_all(l)
193 assert len(l) == 2
194 l = [nx.Graph([(1, 2)]), nx.Graph([(1, 2)])]
195 R = nx.intersection_all(l)
196 assert len(l) == 2
197
198
199 def test_mixed_type_union():
200 with pytest.raises(nx.NetworkXError):
201 G = nx.Graph()
202 H = nx.MultiGraph()
203 I = nx.Graph()
204 U = nx.union_all([G, H, I])
205
206
207 def test_mixed_type_disjoint_union():
208 with pytest.raises(nx.NetworkXError):
209 G = nx.Graph()
210 H = nx.MultiGraph()
211 I = nx.Graph()
212 U = nx.disjoint_union_all([G, H, I])
213
214
215 def test_mixed_type_intersection():
216 with pytest.raises(nx.NetworkXError):
217 G = nx.Graph()
218 H = nx.MultiGraph()
219 I = nx.Graph()
220 U = nx.intersection_all([G, H, I])
221
222
223 def test_mixed_type_compose():
224 with pytest.raises(nx.NetworkXError):
225 G = nx.Graph()
226 H = nx.MultiGraph()
227 I = nx.Graph()
228 U = nx.compose_all([G, H, I])
229
230
231 def test_empty_union():
232 with pytest.raises(ValueError):
233 nx.union_all([])
234
235
236 def test_empty_disjoint_union():
237 with pytest.raises(ValueError):
238 nx.disjoint_union_all([])
239
240
241 def test_empty_compose_all():
242 with pytest.raises(ValueError):
243 nx.compose_all([])
244
245
246 def test_empty_intersection_all():
247 with pytest.raises(ValueError):
248 nx.intersection_all([])