-module(gleam_synapses@model@encoding@preprocessor). -compile(no_auto_import). -export([init/2, encode/2, decode/2, to_json/1, of_json/1, realized/1]). -spec init( gleam_zlists@interop:z_list({binary(), boolean()}), gleam@iterator:iterator(gleam@map:map_(binary(), binary())) ) -> gleam_zlists@interop:z_list(gleam_synapses@model@encoding@attribute@attribute:attribute()). init(Keys_with_flags, Dataset) -> {next, Head@1, Tail@1} = case gleam@iterator:step(Dataset) of {next, Head, Tail} -> {next, Head, Tail}; _try -> erlang:error(#{gleam_error => assert, message => <<"Assertion pattern match failed"/utf8>>, value => _try, module => <<"gleam_synapses/model/encoding/preprocessor"/utf8>>, function => <<"init"/utf8>>, line => 20}) end, _pipe = Keys_with_flags, _pipe@1 = gleam_zlists:map( _pipe, fun(T) -> {Key, Is_distinct} = T, {ok, Str_val@1} = case gleam@map:get(Head@1, Key) of {ok, Str_val} -> {ok, Str_val}; _try@1 -> erlang:error(#{gleam_error => assert, message => <<"Assertion pattern match failed"/utf8>>, value => _try@1, module => <<"gleam_synapses/model/encoding/preprocessor"/utf8>>, function => <<"init"/utf8>>, line => 24}) end, case Is_distinct of true -> {discrete, Key, gleam_zlists:singleton(Str_val@1)}; false -> V = gleam_synapses@model@encoding@attribute@attribute:parse( Str_val@1 ), {continuous, Key, V, V} end end ), gleam@iterator:fold( Tail@1, _pipe@1, fun(Acc, X) -> gleam_zlists:map( Acc, fun(Attr) -> gleam_synapses@model@encoding@attribute@attribute:updated( Attr, X ) end ) end ). -spec encode( gleam_zlists@interop:z_list(gleam_synapses@model@encoding@attribute@attribute:attribute()), gleam@map:map_(binary(), binary()) ) -> gleam_zlists@interop:z_list(float()). encode(Preprocessor, Datapoint) -> gleam_zlists:flat_map( Preprocessor, fun(Attr) -> Key = case Attr of {discrete, K, _@1} -> K; {continuous, K@1, _@2, _@3} -> K@1 end, {ok, V@1} = case gleam@map:get(Datapoint, Key) of {ok, V} -> {ok, V}; _try -> erlang:error(#{gleam_error => assert, message => <<"Assertion pattern match failed"/utf8>>, value => _try, module => <<"gleam_synapses/model/encoding/preprocessor"/utf8>>, function => <<"encode"/utf8>>, line => 53}) end, gleam_synapses@model@encoding@attribute@attribute:encode(Attr, V@1) end ). -spec decode_acc_f( gleam_synapses@model@encoding@attribute@attribute:attribute(), {gleam_zlists@interop:z_list(float()), gleam_zlists@interop:z_list({binary(), binary()})} ) -> {gleam_zlists@interop:z_list(float()), gleam_zlists@interop:z_list({binary(), binary()})}. decode_acc_f(Attr, Acc) -> {Unprocessed_floats, Processed_ks_vs} = Acc, {Key@2, Split_index} = case Attr of {discrete, Key, Values} -> {Key, gleam_zlists:count(Values)}; {continuous, Key@1, _@1, _@2} -> {Key@1, 1} end, {Encoded_values_ls, Next_floats_zls} = gleam_zlists:split( Unprocessed_floats, Split_index ), Decoded_value = begin _pipe = Encoded_values_ls, _pipe@1 = gleam_zlists:of_list(_pipe), gleam_synapses@model@encoding@attribute@attribute:decode(Attr, _pipe@1) end, Next_ks_vs = gleam_zlists:cons(Processed_ks_vs, {Key@2, Decoded_value}), {Next_floats_zls, Next_ks_vs}. -spec decode( gleam_zlists@interop:z_list(gleam_synapses@model@encoding@attribute@attribute:attribute()), gleam_zlists@interop:z_list(float()) ) -> gleam@map:map_(binary(), binary()). decode(Preprocessor, Encoded_datapoint) -> _pipe = Preprocessor, _pipe@1 = gleam_zlists:reduce( _pipe, {Encoded_datapoint, gleam_zlists:new()}, fun(X, Acc) -> decode_acc_f(X, Acc) end ), _pipe@2 = gleam@pair:second(_pipe@1), _pipe@3 = gleam_zlists:to_list(_pipe@2), gleam@map:from_list(_pipe@3). -spec to_json( gleam_zlists@interop:z_list(gleam_synapses@model@encoding@attribute@attribute:attribute()) ) -> binary(). to_json(Preprocessor) -> _pipe = Preprocessor, _pipe@1 = gleam_zlists:map( _pipe, fun gleam_synapses@model@encoding@attribute@attribute_serialized:serialized/1 ), _pipe@2 = gleam_zlists:to_list(_pipe@1), _pipe@3 = gleam@json:array(_pipe@2, fun gleam@function:identity/1), gleam@json:to_string(_pipe@3). -spec of_json(binary()) -> gleam_zlists@interop:z_list(gleam_synapses@model@encoding@attribute@attribute:attribute()). of_json(S) -> {ok, Res@1} = case begin _pipe = gleam_synapses@model@encoding@attribute@attribute_serialized:decoder( ), _pipe@1 = gleam@dynamic:list(_pipe), gleam@json:decode(S, _pipe@1) end of {ok, Res} -> {ok, Res}; _try -> erlang:error(#{gleam_error => assert, message => <<"Assertion pattern match failed"/utf8>>, value => _try, module => <<"gleam_synapses/model/encoding/preprocessor"/utf8>>, function => <<"of_json"/utf8>>, line => 101}) end, gleam_zlists:of_list(Res@1). -spec realized( gleam_zlists@interop:z_list(gleam_synapses@model@encoding@attribute@attribute:attribute()) ) -> gleam_zlists@interop:z_list(gleam_synapses@model@encoding@attribute@attribute:attribute()). realized(Preprocessor) -> _pipe = Preprocessor, _pipe@1 = gleam_zlists:map( _pipe, fun gleam_synapses@model@encoding@attribute@attribute_serialized:realized/1 ), gleam_zlists:to_list(_pipe@1), Preprocessor.