Skip to content

artifact

Artifact implementation for MLTE Evidence.

T = TypeVar('T', bound='Evidence') module-attribute

Needed for generic return of type.

Evidence

Bases: Artifact, ABC

The Evidence class serves as the base class of all semantically-enriched measurement evaluation evidence. The Evidence provides a common interface for inspecting the results of measurement evaluation, and also encapsulates the functionality required to uniquely associate evaluation results with the originating measurement.

Source code in mlte/evidence/artifact.py
 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
class Evidence(Artifact, ABC):
    """
    The Evidence class serves as the base class of all
    semantically-enriched measurement evaluation evidence.
    The Evidence provides a common interface for inspecting
    the results of measurement evaluation, and also
    encapsulates the functionality required to uniquely
    associate evaluation results with the originating measurement.
    """

    type = ArtifactType.EVIDENCE
    """Class attribute indicating type."""

    def __init__(self):
        """Initialize a Evidence instance"""
        """We use the default id for now, it will be updated later with the metadata."""
        super().__init__()

        self.typename: str = meta.get_qualified_name(self.__class__)
        """The class type of the evidence itself."""

        self.metadata: Optional[EvidenceMetadata] = None
        """Metadata has not been initialized yet."""

    def with_metadata(self: T, evidence_metadata: EvidenceMetadata) -> T:
        """Sets the evidence metadata, returns updated object."""
        self.metadata = evidence_metadata

        # Also set the identifier associated to the metadata.
        self.identifier = self.build_full_id(evidence_metadata.test_case_id)

        return self

    def __str__(self) -> str:
        """Return a string representation."""
        return self.to_model().to_json_string()

    # -------------------------------------------------------------------------
    # Model handling.
    # -------------------------------------------------------------------------

    def _to_artifact_model(self, value_model: BaseModel) -> ArtifactModel:
        """
        Convert a evidence artifact to its corresponding artifact model.
        :param value_model: The specific evidence/value model data.
        """
        if not self.metadata:
            raise RuntimeError(
                "Can't convert Evidence to model, it is missing its metadata."
            )

        model = ArtifactModel(
            header=self.build_artifact_header(),
            body=EvidenceModel(
                metadata=self.metadata,
                evidence_class=meta.get_qualified_name(self.__class__),
                value=value_model,  # type: ignore
            ),
        )
        return model

    @abstractmethod
    def to_model(self) -> ArtifactModel:
        """
        Convert a evidence artifact to its corresponding model.
        NOTE: To cope with polymorphism, the Evidence artifact type
        does not define this required method itself; instead, it
        is delegated to subclasses that implement concrete types
        """
        raise NotImplementedError("Evidence.to_model()")

    @classmethod
    @abstractmethod
    def from_model(cls, _: BaseModel) -> Evidence:
        """
        Convert a evidence model to its corresponding artifact.
        NOTE: To cope with polymorphism, the Evidence artifact type
        does not define this required method itself; instead, it
        is delegated to subclasses that implement concrete types
        """
        raise NotImplementedError("Evidence.from_model()")

    @staticmethod
    def _check_proper_types(
        model: BaseModel, evidence_type: EvidenceType
    ) -> EvidenceModel:
        """
        Checks if internal types are the ones expected. Raises exception if expected conditions are not met.
        Returns the type-casted body as an EvidenceModel.
        """
        assert isinstance(
            model, ArtifactModel
        ), "Can't create object from non-ArtifactModel model."
        assert (
            model.header.type == ArtifactType.EVIDENCE
        ), f"Incorrect header type: {model.header.type}, expected {ArtifactType.EVIDENCE}."
        body = typing.cast(EvidenceModel, model.body)
        assert (
            body.value.evidence_type == evidence_type
        ), f"Incorrect evidence type: {body.value.evidence_type}, expected {evidence_type}."

        return body

    # -------------------------------------------------------------------------
    # Class loading methods.
    # -------------------------------------------------------------------------

    @staticmethod
    def load_all() -> list[Evidence]:
        """Loads all Evidences stored for the current session's context and store."""
        evidence_models = Evidence.load_models_for_session(
            ArtifactType.EVIDENCE
        )
        return Evidence._load_from_models(evidence_models)

    @staticmethod
    def load_all_with(context: Context, store: ArtifactStore) -> list[Evidence]:
        """Loads all Evidences stored for the given context and store."""
        evidence_models = Evidence.load_models(
            ArtifactType.EVIDENCE, context, store
        )
        return Evidence._load_from_models(evidence_models)

    @staticmethod
    def _load_from_models(
        evidence_models: list[ArtifactModel],
    ) -> list[Evidence]:
        """Converts a list of evidence models (as Artifact Models) into evidence artifacts."""
        evidences: list[Evidence] = []
        for artifact_model in evidence_models:
            model: EvidenceModel = typing.cast(
                EvidenceModel, artifact_model.body
            )
            evidence_class: Evidence = typing.cast(
                Evidence, load_class_or_function(model.evidence_class)
            )
            evidence = evidence_class.from_model(artifact_model)
            evidences.append(evidence)
        return evidences

    # Overriden.
    @classmethod
    def load(cls, identifier: typing.Optional[str] = None) -> Evidence:
        """
        Load a Evidence from the configured global session.
        :param identifier: The identifier for the artifact. If None,
        the default id is used.
        """
        evidence = super().load(identifier)
        return typing.cast(Evidence, evidence)

