add ar8 lossy
1 changed file+43−42
python/ephys_compression_tests/algorithms/ans/__init__.pymodified+43−42View file
@@ -156,14 +156,14 @@ for a in algorithm_dicts_base:
156156
157157 # add delta encoding
158158 for a in algorithm_dicts_base:
159- def encode0_ar2_lossy(x: np.ndarray, a=a) -> bytes:
159+ def encode0_ar_lossy(x: np.ndarray, a=a) -> bytes:
160160 x_diff = np.diff(x)
161161 x0 = x[0:1]
162162 encoded_diff = a["encode"](x_diff)
163163 # Store the first value at the start
164164 first_value_bytes = x0.tobytes()
165165 return first_value_bytes + encoded_diff
166- def decode0_ar2_lossy(x: bytes, dtype: str, shape: tuple, a=a) -> np.ndarray:
166+ def decode0_ar_lossy(x: bytes, dtype: str, shape: tuple, a=a) -> np.ndarray:
167167 dtype_np = np.dtype(dtype)
168168 num_bytes_first_value = dtype_np.itemsize
169169 first_value_bytes = x[:num_bytes_first_value]
@@ -177,8 +177,8 @@ for a in algorithm_dicts_base:
177177 algorithm_dicts.append({
178178 "name": a["name"] + "-delta",
179179 "version": a["version"],
180- "encode": encode0_ar2_lossy,
181- "decode": decode0_ar2_lossy,
180+ "encode": encode0_ar_lossy,
181+ "decode": decode0_ar_lossy,
182182 "description": a["description"] + " with delta encoding",
183183 "tags": a["tags"] + ["delta"],
184184 "source_file": a["source_file"],
@@ -187,7 +187,7 @@ for a in algorithm_dicts_base:
187187
188188 # add delta2 encoding
189189 for a in algorithm_dicts_base:
190- def encode0_ar2_lossy(x: np.ndarray, a=a) -> bytes:
190+ def encode0_ar_lossy(x: np.ndarray, a=a) -> bytes:
191191 x_diff = np.diff(np.diff(x))
192192 x0 = x[0:1]
193193 encoded_diff = a["encode"](x_diff)
@@ -195,7 +195,7 @@ for a in algorithm_dicts_base:
195195 first_value_bytes = x0.tobytes()
196196 second_value_bytes = x[1:2].tobytes()
197197 return first_value_bytes + second_value_bytes + encoded_diff
198- def decode0_ar2_lossy(x: bytes, dtype: str, shape: tuple, a=a) -> np.ndarray:
198+ def decode0_ar_lossy(x: bytes, dtype: str, shape: tuple, a=a) -> np.ndarray:
199199 dtype_np = np.dtype(dtype)
200200 num_bytes_first_value = dtype_np.itemsize
201201 first_value_bytes = x[:num_bytes_first_value]
@@ -214,8 +214,8 @@ for a in algorithm_dicts_base:
214214 algorithm_dicts.append({
215215 "name": a["name"] + "-delta2",
216216 "version": a["version"],
217- "encode": encode0_ar2_lossy,
218- "decode": decode0_ar2_lossy,
217+ "encode": encode0_ar_lossy,
218+ "decode": decode0_ar_lossy,
219219 "description": a["description"] + " with delta2 encoding",
220220 "tags": a["tags"] + ["delta2"],
221221 "source_file": a["source_file"],
@@ -225,13 +225,13 @@ for a in algorithm_dicts_base:
225225 # Add auto-regressive prediction encoding
226226 for a in algorithm_dicts_base:
227227 for order in [2, 8]:
228- def encode0_ar2_lossy(x: np.ndarray, a=a, order=order) -> bytes:
228+ def encode0_ar_lossy(x: np.ndarray, a=a, order=order) -> bytes:
229229 coeffs, residuals, initial_values = encode_ar(x, order=order)
230230 encoded_residuals = a["encode"](residuals)
231231 coeffs_bytes = coeffs.astype(np.float32).tobytes()
232232 initial_values_bytes = initial_values.astype(np.int16).tobytes()
233233 return coeffs_bytes + initial_values_bytes + encoded_residuals
234- def decode0_ar2_lossy(x: bytes, dtype: str, shape: tuple, a=a, order=order) -> np.ndarray:
234+ def decode0_ar_lossy(x: bytes, dtype: str, shape: tuple, a=a, order=order) -> np.ndarray:
235235 dtype_np = np.dtype(dtype)
236236 num_bytes_coeffs = order * np.dtype(np.float32).itemsize
237237 coeffs_bytes = x[:num_bytes_coeffs]
@@ -247,8 +247,8 @@ for a in algorithm_dicts_base:
247247 algorithm_dicts.append({
248248 "name": a["name"] + f"-ar{order}",
249249 "version": a["version"],
250- "encode": encode0_ar2_lossy,
251- "decode": decode0_ar2_lossy,
250+ "encode": encode0_ar_lossy,
251+ "decode": decode0_ar_lossy,
252252 "description": a["description"] + f" with auto-regressive prediction encoding of order {order}",
253253 "tags": a["tags"] + [f"ar{order}"],
254254 "source_file": a["source_file"],
@@ -256,36 +256,37 @@ for a in algorithm_dicts_base:
256256 })
257257
258258 # Add lossy ar2
259-for tolerance in [1, 2, 3, 4, 5]:
260- def encode0_ar2_lossy(x: np.ndarray, tolerance=tolerance) -> bytes:
261- coeffs, residuals, initial_values = encode_ar_lossy(x, order=2, step=tolerance * 2 + 1)
262- encoded_residuals = ans_encode_0(residuals)
263- coeffs_bytes = coeffs.astype(np.float32).tobytes()
264- initial_values_bytes = initial_values.astype(np.int16).tobytes()
265- return coeffs_bytes + initial_values_bytes + encoded_residuals
266- def decode0_ar2_lossy(x: bytes, dtype: str, shape: tuple) -> np.ndarray:
267- dtype_np = np.dtype(dtype)
268- num_bytes_coeffs = 2 * np.dtype(np.float32).itemsize
269- coeffs_bytes = x[:num_bytes_coeffs]
270- coeffs = np.frombuffer(coeffs_bytes, dtype=np.float32)
271- num_initial_values = len(coeffs)
272- num_bytes_initial_values = num_initial_values * dtype_np.itemsize
273- initial_values_bytes = x[num_bytes_coeffs : num_bytes_coeffs + num_bytes_initial_values]
274- initial_values = np.frombuffer(initial_values_bytes, dtype=dtype_np)
275- encoded_residuals = x[num_bytes_coeffs + num_bytes_initial_values :]
276- residuals = ans_decode_0(encoded_residuals, dtype, (shape[0]-num_initial_values,))
277- reconstructed = decode_ar(coeffs, residuals, initial_values)
278- return reconstructed.reshape(shape)
279- algorithm_dicts.append({
280- "name": f"ans-ar2-lossy-tol{tolerance}",
281- "version": "1",
282- "encode": encode0_ar2_lossy,
283- "decode": decode0_ar2_lossy,
284- "description": f"ANS with lossy auto-regressive prediction encoding of order 2 and tolerance {tolerance}",
285- "tags": ["ans", "lossy", "ar2"],
286- "source_file": SOURCE_FILE,
287- "long_description": LONG_DESCRIPTION
288- })
259+for ar_order in [2, 8]:
260+ for tolerance in [1, 2, 3, 4, 5]:
261+ def encode0_ar_lossy(x: np.ndarray, tolerance=tolerance, order=ar_order) -> bytes:
262+ coeffs, residuals, initial_values = encode_ar_lossy(x, order=order, step=tolerance * 2 + 1)
263+ encoded_residuals = ans_encode_0(residuals)
264+ coeffs_bytes = coeffs.astype(np.float32).tobytes()
265+ initial_values_bytes = initial_values.astype(np.int16).tobytes()
266+ return coeffs_bytes + initial_values_bytes + encoded_residuals
267+ def decode0_ar_lossy(x: bytes, dtype: str, shape: tuple) -> np.ndarray:
268+ dtype_np = np.dtype(dtype)
269+ num_bytes_coeffs = 2 * np.dtype(np.float32).itemsize
270+ coeffs_bytes = x[:num_bytes_coeffs]
271+ coeffs = np.frombuffer(coeffs_bytes, dtype=np.float32)
272+ num_initial_values = len(coeffs)
273+ num_bytes_initial_values = num_initial_values * dtype_np.itemsize
274+ initial_values_bytes = x[num_bytes_coeffs : num_bytes_coeffs + num_bytes_initial_values]
275+ initial_values = np.frombuffer(initial_values_bytes, dtype=dtype_np)
276+ encoded_residuals = x[num_bytes_coeffs + num_bytes_initial_values :]
277+ residuals = ans_decode_0(encoded_residuals, dtype, (shape[0]-num_initial_values,))
278+ reconstructed = decode_ar(coeffs, residuals, initial_values)
279+ return reconstructed.reshape(shape)
280+ algorithm_dicts.append({
281+ "name": f"ans-ar{ar_order}-lossy-tol{tolerance}",
282+ "version": "1",
283+ "encode": encode0_ar_lossy,
284+ "decode": decode0_ar_lossy,
285+ "description": f"ANS with lossy auto-regressive prediction encoding of order {ar_order} and tolerance {tolerance}",
286+ "tags": ["ans", "lossy", f"ar{ar_order}"],
287+ "source_file": SOURCE_FILE,
288+ "long_description": LONG_DESCRIPTION
289+ })
289290
290291 algorithms = [
291292 Algorithm(**a)