-module(gleam_synapses@model@encoding@preprocessor_test). -compile(no_auto_import). -export([encode_dataset_test/0, decode_dataset_test/0, preprocessor_to_json_test/0, preprocessor_of_to_json_test/0]). -spec datapoint_1() -> gleam@map:map_(binary(), binary()). datapoint_1() -> gleam@map:from_list( [{<<"a"/utf8>>, <<"a1"/utf8>>}, {<<"b"/utf8>>, <<"b1"/utf8>>}, {<<"c"/utf8>>, <<"-8.0"/utf8>>}, {<<"d"/utf8>>, <<"3"/utf8>>}] ). -spec datapoint_2() -> gleam@map:map_(binary(), binary()). datapoint_2() -> gleam@map:insert(datapoint_1(), <<"b"/utf8>>, <<"b2"/utf8>>). -spec datapoint_3() -> gleam@map:map_(binary(), binary()). datapoint_3() -> gleam@map:insert(datapoint_1(), <<"b"/utf8>>, <<"b3"/utf8>>). -spec datapoint_4() -> gleam@map:map_(binary(), binary()). datapoint_4() -> gleam@map:insert( gleam@map:insert(datapoint_1(), <<"b"/utf8>>, <<"b4"/utf8>>), <<"d"/utf8>>, <<"5.0"/utf8>> ). -spec datapoint_5() -> gleam@map:map_(binary(), binary()). datapoint_5() -> gleam@map:insert( gleam@map:insert(datapoint_1(), <<"b"/utf8>>, <<"b5"/utf8>>), <<"d"/utf8>>, <<"4.0"/utf8>> ). -spec dataset() -> gleam_zlists@interop:z_list(gleam@map:map_(binary(), binary())). dataset() -> gleam_zlists:of_list( [datapoint_1(), datapoint_2(), datapoint_3(), datapoint_4(), datapoint_5()] ). -spec keys_with_flags() -> gleam_zlists@interop:z_list({binary(), boolean()}). keys_with_flags() -> gleam_zlists:of_list( [{<<"a"/utf8>>, true}, {<<"b"/utf8>>, true}, {<<"c"/utf8>>, false}, {<<"d"/utf8>>, false}] ). -spec my_preprocessor() -> gleam_zlists@interop:z_list(gleam_synapses@model@encoding@serialization:attribute()). my_preprocessor() -> gleam_synapses@model@encoding@preprocessor:init( keys_with_flags(), dataset() ). -spec encoded_dataset() -> gleam_zlists@interop:z_list(gleam_zlists@interop:z_list(float())). encoded_dataset() -> gleam_zlists:map( dataset(), fun(X) -> gleam_synapses@model@encoding@preprocessor:encode( my_preprocessor(), X ) end ). -spec decoded_dataset() -> gleam_zlists@interop:z_list(gleam@map:map_(binary(), binary())). decoded_dataset() -> gleam_zlists:map( encoded_dataset(), fun(X) -> gleam_synapses@model@encoding@preprocessor:decode( my_preprocessor(), X ) end ). -spec encode_dataset_test() -> gleam@should:expectation(). encode_dataset_test() -> gleam@should:equal( gleam_zlists:to_list( gleam_zlists:map(encoded_dataset(), fun gleam_zlists:to_list/1) ), [[1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.5, 0.0], [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.5, 0.0], [1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.5, 0.0], [1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.5, 1.0], [1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5]] ). -spec decode_dataset_test() -> gleam@should:expectation(). decode_dataset_test() -> gleam@should:equal( gleam_zlists:to_list(decoded_dataset()), [gleam@map:from_list( [{<<"a"/utf8>>, <<"a1"/utf8>>}, {<<"b"/utf8>>, <<"b1"/utf8>>}, {<<"c"/utf8>>, <<"-8.0"/utf8>>}, {<<"d"/utf8>>, <<"3.0"/utf8>>}] ), gleam@map:from_list( [{<<"a"/utf8>>, <<"a1"/utf8>>}, {<<"b"/utf8>>, <<"b2"/utf8>>}, {<<"c"/utf8>>, <<"-8.0"/utf8>>}, {<<"d"/utf8>>, <<"3.0"/utf8>>}] ), gleam@map:from_list( [{<<"a"/utf8>>, <<"a1"/utf8>>}, {<<"b"/utf8>>, <<"b3"/utf8>>}, {<<"c"/utf8>>, <<"-8.0"/utf8>>}, {<<"d"/utf8>>, <<"3.0"/utf8>>}] ), gleam@map:from_list( [{<<"a"/utf8>>, <<"a1"/utf8>>}, {<<"b"/utf8>>, <<"b4"/utf8>>}, {<<"c"/utf8>>, <<"-8.0"/utf8>>}, {<<"d"/utf8>>, <<"5.0"/utf8>>}] ), gleam@map:from_list( [{<<"a"/utf8>>, <<"a1"/utf8>>}, {<<"b"/utf8>>, <<"b5"/utf8>>}, {<<"c"/utf8>>, <<"-8.0"/utf8>>}, {<<"d"/utf8>>, <<"4.0"/utf8>>}] )] ). -spec preprocessor_to_json_test() -> gleam@should:expectation(). preprocessor_to_json_test() -> gleam@should:equal( gleam_synapses@model@encoding@preprocessor:to_json(my_preprocessor()), <<"[{\"Case\":\"SerializableDiscrete\",\"Fields\":[{\"key\":\"a\",\"values\":[\"a1\"]}]},{\"Case\":\"SerializableDiscrete\",\"Fields\":[{\"key\":\"b\",\"values\":[\"b5\",\"b4\",\"b3\",\"b2\",\"b1\"]}]},{\"Case\":\"SerializableContinuous\",\"Fields\":[{\"key\":\"c\",\"max\":-8.0,\"min\":-8.0}]},{\"Case\":\"SerializableContinuous\",\"Fields\":[{\"key\":\"d\",\"max\":5.0,\"min\":3.0}]}]"/utf8>> ). -spec preprocessor_of_to_json_test() -> gleam@should:expectation(). preprocessor_of_to_json_test() -> gleam@should:equal( gleam_synapses@model@encoding@preprocessor:to_json( gleam_synapses@model@encoding@preprocessor:of_json( <<"[{\"Case\":\"SerializableDiscrete\",\"Fields\":[{\"key\":\"a\",\"values\":[\"a1\"]}]},{\"Case\":\"SerializableDiscrete\",\"Fields\":[{\"key\":\"b\",\"values\":[\"b5\",\"b4\",\"b3\",\"b2\",\"b1\"]}]},{\"Case\":\"SerializableContinuous\",\"Fields\":[{\"key\":\"c\",\"max\":-8.0,\"min\":-8.0}]},{\"Case\":\"SerializableContinuous\",\"Fields\":[{\"key\":\"d\",\"max\":5.0,\"min\":3.0}]}]"/utf8>> ) ), <<"[{\"Case\":\"SerializableDiscrete\",\"Fields\":[{\"key\":\"a\",\"values\":[\"a1\"]}]},{\"Case\":\"SerializableDiscrete\",\"Fields\":[{\"key\":\"b\",\"values\":[\"b5\",\"b4\",\"b3\",\"b2\",\"b1\"]}]},{\"Case\":\"SerializableContinuous\",\"Fields\":[{\"key\":\"c\",\"max\":-8.0,\"min\":-8.0}]},{\"Case\":\"SerializableContinuous\",\"Fields\":[{\"key\":\"d\",\"max\":5.0,\"min\":3.0}]}]"/utf8>> ).