mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-09-22 12:19:02 +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;
|
namespace Phpml\SupportVectorMachine;
|
||||||
|
|
||||||
use Phpml\Exception\InvalidArgumentException;
|
use Phpml\Exception\InvalidArgumentException;
|
||||||
|
use Phpml\Exception\LibsvmCommandException;
|
||||||
use Phpml\Helper\Trainable;
|
use Phpml\Helper\Trainable;
|
||||||
|
|
||||||
class SupportVectorMachine
|
class SupportVectorMachine
|
||||||
@ -153,7 +154,11 @@ class SupportVectorMachine
|
|||||||
|
|
||||||
$command = $this->buildTrainCommand($trainingSetFileName, $modelFileName);
|
$command = $this->buildTrainCommand($trainingSetFileName, $modelFileName);
|
||||||
$output = '';
|
$output = '';
|
||||||
exec(escapeshellcmd($command), $output);
|
exec(escapeshellcmd($command), $output, $return);
|
||||||
|
|
||||||
|
if ($return !== 0) {
|
||||||
|
throw LibsvmCommandException::failedToRun($command);
|
||||||
|
}
|
||||||
|
|
||||||
$this->model = file_get_contents($modelFileName);
|
$this->model = file_get_contents($modelFileName);
|
||||||
|
|
||||||
@ -168,6 +173,8 @@ class SupportVectorMachine
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array|string
|
* @return array|string
|
||||||
|
*
|
||||||
|
* @throws LibsvmCommandException
|
||||||
*/
|
*/
|
||||||
public function predict(array $samples)
|
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);
|
$command = sprintf('%ssvm-predict%s %s %s %s', $this->binPath, $this->getOSExtension(), $testSetFileName, $modelFileName, $outputFileName);
|
||||||
$output = '';
|
$output = '';
|
||||||
exec(escapeshellcmd($command), $output);
|
exec(escapeshellcmd($command), $output, $return);
|
||||||
|
|
||||||
|
if ($return !== 0) {
|
||||||
|
throw LibsvmCommandException::failedToRun($command);
|
||||||
|
}
|
||||||
|
|
||||||
$predictions = file_get_contents($outputFileName);
|
$predictions = file_get_contents($outputFileName);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user