api
compression_safeguards.api
Implementation of the Safeguards, which compute the correction needed to satisfy a set of Safeguards.
Classes:
-
Safeguards–Collection of
Safeguards.
Safeguards
Safeguards(
*,
safeguards: Collection[dict[str, JSON] | Safeguard],
_version: None | str | Version = None,
)
Collection of Safeguards.
| Parameters: |
|
|---|
| Raises: |
|
|---|
Methods:
-
supported_dtypes–The set of numpy
dtypes that the safeguards support. -
correction_dtype_for_data–Compute the dtype of the correction for data of the provided
dtype. -
check–Check if the
approximationarray upholds the properties enforced by the safeguards with respect to thedataarray. -
compute_correction–Compute the correction required to make the
approximationarray satisfy the safeguards relative to thedataarray. -
apply_correction–Apply the
correctionto theapproximationto satisfy the safeguards for which thecorrectionwas computed. -
compute_required_stencil_for_chunked_correction–Compute the shape of the stencil neighbourhood around chunks of the complete data that is required to compute the chunked corrections.
-
check_chunk–Check if the
approximation_chunkarray chunk upholds the properties enforced by the safeguards with respect to thedata_chunkarray chunk. -
compute_chunked_correction–Compute the correction required to make the
approximation_chunkarray chunk satisfy the safeguards relative to thedata_chunkarray chunk. -
get_config–Returns the configuration of the safeguards.
-
from_config–Instantiate the safeguards from a configuration
dict.
late_bound
property
The set of late-bound parameters that the safeguards have.
Late-bound parameters are only bound when computing the correction, in contrast to the normal early-bound parameters that are configured during safeguard initialisation.
Late-bound parameters can be used for parameters that depend on the specific data that is to be safeguarded.
builtin_late_bound
property
The set of built-in late-bound constants that the safeguards provide
automatically, which include $x and $X.
version
property
version: Version
The semantic version 1 of the safeguards provided by this package, which covers
- the guarantees provided by the safeguards (can only be weakened in a new breaking major release)
- the configurations of the safeguards (can be extended backwards- compatibly in a new minor release)
- the format of the safeguards corrections (can only be changed in a new breaking major release)
The Safeguards can only load configurations for and apply
corrections produced by safeguards with a compatible semantic version.
Note that the version of the safeguards may be different from the
version of the compression-safeguards package, which may make changes
to the implementation or programmatic API without needing to increase
the version of the safeguards.
supported_dtypes
staticmethod
The set of numpy dtypes that the safeguards support.
correction_dtype_for_data
check
check(
data: ndarray[S, dtype[T]],
approximation: ndarray[S, dtype[T]],
*,
late_bound: Mapping[str | Parameter, Value]
| Bindings = EMPTY,
where: Literal[True] | ndarray[S, dtype[bool]] = True,
) -> bool
Check if the approximation array upholds the properties enforced by the safeguards with respect to the data array.
The data array must contain the complete data, i.e. not just a chunk
of data, so that non-pointwise safeguards are correctly applied. Please
use the check_chunk method instead when working with
individual chunks of data.
| Parameters: |
|
|---|
| Returns: |
|
|---|
| Raises: |
|
|---|
- API Reference compression_safeguards api Safeguards check_chunk
compute_correction
compute_correction(
data: ndarray[S, dtype[T]],
approximation: ndarray[S, dtype[T]],
*,
late_bound: Mapping[str | Parameter, Value]
| Bindings = EMPTY,
where: Literal[True] | ndarray[S, dtype[bool]] = True,
) -> ndarray[S, dtype[C]]
Compute the correction required to make the approximation array satisfy the safeguards relative to the data array.
The data array must contain the complete data, i.e. not just a chunk
of data, so that non-pointwise safeguards are correctly applied. Please
use the compute_chunked_correction
method instead when working with individual chunks of data.
The correction is defined as as_bits(corrected) - as_bits(approximation)
using wrapping unsigned integer arithmetic. It has the corresponding
binary unsigned integer data type with the same bit size. The
correction can be applied using apply_correction
to get the corrected approximation.
| Parameters: |
|
|---|
| Returns: |
|---|
| Raises: |
|
|---|
apply_correction
apply_correction(
approximation: ndarray[S, dtype[T]],
correction: ndarray[S, dtype[C]],
) -> ndarray[S, dtype[T]]
Apply the correction to the approximation to satisfy the safeguards for which the correction was computed.
The approximation must be bitwise equivalent to the approximation that
was used to compute the correction.
This method is guaranteed to work for chunked data as well, i.e.
applying a chunk of the correction to the corresponding chunk of the
approximation produces the correct result.
The correction is applied with
from_bits(as_bits(approximation) + correction) using wrapping unsigned
integer arithmetic.
| Parameters: |
|---|
| Returns: |
|---|
| Raises: |
|
|---|
compute_required_stencil_for_chunked_correction
cached
compute_required_stencil_for_chunked_correction(
data_shape: tuple[int, ...],
) -> tuple[
tuple[Literal[valid, wrap], NeighbourhoodAxis], ...
]
Compute the shape of the stencil neighbourhood around chunks of the complete data that is required to compute the chunked corrections.
For each data dimension, the stencil might require either a valid or wrapping boundary condition.
This method also checks that the data shape is compatible with the safeguards.
| Parameters: |
|---|
| Returns: |
|---|
| Raises: |
|
|---|
- API Reference compression_safeguards api Safeguards
check_chunk
check_chunk(
data_chunk: ndarray[S, dtype[T]],
approximation_chunk: ndarray[S, dtype[T]],
*,
data_shape: tuple[int, ...],
chunk_offset: tuple[int, ...],
chunk_stencil: tuple[
tuple[Literal[valid, wrap], NeighbourhoodAxis], ...
],
late_bound_chunk: Mapping[str | Parameter, Value]
| Bindings = EMPTY,
where_chunk: Literal[True]
| ndarray[S, dtype[bool]] = True,
) -> bool
Check if the approximation_chunk array chunk upholds the properties enforced by the safeguards with respect to the data_chunk array chunk.
Both the data_chunk and approximation_chunk contain the stencil around
the chunk, and the shape of this applied stencil is specified in the
chunk_stencil parameter. This stencil must be compatible with the
required stencil returned by
compute_required_stencil_for_chunked_correction(data_shape):
- a wrapping boundary is always compatible with a valid boundary.
- a larger stencil is always compatible with a smaller stencil.
- a smaller stencil is sometimes compatible with a larger stencil, iff
the smaller stencil is near the entire data boundary and still
includes all required elements; for instance, providing the entire
data as a single chunk with no stencil is always compatible with any
stencil
This advanced method should only be used when working with individual
chunks of data, for non-chunked data please use the simpler and more
efficient check method instead.
| Parameters: |
|
|---|
| Returns: |
|
|---|
| Raises: |
|
|---|
- API Reference compression_safeguards api Safeguards check
compute_chunked_correction
compute_chunked_correction(
data_chunk: ndarray[S, dtype[T]],
approximation_chunk: ndarray[S, dtype[T]],
*,
data_shape: tuple[int, ...],
chunk_offset: tuple[int, ...],
chunk_stencil: tuple[
tuple[Literal[valid, wrap], NeighbourhoodAxis], ...
],
any_chunk_check_failed: bool,
late_bound_chunk: Mapping[str | Parameter, Value]
| Bindings = EMPTY,
where_chunk: Literal[True]
| ndarray[S, dtype[bool]] = True,
) -> ndarray[tuple[int, ...], dtype[C]]
Compute the correction required to make the approximation_chunk array chunk satisfy the safeguards relative to the data_chunk array chunk.
Both the data_chunk and approximation_chunk contain the stencil around
the chunk, and the shape of this applied stencil is specified in the
chunk_stencil parameter. This stencil must be compatible with the
required stencil returned by
compute_required_stencil_for_chunked_correction(data_shape):
- a wrapping boundary is always compatible with a valid boundary.
- a larger stencil is always compatible with a smaller stencil.
- a smaller stencil is sometimes compatible with a larger stencil, iff
the smaller stencil is near the entire data boundary and still
includes all required elements; for instance, providing the entire
data as a single chunk with no stencil is always compatible with any
stencil
This advanced method should only be used when working with individual
chunks of data, for non-chunked data please use the simpler and more
efficient compute_correction method instead.
The (chunked) correction is defined as
as_bits(corrected) - as_bits(approximation) using wrapping unsigned
integer arithmetic. It has the corresponding binary unsigned integer
data type with the same bit size. The chunked correction can be applied
using apply_correction to get the corrected
chunked approximation.
| Parameters: |
|
|---|
| Returns: |
|---|
| Raises: |
|
|---|
- API Reference compression_safeguards api Safeguards compute_correction