Skip to content

Commit

Permalink
Add test for Pipeline save and restore with ModelManager (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas authored Jan 12, 2018
1 parent d953ef6 commit 7435bec
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tests/Phpml/PipelineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Phpml\Classification\SVC;
use Phpml\FeatureExtraction\TfIdfTransformer;
use Phpml\FeatureExtraction\TokenCountVectorizer;
use Phpml\ModelManager;
use Phpml\Pipeline;
use Phpml\Preprocessing\Imputer;
use Phpml\Preprocessing\Imputer\Strategy\MostFrequentStrategy;
Expand Down Expand Up @@ -104,4 +105,40 @@ public function testPipelineTransformers(): void

$this->assertEquals($expected, $predicted);
}

public function testSaveAndRestore(): void
{
$pipeline = new Pipeline([
new TokenCountVectorizer(new WordTokenizer()),
new TfIdfTransformer(),
], new SVC());

$pipeline->train([
'Hello Paul',
'Hello Martin',
'Goodbye Tom',
'Hello John',
'Goodbye Alex',
'Bye Tony',
], [
'greetings',
'greetings',
'farewell',
'greetings',
'farewell',
'farewell',
]);

$testSamples = ['Hello Max', 'Goodbye Mark'];
$predicted = $pipeline->predict($testSamples);

$filepath = tempnam(sys_get_temp_dir(), uniqid('pipeline-test', true));
$modelManager = new ModelManager();
$modelManager->saveToFile($pipeline, $filepath);

$restoredClassifier = $modelManager->restoreFromFile($filepath);
$this->assertEquals($pipeline, $restoredClassifier);
$this->assertEquals($predicted, $restoredClassifier->predict($testSamples));
unlink($filepath);
}
}

0 comments on commit 7435bec

Please sign in to comment.