()
| 119 | |
| 120 | |
| 121 | def test_chain1(): |
| 122 | # Parameters for a few example Gaussian entropy models: |
| 123 | leaky_gaussian = constriction.stream.model.QuantizedGaussian(-100, 100) |
| 124 | means = np.array([3.2, -14.3, 5.7], dtype=np.float32) |
| 125 | stds = np.array([6.4, 4.2, 3.9], dtype=np.float32) |
| 126 | |
| 127 | def run_encoder_part(side_information): |
| 128 | # Construct a `ChainCoder` for *decoding*: |
| 129 | coder = constriction.stream.chain.ChainCoder( |
| 130 | side_information, # Provided bit string. |
| 131 | is_remainders=False, # Bit string is *not* remaining data after decoding. |
| 132 | seal=True # Bit string comes from an external source here. |
| 133 | ) |
| 134 | # Decode side information into a sequence of symbols as usual in bits-back coding: |
| 135 | symbols = coder.decode(leaky_gaussian, means, stds) |
| 136 | # Obtain what's *remaining* on the coder after decoding the symbols: |
| 137 | remaining1, remaining2 = coder.get_remainders() |
| 138 | return symbols, np.concatenate([remaining1, remaining2]) |
| 139 | |
| 140 | def run_decoder_part(symbols, remaining): |
| 141 | # Construct a `ChainCoder` for *encoding*: |
| 142 | coder = constriction.stream.chain.ChainCoder( |
| 143 | remaining, # Provided bit string. |
| 144 | is_remainders=True, # Bit string *is* remaining data after decoding. |
| 145 | seal=False # Bit string comes from a `ChainCoder`, no need to seal it. |
| 146 | ) |
| 147 | # Re-encode the symbols to recover the side information: |
| 148 | coder.encode_reverse(symbols, leaky_gaussian, means, stds) |
| 149 | # Obtain the reconstructed data |
| 150 | data1, data2 = coder.get_data(unseal=True) |
| 151 | return np.concatenate([data1, data2]) |
| 152 | |
| 153 | np.random.seed(123) |
| 154 | sample_side_information = np.random.randint(2**32, size=10, dtype=np.uint32) |
| 155 | symbols, remaining = run_encoder_part(sample_side_information) |
| 156 | recovered = run_decoder_part(symbols, remaining) |
| 157 | assert np.all(recovered == sample_side_information) |
| 158 | |
| 159 | |
| 160 | def test_chain2(): |
nothing calls this directly
no test coverage detected