Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/tests/test_convert_pandas.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_nodes_equal | |
4 from networkx.testing import assert_edges_equal | |
5 from networkx.testing import assert_graphs_equal | |
6 | |
7 np = pytest.importorskip("numpy") | |
8 pd = pytest.importorskip("pandas") | |
9 | |
10 | |
11 class TestConvertPandas: | |
12 def setup_method(self): | |
13 self.rng = np.random.RandomState(seed=5) | |
14 ints = self.rng.randint(1, 11, size=(3, 2)) | |
15 a = ["A", "B", "C"] | |
16 b = ["D", "A", "E"] | |
17 df = pd.DataFrame(ints, columns=["weight", "cost"]) | |
18 df[0] = a # Column label 0 (int) | |
19 df["b"] = b # Column label 'b' (str) | |
20 self.df = df | |
21 | |
22 mdf = pd.DataFrame([[4, 16, "A", "D"]], columns=["weight", "cost", 0, "b"]) | |
23 self.mdf = df.append(mdf) | |
24 | |
25 def test_exceptions(self): | |
26 G = pd.DataFrame(["a"]) # adj | |
27 pytest.raises(nx.NetworkXError, nx.to_networkx_graph, G) | |
28 G = pd.DataFrame(["a", 0.0]) # elist | |
29 pytest.raises(nx.NetworkXError, nx.to_networkx_graph, G) | |
30 df = pd.DataFrame([[1, 1], [1, 0]], dtype=int, index=[1, 2], columns=["a", "b"]) | |
31 pytest.raises(nx.NetworkXError, nx.from_pandas_adjacency, df) | |
32 | |
33 def test_from_edgelist_all_attr(self): | |
34 Gtrue = nx.Graph( | |
35 [ | |
36 ("E", "C", {"cost": 9, "weight": 10}), | |
37 ("B", "A", {"cost": 1, "weight": 7}), | |
38 ("A", "D", {"cost": 7, "weight": 4}), | |
39 ] | |
40 ) | |
41 G = nx.from_pandas_edgelist(self.df, 0, "b", True) | |
42 assert_graphs_equal(G, Gtrue) | |
43 # MultiGraph | |
44 MGtrue = nx.MultiGraph(Gtrue) | |
45 MGtrue.add_edge("A", "D", cost=16, weight=4) | |
46 MG = nx.from_pandas_edgelist(self.mdf, 0, "b", True, nx.MultiGraph()) | |
47 assert_graphs_equal(MG, MGtrue) | |
48 | |
49 def test_from_edgelist_multi_attr(self): | |
50 Gtrue = nx.Graph( | |
51 [ | |
52 ("E", "C", {"cost": 9, "weight": 10}), | |
53 ("B", "A", {"cost": 1, "weight": 7}), | |
54 ("A", "D", {"cost": 7, "weight": 4}), | |
55 ] | |
56 ) | |
57 G = nx.from_pandas_edgelist(self.df, 0, "b", ["weight", "cost"]) | |
58 assert_graphs_equal(G, Gtrue) | |
59 | |
60 def test_from_edgelist_multi_attr_incl_target(self): | |
61 Gtrue = nx.Graph( | |
62 [ | |
63 ("E", "C", {0: "C", "b": "E", "weight": 10}), | |
64 ("B", "A", {0: "B", "b": "A", "weight": 7}), | |
65 ("A", "D", {0: "A", "b": "D", "weight": 4}), | |
66 ] | |
67 ) | |
68 G = nx.from_pandas_edgelist(self.df, 0, "b", [0, "b", "weight"]) | |
69 assert_graphs_equal(G, Gtrue) | |
70 | |
71 def test_from_edgelist_multidigraph_and_edge_attr(self): | |
72 # example from issue #2374 | |
73 edges = [ | |
74 ("X1", "X4", {"Co": "zA", "Mi": 0, "St": "X1"}), | |
75 ("X1", "X4", {"Co": "zB", "Mi": 54, "St": "X2"}), | |
76 ("X1", "X4", {"Co": "zB", "Mi": 49, "St": "X3"}), | |
77 ("X1", "X4", {"Co": "zB", "Mi": 44, "St": "X4"}), | |
78 ("Y1", "Y3", {"Co": "zC", "Mi": 0, "St": "Y1"}), | |
79 ("Y1", "Y3", {"Co": "zC", "Mi": 34, "St": "Y2"}), | |
80 ("Y1", "Y3", {"Co": "zC", "Mi": 29, "St": "X2"}), | |
81 ("Y1", "Y3", {"Co": "zC", "Mi": 24, "St": "Y3"}), | |
82 ("Z1", "Z3", {"Co": "zD", "Mi": 0, "St": "Z1"}), | |
83 ("Z1", "Z3", {"Co": "zD", "Mi": 14, "St": "X3"}), | |
84 ] | |
85 Gtrue = nx.MultiDiGraph(edges) | |
86 data = { | |
87 "O": ["X1", "X1", "X1", "X1", "Y1", "Y1", "Y1", "Y1", "Z1", "Z1"], | |
88 "D": ["X4", "X4", "X4", "X4", "Y3", "Y3", "Y3", "Y3", "Z3", "Z3"], | |
89 "St": ["X1", "X2", "X3", "X4", "Y1", "Y2", "X2", "Y3", "Z1", "X3"], | |
90 "Co": ["zA", "zB", "zB", "zB", "zC", "zC", "zC", "zC", "zD", "zD"], | |
91 "Mi": [0, 54, 49, 44, 0, 34, 29, 24, 0, 14], | |
92 } | |
93 df = pd.DataFrame.from_dict(data) | |
94 G1 = nx.from_pandas_edgelist( | |
95 df, source="O", target="D", edge_attr=True, create_using=nx.MultiDiGraph | |
96 ) | |
97 G2 = nx.from_pandas_edgelist( | |
98 df, | |
99 source="O", | |
100 target="D", | |
101 edge_attr=["St", "Co", "Mi"], | |
102 create_using=nx.MultiDiGraph, | |
103 ) | |
104 assert_graphs_equal(G1, Gtrue) | |
105 assert_graphs_equal(G2, Gtrue) | |
106 | |
107 def test_from_edgelist_one_attr(self): | |
108 Gtrue = nx.Graph( | |
109 [ | |
110 ("E", "C", {"weight": 10}), | |
111 ("B", "A", {"weight": 7}), | |
112 ("A", "D", {"weight": 4}), | |
113 ] | |
114 ) | |
115 G = nx.from_pandas_edgelist(self.df, 0, "b", "weight") | |
116 assert_graphs_equal(G, Gtrue) | |
117 | |
118 def test_from_edgelist_int_attr_name(self): | |
119 # note: this also tests that edge_attr can be `source` | |
120 Gtrue = nx.Graph( | |
121 [("E", "C", {0: "C"}), ("B", "A", {0: "B"}), ("A", "D", {0: "A"})] | |
122 ) | |
123 G = nx.from_pandas_edgelist(self.df, 0, "b", 0) | |
124 assert_graphs_equal(G, Gtrue) | |
125 | |
126 def test_from_edgelist_invalid_attr(self): | |
127 pytest.raises( | |
128 nx.NetworkXError, nx.from_pandas_edgelist, self.df, 0, "b", "misspell" | |
129 ) | |
130 pytest.raises(nx.NetworkXError, nx.from_pandas_edgelist, self.df, 0, "b", 1) | |
131 # see Issue #3562 | |
132 edgeframe = pd.DataFrame([[0, 1], [1, 2], [2, 0]], columns=["s", "t"]) | |
133 pytest.raises( | |
134 nx.NetworkXError, nx.from_pandas_edgelist, edgeframe, "s", "t", True | |
135 ) | |
136 pytest.raises( | |
137 nx.NetworkXError, nx.from_pandas_edgelist, edgeframe, "s", "t", "weight" | |
138 ) | |
139 pytest.raises( | |
140 nx.NetworkXError, | |
141 nx.from_pandas_edgelist, | |
142 edgeframe, | |
143 "s", | |
144 "t", | |
145 ["weight", "size"], | |
146 ) | |
147 | |
148 def test_from_edgelist_no_attr(self): | |
149 Gtrue = nx.Graph([("E", "C", {}), ("B", "A", {}), ("A", "D", {})]) | |
150 G = nx.from_pandas_edgelist(self.df, 0, "b") | |
151 assert_graphs_equal(G, Gtrue) | |
152 | |
153 def test_from_edgelist(self): | |
154 # Pandas DataFrame | |
155 G = nx.cycle_graph(10) | |
156 G.add_weighted_edges_from((u, v, u) for u, v in list(G.edges)) | |
157 | |
158 edgelist = nx.to_edgelist(G) | |
159 source = [s for s, t, d in edgelist] | |
160 target = [t for s, t, d in edgelist] | |
161 weight = [d["weight"] for s, t, d in edgelist] | |
162 edges = pd.DataFrame({"source": source, "target": target, "weight": weight}) | |
163 | |
164 GG = nx.from_pandas_edgelist(edges, edge_attr="weight") | |
165 assert_nodes_equal(G.nodes(), GG.nodes()) | |
166 assert_edges_equal(G.edges(), GG.edges()) | |
167 GW = nx.to_networkx_graph(edges, create_using=nx.Graph) | |
168 assert_nodes_equal(G.nodes(), GW.nodes()) | |
169 assert_edges_equal(G.edges(), GW.edges()) | |
170 | |
171 def test_to_edgelist_default_source_or_target_col_exists(self): | |
172 | |
173 G = nx.path_graph(10) | |
174 G.add_weighted_edges_from((u, v, u) for u, v in list(G.edges)) | |
175 nx.set_edge_attributes(G, 0, name="source") | |
176 pytest.raises(nx.NetworkXError, nx.to_pandas_edgelist, G) | |
177 | |
178 # drop source column to test an exception raised for the target column | |
179 for u, v, d in G.edges(data=True): | |
180 d.pop("source", None) | |
181 | |
182 nx.set_edge_attributes(G, 0, name="target") | |
183 pytest.raises(nx.NetworkXError, nx.to_pandas_edgelist, G) | |
184 | |
185 def test_to_edgelist_custom_source_or_target_col_exists(self): | |
186 | |
187 G = nx.path_graph(10) | |
188 G.add_weighted_edges_from((u, v, u) for u, v in list(G.edges)) | |
189 nx.set_edge_attributes(G, 0, name="source_col_name") | |
190 pytest.raises( | |
191 nx.NetworkXError, nx.to_pandas_edgelist, G, source="source_col_name" | |
192 ) | |
193 | |
194 # drop source column to test an exception raised for the target column | |
195 for u, v, d in G.edges(data=True): | |
196 d.pop("source_col_name", None) | |
197 | |
198 nx.set_edge_attributes(G, 0, name="target_col_name") | |
199 pytest.raises( | |
200 nx.NetworkXError, nx.to_pandas_edgelist, G, target="target_col_name" | |
201 ) | |
202 | |
203 def test_from_adjacency(self): | |
204 nodelist = [1, 2] | |
205 dftrue = pd.DataFrame( | |
206 [[1, 1], [1, 0]], dtype=int, index=nodelist, columns=nodelist | |
207 ) | |
208 G = nx.Graph([(1, 1), (1, 2)]) | |
209 df = nx.to_pandas_adjacency(G, dtype=int) | |
210 pd.testing.assert_frame_equal(df, dftrue) | |
211 | |
212 def test_roundtrip(self): | |
213 # edgelist | |
214 Gtrue = nx.Graph([(1, 1), (1, 2)]) | |
215 df = nx.to_pandas_edgelist(Gtrue) | |
216 G = nx.from_pandas_edgelist(df) | |
217 assert_graphs_equal(Gtrue, G) | |
218 # adjacency | |
219 adj = {1: {1: {"weight": 1}, 2: {"weight": 1}}, 2: {1: {"weight": 1}}} | |
220 Gtrue = nx.Graph(adj) | |
221 df = nx.to_pandas_adjacency(Gtrue, dtype=int) | |
222 G = nx.from_pandas_adjacency(df) | |
223 assert_graphs_equal(Gtrue, G) | |
224 | |
225 def test_from_adjacency_named(self): | |
226 # example from issue #3105 | |
227 data = { | |
228 "A": {"A": 0, "B": 0, "C": 0}, | |
229 "B": {"A": 1, "B": 0, "C": 0}, | |
230 "C": {"A": 0, "B": 1, "C": 0}, | |
231 } | |
232 dftrue = pd.DataFrame(data) | |
233 df = dftrue[["A", "C", "B"]] | |
234 G = nx.from_pandas_adjacency(df, create_using=nx.DiGraph()) | |
235 df = nx.to_pandas_adjacency(G, dtype=np.intp) | |
236 pd.testing.assert_frame_equal(df, dftrue) | |
237 | |
238 def test_edgekey_with_multigraph(self): | |
239 df = pd.DataFrame( | |
240 { | |
241 "attr1": {"A": "F1", "B": "F2", "C": "F3"}, | |
242 "attr2": {"A": 1, "B": 0, "C": 0}, | |
243 "attr3": {"A": 0, "B": 1, "C": 0}, | |
244 "source": {"A": "N1", "B": "N2", "C": "N1"}, | |
245 "target": {"A": "N2", "B": "N3", "C": "N1"}, | |
246 } | |
247 ) | |
248 Gtrue = nx.Graph( | |
249 [ | |
250 ("N1", "N2", {"F1": {"attr2": 1, "attr3": 0}}), | |
251 ("N2", "N3", {"F2": {"attr2": 0, "attr3": 1}}), | |
252 ("N1", "N1", {"F3": {"attr2": 0, "attr3": 0}}), | |
253 ] | |
254 ) | |
255 # example from issue #4065 | |
256 G = nx.from_pandas_edgelist( | |
257 df, | |
258 source="source", | |
259 target="target", | |
260 edge_attr=["attr2", "attr3"], | |
261 edge_key="attr1", | |
262 create_using=nx.MultiGraph(), | |
263 ) | |
264 assert_graphs_equal(G, Gtrue) | |
265 | |
266 def test_edgekey_with_normal_graph_no_action(self): | |
267 Gtrue = nx.Graph( | |
268 [ | |
269 ("E", "C", {"cost": 9, "weight": 10}), | |
270 ("B", "A", {"cost": 1, "weight": 7}), | |
271 ("A", "D", {"cost": 7, "weight": 4}), | |
272 ] | |
273 ) | |
274 G = nx.from_pandas_edgelist(self.df, 0, "b", True, edge_key="weight") | |
275 assert_graphs_equal(G, Gtrue) | |
276 | |
277 def test_nonexisting_edgekey_raises(self): | |
278 with pytest.raises(nx.exception.NetworkXError): | |
279 nx.from_pandas_edgelist( | |
280 self.df, | |
281 source="source", | |
282 target="target", | |
283 edge_key="Not_real", | |
284 edge_attr=True, | |
285 create_using=nx.MultiGraph(), | |
286 ) |