Skip to content

external_measurement

mlte/measurement/external_measurement.py

Base class for measurements calculated by external functions.

ExternalMeasurement

Bases: Measurement

Source code in mlte/measurement/external_measurement.py
15
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
class ExternalMeasurement(Measurement):
    def __init__(
        self,
        identifier: str,
        value_type: type,
        function: Optional[Callable[..., Any]] = None,
    ):
        """
        Initialize a new ExternalMeasurement measurement.

        :param identifier: A unique identifier for the instance
        :param value_type: The type of the Value this measurement will return.
        :param function: The function to be used when evaluating.
        """
        super().__init__(self, identifier)

        if not issubclass(value_type, Value):
            raise Exception(
                f"Value type provided is not a subtype of Value: {value_type}"
            )
        self.value_type: type = value_type

        if function is not None:
            # Store the function module+name as additional metadata info, for better traceability.
            self.metadata.info = (
                f"function: {function.__module__}.{function.__name__}"
            )

        self.function: Optional[Callable[..., Any]] = function
        """Store the callable function itself."""

    def __call__(self, *args, **kwargs) -> Value:
        """Evaluate a measurement and return values without semantics."""
        if self.function is None:
            raise Exception("Can't evaluate, no function was set.")

        value: Value = self.value_type(
            self.metadata, self.function(*args, **kwargs)
        )
        return value

    def ingest(self, *args, **kwargs) -> Value:
        """Ingest data without evaluating a function, to wrap it as the configured Value type. Currently works the same as evaluate()."""
        value: Value = self.value_type(self.metadata, *args, **kwargs)
        return value

    @classmethod
    def value(self) -> Type[Value]:
        """Returns the class type object for the Value produced by the Measurement."""
        return self.value_type

function = function instance-attribute

Store the callable function itself.

__call__(*args, **kwargs)

Evaluate a measurement and return values without semantics.

Source code in mlte/measurement/external_measurement.py
46
47
48
49
50
51
52
53
54
def __call__(self, *args, **kwargs) -> Value:
    """Evaluate a measurement and return values without semantics."""
    if self.function is None:
        raise Exception("Can't evaluate, no function was set.")

    value: Value = self.value_type(
        self.metadata, self.function(*args, **kwargs)
    )
    return value

__init__(identifier, value_type, function=None)

Initialize a new ExternalMeasurement measurement.

Parameters:

Name Type Description Default
identifier str

A unique identifier for the instance

required
value_type type

The type of the Value this measurement will return.

required
function Optional[Callable[..., Any]]

The function to be used when evaluating.

None
Source code in mlte/measurement/external_measurement.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
def __init__(
    self,
    identifier: str,
    value_type: type,
    function: Optional[Callable[..., Any]] = None,
):
    """
    Initialize a new ExternalMeasurement measurement.

    :param identifier: A unique identifier for the instance
    :param value_type: The type of the Value this measurement will return.
    :param function: The function to be used when evaluating.
    """
    super().__init__(self, identifier)

    if not issubclass(value_type, Value):
        raise Exception(
            f"Value type provided is not a subtype of Value: {value_type}"
        )
    self.value_type: type = value_type

    if function is not None:
        # Store the function module+name as additional metadata info, for better traceability.
        self.metadata.info = (
            f"function: {function.__module__}.{function.__name__}"
        )

    self.function: Optional[Callable[..., Any]] = function
    """Store the callable function itself."""

ingest(*args, **kwargs)

Ingest data without evaluating a function, to wrap it as the configured Value type. Currently works the same as evaluate().

Source code in mlte/measurement/external_measurement.py
56
57
58
59
def ingest(self, *args, **kwargs) -> Value:
    """Ingest data without evaluating a function, to wrap it as the configured Value type. Currently works the same as evaluate()."""
    value: Value = self.value_type(self.metadata, *args, **kwargs)
    return value

value() classmethod

Returns the class type object for the Value produced by the Measurement.

Source code in mlte/measurement/external_measurement.py
61
62
63
64
@classmethod
def value(self) -> Type[Value]:
    """Returns the class type object for the Value produced by the Measurement."""
    return self.value_type