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

Function test_ans_example

tests/python/test_docexamples_f32.py:503–532  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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