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

Function test_chain1

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 2

run_encoder_partFunction · 0.70
run_decoder_partFunction · 0.70

Tested by

no test coverage detected