Skip to content

real

An Evidence instance for a scalar, real value.

Real

Bases: Evidence

Real implements the Evidence interface for a single real value.

Source code in mlte/evidence/types/real.py
 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
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
class Real(Evidence):
    """
    Real implements the Evidence interface for a single real value.
    """

    def __init__(self, value: float, unit: Optional[Unit] = None):
        """
        Initialize a Real instance.
        :param value: The real value
        :param unit: The unit the values comes in, as a value from Units, defaults to None.
        """
        assert isinstance(value, float), "Argument must be `float`."

        super().__init__()

        self.value = value
        """The wrapped real value."""

        self.unit = unit
        """The unit, if any."""

    def get_value_w_units(self) -> Quantity:  # type: ignore[type-arg]
        """
        Returns the float value as a Quantity, potentially with units.
        """
        return Quantity(self.value, self.unit)

    def to_model(self) -> ArtifactModel:
        """
        Convert a real value artifact to its corresponding model.
        :return: The artifact model
        """
        return self._to_artifact_model(
            value_model=RealValueModel(
                real=self.value, unit=unit_to_str(self.unit)
            )
        )

    @classmethod
    def from_model(cls, model: BaseModel) -> Real:
        """
        Convert a real value model to its corresponding artifact.
        :param model: The model representation
        :return: The real value
        """
        body = cls._check_proper_types(model, EvidenceType.REAL)
        return Real(
            value=body.value.real,  # type: ignore
            unit=str_to_unit(body.value.unit),  # type: ignore
        ).with_metadata(body.metadata)

    def __str__(self) -> str:
        """Return a string representation of the Real."""
        return f"{self.get_value_w_units() if self.unit else self.value}"

    def __eq__(self, other: object) -> bool:
        """Comparison between Real values."""
        if not isinstance(other, Real):
            return False
        return self._equal(other)

    @classmethod
    def less_than(
        cls,
        threshold: float,
        unit: Optional[Unit] = None,
        success: str = "",
        failure: str = "",
    ) -> Validator:
        """
        Determine if real is strictly less than `threshold`.

        :param threshold: The threshold value
        :param unit: the unit the values comes in, as a value from Units
        :return: The Validator that can be used to validate Evidence.
        """
        threshold_w_unit = Quantity(threshold, unit)
        bool_exp: Callable[[Real], bool] = (
            lambda real: real.get_value_w_units() < threshold_w_unit
        )
        validator: Validator = Validator.build_validator(
            bool_exp=bool_exp,
            success=success,
            failure=failure,
            default_success=f"Real magnitude is less than threshold {quantity_to_str(threshold_w_unit)}",
            default_failure=f"Real magnitude exceeds threshold {quantity_to_str(threshold_w_unit)}",
            input_types=[Real],
        )
        return validator

    @classmethod
    def less_or_equal_to(
        cls,
        threshold: float,
        unit: Optional[Unit] = None,
        success: str = "",
        failure: str = "",
    ) -> Validator:
        """
        Determine if real is less than or equal to `threshold`.

        :param threshold: The threshold value
        :param unit: the unit the values comes in, as a value from Units
        :return: The Validator that can be used to validate Evidence.
        """
        threshold_w_unit = Quantity(threshold, unit)
        bool_exp: Callable[[Real], bool] = (
            lambda real: real.get_value_w_units() <= threshold_w_unit
        )
        validator: Validator = Validator.build_validator(
            bool_exp=bool_exp,
            success=success,
            failure=failure,
            default_success=f"Real magnitude is less than or equal to threshold {quantity_to_str(threshold_w_unit)}",
            default_failure=f"Real magnitude exceeds threshold {quantity_to_str(threshold_w_unit)}",
            input_types=[Real],
        )
        return validator

    @classmethod
    def greater_than(
        cls,
        threshold: float,
        unit: Optional[Unit] = None,
        success: str = "",
        failure: str = "",
    ) -> Validator:
        """
        Determine if real is strictly greater than `threshold`.

        :param threshold: The threshold value
        :param unit: the unit the values comes in, as a value from Units
        :return: The Validator that can be used to validate Evidence.
        """
        threshold_w_unit = Quantity(threshold, unit)
        bool_exp: Callable[[Real], bool] = (
            lambda real: real.get_value_w_units() > threshold_w_unit
        )
        validator: Validator = Validator.build_validator(
            bool_exp=bool_exp,
            success=success,
            failure=failure,
            default_success=f"Real magnitude is greater than threshold {quantity_to_str(threshold_w_unit)}",
            default_failure=f"Real magnitude is below threshold {quantity_to_str(threshold_w_unit)}",
            input_types=[Real],
        )
        return validator

    @classmethod
    def greater_or_equal_to(
        cls,
        threshold: float,
        unit: Optional[Unit] = None,
        success: str = "",
        failure: str = "",
    ) -> Validator:
        """
        Determine if real is greater than or equal to `threshold`.

        :param threshold: The threshold value
        :param unit: the unit the values comes in, as a value from Units
        :return: The Validator that can be used to validate Evidence.
        """
        threshold_w_unit = Quantity(threshold, unit)
        bool_exp: Callable[[Real], bool] = (
            lambda real: real.get_value_w_units() >= threshold_w_unit
        )
        validator: Validator = Validator.build_validator(
            bool_exp=bool_exp,
            success=success,
            failure=failure,
            default_success=f"Real magnitude is greater than or equal to threshold {quantity_to_str(threshold_w_unit)}",
            default_failure=f"Real magnitude is below threshold {quantity_to_str(threshold_w_unit)}",
            input_types=[Real],
        )
        return validator

    # Overriden.
    @classmethod
    def load(cls, identifier: typing.Optional[str] = None) -> Real:
        evidence = super().load(identifier)
        return typing.cast(Real, evidence)

unit = unit instance-attribute

The unit, if any.

value = value instance-attribute

The wrapped real value.

__eq__(other)

Comparison between Real values.

Source code in mlte/evidence/types/real.py
79
80
81
82
83
def __eq__(self, other: object) -> bool:
    """Comparison between Real values."""
    if not isinstance(other, Real):
        return False
    return self._equal(other)

__init__(value, unit=None)

Initialize a Real instance.

Parameters:

Name Type Description Default
value float

The real value

required
unit Optional[Unit]

The unit the values comes in, as a value from Units, defaults to None.

None
Source code in mlte/evidence/types/real.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def __init__(self, value: float, unit: Optional[Unit] = None):
    """
    Initialize a Real instance.
    :param value: The real value
    :param unit: The unit the values comes in, as a value from Units, defaults to None.
    """
    assert isinstance(value, float), "Argument must be `float`."

    super().__init__()

    self.value = value
    """The wrapped real value."""

    self.unit = unit
    """The unit, if any."""

__str__()

Return a string representation of the Real.

Source code in mlte/evidence/types/real.py
75
76
77
def __str__(self) -> str:
    """Return a string representation of the Real."""
    return f"{self.get_value_w_units() if self.unit else self.value}"

from_model(model) classmethod

Convert a real value model to its corresponding artifact.

Parameters:

Name Type Description Default
model BaseModel

The model representation

required

Returns:

Type Description
Real

The real value

Source code in mlte/evidence/types/real.py
62
63
64
65
66
67
68
69
70
71
72
73
@classmethod
def from_model(cls, model: BaseModel) -> Real:
    """
    Convert a real value model to its corresponding artifact.
    :param model: The model representation
    :return: The real value
    """
    body = cls._check_proper_types(model, EvidenceType.REAL)
    return Real(
        value=body.value.real,  # type: ignore
        unit=str_to_unit(body.value.unit),  # type: ignore
    ).with_metadata(body.metadata)

get_value_w_units()

Returns the float value as a Quantity, potentially with units.

Source code in mlte/evidence/types/real.py
45
46
47
48
49
def get_value_w_units(self) -> Quantity:  # type: ignore[type-arg]
    """
    Returns the float value as a Quantity, potentially with units.
    """
    return Quantity(self.value, self.unit)

greater_or_equal_to(threshold, unit=None, success='', failure='') classmethod

Determine if real is greater than or equal to threshold.

Parameters:

Name Type Description Default
threshold float

The threshold value

required
unit Optional[Unit]

the unit the values comes in, as a value from Units

None

Returns:

Type Description
Validator

The Validator that can be used to validate Evidence.

Source code in mlte/evidence/types/real.py
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
@classmethod
def greater_or_equal_to(
    cls,
    threshold: float,
    unit: Optional[Unit] = None,
    success: str = "",
    failure: str = "",
) -> Validator:
    """
    Determine if real is greater than or equal to `threshold`.

    :param threshold: The threshold value
    :param unit: the unit the values comes in, as a value from Units
    :return: The Validator that can be used to validate Evidence.
    """
    threshold_w_unit = Quantity(threshold, unit)
    bool_exp: Callable[[Real], bool] = (
        lambda real: real.get_value_w_units() >= threshold_w_unit
    )
    validator: Validator = Validator.build_validator(
        bool_exp=bool_exp,
        success=success,
        failure=failure,
        default_success=f"Real magnitude is greater than or equal to threshold {quantity_to_str(threshold_w_unit)}",
        default_failure=f"Real magnitude is below threshold {quantity_to_str(threshold_w_unit)}",
        input_types=[Real],
    )
    return validator

greater_than(threshold, unit=None, success='', failure='') classmethod

Determine if real is strictly greater than threshold.

Parameters:

Name Type Description Default
threshold float

The threshold value

required
unit Optional[Unit]

the unit the values comes in, as a value from Units

None

Returns:

Type Description
Validator

The Validator that can be used to validate Evidence.

Source code in mlte/evidence/types/real.py
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
@classmethod
def greater_than(
    cls,
    threshold: float,
    unit: Optional[Unit] = None,
    success: str = "",
    failure: str = "",
) -> Validator:
    """
    Determine if real is strictly greater than `threshold`.

    :param threshold: The threshold value
    :param unit: the unit the values comes in, as a value from Units
    :return: The Validator that can be used to validate Evidence.
    """
    threshold_w_unit = Quantity(threshold, unit)
    bool_exp: Callable[[Real], bool] = (
        lambda real: real.get_value_w_units() > threshold_w_unit
    )
    validator: Validator = Validator.build_validator(
        bool_exp=bool_exp,
        success=success,
        failure=failure,
        default_success=f"Real magnitude is greater than threshold {quantity_to_str(threshold_w_unit)}",
        default_failure=f"Real magnitude is below threshold {quantity_to_str(threshold_w_unit)}",
        input_types=[Real],
    )
    return validator

less_or_equal_to(threshold, unit=None, success='', failure='') classmethod

Determine if real is less than or equal to threshold.

Parameters:

Name Type Description Default
threshold float

The threshold value

required
unit Optional[Unit]

the unit the values comes in, as a value from Units

None

Returns:

Type Description
Validator

The Validator that can be used to validate Evidence.

Source code in mlte/evidence/types/real.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
@classmethod
def less_or_equal_to(
    cls,
    threshold: float,
    unit: Optional[Unit] = None,
    success: str = "",
    failure: str = "",
) -> Validator:
    """
    Determine if real is less than or equal to `threshold`.

    :param threshold: The threshold value
    :param unit: the unit the values comes in, as a value from Units
    :return: The Validator that can be used to validate Evidence.
    """
    threshold_w_unit = Quantity(threshold, unit)
    bool_exp: Callable[[Real], bool] = (
        lambda real: real.get_value_w_units() <= threshold_w_unit
    )
    validator: Validator = Validator.build_validator(
        bool_exp=bool_exp,
        success=success,
        failure=failure,
        default_success=f"Real magnitude is less than or equal to threshold {quantity_to_str(threshold_w_unit)}",
        default_failure=f"Real magnitude exceeds threshold {quantity_to_str(threshold_w_unit)}",
        input_types=[Real],
    )
    return validator

less_than(threshold, unit=None, success='', failure='') classmethod

Determine if real is strictly less than threshold.

Parameters:

Name Type Description Default
threshold float

The threshold value

required
unit Optional[Unit]

the unit the values comes in, as a value from Units

None

Returns:

Type Description
Validator

The Validator that can be used to validate Evidence.

Source code in mlte/evidence/types/real.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
@classmethod
def less_than(
    cls,
    threshold: float,
    unit: Optional[Unit] = None,
    success: str = "",
    failure: str = "",
) -> Validator:
    """
    Determine if real is strictly less than `threshold`.

    :param threshold: The threshold value
    :param unit: the unit the values comes in, as a value from Units
    :return: The Validator that can be used to validate Evidence.
    """
    threshold_w_unit = Quantity(threshold, unit)
    bool_exp: Callable[[Real], bool] = (
        lambda real: real.get_value_w_units() < threshold_w_unit
    )
    validator: Validator = Validator.build_validator(
        bool_exp=bool_exp,
        success=success,
        failure=failure,
        default_success=f"Real magnitude is less than threshold {quantity_to_str(threshold_w_unit)}",
        default_failure=f"Real magnitude exceeds threshold {quantity_to_str(threshold_w_unit)}",
        input_types=[Real],
    )
    return validator

to_model()

Convert a real value artifact to its corresponding model.

Returns:

Type Description
ArtifactModel

The artifact model

Source code in mlte/evidence/types/real.py
51
52
53
54
55
56
57
58
59
60
def to_model(self) -> ArtifactModel:
    """
    Convert a real value artifact to its corresponding model.
    :return: The artifact model
    """
    return self._to_artifact_model(
        value_model=RealValueModel(
            real=self.value, unit=unit_to_str(self.unit)
        )
    )