Skip to content

spec_validator

mlte/spec/spec_validator.py

Class in charge of validating a Spec.

SpecValidator

Helper class to validate a spec.

Source code in mlte/validation/spec_validator.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class SpecValidator:
    """
    Helper class to validate a spec.
    """

    def __init__(self, spec: Spec):
        """
        Initialize a SpecValidator instance.

        :param spec: The specification to be validated
        """

        self.spec = spec
        """The specification to be validated."""

        self.values: Dict[str, Value] = {}
        """Where values will be gathered for validation."""

    def add_values(self, values: List[Value]):
        """
        Adds multiple values.

        :param values: The list of values to add to the internal list.
        """
        for value in values:
            self.add_value(value)

    def add_value(self, value: Value):
        """
        Adds a value associated to a QACategory and measurements.

        :param value: The value to add to the internal list.
        """
        if value.metadata.get_id() in self.values:
            raise RuntimeError(
                f"Can't have two values with the same id: {value.metadata.get_id()}"
            )
        self.values[value.metadata.get_id()] = value

    def validate(self) -> ValidatedSpec:
        """
        Validates the internal QACategory given its requirements and the stored values, and generates a ValidatedSpec from it.

        :return: The validated specification
        """
        results = self._validate_results()
        return ValidatedSpec(spec=self.spec, results=results)

    def _validate_results(self) -> Dict[str, Dict[str, Result]]:
        """
        Validates a set of stored Values, and generates Results.

        :return: A dictionary having results for each id, separated by QACategory id.
        """
        # Check that all conditions have values to be validated.
        for conditions in self.spec.qa_categories.values():
            for measurement_id in conditions.keys():
                if measurement_id not in self.values:
                    raise RuntimeError(
                        f"Id '{measurement_id}' does not have a value that can be validated."
                    )

        # Validate and aggregate the results.
        results: Dict[str, Dict[str, Result]] = {}
        for category, conditions in self.spec.qa_categories.items():
            results[category.name] = {}
            for measurement_id, condition in conditions.items():
                results[category.name][measurement_id] = condition(
                    self.values[measurement_id]
                )
        return results

spec = spec instance-attribute

The specification to be validated.

values = {} instance-attribute

Where values will be gathered for validation.

__init__(spec)

Initialize a SpecValidator instance.

Parameters:

Name Type Description Default
spec Spec

The specification to be validated

required
Source code in mlte/validation/spec_validator.py
26
27
28
29
30
31
32
33
34
35
36
37
def __init__(self, spec: Spec):
    """
    Initialize a SpecValidator instance.

    :param spec: The specification to be validated
    """

    self.spec = spec
    """The specification to be validated."""

    self.values: Dict[str, Value] = {}
    """Where values will be gathered for validation."""

add_value(value)

Adds a value associated to a QACategory and measurements.

Parameters:

Name Type Description Default
value Value

The value to add to the internal list.

required
Source code in mlte/validation/spec_validator.py
48
49
50
51
52
53
54
55
56
57
58
def add_value(self, value: Value):
    """
    Adds a value associated to a QACategory and measurements.

    :param value: The value to add to the internal list.
    """
    if value.metadata.get_id() in self.values:
        raise RuntimeError(
            f"Can't have two values with the same id: {value.metadata.get_id()}"
        )
    self.values[value.metadata.get_id()] = value

add_values(values)

Adds multiple values.

Parameters:

Name Type Description Default
values List[Value]

The list of values to add to the internal list.

required
Source code in mlte/validation/spec_validator.py
39
40
41
42
43
44
45
46
def add_values(self, values: List[Value]):
    """
    Adds multiple values.

    :param values: The list of values to add to the internal list.
    """
    for value in values:
        self.add_value(value)

validate()

Validates the internal QACategory given its requirements and the stored values, and generates a ValidatedSpec from it.

Returns:

Type Description
ValidatedSpec

The validated specification

Source code in mlte/validation/spec_validator.py
60
61
62
63
64
65
66
67
def validate(self) -> ValidatedSpec:
    """
    Validates the internal QACategory given its requirements and the stored values, and generates a ValidatedSpec from it.

    :return: The validated specification
    """
    results = self._validate_results()
    return ValidatedSpec(spec=self.spec, results=results)