-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added functionality for generation of test case for BatchParallelKmeans
- Loading branch information
Hoppe
committed
Dec 12, 2023
1 parent
8e8390b
commit 2a2c8a9
Showing
2 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import heat as ht | ||
import unittest | ||
import torch | ||
from heat.core.tests.test_suites.basic_test import TestCase | ||
|
||
|
||
class TestCreateClusters(TestCase): | ||
def test_create_cluster(self): | ||
n_samples = ht.MPI_WORLD.size * 10 + 3 | ||
n_features = 3 | ||
n_clusters = ht.MPI_WORLD.size | ||
cluster_mean = torch.arange(n_clusters, dtype=torch.float32).repeat(n_features, 1).T | ||
|
||
# test case with uneven distribution of clusters over processes and variances given as vector | ||
cluster_weight = torch.zeros(n_clusters) | ||
cluster_weight[ht.MPI_WORLD.rank] += 0.5 | ||
cluster_weight[0] += 0.5 | ||
cluster_std = 0.01 * torch.ones(n_clusters) | ||
data = ht.utils.data.spherical.create_clusters( | ||
n_samples, n_features, n_clusters, cluster_mean, cluster_std, cluster_weight | ||
) | ||
self.assertEqual(data.shape, (n_samples, n_features)) | ||
self.assertEqual(data.dtype, ht.float32) | ||
|
||
# test case with even distribution of clusters over processes and variances given as matrix | ||
cluster_weight = None | ||
cluster_std = 0.01 * torch.rand(n_clusters, n_features, n_features) | ||
cluster_std = torch.transpose(cluster_std, 1, 2) @ cluster_std | ||
data = ht.utils.data.spherical.create_clusters( | ||
n_samples, n_features, n_clusters, cluster_mean, cluster_std, cluster_weight | ||
) | ||
self.assertEqual(data.shape, (n_samples, n_features)) | ||
self.assertEqual(data.dtype, ht.float32) | ||
|
||
def test_if_errors_are_catched(self): | ||
n_samples = ht.MPI_WORLD.size * 10 + 3 | ||
n_features = 3 | ||
n_clusters = ht.MPI_WORLD.size | ||
cluster_mean = torch.arange(n_clusters, dtype=torch.float32).repeat(n_features, 1).T | ||
cluster_std = 0.01 * torch.ones(n_clusters) | ||
|
||
with self.assertRaises(TypeError): | ||
ht.utils.data.spherical.create_clusters( | ||
n_samples, n_features, n_clusters, "abc", cluster_std | ||
) | ||
with self.assertRaises(ValueError): | ||
ht.utils.data.spherical.create_clusters( | ||
n_samples, n_features, n_clusters, torch.zeros(2, 2), cluster_std | ||
) | ||
with self.assertRaises(TypeError): | ||
ht.utils.data.spherical.create_clusters( | ||
n_samples, n_features, n_clusters, cluster_mean, "abc" | ||
) | ||
with self.assertRaises(ValueError): | ||
ht.utils.data.spherical.create_clusters( | ||
n_samples, n_features, n_clusters, cluster_mean, torch.zeros(2, 2) | ||
) | ||
with self.assertRaises(TypeError): | ||
ht.utils.data.spherical.create_clusters( | ||
n_samples, n_features, n_clusters, cluster_mean, cluster_std, "abc" | ||
) | ||
with self.assertRaises(ValueError): | ||
ht.utils.data.spherical.create_clusters( | ||
n_samples, | ||
n_features, | ||
n_clusters, | ||
cluster_mean, | ||
cluster_std, | ||
torch.ones( | ||
n_clusters + 1, | ||
), | ||
) | ||
with self.assertRaises(ValueError): | ||
ht.utils.data.spherical.create_clusters( | ||
n_samples, | ||
n_features, | ||
n_clusters, | ||
cluster_mean, | ||
cluster_std, | ||
2 | ||
* torch.ones( | ||
n_clusters, | ||
), | ||
) |