ADAPT-Chase commited on
Commit
86f8afb
·
verified ·
1 Parent(s): 4f01ca3

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +1 -0
  2. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/coloring/__pycache__/greedy_coloring.cpython-312.pyc +0 -0
  3. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/coloring/tests/__pycache__/__init__.cpython-312.pyc +0 -0
  4. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/coloring/tests/__pycache__/test_coloring.cpython-312.pyc +0 -0
  5. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/__init__.cpython-312.pyc +0 -0
  6. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/asyn_fluid.cpython-312.pyc +0 -0
  7. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/centrality.cpython-312.pyc +0 -0
  8. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/community_utils.cpython-312.pyc +0 -0
  9. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/divisive.cpython-312.pyc +0 -0
  10. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/kclique.cpython-312.pyc +0 -0
  11. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/kernighan_lin.cpython-312.pyc +0 -0
  12. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/label_propagation.cpython-312.pyc +0 -0
  13. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/leiden.cpython-312.pyc +0 -0
  14. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/local.cpython-312.pyc +0 -0
  15. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/louvain.cpython-312.pyc +0 -0
  16. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/lukes.cpython-312.pyc +0 -0
  17. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/modularity_max.cpython-312.pyc +0 -0
  18. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/quality.cpython-312.pyc +0 -0
  19. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__init__.py +0 -0
  20. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/__init__.cpython-312.pyc +0 -0
  21. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_asyn_fluid.cpython-312.pyc +0 -0
  22. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_centrality.cpython-312.pyc +0 -0
  23. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_divisive.cpython-312.pyc +0 -0
  24. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_kclique.cpython-312.pyc +0 -0
  25. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_kernighan_lin.cpython-312.pyc +0 -0
  26. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_label_propagation.cpython-312.pyc +0 -0
  27. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_leiden.cpython-312.pyc +0 -0
  28. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_local.cpython-312.pyc +0 -0
  29. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_louvain.cpython-312.pyc +0 -0
  30. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_lukes.cpython-312.pyc +0 -0
  31. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_modularity_max.cpython-312.pyc +0 -0
  32. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_quality.cpython-312.pyc +0 -0
  33. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_utils.cpython-312.pyc +0 -0
  34. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_asyn_fluid.py +136 -0
  35. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_centrality.py +85 -0
  36. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_divisive.py +106 -0
  37. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_kclique.py +91 -0
  38. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_kernighan_lin.py +92 -0
  39. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_label_propagation.py +241 -0
  40. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_leiden.py +138 -0
  41. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_local.py +76 -0
  42. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_louvain.py +264 -0
  43. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_lukes.py +152 -0
  44. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_modularity_max.py +340 -0
  45. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_quality.py +139 -0
  46. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_utils.py +26 -0
  47. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/components/__pycache__/__init__.cpython-312.pyc +0 -0
  48. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/components/__pycache__/attracting.cpython-312.pyc +0 -0
  49. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/components/__pycache__/biconnected.cpython-312.pyc +0 -0
  50. tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/components/__pycache__/connected.cpython-312.pyc +0 -0
.gitattributes CHANGED
@@ -4024,3 +4024,4 @@ tool_server/.venv/lib/python3.12/site-packages/setuptools/config/_validate_pypro
4024
  tool_server/.venv/lib/python3.12/site-packages/regex/__pycache__/_regex_core.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
4025
  tool_server/.venv/lib/python3.12/site-packages/regex/__pycache__/test_regex.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
4026
  tool_server/.venv/lib/python3.12/site-packages/psutil/tests/__pycache__/test_linux.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
 
 
4024
  tool_server/.venv/lib/python3.12/site-packages/regex/__pycache__/_regex_core.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
4025
  tool_server/.venv/lib/python3.12/site-packages/regex/__pycache__/test_regex.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
4026
  tool_server/.venv/lib/python3.12/site-packages/psutil/tests/__pycache__/test_linux.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
