asldro.validators package

Submodules

asldro.validators.ground_truth_json module

A validator for the JSON file used in the ground truth input

asldro.validators.ground_truth_json.validate_input(input_dict: dict)

Validates the provided dictionary against the ground truth schema. Raises a jsonschema.exceptions.ValidationError on error

asldro.validators.parameters module

Used to perform parameter validation. The most useful documentation can be found on the class ‘ParameterValidator’

class asldro.validators.parameters.Parameter(validators: Union[Callable[[], bool], List[Callable[[], bool]]], default_value=None, optional=False)

Bases: object

A description of a parameter which is to be validated against

class asldro.validators.parameters.ParameterValidator(parameters: Dict[str, asldro.validators.parameters.Parameter], post_validators: List[asldro.validators.parameters.Validator] = None)

Bases: object

Used to validate a dictionary of parameters specified with the Parameter class against an input dictionary. Will also insert any default values that are missing from the input dictionary.

get_defaults()

Return a dictionary of default values for each of the parameters in the ParameterValidator. If a parameter does not have a default value, it is excluded from the dictionary :return: a dictionary of default parameter values

validate(d: dict, error_type: Type[Exception] = <class 'asldro.validators.parameters.ValidationError'>) → dict

Validate an input dictionary, replacing missing dictionary entries with default values. If any of the dictionary entries are invalid w.r.t. any of the validators, a ValidationError will be raised (unless error_type is defined, see parameter docs). :param d: the input dictionary. e.g.: {“foo”: “bar foo bar”} :param error_type: the type of Exception to be raised. :return: the dictionary with any defaults filled. e.g. {

“foo”: “bar foo bar”, “bar”: [1, 2, 3],

}

exception asldro.validators.parameters.ValidationError

Bases: Exception

Used to indicate that a dictionary is invalid

class asldro.validators.parameters.Validator(func: Callable[[Any], bool], criteria_message: str)

Bases: object

All _validator functions return an object of this class. The object can be called with a value to check whether the value is valid. A string method is also available to display the validator’s criteria message.

asldro.validators.parameters.and_validator(validators: List[asldro.validators.parameters.Validator])asldro.validators.parameters.Validator

Boolean AND between supplied validators. If all of the supplied validators evaluate as True, this validator evaluates as True.

Parameters

validators (Union[List[Validator], Tuple[Validator, ..]]) – A list (or tuple) of validators

asldro.validators.parameters.for_each_validator(item_validator=<class 'asldro.validators.parameters.Validator'>)asldro.validators.parameters.Validator

Validates that the value must be iteratable and each of its items are valid based on the item_validator. e.g. validator=for_each_validator(greater_than_validator(0.7)) would create a validator that check all items in a last are > 0.7 :param item_validator: a validator to apply to each item in a list

asldro.validators.parameters.from_list_validator(options: list, case_insensitive: bool = False)asldro.validators.parameters.Validator

Validates that a given value is from a list. :param option: the list of options, one of which the value must match :param case_insensitive: perform a case-insensitive matching

asldro.validators.parameters.greater_than_equal_to_validator(start)asldro.validators.parameters.Validator

Validate that a given value is greater or equal to a number. Can be used with int, float or BaseImageContainer. :param start: the value to be greater than or equal to

asldro.validators.parameters.greater_than_validator(start)asldro.validators.parameters.Validator

Validate that a given value is greater than a number. Can be used with int, float or BaseImageContainer. :param start: the value to be greater than

asldro.validators.parameters.has_attribute_value_validator("a_property", 500.0)

