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

Function test_custom_model_ans

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

Source from the content-addressed store, hash-verified

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