Coverage for lasso/dimred/svd/test_clustering_betas.py: 100%
27 statements
« prev ^ index » next coverage.py v7.2.4, created at 2023-04-28 18:42 +0100
« prev ^ index » next coverage.py v7.2.4, created at 2023-04-28 18:42 +0100
1from unittest import TestCase
2import numpy as np
3from lasso.dimred.svd.clustering_betas import group_betas
4from lasso.dimred.svd.keyword_types import DetectorType, ClusterType
7class TestClustering(TestCase):
8 def test_group_betas(self):
9 """tests correct function of the group_betas function
10 in clustering_betas.py"""
12 fake_names = np.array(["betas_{i}".format(i=i) for i in range(25)])
13 fake_cluster_0 = np.random.rand(12, 3) + 5
14 fake_cluster_1 = np.random.rand(12, 3) - 5
15 fake_betas = np.stack([*fake_cluster_0, *fake_cluster_1, np.array([0, 0, 0])])
16 expected_clusters = 2
17 expected_outliers = 1
19 # test with recommended settings
20 beta_clusters, name_clusters = group_betas(
21 fake_names,
22 fake_betas,
23 cluster=ClusterType.KMeans,
24 detector=DetectorType.LocalOutlierFactor,
25 cluster_params=dict(
26 n_clusters=expected_clusters,
27 ),
28 )
30 # verify correct type of output
31 self.assertIsInstance(beta_clusters, list)
32 self.assertIsInstance(name_clusters, list)
34 # verify that beta_clusters and name_clusters correspond to each other
35 self.assertEqual(len(beta_clusters), len(name_clusters))
36 # verify that beta_clusters contains as many clusters as searched for
37 # inkluding one outlier
38 self.assertEqual(len(beta_clusters), expected_clusters + expected_outliers)
40 # verify that entries correspond to each other
41 for c, cluster in enumerate(name_clusters):
42 for e, entry in enumerate(cluster):
43 index = np.where(fake_names == entry)[0]
44 self.assertTrue((fake_betas[index] - beta_clusters[c][e]).max() == 0)
46 # verify differen keyword kombinations
48 for cluster_type in ClusterType.get_cluster_type_name():
49 for detector_type in DetectorType.get_detector_type_name():
50 beta_clusters, name_clusters = group_betas(
51 fake_names, fake_betas, cluster=cluster_type, detector=detector_type
52 )
54 # verify correct output
55 self.assertIsInstance(beta_clusters, list)
56 self.assertIsInstance(name_clusters, list)
57 self.assertEqual(len(beta_clusters), len(name_clusters))