metadata = None instance-attribute

Metadata has not been initialized yet.

type = ArtifactType.EVIDENCE class-attribute instance-attribute

Class attribute indicating type.

typename = meta.get_qualified_name(self.__class__) instance-attribute

The class type of the evidence itself.

__init__()

Initialize a Evidence instance

Source code in mlte/evidence/artifact.py
39
40
41
42
43
44
45
46
47
48
def __init__(self):
    """Initialize a Evidence instance"""
    """We use the default id for now, it will be updated later with the metadata."""
    super().__init__()

    self.typename: str = meta.get_qualified_name(self.__class__)
    """The class type of the evidence itself."""

    self.metadata: Optional[EvidenceMetadata] = None
    """Metadata has not been initialized yet."""

__str__()

Return a string representation.

Source code in mlte/evidence/artifact.py
59
60
61
def __str__(self) -> str:
    """Return a string representation."""
    return self.to_model().to_json_string()

from_model(_) abstractmethod classmethod

Convert a evidence model to its corresponding artifact. NOTE: To cope with polymorphism, the Evidence artifact type does not define this required method itself; instead, it is delegated to subclasses that implement concrete types

Source code in mlte/evidence/artifact.py
 97
 98
 99
100
101
102
103
104
105
106
@classmethod
@abstractmethod
def from_model(cls, _: BaseModel) -> Evidence:
    """
    Convert a evidence model to its corresponding artifact.
    NOTE: To cope with polymorphism, the Evidence artifact type
    does not define this required method itself; instead, it
    is delegated to subclasses that implement concrete types
    """
    raise NotImplementedError("Evidence.from_model()")

load(identifier=None) classmethod

Load a Evidence from the configured global session.

Parameters:

Name Type Description Default
identifier Optional[str]

The identifier for the artifact. If None, the default id is used.

None
Source code in mlte/evidence/artifact.py
167
168
169
170
171
172
173
174
175
@classmethod
def load(cls, identifier: typing.Optional[str] = None) -> Evidence:
    """
    Load a Evidence from the configured global session.
    :param identifier: The identifier for the artifact. If None,
    the default id is used.
    """
    evidence = super().load(identifier)
    return typing.cast(Evidence, evidence)

load_all() staticmethod

Loads all Evidences stored for the current session's context and store.

Source code in mlte/evidence/artifact.py
133
134
135
136
137
138
139
@staticmethod
def load_all() -> list[Evidence]:
    """Loads all Evidences stored for the current session's context and store."""
    evidence_models = Evidence.load_models_for_session(
        ArtifactType.EVIDENCE
    )
    return Evidence._load_from_models(evidence_models)

load_all_with(context, store) staticmethod

Loads all Evidences stored for the given context and store.

Source code in mlte/evidence/artifact.py
141
142
143
144
145
146
147
@staticmethod
def load_all_with(context: Context, store: ArtifactStore) -> list[Evidence]:
    """Loads all Evidences stored for the given context and store."""
    evidence_models = Evidence.load_models(
        ArtifactType.EVIDENCE, context, store
    )
    return Evidence._load_from_models(evidence_models)

to_model() abstractmethod

Convert a evidence artifact to its corresponding model. NOTE: To cope with polymorphism, the Evidence artifact type does not define this required method itself; instead, it is delegated to subclasses that implement concrete types

Source code in mlte/evidence/artifact.py
87
88
89
90
91
92
93
94
95
@abstractmethod
def to_model(self) -> ArtifactModel:
    """
    Convert a evidence artifact to its corresponding model.
    NOTE: To cope with polymorphism, the Evidence artifact type
    does not define this required method itself; instead, it
    is delegated to subclasses that implement concrete types
    """
    raise NotImplementedError("Evidence.to_model()")

with_metadata(evidence_metadata)

Sets the evidence metadata, returns updated object.

Source code in mlte/evidence/artifact.py
50
51
52
53
54
55
56
57
def with_metadata(self: T, evidence_metadata: EvidenceMetadata) -> T:
    """Sets the evidence metadata, returns updated object."""
    self.metadata = evidence_metadata

    # Also set the identifier associated to the metadata.
    self.identifier = self.build_full_id(evidence_metadata.test_case_id)

    return self