MCPcopy Create free account
hub / github.com/bamler-lab/constriction / test_old_custom_model_chain

Function test_old_custom_model_chain

tests/python/test_docexamples.py:932–992  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

930
931
932def test_old_custom_model_chain():
933 compressed = np.array(
934 [0xa5dd25f7, 0xfaef49b5, 0xd5b12228, 0x156ceb98, 0x71a0a92b,
935 0x99e6d365, 0x2eebfadb, 0x404a567b, 0xf6cbdc09, 0xe63f3848],
936 dtype=np.uint32)
937
938 def fixed_model_params():
939 model_scipy = scipy.stats.cauchy(loc=10.3, scale=5.8)
940 # Wrap the scipy-model in a `CustomModel`, which will implicitly
941 # quantize it to integers in the given range from -100 to 100 (both
942 # ends inclusively).
943 model = constriction.stream.model.CustomModel(
944 model_scipy.cdf, model_scipy.ppf, -100, 100)
945
946 coder = constriction.stream.chain.ChainCoder(compressed, False, False)
947 symbols = coder.decode(model, 4)
948 assert np.all(symbols == np.array([18, 6, 33, 59]))
949 coder.encode_reverse(symbols, model)
950 assert np.all(np.hstack(coder.get_data()) == compressed)
951
952 def variable_model_params():
953 # The optional argument `params` will receive a 1-d python array when
954 # the model is used for encoding or decoding.
955 model = constriction.stream.model.CustomModel(
956 lambda x, loc, scale: scipy.stats.cauchy.cdf(x, loc, scale),
957 lambda x, loc, scale: scipy.stats.cauchy.ppf(x, loc, scale),
958 -100, 100)
959
960 model_parameters = np.array([
961 (7.3, 3.9), # Location and scale of entropy model for 1st symbol.
962 (11.5, 5.2), # Location and scale of entropy model for 2nd symbol.
963 (-3.2, 4.9), # and so on ...
964 (25.9, 7.1),
965 ])
966
967 coder = constriction.stream.chain.ChainCoder(compressed, False, False)
968 symbols = coder.decode(
969 model, model_parameters[:, 0].copy(), model_parameters[:, 1].copy())
970 assert np.all(symbols == np.array([13, 7, 16, 85]))
971 coder.encode_reverse(
972 symbols, model, model_parameters[:, 0].copy(), model_parameters[:, 1].copy())
973 assert np.all(np.hstack(coder.get_data()) == compressed)
974
975 def discrete_distribution():
976 model = constriction.stream.model.CustomModel(
977 lambda x, params: scipy.stats.binom.cdf(x, n=10, p=params),
978 lambda x, params: scipy.stats.binom.ppf(x, n=10, p=params),
979 0, 10)
980
981 success_probabilities = np.array([0.3, 0.7, 0.2, 0.6])
982
983 coder = constriction.stream.chain.ChainCoder(compressed, False, False)
984 symbols = coder.decode(model, success_probabilities)
985 assert np.all(symbols == np.array([4, 6, 4, 9]))
986 coder.encode_reverse(
987 symbols, model, success_probabilities)
988 assert np.all(np.hstack(coder.get_data()) == compressed)
989

Callers

nothing calls this directly

Calls 3

fixed_model_paramsFunction · 0.70
variable_model_paramsFunction · 0.70
discrete_distributionFunction · 0.70

Tested by

no test coverage detected