Implementation:Evidentlyai Evidently Legacy Engine
| Knowledge Sources | |
|---|---|
| Domains | Calculation_Engine, Metrics, Legacy |
| Last Updated | 2026-02-14 12:00 GMT |
Overview
legacy/calculation_engine/engine.py defines the abstract Engine base class and supporting infrastructure for executing metric calculations in the legacy Evidently pipeline, including a metric implementation registry and metric deduplication logic.
Description
This module is the backbone of the legacy calculation engine. It provides:
EngineDatasets-- A generic dataclass holdingcurrentandreferencedatasets (both optional). Supports iteration to yield both datasets in order.
Engine-- An abstract generic class parameterized byTMetricImplementation,TInputData, andTEngineDataType. Core responsibilities:set_metrics/set_tests-- Register metrics and tests for execution.execute_metrics-- The main execution loop. Converts input data, calculates additional (generated) features, injects them into the data, then iterates through metrics with deduplication. Each metric is calculated once and cached; errors are captured asErrorResult.get_additional_features-- Collects all features required by registered metrics from theDataDefinition, deduplicating by fingerprint.get_metric_execution_iterator-- Aggregates metrics by type and parameters, deduplicating identical calculations so metrics with the same parameters share a single computation.get_metric_implementation-- Looks up the engine-specific implementation class for a metric from the global_ImplRegistry.
metric_implementationdecorator -- Registers aMetricImplementationsubclass as the implementation for a specific metric class. The decorator queriessupported_engines()on the implementation to register it for the correct engine type(s).
_ImplRegistry-- A module-level dictionary mapping engine types to dictionaries of metric types to implementation classes. Enforces single-implementation-per-metric-per-engine.
Usage
This module is used internally by the legacy Evidently pipeline. Subclass Engine to create engine-specific implementations (e.g., PythonEngine for pandas). Use the @metric_implementation decorator on metric implementation classes to register them with the engine.
Code Reference
Source Location
- Repository: Evidentlyai_Evidently
- File:
src/evidently/legacy/calculation_engine/engine.py
Signature
@dataclasses.dataclass
class EngineDatasets(Generic[TEngineDataType]):
current: Optional[TEngineDataType]
reference: Optional[TEngineDataType]
def __iter__(self): ...
class Engine(Generic[TMetricImplementation, TInputData, TEngineDataType]):
def __init__(self): ...
def set_metrics(self, metrics): ...
def set_tests(self, tests): ...
def execute_metrics(self, context: "Context", data: GenericInputData): ...
@abc.abstractmethod
def convert_input_data(self, data: GenericInputData) -> TInputData: ...
@abc.abstractmethod
def get_data_definition(self, current_data, reference_data, column_mapping, categorical_features_cardinality=None): ...
@abc.abstractmethod
def calculate_additional_features(self, data, features, options): ...
@abc.abstractmethod
def merge_additional_features(self, features): ...
def inject_additional_features(self, data, features): ...
def get_additional_features(self, data_definition) -> List[GeneratedFeatures]: ...
def get_metric_implementation(self, metric): ...
def get_metric_execution_iterator(self) -> List[Tuple[Metric, TMetricImplementation]]: ...
def form_datasets(self, data, features, data_definition) -> EngineDatasets[TEngineDataType]: ...
def metric_implementation(metric_cls) -> Callable: ...
Import
from evidently.legacy.calculation_engine.engine import Engine, EngineDatasets, metric_implementation
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| context | Context |
Yes | Suite context containing metric results, options, and feature state. |
| data | GenericInputData |
Yes | Input data wrapper containing current data, optional reference data, column mapping, and data definition. |
| metrics | list |
Yes | List of Metric instances to execute (via set_metrics).
|
| tests | list |
Yes | List of test instances (via set_tests).
|
Outputs
| Name | Type | Description |
|---|---|---|
| execute_metrics side effect | None |
Populates context.metric_results with MetricResult or ErrorResult for each metric.
|
| get_metric_execution_iterator return | List[Tuple[Metric, TMetricImplementation]] |
Deduplicated list pairing each metric with its engine-specific implementation. |
| get_additional_features return | List[GeneratedFeatures] |
Deduplicated list of all features required by registered metrics. |
Usage Examples
from evidently.legacy.calculation_engine.engine import metric_implementation
from evidently.legacy.calculation_engine.metric_implementation import MetricImplementation
# Register a metric implementation for a specific engine
@metric_implementation(MyCustomMetric)
class MyCustomMetricImpl(MetricImplementation):
@classmethod
def supported_engines(cls):
return (PythonEngine,)
def calculate(self, context, data):
# compute metric result
...