4027
+ tool_server/.venv/lib/python3.12/site-packages/networkx/drawing/__pycache__/nx_pylab.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/coloring/__pycache__/greedy_coloring.cpython-312.pyc ADDED
Binary file (22.3 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/coloring/tests/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (204 Bytes). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/coloring/tests/__pycache__/test_coloring.cpython-312.pyc ADDED
Binary file (28.2 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (1.5 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/asyn_fluid.cpython-312.pyc ADDED
Binary file (5.63 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/centrality.cpython-312.pyc ADDED
Binary file (7.1 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/community_utils.cpython-312.pyc ADDED
Binary file (1.5 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/divisive.cpython-312.pyc ADDED
Binary file (7.64 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/kclique.cpython-312.pyc ADDED
Binary file (3.14 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/kernighan_lin.cpython-312.pyc ADDED
Binary file (6.76 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/label_propagation.cpython-312.pyc ADDED
Binary file (13.4 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/leiden.cpython-312.pyc ADDED
Binary file (7.51 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/local.cpython-312.pyc ADDED
Binary file (8.36 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/louvain.cpython-312.pyc ADDED
Binary file (17.3 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/lukes.cpython-312.pyc ADDED
Binary file (10.6 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/modularity_max.cpython-312.pyc ADDED
Binary file (17.2 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/__pycache__/quality.cpython-312.pyc ADDED
Binary file (14 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__init__.py ADDED
File without changes
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (205 Bytes). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_asyn_fluid.cpython-312.pyc ADDED
Binary file (5.89 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_centrality.cpython-312.pyc ADDED
Binary file (5.31 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_divisive.cpython-312.pyc ADDED
Binary file (7.77 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_kclique.cpython-312.pyc ADDED
Binary file (4.73 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_kernighan_lin.cpython-312.pyc ADDED
Binary file (4.75 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_label_propagation.cpython-312.pyc ADDED
Binary file (15.6 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_leiden.cpython-312.pyc ADDED
Binary file (8.58 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_local.cpython-312.pyc ADDED
Binary file (3.54 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_louvain.cpython-312.pyc ADDED
Binary file (12.6 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_lukes.cpython-312.pyc ADDED
Binary file (6.29 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_modularity_max.cpython-312.pyc ADDED
Binary file (12.6 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_quality.cpython-312.pyc ADDED
Binary file (8.41 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/__pycache__/test_utils.cpython-312.pyc ADDED
Binary file (1.87 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_asyn_fluid.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ import networkx as nx
4
+ from networkx import Graph, NetworkXError
5
+ from networkx.algorithms.community import asyn_fluidc
6
+
7
+
8
+ @pytest.mark.parametrize("graph_constructor", (nx.DiGraph, nx.MultiGraph))
9
+ def test_raises_on_directed_and_multigraphs(graph_constructor):
10
+ G = graph_constructor([(0, 1), (1, 2)])
11
+ with pytest.raises(nx.NetworkXNotImplemented):
12
+ nx.community.asyn_fluidc(G, 1)
13
+
14
+
15
+ def test_exceptions():
16
+ test = Graph()
17
+ test.add_node("a")
18
+ pytest.raises(NetworkXError, asyn_fluidc, test, "hi")
19
+ pytest.raises(NetworkXError, asyn_fluidc, test, -1)
20
+ pytest.raises(NetworkXError, asyn_fluidc, test, 3)
21
+ test.add_node("b")
22
+ pytest.raises(NetworkXError, asyn_fluidc, test, 1)
23
+
24
+
25
+ def test_single_node():
26
+ test = Graph()
27
+
28
+ test.add_node("a")
29
+
30
+ # ground truth
31
+ ground_truth = {frozenset(["a"])}
32
+
33
+ communities = asyn_fluidc(test, 1)
34
+ result = {frozenset(c) for c in communities}
35
+ assert result == ground_truth
36
+
37
+
38
+ def test_two_nodes():
39
+ test = Graph()
40
+
41
+ test.add_edge("a", "b")
42
+
43
+ # ground truth
44
+ ground_truth = {frozenset(["a"]), frozenset(["b"])}
45
+
46
+ communities = asyn_fluidc(test, 2)
47
+ result = {frozenset(c) for c in communities}
48
+ assert result == ground_truth
49
+
50
+
51
+ def test_two_clique_communities():
52
+ test = Graph()
53
+
54
+ # c1
55
+ test.add_edge("a", "b")
56
+ test.add_edge("a", "c")
57
+ test.add_edge("b", "c")
58
+
59
+ # connection
60
+ test.add_edge("c", "d")
61
+
62
+ # c2
63
+ test.add_edge("d", "e")
64
+ test.add_edge("d", "f")
65
+ test.add_edge("f", "e")
66
+
67
+ # ground truth
68
+ ground_truth = {frozenset(["a", "c", "b"]), frozenset(["e", "d", "f"])}
69
+
70
+ communities = asyn_fluidc(test, 2, seed=7)
71
+ result = {frozenset(c) for c in communities}
72
+ assert result == ground_truth
73
+
74
+
75
+ def test_five_clique_ring():
76
+ test = Graph()
77
+
78
+ # c1
79
+ test.add_edge("1a", "1b")
80
+ test.add_edge("1a", "1c")
81
+ test.add_edge("1a", "1d")
82
+ test.add_edge("1b", "1c")
83
+ test.add_edge("1b", "1d")
84
+ test.add_edge("1c", "1d")
85
+
86
+ # c2
87
+ test.add_edge("2a", "2b")
88
+ test.add_edge("2a", "2c")
89
+ test.add_edge("2a", "2d")
90
+ test.add_edge("2b", "2c")
91
+ test.add_edge("2b", "2d")
92
+ test.add_edge("2c", "2d")
93
+
94
+ # c3
95
+ test.add_edge("3a", "3b")
96
+ test.add_edge("3a", "3c")
97
+ test.add_edge("3a", "3d")
98
+ test.add_edge("3b", "3c")
99
+ test.add_edge("3b", "3d")
100
+ test.add_edge("3c", "3d")
101
+
102
+ # c4
103
+ test.add_edge("4a", "4b")
104
+ test.add_edge("4a", "4c")
105
+ test.add_edge("4a", "4d")
106
+ test.add_edge("4b", "4c")
107
+ test.add_edge("4b", "4d")
108
+ test.add_edge("4c", "4d")
109
+
110
+ # c5
111
+ test.add_edge("5a", "5b")
112
+ test.add_edge("5a", "5c")
113
+ test.add_edge("5a", "5d")
114
+ test.add_edge("5b", "5c")
115
+ test.add_edge("5b", "5d")
116
+ test.add_edge("5c", "5d")
117
+
118
+ # connections
119
+ test.add_edge("1a", "2c")
120
+ test.add_edge("2a", "3c")
121
+ test.add_edge("3a", "4c")
122
+ test.add_edge("4a", "5c")
123
+ test.add_edge("5a", "1c")
124
+
125
+ # ground truth
126
+ ground_truth = {
127
+ frozenset(["1a", "1b", "1c", "1d"]),
128
+ frozenset(["2a", "2b", "2c", "2d"]),
129
+ frozenset(["3a", "3b", "3c", "3d"]),
130
+ frozenset(["4a", "4b", "4c", "4d"]),
131
+ frozenset(["5a", "5b", "5c", "5d"]),
132
+ }
133
+
134
+ communities = asyn_fluidc(test, 5, seed=9)
135
+ result = {frozenset(c) for c in communities}
136
+ assert result == ground_truth
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_centrality.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Unit tests for the :mod:`networkx.algorithms.community.centrality`
2
+ module.
3
+
4
+ """
5
+
6
+ from operator import itemgetter
7
+
8
+ import networkx as nx
9
+
10
+
11
+ def set_of_sets(iterable):
12
+ return set(map(frozenset, iterable))
13
+
14
+
15
+ def validate_communities(result, expected):
16
+ assert set_of_sets(result) == set_of_sets(expected)
17
+
18
+
19
+ def validate_possible_communities(result, *expected):
20
+ assert any(set_of_sets(result) == set_of_sets(p) for p in expected)
21
+
22
+
23
+ class TestGirvanNewman:
24
+ """Unit tests for the
25
+ :func:`networkx.algorithms.community.centrality.girvan_newman`
26
+ function.
27
+
28
+ """
29
+
30
+ def test_no_edges(self):
31
+ G = nx.empty_graph(3)
32
+ communities = list(nx.community.girvan_newman(G))
33
+ assert len(communities) == 1
34
+ validate_communities(communities[0], [{0}, {1}, {2}])
35
+
36
+ def test_undirected(self):
37
+ # Start with the graph .-.-.-.
38
+ G = nx.path_graph(4)
39
+ communities = list(nx.community.girvan_newman(G))
40
+ assert len(communities) == 3
41
+ # After one removal, we get the graph .-. .-.
42
+ validate_communities(communities[0], [{0, 1}, {2, 3}])
43
+ # After the next, we get the graph .-. . ., but there are two
44
+ # symmetric possible versions.
45
+ validate_possible_communities(
46
+ communities[1], [{0}, {1}, {2, 3}], [{0, 1}, {2}, {3}]
47
+ )
48
+ # After the last removal, we always get the empty graph.
49
+ validate_communities(communities[2], [{0}, {1}, {2}, {3}])
50
+
51
+ def test_directed(self):
52
+ G = nx.DiGraph(nx.path_graph(4))
53
+ communities = list(nx.community.girvan_newman(G))
54
+ assert len(communities) == 3
55
+ validate_communities(communities[0], [{0, 1}, {2, 3}])
56
+ validate_possible_communities(
57
+ communities[1], [{0}, {1}, {2, 3}], [{0, 1}, {2}, {3}]
58
+ )
59
+ validate_communities(communities[2], [{0}, {1}, {2}, {3}])
60
+
61
+ def test_selfloops(self):
62
+ G = nx.path_graph(4)
63
+ G.add_edge(0, 0)
64
+ G.add_edge(2, 2)
65
+ communities = list(nx.community.girvan_newman(G))
66
+ assert len(communities) == 3
67
+ validate_communities(communities[0], [{0, 1}, {2, 3}])
68
+ validate_possible_communities(
69
+ communities[1], [{0}, {1}, {2, 3}], [{0, 1}, {2}, {3}]
70
+ )
71
+ validate_communities(communities[2], [{0}, {1}, {2}, {3}])
72
+
73
+ def test_most_valuable_edge(self):
74
+ G = nx.Graph()
75
+ G.add_weighted_edges_from([(0, 1, 3), (1, 2, 2), (2, 3, 1)])
76
+ # Let the most valuable edge be the one with the highest weight.
77
+
78
+ def heaviest(G):
79
+ return max(G.edges(data="weight"), key=itemgetter(2))[:2]
80
+
81
+ communities = list(nx.community.girvan_newman(G, heaviest))
82
+ assert len(communities) == 3
83
+ validate_communities(communities[0], [{0}, {1, 2, 3}])
84
+ validate_communities(communities[1], [{0}, {1}, {2, 3}])
85
+ validate_communities(communities[2], [{0}, {1}, {2}, {3}])
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_divisive.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ import networkx as nx
4
+
5
+
6
+ def test_edge_betweenness_partition():
7
+ G = nx.barbell_graph(3, 0)
8
+ C = nx.community.edge_betweenness_partition(G, 2)
9
+ answer = [{0, 1, 2}, {3, 4, 5}]
10
+ assert len(C) == len(answer)
11
+ for s in answer:
12
+ assert s in C
13
+
14
+ G = nx.barbell_graph(3, 1)
15
+ C = nx.community.edge_betweenness_partition(G, 3)
16
+ answer = [{0, 1, 2}, {4, 5, 6}, {3}]
17
+ assert len(C) == len(answer)
18
+ for s in answer:
19
+ assert s in C
20
+
21
+ C = nx.community.edge_betweenness_partition(G, 7)
22
+ answer = [{n} for n in G]
23
+ assert len(C) == len(answer)
24
+ for s in answer:
25
+ assert s in C
26
+
27
+ C = nx.community.edge_betweenness_partition(G, 1)
28
+ assert C == [set(G)]
29
+
30
+ C = nx.community.edge_betweenness_partition(G, 1, weight="weight")
31
+ assert C == [set(G)]
32
+
33
+ with pytest.raises(nx.NetworkXError):
34
+ nx.community.edge_betweenness_partition(G, 0)
35
+
36
+ with pytest.raises(nx.NetworkXError):
37
+ nx.community.edge_betweenness_partition(G, -1)
38
+
39
+ with pytest.raises(nx.NetworkXError):
40
+ nx.community.edge_betweenness_partition(G, 10)
41
+
42
+
43
+ def test_edge_current_flow_betweenness_partition():
44
+ pytest.importorskip("scipy")
45
+
46
+ G = nx.barbell_graph(3, 0)
47
+ C = nx.community.edge_current_flow_betweenness_partition(G, 2)
48
+ answer = [{0, 1, 2}, {3, 4, 5}]
49
+ assert len(C) == len(answer)
50
+ for s in answer:
51
+ assert s in C
52
+
53
+ G = nx.barbell_graph(3, 1)
54
+ C = nx.community.edge_current_flow_betweenness_partition(G, 2)
55
+ answers = [[{0, 1, 2, 3}, {4, 5, 6}], [{0, 1, 2}, {3, 4, 5, 6}]]
56
+ assert len(C) == len(answers[0])
57
+ assert any(all(s in answer for s in C) for answer in answers)
58
+
59
+ C = nx.community.edge_current_flow_betweenness_partition(G, 3)
60
+ answer = [{0, 1, 2}, {4, 5, 6}, {3}]
61
+ assert len(C) == len(answer)
62
+ for s in answer:
63
+ assert s in C
64
+
65
+ C = nx.community.edge_current_flow_betweenness_partition(G, 4)
66
+ answers = [[{1, 2}, {4, 5, 6}, {3}, {0}], [{0, 1, 2}, {5, 6}, {3}, {4}]]
67
+ assert len(C) == len(answers[0])
68
+ assert any(all(s in answer for s in C) for answer in answers)
69
+
70
+ C = nx.community.edge_current_flow_betweenness_partition(G, 5)
71
+ answer = [{1, 2}, {5, 6}, {3}, {0}, {4}]
72
+ assert len(C) == len(answer)
73
+ for s in answer:
74
+ assert s in C
75
+
76
+ C = nx.community.edge_current_flow_betweenness_partition(G, 6)
77
+ answers = [[{2}, {5, 6}, {3}, {0}, {4}, {1}], [{1, 2}, {6}, {3}, {0}, {4}, {5}]]
78
+ assert len(C) == len(answers[0])
79
+ assert any(all(s in answer for s in C) for answer in answers)
80
+
81
+ C = nx.community.edge_current_flow_betweenness_partition(G, 7)
82
+ answer = [{n} for n in G]
83
+ assert len(C) == len(answer)
84
+ for s in answer:
85
+ assert s in C
86
+
87
+ C = nx.community.edge_current_flow_betweenness_partition(G, 1)
88
+ assert C == [set(G)]
89
+
90
+ C = nx.community.edge_current_flow_betweenness_partition(G, 1, weight="weight")
91
+ assert C == [set(G)]
92
+
93
+ with pytest.raises(nx.NetworkXError):
94
+ nx.community.edge_current_flow_betweenness_partition(G, 0)
95
+
96
+ with pytest.raises(nx.NetworkXError):
97
+ nx.community.edge_current_flow_betweenness_partition(G, -1)
98
+
99
+ with pytest.raises(nx.NetworkXError):
100
+ nx.community.edge_current_flow_betweenness_partition(G, 10)
101
+
102
+ N = 10
103
+ G = nx.empty_graph(N)
104
+ for i in range(2, N - 1):
105
+ C = nx.community.edge_current_flow_betweenness_partition(G, i)
106
+ assert C == [{n} for n in G]
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_kclique.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from itertools import combinations
2
+
3
+ import pytest
4
+
5
+ import networkx as nx
6
+
7
+
8
+ def test_overlapping_K5():
9
+ G = nx.Graph()
10
+ G.add_edges_from(combinations(range(5), 2)) # Add a five clique
11
+ G.add_edges_from(combinations(range(2, 7), 2)) # Add another five clique
12
+ c = list(nx.community.k_clique_communities(G, 4))
13
+ assert c == [frozenset(range(7))]
14
+ c = set(nx.community.k_clique_communities(G, 5))
15
+ assert c == {frozenset(range(5)), frozenset(range(2, 7))}
16
+
17
+
18
+ def test_isolated_K5():
19
+ G = nx.Graph()
20
+ G.add_edges_from(combinations(range(5), 2)) # Add a five clique
21
+ G.add_edges_from(combinations(range(5, 10), 2)) # Add another five clique
22
+ c = set(nx.community.k_clique_communities(G, 5))
23
+ assert c == {frozenset(range(5)), frozenset(range(5, 10))}
24
+
25
+
26
+ class TestZacharyKarateClub:
27
+ def setup_method(self):
28
+ self.G = nx.karate_club_graph()
29
+
30
+ def _check_communities(self, k, expected):
31
+ communities = set(nx.community.k_clique_communities(self.G, k))
32
+ assert communities == expected
33
+
34
+ def test_k2(self):
35
+ # clique percolation with k=2 is just connected components
36
+ expected = {frozenset(self.G)}
37
+ self._check_communities(2, expected)
38
+
39
+ def test_k3(self):
40
+ comm1 = [
41
+ 0,
42
+ 1,
43
+ 2,
44
+ 3,
45
+ 7,
46
+ 8,
47
+ 12,
48
+ 13,
49
+ 14,
50
+ 15,
51
+ 17,
52
+ 18,
53
+ 19,
54
+ 20,
55
+ 21,
56
+ 22,
57
+ 23,
58
+ 26,
59
+ 27,
60
+ 28,
61
+ 29,
62
+ 30,
63
+ 31,
64
+ 32,
65
+ 33,
66
+ ]
67
+ comm2 = [0, 4, 5, 6, 10, 16]
68
+ comm3 = [24, 25, 31]
69
+ expected = {frozenset(comm1), frozenset(comm2), frozenset(comm3)}
70
+ self._check_communities(3, expected)
71
+
72
+ def test_k4(self):
73
+ expected = {
74
+ frozenset([0, 1, 2, 3, 7, 13]),
75
+ frozenset([8, 32, 30, 33]),
76
+ frozenset([32, 33, 29, 23]),
77
+ }
78
+ self._check_communities(4, expected)
79
+
80
+ def test_k5(self):
81
+ expected = {frozenset([0, 1, 2, 3, 7, 13])}
82
+ self._check_communities(5, expected)
83
+
84
+ def test_k6(self):
85
+ expected = set()
86
+ self._check_communities(6, expected)
87
+
88
+
89
+ def test_bad_k():
90
+ with pytest.raises(nx.NetworkXError):
91
+ list(nx.community.k_clique_communities(nx.Graph(), 1))
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_kernighan_lin.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Unit tests for the :mod:`networkx.algorithms.community.kernighan_lin`
2
+ module.
3
+ """
4
+
5
+ from itertools import permutations
6
+
7
+ import pytest
8
+
9
+ import networkx as nx
10
+ from networkx.algorithms.community import kernighan_lin_bisection
11
+
12
+
13
+ def assert_partition_equal(x, y):
14
+ assert set(map(frozenset, x)) == set(map(frozenset, y))
15
+
16
+
17
+ def test_partition():
18
+ G = nx.barbell_graph(3, 0)
19
+ C = kernighan_lin_bisection(G)
20
+ assert_partition_equal(C, [{0, 1, 2}, {3, 4, 5}])
21
+
22
+
23
+ def test_partition_argument():
24
+ G = nx.barbell_graph(3, 0)
25
+ partition = [{0, 1, 2}, {3, 4, 5}]
26
+ C = kernighan_lin_bisection(G, partition)
27
+ assert_partition_equal(C, partition)
28
+
29
+
30
+ def test_partition_argument_non_integer_nodes():
31
+ G = nx.Graph([("A", "B"), ("A", "C"), ("B", "C"), ("C", "D")])
32
+ partition = ({"A", "B"}, {"C", "D"})
33
+ C = kernighan_lin_bisection(G, partition)
34
+ assert_partition_equal(C, partition)
35
+
36
+
37
+ def test_seed_argument():
38
+ G = nx.barbell_graph(3, 0)
39
+ C = kernighan_lin_bisection(G, seed=1)
40
+ assert_partition_equal(C, [{0, 1, 2}, {3, 4, 5}])
41
+
42
+
43
+ def test_non_disjoint_partition():
44
+ with pytest.raises(nx.NetworkXError):
45
+ G = nx.barbell_graph(3, 0)
46
+ partition = ({0, 1, 2}, {2, 3, 4, 5})
47
+ kernighan_lin_bisection(G, partition)
48
+
49
+
50
+ def test_too_many_blocks():
51
+ with pytest.raises(nx.NetworkXError):
52
+ G = nx.barbell_graph(3, 0)
53
+ partition = ({0, 1}, {2}, {3, 4, 5})
54
+ kernighan_lin_bisection(G, partition)
55
+
56
+
57
+ def test_multigraph():
58
+ G = nx.cycle_graph(4)
59
+ M = nx.MultiGraph(G.edges())
60
+ M.add_edges_from(G.edges())
61
+ M.remove_edge(1, 2)
62
+ for labels in permutations(range(4)):
63
+ mapping = dict(zip(M, labels))
64
+ A, B = kernighan_lin_bisection(nx.relabel_nodes(M, mapping), seed=0)
65
+ assert_partition_equal(
66
+ [A, B], [{mapping[0], mapping[1]}, {mapping[2], mapping[3]}]
67
+ )
68
+
69
+
70
+ def test_max_iter_argument():
71
+ G = nx.Graph(
72
+ [
73
+ ("A", "B", {"weight": 1}),
74
+ ("A", "C", {"weight": 2}),
75
+ ("A", "D", {"weight": 3}),
76
+ ("A", "E", {"weight": 2}),
77
+ ("A", "F", {"weight": 4}),
78
+ ("B", "C", {"weight": 1}),
79
+ ("B", "D", {"weight": 4}),
80
+ ("B", "E", {"weight": 2}),
81
+ ("B", "F", {"weight": 1}),
82
+ ("C", "D", {"weight": 3}),
83
+ ("C", "E", {"weight": 2}),
84
+ ("C", "F", {"weight": 1}),
85
+ ("D", "E", {"weight": 4}),
86
+ ("D", "F", {"weight": 3}),
87
+ ("E", "F", {"weight": 2}),
88
+ ]
89
+ )
90
+ partition = ({"A", "B", "C"}, {"D", "E", "F"})
91
+ C = kernighan_lin_bisection(G, partition, max_iter=1)
92
+ assert_partition_equal(C, ({"A", "F", "C"}, {"D", "E", "B"}))
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_label_propagation.py ADDED
@@ -0,0 +1,241 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from itertools import chain, combinations
2
+
3
+ import pytest
4
+
5
+ import networkx as nx
6
+
7
+
8
+ def test_directed_not_supported():
9
+ with pytest.raises(nx.NetworkXNotImplemented):
10
+ # not supported for directed graphs
11
+ test = nx.DiGraph()
12
+ test.add_edge("a", "b")
13
+ test.add_edge("a", "c")
14
+ test.add_edge("b", "d")
15
+ result = nx.community.label_propagation_communities(test)
16
+
17
+
18
+ def test_iterator_vs_iterable():
19
+ G = nx.empty_graph("a")
20
+ assert list(nx.community.label_propagation_communities(G)) == [{"a"}]
21
+ for community in nx.community.label_propagation_communities(G):
22
+ assert community == {"a"}
23
+ pytest.raises(TypeError, next, nx.community.label_propagation_communities(G))
24
+
25
+
26
+ def test_one_node():
27
+ test = nx.Graph()
28
+ test.add_node("a")
29
+
30
+ # The expected communities are:
31
+ ground_truth = {frozenset(["a"])}
32
+
33
+ communities = nx.community.label_propagation_communities(test)
34
+ result = {frozenset(c) for c in communities}
35
+ assert result == ground_truth
36
+
37
+
38
+ def test_unconnected_communities():
39
+ test = nx.Graph()
40
+ # community 1
41
+ test.add_edge("a", "c")
42
+ test.add_edge("a", "d")
43
+ test.add_edge("d", "c")
44
+ # community 2
45
+ test.add_edge("b", "e")
46
+ test.add_edge("e", "f")
47
+ test.add_edge("f", "b")
48
+
49
+ # The expected communities are:
50
+ ground_truth = {frozenset(["a", "c", "d"]), frozenset(["b", "e", "f"])}
51
+
52
+ communities = nx.community.label_propagation_communities(test)
53
+ result = {frozenset(c) for c in communities}
54
+ assert result == ground_truth
55
+
56
+
57
+ def test_connected_communities():
58
+ test = nx.Graph()
59
+ # community 1
60
+ test.add_edge("a", "b")
61
+ test.add_edge("c", "a")
62
+ test.add_edge("c", "b")
63
+ test.add_edge("d", "a")
64
+ test.add_edge("d", "b")
65
+ test.add_edge("d", "c")
66
+ test.add_edge("e", "a")
67
+ test.add_edge("e", "b")
68
+ test.add_edge("e", "c")
69
+ test.add_edge("e", "d")
70
+ # community 2
71
+ test.add_edge("1", "2")
72
+ test.add_edge("3", "1")
73
+ test.add_edge("3", "2")
74
+ test.add_edge("4", "1")
75
+ test.add_edge("4", "2")
76
+ test.add_edge("4", "3")
77
+ test.add_edge("5", "1")
78
+ test.add_edge("5", "2")
79
+ test.add_edge("5", "3")
80
+ test.add_edge("5", "4")
81
+ # edge between community 1 and 2
82
+ test.add_edge("a", "1")
83
+ # community 3
84
+ test.add_edge("x", "y")
85
+ # community 4 with only a single node
86
+ test.add_node("z")
87
+
88
+ # The expected communities are:
89
+ ground_truth1 = {
90
+ frozenset(["a", "b", "c", "d", "e"]),
91
+ frozenset(["1", "2", "3", "4", "5"]),
92
+ frozenset(["x", "y"]),
93
+ frozenset(["z"]),
94
+ }
95
+ ground_truth2 = {
96
+ frozenset(["a", "b", "c", "d", "e", "1", "2", "3", "4", "5"]),
97
+ frozenset(["x", "y"]),
98
+ frozenset(["z"]),
99
+ }
100
+ ground_truth = (ground_truth1, ground_truth2)
101
+
102
+ communities = nx.community.label_propagation_communities(test)
103
+ result = {frozenset(c) for c in communities}
104
+ assert result in ground_truth
105
+
106
+
107
+ def test_termination():
108
+ # ensure termination of asyn_lpa_communities in two cases
109
+ # that led to an endless loop in a previous version
110
+ test1 = nx.karate_club_graph()
111
+ test2 = nx.caveman_graph(2, 10)
112
+ test2.add_edges_from([(0, 20), (20, 10)])
113
+ nx.community.asyn_lpa_communities(test1)
114
+ nx.community.asyn_lpa_communities(test2)
115
+
116
+
117
+ class TestAsynLpaCommunities:
118
+ def _check_communities(self, G, expected):
119
+ """Checks that the communities computed from the given graph ``G``
120
+ using the :func:`~networkx.asyn_lpa_communities` function match
121
+ the set of nodes given in ``expected``.
122
+
123
+ ``expected`` must be a :class:`set` of :class:`frozenset`
124
+ instances, each element of which is a node in the graph.
125
+
126
+ """
127
+ communities = nx.community.asyn_lpa_communities(G)
128
+ result = {frozenset(c) for c in communities}
129
+ assert result == expected
130
+
131
+ def test_null_graph(self):
132
+ G = nx.null_graph()
133
+ ground_truth = set()
134
+ self._check_communities(G, ground_truth)
135
+
136
+ def test_single_node(self):
137
+ G = nx.empty_graph(1)
138
+ ground_truth = {frozenset([0])}
139
+ self._check_communities(G, ground_truth)
140
+
141
+ def test_simple_communities(self):
142
+ # This graph is the disjoint union of two triangles.
143
+ G = nx.Graph(["ab", "ac", "bc", "de", "df", "fe"])
144
+ ground_truth = {frozenset("abc"), frozenset("def")}
145
+ self._check_communities(G, ground_truth)
146
+
147
+ def test_seed_argument(self):
148
+ G = nx.Graph(["ab", "ac", "bc", "de", "df", "fe"])
149
+ ground_truth = {frozenset("abc"), frozenset("def")}
150
+ communities = nx.community.asyn_lpa_communities(G, seed=1)
151
+ result = {frozenset(c) for c in communities}
152
+ assert result == ground_truth
153
+
154
+ def test_several_communities(self):
155
+ # This graph is the disjoint union of five triangles.
156
+ ground_truth = {frozenset(range(3 * i, 3 * (i + 1))) for i in range(5)}
157
+ edges = chain.from_iterable(combinations(c, 2) for c in ground_truth)
158
+ G = nx.Graph(edges)
159
+ self._check_communities(G, ground_truth)
160
+
161
+
162
+ class TestFastLabelPropagationCommunities:
163
+ N = 100 # number of nodes
164
+ K = 15 # average node degree
165
+
166
+ def _check_communities(self, G, truth, weight=None, seed=42):
167
+ C = nx.community.fast_label_propagation_communities(G, weight=weight, seed=seed)
168
+ assert {frozenset(c) for c in C} == truth
169
+
170
+ def test_null_graph(self):
171
+ G = nx.null_graph()
172
+ truth = set()
173
+ self._check_communities(G, truth)
174
+
175
+ def test_empty_graph(self):
176
+ G = nx.empty_graph(self.N)
177
+ truth = {frozenset([i]) for i in G}
178
+ self._check_communities(G, truth)
179
+
180
+ def test_star_graph(self):
181
+ G = nx.star_graph(self.N)
182
+ truth = {frozenset(G)}
183
+ self._check_communities(G, truth)
184
+
185
+ def test_complete_graph(self):
186
+ G = nx.complete_graph(self.N)
187
+ truth = {frozenset(G)}
188
+ self._check_communities(G, truth)
189
+
190
+ def test_bipartite_graph(self):
191
+ G = nx.complete_bipartite_graph(self.N // 2, self.N // 2)
192
+ truth = {frozenset(G)}
193
+ self._check_communities(G, truth)
194
+
195
+ def test_random_graph(self):
196
+ G = nx.gnm_random_graph(self.N, self.N * self.K // 2, seed=42)
197
+ truth = {frozenset(G)}
198
+ self._check_communities(G, truth)
199
+
200
+ def test_disjoin_cliques(self):
201
+ G = nx.Graph(["ab", "AB", "AC", "BC", "12", "13", "14", "23", "24", "34"])
202
+ truth = {frozenset("ab"), frozenset("ABC"), frozenset("1234")}
203
+ self._check_communities(G, truth)
204
+
205
+ def test_ring_of_cliques(self):
206
+ N, K = self.N, self.K
207
+ G = nx.ring_of_cliques(N, K)
208
+ truth = {frozenset([K * i + k for k in range(K)]) for i in range(N)}
209
+ self._check_communities(G, truth)
210
+
211
+ def test_larger_graph(self):
212
+ G = nx.gnm_random_graph(100 * self.N, 50 * self.N * self.K, seed=42)
213
+ nx.community.fast_label_propagation_communities(G)
214
+
215
+ def test_graph_type(self):
216
+ G1 = nx.complete_graph(self.N, nx.MultiDiGraph())
217
+ G2 = nx.MultiGraph(G1)
218
+ G3 = nx.DiGraph(G1)
219
+ G4 = nx.Graph(G1)
220
+ truth = {frozenset(G1)}
221
+ self._check_communities(G1, truth)
222
+ self._check_communities(G2, truth)
223
+ self._check_communities(G3, truth)
224
+ self._check_communities(G4, truth)
225
+
226
+ def test_weight_argument(self):
227
+ G = nx.MultiDiGraph()
228
+ G.add_edge(1, 2, weight=1.41)
229
+ G.add_edge(2, 1, weight=1.41)
230
+ G.add_edge(2, 3)
231
+ G.add_edge(3, 4, weight=3.14)
232
+ truth = {frozenset({1, 2}), frozenset({3, 4})}
233
+ self._check_communities(G, truth, weight="weight")
234
+
235
+ def test_seed_argument(self):
236
+ G = nx.karate_club_graph()
237
+ C = nx.community.fast_label_propagation_communities(G, seed=2023)
238
+ truth = {frozenset(c) for c in C}
239
+ self._check_communities(G, truth, seed=2023)
240
+ # smoke test that seed=None works
241
+ C = nx.community.fast_label_propagation_communities(G, seed=None)
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_leiden.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ import networkx as nx
4
+ from networkx.algorithms.community import leiden_communities, leiden_partitions
5
+
6
+ # Leiden is not yet implemented by networkx, so only run tests in this file for
7
+ # backends that implement Leiden.
8
+ no_backends_for_leiden_communities = (
9
+ "not set(nx.config.backend_priority.algos) & leiden_communities.backends"
10
+ )
11
+
12
+ no_backends_for_leiden_partitions = (
13
+ "not set(nx.config.backend_priority.algos) & leiden_partitions.backends"
14
+ )
15
+
16
+
17
+ def test_leiden_with_nx_backend():
18
+ G = nx.karate_club_graph()
19
+ with pytest.raises(NotImplementedError):
20
+ nx.community.leiden_partitions(G, backend="networkx")
21
+ with pytest.raises(NotImplementedError):
22
+ nx.community.leiden_communities(G, backend="networkx")
23
+
24
+
25
+ @pytest.mark.skipif(no_backends_for_leiden_communities)
26
+ def test_modularity_increase():
27
+ G = nx.LFR_benchmark_graph(
28
+ 250, 3, 1.5, 0.009, average_degree=5, min_community=20, seed=10
29
+ )
30
+ partition = [{u} for u in G.nodes()]
31
+ mod = nx.community.modularity(G, partition)
32
+ partition = nx.community.leiden_communities(G)
33
+
34
+ assert nx.community.modularity(G, partition) > mod
35
+
36
+
37
+ @pytest.mark.skipif(no_backends_for_leiden_communities)
38
+ def test_valid_partition():
39
+ G = nx.LFR_benchmark_graph(
40
+ 250, 3, 1.5, 0.009, average_degree=5, min_community=20, seed=10
41
+ )
42
+ partition = nx.community.leiden_communities(G)
43
+
44
+ assert nx.community.is_partition(G, partition)
45
+
46
+
47
+ @pytest.mark.skipif(no_backends_for_leiden_partitions)
48
+ def test_partition_iterator():
49
+ G = nx.path_graph(15)
50
+ parts_iter = nx.community.leiden_partitions(G, seed=42)
51
+ first_part = next(parts_iter)
52
+ first_copy = [s.copy() for s in first_part]
53
+
54
+ # check 1st part stays fixed even after 2nd iteration (like gh-5901 in louvain)
55
+ assert first_copy[0] == first_part[0]
56
+ second_part = next(parts_iter)
57
+ assert first_copy[0] == first_part[0]
58
+
59
+
60
+ @pytest.mark.skipif(no_backends_for_leiden_communities)
61
+ def test_none_weight_param():
62
+ G = nx.karate_club_graph()
63
+ nx.set_edge_attributes(
64
+ G, {edge: i * i for i, edge in enumerate(G.edges)}, name="foo"
65
+ )
66
+
67
+ partition1 = nx.community.leiden_communities(G, weight=None, seed=2)
68
+ partition2 = nx.community.leiden_communities(G, weight="foo", seed=2)
69
+ partition3 = nx.community.leiden_communities(G, weight="weight", seed=2)
70
+
71
+ assert partition1 != partition2
72
+ assert partition2 != partition3
73
+
74
+
75
+ @pytest.mark.skipif(no_backends_for_leiden_communities)
76
+ def test_quality():
77
+ G = nx.LFR_benchmark_graph(
78
+ 250, 3, 1.5, 0.009, average_degree=5, min_community=20, seed=10
79
+ )
80
+ H = nx.MultiGraph(G)
81
+
82
+ partition = nx.community.leiden_communities(G)
83
+ partition2 = nx.community.leiden_communities(H)
84
+
85
+ quality = nx.community.partition_quality(G, partition)[0]
86
+ quality2 = nx.community.partition_quality(H, partition2)[0]
87
+
88
+ assert quality >= 0.65
89
+ assert quality2 >= 0.65
90
+
91
+
92
+ @pytest.mark.skipif(no_backends_for_leiden_communities)
93
+ def test_resolution():
94
+ G = nx.LFR_benchmark_graph(
95
+ 250, 3, 1.5, 0.009, average_degree=5, min_community=20, seed=10
96
+ )
97
+
98
+ partition1 = nx.community.leiden_communities(G, resolution=0.5, seed=12)
99
+ partition2 = nx.community.leiden_communities(G, seed=12)
100
+ partition3 = nx.community.leiden_communities(G, resolution=2, seed=12)
101
+
102
+ assert len(partition1) <= len(partition2)
103
+ assert len(partition2) <= len(partition3)
104
+
105
+
106
+ @pytest.mark.skipif(no_backends_for_leiden_communities)
107
+ def test_empty_graph():
108
+ G = nx.Graph()
109
+ G.add_nodes_from(range(5))
110
+ expected = [{0}, {1}, {2}, {3}, {4}]
111
+ assert nx.community.leiden_communities(G) == expected
112
+
113
+
114
+ @pytest.mark.skipif(no_backends_for_leiden_communities)
115
+ def test_directed_not_implemented():
116
+ G = nx.cycle_graph(4, create_using=nx.DiGraph)
117
+ with pytest.raises(nx.NetworkXNotImplemented):
118
+ nx.community.leiden_communities(G)
119
+
120
+
121
+ @pytest.mark.skipif(no_backends_for_leiden_partitions)
122
+ @pytest.mark.skipif(no_backends_for_leiden_communities)
123
+ def test_max_level():
124
+ G = nx.LFR_benchmark_graph(
125
+ 250, 3, 1.5, 0.009, average_degree=5, min_community=20, seed=10
126
+ )
127
+ parts_iter = nx.community.leiden_partitions(G, seed=42)
128
+ for max_level, expected in enumerate(parts_iter, 1):
129
+ partition = nx.community.leiden_communities(G, max_level=max_level, seed=42)
130
+ assert partition == expected
131
+ assert max_level > 1 # Ensure we are actually testing max_level
132
+ # max_level is an upper limit; it's okay if we stop before it's hit.
133
+ partition = nx.community.leiden_communities(G, max_level=max_level + 1, seed=42)
134
+ assert partition == expected
135
+ with pytest.raises(
136
+ ValueError, match="max_level argument must be a positive integer"
137
+ ):
138
+ nx.community.leiden_communities(G, max_level=0)
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_local.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ import networkx as nx
4
+
5
+
6
+ def test_greedy_source_expansion_karate_club():
7
+ G = nx.karate_club_graph()
8
+
9
+ community = nx.community.greedy_source_expansion(G, source=16)
10
+
11
+ expected = {0, 4, 5, 6, 10, 16}
12
+
13
+ assert community == expected
14
+
15
+
16
+ def test_greedy_source_expansion_cutoff():
17
+ G = nx.karate_club_graph()
18
+
19
+ community = nx.community.greedy_source_expansion(G, source=16, cutoff=3)
20
+
21
+ assert community == {5, 6, 16}
22
+
23
+
24
+ def test_greedy_source_expansion_invalid_method():
25
+ G = nx.karate_club_graph()
26
+
27
+ with pytest.raises(ValueError):
28
+ nx.community.greedy_source_expansion(G, source=16, cutoff=3, method="invalid")
29
+
30
+
31
+ def test_greedy_source_expansion_connected_component():
32
+ G_edges = [(0, 2), (0, 1), (1, 0), (2, 1), (2, 0), (3, 4), (4, 3)]
33
+ G = nx.Graph(G_edges)
34
+ expected = {0, 1, 2}
35
+ community = nx.community.greedy_source_expansion(G, source=0)
36
+ assert community == expected
37
+
38
+
39
+ def test_greedy_source_expansion_directed_graph():
40
+ G_edges = [
41
+ (0, 2),
42
+ (0, 1),
43
+ (1, 0),
44
+ (2, 1),
45
+ (2, 0),
46
+ (3, 4),
47
+ (4, 3),
48
+ (4, 5),
49
+ (5, 3),
50
+ (5, 6),
51
+ (0, 6),
52
+ ]
53
+ G = nx.DiGraph(G_edges)
54
+
55
+ expected = {0, 1, 2, 6}
56
+ community = nx.community.greedy_source_expansion(G, source=0)
57
+ assert community == expected
58
+
59
+
60
+ def test_greedy_source_expansion_multigraph():
61
+ G = nx.MultiGraph(nx.karate_club_graph())
62
+ G.add_edge(0, 1)
63
+ G.add_edge(0, 9)
64
+
65
+ expected = {0, 4, 5, 6, 10, 16}
66
+
67
+ community = nx.community.greedy_source_expansion(G, source=16)
68
+
69
+ assert community == expected
70
+
71
+
72
+ def test_greedy_source_expansion_empty_graph():
73
+ G = nx.Graph()
74
+ G.add_nodes_from(range(5))
75
+ expected = {0}
76
+ assert nx.community.greedy_source_expansion(G, source=0) == expected
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_louvain.py ADDED
@@ -0,0 +1,264 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ import networkx as nx
4
+
5
+
6
+ def test_modularity_increase():
7
+ G = nx.LFR_benchmark_graph(
8
+ 250, 3, 1.5, 0.009, average_degree=5, min_community=20, seed=10
9
+ )
10
+ partition = [{u} for u in G.nodes()]
11
+ mod = nx.community.modularity(G, partition)
12
+ partition = nx.community.louvain_communities(G)
13
+
14
+ assert nx.community.modularity(G, partition) > mod
15
+
16
+
17
+ def test_valid_partition():
18
+ G = nx.LFR_benchmark_graph(
19
+ 250, 3, 1.5, 0.009, average_degree=5, min_community=20, seed=10
20
+ )
21
+ H = G.to_directed()
22
+ partition = nx.community.louvain_communities(G)
23
+ partition2 = nx.community.louvain_communities(H)
24
+
25
+ assert nx.community.is_partition(G, partition)
26
+ assert nx.community.is_partition(H, partition2)
27
+
28
+
29
+ def test_karate_club_partition():
30
+ G = nx.karate_club_graph()
31
+ part = [
32
+ {0, 1, 2, 3, 7, 9, 11, 12, 13, 17, 19, 21},
33
+ {16, 4, 5, 6, 10},
34
+ {23, 25, 27, 28, 24, 31},
35
+ {32, 33, 8, 14, 15, 18, 20, 22, 26, 29, 30},
36
+ ]
37
+ partition = nx.community.louvain_communities(G, seed=2, weight=None)
38
+
39
+ assert part == partition
40
+
41
+
42
+ def test_partition_iterator():
43
+ G = nx.path_graph(15)
44
+ parts_iter = nx.community.louvain_partitions(G, seed=42)
45
+ first_part = next(parts_iter)
46
+ first_copy = [s.copy() for s in first_part]
47
+
48
+ # gh-5901 reports sets changing after next partition is yielded
49
+ assert first_copy[0] == first_part[0]
50
+ second_part = next(parts_iter)
51
+ assert first_copy[0] == first_part[0]
52
+
53
+
54
+ def test_undirected_selfloops():
55
+ G = nx.karate_club_graph()
56
+ expected_partition = nx.community.louvain_communities(G, seed=2, weight=None)
57
+ part = [
58
+ {0, 1, 2, 3, 7, 9, 11, 12, 13, 17, 19, 21},
59
+ {16, 4, 5, 6, 10},
60
+ {23, 25, 27, 28, 24, 31},
61
+ {32, 33, 8, 14, 15, 18, 20, 22, 26, 29, 30},
62
+ ]
63
+ assert expected_partition == part
64
+
65
+ G.add_weighted_edges_from([(i, i, i * 1000) for i in range(9)])
66
+ # large self-loop weight impacts partition
67
+ partition = nx.community.louvain_communities(G, seed=2, weight="weight")
68
+ assert part != partition
69
+
70
+ # small self-loop weights aren't enough to impact partition in this graph
71
+ partition = nx.community.louvain_communities(G, seed=2, weight=None)
72
+ assert part == partition
73
+
74
+
75
+ def test_directed_selfloops():
76
+ G = nx.DiGraph()
77
+ G.add_nodes_from(range(11))
78
+ G_edges = [
79
+ (0, 2),
80
+ (0, 1),
81
+ (1, 0),
82
+ (2, 1),
83
+ (2, 0),
84
+ (3, 4),
85
+ (4, 3),
86
+ (7, 8),
87
+ (8, 7),
88
+ (9, 10),
89
+ (10, 9),
90
+ ]
91
+ G.add_edges_from(G_edges)
92
+ G_expected_partition = nx.community.louvain_communities(G, seed=123, weight=None)
93
+
94
+ G.add_weighted_edges_from([(i, i, i * 1000) for i in range(3)])
95
+ # large self-loop weight impacts partition
96
+ G_partition = nx.community.louvain_communities(G, seed=123, weight="weight")
97
+ assert G_partition != G_expected_partition
98
+
99
+ # small self-loop weights aren't enough to impact partition in this graph
100
+ G_partition = nx.community.louvain_communities(G, seed=123, weight=None)
101
+ assert G_partition == G_expected_partition
102
+
103
+
104
+ def test_directed_partition():
105
+ """
106
+ Test 2 cases that were looping infinitely
107
+ from issues #5175 and #5704
108
+ """
109
+ G = nx.DiGraph()
110
+ H = nx.DiGraph()
111
+ G.add_nodes_from(range(10))
112
+ H.add_nodes_from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
113
+ G_edges = [
114
+ (0, 2),
115
+ (0, 1),
116
+ (1, 0),
117
+ (2, 1),
118
+ (2, 0),
119
+ (3, 4),
120
+ (4, 3),
121
+ (7, 8),
122
+ (8, 7),
123
+ (9, 10),
124
+ (10, 9),
125
+ ]
126
+ H_edges = [
127
+ (1, 2),
128
+ (1, 6),
129
+ (1, 9),
130
+ (2, 3),
131
+ (2, 4),
132
+ (2, 5),
133
+ (3, 4),
134
+ (4, 3),
135
+ (4, 5),
136
+ (5, 4),
137
+ (6, 7),
138
+ (6, 8),
139
+ (9, 10),
140
+ (9, 11),
141
+ (10, 11),
142
+ (11, 10),
143
+ ]
144
+ G.add_edges_from(G_edges)
145
+ H.add_edges_from(H_edges)
146
+
147
+ G_expected_partition = [{0, 1, 2}, {3, 4}, {5}, {6}, {8, 7}, {9, 10}]
148
+ G_partition = nx.community.louvain_communities(G, seed=123, weight=None)
149
+
150
+ H_expected_partition = [{2, 3, 4, 5}, {8, 1, 6, 7}, {9, 10, 11}]
151
+ H_partition = nx.community.louvain_communities(H, seed=123, weight=None)
152
+
153
+ assert G_partition == G_expected_partition
154
+ assert H_partition == H_expected_partition
155
+
156
+
157
+ def test_none_weight_param():
158
+ G = nx.karate_club_graph()
159
+ nx.set_edge_attributes(
160
+ G, {edge: i * i for i, edge in enumerate(G.edges)}, name="foo"
161
+ )
162
+
163
+ part = [
164
+ {0, 1, 2, 3, 7, 9, 11, 12, 13, 17, 19, 21},
165
+ {16, 4, 5, 6, 10},
166
+ {23, 25, 27, 28, 24, 31},
167
+ {32, 33, 8, 14, 15, 18, 20, 22, 26, 29, 30},
168
+ ]
169
+ partition1 = nx.community.louvain_communities(G, weight=None, seed=2)
170
+ partition2 = nx.community.louvain_communities(G, weight="foo", seed=2)
171
+ partition3 = nx.community.louvain_communities(G, weight="weight", seed=2)
172
+
173
+ assert part == partition1
174
+ assert part != partition2
175
+ assert part != partition3
176
+ assert partition2 != partition3
177
+
178
+
179
+ def test_quality():
180
+ G = nx.LFR_benchmark_graph(
181
+ 250, 3, 1.5, 0.009, average_degree=5, min_community=20, seed=10
182
+ )
183
+ H = nx.gn_graph(200, seed=1234)
184
+ I = nx.MultiGraph(G)
185
+ J = nx.MultiDiGraph(H)
186
+
187
+ partition = nx.community.louvain_communities(G)
188
+ partition2 = nx.community.louvain_communities(H)
189
+ partition3 = nx.community.louvain_communities(I)
190
+ partition4 = nx.community.louvain_communities(J)
191
+
192
+ quality = nx.community.partition_quality(G, partition)[0]
193
+ quality2 = nx.community.partition_quality(H, partition2)[0]
194
+ quality3 = nx.community.partition_quality(I, partition3)[0]
195
+ quality4 = nx.community.partition_quality(J, partition4)[0]
196
+
197
+ assert quality >= 0.65
198
+ assert quality2 >= 0.65
199
+ assert quality3 >= 0.65
200
+ assert quality4 >= 0.65
201
+
202
+
203
+ def test_multigraph():
204
+ G = nx.karate_club_graph()
205
+ H = nx.MultiGraph(G)
206
+ G.add_edge(0, 1, weight=10)
207
+ H.add_edge(0, 1, weight=9)
208
+ G.add_edge(0, 9, foo=20)
209
+ H.add_edge(0, 9, foo=20)
210
+
211
+ partition1 = nx.community.louvain_communities(G, seed=1234)
212
+ partition2 = nx.community.louvain_communities(H, seed=1234)
213
+ partition3 = nx.community.louvain_communities(H, weight="foo", seed=1234)
214
+
215
+ assert partition1 == partition2 != partition3
216
+
217
+
218
+ def test_resolution():
219
+ G = nx.LFR_benchmark_graph(
220
+ 250, 3, 1.5, 0.009, average_degree=5, min_community=20, seed=10
221
+ )
222
+
223
+ partition1 = nx.community.louvain_communities(G, resolution=0.5, seed=12)
224
+ partition2 = nx.community.louvain_communities(G, seed=12)
225
+ partition3 = nx.community.louvain_communities(G, resolution=2, seed=12)
226
+
227
+ assert len(partition1) <= len(partition2) <= len(partition3)
228
+
229
+
230
+ def test_threshold():
231
+ G = nx.LFR_benchmark_graph(
232
+ 250, 3, 1.5, 0.009, average_degree=5, min_community=20, seed=10
233
+ )
234
+ partition1 = nx.community.louvain_communities(G, threshold=0.3, seed=2)
235
+ partition2 = nx.community.louvain_communities(G, seed=2)
236
+ mod1 = nx.community.modularity(G, partition1)
237
+ mod2 = nx.community.modularity(G, partition2)
238
+
239
+ assert mod1 <= mod2
240
+
241
+
242
+ def test_empty_graph():
243
+ G = nx.Graph()
244
+ G.add_nodes_from(range(5))
245
+ expected = [{0}, {1}, {2}, {3}, {4}]
246
+ assert nx.community.louvain_communities(G) == expected
247
+
248
+
249
+ def test_max_level():
250
+ G = nx.LFR_benchmark_graph(
251
+ 250, 3, 1.5, 0.009, average_degree=5, min_community=20, seed=10
252
+ )
253
+ parts_iter = nx.community.louvain_partitions(G, seed=42)
254
+ for max_level, expected in enumerate(parts_iter, 1):
255
+ partition = nx.community.louvain_communities(G, max_level=max_level, seed=42)
256
+ assert partition == expected
257
+ assert max_level > 1 # Ensure we are actually testing max_level
258
+ # max_level is an upper limit; it's okay if we stop before it's hit.
259
+ partition = nx.community.louvain_communities(G, max_level=max_level + 1, seed=42)
260
+ assert partition == expected
261
+ with pytest.raises(
262
+ ValueError, match="max_level argument must be a positive integer"
263
+ ):
264
+ nx.community.louvain_communities(G, max_level=0)
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_lukes.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from itertools import product
2
+
3
+ import pytest
4
+
5
+ import networkx as nx
6
+
7
+ EWL = "e_weight"
8
+ NWL = "n_weight"
9
+
10
+
11
+ # first test from the Lukes original paper
12
+ def paper_1_case(float_edge_wt=False, explicit_node_wt=True, directed=False):
13
+ # problem-specific constants
14
+ limit = 3
15
+
16
+ # configuration
17
+ if float_edge_wt:
18
+ shift = 0.001
19
+ else:
20
+ shift = 0
21
+
22
+ if directed:
23
+ example_1 = nx.DiGraph()
24
+ else:
25
+ example_1 = nx.Graph()
26
+
27
+ # graph creation
28
+ example_1.add_edge(1, 2, **{EWL: 3 + shift})
29
+ example_1.add_edge(1, 4, **{EWL: 2 + shift})
30
+ example_1.add_edge(2, 3, **{EWL: 4 + shift})
31
+ example_1.add_edge(2, 5, **{EWL: 6 + shift})
32
+
33
+ # node weights
34
+ if explicit_node_wt:
35
+ nx.set_node_attributes(example_1, 1, NWL)
36
+ wtu = NWL
37
+ else:
38
+ wtu = None
39
+
40
+ # partitioning
41
+ clusters_1 = {
42
+ frozenset(x)
43
+ for x in nx.community.lukes_partitioning(
44
+ example_1, limit, node_weight=wtu, edge_weight=EWL
45
+ )
46
+ }
47
+
48
+ return clusters_1
49
+
50
+
51
+ # second test from the Lukes original paper
52
+ def paper_2_case(explicit_edge_wt=True, directed=False):
53
+ # problem specific constants
54
+ byte_block_size = 32
55
+
56
+ # configuration
57
+ if directed:
58
+ example_2 = nx.DiGraph()
59
+ else:
60
+ example_2 = nx.Graph()
61
+
62
+ if explicit_edge_wt:
63
+ edic = {EWL: 1}
64
+ wtu = EWL
65
+ else:
66
+ edic = {}
67
+ wtu = None
68
+
69
+ # graph creation
70
+ example_2.add_edge("name", "home_address", **edic)
71
+ example_2.add_edge("name", "education", **edic)
72
+ example_2.add_edge("education", "bs", **edic)
73
+ example_2.add_edge("education", "ms", **edic)
74
+ example_2.add_edge("education", "phd", **edic)
75
+ example_2.add_edge("name", "telephone", **edic)
76
+ example_2.add_edge("telephone", "home", **edic)
77
+ example_2.add_edge("telephone", "office", **edic)
78
+ example_2.add_edge("office", "no1", **edic)
79
+ example_2.add_edge("office", "no2", **edic)
80
+
81
+ example_2.nodes["name"][NWL] = 20
82
+ example_2.nodes["education"][NWL] = 10
83
+ example_2.nodes["bs"][NWL] = 1
84
+ example_2.nodes["ms"][NWL] = 1
85
+ example_2.nodes["phd"][NWL] = 1
86
+ example_2.nodes["home_address"][NWL] = 8
87
+ example_2.nodes["telephone"][NWL] = 8
88
+ example_2.nodes["home"][NWL] = 8
89
+ example_2.nodes["office"][NWL] = 4
90
+ example_2.nodes["no1"][NWL] = 1
91
+ example_2.nodes["no2"][NWL] = 1
92
+
93
+ # partitioning
94
+ clusters_2 = {
95
+ frozenset(x)
96
+ for x in nx.community.lukes_partitioning(
97
+ example_2, byte_block_size, node_weight=NWL, edge_weight=wtu
98
+ )
99
+ }
100
+
101
+ return clusters_2
102
+
103
+
104
+ def test_paper_1_case():
105
+ ground_truth = {frozenset([1, 4]), frozenset([2, 3, 5])}
106
+
107
+ tf = (True, False)
108
+ for flt, nwt, drc in product(tf, tf, tf):
109
+ part = paper_1_case(flt, nwt, drc)
110
+ assert part == ground_truth
111
+
112
+
113
+ def test_paper_2_case():
114
+ ground_truth = {
115
+ frozenset(["education", "bs", "ms", "phd"]),
116
+ frozenset(["name", "home_address"]),
117
+ frozenset(["telephone", "home", "office", "no1", "no2"]),
118
+ }
119
+
120
+ tf = (True, False)
121
+ for ewt, drc in product(tf, tf):
122
+ part = paper_2_case(ewt, drc)
123
+ assert part == ground_truth
124
+
125
+
126
+ def test_mandatory_tree():
127
+ not_a_tree = nx.complete_graph(4)
128
+
129
+ with pytest.raises(nx.NotATree):
130
+ nx.community.lukes_partitioning(not_a_tree, 5)
131
+
132
+
133
+ def test_mandatory_integrality():
134
+ byte_block_size = 32
135
+
136
+ ex_1_broken = nx.DiGraph()
137
+
138
+ ex_1_broken.add_edge(1, 2, **{EWL: 3.2})
139
+ ex_1_broken.add_edge(1, 4, **{EWL: 2.4})
140
+ ex_1_broken.add_edge(2, 3, **{EWL: 4.0})
141
+ ex_1_broken.add_edge(2, 5, **{EWL: 6.3})
142
+
143
+ ex_1_broken.nodes[1][NWL] = 1.2 # !
144
+ ex_1_broken.nodes[2][NWL] = 1
145
+ ex_1_broken.nodes[3][NWL] = 1
146
+ ex_1_broken.nodes[4][NWL] = 1
147
+ ex_1_broken.nodes[5][NWL] = 2
148
+
149
+ with pytest.raises(TypeError):
150
+ nx.community.lukes_partitioning(
151
+ ex_1_broken, byte_block_size, node_weight=NWL, edge_weight=EWL
152
+ )
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_modularity_max.py ADDED
@@ -0,0 +1,340 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ import networkx as nx
4
+ from networkx.algorithms.community import (
5
+ greedy_modularity_communities,
6
+ naive_greedy_modularity_communities,
7
+ )
8
+
9
+
10
+ @pytest.mark.parametrize(
11
+ "func", (greedy_modularity_communities, naive_greedy_modularity_communities)
12
+ )
13
+ def test_modularity_communities(func):
14
+ G = nx.karate_club_graph()
15
+ john_a = frozenset(
16
+ [8, 14, 15, 18, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]
17
+ )
18
+ mr_hi = frozenset([0, 4, 5, 6, 10, 11, 16, 19])
19
+ overlap = frozenset([1, 2, 3, 7, 9, 12, 13, 17, 21])
20
+ expected = {john_a, overlap, mr_hi}
21
+ assert set(func(G, weight=None)) == expected
22
+
23
+
24
+ @pytest.mark.parametrize(
25
+ "func", (greedy_modularity_communities, naive_greedy_modularity_communities)
26
+ )
27
+ def test_modularity_communities_categorical_labels(func):
28
+ # Using other than 0-starting contiguous integers as node-labels.
29
+ G = nx.Graph(
30
+ [
31
+ ("a", "b"),
32
+ ("a", "c"),
33
+ ("b", "c"),
34
+ ("b", "d"), # inter-community edge
35
+ ("d", "e"),
36
+ ("d", "f"),
37
+ ("d", "g"),
38
+ ("f", "g"),
39
+ ("d", "e"),
40
+ ("f", "e"),
41
+ ]
42
+ )
43
+ expected = {frozenset({"f", "g", "e", "d"}), frozenset({"a", "b", "c"})}
44
+ assert set(func(G)) == expected
45
+
46
+
47
+ def test_greedy_modularity_communities_components():
48
+ # Test for gh-5530
49
+ G = nx.Graph([(0, 1), (2, 3), (4, 5), (5, 6)])
50
+ # usual case with 3 components
51
+ assert greedy_modularity_communities(G) == [{4, 5, 6}, {0, 1}, {2, 3}]
52
+ # best_n can make the algorithm continue even when modularity goes down
53
+ assert greedy_modularity_communities(G, best_n=3) == [{4, 5, 6}, {0, 1}, {2, 3}]
54
+ assert greedy_modularity_communities(G, best_n=2) == [{0, 1, 4, 5, 6}, {2, 3}]
55
+ assert greedy_modularity_communities(G, best_n=1) == [{0, 1, 2, 3, 4, 5, 6}]
56
+
57
+
58
+ def test_greedy_modularity_communities_relabeled():
59
+ # Test for gh-4966
60
+ G = nx.balanced_tree(2, 2)
61
+ mapping = {0: "a", 1: "b", 2: "c", 3: "d", 4: "e", 5: "f", 6: "g", 7: "h"}
62
+ G = nx.relabel_nodes(G, mapping)
63
+ expected = [frozenset({"e", "d", "a", "b"}), frozenset({"c", "f", "g"})]
64
+ assert greedy_modularity_communities(G) == expected
65
+
66
+
67
+ def test_greedy_modularity_communities_directed():
68
+ G = nx.DiGraph(
69
+ [
70
+ ("a", "b"),
71
+ ("a", "c"),
72
+ ("b", "c"),
73
+ ("b", "d"), # inter-community edge
74
+ ("d", "e"),
75
+ ("d", "f"),
76
+ ("d", "g"),
77
+ ("f", "g"),
78
+ ("d", "e"),
79
+ ("f", "e"),
80
+ ]
81
+ )
82
+ expected = [frozenset({"f", "g", "e", "d"}), frozenset({"a", "b", "c"})]
83
+ assert greedy_modularity_communities(G) == expected
84
+
85
+ # with loops
86
+ G = nx.DiGraph()
87
+ G.add_edges_from(
88
+ [(1, 1), (1, 2), (1, 3), (2, 3), (1, 4), (4, 4), (5, 5), (4, 5), (4, 6), (5, 6)]
89
+ )
90
+ expected = [frozenset({1, 2, 3}), frozenset({4, 5, 6})]
91
+ assert greedy_modularity_communities(G) == expected
92
+
93
+
94
+ @pytest.mark.parametrize(
95
+ "func", (greedy_modularity_communities, naive_greedy_modularity_communities)
96
+ )
97
+ def test_modularity_communities_weighted(func):
98
+ G = nx.balanced_tree(2, 3)
99
+ for a, b in G.edges:
100
+ if ((a == 1) or (a == 2)) and (b != 0):
101
+ G[a][b]["weight"] = 10.0
102
+ else:
103
+ G[a][b]["weight"] = 1.0
104
+
105
+ expected = [{0, 1, 3, 4, 7, 8, 9, 10}, {2, 5, 6, 11, 12, 13, 14}]
106
+
107
+ assert func(G, weight="weight") == expected
108
+ assert func(G, weight="weight", resolution=0.9) == expected
109
+ assert func(G, weight="weight", resolution=0.3) == expected
110
+ assert func(G, weight="weight", resolution=1.1) != expected
111
+
112
+
113
+ def test_modularity_communities_floating_point():
114
+ # check for floating point error when used as key in the mapped_queue dict.
115
+ # Test for gh-4992 and gh-5000
116
+ G = nx.Graph()
117
+ G.add_weighted_edges_from(
118
+ [(0, 1, 12), (1, 4, 71), (2, 3, 15), (2, 4, 10), (3, 6, 13)]
119
+ )
120
+ expected = [{0, 1, 4}, {2, 3, 6}]
121
+ assert greedy_modularity_communities(G, weight="weight") == expected
122
+ assert (
123
+ greedy_modularity_communities(G, weight="weight", resolution=0.99) == expected
124
+ )
125
+
126
+
127
+ def test_modularity_communities_directed_weighted():
128
+ G = nx.DiGraph()
129
+ G.add_weighted_edges_from(
130
+ [
131
+ (1, 2, 5),
132
+ (1, 3, 3),
133
+ (2, 3, 6),
134
+ (2, 6, 1),
135
+ (1, 4, 1),
136
+ (4, 5, 3),
137
+ (4, 6, 7),
138
+ (5, 6, 2),
139
+ (5, 7, 5),
140
+ (5, 8, 4),
141
+ (6, 8, 3),
142
+ ]
143
+ )
144
+ expected = [frozenset({4, 5, 6, 7, 8}), frozenset({1, 2, 3})]
145
+ assert greedy_modularity_communities(G, weight="weight") == expected
146
+
147
+ # A large weight of the edge (2, 6) causes 6 to change group, even if it shares
148
+ # only one connection with the new group and 3 with the old one.
149
+ G[2][6]["weight"] = 20
150
+ expected = [frozenset({1, 2, 3, 6}), frozenset({4, 5, 7, 8})]
151
+ assert greedy_modularity_communities(G, weight="weight") == expected
152
+
153
+
154
+ def test_greedy_modularity_communities_multigraph():
155
+ G = nx.MultiGraph()
156
+ G.add_edges_from(
157
+ [
158
+ (1, 2),
159
+ (1, 2),
160
+ (1, 3),
161
+ (2, 3),
162
+ (1, 4),
163
+ (2, 4),
164
+ (4, 5),
165
+ (5, 6),
166
+ (5, 7),
167
+ (5, 7),
168
+ (6, 7),
169
+ (7, 8),
170
+ (5, 8),
171
+ ]
172
+ )
173
+ expected = [frozenset({1, 2, 3, 4}), frozenset({5, 6, 7, 8})]
174
+ assert greedy_modularity_communities(G) == expected
175
+
176
+ # Converting (4, 5) into a multi-edge causes node 4 to change group.
177
+ G.add_edge(4, 5)
178
+ expected = [frozenset({4, 5, 6, 7, 8}), frozenset({1, 2, 3})]
179
+ assert greedy_modularity_communities(G) == expected
180
+
181
+
182
+ def test_greedy_modularity_communities_multigraph_weighted():
183
+ G = nx.MultiGraph()
184
+ G.add_weighted_edges_from(
185
+ [
186
+ (1, 2, 5),
187
+ (1, 2, 3),
188
+ (1, 3, 6),
189
+ (1, 3, 6),
190
+ (2, 3, 4),
191
+ (1, 4, 1),
192
+ (1, 4, 1),
193
+ (2, 4, 3),
194
+ (2, 4, 3),
195
+ (4, 5, 1),
196
+ (5, 6, 3),
197
+ (5, 6, 7),
198
+ (5, 6, 4),
199
+ (5, 7, 9),
200
+ (5, 7, 9),
201
+ (6, 7, 8),
202
+ (7, 8, 2),
203
+ (7, 8, 2),
204
+ (5, 8, 6),
205
+ (5, 8, 6),
206
+ ]
207
+ )
208
+ expected = [frozenset({1, 2, 3, 4}), frozenset({5, 6, 7, 8})]
209
+ assert greedy_modularity_communities(G, weight="weight") == expected
210
+
211
+ # Adding multi-edge (4, 5, 16) causes node 4 to change group.
212
+ G.add_edge(4, 5, weight=16)
213
+ expected = [frozenset({4, 5, 6, 7, 8}), frozenset({1, 2, 3})]
214
+ assert greedy_modularity_communities(G, weight="weight") == expected
215
+
216
+ # Increasing the weight of edge (1, 4) causes node 4 to return to the former group.
217
+ G[1][4][1]["weight"] = 3
218
+ expected = [frozenset({1, 2, 3, 4}), frozenset({5, 6, 7, 8})]
219
+ assert greedy_modularity_communities(G, weight="weight") == expected
220
+
221
+
222
+ def test_greed_modularity_communities_multidigraph():
223
+ G = nx.MultiDiGraph()
224
+ G.add_edges_from(
225
+ [
226
+ (1, 2),
227
+ (1, 2),
228
+ (3, 1),
229
+ (2, 3),
230
+ (2, 3),
231
+ (3, 2),
232
+ (1, 4),
233
+ (2, 4),
234
+ (4, 2),
235
+ (4, 5),
236
+ (5, 6),
237
+ (5, 6),
238
+ (6, 5),
239
+ (5, 7),
240
+ (6, 7),
241
+ (7, 8),
242
+ (5, 8),
243
+ (8, 4),
244
+ ]
245
+ )
246
+ expected = [frozenset({1, 2, 3, 4}), frozenset({5, 6, 7, 8})]
247
+ assert greedy_modularity_communities(G, weight="weight") == expected
248
+
249
+
250
+ def test_greed_modularity_communities_multidigraph_weighted():
251
+ G = nx.MultiDiGraph()
252
+ G.add_weighted_edges_from(
253
+ [
254
+ (1, 2, 5),
255
+ (1, 2, 3),
256
+ (3, 1, 6),
257
+ (1, 3, 6),
258
+ (3, 2, 4),
259
+ (1, 4, 2),
260
+ (1, 4, 5),
261
+ (2, 4, 3),
262
+ (3, 2, 8),
263
+ (4, 2, 3),
264
+ (4, 3, 5),
265
+ (4, 5, 2),
266
+ (5, 6, 3),
267
+ (5, 6, 7),
268
+ (6, 5, 4),
269
+ (5, 7, 9),
270
+ (5, 7, 9),
271
+ (7, 6, 8),
272
+ (7, 8, 2),
273
+ (8, 7, 2),
274
+ (5, 8, 6),
275
+ (5, 8, 6),
276
+ ]
277
+ )
278
+ expected = [frozenset({1, 2, 3, 4}), frozenset({5, 6, 7, 8})]
279
+ assert greedy_modularity_communities(G, weight="weight") == expected
280
+
281
+
282
+ def test_resolution_parameter_impact():
283
+ G = nx.barbell_graph(5, 3)
284
+
285
+ gamma = 1
286
+ expected = [frozenset(range(5)), frozenset(range(8, 13)), frozenset(range(5, 8))]
287
+ assert greedy_modularity_communities(G, resolution=gamma) == expected
288
+ assert naive_greedy_modularity_communities(G, resolution=gamma) == expected
289
+
290
+ gamma = 2.5
291
+ expected = [{0, 1, 2, 3}, {9, 10, 11, 12}, {5, 6, 7}, {4}, {8}]
292
+ assert greedy_modularity_communities(G, resolution=gamma) == expected
293
+ assert naive_greedy_modularity_communities(G, resolution=gamma) == expected
294
+
295
+ gamma = 0.3
296
+ expected = [frozenset(range(8)), frozenset(range(8, 13))]
297
+ assert greedy_modularity_communities(G, resolution=gamma) == expected
298
+ assert naive_greedy_modularity_communities(G, resolution=gamma) == expected
299
+
300
+
301
+ def test_cutoff_parameter():
302
+ G = nx.circular_ladder_graph(4)
303
+
304
+ # No aggregation:
305
+ expected = [{k} for k in range(8)]
306
+ assert greedy_modularity_communities(G, cutoff=8) == expected
307
+
308
+ # Aggregation to half order (number of nodes)
309
+ expected = [{k, k + 1} for k in range(0, 8, 2)]
310
+ assert greedy_modularity_communities(G, cutoff=4) == expected
311
+
312
+ # Default aggregation case (here, 2 communities emerge)
313
+ expected = [frozenset(range(4)), frozenset(range(4, 8))]
314
+ assert greedy_modularity_communities(G, cutoff=1) == expected
315
+
316
+
317
+ def test_best_n():
318
+ G = nx.barbell_graph(5, 3)
319
+
320
+ # Same result as without enforcing cutoff:
321
+ best_n = 3
322
+ expected = [frozenset(range(5)), frozenset(range(8, 13)), frozenset(range(5, 8))]
323
+ assert greedy_modularity_communities(G, best_n=best_n) == expected
324
+
325
+ # One additional merging step:
326
+ best_n = 2
327
+ expected = [frozenset(range(8)), frozenset(range(8, 13))]
328
+ assert greedy_modularity_communities(G, best_n=best_n) == expected
329
+
330
+ # Two additional merging steps:
331
+ best_n = 1
332
+ expected = [frozenset(range(13))]
333
+ assert greedy_modularity_communities(G, best_n=best_n) == expected
334
+
335
+
336
+ def test_greedy_modularity_communities_corner_cases():
337
+ G = nx.empty_graph()
338
+ assert nx.community.greedy_modularity_communities(G) == []
339
+ G.add_nodes_from(range(3))
340
+ assert nx.community.greedy_modularity_communities(G) == [{0}, {1}, {2}]
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_quality.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Unit tests for the :mod:`networkx.algorithms.community.quality`
2
+ module.
3
+
4
+ """
5
+
6
+ import pytest
7
+
8
+ import networkx as nx
9
+ from networkx import barbell_graph
10
+ from networkx.algorithms.community import modularity, partition_quality
11
+ from networkx.algorithms.community.quality import inter_community_edges
12
+
13
+
14
+ class TestPerformance:
15
+ """Unit tests for the :func:`performance` function."""
16
+
17
+ def test_bad_partition(self):
18
+ """Tests that a poor partition has a low performance measure."""
19
+ G = barbell_graph(3, 0)
20
+ partition = [{0, 1, 4}, {2, 3, 5}]
21
+ assert 8 / 15 == pytest.approx(partition_quality(G, partition)[1], abs=1e-7)
22
+
23
+ def test_good_partition(self):
24
+ """Tests that a good partition has a high performance measure."""
25
+ G = barbell_graph(3, 0)
26
+ partition = [{0, 1, 2}, {3, 4, 5}]
27
+ assert 14 / 15 == pytest.approx(partition_quality(G, partition)[1], abs=1e-7)
28
+
29
+
30
+ class TestCoverage:
31
+ """Unit tests for the :func:`coverage` function."""
32
+
33
+ def test_bad_partition(self):
34
+ """Tests that a poor partition has a low coverage measure."""
35
+ G = barbell_graph(3, 0)
36
+ partition = [{0, 1, 4}, {2, 3, 5}]
37
+ assert 3 / 7 == pytest.approx(partition_quality(G, partition)[0], abs=1e-7)
38
+
39
+ def test_good_partition(self):
40
+ """Tests that a good partition has a high coverage measure."""
41
+ G = barbell_graph(3, 0)
42
+ partition = [{0, 1, 2}, {3, 4, 5}]
43
+ assert 6 / 7 == pytest.approx(partition_quality(G, partition)[0], abs=1e-7)
44
+
45
+
46
+ def test_modularity():
47
+ G = nx.barbell_graph(3, 0)
48
+ C = [{0, 1, 4}, {2, 3, 5}]
49
+ assert (-16 / (14**2)) == pytest.approx(modularity(G, C), abs=1e-7)
50
+ C = [{0, 1, 2}, {3, 4, 5}]
51
+ assert (35 * 2) / (14**2) == pytest.approx(modularity(G, C), abs=1e-7)
52
+
53
+ n = 1000
54
+ G = nx.erdos_renyi_graph(n, 0.09, seed=42, directed=True)
55
+ C = [set(range(n // 2)), set(range(n // 2, n))]
56
+ assert 0.00017154251389292754 == pytest.approx(modularity(G, C), abs=1e-7)
57
+
58
+ G = nx.margulis_gabber_galil_graph(10)
59
+ mid_value = G.number_of_nodes() // 2
60
+ nodes = list(G.nodes)
61
+ C = [set(nodes[:mid_value]), set(nodes[mid_value:])]
62
+ assert 0.13 == pytest.approx(modularity(G, C), abs=1e-7)
63
+
64
+ G = nx.DiGraph()
65
+ G.add_edges_from([(2, 1), (2, 3), (3, 4)])
66
+ C = [{1, 2}, {3, 4}]
67
+ assert 2 / 9 == pytest.approx(modularity(G, C), abs=1e-7)
68
+
69
+
70
+ def test_modularity_resolution():
71
+ G = nx.barbell_graph(3, 0)
72
+ C = [{0, 1, 4}, {2, 3, 5}]
73
+ assert modularity(G, C) == pytest.approx(3 / 7 - 100 / 14**2)
74
+ gamma = 2
75
+ result = modularity(G, C, resolution=gamma)
76
+ assert result == pytest.approx(3 / 7 - gamma * 100 / 14**2)
77
+ gamma = 0.2
78
+ result = modularity(G, C, resolution=gamma)
79
+ assert result == pytest.approx(3 / 7 - gamma * 100 / 14**2)
80
+
81
+ C = [{0, 1, 2}, {3, 4, 5}]
82
+ assert modularity(G, C) == pytest.approx(6 / 7 - 98 / 14**2)
83
+ gamma = 2
84
+ result = modularity(G, C, resolution=gamma)
85
+ assert result == pytest.approx(6 / 7 - gamma * 98 / 14**2)
86
+ gamma = 0.2
87
+ result = modularity(G, C, resolution=gamma)
88
+ assert result == pytest.approx(6 / 7 - gamma * 98 / 14**2)
89
+
90
+ G = nx.barbell_graph(5, 3)
91
+ C = [frozenset(range(5)), frozenset(range(8, 13)), frozenset(range(5, 8))]
92
+ gamma = 1
93
+ result = modularity(G, C, resolution=gamma)
94
+ # This C is maximal for gamma=1: modularity = 0.518229
95
+ assert result == pytest.approx((22 / 24) - gamma * (918 / (48**2)))
96
+ gamma = 2
97
+ result = modularity(G, C, resolution=gamma)
98
+ assert result == pytest.approx((22 / 24) - gamma * (918 / (48**2)))
99
+ gamma = 0.2
100
+ result = modularity(G, C, resolution=gamma)
101
+ assert result == pytest.approx((22 / 24) - gamma * (918 / (48**2)))
102
+
103
+ C = [{0, 1, 2, 3}, {9, 10, 11, 12}, {5, 6, 7}, {4}, {8}]
104
+ gamma = 1
105
+ result = modularity(G, C, resolution=gamma)
106
+ assert result == pytest.approx((14 / 24) - gamma * (598 / (48**2)))
107
+ gamma = 2.5
108
+ result = modularity(G, C, resolution=gamma)
109
+ # This C is maximal for gamma=2.5: modularity = -0.06553819
110
+ assert result == pytest.approx((14 / 24) - gamma * (598 / (48**2)))
111
+ gamma = 0.2
112
+ result = modularity(G, C, resolution=gamma)
113
+ assert result == pytest.approx((14 / 24) - gamma * (598 / (48**2)))
114
+
115
+ C = [frozenset(range(8)), frozenset(range(8, 13))]
116
+ gamma = 1
117
+ result = modularity(G, C, resolution=gamma)
118
+ assert result == pytest.approx((23 / 24) - gamma * (1170 / (48**2)))
119
+ gamma = 2
120
+ result = modularity(G, C, resolution=gamma)
121
+ assert result == pytest.approx((23 / 24) - gamma * (1170 / (48**2)))
122
+ gamma = 0.3
123
+ result = modularity(G, C, resolution=gamma)
124
+ # This C is maximal for gamma=0.3: modularity = 0.805990
125
+ assert result == pytest.approx((23 / 24) - gamma * (1170 / (48**2)))
126
+
127
+
128
+ def test_inter_community_edges_with_digraphs():
129
+ G = nx.complete_graph(2, create_using=nx.DiGraph())
130
+ partition = [{0}, {1}]
131
+ assert inter_community_edges(G, partition) == 2
132
+
133
+ G = nx.complete_graph(10, create_using=nx.DiGraph())
134
+ partition = [{0}, {1, 2}, {3, 4, 5}, {6, 7, 8, 9}]
135
+ assert inter_community_edges(G, partition) == 70
136
+
137
+ G = nx.cycle_graph(4, create_using=nx.DiGraph())
138
+ partition = [{0, 1}, {2, 3}]
139
+ assert inter_community_edges(G, partition) == 2
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/community/tests/test_utils.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Unit tests for the :mod:`networkx.algorithms.community.utils` module."""
2
+
3
+ import networkx as nx
4
+
5
+
6
+ def test_is_partition():
7
+ G = nx.empty_graph(3)
8
+ assert nx.community.is_partition(G, [{0, 1}, {2}])
9
+ assert nx.community.is_partition(G, ({0, 1}, {2}))
10
+ assert nx.community.is_partition(G, ([0, 1], [2]))
11
+ assert nx.community.is_partition(G, [[0, 1], [2]])
12
+
13
+
14
+ def test_not_covering():
15
+ G = nx.empty_graph(3)
16
+ assert not nx.community.is_partition(G, [{0}, {1}])
17
+
18
+
19
+ def test_not_disjoint():
20
+ G = nx.empty_graph(3)
21
+ assert not nx.community.is_partition(G, [{0, 1}, {1, 2}])
22
+
23
+
24
+ def test_not_node():
25
+ G = nx.empty_graph(3)
26
+ assert not nx.community.is_partition(G, [{0, 1}, {3}])
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/components/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (374 Bytes). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/components/__pycache__/attracting.cpython-312.pyc ADDED
Binary file (3.74 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/components/__pycache__/biconnected.cpython-312.pyc ADDED
Binary file (13.1 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/networkx/algorithms/components/__pycache__/connected.cpython-312.pyc ADDED
Binary file (5.89 kB). View file