-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComboQuartiles.php
146 lines (118 loc) · 4.76 KB
/
ComboQuartiles.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* JBZoo Toolbox - Csv-Blueprint.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Csv-Blueprint
*/
declare(strict_types=1);
namespace JBZoo\CsvBlueprint\Rules\Aggregate;
use JBZoo\CsvBlueprint\Rules\AbstractRule;
use JBZoo\CsvBlueprint\Utils;
use MathPHP\Statistics\Descriptive;
use function JBZoo\Utils\float;
final class ComboQuartiles extends AbstractAggregateRuleCombo
{
public const INPUT_TYPE = AbstractRule::INPUT_TYPE_FLOATS;
protected const NAME = 'quartile';
private const TYPES = ['0%', 'Q1', 'Q2', 'Q3', '100%', 'IQR'];
private const METHODS = ['exclusive', 'inclusive'];
private const ARGS = 3;
private const METHOD = 0;
private const TYPE = 1;
private const VAL = 2;
public function getHelpMeta(): array
{
return [
[
'Quartiles. Three points that divide the data set into four equal groups, '
. 'each group comprising a quarter of the data.',
'See: https://en.wikipedia.org/wiki/Quartile',
// Options
'There are multiple methods for computing quartiles: ' . Utils::printList(self::METHODS) . '. '
. 'Exclusive is ussually classic.',
'Available types: ' . Utils::printList(self::TYPES) . ' ("IQR" is Interquartile Range)',
// Example
'Example: `[ ' . self::METHODS[1] . ", '" . self::TYPES[3] . "', 42.0 ]`"
. ' - the ' . self::TYPES[3] . ' ' . self::METHODS[1] . ' quartile is 42.0',
],
[
self::MIN => ['[ ' . self::METHODS[0] . ", '" . self::TYPES[0] . "', 1.0 ]", 'x >= 1.0'],
self::GREATER => ['[ ' . self::METHODS[1] . ", '" . self::TYPES[1] . "', 2.0 ]", 'x > 2.0'],
self::NOT => ['[ ' . self::METHODS[0] . ", '" . self::TYPES[2] . "', 5.0 ]", 'x != 5.0'],
self::EQ => ['[ ' . self::METHODS[1] . ", '" . self::TYPES[3] . "', 7.0 ]", 'x == 7.0'],
self::LESS => ['[ ' . self::METHODS[0] . ", '" . self::TYPES[4] . "', 8.0 ]", 'x < 8.0'],
self::MAX => ['[ ' . self::METHODS[1] . ", '" . self::TYPES[5] . "', 9.0 ]", 'x <= 9.0'],
],
];
}
protected function getExpected(): float
{
return float($this->getParams()[self::VAL]);
}
protected function getActualAggregate(array $colValues): ?float
{
if (\count($colValues) === 0) {
return null;
}
$method = $this->getMethod();
$type = $this->getType();
return self::calcValue($colValues, ['method' => $method, 'type' => $type]);
}
protected static function calcValue(array $columnValues, ?array $options = null): null|float|int
{
$columnValues = Utils::analyzeGuard($columnValues, self::INPUT_TYPE);
if ($columnValues === null) {
return null;
}
if (!isset($options['method'])) {
throw new Exception('The rule expects the "method" option');
}
if (!isset($options['type'])) {
throw new Exception('The rule expects the "type" option');
}
$result = Descriptive::quartiles($columnValues, $options['method']);
if (!isset($result[$options['type']])) {
throw new Exception("Unknown quartile type: {$options['type']}");
}
return $result[$options['type']];
}
private function getType(): string
{
$type = $this->getParams()[self::TYPE];
if (!\in_array($type, self::TYPES, true)) {
throw new Exception(
"Unknown quartile type: \"{$type}\". Allowed: " . Utils::printList(self::TYPES, 'green'),
);
}
return $type;
}
private function getMethod(): string
{
$method = $this->getParams()[self::METHOD];
if (!\in_array($method, self::METHODS, true)) {
throw new Exception(
"Unknown quartile method: \"{$method}\". Allowed: " . Utils::printList(self::METHODS, 'green'),
);
}
return $method;
}
private function getParams(): array
{
$params = $this->getOptionAsArray();
if (\count($params) !== self::ARGS) {
throw new Exception(
'The rule expects exactly three params: '
. 'method ' . Utils::printList(self::METHODS) . ', '
. 'type ' . Utils::printList(self::TYPES) . ', '
. 'expected value (float)',
);
}
return $params;
}
}