Passed
Pull Request — master (#262)
by
unknown
03:02
created

Cluster::getPointsWithLabels()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Phpml\Clustering\KMeans;
6
7
use Countable;
8
use IteratorAggregate;
9
use LogicException;
10
use SplObjectStorage;
11
12
class Cluster extends Point implements IteratorAggregate, Countable
13
{
14
    /**
15
     * @var Space
16
     */
17
    protected $space;
18
19
    /**
20
     * @var SplObjectStorage|Point[]
21
     */
22
    protected $points;
23
24
    public function __construct(Space $space, array $coordinates)
25
    {
26
        parent::__construct($coordinates);
27
        $this->space = $space;
28
        $this->points = new SplObjectStorage();
29
    }
30
31
    public function getPoints(): array
32
    {
33
        $points = [];
34
        foreach ($this->points as $point) {
35
            $points[] = $point->toArray();
36
        }
37
38
        return $points;
39
    }
40
41
    public function getPointsWithLabels(): array
42
    {
43
        $points = [];
44
        foreach ($this->points as $point) {
45
            $points[$point->label] = $point->toArray();
46
        }
47
48
        return $points;
49
    }
50
51
    public function toArray(): array
52
    {
53
        return [
54
            'centroid' => parent::toArray(),
55
            'points' => $this->getPoints(),
56
        ];
57
    }
58
59
    public function attach(Point $point): Point
60
    {
61
        if ($point instanceof self) {
62
            throw new LogicException('Cannot attach a cluster to another');
63
        }
64
65
        $this->points->attach($point);
66
67
        return $point;
68
    }
69
70
    public function detach(Point $point): Point
71
    {
72
        $this->points->detach($point);
73
74
        return $point;
75
    }
76
77
    public function attachAll(SplObjectStorage $points): void
78
    {
79
        $this->points->addAll($points);
80
    }
81
82
    public function detachAll(SplObjectStorage $points): void
83
    {
84
        $this->points->removeAll($points);
85
    }
86
87
    public function updateCentroid(): void
88
    {
89
        $count = count($this->points);
90
        if ($count === 0) {
91
            return;
92
        }
93
94
        $centroid = $this->space->newPoint(array_fill(0, $this->dimension, 0));
95
96
        foreach ($this->points as $point) {
97 View Code Duplication
            for ($n = 0; $n < $this->dimension; ++$n) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
                $centroid->coordinates[$n] += $point->coordinates[$n];
99
            }
100
        }
101
102 View Code Duplication
        for ($n = 0; $n < $this->dimension; ++$n) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
            $this->coordinates[$n] = $centroid->coordinates[$n] / $count;
104
        }
105
    }
106
107
    /**
108
     * @return Point[]|SplObjectStorage
109
     */
110
    public function getIterator()
111
    {
112
        return $this->points;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->points; of type SplObjectStorage|Phpml\Clustering\KMeans\Point[] adds the type Phpml\Clustering\KMeans\Point[] to the return on line 112 which is incompatible with the return type declared by the interface IteratorAggregate::getIterator of type Traversable.
Loading history...
113
    }
114
115
    /**
116
     * @return mixed
117
     */
118
    public function count()
119
    {
120
        return count($this->points);
121
    }
122
123
    public function setCoordinates(array $newCoordinates): void
124
    {
125
        $this->coordinates = $newCoordinates;
126
    }
127
}
128