Skip to content

Commit 3a05157

Browse files
authored
refactor: updated OpenMLEvaluation to use dataclass decorator (#1559)
I have Refactored the `OpenMLEvaluation` class from a traditional Python class to use the `@dataclass` decorator to reduce boilerplate code and improve code maintainability. #### Metadata * Reference Issue: #1540 * New Tests Added: No * Documentation Updated: No * Change Log Entry: Refactored the `OpenMLEvaluation` class to use the `@dataclass` #### Details Edited the `OpenMLEvaluation` class in `openml\evaluations\evaluation.py` to use `@dataclass` decorator. This significantly reduces the boilerplate code in the following places: - Instance Variable Definitions **Before:** ```python def __init__( self, run_id: int, task_id: int, setup_id: int, flow_id: int, flow_name: str, data_id: int, data_name: str, function: str, upload_time: str, uploader: int, uploader_name: str, value: float | None, values: list[float] | None, array_data: str | None = None, ): self.run_id = run_id self.task_id = task_id self.setup_id = setup_id self.flow_id = flow_id self.flow_name = flow_name self.data_id = data_id self.data_name = data_name self.function = function self.upload_time = upload_time self.uploader = uploader self.uploader_name = uploader_name self.value = value self.values = values self.array_data = array_data ``` **After:** ```python run_id: int task_id: int setup_id: int flow_id: int flow_name: str data_id: int data_name: str function: str upload_time: str uploader: int uploader_name: str value: float | None values: list[float] | None array_data: str | None = None ``` - _to_dict Method Simplification **Before:** ```python def _to_dict(self) -> dict: return { "run_id": self.run_id, "task_id": self.task_id, "setup_id": self.setup_id, "flow_id": self.flow_id, "flow_name": self.flow_name, "data_id": self.data_id, "data_name": self.data_name, "function": self.function, "upload_time": self.upload_time, "uploader": self.uploader, "uploader_name": self.uploader_name, "value": self.value, "values": self.values, "array_data": self.array_data, } ``` **After:** ```python def _to_dict(self) -> dict: return asdict(self) ``` All tests are passing with accordnce to the changes: ```bash PS C:\Users\ASUS\Documents\work\opensource\openml-python> pytest tests/test_evaluations/ ======================================= test session starts ======================================= platform win32 -- Python 3.14.0, pytest-9.0.2, pluggy-1.6.0 rootdir: C:\Users\ASUS\Documents\work\opensource\openml-python configfile: pyproject.toml plugins: anyio-4.12.0, flaky-3.8.1, asyncio-1.3.0, cov-7.0.0, mock-3.15.1, rerunfailures-16.1, timeout-2.4.0, xdist-3.8.0, requests-mock-1.12.1 asyncio: mode=Mode.STRICT, debug=False, asyncio_default_fixture_loop_scope=None, asyncio_default_test_loop_scope=function collected 13 items tests\test_evaluations\test_evaluation_functions.py ............ [ 92%] tests\test_evaluations\test_evaluations_example.py . [100%] ================================= 13 passed in 274.80s (0:04:34) ================================== ```
1 parent 8672ffb commit 3a05157

File tree

1 file changed

+21
-51
lines changed

1 file changed

+21
-51
lines changed

openml/evaluations/evaluation.py

Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# License: BSD 3-Clause
22
from __future__ import annotations
33

4+
from dataclasses import asdict, dataclass
5+
46
import openml.config
57
import openml.datasets
68
import openml.flows
79
import openml.runs
810
import openml.tasks
911

1012

11-
# TODO(eddiebergman): A lot of this class is automatically
12-
# handled by a dataclass
13+
@dataclass
1314
class OpenMLEvaluation:
1415
"""
1516
Contains all meta-information about a run / evaluation combination,
@@ -48,55 +49,23 @@ class OpenMLEvaluation:
4849
(e.g., in case of precision, auroc, recall)
4950
"""
5051

51-
def __init__( # noqa: PLR0913
52-
self,
53-
run_id: int,
54-
task_id: int,
55-
setup_id: int,
56-
flow_id: int,
57-
flow_name: str,
58-
data_id: int,
59-
data_name: str,
60-
function: str,
61-
upload_time: str,
62-
uploader: int,
63-
uploader_name: str,
64-
value: float | None,
65-
values: list[float] | None,
66-
array_data: str | None = None,
67-
):
68-
self.run_id = run_id
69-
self.task_id = task_id
70-
self.setup_id = setup_id
71-
self.flow_id = flow_id
72-
self.flow_name = flow_name
73-
self.data_id = data_id
74-
self.data_name = data_name
75-
self.function = function
76-
self.upload_time = upload_time
77-
self.uploader = uploader
78-
self.uploader_name = uploader_name
79-
self.value = value
80-
self.values = values
81-
self.array_data = array_data
52+
run_id: int
53+
task_id: int
54+
setup_id: int
55+
flow_id: int
56+
flow_name: str
57+
data_id: int
58+
data_name: str
59+
function: str
60+
upload_time: str
61+
uploader: int
62+
uploader_name: str
63+
value: float | None
64+
values: list[float] | None
65+
array_data: str | None = None
8266

8367
def _to_dict(self) -> dict:
84-
return {
85-
"run_id": self.run_id,
86-
"task_id": self.task_id,
87-
"setup_id": self.setup_id,
88-
"flow_id": self.flow_id,
89-
"flow_name": self.flow_name,
90-
"data_id": self.data_id,
91-
"data_name": self.data_name,
92-
"function": self.function,
93-
"upload_time": self.upload_time,
94-
"uploader": self.uploader,
95-
"uploader_name": self.uploader_name,
96-
"value": self.value,
97-
"values": self.values,
98-
"array_data": self.array_data,
99-
}
68+
return asdict(self)
10069

10170
def __repr__(self) -> str:
10271
header = "OpenML Evaluation"
@@ -119,11 +88,12 @@ def __repr__(self) -> str:
11988
}
12089

12190
order = [
122-
"Uploader Date",
91+
"Upload Date",
12392
"Run ID",
12493
"OpenML Run URL",
12594
"Task ID",
126-
"OpenML Task URL" "Flow ID",
95+
"OpenML Task URL",
96+
"Flow ID",
12797
"OpenML Flow URL",
12898
"Setup ID",
12999
"Data ID",

0 commit comments

Comments
 (0)