intervals
compression_safeguards.utils.intervals
Types and helpers to construct safe intervals for the safeguards.
Classes:
-
Interval–Single interval over a
N-sizedndarrayof data typeT. -
Lower–Array wrapper to override an
Interval's lower bound using comparison syntax. -
Upper–Array wrapper to override an
Interval's upper bound using comparison syntax. -
IntervalUnion–Union of
Uintervals, each over aN-sizedndarrayof data typeT.
N
module-attribute
The number of elements in the interval (covariant).
- API Reference compression_safeguards utils intervals
Ni
module-attribute
The number of elements in the interval (invariant).
- API Reference compression_safeguards utils intervals
- API Reference compression_safeguards utils intervals
U
module-attribute
The maximum number of intervals in an interval union (covariant).
- API Reference compression_safeguards utils intervals IntervalUnion u
- API Reference compression_safeguards utils intervals IntervalUnion
Ui
module-attribute
The maximum number of intervals in an interval union (invariant).
- API Reference compression_safeguards utils intervals IntervalUnion empty
- API Reference compression_safeguards utils intervals IntervalUnion empty
Interval
Single interval over a N-sized ndarray of data type T.
Construction
An interval should only be constructed using
Interval.emptyorInterval.empty_likefor an empty interval that contains no valuesInterval.singletonfor a singleton interval that only contains one array of valuesInterval.fullorInterval.full_likefor a full interval that contains all possible values
Overriding lower and upper bounds
The lower and upper bounds of the interval can be set as follows:
Lower(lower_data) <= interval <= Upper(upper_data)
They can also be set to the smallest and largest possible values using:
Minimum <= interval <= Maximum
If only the interval bounds for some array members should be updated, only the interval itself needs to be indexed:
# bound infinite values to be exactly themselves
Lower(data) <= interval[np.isinf(data)] <= Upper(data)
Common lower and upper bounds
The following common lower and upper bounds are provided for ease of use:
Interval.preserve_infInterval.preserve_signed_nanInterval.preserve_any_nanInterval.preserve_finiteInterval.preserve_non_nan
Interval operations
Two intervals can be
- intersected using
Interval.intersect - unioned using
Interval.union
or converted into a single-member union of intervals using
Interval.into_union.
Methods:
-
non_empty_width–Binary width of the interval.
-
empty–Create an empty interval that contains no values.
-
empty_like–Create an empty interval that contains no values and has the same dtype and size as
a. -
singleton–Create a singleton interval that has the same dtype and size as
aand contains, for each element ina, only this singular value. -
full–Create a full interval that contains all possible values of
dtype. -
full_like–Create a full interval that contains all possible values and has the same dtype and size as
a. -
preserve_inf–Preserve all infinite values in
aexactly. -
preserve_signed_nan–Preserve all NaN values in
a, preserving their sign bit. -
preserve_any_nan–Preserve all NaN values in
a, ignoring their sign bit. -
preserve_finite–Preserve all finite values in
aas finite values. -
preserve_non_nan–Preserve all non-NaN values in
aas non-NaN values. -
intersect–Computes the intersection with the
otherinterval. -
union–Computes the union with the
otherinterval (union). -
into_union–Convert this interval into a single-member union of exactly this one interval.
-
preserve_only_where–Preserve safe intervals only at the data points where the
where
non_empty_width
non_empty_width() -> ndarray[
tuple[N], dtype[unsignedinteger]
]
Binary width of the interval.
The interval must not have any empty entries.
Single-element intervals have width zero, full intervals have maximum width.
| Returns: |
|
|---|
empty
staticmethod
empty_like
staticmethod
Create an empty interval that contains no values and has the same dtype and size as a.
| Parameters: |
|---|
| Returns: |
|---|
- API Reference compression_safeguards utils intervals Interval Construction
singleton
staticmethod
Create a singleton interval that has the same dtype and size as a and contains, for each element in a, only this singular value.
| Parameters: |
|---|
| Returns: |
|---|
- API Reference compression_safeguards utils intervals Interval Construction
full
staticmethod
Create a full interval that contains all possible values of dtype.
| Parameters: |
|---|
| Returns: |
|---|
- API Reference compression_safeguards utils intervals Interval Construction
full_like
staticmethod
Create a full interval that contains all possible values and has the same dtype and size as a.
| Parameters: |
|---|
| Returns: |
|---|
- API Reference compression_safeguards utils intervals Interval Construction
preserve_inf
Preserve all infinite values in a exactly.
Specifically, set their lower and upper bounds in this interval to their values.
Equivalent to
Lower(a) <= self[np.isinf(a)] <= Upper(a)
| Parameters: |
|---|
| Returns: |
|
|---|
preserve_signed_nan
Preserve all NaN values in a, preserving their sign bit.
- If
equal_nanisTrue, the intervals corresponding to the NaN values will include all possible NaN values with the same sign bit. - If
equal_nanisFalse, all NaN values are preserved exactly.
| Parameters: |
|---|
| Returns: |
|
|---|
preserve_any_nan
Preserve all NaN values in a, ignoring their sign bit.
- If
equal_nanisTrue, the intervals corresponding to the NaN values will include all possible NaN values, irrespective of their sign bit. - If
equal_nanisFalse, all NaN values are preserved exactly.
Since there are two disjoint value regions of NaNs (positive and negative), this method returns a union of intervals.
| Parameters: |
|---|
| Returns: |
|
|---|
preserve_finite
Preserve all finite values in a as finite values.
Specifically, set their lower and upper bounds to exclude non-finite values.
| Parameters: |
|---|
| Returns: |
|
|---|
preserve_non_nan
Preserve all non-NaN values in a as non-NaN values.
Specifically, set their lower and upper bounds to exclude NaN values.
| Parameters: |
|---|
| Returns: |
|
|---|
intersect
union
Computes the union with the other interval (union).
| Parameters: |
|---|
| Returns: |
|
|---|
- API Reference compression_safeguards utils intervals Interval Interval operations
into_union
into_union() -> IntervalUnion[T, N, Literal[1]]
Convert this interval into a single-member union of exactly this one interval.
| Returns: |
|
|---|
- API Reference compression_safeguards utils intervals Interval Interval operations
Minimum
module-attribute
Minimum = _Minimum()
The smallest representable value
Maximum
module-attribute
Maximum = _Maximum()
The largest representable value
Lower
Upper
IntervalUnion
Union of U intervals, each over a N-sized ndarray of data type T.
Methods:
-
non_empty_width–Binary width of the interval union.
-
empty–Create an empty interval union that contains no values.
-
intersect–Computes the intersection with the
otherinterval union. -
union–Computes the union with the
otherinterval (union). -
preserve_only_where–Preserve safe intervals only at the data points where the
where -
contains–Check if this interval union contains the elements of the
otherarray. -
pick–Pick a member of the interval union that minimises the cost of correcting the approximation to be a member of the interval union.
non_empty_width
non_empty_width() -> ndarray[
tuple[N], dtype[unsignedinteger]
]
Binary width of the interval union.
The interval union must not have any empty entries.
Single-element intervals have width zero, full intervals have maximum width.
| Returns: |
|
|---|
empty
staticmethod
intersect
intersect(
other: IntervalUnion[T, N, int],
) -> IntervalUnion[T, N, int]
Computes the intersection with the other interval union.
| Parameters: |
|
|---|
| Returns: |
|
|---|
union
preserve_only_where
preserve_only_where(
where: Literal[True] | ndarray[tuple[N], dtype[bool]],
) -> IntervalUnion[T, N, int]
Preserve safe intervals only at the data points where the where
condition is True.
This method must only be used for pointwise safe intervals.
| Parameters: |
|---|
| Returns: |
|
|---|
contains
pick
Pick a member of the interval union that minimises the cost of correcting the approximation to be a member of the interval union.
The metric for minimising the correction cost is unspecified and may be approximate.
| Parameters: |
|---|
| Returns: |
|---|