Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Evidentlyai Evidently Legacy Python Engine

From Leeroopedia
Knowledge Sources
Domains Calculation_Engine, Pandas, Legacy
Last Updated 2026-02-14 12:00 GMT

Overview

legacy/calculation_engine/python_engine.py provides the PythonEngine class, a pandas-based concrete implementation of the abstract Engine, and the PythonMetricImplementation base class for metric implementations that operate on pandas DataFrames.

Description

This module implements the legacy calculation engine for pandas DataFrames:

  • PythonEngine -- Concrete engine that specializes Engine for PythonMetricImplementation, InputData, and pd.DataFrame. Key methods:
    • convert_input_data -- Validates that input data is a pd.DataFrame and wraps it in an InputData object.
    • get_data_definition -- Creates a DataDefinition from current and reference DataFrames using create_data_definition.
    • calculate_additional_features -- Iterates over GeneratedFeatures and generates renamed feature columns for both current and reference data.
    • merge_additional_features -- Joins all generated feature DataFrames into single current and reference DataFrames using pd.DataFrame.join.
    • get_metric_implementation -- Extends the parent method with a fallback: if no registered implementation exists for a metric, creates a dynamic wrapper (_Wrapper) that delegates to the metric's own calculate method.
    • form_datasets -- Joins additional features into the main DataFrames and renames columns to their display names.
  • PythonMetricImplementation -- Generic abstract base class for metric implementations running on PythonEngine. Holds references to the engine and the metric instance. Declares PythonEngine as the only supported engine via supported_engines.

Usage

This is the default engine used by the legacy Evidently pipeline when working with pandas DataFrames. Subclass PythonMetricImplementation and decorate with @metric_implementation to register custom metric calculations.

Code Reference

Source Location

Signature

class PythonEngine(Engine["PythonMetricImplementation", InputData, pd.DataFrame]):
    def convert_input_data(self, data: GenericInputData[pd.DataFrame]) -> InputData: ...
    def get_data_definition(self, current_data, reference_data, column_mapping, categorical_features_cardinality=None): ...
    def calculate_additional_features(self, data, features, options) -> Dict[GeneratedFeatures, FeatureResult[pd.DataFrame]]: ...
    def merge_additional_features(self, features) -> EngineDatasets[pd.DataFrame]: ...
    def get_metric_implementation(self, metric): ...
    def form_datasets(self, data, features, data_definition) -> EngineDatasets[pd.DataFrame]: ...

class PythonMetricImplementation(Generic[TMetric], MetricImplementation):
    def __init__(self, engine: PythonEngine, metric: TMetric): ...
    @abc.abstractmethod
    def calculate(self, context, data: InputData): ...
    @classmethod
    def supported_engines(cls): ...

Import

from evidently.legacy.calculation_engine.python_engine import PythonEngine, PythonMetricImplementation

I/O Contract

Inputs

Name Type Required Description
data GenericInputData[pd.DataFrame] Yes Input data containing current and optional reference DataFrames plus column mapping and data definition.
features List[GeneratedFeatures] Yes List of generated feature definitions to compute and merge into datasets.
options Options Yes Legacy options for feature generation.
context Context Yes Suite context (passed to calculate of metric implementations).

Outputs

Name Type Description
convert_input_data return InputData Validated and wrapped input data for the pandas engine.
calculate_additional_features return Dict[GeneratedFeatures, FeatureResult[pd.DataFrame]] Mapping from feature definitions to their computed current/reference DataFrames.
merge_additional_features return EngineDatasets[pd.DataFrame] Merged current and reference additional feature DataFrames.
form_datasets return EngineDatasets[pd.DataFrame] Final datasets with additional features joined and columns renamed.

Usage Examples

from evidently.legacy.calculation_engine.python_engine import PythonEngine, PythonMetricImplementation
from evidently.legacy.calculation_engine.engine import metric_implementation
from evidently.legacy.base_metric import InputData

@metric_implementation(MyMetric)
class MyMetricPythonImpl(PythonMetricImplementation["MyMetric"]):
    def calculate(self, context, data: InputData):
        current_df = data.current_data
        # Perform calculations on pandas DataFrame
        return MyMetricResult(value=current_df["column"].mean())

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment