Skip to content

opaque

An opaque evaluation evidence, without semantics.

Opaque

Bases: Evidence

The 'default' Value instance for measurements that do not provide their own.

Source code in mlte/evidence/types/opaque.py
16
17
18
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
class Opaque(Evidence):
    """
    The 'default' Value instance for measurements that do not provide their own.
    """

    def __init__(self, data: Dict[str, Any]):
        """
        Initialize an Opaque instance.
        :param data: The output of the measurement
        """
        super().__init__()

        self.data = data
        """The raw output from measurement execution."""

    def to_model(self) -> ArtifactModel:
        """
        Convert an opaque value artifact to its corresponding model.
        :return: The artifact model
        """
        return self._to_artifact_model(
            value_model=OpaqueValueModel(data=self.data)
        )

    @classmethod
    def from_model(cls, model: BaseModel) -> Opaque:
        """
        Convert an opaque value model to its corresponding artifact.
        :param model: The model representation
        :return: The real value
        """
        body = cls._check_proper_types(model, EvidenceType.OPAQUE)
        return Opaque(data=body.value.data).with_metadata(body.metadata)  # type: ignore

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

    def __getitem__(self, key: str) -> Any:
        """
        Access an item from the wrapped data object.
        :param key: The key that identifies the item to access
        :raises KeyError: If the key is not present
        :return: The value associated with `key`.
        """
        if key not in self.data:
            raise KeyError(f"Key {key} not found.")
        return self.data[key]

    def __setitem__(self, key: str, value: str) -> None:
        """Raise ValueError to indicate Opaque is read-only."""
        raise ValueError("Opaque is read-only.")

    def __eq__(self, other: object) -> bool:
        """Compare Opaque instances for equality."""
        if not isinstance(other, Opaque):
            return False
        return self._equal(other)

    def __str__(self) -> str:
        """Return a string representation of this Evidence."""
        return f"{self.data}"

data = data instance-attribute

The raw output from measurement execution.

__eq__(other)

Compare Opaque instances for equality.

Source code in mlte/evidence/types/opaque.py
71
72
73
74
75
def __eq__(self, other: object) -> bool:
    """Compare Opaque instances for equality."""
    if not isinstance(other, Opaque):
        return False
    return self._equal(other)

__getitem__(key)

Access an item from the wrapped data object.

Parameters:

Name Type Description Default
key str

The key that identifies the item to access

required

Returns:

Type Description
Any

The value associated with key.

Raises:

Type Description
KeyError

If the key is not present

Source code in mlte/evidence/types/opaque.py
56
57
58
59
60
61
62
63
64
65
def __getitem__(self, key: str) -> Any:
    """
    Access an item from the wrapped data object.
    :param key: The key that identifies the item to access
    :raises KeyError: If the key is not present
    :return: The value associated with `key`.
    """
    if key not in self.data:
        raise KeyError(f"Key {key} not found.")
    return self.data[key]

__init__(data)

Initialize an Opaque instance.

Parameters:

Name Type Description Default
data Dict[str, Any]

The output of the measurement

required
Source code in mlte/evidence/types/opaque.py
21
22
23
24
25
26
27
28
29
def __init__(self, data: Dict[str, Any]):
    """
    Initialize an Opaque instance.
    :param data: The output of the measurement
    """
    super().__init__()

    self.data = data
    """The raw output from measurement execution."""

__setitem__(key, value)

Raise ValueError to indicate Opaque is read-only.

Source code in mlte/evidence/types/opaque.py
67
68
69
def __setitem__(self, key: str, value: str) -> None:
    """Raise ValueError to indicate Opaque is read-only."""
    raise ValueError("Opaque is read-only.")

__str__()

Return a string representation of this Evidence.

Source code in mlte/evidence/types/opaque.py
77
78
79
def __str__(self) -> str:
    """Return a string representation of this Evidence."""
    return f"{self.data}"

from_model(model) classmethod

Convert an opaque value model to its corresponding artifact.

Parameters:

Name Type Description Default
model BaseModel

The model representation

required

Returns:

Type Description
Opaque

The real value

Source code in mlte/evidence/types/opaque.py
40
41
42
43
44
45
46
47
48
@classmethod
def from_model(cls, model: BaseModel) -> Opaque:
    """
    Convert an opaque value model to its corresponding artifact.
    :param model: The model representation
    :return: The real value
    """
    body = cls._check_proper_types(model, EvidenceType.OPAQUE)
    return Opaque(data=body.value.data).with_metadata(body.metadata)  # type: ignore

to_model()

Convert an opaque value artifact to its corresponding model.

Returns:

Type Description
ArtifactModel

The artifact model

Source code in mlte/evidence/types/opaque.py
31
32
33
34
35
36
37
38
def to_model(self) -> ArtifactModel:
    """
    Convert an opaque value artifact to its corresponding model.
    :return: The artifact model
    """
    return self._to_artifact_model(
        value_model=OpaqueValueModel(data=self.data)
    )