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

Function test_old_module_example1

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

Source from the content-addressed store, hash-verified

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