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

Function test_old_module_example1

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

Source from the content-addressed store, hash-verified

48
49
50def test_old_module_example1():
51 # Create an empty Asymmetric Numeral Systems (ANS) Coder:
52 coder = constriction.stream.stack.AnsCoder()
53
54 # Some made up data and entropy models for demonstration purpose:
55 model = constriction.stream.model.QuantizedGaussian(-100, 100)
56 symbols = np.array([23, -15, 78, 43, -69], dtype=np.int32)
57 means = np.array([35.2, -1.7, 30.1, 71.2, -75.1], dtype=np.float32)
58 stds = np.array([10.1, 25.3, 23.8, 35.4, 3.9], dtype=np.float32)
59
60 # Encode the data (in reverse order, since ANS is a stack):
61 coder.encode_reverse(symbols, model, means, stds)
62
63 print(f"Compressed size: {coder.num_bits()} bits")
64 print(
65 f"(without unnecessary trailing zeros: {coder.num_valid_bits()} bits)")
66
67 # Get the compressed bit string, convert it into an architecture-independent
68 # byte order, and write it to a binary file:
69 compressed = coder.get_compressed()
70 if sys.byteorder == "big":
71 compressed.byteswap(inplace=True)
72
73 # We won't write it to a file her, let's just directly continue decoding.
74
75 if sys.byteorder == "big":
76 compressed.byteswap(inplace=True)
77
78 # Initialize an ANS coder from the compressed bit string:
79 coder = constriction.stream.stack.AnsCoder(compressed)
80
81 # Use the same entropy models that we used for encoding:
82 min_supported_symbol, max_supported_symbol = -100, 100 # both inclusively
83 means = np.array([35.2, -1.7, 30.1, 71.2, -75.1], dtype=np.float32)
84 stds = np.array([10.1, 25.3, 23.8, 35.4, 3.9], dtype=np.float32)
85
86 # Decode and print the data:
87 reconstructed = coder.decode(model, means, stds)
88 assert coder.is_empty()
89 assert np.all(reconstructed == symbols)
90
91
92def test_module_example3():

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected