Skip to content

metric

Metric

Bases: Named, Protocol

Minimal template for metrics.

Call flow

call -> prepare(true), prepare(predicted) -> compute(true, predicted)

prepare(data)

Hook to normalize/collect/select data before computing metric.

Default: identity. Mixins override and call super().prepare(...)

Source code in src/flowcean/core/metric.py
25
26
27
28
29
30
def prepare(self, data: Data) -> Data:
    """Hook to normalize/collect/select data before computing metric.

    Default: identity. Mixins override and call super().prepare(...)
    """
    return data

__call__(true, predicted)

Execute metric: prepare inputs then compute.

Source code in src/flowcean/core/metric.py
36
37
38
39
40
41
42
43
@final
def __call__(
    self,
    true: Data,
    predicted: Data,
) -> Reportable | dict[str, Reportable]:
    """Execute metric: prepare inputs then compute."""
    return self.compute(true, predicted)

compute(true, predicted)

Implement metric logic on prepared inputs.

Source code in src/flowcean/core/metric.py
45
46
47
48
49
def compute(self, true: Data, predicted: Data) -> Reportable:
    """Implement metric logic on prepared inputs."""
    t = self.prepare(true)
    p = self.prepare(predicted)
    return self._compute(t, p)

ActiveMetric

Bases: Named, Protocol

Base class for metrics for active environments.

__call__(observations, actions) abstractmethod

Calculate the metric value based on the observations.

Parameters:

Name Type Description Default
observations list[Observation]

list of observations of the environment

required
actions list[Action]

list of actions of the learner

required

Returns:

Type Description
Reportable | dict[str, Reportable]

Metric value

Source code in src/flowcean/core/metric.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
@abstractmethod
def __call__(
    self,
    observations: list[Observation],
    actions: list[Action],
) -> Reportable | dict[str, Reportable]:
    """Calculate the metric value based on the observations.

    Args:
        observations: list of observations of the environment
        actions: list of actions of the learner

    Returns:
        Metric value
    """