Skip to content

Commit

Permalink
Add implementations for column_max, column_mean, column_min and colum…
Browse files Browse the repository at this point in the history
…n_quantile.
  • Loading branch information
Liraim committed Dec 12, 2024
1 parent c350d95 commit 92bd2f5
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/evidently/v2/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
from .base import SingleValue
from .base import SingleValueCheck
from .container import MetricContainer
from .max import column_max
from .mean import column_mean
from .min import column_min
from .quantile import column_quantile

__all__ = [
"column_max",
"column_mean",
"column_min",
"column_quantile",
"Metric",
"MetricContainer",
"MetricResult",
Expand Down
21 changes: 19 additions & 2 deletions src/evidently/v2/metrics/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ class Metric(Generic[TResult]):
"""

_metric_id: MetricId
_checks: List[Check]
_checks: Optional[List[Check]]

def __init__(self, metric_id: MetricId, checks: Optional[List[Check]] = None) -> None:
self._metric_id = metric_id
self._checks = checks if checks is not None else []
self._checks = checks

def call(self, current_data: Dataset, reference_data: Optional[Dataset]) -> TResult:
"""
Expand All @@ -165,6 +165,23 @@ def call(self, current_data: Dataset, reference_data: Optional[Dataset]) -> TRes
def calculate(self, current_data: Dataset, reference_data: Optional[Dataset]) -> TResult:
raise NotImplementedError()

def _default_checks(self) -> List[Check]:
"""
allows to redefine default checks for metric
Returns:
list of checks to use as default
"""
return []

def _default_checks_with_reference(self) -> Optional[List[Check]]:
"""
allows to redefine default checks for metric when calculated with reference
Returns:
list of checks to use as default when called with reference data
None - if default checks should be returned
"""
return None

@property
def id(self) -> MetricId:
return self._metric_id
Expand Down
25 changes: 25 additions & 0 deletions src/evidently/v2/metrics/max.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from typing import List
from typing import Optional

from evidently.v2.datasets import Dataset
from evidently.v2.metrics import Metric
from evidently.v2.metrics import SingleValue
from evidently.v2.metrics import SingleValueCheck


class ColumnMax(Metric[SingleValue]):
def __init__(self, column: str, checks: Optional[List[SingleValueCheck]] = None):
super().__init__(f"max:{column}", checks)
self._column = column

def calculate(self, current_data: Dataset, reference_data: Optional[Dataset]) -> SingleValue:
data = current_data.column(self._column)
value = data.data.max()
return SingleValue(value)

def display_name(self) -> str:
return f"Maximum value of {self._column}"


def column_max(column_name: str, checks: Optional[List[SingleValueCheck]] = None) -> ColumnMax:
return ColumnMax(column_name, checks)
24 changes: 24 additions & 0 deletions src/evidently/v2/metrics/mean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from typing import List
from typing import Optional

from evidently.v2.datasets import Dataset
from evidently.v2.metrics import Metric
from evidently.v2.metrics import SingleValue
from evidently.v2.metrics import SingleValueCheck


class ColumnMean(Metric[SingleValue]):
def __init__(self, column: str, checks: Optional[List[SingleValueCheck]] = None):
super().__init__(f"mean:{column}", checks)
self._column = column

def calculate(self, current_data: Dataset, reference_data: Optional[Dataset]) -> SingleValue:
value = current_data.column(self._column).data.mean()
return SingleValue(value)

def display_name(self) -> str:
return f"Mean value of '{self._column}'"


def column_mean(column: str, checks: Optional[List[SingleValueCheck]] = None):
return ColumnMean(column, checks)
8 changes: 4 additions & 4 deletions src/evidently/v2/metrics/min.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from evidently.v2.metrics import SingleValueCheck


class MinMetric(Metric[SingleValue]):
class ColumnMin(Metric[SingleValue]):
def __init__(self, column: str, checks: Optional[List[SingleValueCheck]] = None):
super().__init__(f"min:{column}", checks if checks is not None else [])
super().__init__(f"min:{column}", checks)
self._column = column

def calculate(self, current_data: Dataset, reference_data: Optional[Dataset]) -> SingleValue:
Expand All @@ -21,5 +21,5 @@ def display_name(self) -> str:
return f"Minimal value of {self._column}"


def min_metric(column_name: str) -> MinMetric:
return MinMetric(column_name)
def column_min(column_name: str, checks: Optional[List[SingleValueCheck]] = None) -> ColumnMin:
return ColumnMin(column_name, checks)
28 changes: 28 additions & 0 deletions src/evidently/v2/metrics/quantile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import List
from typing import Optional

from evidently.v2.datasets import Dataset
from evidently.v2.metrics import Metric
from evidently.v2.metrics import SingleValue
from evidently.v2.metrics import SingleValueCheck


class ColumnQuantile(Metric[SingleValue]):
def __init__(self, column: str, quantile: float, checks: Optional[List[SingleValueCheck]] = None):
super().__init__(f"quantile:{quantile}:{column}", checks)
self._quantile = quantile
self._column = column

def calculate(self, current_data: Dataset, reference_data: Optional[Dataset]) -> SingleValue:
data = current_data.column(self._column)
value = data.data.quantile(self._quantile)
return SingleValue(value)

def display_name(self) -> str:
return f"Quantile {self._quantile} of {self._column}"


def column_quantile(
column_name: str, quantile: float, checks: Optional[List[SingleValueCheck]] = None
) -> ColumnQuantile:
return ColumnQuantile(column_name, quantile, checks)

0 comments on commit 92bd2f5

Please sign in to comment.