Skip to content

real

mlte/value/types/real.py

An Value instance for a scalar, real value.

Real

Bases: Value

Real implements the Value interface for a single real value.

Source code in mlte/value/types/real.py
 19
 20
 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
 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
class Real(Value):
    """
    Real implements the Value interface for a single real value.
    """

    def __init__(self, metadata: EvidenceMetadata, value: float):
        """
        Initialize a Real instance.
        :param metadata: The generating measurement's metadata
        :param value: The real value
        """
        assert isinstance(value, float), "Argument must be `float`."

        super().__init__(self, metadata)

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

    def to_model(self) -> ArtifactModel:
        """
        Convert a real value artifact to its corresponding model.
        :return: The artifact model
        """
        return ArtifactModel(
            header=self.build_artifact_header(),
            body=ValueModel(
                metadata=self.metadata,
                value_class=self.get_class_path(),
                value=RealValueModel(
                    real=self.value,
                ),
            ),
        )

    @classmethod
    def from_model(cls, model: ArtifactModel) -> Real:
        """
        Convert a real value model to its corresponding artifact.
        :param model: The model representation
        :return: The real value
        """
        assert model.header.type == ArtifactType.VALUE, "Broken Precondition."
        body = typing.cast(ValueModel, model.body)

        assert body.value.value_type == ValueType.REAL, "Broken Precondition."
        return Real(
            metadata=body.metadata,
            value=body.value.real,
        )

    def __str__(self) -> str:
        """Return a string representation of the Real."""
        return f"{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) -> Condition:
        """
        Determine if real is strictly less than `threshold`.

        :param threshold: The threshold value
        :return: The Condition that can be used to validate a Value.
        """
        condition: Condition = Condition.build_condition(
            bool_exp=lambda real: real.value < threshold,
            success=f"Real magnitude is less than threshold {threshold}",
            failure=f"Real magnitude exceeds threshold {threshold}",
        )
        return condition

    @classmethod
    def less_or_equal_to(cls, threshold: float) -> Condition:
        """
        Determine if real is less than or equal to `threshold`.

        :param threshold: The threshold value
        :return: The Condition that can be used to validate a Value.
        """
        condition: Condition = Condition.build_condition(
            bool_exp=lambda real: real.value <= threshold,
            success=f"Real magnitude is less than or equal to threshold {threshold}",
            failure=f"Real magnitude exceeds threshold {threshold}",
        )
        return condition

    @classmethod
    def greater_than(cls, threshold: float) -> Condition:
        """
        Determine if real is strictly greater than `threshold`.

        :param threshold: The threshold value
        :return: The Condition that can be used to validate a Value.
        """
        condition: Condition = Condition.build_condition(
            bool_exp=lambda real: real.value > threshold,
            success=f"Real magnitude is greater than threshold {threshold}",
            failure=f"Real magnitude is below threshold {threshold}",
        )
        return condition

    @classmethod
    def greater_or_equal_to(cls, threshold: float) -> Condition:
        """
        Determine if real is greater than or equal to `threshold`.

        :param threshold: The threshold value
        :return: The Condition that can be used to validate a Value.
        """
        condition: Condition = Condition.build_condition(
            bool_exp=lambda real: real.value >= threshold,
            success=f"Real magnitude is greater than or equal to threshold {threshold}",
            failure=f"Real magnitude is below threshold {threshold}",
        )
        return condition

value = value instance-attribute

The wrapped real value.

__eq__(other)

Comparison between Real values.

Source code in mlte/value/types/real.py
73
74
75
76
77
def __eq__(self, other: object) -> bool:
    """Comparison between Real values."""
    if not isinstance(other, Real):
        return False
    return self._equal(other)

__init__(metadata, value)

Initialize a Real instance.

Parameters:

Name Type Description Default
metadata EvidenceMetadata

The generating measurement's metadata

required
value float

The real value

required
Source code in mlte/value/types/real.py
24
25
26
27
28
29
30
31
32
33
34
35
def __init__(self, metadata: EvidenceMetadata, value: float):
    """
    Initialize a Real instance.
    :param metadata: The generating measurement's metadata
    :param value: The real value
    """
    assert isinstance(value, float), "Argument must be `float`."

    super().__init__(self, metadata)

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

__str__()

Return a string representation of the Real.

Source code in mlte/value/types/real.py
69
70
71
def __str__(self) -> str:
    """Return a string representation of the Real."""
    return f"{self.value}"

