cast
compression_safeguards.utils.cast
Utility functions to cast arrays to floating-point, binary, and total-order representations.
Classes:
-
ToFloatMode–Mode for losslessly converting numeric data to floating-point.
Functions:
-
to_float–Losslessly convert the array
xto the floating-point data typeftype. -
from_float–Reverses the conversion of the array
x, converted using the -
as_bits–Reinterprets the array
ato its binary unsigned integer representation. -
to_total_order–Reinterprets the array
ato its total-order unsigned binary -
from_total_order–Reverses the reinterpretation of the array
aback from total-order -
lossless_cast–Try to losslessly convert
xto the provideddtype. -
saturating_finite_float_cast–Try to convert the finite
xto the provided floating-pointdtype.
ToFloatMode
Bases: Enum
Mode for losslessly converting numeric data to floating-point.
Methods:
-
floating_point_dtype_for–Select the floating-point dtype for the input
dtype.
lossless
class-attribute
instance-attribute
lossless = auto()
Automatically select the smallest floating-point data type that can losslessly represent the input data.
float16
class-attribute
instance-attribute
float16 = auto()
Use 16 bit floating-point precision, if lossless.
float32
class-attribute
instance-attribute
float32 = auto()
Use 32 bit floating-point precision, if lossless.
float64
class-attribute
instance-attribute
float64 = auto()
Use 64 bit floating-point precision, if lossless.
float128
class-attribute
instance-attribute
float128 = auto()
Use 128 bit floating-point precision, if lossless.
floating_point_dtype_for
Select the floating-point dtype for the input dtype.
Only data type supported by the safeguards (see
Safeguards.supported_dtypes)
are supported by this method.
The numpy_quaddtype
package is used to provide a true 128 bit floating-point data type.
| Parameters: |
|---|
| Returns: |
|---|
| Raises: |
|
|---|
- API Reference compression_safeguards
to_float
Losslessly convert the array x to the floating-point data type ftype.
ftype must be a floating-point data type that can represent all values
of the data type of x without loss in precision:
-
For floating-point data, it is at least the input data type.
-
For integer data, it is a floating-point type with sufficient precision to represent all integer values, i.e. a type whose mantissa has more bits than the integer type. For the supported floating-point types, this corresponds to choosing a floating-point data type with a larger bit width (e.g. at least
np.float64fornp.int32ornp.uint32data).
The
ToFloatMode.floating_point_dtype_for
method can be used to select a floating-point data type that fits the above
criteria.
| Parameters: |
|---|
| Returns: |
|---|
- API Reference compression_safeguards
from_float
Reverses the conversion of the array x, converted using the
to_float, back to the original dtype.
If the original dtype was floating-point with lower precision, the
conversion is lossy.
If the original dtype was integer, the rounding conversion is lossy.
Infinite values are clamped to the minimum/maximum integer values. NaN
values are converted to integer zero.
| Parameters: |
|---|
| Returns: |
|---|
as_bits
to_total_order
Reinterprets the array a to its total-order unsigned binary
representation.
In their total-order representation, the smallest value is mapped to unsigned zero, and the largest value is mapped to the largest unsigned value.
For floating-point values, this implementation is based on Michael Herf's
FloatFlip function, see http://stereopsis.com/radix.html.
| Parameters: |
|---|
| Returns: |
|---|
| Raises: |
|
|---|
from_total_order
Reverses the reinterpretation of the array a back from total-order
unsigned binary to the provided dtype.
For floating-point values, this implementation is based on Michael Herf's
IFloatFlip function, see http://stereopsis.com/radix.html.
| Parameters: |
|---|
| Returns: |
|---|
| Raises: |
|
|---|
lossless_cast
lossless_cast(x: int | float | number | ndarray[S, dtype[number]], dtype: dtype[T]) -> ndarray[tuple[] | S, dtype[T]]
Try to losslessly convert x to the provided dtype.
A lossless conversion is one that can be reversed while preserving the original value. Integer values can be losslessly converted to integer or floating-point types with sufficient precision. Floating-point values can only be converted to floating-point types.
| Parameters: |
|---|
| Returns: |
|---|
| Raises: |
|
|---|
saturating_finite_float_cast
saturating_finite_float_cast(x: int | float | number | ndarray[S, dtype[number]], dtype: dtype[F]) -> ndarray[tuple[] | S, dtype[F]]
Try to convert the finite x to the provided floating-point dtype.
Under- and overflows are clamped to finite values.
| Parameters: |
|---|
| Returns: |
|---|
| Raises: |
|
|---|