Files
perceptron-viewer/app/Services/DatasetReader/RandomOrderDataSetReader.php
T
2026-09-08 19:06:02 +02:00

87 lines
2.1 KiB
PHP

<?php
namespace App\Services\DatasetReader;
use App\Services\CsvReader;
class RandomOrderDataSetReader implements IDataSetReader
{
public array $lines = [];
private array $currentLineIndexes = [];
private int $currentLineIndex = 0;
private int $lastReadLineIndex = -1;
public function __construct(
public string $filename,
) {
// For now, we only support CSV files, so we can delegate to CsvReader
$csvReader = new CsvReader($filename);
$this->readEntireFile($csvReader);
$this->reset();
}
private function readEntireFile(CsvReader $reader): void
{
while ($line = $reader->readNextLine()) {
$newLine = [];
foreach ($line as $value) { // Transform to float
$newLine[] = (float) $value;
}
$this->lines[] = $newLine;
}
}
public function getNextLine(): ?array
{
if (! isset($this->currentLineIndexes[$this->currentLineIndex])) {
return null; // No more lines to read
}
$lineIndex = $this->currentLineIndexes[$this->currentLineIndex++];
$this->lastReadLineIndex = $lineIndex;
return $this->lines[$lineIndex];
}
public function getInputSize(): int
{
return count($this->lines[0]) - 1; // Don't count the label
}
public function getOutputSize(): int
{
// Count the number of unique labels in the dataset
$labels = array_map(fn ($line) => end($line), $this->lines);
return count(array_unique($labels));
}
public function getLabels(): array
{
$labels = array_map(fn ($line) => end($line), $this->lines);
return array_values(array_unique($labels));
}
public function reset(): void
{
$this->currentLineIndexes = array_keys($this->lines);
shuffle($this->currentLineIndexes);
$this->currentLineIndex = 0;
}
public function getLastReadLineIndex(): int
{
return $this->lastReadLineIndex;
}
public function getEpochExamplesCount(): int
{
return count($this->lines);
}
}