-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new "quartiles" and "midhinge" rules (#89)
- Loading branch information
Showing
12 changed files
with
426 additions
and
16 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
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
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,49 @@ | ||
<?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\AbstarctRule; | ||
use MathPHP\Statistics\Descriptive; | ||
|
||
final class ComboMidhinge extends AbstarctAggregateRuleCombo | ||
{ | ||
public const INPUT_TYPE = AbstarctRule::INPUT_TYPE_FLOATS; | ||
|
||
protected const NAME = 'midhinge'; | ||
|
||
public function getHelpMeta(): array | ||
{ | ||
return [ | ||
[ | ||
'Midhinge. The average of the first and third quartiles and is thus a measure of location.', | ||
'Equivalently, it is the 25% trimmed mid-range or 25% midsummary; it is an L-estimator.', | ||
'See: https://en.wikipedia.org/wiki/Midhinge', | ||
'Midhinge = (first quartile, third quartile) / 2', | ||
], | ||
[], | ||
]; | ||
} | ||
|
||
protected function getActualAggregate(array $colValues): ?float | ||
{ | ||
if (\count($colValues) === 0) { | ||
return null; | ||
} | ||
|
||
return Descriptive::midhinge(self::stringsToFloat($colValues)); | ||
} | ||
} |
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,124 @@ | ||
<?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\AbstarctRule; | ||
use MathPHP\Statistics\Descriptive; | ||
|
||
use function JBZoo\Utils\float; | ||
|
||
final class ComboQuartiles extends AbstarctAggregateRuleCombo | ||
{ | ||
public const INPUT_TYPE = AbstarctRule::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: "' . \implode('", "', self::METHODS) . '". ' . | ||
'Exclusive is ussually classic.', | ||
'Available types: "' . \implode('", "', self::TYPES) . '" (aka Interquartile Range)', | ||
// Example | ||
'Example: `[ ' . self::METHODS[1] . ", '" . self::TYPES[3] . "', 42.0 ]`" . | ||
' - the ' . self::TYPES[3] . ' ' . self::METHODS[1] . ' quartile is 50.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(); | ||
$result = Descriptive::quartiles(self::stringsToFloat($colValues), $method); | ||
|
||
return $result[$type]; | ||
} | ||
|
||
private function getType(): string | ||
{ | ||
$allowedTypes = ['0%', 'Q1', 'Q2', 'Q3', '100%', 'IQR']; | ||
|
||
$type = $this->getParams()[self::TYPE]; | ||
|
||
if (!\in_array($type, $allowedTypes, true)) { | ||
throw new \RuntimeException( | ||
"Unknown quartile type: \"{$type}\". Allowed: \"" . \implode('", "', $allowedTypes) . '"', | ||
); | ||
} | ||
|
||
return $type; | ||
} | ||
|
||
private function getMethod(): string | ||
{ | ||
$allowedMethods = ['exclusive', 'inclusive']; | ||
|
||
$method = $this->getParams()[self::METHOD]; | ||
|
||
if (!\in_array($method, $allowedMethods, true)) { | ||
throw new \RuntimeException( | ||
"Unknown quartile method: \"{$method}\". Allowed: \"" . \implode('", "', $allowedMethods) . '"', | ||
); | ||
} | ||
|
||
return $method; | ||
} | ||
|
||
private function getParams(): array | ||
{ | ||
$params = $this->getOptionAsArray(); | ||
if (\count($params) !== self::ARGS) { | ||
throw new \RuntimeException( | ||
'The rule expects exactly three params: ' . | ||
'method (exclusive, inclusive), type (0%, Q1, Q2, Q3, 100%, IQR), expected value (float)', | ||
); | ||
} | ||
|
||
return $params; | ||
} | ||
} |
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
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,41 @@ | ||
<?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\PHPUnit\Rules\Aggregate; | ||
|
||
use JBZoo\CsvBlueprint\Rules\AbstarctRule as Combo; | ||
use JBZoo\CsvBlueprint\Rules\Aggregate\ComboMidhinge; | ||
use JBZoo\PHPUnit\Rules\TestAbstractAggregateRuleCombo; | ||
|
||
use function JBZoo\PHPUnit\isSame; | ||
|
||
class ComboMidhingeTest extends TestAbstractAggregateRuleCombo | ||
{ | ||
protected string $ruleClass = ComboMidhinge::class; | ||
|
||
public function testEqual(): void | ||
{ | ||
$rule = $this->create(18, Combo::EQ); | ||
isSame('', $rule->test([])); | ||
isSame('', $rule->test(\range(1, 35))); | ||
|
||
$rule = $this->create(3, Combo::EQ); | ||
isSame( | ||
'The midhinge in the column is "18", which is not equal than the expected "3"', | ||
$rule->test(\range(1, 35)), | ||
); | ||
} | ||
} |
Oops, something went wrong.