-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPredictCommand.php
64 lines (53 loc) · 1.77 KB
/
PredictCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace App\UI\CLI;
use App\Application\Path\AppPathResolver;
use App\Domain\FileNames;
use League\Csv\CannotInsertRecord;
use League\Csv\Exception;
use League\Csv\UnavailableStream;
use League\Csv\Writer;
use Rubix\ML\Datasets\Labeled;
use Rubix\ML\Extractors\CSV;
use Rubix\ML\PersistentModel;
use Rubix\ML\Persisters\Filesystem;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'app:predict',
)]
class PredictCommand extends Command
{
public function __construct(
private readonly AppPathResolver $appPathResolver,
) {
parent::__construct();
}
/**
* @throws UnavailableStream
* @throws CannotInsertRecord
* @throws Exception
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$dataset = Labeled::fromIterator(new CSV(
$this->appPathResolver->getResourcesPath(FileNames::TEST_SET),
header: false,
))->transformLabels(fn ($value) => (float) $value);
$model = PersistentModel::load(new Filesystem(
$this->appPathResolver->getResourcesPath(FileNames::MODEL_FILENAME),
));
$writer = Writer::createFromPath($this->appPathResolver->getResourcesPath(
FileNames::PREDICTIONS_FILENAME,
), 'w');
$predictions = $model->predict($dataset);
foreach ($predictions as $prediction) {
$writer->insertOne([$prediction]);
}
$io->success('Results saved to file');
return Command::SUCCESS;
}
}