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

Function test_categorical1

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

Source from the content-addressed store, hash-verified

762
763
764def test_categorical1():
765 # Define a categorical distribution over the (implied) alphabet {0,1,2,3}
766 # with P(X=0) = 0.2, P(X=1) = 0.4, P(X=2) = 0.1, and P(X=3) = 0.3:
767 probabilities = np.array([0.2, 0.4, 0.1, 0.3], dtype=np.float64)
768 model = constriction.stream.model.Categorical(probabilities, perfect=False)
769
770 # Encode and decode an example message:
771 symbols = np.array([0, 3, 2, 3, 2, 0, 2, 1], dtype=np.int32)
772 coder = constriction.stream.stack.AnsCoder() # (RangeEncoder also works)
773 coder.encode_reverse(symbols, model)
774 assert np.all(coder.get_compressed() == np.array(
775 [488222996, 175], dtype=np.uint32))
776
777 reconstructed = coder.decode(model, 8) # (decodes 8 i.i.d. symbols)
778 assert np.all(reconstructed == symbols) # (verify correctness)
779
780
781def test_categorical2():

Callers

nothing calls this directly

Calls 3

encode_reverseMethod · 0.95
get_compressedMethod · 0.95
decodeMethod · 0.95

Tested by

no test coverage detected