Skip to content

Commit

Permalink
Support value names in all metrics. (#1431)
Browse files Browse the repository at this point in the history
  • Loading branch information
Liraim authored Jan 17, 2025
1 parent 5ffb8a7 commit 64ba965
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/evidently/future/metric_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,12 @@ def labels(self) -> List[Label]:

def get_label_result(self, label: Label) -> SingleValue:
value = SingleValue(self.values[label])
value._metric = self.metric
value._metric_value_location = ByLabelValueLocation(self.metric.to_metric(), label)
metric = self.metric
value._metric = metric
if not isinstance(metric, ByLabelCalculation):
raise ValueError(f"Metric {type(metric)} isn't ByLabelCalculation")
value.set_display_name(metric.label_display_name(label))
value._metric_value_location = ByLabelValueLocation(metric.to_metric(), label)
return value

def dict(self) -> object:
Expand Down Expand Up @@ -254,14 +258,22 @@ class MeanStdValue(MetricResult):

def get_mean(self) -> SingleValue:
value = SingleValue(self.mean)
value._metric = self.metric
value._metric_value_location = MeanStdValueLocation(self.metric.to_metric(), True)
metric = self.metric
value._metric = metric
if not isinstance(metric, MeanStdCalculation):
raise ValueError(f"Metric {type(metric)} is not MeanStdCalculation")
value.set_display_name(metric.mean_display_name())
value._metric_value_location = MeanStdValueLocation(metric.to_metric(), True)
return value

def get_std(self) -> SingleValue:
value = SingleValue(self.std)
value._metric = self.metric
value._metric_value_location = MeanStdValueLocation(self.metric.to_metric(), False)
metric = self.metric
if not isinstance(metric, MeanStdCalculation):
raise ValueError(f"Metric {type(metric)} is not MeanStdCalculation")
value._metric = metric
value.set_display_name(metric.std_display_name())
value._metric_value_location = MeanStdValueLocation(metric.to_metric(), False)
return value

def dict(self) -> object:
Expand Down Expand Up @@ -755,6 +767,9 @@ class ByLabelCalculation(MetricCalculation[ByLabelValue, TByLabelMetric], Generi
def label_metric(self, label: Label) -> SingleValueCalculation:
raise NotImplementedError

def label_display_name(self, label: Label) -> str:
return self.display_name() + f" for label {label}"


class CountBoundTest(BoundTest[CountValue]):
is_count: bool
Expand Down Expand Up @@ -834,4 +849,8 @@ def get_bound_tests(self, context: "Context") -> Sequence[BoundTest]:


class MeanStdCalculation(MetricCalculation[MeanStdValue, TMeanStdMetric], Generic[TMeanStdMetric], ABC):
pass
def mean_display_name(self) -> str:
return self.display_name()

def std_display_name(self) -> str:
return self.display_name()
4 changes: 4 additions & 0 deletions src/evidently/future/metrics/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
from typing import TypeVar
from typing import Union

from evidently.future.metric_types import ByLabelCalculation
from evidently.future.metric_types import ByLabelMetric
from evidently.future.metric_types import ByLabelValue
from evidently.future.metric_types import SingleValue
from evidently.future.metric_types import SingleValueCalculation
from evidently.future.metric_types import SingleValueMetric
from evidently.future.metrics._legacy import LegacyMetricCalculation
from evidently.future.report import Context
Expand Down Expand Up @@ -37,6 +39,7 @@ class ClassificationQuality(SingleValueMetric):


class LegacyClassificationQualityByClass(
ByLabelCalculation[TByLabelMetric],
LegacyMetricCalculation[
ByLabelValue,
TByLabelMetric,
Expand Down Expand Up @@ -171,6 +174,7 @@ def display_name(self) -> str:


class LegacyClassificationQuality(
SingleValueCalculation[TSingleValueMetric],
LegacyMetricCalculation[
SingleValue,
TSingleValueMetric,
Expand Down
6 changes: 6 additions & 0 deletions src/evidently/future/metrics/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from typing import List

from evidently.base_metric import InputData
from evidently.future.metric_types import MeanStdCalculation
from evidently.future.metric_types import MeanStdMetric
from evidently.future.metric_types import MeanStdValue
from evidently.future.metric_types import SingleValue
from evidently.future.metric_types import SingleValueCalculation
from evidently.future.metric_types import SingleValueMetric
from evidently.future.metric_types import TMeanStdMetric
from evidently.future.metric_types import TSingleValueMetric
Expand All @@ -20,6 +22,7 @@


class LegacyRegressionMeanStdMetric(
MeanStdCalculation[TMeanStdMetric],
LegacyMetricCalculation[MeanStdValue, TMeanStdMetric, RegressionQualityMetricResults, RegressionQualityMetric],
Generic[TMeanStdMetric],
abc.ABC,
Expand All @@ -45,6 +48,7 @@ def _gen_input_data(self, context: "Context") -> InputData:


class LegacyRegressionSingleValueMetric(
SingleValueCalculation[TSingleValueMetric],
LegacyMetricCalculation[SingleValue, TSingleValueMetric, RegressionQualityMetricResults, RegressionQualityMetric],
Generic[TSingleValueMetric],
abc.ABC,
Expand Down Expand Up @@ -178,6 +182,7 @@ def display_name(self) -> str:


class LegacyRegressionDummyMeanStdMetric(
MeanStdCalculation[TMeanStdMetric],
LegacyMetricCalculation[MeanStdValue, TMeanStdMetric, RegressionDummyMetricResults, RegressionDummyMetric],
Generic[TMeanStdMetric],
abc.ABC,
Expand All @@ -203,6 +208,7 @@ def _gen_input_data(self, context: "Context") -> InputData:


class LegacyRegressionDummyValueMetric(
SingleValueCalculation[TSingleValueMetric],
LegacyMetricCalculation[SingleValue, TSingleValueMetric, RegressionDummyMetricResults, RegressionDummyMetric],
Generic[TSingleValueMetric],
abc.ABC,
Expand Down

0 comments on commit 64ba965

Please sign in to comment.