mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-11-05 04:57:52 +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
|
||||
{
|
||||
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';
|
||||
|
||||
$command = $this->buildTrainCommand($trainingSetFileName, $modelFileName);
|
||||
$output = '';
|
||||
exec(escapeshellcmd($command), $output, $return);
|
||||
$output = [];
|
||||
exec(escapeshellcmd($command).' 2>&1', $output, $return);
|
||||
|
||||
unlink($trainingSetFileName);
|
||||
|
||||
if ($return !== 0) {
|
||||
throw LibsvmCommandException::failedToRun($command);
|
||||
throw LibsvmCommandException::failedToRun($command, array_pop($output));
|
||||
}
|
||||
|
||||
$this->model = file_get_contents($modelFileName);
|
||||
|
||||
unlink($trainingSetFileName);
|
||||
unlink($modelFileName);
|
||||
}
|
||||
|
||||
@ -184,19 +185,19 @@ class SupportVectorMachine
|
||||
$outputFileName = $testSetFileName.'-output';
|
||||
|
||||
$command = sprintf('%ssvm-predict%s %s %s %s', $this->binPath, $this->getOSExtension(), $testSetFileName, $modelFileName, $outputFileName);
|
||||
$output = '';
|
||||
exec(escapeshellcmd($command), $output, $return);
|
||||
|
||||
if ($return !== 0) {
|
||||
throw LibsvmCommandException::failedToRun($command);
|
||||
}
|
||||
|
||||
$predictions = file_get_contents($outputFileName);
|
||||
$output = [];
|
||||
exec(escapeshellcmd($command).' 2>&1', $output, $return);
|
||||
|
||||
unlink($testSetFileName);
|
||||
unlink($modelFileName);
|
||||
$predictions = file_get_contents($outputFileName);
|
||||
|
||||
unlink($outputFileName);
|
||||
|
||||
if ($return !== 0) {
|
||||
throw LibsvmCommandException::failedToRun($command, array_pop($output));
|
||||
}
|
||||
|
||||
if (in_array($this->type, [Type::C_SVC, Type::NU_SVC])) {
|
||||
$predictions = DataTransformer::predictions($predictions, $this->targets);
|
||||
} else {
|
||||
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace Phpml\Tests\SupportVectorMachine;
|
||||
|
||||
use Phpml\Exception\InvalidArgumentException;
|
||||
use Phpml\Exception\LibsvmCommandException;
|
||||
use Phpml\SupportVectorMachine\Kernel;
|
||||
use Phpml\SupportVectorMachine\SupportVectorMachine;
|
||||
use Phpml\SupportVectorMachine\Type;
|
||||
@ -105,4 +106,22 @@ SV
|
||||
$svm = new SupportVectorMachine(Type::C_SVC, Kernel::RBF);
|
||||
$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