comparison env/lib/python3.9/site-packages/networkx/algorithms/community/tests/test_label_propagation.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 from itertools import chain
2 from itertools import combinations
3
4 import pytest
5
6 import networkx as nx
7 from networkx.algorithms.community import label_propagation_communities
8 from networkx.algorithms.community import asyn_lpa_communities
9
10
11 def test_directed_not_supported():
12 with pytest.raises(nx.NetworkXNotImplemented):
13 # not supported for directed graphs
14 test = nx.DiGraph()
15 test.add_edge("a", "b")
16 test.add_edge("a", "c")
17 test.add_edge("b", "d")
18 result = label_propagation_communities(test)
19
20
21 def test_one_node():
22 test = nx.Graph()
23 test.add_node("a")
24
25 # The expected communities are:
26 ground_truth = {frozenset(["a"])}
27
28 communities = label_propagation_communities(test)
29 result = {frozenset(c) for c in communities}
30 assert result == ground_truth
31
32
33 def test_unconnected_communities():
34 test = nx.Graph()
35 # community 1
36 test.add_edge("a", "c")
37 test.add_edge("a", "d")
38 test.add_edge("d", "c")
39 # community 2
40 test.add_edge("b", "e")
41 test.add_edge("e", "f")
42 test.add_edge("f", "b")
43
44 # The expected communities are:
45 ground_truth = {frozenset(["a", "c", "d"]), frozenset(["b", "e", "f"])}
46
47 communities = label_propagation_communities(test)
48 result = {frozenset(c) for c in communities}
49 assert result == ground_truth
50
51
52 def test_connected_communities():
53 test = nx.Graph()
54 # community 1
55 test.add_edge("a", "b")
56 test.add_edge("c", "a")
57 test.add_edge("c", "b")
58 test.add_edge("d", "a")
59 test.add_edge("d", "b")
60 test.add_edge("d", "c")
61 test.add_edge("e", "a")
62 test.add_edge("e", "b")
63 test.add_edge("e", "c")
64 test.add_edge("e", "d")
65 # community 2
66 test.add_edge("1", "2")
67 test.add_edge("3", "1")
68 test.add_edge("3", "2")
69 test.add_edge("4", "1")
70 test.add_edge("4", "2")
71 test.add_edge("4", "3")
72 test.add_edge("5", "1")
73 test.add_edge("5", "2")
74 test.add_edge("5", "3")
75 test.add_edge("5", "4")
76 # edge between community 1 and 2
77 test.add_edge("a", "1")
78 # community 3
79 test.add_edge("x", "y")
80 # community 4 with only a single node
81 test.add_node("z")
82
83 # The expected communities are:
84 ground_truth1 = {
85 frozenset(["a", "b", "c", "d", "e"]),
86 frozenset(["1", "2", "3", "4", "5"]),
87 frozenset(["x", "y"]),
88 frozenset(["z"]),
89 }
90 ground_truth2 = {
91 frozenset(["a", "b", "c", "d", "e", "1", "2", "3", "4", "5"]),
92 frozenset(["x", "y"]),
93 frozenset(["z"]),
94 }
95 ground_truth = (ground_truth1, ground_truth2)
96
97 communities = label_propagation_communities(test)
98 result = {frozenset(c) for c in communities}
99 assert result in ground_truth
100
101
102 def test_termination():
103 # ensure termination of asyn_lpa_communities in two cases
104 # that led to an endless loop in a previous version
105 test1 = nx.karate_club_graph()
106 test2 = nx.caveman_graph(2, 10)
107 test2.add_edges_from([(0, 20), (20, 10)])
108 asyn_lpa_communities(test1)
109 asyn_lpa_communities(test2)
110
111
112 class TestAsynLpaCommunities:
113 def _check_communities(self, G, expected):
114 """Checks that the communities computed from the given graph ``G``
115 using the :func:`~networkx.asyn_lpa_communities` function match
116 the set of nodes given in ``expected``.
117
118 ``expected`` must be a :class:`set` of :class:`frozenset`
119 instances, each element of which is a node in the graph.
120
121 """
122 communities = asyn_lpa_communities(G)
123 result = {frozenset(c) for c in communities}
124 assert result == expected
125
126 def test_null_graph(self):
127 G = nx.null_graph()
128 ground_truth = set()
129 self._check_communities(G, ground_truth)
130
131 def test_single_node(self):
132 G = nx.empty_graph(1)
133 ground_truth = {frozenset([0])}
134 self._check_communities(G, ground_truth)
135
136 def test_simple_communities(self):
137 # This graph is the disjoint union of two triangles.
138 G = nx.Graph(["ab", "ac", "bc", "de", "df", "fe"])
139 ground_truth = {frozenset("abc"), frozenset("def")}
140 self._check_communities(G, ground_truth)
141
142 def test_seed_argument(self):
143 G = nx.Graph(["ab", "ac", "bc", "de", "df", "fe"])
144 ground_truth = {frozenset("abc"), frozenset("def")}
145 communities = asyn_lpa_communities(G, seed=1)
146 result = {frozenset(c) for c in communities}
147 assert result == ground_truth
148
149 def test_several_communities(self):
150 # This graph is the disjoint union of five triangles.
151 ground_truth = {frozenset(range(3 * i, 3 * (i + 1))) for i in range(5)}
152 edges = chain.from_iterable(combinations(c, 2) for c in ground_truth)
153 G = nx.Graph(edges)
154 self._check_communities(G, ground_truth)