select

compression_safeguards.safeguards.combinators.select

Logical selector (switch case) combinator safeguard.

Classes:

  • SelectSafeguard

    The SelectSafeguard guarantees that, for each element, the guarantees of

SelectSafeguard

SelectSafeguard(
    *,
    selector: int | str | Parameter,
    safeguards: Collection[
        dict[str, JSON]
        | PointwiseSafeguard
        | StencilSafeguard
    ],
)

Bases: Safeguard

flowchart LR compression_safeguards.safeguards.combinators.select.SelectSafeguard[SelectSafeguard] compression_safeguards.safeguards.abc.Safeguard[Safeguard] compression_safeguards.safeguards.abc.Safeguard --> compression_safeguards.safeguards.combinators.select.SelectSafeguard click compression_safeguards.safeguards.combinators.select.SelectSafeguard href "" "compression_safeguards.safeguards.combinators.select.SelectSafeguard" click compression_safeguards.safeguards.abc.Safeguard href "" "compression_safeguards.safeguards.abc.Safeguard"

The SelectSafeguard guarantees that, for each element, the guarantees of the pointwise selected safeguard are upheld. This combinator allows selecting between several safeguards with per-element granularity.

This combinator can be used to describe simple regions of interest where different safeguards, e.g. with different error bounds, are applied to different parts of the data.

At the moment, only pointwise and stencil safeguards and combinations thereof can be combined by this select-combinator. The combinator is a pointwise or a stencil safeguard, depending on the safeguards it combines.

Parameters:
Raises:
  • TypeCheckError

    if any parameter has the wrong type.

  • ValueError

    if the safeguards collection is empty.

  • IndexError

    if the selector is a literal int index that is not within the 0 <= selector < len(safeguards) bounds.

  • ...

    if instantiating a safeguard raises an exception.

Methods:

  • check

    Check if, for all elements, the selected safeguard succeed the check.

  • check_pointwise

    Check for which elements the selected safeguard succeed the check.

  • compute_safe_intervals

    Compute the safe intervals for the selected safeguard.

  • compute_footprint

    Compute the footprint of the foot array, e.g. for expanding data

  • compute_inverse_footprint

    Compute the inverse footprint of the foot array, e.g. for expanding

  • get_config

    Returns the configuration of the safeguard.

kind class-attribute

kind: str = 'select'

selector property

selector: int | Parameter

The selector index or late-bound selector parameter.

safeguards property

The set of safeguards between which this combinator selects.

late_bound property

late_bound: Set[Parameter]

The set of late-bound parameters that this safeguard has.

Late-bound parameters are only bound when checking and applying the safeguard, 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.

check

check(
    data: ndarray[S, dtype[T]],
    approximation: ndarray[S, dtype[T]],
    *,
    late_bound: Bindings,
    where: Literal[True] | ndarray[S, dtype[bool]] = True,
) -> bool

Check if, for all elements, the selected safeguard succeed the check.

Parameters:
  • data (ndarray[S, dtype[T]]) –

    Original data array, relative to which the approximation is checked.

  • approximation (ndarray[S, dtype[T]]) –

    Approximation of the data array.

  • late_bound (Bindings) –

    Bindings for late-bound parameters, including for this safeguard.

  • where (Literal[True] | ndarray[S, dtype[bool]], default: True ) –

    Only check at data points where the condition is True.

Returns:
  • ok( bool ) –

    True if the check succeeded.

Raises:
  • LateBoundParameterResolutionError

    if the selector's late-bound parameter is not in late_bound.

  • ValueError

    if the late-bound selector could not be broadcast to the data's shape.

  • TypeError

    if the late-bound selector is floating-point.

  • ValueError

    if not all late-bound selector values could be losslessly converted to integer indices.

  • IndexError

    if the late-bound selector indices are invalid for the selected-over safeguards.

  • ...

    if checking a safeguard raises an exception.

check_pointwise

