Coverage for tests\clustering\test_spectral.py: 100.00%
73 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-17 13:31 +0100
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-17 13:31 +0100
1# -*- coding: utf-8 -*-
3"""
4* Name: interactive-clustering/tests/clustering/test_spectral.py
5* Description: Unittests for the `clustering.spectral` module.
6* Author: Erwan SCHILD
7* Created: 17/03/2021
8* Licence: CeCILL (https://cecill.info/licences.fr.html)
9"""
11# ==============================================================================
12# IMPORT PYTHON DEPENDENCIES
13# ==============================================================================
15import numpy as np
16import pytest
17from scipy.sparse import csr_matrix
19from cognitivefactory.interactive_clustering.clustering.spectral import SpectralConstrainedClustering
20from cognitivefactory.interactive_clustering.constraints.binary import BinaryConstraintsManager
23# ==============================================================================
24# test_SpectralConstrainedClustering_for_inconsistent_model
25# ==============================================================================
26def test_SpectralConstrainedClustering_for_inconsistent_model():
27 """
28 Test that the `clustering.spectral.SpectralConstrainedClustering` initialization raises an `ValueError` for inconsistent `model` parameter.
29 """
31 # Check `ValueError` for bad string value for `model`.
32 with pytest.raises(ValueError, match="`model`"):
33 SpectralConstrainedClustering(
34 model="as_you_want",
35 )
38# ==============================================================================
39# test_SpectralConstrainedClustering_for_inconsistent_nb_components
40# ==============================================================================
41def test_SpectralConstrainedClustering_for_inconsistent_nb_components():
42 """
43 Test that the `clustering.spectral.SpectralConstrainedClustering` initialization raises an `ValueError` for inconsistent `nb_components` parameter.
44 """
46 # Check `ValueError` for bad string value for `nb_components`.
47 with pytest.raises(ValueError, match="`nb_components`"):
48 SpectralConstrainedClustering(
49 nb_components=-1,
50 )
53# ==============================================================================
54# test_SpectralConstrainedClustering_for_correct_settings
55# ==============================================================================
56def test_SpectralConstrainedClustering_for_correct_settings():
57 """
58 Test that the `clustering.spectral.SpectralConstrainedClustering` initialization runs correctly with the correct settings.
59 """
61 # Check a correct initialization.
62 clustering_model = SpectralConstrainedClustering(
63 model="SPEC",
64 nb_components=100,
65 random_seed=3,
66 )
67 assert clustering_model
68 assert clustering_model.model == "SPEC"
69 assert clustering_model.nb_components == 100
70 assert clustering_model.random_seed == 3
73# ==============================================================================
74# test_SpectralConstrainedClustering_cluster_for_inconsistent_constraints_manager
75# ==============================================================================
76def test_SpectralConstrainedClustering_cluster_for_inconsistent_constraints_manager():
77 """
78 Test that the `clustering.spectral.SpectralConstrainedClustering` clustering raises an `ValueError` for inconsistent `constraints_manager` parameter.
79 """
81 # Initialize a `SpectralConstrainedClustering` instance.
82 clustering_model = SpectralConstrainedClustering()
84 # Check `ValueError` for not matrix `vectors`.
85 with pytest.raises(ValueError, match="`constraints_manager`"):
86 clustering_model.cluster(
87 constraints_manager=None,
88 vectors=None,
89 nb_clusters=2,
90 )
93# ==============================================================================
94# test_SpectralConstrainedClustering_cluster_for_inconsistent_vectors
95# ==============================================================================
96def test_SpectralConstrainedClustering_cluster_for_inconsistent_vectors():
97 """
98 Test that the `clustering.spectral.SpectralConstrainedClustering` clustering raises an `ValueError` for inconsistent `vectors` parameter.
99 """
101 # Initialize a `SpectralConstrainedClustering` instance.
102 clustering_model = SpectralConstrainedClustering()
104 # Check `ValueError` for not matrix `vectors`.
105 with pytest.raises(ValueError, match="`vectors`"):
106 clustering_model.cluster(
107 constraints_manager=BinaryConstraintsManager(list_of_data_IDs=["first", "second", "third"]),
108 vectors=None,
109 nb_clusters=2,
110 )
113# ==============================================================================
114# test_SpectralConstrainedClustering_cluster_for_inconsistent_nb_clusters_1
115# ==============================================================================
116def test_SpectralConstrainedClustering_cluster_for_inconsistent_nb_clusters_1():
117 """
118 Test that the `clustering.spectral.SpectralConstrainedClustering` clustering raises an `ValueError` for inconsistent `nb_clusters` parameter.
119 """
121 # Initialize a `SpectralConstrainedClustering` instance.
122 clustering_model = SpectralConstrainedClustering()
124 # Check `ValueError` for too small `nb_clusters`.
125 with pytest.raises(ValueError, match="`nb_clusters`"):
126 clustering_model.cluster(
127 constraints_manager=BinaryConstraintsManager(list_of_data_IDs=["first", "second", "third"]),
128 vectors={"first": np.array([1, 2, 3]), "second": np.array([[4, 5, 6]]), "third": csr_matrix([7, 8, 9])},
129 nb_clusters=None,
130 )
133# ==============================================================================
134# test_SpectralConstrainedClustering_cluster_for_inconsistent_nb_clusters_2
135# ==============================================================================
136def test_SpectralConstrainedClustering_cluster_for_inconsistent_nb_clusters_2():
137 """
138 Test that the `clustering.spectral.SpectralConstrainedClustering` clustering raises an `ValueError` for inconsistent `nb_clusters` parameter.
139 """
141 # Initialize a `SpectralConstrainedClustering` instance.
142 clustering_model = SpectralConstrainedClustering()
144 # Check `ValueError` for too small `nb_clusters`.
145 with pytest.raises(ValueError, match="`nb_clusters`"):
146 clustering_model.cluster(
147 constraints_manager=BinaryConstraintsManager(list_of_data_IDs=["first", "second", "third"]),
148 vectors={"first": np.array([1, 2, 3]), "second": np.array([[4, 5, 6]]), "third": csr_matrix([7, 8, 9])},
149 nb_clusters=-1,
150 )
153# ==============================================================================
154# test_SpectralConstrainedClustering_cluster_model_SPEC_with_no_constraints
155# ==============================================================================
156def test_SpectralConstrainedClustering_cluster_model_SPEC_with_no_constraints():
157 """
158 Test that the `clustering.spectral.SpectralConstrainedClustering` clustering works with SPEC `model` and no constraints.
159 """
161 # Define `vectors` and `constraints_manager`
162 vectors = {
163 "0": csr_matrix([1.00, 0.00, 0.00, 0.00]),
164 "1": csr_matrix([0.95, 0.02, 0.02, 0.01]),
165 "2": csr_matrix([0.98, 0.00, 0.02, 0.00]),
166 "3": csr_matrix([0.99, 0.00, 0.01, 0.00]),
167 "4": csr_matrix([0.60, 0.17, 0.16, 0.07]),
168 "5": csr_matrix([0.60, 0.16, 0.17, 0.07]),
169 "6": csr_matrix([0.01, 0.01, 0.01, 0.97]),
170 "7": csr_matrix([0.00, 0.01, 0.00, 0.99]),
171 "8": csr_matrix([0.00, 0.00, 0.00, 1.00]),
172 }
173 constraints_manager = BinaryConstraintsManager(list_of_data_IDs=list(vectors.keys()))
175 # Initialize a `SpectralConstrainedClustering` instance.
176 clustering_model = SpectralConstrainedClustering(
177 model="SPEC",
178 random_seed=1,
179 )
181 # Run clustering 3 clusters and no constraints.
182 dict_of_predicted_clusters = clustering_model.cluster(
183 constraints_manager=constraints_manager,
184 vectors=vectors,
185 nb_clusters=3,
186 )
188 assert clustering_model.dict_of_predicted_clusters
189 assert dict_of_predicted_clusters == {
190 "0": 0,
191 "1": 0,
192 "2": 0,
193 "3": 0,
194 "4": 1,
195 "5": 1,
196 "6": 2,
197 "7": 2,
198 "8": 2,
199 }
202# ==============================================================================
203# test_SpectralConstrainedClustering_cluster_model_SPEC_with_some_constraints
204# ==============================================================================
205def test_SpectralConstrainedClustering_cluster_model_SPEC_with_some_constraints():
206 """
207 Test that the `clustering.spectral.SpectralConstrainedClustering` clustering works with SPEC `model` and some constraints.
208 """
210 # Define `vectors` and `constraints_manager`
211 vectors = {
212 "0": csr_matrix([1.00, 0.00, 0.00, 0.00]),
213 "1": csr_matrix([0.95, 0.02, 0.02, 0.01]),
214 "2": csr_matrix([0.98, 0.00, 0.02, 0.00]),
215 "3": csr_matrix([0.99, 0.00, 0.01, 0.00]),
216 "4": csr_matrix([0.60, 0.17, 0.16, 0.07]),
217 "5": csr_matrix([0.60, 0.16, 0.17, 0.07]),
218 "6": csr_matrix([0.01, 0.01, 0.01, 0.97]),
219 "7": csr_matrix([0.00, 0.01, 0.00, 0.99]),
220 "8": csr_matrix([0.00, 0.00, 0.00, 1.00]),
221 }
222 constraints_manager = BinaryConstraintsManager(list_of_data_IDs=["0", "1", "2", "3", "4", "5", "6", "7", "8"])
223 constraints_manager.add_constraint(data_ID1="0", data_ID2="1", constraint_type="MUST_LINK")
224 constraints_manager.add_constraint(data_ID1="2", data_ID2="3", constraint_type="MUST_LINK")
225 constraints_manager.add_constraint(data_ID1="4", data_ID2="5", constraint_type="MUST_LINK")
226 constraints_manager.add_constraint(data_ID1="7", data_ID2="8", constraint_type="MUST_LINK")
227 constraints_manager.add_constraint(data_ID1="0", data_ID2="4", constraint_type="CANNOT_LINK")
228 constraints_manager.add_constraint(data_ID1="2", data_ID2="4", constraint_type="CANNOT_LINK")
229 constraints_manager.add_constraint(data_ID1="4", data_ID2="7", constraint_type="CANNOT_LINK")
231 # Initialize a `SpectralConstrainedClustering` instance.
232 clustering_model = SpectralConstrainedClustering(
233 model="SPEC",
234 random_seed=1,
235 )
237 # Run clustering 3 clusters and some constraints.
238 dict_of_predicted_clusters = clustering_model.cluster(
239 constraints_manager=constraints_manager,
240 vectors=vectors,
241 nb_clusters=3,
242 )
244 assert clustering_model.dict_of_predicted_clusters
245 assert dict_of_predicted_clusters == {
246 "0": 0,
247 "1": 0,
248 "2": 0,
249 "3": 0,
250 "4": 1,
251 "5": 1,
252 "6": 2,
253 "7": 2,
254 "8": 2,
255 }
258# ==============================================================================
259# test_SpectralConstrainedClustering_cluster_model_SPEC_with_full_constraints
260# ==============================================================================
261def test_SpectralConstrainedClustering_cluster_model_SPEC_with_full_constraints():
262 """
263 Test that the `clustering.spectral.SpectralConstrainedClustering` clustering works with SPEC `model` and full constraints.
264 """
266 # Define `vectors` and `constraints_manager`
267 vectors = {
268 "0": csr_matrix([1.00, 0.00, 0.00, 0.00]),
269 "1": csr_matrix([0.95, 0.02, 0.02, 0.01]),
270 "2": csr_matrix([0.98, 0.00, 0.02, 0.00]),
271 "3": csr_matrix([0.99, 0.00, 0.01, 0.00]),
272 "4": csr_matrix([0.60, 0.17, 0.16, 0.07]),
273 "5": csr_matrix([0.60, 0.16, 0.17, 0.07]),
274 "6": csr_matrix([0.01, 0.01, 0.01, 0.97]),
275 "7": csr_matrix([0.00, 0.01, 0.00, 0.99]),
276 "8": csr_matrix([0.00, 0.00, 0.00, 1.00]),
277 }
278 constraints_manager = BinaryConstraintsManager(list_of_data_IDs=["0", "1", "2", "3", "4", "5", "6", "7", "8"])
279 constraints_manager.add_constraint(data_ID1="0", data_ID2="4", constraint_type="MUST_LINK")
280 constraints_manager.add_constraint(data_ID1="0", data_ID2="8", constraint_type="MUST_LINK")
281 constraints_manager.add_constraint(data_ID1="1", data_ID2="5", constraint_type="MUST_LINK")
282 constraints_manager.add_constraint(data_ID1="2", data_ID2="6", constraint_type="MUST_LINK")
283 constraints_manager.add_constraint(data_ID1="3", data_ID2="7", constraint_type="MUST_LINK")
284 constraints_manager.add_constraint(data_ID1="0", data_ID2="1", constraint_type="CANNOT_LINK")
285 constraints_manager.add_constraint(data_ID1="0", data_ID2="2", constraint_type="CANNOT_LINK")
286 constraints_manager.add_constraint(data_ID1="0", data_ID2="3", constraint_type="CANNOT_LINK")
287 constraints_manager.add_constraint(data_ID1="1", data_ID2="2", constraint_type="CANNOT_LINK")
288 constraints_manager.add_constraint(data_ID1="1", data_ID2="3", constraint_type="CANNOT_LINK")
289 constraints_manager.add_constraint(data_ID1="2", data_ID2="3", constraint_type="CANNOT_LINK")
291 # Initialize a `SpectralConstrainedClustering` instance.
292 clustering_model = SpectralConstrainedClustering(
293 model="SPEC",
294 random_seed=1,
295 )
297 # Run clustering 3 clusters and full constraints.
298 dict_of_predicted_clusters = clustering_model.cluster(
299 constraints_manager=constraints_manager,
300 vectors=vectors,
301 nb_clusters=4,
302 )
304 assert clustering_model.dict_of_predicted_clusters
305 assert dict_of_predicted_clusters == {
306 "0": 0,
307 "1": 1,
308 "2": 2,
309 "3": 3,
310 "4": 0,
311 "5": 1,
312 "6": 2,
313 "7": 3,
314 "8": 0,
315 }