evaluate_from_predictions#

maite.tasks.evaluate_from_predictions(*, metric, pred_batches, target_batches, metadata_batches)[source]#

Evaluate pre-calculated predictions against target (truth) data for some specified metric.

This is useful when predictions have already been computed (e.g., by a prior call to predict, or by a model outside of MAITE) and only the metric value is needed, avoiding a second inference pass.

Parameters:
metricSomeMetric

Compatible MAITE Metric.

pred_batchesSequence[Sequence[SomeTargetType]]

Sequence of batches of predictions generated by running inference on some model.

target_batchesSequence[Sequence[SomeTargetType]]

Sequence of batches of ground-truth targets that correspond to provided predictions argument.

metadata_batchesSequence[Sequence[SomeDatumMetadataType]]

Sequence of batches of datum metadata type.

Returns:
metric_calculation: MetricComputeReturnType

The resulting metric value.

Raises:
ValueError

If predictions or targets arguments have zero length (i.e. no batches), differing lengths, or corresponding elements (batches) have differing lengths.

Examples

We illustrate with image classification, but the same pattern applies to any AI problem. See the maite.protocols.image_classification documentation for details on writing compliant components.

>>> import numpy as np
>>> from collections.abc import Sequence
>>> from maite.protocols import ArrayLike, DatumMetadata, MetricMetadata
>>> from maite.protocols import image_classification as ic

First we define a Metric that accumulates predictions and targets across batches and computes accuracy on compute.

>>> class Accuracy:
...     metadata: MetricMetadata = {"id": "accuracy"}
...
...     def __init__(self) -> None:
...         self.reset()
...
...     def reset(self) -> None:
...         self._correct = 0
...         self._total = 0
...
...     def update(
...         self,
...         preds: Sequence[ArrayLike],
...         targets: Sequence[ArrayLike],
...         metadata: Sequence[DatumMetadata],
...     ) -> None:
...         pred_classes = np.vstack([np.asarray(p) for p in preds]).argmax(axis=1)
...         true_classes = np.vstack([np.asarray(t) for t in targets]).argmax(axis=1)
...         self._correct += int((pred_classes == true_classes).sum())
...         self._total += len(pred_classes)
...
...     def compute(self) -> dict[str, float]:
...         return {"accuracy": self._correct / self._total}

Predictions and targets are each supplied as a sequence of batches, where a batch is a sequence of per-datum score or one-hot label vectors. Here we have two batches of two datapoints, with one of the four predictions disagreeing with its target.

>>> pred_batches = [[np.eye(3)[c] for c in [0, 1]], [np.eye(3)[c] for c in [2, 0]]]
>>> target_batches = [[np.eye(3)[c] for c in [0, 1]], [np.eye(3)[c] for c in [2, 1]]]
>>> metadata_batches: list[list[DatumMetadata]] = [
...     [{"id": 0}, {"id": 1}],
...     [{"id": 2}, {"id": 3}],
... ]
>>> from maite.tasks import evaluate_from_predictions
>>> metric: ic.Metric = Accuracy()
>>> evaluate_from_predictions(
...     metric=metric,
...     pred_batches=pred_batches,
...     target_batches=target_batches,
...     metadata_batches=metadata_batches,
... )
{'accuracy': 0.75}