from_model(model) classmethod

Convert a real value model to its corresponding artifact.

Parameters:

Name Type Description Default
model ArtifactModel

The model representation

required

Returns:

Type Description
Real

The real value

Source code in mlte/value/types/real.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
@classmethod
def from_model(cls, model: ArtifactModel) -> Real:
    """
    Convert a real value model to its corresponding artifact.
    :param model: The model representation
    :return: The real value
    """
    assert model.header.type == ArtifactType.VALUE, "Broken Precondition."
    body = typing.cast(ValueModel, model.body)

    assert body.value.value_type == ValueType.REAL, "Broken Precondition."
    return Real(
        metadata=body.metadata,
        value=body.value.real,
    )

greater_or_equal_to(threshold) classmethod

Determine if real is greater than or equal to threshold.

Parameters:

Name Type Description Default
threshold float

The threshold value

required

Returns:

Type Description
Condition

The Condition that can be used to validate a Value.

Source code in mlte/value/types/real.py
124
125
126
127
128
129
130
131
132
133
134
135
136
137
@classmethod
def greater_or_equal_to(cls, threshold: float) -> Condition:
    """
    Determine if real is greater than or equal to `threshold`.

    :param threshold: The threshold value
    :return: The Condition that can be used to validate a Value.
    """
    condition: Condition = Condition.build_condition(
        bool_exp=lambda real: real.value >= threshold,
        success=f"Real magnitude is greater than or equal to threshold {threshold}",
        failure=f"Real magnitude is below threshold {threshold}",
    )
    return condition

greater_than(threshold) classmethod

Determine if real is strictly greater than threshold.

Parameters:

Name Type Description Default
threshold float

The threshold value

required

Returns:

Type Description
Condition

The Condition that can be used to validate a Value.

Source code in mlte/value/types/real.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
@classmethod
def greater_than(cls, threshold: float) -> Condition:
    """
    Determine if real is strictly greater than `threshold`.

    :param threshold: The threshold value
    :return: The Condition that can be used to validate a Value.
    """
    condition: Condition = Condition.build_condition(
        bool_exp=lambda real: real.value > threshold,
        success=f"Real magnitude is greater than threshold {threshold}",
        failure=f"Real magnitude is below threshold {threshold}",
    )
    return condition

less_or_equal_to(threshold) classmethod

Determine if real is less than or equal to threshold.

Parameters:

Name Type Description Default
threshold float

The threshold value

required

Returns:

Type Description
Condition

The Condition that can be used to validate a Value.

Source code in mlte/value/types/real.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
@classmethod
def less_or_equal_to(cls, threshold: float) -> Condition:
    """
    Determine if real is less than or equal to `threshold`.

    :param threshold: The threshold value
    :return: The Condition that can be used to validate a Value.
    """
    condition: Condition = Condition.build_condition(
        bool_exp=lambda real: real.value <= threshold,
        success=f"Real magnitude is less than or equal to threshold {threshold}",
        failure=f"Real magnitude exceeds threshold {threshold}",
    )
    return condition

less_than(threshold) classmethod

Determine if real is strictly less than threshold.

Parameters:

Name Type Description Default
threshold float

The threshold value

required

Returns:

Type Description
Condition

The Condition that can be used to validate a Value.

Source code in mlte/value/types/real.py
79
80
81
82
83
84
85
86
87
88
89
90
91
92
@classmethod
def less_than(cls, threshold: float) -> Condition:
    """
    Determine if real is strictly less than `threshold`.

    :param threshold: The threshold value
    :return: The Condition that can be used to validate a Value.
    """
    condition: Condition = Condition.build_condition(
        bool_exp=lambda real: real.value < threshold,
        success=f"Real magnitude is less than threshold {threshold}",
        failure=f"Real magnitude exceeds threshold {threshold}",
    )
    return condition

to_model()

Convert a real value artifact to its corresponding model.

Returns:

Type Description
ArtifactModel

The artifact model

Source code in mlte/value/types/real.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def to_model(self) -> ArtifactModel:
    """
    Convert a real value artifact to its corresponding model.
    :return: The artifact model
    """
    return ArtifactModel(
        header=self.build_artifact_header(),
        body=ValueModel(
            metadata=self.metadata,
            value_class=self.get_class_path(),
            value=RealValueModel(
                real=self.value,
            ),
        ),
    )