comparison env/lib/python3.9/site-packages/networkx/algorithms/bipartite/tests/test_cluster.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 networkx as nx
2 import pytest
3 from networkx.algorithms.bipartite.cluster import cc_dot, cc_min, cc_max
4 import networkx.algorithms.bipartite as bipartite
5
6
7 def test_pairwise_bipartite_cc_functions():
8 # Test functions for different kinds of bipartite clustering coefficients
9 # between pairs of nodes using 3 example graphs from figure 5 p. 40
10 # Latapy et al (2008)
11 G1 = nx.Graph([(0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (1, 5), (1, 6), (1, 7)])
12 G2 = nx.Graph([(0, 2), (0, 3), (0, 4), (1, 3), (1, 4), (1, 5)])
13 G3 = nx.Graph(
14 [(0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9)]
15 )
16 result = {
17 0: [1 / 3.0, 2 / 3.0, 2 / 5.0],
18 1: [1 / 2.0, 2 / 3.0, 2 / 3.0],
19 2: [2 / 8.0, 2 / 5.0, 2 / 5.0],
20 }
21 for i, G in enumerate([G1, G2, G3]):
22 assert bipartite.is_bipartite(G)
23 assert cc_dot(set(G[0]), set(G[1])) == result[i][0]
24 assert cc_min(set(G[0]), set(G[1])) == result[i][1]
25 assert cc_max(set(G[0]), set(G[1])) == result[i][2]
26
27
28 def test_star_graph():
29 G = nx.star_graph(3)
30 # all modes are the same
31 answer = {0: 0, 1: 1, 2: 1, 3: 1}
32 assert bipartite.clustering(G, mode="dot") == answer
33 assert bipartite.clustering(G, mode="min") == answer
34 assert bipartite.clustering(G, mode="max") == answer
35
36
37 def test_not_bipartite():
38 with pytest.raises(nx.NetworkXError):
39 bipartite.clustering(nx.complete_graph(4))
40
41
42 def test_bad_mode():
43 with pytest.raises(nx.NetworkXError):
44 bipartite.clustering(nx.path_graph(4), mode="foo")
45
46
47 def test_path_graph():
48 G = nx.path_graph(4)
49 answer = {0: 0.5, 1: 0.5, 2: 0.5, 3: 0.5}
50 assert bipartite.clustering(G, mode="dot") == answer
51 assert bipartite.clustering(G, mode="max") == answer
52 answer = {0: 1, 1: 1, 2: 1, 3: 1}
53 assert bipartite.clustering(G, mode="min") == answer
54
55
56 def test_average_path_graph():
57 G = nx.path_graph(4)
58 assert bipartite.average_clustering(G, mode="dot") == 0.5
59 assert bipartite.average_clustering(G, mode="max") == 0.5
60 assert bipartite.average_clustering(G, mode="min") == 1
61
62
63 def test_ra_clustering_davis():
64 G = nx.davis_southern_women_graph()
65 cc4 = round(bipartite.robins_alexander_clustering(G), 3)
66 assert cc4 == 0.468
67
68
69 def test_ra_clustering_square():
70 G = nx.path_graph(4)
71 G.add_edge(0, 3)
72 assert bipartite.robins_alexander_clustering(G) == 1.0
73
74
75 def test_ra_clustering_zero():
76 G = nx.Graph()
77 assert bipartite.robins_alexander_clustering(G) == 0
78 G.add_nodes_from(range(4))
79 assert bipartite.robins_alexander_clustering(G) == 0
80 G.add_edges_from([(0, 1), (2, 3), (3, 4)])
81 assert bipartite.robins_alexander_clustering(G) == 0
82 G.add_edge(1, 2)
83 assert bipartite.robins_alexander_clustering(G) == 0