Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/readwrite/tests/test_pajek.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 Pajek tests | |
3 """ | |
4 import networkx as nx | |
5 import os | |
6 import tempfile | |
7 from networkx.testing import assert_edges_equal, assert_nodes_equal | |
8 | |
9 | |
10 class TestPajek: | |
11 @classmethod | |
12 def setup_class(cls): | |
13 cls.data = """*network Tralala\n*vertices 4\n 1 "A1" 0.0938 0.0896 ellipse x_fact 1 y_fact 1\n 2 "Bb" 0.8188 0.2458 ellipse x_fact 1 y_fact 1\n 3 "C" 0.3688 0.7792 ellipse x_fact 1\n 4 "D2" 0.9583 0.8563 ellipse x_fact 1\n*arcs\n1 1 1 h2 0 w 3 c Blue s 3 a1 -130 k1 0.6 a2 -130 k2 0.6 ap 0.5 l "Bezier loop" lc BlueViolet fos 20 lr 58 lp 0.3 la 360\n2 1 1 h2 0 a1 120 k1 1.3 a2 -120 k2 0.3 ap 25 l "Bezier arc" lphi 270 la 180 lr 19 lp 0.5\n1 2 1 h2 0 a1 40 k1 2.8 a2 30 k2 0.8 ap 25 l "Bezier arc" lphi 90 la 0 lp 0.65\n4 2 -1 h2 0 w 1 k1 -2 k2 250 ap 25 l "Circular arc" c Red lc OrangeRed\n3 4 1 p Dashed h2 0 w 2 c OliveGreen ap 25 l "Straight arc" lc PineGreen\n1 3 1 p Dashed h2 0 w 5 k1 -1 k2 -20 ap 25 l "Oval arc" c Brown lc Black\n3 3 -1 h1 6 w 1 h2 12 k1 -2 k2 -15 ap 0.5 l "Circular loop" c Red lc OrangeRed lphi 270 la 180""" | |
14 cls.G = nx.MultiDiGraph() | |
15 cls.G.add_nodes_from(["A1", "Bb", "C", "D2"]) | |
16 cls.G.add_edges_from( | |
17 [ | |
18 ("A1", "A1"), | |
19 ("A1", "Bb"), | |
20 ("A1", "C"), | |
21 ("Bb", "A1"), | |
22 ("C", "C"), | |
23 ("C", "D2"), | |
24 ("D2", "Bb"), | |
25 ] | |
26 ) | |
27 | |
28 cls.G.graph["name"] = "Tralala" | |
29 (fd, cls.fname) = tempfile.mkstemp() | |
30 with os.fdopen(fd, "wb") as fh: | |
31 fh.write(cls.data.encode("UTF-8")) | |
32 | |
33 @classmethod | |
34 def teardown_class(cls): | |
35 os.unlink(cls.fname) | |
36 | |
37 def test_parse_pajek_simple(self): | |
38 # Example without node positions or shape | |
39 data = """*Vertices 2\n1 "1"\n2 "2"\n*Edges\n1 2\n2 1""" | |
40 G = nx.parse_pajek(data) | |
41 assert sorted(G.nodes()) == ["1", "2"] | |
42 assert_edges_equal(G.edges(), [("1", "2"), ("1", "2")]) | |
43 | |
44 def test_parse_pajek(self): | |
45 G = nx.parse_pajek(self.data) | |
46 assert sorted(G.nodes()) == ["A1", "Bb", "C", "D2"] | |
47 assert_edges_equal( | |
48 G.edges(), | |
49 [ | |
50 ("A1", "A1"), | |
51 ("A1", "Bb"), | |
52 ("A1", "C"), | |
53 ("Bb", "A1"), | |
54 ("C", "C"), | |
55 ("C", "D2"), | |
56 ("D2", "Bb"), | |
57 ], | |
58 ) | |
59 | |
60 def test_parse_pajet_mat(self): | |
61 data = """*Vertices 3\n1 "one"\n2 "two"\n3 "three"\n*Matrix\n1 1 0\n0 1 0\n0 1 0\n""" | |
62 G = nx.parse_pajek(data) | |
63 assert set(G.nodes()) == {"one", "two", "three"} | |
64 assert G.nodes["two"] == {"id": "2"} | |
65 assert_edges_equal( | |
66 set(G.edges()), | |
67 {("one", "one"), ("two", "one"), ("two", "two"), ("two", "three")}, | |
68 ) | |
69 | |
70 def test_read_pajek(self): | |
71 G = nx.parse_pajek(self.data) | |
72 Gin = nx.read_pajek(self.fname) | |
73 assert sorted(G.nodes()) == sorted(Gin.nodes()) | |
74 assert_edges_equal(G.edges(), Gin.edges()) | |
75 assert self.G.graph == Gin.graph | |
76 for n in G: | |
77 assert G.nodes[n] == Gin.nodes[n] | |
78 | |
79 def test_write_pajek(self): | |
80 import io | |
81 | |
82 G = nx.parse_pajek(self.data) | |
83 fh = io.BytesIO() | |
84 nx.write_pajek(G, fh) | |
85 fh.seek(0) | |
86 H = nx.read_pajek(fh) | |
87 assert_nodes_equal(list(G), list(H)) | |
88 assert_edges_equal(list(G.edges()), list(H.edges())) | |
89 # Graph name is left out for now, therefore it is not tested. | |
90 # assert_equal(G.graph, H.graph) | |
91 | |
92 def test_ignored_attribute(self): | |
93 import io | |
94 | |
95 G = nx.Graph() | |
96 fh = io.BytesIO() | |
97 G.add_node(1, int_attr=1) | |
98 G.add_node(2, empty_attr=" ") | |
99 G.add_edge(1, 2, int_attr=2) | |
100 G.add_edge(2, 3, empty_attr=" ") | |
101 | |
102 import warnings | |
103 | |
104 with warnings.catch_warnings(record=True) as w: | |
105 nx.write_pajek(G, fh) | |
106 assert len(w) == 4 | |
107 | |
108 def test_noname(self): | |
109 # Make sure we can parse a line such as: *network | |
110 # Issue #952 | |
111 line = "*network\n" | |
112 other_lines = self.data.split("\n")[1:] | |
113 data = line + "\n".join(other_lines) | |
114 G = nx.parse_pajek(data) | |
115 | |
116 def test_unicode(self): | |
117 import io | |
118 | |
119 G = nx.Graph() | |
120 name1 = chr(2344) + chr(123) + chr(6543) | |
121 name2 = chr(5543) + chr(1543) + chr(324) | |
122 G.add_edge(name1, "Radiohead", foo=name2) | |
123 fh = io.BytesIO() | |
124 nx.write_pajek(G, fh) | |
125 fh.seek(0) | |
126 H = nx.read_pajek(fh) | |
127 assert_nodes_equal(list(G), list(H)) | |
128 assert_edges_equal(list(G.edges()), list(H.edges())) | |
129 assert G.graph == H.graph |