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

Function test_custom_model_range

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

Source from the content-addressed store, hash-verified

869
870
871def test_custom_model_range():
872 def fixed_model_params():
873 model_scipy = scipy.stats.cauchy(loc=10.3, scale=5.8)
874 # Wrap the scipy-model in a `CustomModel`, which will implicitly
875 # quantize it to integers in the given range from -100 to 100 (both
876 # ends inclusively).
877 model = constriction.stream.model.CustomModel(
878 model_scipy.cdf, model_scipy.ppf, -100, 100)
879
880 symbols = np.array([5, 14, -1, 21], dtype=np.int32)
881 encoder = constriction.stream.queue.RangeEncoder()
882 encoder.encode(symbols, model)
883 compressed = encoder.get_compressed()
884 decoder = constriction.stream.queue.RangeDecoder(compressed)
885 assert np.all(decoder.decode(model, 4) == symbols)
886
887 def variable_model_params():
888 # The optional argument `params` will receive a 1-d python array when
889 # the model is used for encoding or decoding.
890 model = constriction.stream.model.CustomModel(
891 lambda x, loc, scale: scipy.stats.cauchy.cdf(x, loc, scale),
892 lambda x, loc, scale: scipy.stats.cauchy.ppf(x, loc, scale),
893 -100, 100)
894
895 model_parameters = np.array([
896 (7.3, 3.9), # Location and scale of entropy model for 1st symbol.
897 (11.5, 5.2), # Location and scale of entropy model for 2nd symbol.
898 (-3.2, 4.9), # and so on ...
899 (25.9, 7.1),
900 ])
901
902 symbols = np.array([5, 14, -1, 21], dtype=np.int32)
903 encoder = constriction.stream.queue.RangeEncoder()
904 encoder.encode(
905 symbols, model, model_parameters[:, 0].copy(), model_parameters[:, 1].copy())
906 compressed = encoder.get_compressed()
907 decoder = constriction.stream.queue.RangeDecoder(compressed)
908 assert np.all(
909 decoder.decode(model, model_parameters[:, 0].copy(), model_parameters[:, 1].copy()) == symbols)
910
911 def discrete_distribution():
912 model = constriction.stream.model.CustomModel(
913 lambda x, params: scipy.stats.binom.cdf(x, n=10, p=params),
914 lambda x, params: scipy.stats.binom.ppf(x, n=10, p=params),
915 0, 10)
916
917 success_probabilities = np.array([0.3, 0.7, 0.2, 0.6])
918
919 symbols = np.array([4, 8, 1, 5], dtype=np.int32)
920 encoder = constriction.stream.queue.RangeEncoder()
921 encoder.encode(symbols, model, success_probabilities)
922 compressed = encoder.get_compressed()
923 decoder = constriction.stream.queue.RangeDecoder(compressed)
924 assert np.all(
925 decoder.decode(model, success_probabilities) == symbols)
926
927 fixed_model_params()
928 variable_model_params()

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