Skip to content

Add a libpressio codec wrapper #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ jobs:
cargo hack check --all \
--exclude numcodecs-python \
--exclude numcodecs-jpeg2000 \
--exclude numcodecs-pressio \
--exclude numcodecs-sz3 \
--exclude numcodecs-tthresh \
--exclude numcodecs-zfp \
Expand Down Expand Up @@ -221,6 +222,7 @@ jobs:
cargo hack clippy --all \
--exclude numcodecs-python \
--exclude numcodecs-jpeg2000 \
--exclude numcodecs-pressio \
--exclude numcodecs-sz3 \
--exclude numcodecs-tthresh \
--exclude numcodecs-zfp \
Expand All @@ -236,6 +238,7 @@ jobs:
cargo hack clippy --all \
--exclude numcodecs-python \
--exclude numcodecs-jpeg2000 \
--exclude numcodecs-pressio \
--exclude numcodecs-sz3 \
--exclude numcodecs-tthresh \
--exclude numcodecs-zfp \
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ members = [
"codecs/linear-quantize",
"codecs/log",
"codecs/pco",
"codecs/pressio",
"codecs/random-projection",
"codecs/reinterpret",
"codecs/round",
Expand Down Expand Up @@ -62,6 +63,7 @@ numcodecs-jpeg2000 = { version = "0.2", path = "codecs/jpeg2000", default-featur
numcodecs-linear-quantize = { version = "0.4", path = "codecs/linear-quantize", default-features = false }
numcodecs-log = { version = "0.4", path = "codecs/log", default-features = false }
numcodecs-pco = { version = "0.2", path = "codecs/pco", default-features = false }
numcodecs-pressio = { version = "0.1", path = "codecs/pressio", default-features = false }
numcodecs-random-projection = { version = "0.3", path = "codecs/random-projection", default-features = false }
numcodecs-reinterpret = { version = "0.3", path = "codecs/reinterpret", default-features = false }
numcodecs-round = { version = "0.3", path = "codecs/round", default-features = false }
Expand Down
27 changes: 27 additions & 0 deletions codecs/pressio/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "numcodecs-pressio"
version = "0.1.0"
edition = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
license = { workspace = true }
rust-version = { workspace = true }

description = "libpressio codec wrapper for the numcodecs API"
readme = "README.md"
categories = ["compression", "encoding"]
keywords = ["libpressio", "numcodecs", "compression", "encoding"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
numcodecs = { workspace = true }
schemars = { workspace = true, features = ["derive", "preserve_order"] }
serde = { workspace = true, features = ["std", "derive"] }
thiserror = { workspace = true }

# FIXME: move into workspace dependencies
libpressio = { git = "https://github.com/juntyr/libpressio-rs.git", rev = "6d25c06", default-features = false }

[lints]
workspace = true
1 change: 1 addition & 0 deletions codecs/pressio/LICENSE
38 changes: 38 additions & 0 deletions codecs/pressio/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[![CI Status]][workflow] [![MSRV]][repo] [![Latest Version]][crates.io] [![PyPi Release]][pypi] [![Rust Doc Crate]][docs.rs] [![Rust Doc Main]][docs] [![Read the Docs]][rtdocs]

[CI Status]: https://img.shields.io/github/actions/workflow/status/juntyr/numcodecs-rs/ci.yml?branch=main
[workflow]: https://github.com/juntyr/numcodecs-rs/actions/workflows/ci.yml?query=branch%3Amain

[MSRV]: https://img.shields.io/badge/MSRV-1.85.0-blue
[repo]: https://github.com/juntyr/numcodecs-rs

[Latest Version]: https://img.shields.io/crates/v/numcodecs-pressio
[crates.io]: https://crates.io/crates/numcodecs-pressio

[PyPi Release]: https://img.shields.io/pypi/v/numcodecs-wasm-pressio.svg
[pypi]: https://pypi.python.org/pypi/numcodecs-wasm-pressio

[Rust Doc Crate]: https://img.shields.io/docsrs/numcodecs-pressio
[docs.rs]: https://docs.rs/numcodecs-pressio/

[Rust Doc Main]: https://img.shields.io/badge/docs-main-blue
[docs]: https://juntyr.github.io/numcodecs-rs/numcodecs_pressio

[Read the Docs]: https://img.shields.io/readthedocs/numcodecs-wasm?label=readthedocs
[rtdocs]: https://numcodecs-wasm.readthedocs.io/en/stable/api/numcodecs_wasm_pressio/

# numcodecs-pressio

libpressio codec wrapper for the [`numcodecs`] API.

[`numcodecs`]: https://docs.rs/numcodecs/0.2/numcodecs/

## License

Licensed under the Mozilla Public License, Version 2.0 ([LICENSE](LICENSE) or https://www.mozilla.org/en-US/MPL/2.0/).

## Funding

The `numcodecs-pressio` crate has been developed as part of [ESiWACE3](https://www.esiwace.eu), the third phase of the Centre of Excellence in Simulation of Weather and Climate in Europe.

Funded by the European Union. This work has received funding from the European High Performance Computing Joint Undertaking (JU) under grant agreement No 101093054.
83 changes: 83 additions & 0 deletions codecs/pressio/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//! [![CI Status]][workflow] [![MSRV]][repo] [![Latest Version]][crates.io] [![Rust Doc Crate]][docs.rs] [![Rust Doc Main]][docs]
//!
//! [CI Status]: https://img.shields.io/github/actions/workflow/status/juntyr/numcodecs-rs/ci.yml?branch=main
//! [workflow]: https://github.com/juntyr/numcodecs-rs/actions/workflows/ci.yml?query=branch%3Amain
//!
//! [MSRV]: https://img.shields.io/badge/MSRV-1.85.0-blue
//! [repo]: https://github.com/juntyr/numcodecs-rs
//!
//! [Latest Version]: https://img.shields.io/crates/v/numcodecs-pressio
//! [crates.io]: https://crates.io/crates/numcodecs-pressio
//!
//! [Rust Doc Crate]: https://img.shields.io/docsrs/numcodecs-pressio
//! [docs.rs]: https://docs.rs/numcodecs-pressio/
//!
//! [Rust Doc Main]: https://img.shields.io/badge/docs-main-blue
//! [docs]: https://juntyr.github.io/numcodecs-rs/numcodecs_pressio
//!
//! libpressio codec wrapper for the [`numcodecs`] API.

use ::libpressio as _;
use numcodecs::{
AnyArray, AnyArrayAssignError, AnyArrayView, AnyArrayViewMut, AnyCowArray, Codec, StaticCodec,
StaticCodecConfig, StaticCodecVersion,
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use thiserror::Error;

#[derive(Clone, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
/// Identity codec which applies the identity function, i.e. passes through the
/// input unchanged during encoding and decoding.
pub struct IdentityCodec {
/// The codec's encoding format version. Do not provide this parameter explicitly.
#[serde(default, rename = "_version")]
pub version: StaticCodecVersion<1, 0, 0>,
}

impl Codec for IdentityCodec {
type Error = IdentityCodecError;

fn encode(&self, data: AnyCowArray) -> Result<AnyArray, Self::Error> {
Ok(data.into_owned())
}

fn decode(&self, encoded: AnyCowArray) -> Result<AnyArray, Self::Error> {
Ok(encoded.into_owned())
}

fn decode_into(
&self,
encoded: AnyArrayView,
mut decoded: AnyArrayViewMut,
) -> Result<(), Self::Error> {
Ok(decoded.assign(&encoded)?)
}
}

impl StaticCodec for IdentityCodec {
const CODEC_ID: &'static str = "identity.rs";

type Config<'de> = Self;

fn from_config(config: Self::Config<'_>) -> Self {
config
}

fn get_config(&self) -> StaticCodecConfig<Self> {
StaticCodecConfig::from(self)
}
}

#[derive(Debug, Error)]
/// Errors that may occur when applying the [`IdentityCodec`].
pub enum IdentityCodecError {
/// [`IdentityCodec`] cannot decode into the provided array
#[error("Identity cannot decode into the provided array")]
MismatchedDecodeIntoArray {
/// The source of the error
#[from]
source: AnyArrayAssignError,
},
}
18 changes: 15 additions & 3 deletions crates/numcodecs-wasm-builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ struct Args {
#[arg(long)]
version: Version,

/// Path to the local version of the numcodecs codec crate to compile
#[arg(long)]
path: Option<PathBuf>,

/// Path to the codec type to export, without the leading crate name
#[arg(long)]
codec: String,
Expand All @@ -46,8 +50,13 @@ fn main() -> io::Result<()> {
eprintln!("creating {target_dir:?}");
fs::create_dir_all(&target_dir)?;

let crate_dir =
create_codec_wasm_component_crate(&scratch_dir, &args.crate_, &args.version, &args.codec)?;
let crate_dir = create_codec_wasm_component_crate(
&scratch_dir,
&args.crate_,
&args.version,
args.path.as_deref(),
&args.codec,
)?;
copy_buildenv_to_crate(&crate_dir)?;

let nix_env = NixEnv::new(&crate_dir)?;
Expand All @@ -70,6 +79,7 @@ fn create_codec_wasm_component_crate(
scratch_dir: &Path,
crate_: &str,
version: &Version,
path: Option<&Path>,
codec: &str,
) -> io::Result<PathBuf> {
let crate_dir = scratch_dir.join(format!("{crate_}-wasm-{version}"));
Expand All @@ -80,6 +90,8 @@ fn create_codec_wasm_component_crate(
}
fs::create_dir_all(&crate_dir)?;

let path = path.map_or_else(String::new, |p| format!("path = \"{}\", ", p.display()));

fs::write(
crate_dir.join("Cargo.toml"),
format!(
Expand All @@ -96,7 +108,7 @@ edition = "2024"
[dependencies]
numcodecs-wasm-logging = {{ version = "0.1", default-features = false }}
numcodecs-wasm-guest = {{ version = "0.2", default-features = false }}
numcodecs-my-codec = {{ package = "{crate_}", version = "{version}", default-features = false }}
numcodecs-my-codec = {{ package = "{crate_}", version = "{version}", {path}default-features = false }}
"#
),
)?;
Expand Down
Loading