Some bugfixes and misc
This commit is contained in:
@@ -4,12 +4,12 @@ namespace App\Events;
|
||||
|
||||
use Illuminate\Broadcasting\Channel;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class PerceptronTrainingIteration implements ShouldBroadcast
|
||||
class PerceptronTrainingIteration implements ShouldBroadcastNow
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
@@ -39,19 +39,20 @@ class PerceptronTrainingIteration implements ShouldBroadcast
|
||||
|
||||
public function broadcastWith(): array
|
||||
{
|
||||
$weights = collect($this->iterations)
|
||||
$iterations = self::normalizeForJson($this->iterations);
|
||||
$weights = collect($iterations)
|
||||
->pluck('weights')
|
||||
->first(fn (array $weights): bool => $weights !== []);
|
||||
$shouldBroadcastAllWeights = $weights !== null
|
||||
&& count(Arr::flatten($weights)) <= config('perceptron.max_displayed_weights');
|
||||
|
||||
$lastIterationIndex = count($this->iterations) - 1;
|
||||
$lastIterationIndex = count($iterations) - 1;
|
||||
$iterations = array_map(
|
||||
fn (array $iteration, int $index): array => $shouldBroadcastAllWeights || $index === $lastIterationIndex
|
||||
? $iteration
|
||||
: [...$iteration, 'weights' => []],
|
||||
$this->iterations,
|
||||
array_keys($this->iterations),
|
||||
$iterations,
|
||||
array_keys($iterations),
|
||||
);
|
||||
|
||||
return [
|
||||
@@ -59,4 +60,20 @@ class PerceptronTrainingIteration implements ShouldBroadcast
|
||||
'trainingId' => $this->trainingId,
|
||||
];
|
||||
}
|
||||
|
||||
public static function normalizeForJson(mixed $value): mixed
|
||||
{
|
||||
if (is_float($value) && ! is_finite($value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! is_array($value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return array_map(
|
||||
fn (mixed $item): mixed => self::normalizeForJson($item),
|
||||
$value,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user