Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 21 additions & 0 deletions src/Exceptions/ModelFileNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace DivineOmega\EloquentAttributeValuePrediction\Exceptions;

use Exception;
use Facade\IgnitionContracts\Solution;
use Facade\IgnitionContracts\BaseSolution;
use Facade\IgnitionContracts\ProvidesSolution;

class ModelFileNotFound extends Exception implements ProvidesSolution
{
public function getSolution(): Solution
{

return BaseSolution::create('Data Model File for '.$this->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',
]);
}
}
13 changes: 11 additions & 2 deletions src/Traits/PredictsAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];

Expand Down
10 changes: 10 additions & 0 deletions tests/Unit/PredictionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -80,4 +82,12 @@ public function testPetalWidthGetPredictionsFails()
IrisFlower::first()->getPredictions('petal_width');
}

public function testModelFileNotFoundThrown()
{

$this->expectException(ModelFileNotFound::class);

Untrained::first()->getPredictions('abr');

}
}
39 changes: 39 additions & 0 deletions tests/Unit/TestClasses/Eloquent/Untrained.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace DivineOmega\EloquentAttributeValuePrediction\Tests\Unit\TestClasses\Eloquent;

use DivineOmega\EloquentAttributeValuePrediction\Interfaces\HasPredictableAttributes;
use DivineOmega\EloquentAttributeValuePrediction\Traits\PredictsAttributes;
use Illuminate\Database\Eloquent\Model;
use Sushi\Sushi;

class Untrained extends Model implements HasPredictableAttributes
{
use PredictsAttributes;
use Sushi;

protected $casts = [
'abr' => 'string'
];

protected $rows = [
[
'abbr' => 'NY',
'name' => 'New York',
],
[
'abbr' => 'CA',
'name' => 'California',
],
];

public function registerPredictableAttributes(): array
{
return [
'abr' => [
'name'
]
];
}

}