diff --git a/composer.json b/composer.json index 5773a37..1d91955 100644 --- a/composer.json +++ b/composer.json @@ -11,8 +11,9 @@ ], "require": { "php": ">=7.1", - "laravel/framework": "^5.1||^6.0||^7.0||^8.0", - "rubix/ml": "0.2.*" + "laravel/framework": "^5.1||^6.0||^7.0||^8.0||^9.0", + "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