comparison env/lib/python3.9/site-packages/networkx/readwrite/tests/test_edgelist.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 """
2 Unit tests for edgelists.
3 """
4 import pytest
5 import io
6 import tempfile
7 import os
8
9 import networkx as nx
10 from networkx.testing import assert_edges_equal, assert_nodes_equal, assert_graphs_equal
11
12
13 class TestEdgelist:
14 @classmethod
15 def setup_class(cls):
16 cls.G = nx.Graph(name="test")
17 e = [("a", "b"), ("b", "c"), ("c", "d"), ("d", "e"), ("e", "f"), ("a", "f")]
18 cls.G.add_edges_from(e)
19 cls.G.add_node("g")
20 cls.DG = nx.DiGraph(cls.G)
21 cls.XG = nx.MultiGraph()
22 cls.XG.add_weighted_edges_from([(1, 2, 5), (1, 2, 5), (1, 2, 1), (3, 3, 42)])
23 cls.XDG = nx.MultiDiGraph(cls.XG)
24
25 def test_read_edgelist_1(self):
26 s = b"""\
27 # comment line
28 1 2
29 # comment line
30 2 3
31 """
32 bytesIO = io.BytesIO(s)
33 G = nx.read_edgelist(bytesIO, nodetype=int)
34 assert_edges_equal(G.edges(), [(1, 2), (2, 3)])
35
36 def test_read_edgelist_2(self):
37 s = b"""\
38 # comment line
39 1 2 2.0
40 # comment line
41 2 3 3.0
42 """
43 bytesIO = io.BytesIO(s)
44 G = nx.read_edgelist(bytesIO, nodetype=int, data=False)
45 assert_edges_equal(G.edges(), [(1, 2), (2, 3)])
46
47 bytesIO = io.BytesIO(s)
48 G = nx.read_weighted_edgelist(bytesIO, nodetype=int)
49 assert_edges_equal(
50 G.edges(data=True), [(1, 2, {"weight": 2.0}), (2, 3, {"weight": 3.0})]
51 )
52
53 def test_read_edgelist_3(self):
54 s = b"""\
55 # comment line
56 1 2 {'weight':2.0}
57 # comment line
58 2 3 {'weight':3.0}
59 """
60 bytesIO = io.BytesIO(s)
61 G = nx.read_edgelist(bytesIO, nodetype=int, data=False)
62 assert_edges_equal(G.edges(), [(1, 2), (2, 3)])
63
64 bytesIO = io.BytesIO(s)
65 G = nx.read_edgelist(bytesIO, nodetype=int, data=True)
66 assert_edges_equal(
67 G.edges(data=True), [(1, 2, {"weight": 2.0}), (2, 3, {"weight": 3.0})]
68 )
69
70 def test_read_edgelist_4(self):
71 s = b"""\
72 # comment line
73 1 2 {'weight':2.0}
74 # comment line
75 2 3 {'weight':3.0}
76 """
77 bytesIO = io.BytesIO(s)
78 G = nx.read_edgelist(bytesIO, nodetype=int, data=False)
79 assert_edges_equal(G.edges(), [(1, 2), (2, 3)])
80
81 bytesIO = io.BytesIO(s)
82 G = nx.read_edgelist(bytesIO, nodetype=int, data=True)
83 assert_edges_equal(
84 G.edges(data=True), [(1, 2, {"weight": 2.0}), (2, 3, {"weight": 3.0})]
85 )
86
87 s = """\
88 # comment line
89 1 2 {'weight':2.0}
90 # comment line
91 2 3 {'weight':3.0}
92 """
93 StringIO = io.StringIO(s)
94 G = nx.read_edgelist(StringIO, nodetype=int, data=False)
95 assert_edges_equal(G.edges(), [(1, 2), (2, 3)])
96
97 StringIO = io.StringIO(s)
98 G = nx.read_edgelist(StringIO, nodetype=int, data=True)
99 assert_edges_equal(
100 G.edges(data=True), [(1, 2, {"weight": 2.0}), (2, 3, {"weight": 3.0})]
101 )
102
103 def test_read_edgelist_5(self):
104 s = b"""\
105 # comment line
106 1 2 {'weight':2.0, 'color':'green'}
107 # comment line
108 2 3 {'weight':3.0, 'color':'red'}
109 """
110 bytesIO = io.BytesIO(s)
111 G = nx.read_edgelist(bytesIO, nodetype=int, data=False)
112 assert_edges_equal(G.edges(), [(1, 2), (2, 3)])
113
114 bytesIO = io.BytesIO(s)
115 G = nx.read_edgelist(bytesIO, nodetype=int, data=True)
116 assert_edges_equal(
117 G.edges(data=True),
118 [
119 (1, 2, {"weight": 2.0, "color": "green"}),
120 (2, 3, {"weight": 3.0, "color": "red"}),
121 ],
122 )
123
124 def test_read_edgelist_6(self):
125 s = b"""\
126 # comment line
127 1, 2, {'weight':2.0, 'color':'green'}
128 # comment line
129 2, 3, {'weight':3.0, 'color':'red'}
130 """
131 bytesIO = io.BytesIO(s)
132 G = nx.read_edgelist(bytesIO, nodetype=int, data=False, delimiter=",")
133 assert_edges_equal(G.edges(), [(1, 2), (2, 3)])
134
135 bytesIO = io.BytesIO(s)
136 G = nx.read_edgelist(bytesIO, nodetype=int, data=True, delimiter=",")
137 assert_edges_equal(
138 G.edges(data=True),
139 [
140 (1, 2, {"weight": 2.0, "color": "green"}),
141 (2, 3, {"weight": 3.0, "color": "red"}),
142 ],
143 )
144
145 def test_write_edgelist_1(self):
146 fh = io.BytesIO()
147 G = nx.OrderedGraph()
148 G.add_edges_from([(1, 2), (2, 3)])
149 nx.write_edgelist(G, fh, data=False)
150 fh.seek(0)
151 assert fh.read() == b"1 2\n2 3\n"
152
153 def test_write_edgelist_2(self):
154 fh = io.BytesIO()
155 G = nx.OrderedGraph()
156 G.add_edges_from([(1, 2), (2, 3)])
157 nx.write_edgelist(G, fh, data=True)
158 fh.seek(0)
159 assert fh.read() == b"1 2 {}\n2 3 {}\n"
160
161 def test_write_edgelist_3(self):
162 fh = io.BytesIO()
163 G = nx.OrderedGraph()
164 G.add_edge(1, 2, weight=2.0)
165 G.add_edge(2, 3, weight=3.0)
166 nx.write_edgelist(G, fh, data=True)
167 fh.seek(0)
168 assert fh.read() == b"1 2 {'weight': 2.0}\n2 3 {'weight': 3.0}\n"
169
170 def test_write_edgelist_4(self):
171 fh = io.BytesIO()
172 G = nx.OrderedGraph()
173 G.add_edge(1, 2, weight=2.0)
174 G.add_edge(2, 3, weight=3.0)
175 nx.write_edgelist(G, fh, data=[("weight")])
176 fh.seek(0)
177 assert fh.read() == b"1 2 2.0\n2 3 3.0\n"
178
179 def test_unicode(self):
180 G = nx.Graph()
181 name1 = chr(2344) + chr(123) + chr(6543)
182 name2 = chr(5543) + chr(1543) + chr(324)
183 G.add_edge(name1, "Radiohead", **{name2: 3})
184 fd, fname = tempfile.mkstemp()
185 nx.write_edgelist(G, fname)
186 H = nx.read_edgelist(fname)
187 assert_graphs_equal(G, H)
188 os.close(fd)
189 os.unlink(fname)
190
191 def test_latin1_issue(self):
192 G = nx.Graph()
193 name1 = chr(2344) + chr(123) + chr(6543)
194 name2 = chr(5543) + chr(1543) + chr(324)
195 G.add_edge(name1, "Radiohead", **{name2: 3})
196 fd, fname = tempfile.mkstemp()
197 pytest.raises(
198 UnicodeEncodeError, nx.write_edgelist, G, fname, encoding="latin-1"
199 )
200 os.close(fd)
201 os.unlink(fname)
202
203 def test_latin1(self):
204 G = nx.Graph()
205 name1 = "Bj" + chr(246) + "rk"
206 name2 = chr(220) + "ber"
207 G.add_edge(name1, "Radiohead", **{name2: 3})
208 fd, fname = tempfile.mkstemp()
209 nx.write_edgelist(G, fname, encoding="latin-1")
210 H = nx.read_edgelist(fname, encoding="latin-1")
211 assert_graphs_equal(G, H)
212 os.close(fd)
213 os.unlink(fname)
214
215 def test_edgelist_graph(self):
216 G = self.G
217 (fd, fname) = tempfile.mkstemp()
218 nx.write_edgelist(G, fname)
219 H = nx.read_edgelist(fname)
220 H2 = nx.read_edgelist(fname)
221 assert H != H2 # they should be different graphs
222 G.remove_node("g") # isolated nodes are not written in edgelist
223 assert_nodes_equal(list(H), list(G))
224 assert_edges_equal(list(H.edges()), list(G.edges()))
225 os.close(fd)
226 os.unlink(fname)
227
228 def test_edgelist_digraph(self):
229 G = self.DG
230 (fd, fname) = tempfile.mkstemp()
231 nx.write_edgelist(G, fname)
232 H = nx.read_edgelist(fname, create_using=nx.DiGraph())
233 H2 = nx.read_edgelist(fname, create_using=nx.DiGraph())
234 assert H != H2 # they should be different graphs
235 G.remove_node("g") # isolated nodes are not written in edgelist
236 assert_nodes_equal(list(H), list(G))
237 assert_edges_equal(list(H.edges()), list(G.edges()))
238 os.close(fd)
239 os.unlink(fname)
240
241 def test_edgelist_integers(self):
242 G = nx.convert_node_labels_to_integers(self.G)
243 (fd, fname) = tempfile.mkstemp()
244 nx.write_edgelist(G, fname)
245 H = nx.read_edgelist(fname, nodetype=int)
246 # isolated nodes are not written in edgelist
247 G.remove_nodes_from(list(nx.isolates(G)))
248 assert_nodes_equal(list(H), list(G))
249 assert_edges_equal(list(H.edges()), list(G.edges()))
250 os.close(fd)
251 os.unlink(fname)
252
253 def test_edgelist_multigraph(self):
254 G = self.XG
255 (fd, fname) = tempfile.mkstemp()
256 nx.write_edgelist(G, fname)
257 H = nx.read_edgelist(fname, nodetype=int, create_using=nx.MultiGraph())
258 H2 = nx.read_edgelist(fname, nodetype=int, create_using=nx.MultiGraph())
259 assert H != H2 # they should be different graphs
260 assert_nodes_equal(list(H), list(G))
261 assert_edges_equal(list(H.edges()), list(G.edges()))
262 os.close(fd)
263 os.unlink(fname)
264
265 def test_edgelist_multidigraph(self):
266 G = self.XDG
267 (fd, fname) = tempfile.mkstemp()
268 nx.write_edgelist(G, fname)
269 H = nx.read_edgelist(fname, nodetype=int, create_using=nx.MultiDiGraph())
270 H2 = nx.read_edgelist(fname, nodetype=int, create_using=nx.MultiDiGraph())
271 assert H != H2 # they should be different graphs
272 assert_nodes_equal(list(H), list(G))
273 assert_edges_equal(list(H.edges()), list(G.edges()))
274 os.close(fd)
275 os.unlink(fname)