would create a validators that check that an object (obj’) has a property `a_property that matches 500.0. i.e. obj.a_property == 500.0 :param attribute_name: the attribute name to compare :param attribute_value: the value of the attribute to compare against

asldro.validators.parameters.isinstance_validator(a_type: Union[type, Tuple[type, ]])asldro.validators.parameters.Validator

Validates that a given value is an instance of the given type(s) (or or derived from). a_type: a type e.g. str, or a tuple of types e.g. (int, str)

asldro.validators.parameters.list_of_type_validator(a_type: Union[type, Tuple[type, ]])asldro.validators.parameters.Validator

Validates that a given value is a list of the given type(s). a_type: a type e.g. str, or a tuple of types e.g. (int, str)

asldro.validators.parameters.non_empty_list_validator()asldro.validators.parameters.Validator

Validates that a value is a list and is non-empty

asldro.validators.parameters.of_length_validator(length: int)asldro.validators.parameters.Validator

Validates that a given value has a given length. Might be, for example, a list or a string. :param length: the required length of the value

asldro.validators.parameters.or_validator(validators: List[asldro.validators.parameters.Validator])asldro.validators.parameters.Validator

Boolean OR between supplied validators. If any of the supplied validators evaluate as True, this validator evaluates as True.

Parameters

validators (Union[List[Validator], Tuple[Validator, ..]]) – A list (or tuple) of validators

asldro.validators.parameters.range_exclusive_validator(start, end)asldro.validators.parameters.Validator

Validate that a given value is between a given range (excluding the start and end values). Can be used with int, float or BaseImageContainer. :param start: the start value :param end: the end value

asldro.validators.parameters.range_inclusive_validator(start, end)asldro.validators.parameters.Validator

Validate that a given value is between a given range (including the start and end values) Can be used with int, float or BaseImageContainer. :param start: the start value :param end: the end value

asldro.validators.parameters.regex_validator(pattern: str, case_insensitive: bool = False)asldro.validators.parameters.Validator

Validates that a value matches the given regex pattern :param pattern: the regex pattern to match :param case_insensitive: perform a case-insensitive matching

asldro.validators.parameters.reserved_string_list_validator(strings: List[str], delimiter=' ', case_insensitive: bool = False)asldro.validators.parameters.Validator

Validates that the value is a string which is comprised only of the list of given strings, separated by the delimiter. The strings may be repeated multiple times and in any order, although the value must not be the empty string. e.g. with strings=[‘foo’,’bar’] and delimiter=’_’, this would match: “foo_bar_foo”, “foo”, “bar”, “bar_bar_bar_bar_foo” but would not match: “”, “FOO”, “anythingelse”, “foo__bar”, “bar foo” :param strings: a list of strings :param delimiter: a delimiter (defaults to space) :param case_insensitive: perform a case-insensitive matching

asldro.validators.parameters.shape_validator(keys: List[asldro.containers.image.BaseImageContainer], maxdim: int = None)asldro.validators.parameters.Validator

Checks that all of the keys have the same shape If all the supplied inputs have matching shapes, this validator evaluates as True.

Note this is intended to be used as a post validator as the argument for the validator is a dictionary.

Parameters

images (Union[List[str], Tuple[str]]) – A list (or tuple) of strings

asldro.validators.user_parameter_input module

A user input validator. Used to initialise the model. All of the validation rules are contained within this file. The validator may be used with: d = USER_INPUT_VALIDATOR(some_input_dictionary) d will now contain the input dictionary with any defaults values added. A ValidationError will be raised if any validation rules fail.

asldro.validators.user_parameter_input.asl_context_length_validator_generator(other)
asldro.validators.user_parameter_input.generate_parameter_distribution(param: dict, length=1) → list

Generates a list of values based on the supplied distribution specification. If the argument param is not a dictionary, its value will be returned. Values will be returned rounded to 4 decimal places.

Parameters
  • param (dict) – Parameter distribution

  • length (int, optional) – number of values to generate, defaults to 1

Raises

ValidationError – [description]

Returns

[description]

Return type

list

asldro.validators.user_parameter_input.get_example_input_params() → dict

Generate and validate an example input parameter dictionary. Will contain one of each supported image type containing the default parameters for each. :return: the validated input parameter dictionary :raises asldro.validators.parameters.ValidationError: if the input

validation does not pass

asldro.validators.user_parameter_input.validate_input_params(input_params: dict) → dict

Validate the input parameters :param input_params: The input parameters asa Python dict :returns: The parsed input parameter dictionary, with any defaults added :raises asldro.validators.parameters.ValidationError: if the input

validation does not pass

Module contents