check_pointwise(
    data: ndarray[S, dtype[T]],
    approximation: ndarray[S, dtype[T]],
    *,
    late_bound: Bindings,
    where: Literal[True] | ndarray[S, dtype[bool]] = True,
) -> ndarray[S, dtype[bool]]

Check for which elements the selected safeguard succeed the check.

Parameters:
  • data (ndarray[S, dtype[T]]) –

    Original data array, relative to which the approximation is checked.

  • approximation (ndarray[S, dtype[T]]) –

    Approximation of the data array.

  • late_bound (Bindings) –

    Bindings for late-bound parameters, including for this safeguard.

  • where (Literal[True] | ndarray[S, dtype[bool]], default: True ) –

    Only check at data points where the condition is True.

Returns:
  • ok( ndarray[S, dtype[bool]] ) –

    Pointwise, True if the check succeeded for this element.

Raises:
  • LateBoundParameterResolutionError

    if the selector's late-bound parameter is not in late_bound.

  • ValueError

    if the late-bound selector could not be broadcast to the data's shape.

  • TypeError

    if the late-bound selector is floating-point.

  • ValueError

    if not all late-bound selector values could be losslessly converted to integer indices.

  • IndexError

    if the late-bound selector indices are invalid for the selected-over safeguards.

  • ...

    if checking a safeguard raises an exception.

compute_safe_intervals

compute_safe_intervals(
    data: ndarray[S, dtype[T]],
    *,
    late_bound: Bindings,
    where: Literal[True] | ndarray[S, dtype[bool]] = True,
) -> IntervalUnion[T, int, int]

Compute the safe intervals for the selected safeguard.

Parameters:
  • data (ndarray[S, dtype[T]]) –

    Data for which the safe intervals should be computed.

  • late_bound (Bindings) –

    Bindings for late-bound parameters, including for this safeguard.

  • where (Literal[True] | ndarray[S, dtype[bool]], default: True ) –

    Only compute the safe intervals at pointwise checks where the condition is True.

Returns:
Raises:
  • LateBoundParameterResolutionError

    if the selector's late-bound parameter is not in late_bound.

  • ValueError

    if the late-bound selector could not be broadcast to the data's shape.

  • TypeError

    if the late-bound selector is floating-point.

  • ValueError

    if not all late-bound selector values could be losslessly converted to integer indices.

  • IndexError

    if the late-bound selector indices are invalid for the selected-over safeguards.

  • ...

    if computing the safe intervals for a safeguard raises an exception.

compute_footprint

compute_footprint(
    foot: ndarray[S, dtype[bool]],
    *,
    late_bound: Bindings,
    where: Literal[True] | ndarray[S, dtype[bool]] = True,
) -> ndarray[S, dtype[bool]]

Compute the footprint of the foot array, e.g. for expanding data points into the pointwise checks that they contribute to.

Parameters:
  • foot (ndarray[S, dtype[bool]]) –

    Array for which the footprint is computed.

  • late_bound (Bindings) –

    Bindings for late-bound parameters, including for this safeguard.

  • where (Literal[True] | ndarray[S, dtype[bool]], default: True ) –

    Only compute the inverse footprint at pointwise checks where the condition is True.

Returns:

compute_inverse_footprint

compute_inverse_footprint(
    foot: ndarray[S, dtype[bool]],
    *,
    late_bound: Bindings,
    where: Literal[True] | ndarray[S, dtype[bool]] = True,
) -> ndarray[S, dtype[bool]]

Compute the inverse footprint of the foot array, e.g. for expanding pointwise check fails into the points that could have contributed to the failures.

Parameters:
  • foot (ndarray[S, dtype[bool]]) –

    Array for which the inverse footprint is computed.

  • late_bound (Bindings) –

    Bindings for late-bound parameters, including for this safeguard.

  • where (Literal[True] | ndarray[S, dtype[bool]], default: True ) –

    Only compute the inverse footprint at pointwise checks where the condition is True.

Returns:

get_config

get_config() -> dict[str, JSON]

Returns the configuration of the safeguard.

Returns:
  • config( dict ) –

    Configuration of the safeguard.