mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-11-21 20:45:10 +00:00
Throw exception when libsvm command fails to run (#200)
* Throw exception when libsvm command fails to run * Update CS
This commit is contained in:
parent
7435bece34
commit
89268ecb1a
15
src/Phpml/Exception/LibsvmCommandException.php
Normal file
15
src/Phpml/Exception/LibsvmCommandException.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Phpml\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
class LibsvmCommandException extends Exception
|
||||
{
|
||||
public static function failedToRun(string $command): self
|
||||
{
|
||||
return new self(sprintf('Failed running libsvm command: "%s"', $command));
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace Phpml\SupportVectorMachine;
|
||||
|
||||
use Phpml\Exception\InvalidArgumentException;
|
||||
use Phpml\Exception\LibsvmCommandException;
|
||||
use Phpml\Helper\Trainable;
|
||||
|
||||
class SupportVectorMachine
|
||||
@ -153,7 +154,11 @@ class SupportVectorMachine
|
||||
|
||||
$command = $this->buildTrainCommand($trainingSetFileName, $modelFileName);
|
||||
$output = '';
|
||||
exec(escapeshellcmd($command), $output);
|
||||
exec(escapeshellcmd($command), $output, $return);
|
||||
|
||||
if ($return !== 0) {
|
||||
throw LibsvmCommandException::failedToRun($command);
|
||||
}
|
||||
|
||||
$this->model = file_get_contents($modelFileName);
|
||||
|
||||
@ -168,6 +173,8 @@ class SupportVectorMachine
|
||||
|
||||
/**
|
||||
* @return array|string
|
||||
*
|
||||
* @throws LibsvmCommandException
|
||||
*/
|
||||
public function predict(array $samples)
|
||||
{
|
||||
@ -178,7 +185,11 @@ class SupportVectorMachine
|
||||
|
||||
$command = sprintf('%ssvm-predict%s %s %s %s', $this->binPath, $this->getOSExtension(), $testSetFileName, $modelFileName, $outputFileName);
|
||||
$output = '';
|
||||
exec(escapeshellcmd($command), $output);
|
||||
exec(escapeshellcmd($command), $output, $return);
|
||||
|
||||
if ($return !== 0) {
|
||||
throw LibsvmCommandException::failedToRun($command);
|
||||
}
|
||||
|
||||
$predictions = file_get_contents($outputFileName);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user