Skip to main content
Version: 0.5.x

sanitizers

Utility functions for validation and treatment of parameters.

Module: aisp.utils.sanitizers
Import: from aisp.utils import sanitizers

Functions

sanitize_choice

def sanitize_choice(value: T, valid_choices: Iterable[T], default: T) -> T:
...

Value if present in the set of valid choices; otherwise, the default value.

Parameters

NameTypeDefaultDescription
valueT-The value to be checked.
valid_choicesIterable[T]-A collection of valid choices.
defaultT-The default value to be returned if 'value' is not in 'valid_choices'.

Returns

TypeDescription
TThe original value if valid, or the default value if not.

sanitize_param

def sanitize_param(value: T, default: T, condition: Callable[[T], bool]) -> T:
...

Value if it satisfies the specified condition; otherwise, the default value.

Parameters

NameTypeDefaultDescription
valueT-The value to be checked.
defaultT-The default value to be returned if the condition is not satisfied.
conditionCallable[[T], bool]-A function that takes a value and returns a boolean, determining if the value is valid.

Returns

TypeDescription
TThe original value if the condition is satisfied, or the default value if not.

sanitize_seed

def sanitize_seed(seed: Any) -> Optional[int]:
...

Seed if it is a non-negative integer; otherwise, None.

Parameters

NameTypeDefaultDescription
seedAny-The seed value to be validated.

Returns

TypeDescription
Optional[int]The original seed if it is a non-negative integer, or None if it is invalid.

sanitize_bounds

def sanitize_bounds(
bounds: Any, problem_size: int
) -> Dict[str, npt.NDArray[np.float64]]:
...

Validate and normalize feature bounds.

Parameters

NameTypeDefaultDescription
boundsAny-The input bounds, which must be either None or a dictionary with 'low' and 'high' keys.
problem_sizeint-The expected length for the normalized bounds lists, corresponding to the number of features in the problem.

Returns

TypeDescription
Dict[str, list]Dictionary {'low': [low_1, ..., low_N], 'high': [high_1, ..., high_N]}.

Raises

ExceptionDescription
TypeErrorIf bounds is not None and not a dict with 'low'/'high', or if items are non-numeric.
ValueErrorIf provided iterables have the wrong length.