()
| 426 | |
| 427 | |
| 428 | def test_range_coding_mod(): |
| 429 | # Define the two parts of the message and their respective entropy models: |
| 430 | message_part1 = np.array([1, 2, 0, 3, 2, 3, 0], dtype=np.int32) |
| 431 | probabilities_part1 = np.array([0.2, 0.4, 0.1, 0.3], dtype=np.float64) |
| 432 | model_part1 = constriction.stream.model.Categorical(probabilities_part1, perfect=False) |
| 433 | # `model_part1` is a categorical distribution over the (implied) alphabet |
| 434 | # {0,1,2,3} with P(X=0) = 0.2, P(X=1) = 0.4, P(X=2) = 0.1, and P(X=3) = 0.3; |
| 435 | # we will use it below to encode each of the 7 symbols in `message_part1`. |
| 436 | |
| 437 | message_part2 = np.array([6, 10, -4, 2], dtype=np.int32) |
| 438 | means_part2 = np.array([2.5, 13.1, -1.1, -3.0], dtype=np.float64) |
| 439 | stds_part2 = np.array([4.1, 8.7, 6.2, 5.4], dtype=np.float64) |
| 440 | model_family_part2 = constriction.stream.model.QuantizedGaussian(-100, 100) |
| 441 | # `model_family_part2` is a *family* of Gaussian distributions, quantized to |
| 442 | # bins of width 1 centered at the integers -100, -99, ..., 100. We could |
| 443 | # have provided a fixed mean and standard deviation to the constructor of |
| 444 | # `QuantizedGaussian` but we'll instead provide individual means and standard |
| 445 | # deviations for each symbol when we encode and decode `message_part2` below. |
| 446 | |
| 447 | print( |
| 448 | f"Original message: {np.concatenate([message_part1, message_part2])}") |
| 449 | |
| 450 | # Encode both parts of the message in sequence: |
| 451 | encoder = constriction.stream.queue.RangeEncoder() |
| 452 | encoder.encode(message_part1, model_part1) |
| 453 | encoder.encode(message_part2, model_family_part2, means_part2, stds_part2) |
| 454 | |
| 455 | # Get and print the compressed representation: |
| 456 | compressed = encoder.get_compressed() |
| 457 | print(f"compressed representation: {compressed}") |
| 458 | print(f"(in binary: {[bin(word) for word in compressed]})") |
| 459 | |
| 460 | # You could save `compressed` to a file using `compressed.tofile("filename")` |
| 461 | # and read it back in: `compressed = np.fromfile("filename", dtype=np.uint32). |
| 462 | |
| 463 | # Decode the message: |
| 464 | decoder = constriction.stream.queue.RangeDecoder(compressed) |
| 465 | decoded_part1 = decoder.decode(model_part1, 7) # (decodes 7 symbols) |
| 466 | decoded_part2 = decoder.decode(model_family_part2, means_part2, stds_part2) |
| 467 | print(f"Decoded message: {np.concatenate([decoded_part1, decoded_part2])}") |
| 468 | assert np.all(decoded_part1 == message_part1) |
| 469 | assert np.all(decoded_part2 == message_part2) |
| 470 | |
| 471 | |
| 472 | def test_old_module_example2(): |
nothing calls this directly
no test coverage detected