comparison env/lib/python3.9/site-packages/networkx/algorithms/bipartite/tests/test_project.py @ 0:4f3585e2f14bdraftdefaulttip

author shellac Mon, 22 Mar 2021 18:12:50 +0000
comparison
equal inserted replaced
-1:000000000000 0:4f3585e2f14b
1 import networkx as nx
2 from networkx.algorithms import bipartite
3 from networkx.testing import assert_edges_equal, assert_nodes_equal
4
5
6 class TestBipartiteProject:
7 def test_path_projected_graph(self):
8 G = nx.path_graph(4)
9 P = bipartite.projected_graph(G, [1, 3])
10 assert_nodes_equal(list(P), [1, 3])
11 assert_edges_equal(list(P.edges()), [(1, 3)])
12 P = bipartite.projected_graph(G, [0, 2])
13 assert_nodes_equal(list(P), [0, 2])
14 assert_edges_equal(list(P.edges()), [(0, 2)])
15
16 def test_path_projected_properties_graph(self):
17 G = nx.path_graph(4)
20 P = bipartite.projected_graph(G, [1, 3])
21 assert_nodes_equal(list(P), [1, 3])
22 assert_edges_equal(list(P.edges()), [(1, 3)])
23 assert P.nodes[1]["name"] == G.nodes[1]["name"]
24 P = bipartite.projected_graph(G, [0, 2])
25 assert_nodes_equal(list(P), [0, 2])
26 assert_edges_equal(list(P.edges()), [(0, 2)])
27 assert P.nodes[2]["name"] == G.nodes[2]["name"]
28
29 def test_path_collaboration_projected_graph(self):
30 G = nx.path_graph(4)
31 P = bipartite.collaboration_weighted_projected_graph(G, [1, 3])
32 assert_nodes_equal(list(P), [1, 3])
33 assert_edges_equal(list(P.edges()), [(1, 3)])
34 P[1][3]["weight"] = 1
35 P = bipartite.collaboration_weighted_projected_graph(G, [0, 2])
36 assert_nodes_equal(list(P), [0, 2])
37 assert_edges_equal(list(P.edges()), [(0, 2)])
38 P[0][2]["weight"] = 1
39
40 def test_directed_path_collaboration_projected_graph(self):
41 G = nx.DiGraph()
43 P = bipartite.collaboration_weighted_projected_graph(G, [1, 3])
44 assert_nodes_equal(list(P), [1, 3])
45 assert_edges_equal(list(P.edges()), [(1, 3)])
46 P[1][3]["weight"] = 1
47 P = bipartite.collaboration_weighted_projected_graph(G, [0, 2])
48 assert_nodes_equal(list(P), [0, 2])
49 assert_edges_equal(list(P.edges()), [(0, 2)])
50 P[0][2]["weight"] = 1
51
52 def test_path_weighted_projected_graph(self):
53 G = nx.path_graph(4)
54 P = bipartite.weighted_projected_graph(G, [1, 3])
55 assert_nodes_equal(list(P), [1, 3])
56 assert_edges_equal(list(P.edges()), [(1, 3)])
57 P[1][3]["weight"] = 1
58 P = bipartite.weighted_projected_graph(G, [0, 2])
59 assert_nodes_equal(list(P), [0, 2])
60 assert_edges_equal(list(P.edges()), [(0, 2)])
61 P[0][2]["weight"] = 1
62
63 def test_path_weighted_projected_directed_graph(self):
64 G = nx.DiGraph()
66 P = bipartite.weighted_projected_graph(G, [1, 3])
67 assert_nodes_equal(list(P), [1, 3])
68 assert_edges_equal(list(P.edges()), [(1, 3)])
69 P[1][3]["weight"] = 1
70 P = bipartite.weighted_projected_graph(G, [0, 2])
71 assert_nodes_equal(list(P), [0, 2])
72 assert_edges_equal(list(P.edges()), [(0, 2)])
73 P[0][2]["weight"] = 1
74
75 def test_star_projected_graph(self):
76 G = nx.star_graph(3)
77 P = bipartite.projected_graph(G, [1, 2, 3])
78 assert_nodes_equal(list(P), [1, 2, 3])
79 assert_edges_equal(list(P.edges()), [(1, 2), (1, 3), (2, 3)])
80 P = bipartite.weighted_projected_graph(G, [1, 2, 3])
81 assert_nodes_equal(list(P), [1, 2, 3])
82 assert_edges_equal(list(P.edges()), [(1, 2), (1, 3), (2, 3)])
83
84 P = bipartite.projected_graph(G, [0])
85 assert_nodes_equal(list(P), [0])
86 assert_edges_equal(list(P.edges()), [])
87
88 def test_project_multigraph(self):
89 G = nx.Graph()
94 P = bipartite.projected_graph(G, "ab")
95 assert_edges_equal(list(P.edges()), [("a", "b")])
96 P = bipartite.weighted_projected_graph(G, "ab")
97 assert_edges_equal(list(P.edges()), [("a", "b")])
98 P = bipartite.projected_graph(G, "ab", multigraph=True)
99 assert_edges_equal(list(P.edges()), [("a", "b"), ("a", "b")])
100
101 def test_project_collaboration(self):
102 G = nx.Graph()
110 P = bipartite.collaboration_weighted_projected_graph(G, "abc")
111 assert P["a"]["b"]["weight"] == 1
112 assert P["b"]["c"]["weight"] == 2
113
114 def test_directed_projection(self):
115 G = nx.DiGraph()
120 P = bipartite.projected_graph(G, "AB")
121 assert_edges_equal(list(P.edges()), [("A", "B")])
122 P = bipartite.weighted_projected_graph(G, "AB")
123 assert_edges_equal(list(P.edges()), [("A", "B")])
124 assert P["A"]["B"]["weight"] == 1
125
126 P = bipartite.projected_graph(G, "AB", multigraph=True)
127 assert_edges_equal(list(P.edges()), [("A", "B")])
128
129 G = nx.DiGraph()
134 P = bipartite.projected_graph(G, "AB")
135 assert_edges_equal(list(P.edges()), [("A", "B")])
136 P = bipartite.weighted_projected_graph(G, "AB")
137 assert_edges_equal(list(P.edges()), [("A", "B")])
138 assert P["A"]["B"]["weight"] == 2
139
140 P = bipartite.projected_graph(G, "AB", multigraph=True)
141 assert_edges_equal(list(P.edges()), [("A", "B"), ("A", "B")])
142
143
144 class TestBipartiteWeightedProjection:
145 @classmethod
146 def setup_class(cls):
147 # Tore Opsahl's example
148 # http://toreopsahl.com/2009/05/01/projecting-two-mode-networks-onto-weighted-one-mode-networks/
149 cls.G = nx.Graph()
163 # Graph based on figure 6 from Newman (2001)
164 cls.N = nx.Graph()
174
175 def test_project_weighted_shared(self):
176 edges = [
177 ("A", "B", 2),
178 ("A", "C", 1),
179 ("B", "C", 1),
180 ("B", "D", 1),
181 ("B", "E", 2),
182 ("E", "F", 1),
183 ]
184 Panswer = nx.Graph()
186 P = bipartite.weighted_projected_graph(self.G, "ABCDEF")
188 for u, v in list(P.edges()):
189 assert P[u][v]["weight"] == Panswer[u][v]["weight"]
190
191 edges = [
192 ("A", "B", 3),
193 ("A", "E", 1),
194 ("A", "C", 1),
195 ("A", "D", 1),
196 ("B", "E", 1),
197 ("B", "C", 1),
198 ("B", "D", 1),
199 ("C", "D", 1),
200 ]
201 Panswer = nx.Graph()
203 P = bipartite.weighted_projected_graph(self.N, "ABCDE")
205 for u, v in list(P.edges()):
206 assert P[u][v]["weight"] == Panswer[u][v]["weight"]
207
208 def test_project_weighted_newman(self):
209 edges = [
210 ("A", "B", 1.5),
211 ("A", "C", 0.5),
212 ("B", "C", 0.5),
213 ("B", "D", 1),
214 ("B", "E", 2),
215 ("E", "F", 1),
216 ]
217 Panswer = nx.Graph()
219 P = bipartite.collaboration_weighted_projected_graph(self.G, "ABCDEF")
221 for u, v in list(P.edges()):
222 assert P[u][v]["weight"] == Panswer[u][v]["weight"]
223
224 edges = [
225 ("A", "B", 11 / 6.0),
226 ("A", "E", 1 / 2.0),
227 ("A", "C", 1 / 3.0),
228 ("A", "D", 1 / 3.0),
229 ("B", "E", 1 / 2.0),
230 ("B", "C", 1 / 3.0),
231 ("B", "D", 1 / 3.0),
232 ("C", "D", 1 / 3.0),
233 ]
234 Panswer = nx.Graph()
236 P = bipartite.collaboration_weighted_projected_graph(self.N, "ABCDE")
238 for u, v in list(P.edges()):
239 assert P[u][v]["weight"] == Panswer[u][v]["weight"]
240
241 def test_project_weighted_ratio(self):
242 edges = [
243 ("A", "B", 2 / 6.0),
244 ("A", "C", 1 / 6.0),
245 ("B", "C", 1 / 6.0),
246 ("B", "D", 1 / 6.0),
247 ("B", "E", 2 / 6.0),
248 ("E", "F", 1 / 6.0),
249 ]
250 Panswer = nx.Graph()
252 P = bipartite.weighted_projected_graph(self.G, "ABCDEF", ratio=True)
254 for u, v in list(P.edges()):
255 assert P[u][v]["weight"] == Panswer[u][v]["weight"]
256
257 edges = [
258 ("A", "B", 3 / 3.0),
259 ("A", "E", 1 / 3.0),
260 ("A", "C", 1 / 3.0),
261 ("A", "D", 1 / 3.0),
262 ("B", "E", 1 / 3.0),
263 ("B", "C", 1 / 3.0),
264 ("B", "D", 1 / 3.0),
265 ("C", "D", 1 / 3.0),
266 ]
267 Panswer = nx.Graph()
269 P = bipartite.weighted_projected_graph(self.N, "ABCDE", ratio=True)
271 for u, v in list(P.edges()):
272 assert P[u][v]["weight"] == Panswer[u][v]["weight"]
273
274 def test_project_weighted_overlap(self):
275 edges = [
276 ("A", "B", 2 / 2.0),
277 ("A", "C", 1 / 1.0),
278 ("B", "C", 1 / 1.0),
279 ("B", "D", 1 / 1.0),
280 ("B", "E", 2 / 3.0),
281 ("E", "F", 1 / 1.0),
282 ]
283 Panswer = nx.Graph()
285 P = bipartite.overlap_weighted_projected_graph(self.G, "ABCDEF", jaccard=False)
287 for u, v in list(P.edges()):
288 assert P[u][v]["weight"] == Panswer[u][v]["weight"]
289
290 edges = [
291 ("A", "B", 3 / 3.0),
292 ("A", "E", 1 / 1.0),
293 ("A", "C", 1 / 1.0),
294 ("A", "D", 1 / 1.0),
295 ("B", "E", 1 / 1.0),
296 ("B", "C", 1 / 1.0),
297 ("B", "D", 1 / 1.0),
298 ("C", "D", 1 / 1.0),
299 ]
300 Panswer = nx.Graph()
302 P = bipartite.overlap_weighted_projected_graph(self.N, "ABCDE", jaccard=False)
304 for u, v in list(P.edges()):
305 assert P[u][v]["weight"] == Panswer[u][v]["weight"]
306
307 def test_project_weighted_jaccard(self):
308 edges = [
309 ("A", "B", 2 / 5.0),
310 ("A", "C", 1 / 2.0),
311 ("B", "C", 1 / 5.0),
312 ("B", "D", 1 / 5.0),
313 ("B", "E", 2 / 6.0),
314 ("E", "F", 1 / 3.0),
315 ]
316 Panswer = nx.Graph()
318 P = bipartite.overlap_weighted_projected_graph(self.G, "ABCDEF")
320 for u, v in list(P.edges()):
321 assert P[u][v]["weight"] == Panswer[u][v]["weight"]
322
323 edges = [
324 ("A", "B", 3 / 3.0),
325 ("A", "E", 1 / 3.0),
326 ("A", "C", 1 / 3.0),
327 ("A", "D", 1 / 3.0),
328 ("B", "E", 1 / 3.0),
329 ("B", "C", 1 / 3.0),
330 ("B", "D", 1 / 3.0),
331 ("C", "D", 1 / 1.0),
332 ]
333 Panswer = nx.Graph()
335 P = bipartite.overlap_weighted_projected_graph(self.N, "ABCDE")
337 for u, v in P.edges():
338 assert P[u][v]["weight"] == Panswer[u][v]["weight"]
339
340 def test_generic_weighted_projected_graph_simple(self):
341 def shared(G, u, v):
342 return len(set(G[u]) & set(G[v]))
343
344 B = nx.path_graph(5)
345 G = bipartite.generic_weighted_projected_graph(
346 B, [0, 2, 4], weight_function=shared
347 )
348 assert_nodes_equal(list(G), [0, 2, 4])
349 assert_edges_equal(
350 list(list(G.edges(data=True))),
351 [(0, 2, {"weight": 1}), (2, 4, {"weight": 1})],
352 )
353
354 G = bipartite.generic_weighted_projected_graph(B, [0, 2, 4])
355 assert_nodes_equal(list(G), [0, 2, 4])
356 assert_edges_equal(
357 list(list(G.edges(data=True))),
358 [(0, 2, {"weight": 1}), (2, 4, {"weight": 1})],
359 )
360 B = nx.DiGraph()
362 G = bipartite.generic_weighted_projected_graph(B, [0, 2, 4])
363 assert_nodes_equal(list(G), [0, 2, 4])
364 assert_edges_equal(
365 list(G.edges(data=True)), [(0, 2, {"weight": 1}), (2, 4, {"weight": 1})]
366 )
367
368 def test_generic_weighted_projected_graph_custom(self):
369 def jaccard(G, u, v):
370 unbrs = set(G[u])
371 vnbrs = set(G[v])
372 return float(len(unbrs & vnbrs)) / len(unbrs | vnbrs)
373
374 def my_weight(G, u, v, weight="weight"):
375 w = 0
376 for nbr in set(G[u]) & set(G[v]):
377 w += G.edges[u, nbr].get(weight, 1) + G.edges[v, nbr].get(weight, 1)
378 return w
379
380 B = nx.bipartite.complete_bipartite_graph(2, 2)
381 for i, (u, v) in enumerate(B.edges()):
382 B.edges[u, v]["weight"] = i + 1
383 G = bipartite.generic_weighted_projected_graph(
384 B, [0, 1], weight_function=jaccard
385 )
386 assert_edges_equal(list(G.edges(data=True)), [(0, 1, {"weight": 1.0})])
387 G = bipartite.generic_weighted_projected_graph(
388 B, [0, 1], weight_function=my_weight
389 )
390 assert_edges_equal(list(G.edges(data=True)), [(0, 1, {"weight": 10})])
391 G = bipartite.generic_weighted_projected_graph(B, [0, 1])
392 assert_edges_equal(list(G.edges(data=True)), [(0, 1, {"weight": 2})])