()
| 762 | |
| 763 | |
| 764 | def 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 | |
| 781 | def test_categorical2(): |
nothing calls this directly
no test coverage detected