Skip to main content

zarrs/array/codec/array_to_bytes/
zfpy.rs

1//! The `zfpy` array to bytes codec (Experimental).
2//!
3//! <div class="warning">
4//! This codec is experimental and may be incompatible with other Zarr V3 implementations.
5//! </div>
6//!
7//! [zfp](https://zfp.io/) is a compressed number format for 1D to 4D arrays of 32/64-bit floating point or integer data.
8//! 8/16-bit integer types are supported through promotion to 32-bit in accordance with the [zfp utility functions](https://zfp.readthedocs.io/en/release1.0.1/low-level-api.html#utility-functions).
9//!
10//! This codec requires the `zfp` feature, which is disabled by default.
11//!
12//! ### Compatible Implementations
13//! This codec is fully compatible with the `numcodecs.zfpy` codec in `zarr-python`.
14//!
15//! ### Specification
16//! - <https://github.com/zarr-developers/zarr-extensions/tree/numcodecs/codecs/numcodecs.zfpy>
17//! - <https://codec.zarrs.dev/array_to_bytes/zfpy>
18//!
19//! ### Codec `name` Aliases (Zarr V3)
20//! - `numcodecs.zfpy`
21//! - `https://codec.zarrs.dev/array_to_bytes/zfpy`
22//!
23//! ### Codec `id` Aliases (Zarr V2)
24//! - `zfpy`
25//!
26//! ### Codec `configuration` Example - [`ZfpyCodecConfiguration`]:
27//! #### Encode in fixed rate mode with 10.5 compressed bits per value
28//! ```rust
29//! # let JSON = r#"
30//! {
31//!     "mode": 2,
32//!     "rate": 10.5
33//! }
34//! # "#;
35//! # use zarrs::metadata_ext::codec::zfpy::ZfpyCodecConfiguration;
36//! # let configuration: ZfpyCodecConfiguration = serde_json::from_str(JSON).unwrap();
37//! ```
38//!
39//! #### Encode in fixed precision mode with 19 uncompressed bits per value
40//! ```rust
41//! # let JSON = r#"
42//! {
43//!     "mode": 3,
44//!     "precision": 19
45//! }
46//! # "#;
47//! # use zarrs::metadata_ext::codec::zfpy::ZfpyCodecConfiguration;
48//! # let configuration: ZfpyCodecConfiguration = serde_json::from_str(JSON).unwrap();
49//! ```
50//!
51//! #### Encode in fixed accuracy mode with a tolerance of 0.05
52//! ```rust
53//! # let JSON = r#"
54//! {
55//!     "mode": 4,
56//!     "tolerance": 0.05
57//! }
58//! # "#;
59//! # use zarrs::metadata_ext::codec::zfpy::ZfpyCodecConfiguration;
60//! # let configuration: ZfpyCodecConfiguration = serde_json::from_str(JSON).unwrap();
61//! ```
62
63mod zfpy_codec;
64
65use std::sync::Arc;
66
67use zarrs_metadata::v2::MetadataV2;
68use zarrs_metadata::v3::MetadataV3;
69use zarrs_plugin::PluginCreateError;
70
71use zarrs_codec::{Codec, CodecPluginV2, CodecPluginV3, CodecTraitsV2, CodecTraitsV3};
72pub use zarrs_metadata_ext::codec::zfpy::{
73    ZfpyCodecConfiguration, ZfpyCodecConfigurationNumcodecs,
74};
75pub use zfpy_codec::ZfpyCodec;
76
77zarrs_plugin::impl_extension_aliases!(ZfpyCodec,
78    v3: "numcodecs.zfpy", ["https://codec.zarrs.dev/array_to_bytes/zfpy"],
79    v2: "zfpy"
80);
81
82// Register the V3 codec.
83inventory::submit! {
84    CodecPluginV3::new::<ZfpyCodec>()
85}
86// Register the V2 codec.
87inventory::submit! {
88    CodecPluginV2::new::<ZfpyCodec>()
89}
90
91impl CodecTraitsV3 for ZfpyCodec {
92    fn create(metadata: &MetadataV3) -> Result<Codec, PluginCreateError> {
93        let configuration: ZfpyCodecConfiguration = metadata.to_typed_configuration()?;
94        let codec = Arc::new(ZfpyCodec::new_with_configuration(&configuration)?);
95        Ok(Codec::ArrayToBytes(codec))
96    }
97}
98
99impl CodecTraitsV2 for ZfpyCodec {
100    fn create(metadata: &MetadataV2) -> Result<Codec, PluginCreateError> {
101        let configuration: ZfpyCodecConfiguration = metadata.to_typed_configuration()?;
102        let codec = Arc::new(ZfpyCodec::new_with_configuration(&configuration)?);
103        Ok(Codec::ArrayToBytes(codec))
104    }
105}