Skip to content

Commit 5cfae37

Browse files
committed
minor refactoring of Algorithm class
1 parent 0a38388 commit 5cfae37

File tree

2 files changed

+65
-54
lines changed

2 files changed

+65
-54
lines changed

src/Algorithm.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
class Algorithm implements AlgorithmInterface
1515
{
1616
private InitializationSchemeInterface $initScheme;
17-
/** @var array<callable> */
18-
private array $iterationCallbacks = [];
1917

18+
/**
19+
* @var array<callable>
20+
*/
21+
private array $iterationCallbacks = [];
2022

2123
public function __construct(InitializationSchemeInterface $initScheme)
2224
{
@@ -46,7 +48,19 @@ public function clusterize(PointCollectionInterface $points, int $nbClusters): C
4648
return $clusters;
4749
}
4850

49-
protected function iterate(ClusterCollectionInterface $clusters): bool
51+
protected function getDistanceBetween(PointInterface $pointA, PointInterface $pointB): float
52+
{
53+
return Math::euclideanDist($pointA->getCoordinates(), $pointB->getCoordinates());
54+
}
55+
56+
protected function findCentroid(PointCollectionInterface $points): PointInterface
57+
{
58+
return new Point($points->getSpace(), Math::centroid(
59+
array_map(fn (PointInterface $point) => $point->getCoordinates(), iterator_to_array($points))
60+
));
61+
}
62+
63+
private function iterate(ClusterCollectionInterface $clusters): bool
5064
{
5165
/** @var \SplObjectStorage<ClusterInterface, null> */
5266
$changed = new \SplObjectStorage();
@@ -78,7 +92,7 @@ protected function iterate(ClusterCollectionInterface $clusters): bool
7892
return count($changed) > 0;
7993
}
8094

81-
protected function getClosestCluster(ClusterCollectionInterface $clusters, PointInterface $point): ClusterInterface
95+
private function getClosestCluster(ClusterCollectionInterface $clusters, PointInterface $point): ClusterInterface
8296
{
8397
$min = null;
8498
$closest = null;
@@ -96,18 +110,6 @@ protected function getClosestCluster(ClusterCollectionInterface $clusters, Point
96110
return $closest;
97111
}
98112

99-
protected function getDistanceBetween(PointInterface $pointA, PointInterface $pointB): float
100-
{
101-
return Math::euclideanDist($pointA->getCoordinates(), $pointB->getCoordinates());
102-
}
103-
104-
protected function findCentroid(PointCollectionInterface $points): PointInterface
105-
{
106-
return new Point($points->getSpace(), Math::centroid(
107-
array_map(fn ($point) => $point->getCoordinates(), iterator_to_array($points))
108-
));
109-
}
110-
111113
protected function invokeIterationCallbacks(ClusterCollectionInterface $clusters): void
112114
{
113115
foreach ($this->iterationCallbacks as $callback) {

tests/Unit/AlgorithmTest.php

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public function tearDown(): void
4040
Mockery::close();
4141
}
4242

43+
// ------------------------------------------------------------------------
44+
// tests
45+
4346
/**
4447
* @dataProvider clusterizeDataProvider
4548
* @covers ::__construct
@@ -102,44 +105,6 @@ public function testClusterize(
102105
}
103106
}
104107

105-
/**
106-
* @return array<mixed>
107-
*/
108-
public function clusterizeDataProvider(): array
109-
{
110-
return [
111-
'one dimension, 3 clusters, 5 points per cluster' => [
112-
'dimension' => 1,
113-
'expected' => [
114-
[-50],
115-
[0],
116-
[50],
117-
],
118-
'initialClusterCentroids' => [
119-
[-10],
120-
[0],
121-
[10]
122-
],
123-
'nbPointsPerCentroid' => 5,
124-
],
125-
126-
'two dimensions, 3 clusters, 50 points per cluster' => [
127-
'dimension' => 2,
128-
'expected' => [
129-
[20, 10],
130-
[40, 20],
131-
[60, 15],
132-
],
133-
'initialClusterCentroids' => [
134-
[12, 10],
135-
[33, 20],
136-
[60, 10],
137-
],
138-
'nbPointsPerCentroid' => 50,
139-
],
140-
];
141-
}
142-
143108
/**
144109
* @covers ::__construct
145110
* @covers ::clusterize
@@ -198,6 +163,50 @@ function (AlgorithmInterface $algo, ClusterCollectionInterface $cluster) use (&$
198163
$this->assertTrue($callbackCalled);
199164
}
200165

166+
// ------------------------------------------------------------------------
167+
// data-providers
168+
169+
/**
170+
* @return array<mixed>
171+
*/
172+
public function clusterizeDataProvider(): array
173+
{
174+
return [
175+
'one dimension, 3 clusters, 5 points per cluster' => [
176+
'dimension' => 1,
177+
'expected' => [
178+
[-50],
179+
[0],
180+
[50],
181+
],
182+
'initialClusterCentroids' => [
183+
[-10],
184+
[0],
185+
[10]
186+
],
187+
'nbPointsPerCentroid' => 5,
188+
],
189+
190+
'two dimensions, 3 clusters, 50 points per cluster' => [
191+
'dimension' => 2,
192+
'expected' => [
193+
[20, 10],
194+
[40, 20],
195+
[60, 15],
196+
],
197+
'initialClusterCentroids' => [
198+
[12, 10],
199+
[33, 20],
200+
[60, 10],
201+
],
202+
'nbPointsPerCentroid' => 50,
203+
],
204+
];
205+
}
206+
207+
// ------------------------------------------------------------------------
208+
// helpers
209+
201210
/**
202211
* @param array<array<float>> $centroids
203212
* @param int<0, max> $nbPointsPerCentroid

0 commit comments

Comments
 (0)