()
| 46 | |
| 47 | |
| 48 | def 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 | |
| 90 | def test_module_example3(): |
nothing calls this directly
no test coverage detected