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

Function test_custom_model_ans

tests/python/test_docexamples_f32.py:676–729  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

674
675
676def test_custom_model_ans():
677 def fixed_model_params():
678 model_scipy = scipy.stats.cauchy(loc=10.3, scale=5.8)
679 # Wrap the scipy-model in a `CustomModel`, which will implicitly
680 # quantize it to integers in the given range from -100 to 100 (both
681 # ends inclusively).
682 model = constriction.stream.model.CustomModel(
683 model_scipy.cdf, model_scipy.ppf, -100, 100)
684
685 symbols = np.array([5, 14, -1, 21], dtype=np.int32)
686 coder = constriction.stream.stack.AnsCoder()
687 coder.encode_reverse(symbols, model)
688 assert np.all(coder.decode(model, 4) == symbols)
689
690 def variable_model_params():
691 # The optional argument `params` will receive a 1-d python array when
692 # the model is used for encoding or decoding.
693 model = constriction.stream.model.CustomModel(
694 lambda x, loc, scale: scipy.stats.cauchy.cdf(x, loc, scale),
695 lambda x, loc, scale: scipy.stats.cauchy.ppf(x, loc, scale),
696 -100, 100)
697
698 model_parameters = np.array([
699 (7.3, 3.9), # Location and scale of entropy model for 1st symbol.
700 (11.5, 5.2), # Location and scale of entropy model for 2nd symbol.
701 (-3.2, 4.9), # and so on ...
702 (25.9, 7.1),
703 ])
704
705 symbols = np.array([5, 14, -1, 21], dtype=np.int32)
706 coder = constriction.stream.stack.AnsCoder()
707 coder.encode_reverse(
708 symbols, model, model_parameters[:, 0].copy(), model_parameters[:, 1].copy())
709 assert np.all(
710 coder.decode(model, model_parameters[:, 0].copy(), model_parameters[:, 1].copy()) == symbols)
711
712 def discrete_distribution():
713 model = constriction.stream.model.CustomModel(
714 lambda x, params: scipy.stats.binom.cdf(x, n=10, p=params),
715 lambda x, params: scipy.stats.binom.ppf(x, n=10, p=params),
716 0, 10)
717
718 success_probabilities = np.array([0.3, 0.7, 0.2, 0.6])
719
720 symbols = np.array([4, 8, 1, 5], dtype=np.int32)
721 coder = constriction.stream.stack.AnsCoder()
722 coder.encode_reverse(
723 symbols, model, success_probabilities)
724 assert np.all(
725 coder.decode(model, success_probabilities) == symbols)
726
727 fixed_model_params()
728 variable_model_params()
729 discrete_distribution()
730
731
732def test_model_mod1():

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