comparison env/lib/python3.9/site-packages/networkx/algorithms/community/tests/test_kclique.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 combinations
2
3 import pytest
4
5 import networkx as nx
6 from networkx.algorithms.community import k_clique_communities
7
8
9 def test_overlapping_K5():
10 G = nx.Graph()
11 G.add_edges_from(combinations(range(5), 2)) # Add a five clique
12 G.add_edges_from(combinations(range(2, 7), 2)) # Add another five clique
13 c = list(k_clique_communities(G, 4))
14 assert c == [frozenset(range(7))]
15 c = set(k_clique_communities(G, 5))
16 assert c == {frozenset(range(5)), frozenset(range(2, 7))}
17
18
19 def test_isolated_K5():
20 G = nx.Graph()
21 G.add_edges_from(combinations(range(0, 5), 2)) # Add a five clique
22 G.add_edges_from(combinations(range(5, 10), 2)) # Add another five clique
23 c = set(k_clique_communities(G, 5))
24 assert c == {frozenset(range(5)), frozenset(range(5, 10))}
25
26
27 class TestZacharyKarateClub:
28 def setup(self):
29 self.G = nx.karate_club_graph()
30
31 def _check_communities(self, k, expected):
32 communities = set(k_clique_communities(self.G, k))
33 assert communities == expected
34
35 def test_k2(self):
36 # clique percolation with k=2 is just connected components
37 expected = {frozenset(self.G)}
38 self._check_communities(2, expected)
39
40 def test_k3(self):
41 comm1 = [
42 0,
43 1,
44 2,
45 3,
46 7,
47 8,
48 12,
49 13,
50 14,
51 15,
52 17,
53 18,
54 19,
55 20,
56 21,
57 22,
58 23,
59 26,
60 27,
61 28,
62 29,
63 30,
64 31,
65 32,
66 33,
67 ]
68 comm2 = [0, 4, 5, 6, 10, 16]
69 comm3 = [24, 25, 31]
70 expected = {frozenset(comm1), frozenset(comm2), frozenset(comm3)}
71 self._check_communities(3, expected)
72
73 def test_k4(self):
74 expected = {
75 frozenset([0, 1, 2, 3, 7, 13]),
76 frozenset([8, 32, 30, 33]),
77 frozenset([32, 33, 29, 23]),
78 }
79 self._check_communities(4, expected)
80
81 def test_k5(self):
82 expected = {frozenset([0, 1, 2, 3, 7, 13])}
83 self._check_communities(5, expected)
84
85 def test_k6(self):
86 expected = set()
87 self._check_communities(6, expected)
88
89
90 def test_bad_k():
91 with pytest.raises(nx.NetworkXError):
92 list(k_clique_communities(nx.Graph(), 1))