-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #233. Introduces `BitroundCodec` with a no-op for decoding because bit-rounding is lossy, and precision cannot be restored. Encoding is not yet implemented, reflecting zarrita's current focus on read-heavy contexts. Open an issue if encoding support is needed.
- Loading branch information
Showing
2 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type { Chunk, Float32, Float64 } from "../metadata.js"; | ||
|
||
/** | ||
* A codec for bit-rounding. | ||
* | ||
* Reduces floating-point precision by truncating mantissa bits during encoding. | ||
* Decoding is a no-op as the process is lossy and precision cannot be restored. | ||
* | ||
* Note: {@link BitroundCodec.encode} is not yet implemented since Zarrita is | ||
* primarily used in read-only contexts (web browser). If you need encoding support, | ||
* please open an issue at {@link https://github.com/manzt/zarrita.js/issues}. | ||
* | ||
* @see {@link https://github.com/zarr-developers/numcodecs/blob/main/numcodecs/bitround.py} | ||
* for the original Python implementation. | ||
* | ||
* @remarks | ||
* Data types are not validated, and `float16` arrays are not supported (reflecting browser support). | ||
*/ | ||
export class BitroundCodec<D extends Float64 | Float32> { | ||
kind = "array_to_array"; | ||
|
||
constructor(configuration: { keepbits: number }, _meta: { data_type: D }) { | ||
if (configuration.keepbits < 0) { | ||
throw new Error("keepbits must be zero or positive"); | ||
} | ||
} | ||
|
||
static fromConfig<D extends Float32 | Float64>( | ||
configuration: { keepbits: number }, | ||
meta: { data_type: D }, | ||
): BitroundCodec<D> { | ||
return new BitroundCodec(configuration, meta); | ||
} | ||
|
||
/** | ||
* Encode a chunk of data with bit-rounding. | ||
* @param _arr - The chunk to encode | ||
*/ | ||
encode(_arr: Chunk<D>): Chunk<D> { | ||
throw new Error( | ||
"`BitroundCodec.encode` is not implemented. Please open an issue at https://github.com/manzt/zarrita.js/issues.", | ||
); | ||
} | ||
|
||
/** | ||
* Decode a chunk of data (no-op). | ||
* @param arr - The chunk to decode | ||
* @returns The decoded chunk | ||
*/ | ||
decode(arr: Chunk<D>): Chunk<D> { | ||
return arr; // No-op as bit-rounding is lossy | ||
} | ||
} |