mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-09-22 04:09:01 +00:00
Add libsvm exception tests (#202)
This commit is contained in:
parent
89268ecb1a
commit
ba7114a3f7
@ -8,8 +8,8 @@ use Exception;
|
|||||||
|
|
||||||
class LibsvmCommandException extends Exception
|
class LibsvmCommandException extends Exception
|
||||||
{
|
{
|
||||||
public static function failedToRun(string $command): self
|
public static function failedToRun(string $command, string $reason): self
|
||||||
{
|
{
|
||||||
return new self(sprintf('Failed running libsvm command: "%s"', $command));
|
return new self(sprintf('Failed running libsvm command: "%s" with reason: "%s"', $command, $reason));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -153,16 +153,17 @@ class SupportVectorMachine
|
|||||||
$modelFileName = $trainingSetFileName.'-model';
|
$modelFileName = $trainingSetFileName.'-model';
|
||||||
|
|
||||||
$command = $this->buildTrainCommand($trainingSetFileName, $modelFileName);
|
$command = $this->buildTrainCommand($trainingSetFileName, $modelFileName);
|
||||||
$output = '';
|
$output = [];
|
||||||
exec(escapeshellcmd($command), $output, $return);
|
exec(escapeshellcmd($command).' 2>&1', $output, $return);
|
||||||
|
|
||||||
|
unlink($trainingSetFileName);
|
||||||
|
|
||||||
if ($return !== 0) {
|
if ($return !== 0) {
|
||||||
throw LibsvmCommandException::failedToRun($command);
|
throw LibsvmCommandException::failedToRun($command, array_pop($output));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->model = file_get_contents($modelFileName);
|
$this->model = file_get_contents($modelFileName);
|
||||||
|
|
||||||
unlink($trainingSetFileName);
|
|
||||||
unlink($modelFileName);
|
unlink($modelFileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -184,19 +185,19 @@ class SupportVectorMachine
|
|||||||
$outputFileName = $testSetFileName.'-output';
|
$outputFileName = $testSetFileName.'-output';
|
||||||
|
|
||||||
$command = sprintf('%ssvm-predict%s %s %s %s', $this->binPath, $this->getOSExtension(), $testSetFileName, $modelFileName, $outputFileName);
|
$command = sprintf('%ssvm-predict%s %s %s %s', $this->binPath, $this->getOSExtension(), $testSetFileName, $modelFileName, $outputFileName);
|
||||||
$output = '';
|
$output = [];
|
||||||
exec(escapeshellcmd($command), $output, $return);
|
exec(escapeshellcmd($command).' 2>&1', $output, $return);
|
||||||
|
|
||||||
if ($return !== 0) {
|
|
||||||
throw LibsvmCommandException::failedToRun($command);
|
|
||||||
}
|
|
||||||
|
|
||||||
$predictions = file_get_contents($outputFileName);
|
|
||||||
|
|
||||||
unlink($testSetFileName);
|
unlink($testSetFileName);
|
||||||
unlink($modelFileName);
|
unlink($modelFileName);
|
||||||
|
$predictions = file_get_contents($outputFileName);
|
||||||
|
|
||||||
unlink($outputFileName);
|
unlink($outputFileName);
|
||||||
|
|
||||||
|
if ($return !== 0) {
|
||||||
|
throw LibsvmCommandException::failedToRun($command, array_pop($output));
|
||||||
|
}
|
||||||
|
|
||||||
if (in_array($this->type, [Type::C_SVC, Type::NU_SVC])) {
|
if (in_array($this->type, [Type::C_SVC, Type::NU_SVC])) {
|
||||||
$predictions = DataTransformer::predictions($predictions, $this->targets);
|
$predictions = DataTransformer::predictions($predictions, $this->targets);
|
||||||
} else {
|
} else {
|
||||||
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||||||
namespace Phpml\Tests\SupportVectorMachine;
|
namespace Phpml\Tests\SupportVectorMachine;
|
||||||
|
|
||||||
use Phpml\Exception\InvalidArgumentException;
|
use Phpml\Exception\InvalidArgumentException;
|
||||||
|
use Phpml\Exception\LibsvmCommandException;
|
||||||
use Phpml\SupportVectorMachine\Kernel;
|
use Phpml\SupportVectorMachine\Kernel;
|
||||||
use Phpml\SupportVectorMachine\SupportVectorMachine;
|
use Phpml\SupportVectorMachine\SupportVectorMachine;
|
||||||
use Phpml\SupportVectorMachine\Type;
|
use Phpml\SupportVectorMachine\Type;
|
||||||
@ -105,4 +106,22 @@ SV
|
|||||||
$svm = new SupportVectorMachine(Type::C_SVC, Kernel::RBF);
|
$svm = new SupportVectorMachine(Type::C_SVC, Kernel::RBF);
|
||||||
$svm->setBinPath('var');
|
$svm->setBinPath('var');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testThrowExceptionWhenLibsvmFailsDuringTrain(): void
|
||||||
|
{
|
||||||
|
$this->expectException(LibsvmCommandException::class);
|
||||||
|
$this->expectExceptionMessage('ERROR: unknown svm type');
|
||||||
|
|
||||||
|
$svm = new SupportVectorMachine(99, Kernel::RBF);
|
||||||
|
$svm->train([], []);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testThrowExceptionWhenLibsvmFailsDuringPredict(): void
|
||||||
|
{
|
||||||
|
$this->expectException(LibsvmCommandException::class);
|
||||||
|
$this->expectExceptionMessage('can\'t open model file');
|
||||||
|
|
||||||
|
$svm = new SupportVectorMachine(Type::C_SVC, Kernel::RBF);
|
||||||
|
$svm->predict([1]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user