Refactored into folders
This commit is contained in:
11
app/Services/DatasetReader/IDataSetReader.php
Normal file
11
app/Services/DatasetReader/IDataSetReader.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\DatasetReader;
|
||||
|
||||
interface IDataSetReader {
|
||||
public function getNextLine(): array | null;
|
||||
public function getInputSize(): int;
|
||||
public function reset(): void;
|
||||
public function getLastReadLineIndex(): int;
|
||||
public function getEpochExamplesCount(): int;
|
||||
}
|
||||
68
app/Services/DatasetReader/LinearOrderDataSetReader.php
Normal file
68
app/Services/DatasetReader/LinearOrderDataSetReader.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\DatasetReader;
|
||||
|
||||
use App\Services\CsvReader;
|
||||
|
||||
class LinearOrderDataSetReader implements IDataSetReader {
|
||||
public array $lines = [];
|
||||
private array $currentLines = [];
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// if the dataset is for regression, we add a fake label of 0
|
||||
if (count($newLine) === 2) {
|
||||
$newLine[] = 0.0;
|
||||
}
|
||||
|
||||
$this->lines[] = $newLine;
|
||||
}
|
||||
}
|
||||
|
||||
public function getNextLine(): array | null {
|
||||
if (!isset($this->currentLines[0])) {
|
||||
return null; // No more lines to read
|
||||
}
|
||||
|
||||
$this->lastReadLineIndex = array_search($this->currentLines[0], $this->lines, true);
|
||||
|
||||
return array_shift($this->currentLines);
|
||||
}
|
||||
|
||||
public function getInputSize(): int
|
||||
{
|
||||
return count($this->lines[0]) - 1; // Don't count the label
|
||||
}
|
||||
|
||||
public function reset(): void
|
||||
{
|
||||
$this->currentLines = $this->lines;
|
||||
}
|
||||
|
||||
public function getLastReadLineIndex(): int
|
||||
{
|
||||
return $this->lastReadLineIndex;
|
||||
}
|
||||
|
||||
public function getEpochExamplesCount(): int
|
||||
{
|
||||
return count($this->lines);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
namespace App\Services\DatasetReader;
|
||||
|
||||
class DataSetReader {
|
||||
use App\Services\CsvReader;
|
||||
|
||||
class RandomOrderDataSetReaders implements IDataSetReader {
|
||||
public array $lines = [];
|
||||
private array $currentLines = [];
|
||||
|
||||
@@ -34,7 +36,7 @@ class DataSetReader {
|
||||
}
|
||||
}
|
||||
|
||||
public function getRandomLine(): array | null
|
||||
public function getNextLine(): array | null
|
||||
{
|
||||
if (empty($this->currentLines)) {
|
||||
return null; // No more lines to read
|
||||
@@ -51,16 +53,6 @@ class DataSetReader {
|
||||
return $randomLine;
|
||||
}
|
||||
|
||||
public function getNextLine(): array | null {
|
||||
if (!isset($this->currentLines[0])) {
|
||||
return null; // No more lines to read
|
||||
}
|
||||
|
||||
$this->lastReadLineIndex = array_search($this->currentLines[0], $this->lines, true);
|
||||
|
||||
return array_shift($this->currentLines);
|
||||
}
|
||||
|
||||
public function getInputSize(): int
|
||||
{
|
||||
return count($this->lines[0]) - 1; // Don't count the label
|
||||
@@ -75,4 +67,9 @@ class DataSetReader {
|
||||
{
|
||||
return $this->lastReadLineIndex;
|
||||
}
|
||||
|
||||
public function getEpochExamplesCount(): int
|
||||
{
|
||||
return count($this->lines);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
namespace App\Services\IterationEventBuffer;
|
||||
|
||||
interface IPerceptronIterationEventBuffer {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
namespace App\Services\IterationEventBuffer;
|
||||
|
||||
class PerceptronIterationEventBuffer implements IPerceptronIterationEventBuffer {
|
||||
private $data;
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
namespace App\Services\IterationEventBuffer;
|
||||
|
||||
class PerceptronLimitedEpochEventBuffer implements IPerceptronIterationEventBuffer {
|
||||
private array $data;
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
namespace App\Services\SynapticWeightsProvider;
|
||||
|
||||
interface ISynapticWeightsProvider {
|
||||
public function generate(int $input_size): array;
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
namespace App\Services\SynapticWeightsProvider;
|
||||
|
||||
class RandomSynapticWeights implements ISynapticWeightsProvider {
|
||||
public function generate(int $input_size): array
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
namespace App\Services\SynapticWeightsProvider;
|
||||
|
||||
class ZeroSynapticWeights implements ISynapticWeightsProvider {
|
||||
public function generate(int $input_size): array
|
||||
Reference in New Issue
Block a user