Skip to content

Python API

Zarr Codec

cast_value.CastValueRustV1 dataclass

Bases: _CastValueBaseV1

Cast-value codec backed by the cast-value-rs Rust implementation.

Source code in src/cast_value/zarr_compat/v1/rust_codec.py
@dataclass(frozen=True, init=False)
class CastValueRustV1(_CastValueBaseV1):
    """Cast-value codec backed by the cast-value-rs Rust implementation."""

    def _cast_array(
        self,
        arr: np.ndarray,
        *,
        target_dtype: np.dtype,
        scalar_map_entries: Iterable[ScalarMapEntry] | None,
    ) -> np.ndarray:
        from cast_value_rs import (  # noqa: PLC0415  # pylint: disable=import-outside-toplevel
            cast_array as rs_cast_array,
        )

        return rs_cast_array(
            arr=arr,
            target_dtype=_dtype_to_str(target_dtype),  # ty: ignore[invalid-argument-type]
            rounding_mode=self.rounding,
            out_of_range_mode=self.out_of_range,
            scalar_map_entries=_convert_scalar_map(scalar_map_entries),
        )

cast_value.CastValueNumpyV1 dataclass

Bases: _CastValueBaseV1

Cast-value codec backed by the pure-numpy implementation.

Source code in src/cast_value/zarr_compat/v1/numpy_codec.py
@dataclass(frozen=True, init=False)
class CastValueNumpyV1(_CastValueBaseV1):
    """Cast-value codec backed by the pure-numpy implementation."""

    def _cast_array(
        self,
        arr: np.ndarray,
        *,
        target_dtype: np.dtype,
        scalar_map_entries: Iterable[ScalarMapEntry] | None,
    ) -> np.ndarray:
        return cast_array(
            arr,
            target_dtype=target_dtype,
            rounding_mode=self.rounding,
            out_of_range_mode=self.out_of_range,
            scalar_map_entries=scalar_map_entries,
        )

cast_value.cast_array

cast_array(arr: ndarray, *, target_dtype: dtype, rounding_mode: RoundingMode, out_of_range_mode: OutOfRangeMode | None, scalar_map_entries: ScalarMapEntries | None = None) -> ndarray

Cast an array to target_dtype with rounding, out-of-range, and scalar_map handling.

Optimized to minimize allocations and passes over the data. For the simple case (no scalar_map, no rounding needed, no out-of-range), this is essentially just arr.astype(target_dtype).

All casts are performed under np.errstate(over='raise', invalid='raise') so that numpy overflow or invalid-value warnings become hard errors instead of being silently swallowed.

Source code in src/cast_value/impl/_numpy.py
def cast_array(
    arr: np.ndarray,
    *,
    target_dtype: np.dtype,
    rounding_mode: RoundingMode,
    out_of_range_mode: OutOfRangeMode | None,
    scalar_map_entries: ScalarMapEntries | None = None,
) -> np.ndarray:
    """Cast an array to target_dtype with rounding, out-of-range, and scalar_map handling.

    Optimized to minimize allocations and passes over the data.
    For the simple case (no scalar_map, no rounding needed, no out-of-range),
    this is essentially just ``arr.astype(target_dtype)``.

    All casts are performed under ``np.errstate(over='raise', invalid='raise')``
    so that numpy overflow or invalid-value warnings become hard errors instead
    of being silently swallowed.
    """
    entries = _normalize_scalar_map(scalar_map_entries)
    with np.errstate(over="raise", invalid="raise"):
        return _cast_array_impl(
            arr,
            target_dtype=target_dtype,
            rounding=rounding_mode,
            out_of_range=out_of_range_mode,
            scalar_map_entries=entries or None,
        )

cast_value.ScalarMapJSON

Bases: TypedDict

JSON representation of the scalar_map codec configuration field.

This type models permitted values for the scalar_map field in the configuration field of the cast_value codec metadata.

See the cast_value spec for details.

Source code in src/cast_value/types.py
class ScalarMapJSON(TypedDict):
    """
    JSON representation of the scalar_map codec configuration field.

    This type models permitted values for the [`scalar_map`](https://github.com/zarr-developers/zarr-extensions/tree/main/codecs/cast_value#scalar_map) field in the
    [`configuration`](https://github.com/zarr-developers/zarr-extensions/tree/main/codecs/cast_value#configuration) field of the `cast_value` codec metadata.

    See the [`cast_value` spec](https://github.com/zarr-developers/zarr-extensions/tree/main/codecs/cast_value) for details.
    """

    encode: NotRequired[list[tuple[object, object]]]
    decode: NotRequired[list[tuple[object, object]]]

encode instance-attribute

decode instance-attribute

cast_value.ScalarMapEntry module-attribute

ScalarMapEntry = tuple[NumericScalar, NumericScalar]

cast_value.ScalarMapEntries module-attribute

cast_value.RoundingMode module-attribute

RoundingMode = Literal['nearest-even', 'towards-zero', 'towards-positive', 'towards-negative', 'nearest-away']

cast_value.OutOfRangeMode module-attribute

OutOfRangeMode = Literal['clamp', 'wrap']

cast_value.NumericScalar module-attribute

NumericScalar: TypeAlias = integer | floating