Implementation:Evidentlyai Evidently Legacy Metric Calculation
| Knowledge Sources | |
|---|---|
| Domains | Metrics, Legacy Compatibility |
| Last Updated | 2026-02-14 12:00 GMT |
Overview
Provides the LegacyMetricCalculation abstract base class that bridges legacy Evidently metrics into the modern metric calculation framework.
Description
The Legacy Metric Calculation module defines the LegacyMetricCalculation abstract base class, which acts as an adapter between the legacy metric system (evidently.legacy.base_metric.Metric and MetricResult) and the modern metric calculation system (evidently.core.metric_types.MetricCalculation). This allows existing legacy metrics to be used transparently within the new report and metric pipeline.
LegacyMetricCalculation is a generic class parameterized by four type variables:
- TResult -- the modern metric result type
- TMetric -- the modern metric type
- TLegacyResult -- the legacy metric result type (bound to LegacyMetricResult)
- TLegacyMetric -- the legacy metric type (bound to LegacyMetric)
Subclasses must implement two abstract methods:
- legacy_metric() -- returns the corresponding legacy metric instance
- calculate_value() -- converts the legacy result and widgets into the modern metric result format
The calculate method orchestrates the bridge: it calls the context's get_legacy_metric to execute the legacy metric, then delegates to calculate_value for conversion. It handles both single and paired (current/reference) results and applies default rendering widgets. Additional widgets can be provided via the overridable get_additional_widgets method.
The _gen_input_data method generates InputData from the current Context for the legacy metric execution.
Usage
Use this base class when you need to wrap an existing legacy Evidently metric for use in the modern metric system. Subclass LegacyMetricCalculation, implement legacy_metric and calculate_value, and optionally override get_additional_widgets or _gen_input_data for custom input data generation.
Code Reference
Source Location
- Repository: Evidentlyai_Evidently
- File:
src/evidently/metrics/_legacy.py
Signature
class LegacyMetricCalculation(
MetricCalculation[TResult, TMetric],
Generic[TResult, TMetric, TLegacyResult, TLegacyMetric],
abc.ABC,
):
@abc.abstractmethod
def legacy_metric(self) -> TLegacyMetric:
...
def task_name(self) -> Optional[str]:
...
def calculate(self, context: "Context", current_data: Dataset, reference_data: Optional[Dataset]) -> TMetricResult:
...
def get_additional_widgets(self, context: "Context") -> List[BaseWidgetInfo]:
...
@abc.abstractmethod
def calculate_value(
self,
context: "Context",
legacy_result: TLegacyResult,
render: List[BaseWidgetInfo],
) -> TMetricResult:
...
def _gen_input_data(self, context: "Context", task_name: Optional[str]) -> InputData:
...
Import
from evidently.metrics._legacy import LegacyMetricCalculation
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| context | Context | Yes | The report context providing access to data, metrics, and legacy metric execution |
| current_data | Dataset | Yes | The current dataset to evaluate |
| reference_data | Optional[Dataset] | No | An optional reference dataset for comparison |
| legacy_result | TLegacyResult | Yes (for calculate_value) | The result object produced by the legacy metric |
| render | List[BaseWidgetInfo] | Yes (for calculate_value) | Widget rendering information from the legacy metric |
Outputs
| Name | Type | Description |
|---|---|---|
| calculate return | TMetricResult | A tuple of (current_result, reference_result) or a single result; the modern metric result with widgets attached |
| calculate_value return | TMetricResult | The converted modern metric result from legacy metric output |
| legacy_metric return | TLegacyMetric | The legacy metric instance to be executed |
| get_additional_widgets return | List[BaseWidgetInfo] | Additional rendering widgets to append (default: empty list) |
Usage Examples
from evidently.metrics._legacy import LegacyMetricCalculation
from evidently.core.metric_types import SingleValue, SingleValueMetric
from evidently.legacy.base_metric import Metric as LegacyMetric, MetricResult as LegacyMetricResult
from evidently.legacy.model.widget import BaseWidgetInfo
from typing import List
# Example subclass bridging a legacy metric
class MyLegacyCalculation(
LegacyMetricCalculation[SingleValue, MyMetric, MyLegacyResult, MyLegacyMetric]
):
def legacy_metric(self) -> MyLegacyMetric:
return MyLegacyMetric(param=self.metric.param)
def calculate_value(
self, context, legacy_result: MyLegacyResult, render: List[BaseWidgetInfo]
) -> SingleValue:
result = SingleValue(value=legacy_result.score, display_name=self.display_name())
result.widget = render
return result
def display_name(self) -> str:
return "My Custom Metric"