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

Function test_ans_example

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

Source from the content-addressed store, hash-verified

499
500
501def test_ans_example():
502 ans = constriction.stream.stack.AnsCoder() # No arguments => empty ANS coder
503
504 model = constriction.stream.model.QuantizedGaussian(-10, 10)
505 symbols = np.array([2, -1, 0, 2, 3], dtype=np.int32)
506 means = np.array([2.3, -1.7, 0.1, 2.2, -5.1], dtype=np.float64)
507 stds = np.array([1.1, 5.3, 3.8, 1.4, 3.9], dtype=np.float64)
508
509 ans.encode_reverse(symbols, model, means, stds)
510
511 print(f"Compressed size: {ans.num_valid_bits()} bits")
512
513 compressed = ans.get_compressed()
514 if sys.byteorder == "big":
515 # Convert native byte order to a consistent one (here: little endian).
516 compressed.byteswap(inplace=True)
517
518 if sys.byteorder == "big":
519 # Convert little endian byte order to native byte order.
520 compressed.byteswap(inplace=True)
521
522 ans = constriction.stream.stack.AnsCoder(compressed)
523
524 min_supported_symbol, max_supported_symbol = -10, 10 # both inclusively
525 means = np.array([2.3, -1.7, 0.1, 2.2, -5.1], dtype=np.float64)
526 stds = np.array([1.1, 5.3, 3.8, 1.4, 3.9], dtype=np.float64)
527
528 reconstructed = ans.decode(model, means, stds)
529 assert ans.is_empty()
530 assert np.all(reconstructed == symbols)
531
532
533def test_range_coder_encode1():

Callers

nothing calls this directly

Calls 5

encode_reverseMethod · 0.95
num_valid_bitsMethod · 0.95
get_compressedMethod · 0.95
decodeMethod · 0.95
is_emptyMethod · 0.95

Tested by

no test coverage detected