From a55a98d83fcaf9a7b050a4ba9ca8a41b732d6577 Mon Sep 17 00:00:00 2001 From: Brad Coudriet Date: Tue, 5 Jan 2021 09:12:57 -0500 Subject: [PATCH 1/2] Added a Custom Exception and supporting tests if a model file is not found. This should satisfy #1 --- composer.json | 3 +- src/Exceptions/ModelFileNotFound.php | 21 ++++++++++ src/Traits/PredictsAttributes.php | 13 ++++++- tests/Unit/PredictionTest.php | 10 +++++ tests/Unit/TestClasses/Eloquent/Untrained.php | 39 +++++++++++++++++++ 5 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 src/Exceptions/ModelFileNotFound.php create mode 100644 tests/Unit/TestClasses/Eloquent/Untrained.php diff --git a/composer.json b/composer.json index 5773a37..a89275b 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "require": { "php": ">=7.1", "laravel/framework": "^5.1||^6.0||^7.0||^8.0", - "rubix/ml": "0.2.*" + "rubix/ml": "0.2.*", + "facade/ignition-contracts": "^1.0" }, "require-dev": { "phpunit/phpunit": "^7.0||^8.0", diff --git a/src/Exceptions/ModelFileNotFound.php b/src/Exceptions/ModelFileNotFound.php new file mode 100644 index 0000000..9511749 --- /dev/null +++ b/src/Exceptions/ModelFileNotFound.php @@ -0,0 +1,21 @@ +getMessage().' Not Found') + ->setSolutionDescription('Generate your data model by running `php artisan eavp:train '.addslashes($this->getMessage()).'`') + ->setDocumentationLinks([ + 'Training Documentation' => 'https://github.com/DivineOmega/eloquent-attribute-value-prediction#training', + ]); + } +} \ No newline at end of file diff --git a/src/Traits/PredictsAttributes.php b/src/Traits/PredictsAttributes.php index c5d660f..d6184c0 100644 --- a/src/Traits/PredictsAttributes.php +++ b/src/Traits/PredictsAttributes.php @@ -2,6 +2,7 @@ namespace DivineOmega\EloquentAttributeValuePrediction\Traits; +use DivineOmega\EloquentAttributeValuePrediction\Exceptions\ModelFileNotFound; use DivineOmega\EloquentAttributeValuePrediction\Helpers\DatasetHelper; use DivineOmega\EloquentAttributeValuePrediction\Helpers\PathHelper; use Exception; @@ -28,15 +29,23 @@ public function getPredictions(string $attribute): array { if ($this->isAttributeContinuous($attribute)) { throw new InvalidArgumentException( - 'You can not get multiple predictions for a continious (numeric) argument. Try using the `predict` method instead.' + 'You can not get multiple predictions for a continuous (numeric) argument. Try using the `predict` method instead.' ); } $dataset = DatasetHelper::buildUnlabeledDataset($this, $attribute); + //If Model File is missing, throw a custom exception with a solution in it. $modelPath = PathHelper::getModelPath(get_class($this), $attribute); - $estimator = PersistentModel::load(new Filesystem($modelPath)); + if(!is_file($modelPath)) + { + throw new ModelFileNotFound(get_class($this)); + } + + $modelFile = new Filesystem($modelPath); + + $estimator = PersistentModel::load($modelFile); $predictions = $estimator->proba($dataset)[0]; diff --git a/tests/Unit/PredictionTest.php b/tests/Unit/PredictionTest.php index a9ba4b8..583060c 100644 --- a/tests/Unit/PredictionTest.php +++ b/tests/Unit/PredictionTest.php @@ -2,9 +2,11 @@ namespace DivineOmega\EloquentAttributeValuePrediction\Tests\Unit; +use DivineOmega\EloquentAttributeValuePrediction\Exceptions\ModelFileNotFound; use DivineOmega\EloquentAttributeValuePrediction\Helpers\PathHelper; use DivineOmega\EloquentAttributeValuePrediction\ServiceProvider; use DivineOmega\EloquentAttributeValuePrediction\Tests\Unit\TestClasses\Eloquent\IrisFlower; +use DivineOmega\EloquentAttributeValuePrediction\Tests\Unit\TestClasses\Eloquent\Untrained; use Illuminate\Database\Eloquent\Model; use Orchestra\Testbench\TestCase; @@ -80,4 +82,12 @@ public function testPetalWidthGetPredictionsFails() IrisFlower::first()->getPredictions('petal_width'); } + public function testModelFileNotFoundThrown() + { + + $this->expectException(ModelFileNotFound::class); + + Untrained::first()->getPredictions('abr'); + + } } diff --git a/tests/Unit/TestClasses/Eloquent/Untrained.php b/tests/Unit/TestClasses/Eloquent/Untrained.php new file mode 100644 index 0000000..1c9fa45 --- /dev/null +++ b/tests/Unit/TestClasses/Eloquent/Untrained.php @@ -0,0 +1,39 @@ + 'string' + ]; + + protected $rows = [ + [ + 'abbr' => 'NY', + 'name' => 'New York', + ], + [ + 'abbr' => 'CA', + 'name' => 'California', + ], + ]; + + public function registerPredictableAttributes(): array + { + return [ + 'abr' => [ + 'name' + ] + ]; + } + +} \ No newline at end of file From 2d2e7a0d0f0bb937e0fb086d93c081c46acb90c1 Mon Sep 17 00:00:00 2001 From: Brad Coudriet Date: Wed, 20 Apr 2022 20:18:45 -0400 Subject: [PATCH 2/2] Laravel 9 support --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a89275b..1d91955 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ ], "require": { "php": ">=7.1", - "laravel/framework": "^5.1||^6.0||^7.0||^8.0", + "laravel/framework": "^5.1||^6.0||^7.0||^8.0||^9.0", "rubix/ml": "0.2.*", "facade/ignition-contracts": "^1.0" },