|
1 | | -import unittest |
2 | | - |
3 | 1 | import numpy as np |
| 2 | +import pytest |
4 | 3 |
|
5 | 4 | from skmatter.clustering import QuickShift |
6 | 5 |
|
7 | 6 |
|
8 | | -class QuickShiftTests(unittest.TestCase): |
9 | | - @classmethod |
10 | | - def setUpClass(cls) -> None: |
11 | | - cls.points = np.array( |
12 | | - [ |
13 | | - [-1.72779275, -1.32763554], |
14 | | - [-4.44991964, -2.13474901], |
15 | | - [0.54817734, -2.43319467], |
16 | | - [3.19881307, -0.49547222], |
17 | | - [-1.1335991, 2.33478428], |
18 | | - [0.55437388, 0.18745963], |
19 | | - ] |
20 | | - ) |
21 | | - cls.cuts = np.array( |
22 | | - [6.99485011, 8.80292681, 7.68486852, 9.5115009, 8.07736919, 6.22057056] |
23 | | - ) |
24 | | - cls.weights = np.array( |
25 | | - [ |
26 | | - -3.94008092, |
27 | | - -12.68095664, |
28 | | - -7.07512499, |
29 | | - -9.03064023, |
30 | | - -8.26529849, |
31 | | - -2.61132267, |
32 | | - ] |
33 | | - ) |
34 | | - cls.qs_labels_ = np.array([0, 0, 0, 5, 5, 5]) |
35 | | - cls.qs_cluster_centers_idx_ = np.array([0, 5]) |
36 | | - cls.gabriel_labels_ = np.array([5, 5, 5, 5, 5, 5]) |
37 | | - cls.gabriel_cluster_centers_idx_ = np.array([5]) |
38 | | - cls.cell = [3, 3] |
39 | | - cls.gabriel_shell = 2 |
40 | | - |
41 | | - def test_fit_qs(self): |
42 | | - model = QuickShift(dist_cutoff_sq=self.cuts) |
43 | | - model.fit(self.points, samples_weight=self.weights) |
44 | | - self.assertTrue(np.all(model.labels_ == self.qs_labels_)) |
45 | | - self.assertTrue( |
46 | | - np.all(model.cluster_centers_idx_ == self.qs_cluster_centers_idx_) |
47 | | - ) |
48 | | - |
49 | | - def test_fit_garbriel(self): |
50 | | - model = QuickShift(gabriel_shell=self.gabriel_shell) |
51 | | - model.fit(self.points, samples_weight=self.weights) |
52 | | - self.assertTrue(np.all(model.labels_ == self.gabriel_labels_)) |
53 | | - self.assertTrue( |
54 | | - np.all(model.cluster_centers_idx_ == self.gabriel_cluster_centers_idx_) |
55 | | - ) |
56 | | - |
57 | | - def test_dimension_check(self): |
58 | | - model = QuickShift(self.cuts, metric_params={"cell_length": self.cell}) |
59 | | - self.assertRaises(ValueError, model.fit, np.array([[2]])) |
| 7 | +@pytest.fixture(scope="module") |
| 8 | +def test_data(): |
| 9 | + points = np.array( |
| 10 | + [ |
| 11 | + [-1.72779275, -1.32763554], |
| 12 | + [-4.44991964, -2.13474901], |
| 13 | + [0.54817734, -2.43319467], |
| 14 | + [3.19881307, -0.49547222], |
| 15 | + [-1.1335991, 2.33478428], |
| 16 | + [0.55437388, 0.18745963], |
| 17 | + ] |
| 18 | + ) |
| 19 | + cuts = np.array( |
| 20 | + [6.99485011, 8.80292681, 7.68486852, 9.5115009, 8.07736919, 6.22057056] |
| 21 | + ) |
| 22 | + weights = np.array( |
| 23 | + [ |
| 24 | + -3.94008092, |
| 25 | + -12.68095664, |
| 26 | + -7.07512499, |
| 27 | + -9.03064023, |
| 28 | + -8.26529849, |
| 29 | + -2.61132267, |
| 30 | + ] |
| 31 | + ) |
| 32 | + qs_labels_ = np.array([0, 0, 0, 5, 5, 5]) |
| 33 | + qs_cluster_centers_idx_ = np.array([0, 5]) |
| 34 | + gabriel_labels_ = np.array([5, 5, 5, 5, 5, 5]) |
| 35 | + gabriel_cluster_centers_idx_ = np.array([5]) |
| 36 | + cell = [3, 3] |
| 37 | + gabriel_shell = 2 |
| 38 | + |
| 39 | + return { |
| 40 | + "points": points, |
| 41 | + "cuts": cuts, |
| 42 | + "weights": weights, |
| 43 | + "qs_labels_": qs_labels_, |
| 44 | + "qs_cluster_centers_idx_": qs_cluster_centers_idx_, |
| 45 | + "gabriel_labels_": gabriel_labels_, |
| 46 | + "gabriel_cluster_centers_idx_": gabriel_cluster_centers_idx_, |
| 47 | + "cell": cell, |
| 48 | + "gabriel_shell": gabriel_shell, |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | +def test_fit_qs(test_data): |
| 53 | + model = QuickShift(dist_cutoff_sq=test_data["cuts"]) |
| 54 | + model.fit(test_data["points"], samples_weight=test_data["weights"]) |
| 55 | + assert np.all(model.labels_ == test_data["qs_labels_"]) |
| 56 | + assert np.all(model.cluster_centers_idx_ == test_data["qs_cluster_centers_idx_"]) |
| 57 | + |
| 58 | + |
| 59 | +def test_fit_garbriel(test_data): |
| 60 | + model = QuickShift(gabriel_shell=test_data["gabriel_shell"]) |
| 61 | + model.fit(test_data["points"], samples_weight=test_data["weights"]) |
| 62 | + assert np.all(model.labels_ == test_data["gabriel_labels_"]) |
| 63 | + assert np.all( |
| 64 | + model.cluster_centers_idx_ == test_data["gabriel_cluster_centers_idx_"] |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +def test_dimension_check(test_data): |
| 69 | + model = QuickShift( |
| 70 | + test_data["cuts"], metric_params={"cell_length": test_data["cell"]} |
| 71 | + ) |
| 72 | + with pytest.raises(ValueError): |
| 73 | + model.fit(np.array([[2]])) |
0 commit comments