-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolveTaskCommand.php
108 lines (87 loc) · 3.68 KB
/
SolveTaskCommand.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace App\UI\CLI;
use App\Application\Analytics\PowerCorrelationAnalyzer;
use App\Application\ML\PowerStationDatasetSplitter;
use App\Application\ML\PowerStationReport;
use App\Application\ML\PowerStationTester;
use App\Application\ML\PowerStationTrainer;
use App\Domain\PowerStation;
use App\Infrastructure\Reader\PowerStationDatasetReader;
use App\Infrastructure\Writer\PowerStationValidDataWriter;
use League\Csv\Exception;
use MathPHP\Exception\MathException;
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;
use Symfony\Component\Validator\Validator\ValidatorInterface;
#[AsCommand(
name: 'app:solve-task',
)]
class SolveTaskCommand extends Command
{
public function __construct(
private readonly PowerStationDatasetReader $datasetReader,
private readonly ValidatorInterface $validator,
private readonly PowerStationValidDataWriter $powerStationValidDataWriter,
private readonly PowerStationDatasetSplitter $powerStationDatasetSplitter,
private readonly PowerStationTrainer $powerStationTrainer,
private readonly PowerStationTester $powerStationTester,
private readonly PowerStationReport $powerStationReport,
) {
parent::__construct();
}
/**
* @throws MathException
* @throws Exception
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$io->info('Dataset reading...');
$dataset = $this->datasetReader->read();
$io->block([
sprintf('Valid rows: %d', $dataset->get('valid')->count()),
sprintf('Invalid rows: %d', $dataset->get('invalid')->count()),
]);
if (!$dataset->get('invalid')->isEmpty()) {
foreach ($dataset->get('invalid') as $invalid) {
$firstError = $this->validator->validate($invalid)->get(0);
$io->warning(sprintf(
'Invalid %s: %f - %s',
$firstError->getPropertyPath(),
$invalid->{$firstError->getPropertyPath()},
$firstError->getMessage(),
));
}
}
$io->info('Analysis of the impact of variables on power...');
$io->comment('Variables:');
dump(PowerStation::VARIABLES);
$io->writeln('');
$io->comment('Correlations:');
dump(PowerCorrelationAnalyzer::checkCorrelation($dataset->get('valid')));
$io->writeln('');
$pca = PowerCorrelationAnalyzer::analysisPCA($dataset->get('valid'));
$io->comment('Importance of variables');
dump($pca->getEigenvalues());
$io->writeln('');
$io->comment('Coefficient of determination');
dump($pca->getR2());
$io->writeln('');
$validDataPath = $this->powerStationValidDataWriter->write($dataset->get('valid')->toArray());
$io->info(sprintf('Valid data has been saved to %s', $validDataPath));
$io->info('Splitting the data into training and test sets...');
$this->powerStationDatasetSplitter->split();
$io->info('Training the model...');
$this->powerStationTrainer->train();
$io->info('Testing the model...');
$score = $this->powerStationTester->test();
$io->success(sprintf('MeanSquaredError: %f', abs($score)));
$io->info('Generating model report...');
dump($this->powerStationReport->generateReport());
$io->success('OK');
return Command::SUCCESS;
}
}