Current section
Files
Jump to
Current section
Files
src/gleam_synapses@model@encoding@attribute@attribute.erl
-module(gleam_synapses@model@encoding@attribute@attribute).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([parse/1, updated/2, encode/2, decode/2]).
-export_type([attribute/0]).
-type attribute() :: {discrete, binary(), gleam_zlists@interop:z_list(binary())} |
{continuous, binary(), float(), float()}.
-file("/home/dimos/Projects/Gleam/gleam_synapses/src/gleam_synapses/model/encoding/attribute/attribute.gleam", 15).
-spec parse(binary()) -> float().
parse(S) ->
Trimmed = gleam@string:trim(S),
case {gleam@float:parse(Trimmed), gleam@int:parse(Trimmed)} of
{{ok, X}, _} ->
X;
{_, {ok, X@1}} ->
gleam@int:to_float(X@1);
{{error, _}, {error, _}} ->
erlang:error(#{gleam_error => panic,
message => <<"Numeric value cannot be parsed"/utf8>>,
module => <<"gleam_synapses/model/encoding/attribute/attribute"/utf8>>,
function => <<"parse"/utf8>>,
line => 20})
end.
-file("/home/dimos/Projects/Gleam/gleam_synapses/src/gleam_synapses/model/encoding/attribute/attribute.gleam", 24).
-spec updated(attribute(), gleam@dict:dict(binary(), binary())) -> attribute().
updated(Attribute, Datapoint) ->
case Attribute of
{discrete, Key, Values} ->
_assert_subject = gleam@dict:get(Datapoint, Key),
{ok, V} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"gleam_synapses/model/encoding/attribute/attribute"/utf8>>,
function => <<"updated"/utf8>>,
line => 30})
end,
Updated_values = case gleam_zlists:has_member(Values, V) of
true ->
Values;
false ->
gleam_zlists:cons(Values, V)
end,
{discrete, Key, Updated_values};
{continuous, Key@1, Min, Max} ->
_assert_subject@1 = begin
_pipe = Datapoint,
_pipe@1 = gleam@dict:get(_pipe, Key@1),
gleam@result:map(_pipe@1, fun parse/1)
end,
{ok, V@1} = case _assert_subject@1 of
{ok, _} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@1,
module => <<"gleam_synapses/model/encoding/attribute/attribute"/utf8>>,
function => <<"updated"/utf8>>,
line => 38})
end,
{continuous,
Key@1,
gleam@float:min(V@1, Min),
gleam@float:max(V@1, Max)}
end.
-file("/home/dimos/Projects/Gleam/gleam_synapses/src/gleam_synapses/model/encoding/attribute/attribute.gleam", 47).
-spec encode(attribute(), binary()) -> gleam_zlists@interop:z_list(float()).
encode(Attribute, Value) ->
case Attribute of
{discrete, _, Values} ->
_pipe = Values,
gleam_zlists:map(_pipe, fun(X) -> case X =:= Value of
true ->
1.0;
false ->
+0.0
end end);
{continuous, _, Min, Max} ->
_pipe@1 = case Min =:= Max of
true ->
0.5;
false ->
Nomin = parse(Value) - Min,
Denomin = Max - Min,
case Denomin of
+0.0 -> +0.0;
-0.0 -> -0.0;
Gleam@denominator -> Nomin / Gleam@denominator
end
end,
gleam_zlists:singleton(_pipe@1)
end.
-file("/home/dimos/Projects/Gleam/gleam_synapses/src/gleam_synapses/model/encoding/attribute/attribute.gleam", 70).
-spec decode(attribute(), gleam_zlists@interop:z_list(float())) -> binary().
decode(Attribute, Encoded_values) ->
case Attribute of
{discrete, _, Values} ->
_assert_subject = begin
_pipe = Values,
_pipe@1 = gleam_zlists:zip(_pipe, Encoded_values),
gleam_zlists:uncons(_pipe@1)
end,
{ok, {Hd, Tl}} = case _assert_subject of
{ok, {_, _}} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"gleam_synapses/model/encoding/attribute/attribute"/utf8>>,
function => <<"decode"/utf8>>,
line => 73})
end,
_pipe@2 = gleam_zlists:reduce(
Tl,
Hd,
fun(X, Acc) ->
{_, X_float_val} = X,
{_, Acc_float_val} = Acc,
case X_float_val > Acc_float_val of
true ->
X;
false ->
Acc
end
end
),
gleam@pair:first(_pipe@2);
{continuous, _, Min, Max} ->
_pipe@3 = case Min =:= Max of
true ->
Min;
false ->
_assert_subject@1 = gleam_zlists:head(Encoded_values),
{ok, V} = case _assert_subject@1 of
{ok, _} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@1,
module => <<"gleam_synapses/model/encoding/attribute/attribute"/utf8>>,
function => <<"decode"/utf8>>,
line => 91})
end,
Factor = Max - Min,
(V * Factor) + Min
end,
_pipe@4 = gleam@json:float(_pipe@3),
gleam@json:to_string(_pipe@4)
end.