Fix linting
Some checks failed
linter / quality (push) Failing after 6m26s
tests / ci (8.4) (push) Successful in 5m6s
tests / ci (8.5) (push) Successful in 5m38s

This commit is contained in:
2026-03-23 08:44:50 +01:00
parent 9d4b02fab5
commit ef90236adc
32 changed files with 123 additions and 113 deletions

View File

@@ -2,7 +2,8 @@
namespace App\Services;
class CsvReader {
class CsvReader
{
private $file;
// private array $headers;
@@ -10,11 +11,10 @@ class CsvReader {
public function __construct(
public string $filename,
)
{
$this->file = fopen($filename, "r");
if (!$this->file) {
throw new \RuntimeException("Failed to open file: " . $filename);
) {
$this->file = fopen($filename, 'r');
if (! $this->file) {
throw new \RuntimeException('Failed to open file: '.$filename);
}
// $this->headers = $this->readNextLine();
@@ -22,9 +22,10 @@ class CsvReader {
public function readNextLine(): ?array
{
if (($data = fgetcsv($this->file, 1000, ",")) !== FALSE) {
if (($data = fgetcsv($this->file, 1000, ',')) !== false) {
return $data;
}
return null; // End of file or error
}
}

View File

@@ -2,10 +2,15 @@
namespace App\Services\DatasetReader;
interface IDataSetReader {
public function getNextLine(): array | null;
interface IDataSetReader
{
public function getNextLine(): ?array;
public function getInputSize(): int;
public function reset(): void;
public function getLastReadLineIndex(): int;
public function getEpochExamplesCount(): int;
}

View File

@@ -4,8 +4,10 @@ namespace App\Services\DatasetReader;
use App\Services\CsvReader;
class LinearOrderDataSetReader implements IDataSetReader {
class LinearOrderDataSetReader implements IDataSetReader
{
public array $lines = [];
private array $currentLines = [];
private int $lastReadLineIndex = -1;
@@ -36,8 +38,9 @@ class LinearOrderDataSetReader implements IDataSetReader {
}
}
public function getNextLine(): array | null {
if (!isset($this->currentLines[0])) {
public function getNextLine(): ?array
{
if (! isset($this->currentLines[0])) {
return null; // No more lines to read
}

View File

@@ -4,8 +4,10 @@ namespace App\Services\DatasetReader;
use App\Services\CsvReader;
class RandomOrderDataSetReader implements IDataSetReader {
class RandomOrderDataSetReader implements IDataSetReader
{
public array $lines = [];
private array $currentLines = [];
private int $lastReadLineIndex = -1;
@@ -36,7 +38,7 @@ class RandomOrderDataSetReader implements IDataSetReader {
}
}
public function getNextLine(): array | null
public function getNextLine(): ?array
{
if (empty($this->currentLines)) {
return null; // No more lines to read

View File

@@ -2,9 +2,9 @@
namespace App\Services\IterationEventBuffer;
interface IPerceptronIterationEventBuffer {
interface IPerceptronIterationEventBuffer
{
public function flush(): void;
public function flush(): void ;
public function addIteration(int $iteration, int $exampleIndex, float $error, array $synaptic_weights): void ;
public function addIteration(int $iteration, int $exampleIndex, float $error, array $synaptic_weights): void;
}

View File

@@ -2,9 +2,12 @@
namespace App\Services\IterationEventBuffer;
class PerceptronIterationEventBuffer implements IPerceptronIterationEventBuffer {
class PerceptronIterationEventBuffer implements IPerceptronIterationEventBuffer
{
private $data;
private int $nextSizeIncreaseThreshold;
private int $underSizeIncreaseCount = 0;
public function __construct(
@@ -17,24 +20,25 @@ class PerceptronIterationEventBuffer implements IPerceptronIterationEventBuffer
$this->nextSizeIncreaseThreshold = $sizeIncreaseStart;
}
public function flush(): void {
public function flush(): void
{
event(new \App\Events\PerceptronTrainingIteration($this->data, $this->sessionId, $this->trainingId));
$this->data = [];
}
public function addIteration(int $epoch, int $exampleIndex, float $error, array $synaptic_weights): void {
public function addIteration(int $epoch, int $exampleIndex, float $error, array $synaptic_weights): void
{
$this->data[] = [
"epoch" => $epoch,
"exampleIndex" => $exampleIndex,
"error" => $error,
"weights" => $synaptic_weights,
'epoch' => $epoch,
'exampleIndex' => $exampleIndex,
'error' => $error,
'weights' => $synaptic_weights,
];
if ($this->underSizeIncreaseCount <= $this->sizeIncreaseStart) { // We can still send a single date because we are under the increase start threshold
$this->underSizeIncreaseCount++;
$this->flush();
}
else if (count($this->data) >= $this->nextSizeIncreaseThreshold) {
} elseif (count($this->data) >= $this->nextSizeIncreaseThreshold) {
$this->flush();
$this->nextSizeIncreaseThreshold *= $this->sizeIncreaseFactor;

View File

@@ -2,8 +2,10 @@
namespace App\Services\IterationEventBuffer;
class PerceptronLimitedEpochEventBuffer implements IPerceptronIterationEventBuffer {
class PerceptronLimitedEpochEventBuffer implements IPerceptronIterationEventBuffer
{
private array $data;
private int $underSizeIncreaseCount = 0;
public function __construct(
@@ -15,23 +17,26 @@ class PerceptronLimitedEpochEventBuffer implements IPerceptronIterationEventBuff
$this->data = [];
}
public function flush(): void {
public function flush(): void
{
event(new \App\Events\PerceptronTrainingIteration($this->data, $this->sessionId, $this->trainingId));
$this->data = [];
}
public function addIteration(int $epoch, int $exampleIndex, float $error, array $synaptic_weights): void {
public function addIteration(int $epoch, int $exampleIndex, float $error, array $synaptic_weights): void
{
$newData = [
"epoch" => $epoch,
"exampleIndex" => $exampleIndex,
"error" => $error,
"weights" => $synaptic_weights,
'epoch' => $epoch,
'exampleIndex' => $exampleIndex,
'error' => $error,
'weights' => $synaptic_weights,
];
if ($this->underSizeIncreaseCount <= $this->sizeIncreaseStart) { // Special case where we need to send each iteration separately
$this->underSizeIncreaseCount++;
$this->data[] = $newData;
$this->flush();
return;
}
@@ -39,8 +44,7 @@ class PerceptronLimitedEpochEventBuffer implements IPerceptronIterationEventBuff
if ($this->data && $lastEpoch !== $epoch) { // Current Epoch has changed from the last one
if ($lastEpoch % $this->epochInterval === 0) { // The last epoch need to be sent
$this->flush(); // Flush all data from the previous epoch
}
else {
} else {
$this->data = [];
}

View File

@@ -2,6 +2,7 @@
namespace App\Services\SynapticWeightsProvider;
interface ISynapticWeightsProvider {
interface ISynapticWeightsProvider
{
public function generate(int $input_size): array;
}

View File

@@ -2,13 +2,15 @@
namespace App\Services\SynapticWeightsProvider;
class RandomSynapticWeights implements ISynapticWeightsProvider {
class RandomSynapticWeights implements ISynapticWeightsProvider
{
public function generate(int $input_size): array
{
$weights = [];
for ($i = 0; $i < $input_size + 1; $i++) { // +1 for bias weight
$weights[] = rand(-100, 100) / 100; // Random weights between -1 and 1
}
return $weights;
}
}

View File

@@ -2,13 +2,15 @@
namespace App\Services\SynapticWeightsProvider;
class ZeroSynapticWeights implements ISynapticWeightsProvider {
class ZeroSynapticWeights implements ISynapticWeightsProvider
{
public function generate(int $input_size): array
{
$weights = [];
for ($i = 0; $i < $input_size + 1; $i++) { // +1 for bias weight
$weights[] = 0; // Zero weights
}
return $weights;
}
}