-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathmetric.py
More file actions
172 lines (143 loc) · 5.41 KB
/
metric.py
File metadata and controls
172 lines (143 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""Class definition of the payload used to send a metric to ``hub``."""
from __future__ import annotations
from abc import ABC, abstractmethod
from collections.abc import Callable
from contextlib import suppress
from functools import reduce
from math import isfinite
from typing import (
TYPE_CHECKING,
Any,
ClassVar,
Generic,
Literal,
TypeVar,
cast,
)
from pydantic import BaseModel, ConfigDict, Field, computed_field
from skore_hub_project.protocol import CrossValidationReport, EstimatorReport
if TYPE_CHECKING:
from pandas import DataFrame
Report = TypeVar("Report", bound=(EstimatorReport | CrossValidationReport))
def cast_to_float(value: Any) -> float | None:
"""Cast value to float."""
with suppress(TypeError):
float_value = float(value)
if isfinite(float_value):
return float_value
return None
class Metric(BaseModel, ABC, Generic[Report]):
"""
Payload used to send a metric to ``hub``.
Attributes
----------
report : EstimatorReport | CrossValidationReport
The report on which compute the metric.
name : str
Name of the metric.
verbose_name : str
Verbose name of the metric.
data_source : Literal["train", "test"] | None, optional
Data source of the metric when it can be declined in several ways, default None.
greater_is_better: bool | None, optional
Indicator of "greater value is better", default None.
position: int | None, optional
Indicator of the "position" of the metric in the parallel coordinates plot,
default None to disable its display.
"""
model_config = ConfigDict(arbitrary_types_allowed=True)
report: Report = Field(repr=False, exclude=True)
name: str = Field(init=False)
verbose_name: str = Field(init=False)
data_source: Literal["train", "test"] | None = Field(init=False)
greater_is_better: bool | None = Field(init=False)
position: int | None = Field(init=False)
@computed_field
@property
def value(self) -> float | None:
"""The value of the metric."""
try:
return self.__value
except AttributeError:
message = (
"You cannot access the value of a metric "
"without explicitly calculating it. "
"Please use `metric.compute()` before."
)
raise RuntimeError(message) from None
@value.setter
def value(self, value: float | None) -> None:
self.__value = value
@abstractmethod
def compute(self) -> None:
"""Compute the value of the metric."""
class EstimatorReportMetric(Metric[EstimatorReport]):
"""
Payload used to send an estimator report metric.
Attributes
----------
report: EstimatorReport
The report on which compute the metric.
name : str
Name of the metric.
verbose_name : str
Verbose name of the metric.
data_source : Literal["train", "test"] | None, optional
Data source of the metric when it can be declined in several ways, default None.
greater_is_better: bool | None, optional
Indicator of "greater value is better", default None.
position: int | None, optional
Indicator of the "position" of the metric in the parallel coordinates plot,
default None to disable its display.
accessor : ClassVar[str]
The "accessor" of the metric i.e., the path to the metric calculation function.
"""
accessor: ClassVar[str]
def compute(self) -> None:
"""Compute the value of the metric."""
try:
function = cast(
Callable[..., float | None],
reduce(getattr, self.accessor.split("."), self.report),
)
except AttributeError:
self.value = None
else:
self.value = cast_to_float(function(data_source=self.data_source))
class CrossValidationReportMetric(Metric[CrossValidationReport]):
"""
Payload used to send a cross-validation report metric, usually MEAN or STD.
Attributes
----------
report: CrossValidationReport
The report on which compute the metric.
name : str
Name of the metric.
verbose_name : str
Verbose name of the metric.
data_source : Literal["train", "test"] | None, optional
Data source of the metric when it can be declined in several ways, default None.
greater_is_better: bool | None, optional
Indicator of "greater value is better", default None.
position: int | None, optional
Indicator of the "position" of the metric in the parallel coordinates plot,
default None to disable its display.
accessor : ClassVar[str]
The "accessor" of the metric i.e., the path to the metric calculation function.
aggregate : ClassVar[Literal["mean", "std"]]
The aggregation parameter passed to the ``accessor``.
"""
accessor: ClassVar[str]
aggregate: ClassVar[Literal["mean", "std"]]
def compute(self) -> None:
"""Compute the value of the metric."""
try:
function = cast(
"Callable[..., DataFrame]",
reduce(getattr, self.accessor.split("."), self.report),
)
except AttributeError:
self.value = None
else:
dataframe = function(data_source=self.data_source, aggregate=self.aggregate)
self.value = cast_to_float(dataframe.iloc[0, 0])