bindings

compression_safeguards.utils.bindings

Types and helpers for late-bound safeguard parameters.

Classes:

  • Parameter

    Parameter name / identifier type.

  • Bindings

    Bindings from parameter names to values.

Parameter

Parameter(param: str)

Bases: str

flowchart LR compression_safeguards.utils.bindings.Parameter[Parameter] click compression_safeguards.utils.bindings.Parameter href "" "compression_safeguards.utils.bindings.Parameter"

Parameter name / identifier type.

Parameters:
  • param (str) –

    Name of the parameter, which must be a valid identifier.

Raises:
  • ValueError

    if param is not a valid identifier.

is_builtin property

is_builtin: bool

Is the parameter a built-in parameter (starts with an $)?

Value module-attribute

Value: TypeAlias = (
    int
    | float
    | number
    | ndarray[tuple[int, ...], dtype[number]]
)

Parameter value type that includes scalar numbers and arrays thereof.

Bindings

Bindings(**kwargs: Value)

Bindings from parameter names to values.

Parameters:
  • **kwargs (Value, default: {} ) –

    Mapping from parameters to values as keyword arguments.

Methods:

update

update(**kwargs: Value) -> Self

Create new bindings that contain the old and the new parameters, where new parameters may override old ones.

Parameters:
  • **kwargs (Value, default: {} ) –

    Mapping from new parameters to values as keyword arguments.

Returns:
  • bindings( Bindings ) –

    The updated bindings.

parameters

parameters() -> Set[Parameter]

Access the set of parameter names for which bindings exist.

Returns:
  • params( Set[Parameter] ) –

    The set of parameters in these bindings.

resolve_ndarray_with_lossless_cast

resolve_ndarray_with_lossless_cast(
    param: Parameter, shape: Si, dtype: dtype[T]
) -> ndarray[Si, dtype[T]]

Resolve the parameter to a numpy array with the given shape and dtype.

The parameter must be contained in the bindings and refer to a value that can be broadcast to the shape and losslessly converted to the dtype.

Parameters:
  • param (Parameter) –

    The parameter that will be resolved.

  • shape (Si) –

    The shape of the array to resolve to.

  • dtype (dtype[T]) –

    The dtype of the array to resolve to.

Returns:
  • array( ndarray[Si, dtype[T]] ) –

    A read-only view to the resolved array of the given shape and dtype.

Raises:

resolve_ndarray_with_saturating_finite_float_cast

resolve_ndarray_with_saturating_finite_float_cast(
    param: Parameter, shape: Si, dtype: dtype[F]
) -> ndarray[Si, dtype[F]]

Resolve the parameter to a numpy array with the given shape and floating-point dtype.

The parameter must be contained in the bindings and refer to a finite value that can be broadcast to the shape. It will be converted to the floating-point dtype, with under- and overflows being clamped to finite values.

Parameters:
  • param (Parameter) –

    The parameter that will be resolved.

  • shape (Si) –

    The shape of the array to resolve to.

  • dtype (dtype[F]) –

    The floating-point dtype of the array to resolve to.

Returns:
  • array( ndarray[Si, dtype[F]] ) –

    A read-only view to the resolved array of the given shape and dtype.

Raises:

expect_broadcastable_to

expect_broadcastable_to(shape: tuple[int, ...])

Check that all late-bound array parameters can be broadcast to the given shape.

Parameters:
  • shape (tuple[int, ...]) –

    The shape that all array parameters should be broadcastable to.

Raises:
  • ValueError

    if an array parameter could not be broadcast to the shape.

apply_slice_index

apply_slice_index(index: tuple[slice, ...]) -> Self

Apply the slice index to the late-bound array values and return the sliced bindings.

The index is only applied to an array value if - the value is not scalar (no effect) - the value has the same number of dimensions as the index (bail out)

Furthermore, the index is only applied along dimensions with a size greater than 1, since smaller dimensions can be broadcast.

Parameters:
  • index (tuple[slice, ...]) –

    The slice index that is applied to the late-bound array values.

Returns:
  • bindings( Bindings ) –

    The sliced bindings.

Raises:
  • IndexError

    if an array value has the right number of dimensions but indexing with index fails

apply_roll

apply_roll(shift: tuple[int, ...]) -> Self

Apply the roll shift to the late-bound array values and return the rolled bindings.

The shift is only applied to an array value if - the value is not scalar (no effect) - the value has the same number of dimensions as the shift (bail out)

Furthermore, the shift is only applied along dimensions with a size greater than 1, since smaller dimensions can be broadcast.

Parameters:
  • shift (tuple[int, ...]) –

    The roll shift that is applied to the late-bound array values.

Returns:
  • bindings( Bindings ) –

    The rolled bindings.

get_config

get_config() -> dict[str, JSON]

Returns the configuration of the bindings in a JSON compatible format.

Returns:
  • config( dict[str, JSON] ) –

    Configuration of the bindings.

from_config classmethod

from_config(config: dict[str, JSON]) -> Self

Instantiate the bindings from a configuration dict.

Parameters:
  • config (dict[str, JSON]) –

    Configuration of the bindings.

Returns:
  • bindings( Self ) –

    Instantiated bindings.