Skip to main content

Python implementation of the `cast_value` codec.

Project description

cast-value

Actions Status Documentation Status

PyPI version Conda-Forge PyPI platforms

GitHub Discussion

Coverage

cast-value.py

A Python implementation of the cast_value codec for Zarr, with zarr-python integration.

What

The cast_value codec defines how to safely convert arrays between integer and float data types. In Zarr terminology, this codec is an "array -> array" codec, which means its input and output are both arrays.

You can find the specification for this codec in the zarr-extensions repository.

Why

This codec is commonly used for lossy data compression: when decoded data should be high-precision floats, but the absolute range of the values fits within the range of a smaller integer data type, then encoding the floats as ints before writing data can vastly shrink the stored values.

For example, if your data is a sequence of float64 values like [100.1, 120.3, 125.5], storing those values as uint8, e.g. [100, 120, 125], offers 8-fold reduction in storage size, provided the precision lost due to rounding is acceptable.

Installation

pip install cast-value

For the optional Rust backend (faster for large arrays):

pip install 'cast-value[rs]'

Usage

The codec is automatically registered with zarr-python via the zarr.codecs entrypoint. When cast-value[rs] is installed, the Rust backend is used; otherwise it falls back to the pure-NumPy backend.

import numpy as np
import zarr
import zarr.storage

from cast_value import CastValueNumpyV1

codec = CastValueNumpyV1(
    data_type="uint8",
    rounding="nearest-even",
    out_of_range="clamp",
)

# Write float64 data -- values are rounded and clamped to [0, 255]
data = np.array([1.5, 100.7, 255.9, -3.0], dtype=np.float64)
arr = zarr.create_array(store={}, data=data, filters=codec)

# Read it back -- comes back as float64, but with uint8 precision
result = arr[:]

print(f"Array dtype: {arr.dtype}")
print(f"Values written: {data}")
print(f"Values read:    {result}")
Array dtype: float64
Values written: [  1.5 100.7 255.9  -3. ]
Values read:    [  2. 101. 255.   0.]

Development

This project uses uv for dependency management.

Setup

# Clone the repo
git clone https://github.com/zarr-developers/cast-value.py.git
cd cast-value.py

# Install dev dependencies (includes test + benchmark deps)
uv sync --group dev

Running tests

# Run the full test suite
uv run pytest tests

# Run with coverage
uv run pytest tests --cov=cast_value --cov-report=term-missing

Running all checks

# Run the full CI suite locally (tests, linting, type checking)
uvx nox

# Or run just linting and type checking
uvx prek

Building docs

uv sync --group docs
uv run zensical build
# Output is in site/

Running examples

uv run python examples/zarr_integration/zarr_cast_value.py
uv run python examples/benchmarks/bench_numpy_vs_rust.py

Who

Davis Bennett (@d-v-b)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cast_value-0.2.1.tar.gz (99.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cast_value-0.2.1-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file cast_value-0.2.1.tar.gz.

File metadata

  • Download URL: cast_value-0.2.1.tar.gz
  • Upload date:
  • Size: 99.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cast_value-0.2.1.tar.gz
Algorithm Hash digest
SHA256 38173b6fc7e75aaf4f62bf7c46cd506309156a4b988a7ee4dd9f77569f410f1d
MD5 e7523a965bea90ccbb64db66f3a8b6f8
BLAKE2b-256 a180d486892982a81ba344193c7bd29b85d3cb353375a2e13a2db2be59e34c62

See more details on using hashes here.

Provenance

The following attestation bundles were made for cast_value-0.2.1.tar.gz:

Publisher: cd.yml on zarr-developers/cast-value.py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cast_value-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: cast_value-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cast_value-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1b32e9cccbcac63210f41774942ef4dc92a3b2ddbbc0342642e0b114a373894c
MD5 ed2ff1915bbecd480e5428dee6bb52d1
BLAKE2b-256 59b18a0793034fc781b092b2c7827115f5fe63175f985dc246d54d5ea2845353

See more details on using hashes here.

Provenance

The following attestation bundles were made for cast_value-0.2.1-py3-none-any.whl:

Publisher: cd.yml on zarr-developers/cast-value.py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page