forked from openml/openml-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
437 lines (373 loc) · 14.4 KB
/
functions.py
File metadata and controls
437 lines (373 loc) · 14.4 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# License: BSD 3-Clause
# ruff: noqa: PLR0913
from __future__ import annotations
import json
from functools import partial
from itertools import chain
from typing import Any
from typing_extensions import Literal, overload
import numpy as np
import pandas as pd
import xmltodict
import openml
import openml._api_calls
import openml.utils
from openml.evaluations import OpenMLEvaluation
@overload
def list_evaluations(
function: str,
offset: int | None = None,
size: int | None = None,
tasks: list[str | int] | None = None,
setups: list[str | int] | None = None,
flows: list[str | int] | None = None,
runs: list[str | int] | None = None,
uploaders: list[str | int] | None = None,
tag: str | None = None,
study: int | None = None,
per_fold: bool | None = None,
sort_order: str | None = None,
output_format: Literal["dataframe"] = ...,
) -> pd.DataFrame: ...
@overload
def list_evaluations(
function: str,
offset: int | None = None,
size: int | None = None,
tasks: list[str | int] | None = None,
setups: list[str | int] | None = None,
flows: list[str | int] | None = None,
runs: list[str | int] | None = None,
uploaders: list[str | int] | None = None,
tag: str | None = None,
study: int | None = None,
per_fold: bool | None = None,
sort_order: str | None = None,
output_format: Literal["object"] = "object",
) -> dict[int, OpenMLEvaluation]: ...
def list_evaluations(
function: str,
offset: int | None = None,
size: int | None = None,
tasks: list[str | int] | None = None,
setups: list[str | int] | None = None,
flows: list[str | int] | None = None,
runs: list[str | int] | None = None,
uploaders: list[str | int] | None = None,
tag: str | None = None,
study: int | None = None,
per_fold: bool | None = None,
sort_order: str | None = None,
output_format: Literal["object", "dataframe"] = "object",
) -> dict[int, OpenMLEvaluation] | pd.DataFrame:
"""List all run-evaluation pairs matching all of the given filters.
(Supports large amount of results)
Parameters
----------
function : str
the evaluation function. e.g., predictive_accuracy
offset : int, optional
the number of runs to skip, starting from the first
size : int, default 10000
The maximum number of runs to show.
If set to ``None``, it returns all the results.
tasks : list[int,str], optional
the list of task IDs
setups: list[int,str], optional
the list of setup IDs
flows : list[int,str], optional
the list of flow IDs
runs :list[int,str], optional
the list of run IDs
uploaders : list[int,str], optional
the list of uploader IDs
tag : str, optional
filter evaluation based on given tag
study : int, optional
per_fold : bool, optional
sort_order : str, optional
order of sorting evaluations, ascending ("asc") or descending ("desc")
output_format: str, optional (default='object')
The parameter decides the format of the output.
- If 'object' the output is a dict of OpenMLEvaluation objects
- If 'dataframe' the output is a pandas DataFrame
Returns
-------
dict or dataframe
"""
if output_format not in ("dataframe", "object"):
raise ValueError("Invalid output format. Only 'object', 'dataframe'.")
per_fold_str = None
if per_fold is not None:
per_fold_str = str(per_fold).lower()
listing_call = partial(
_list_evaluations,
function=function,
tasks=tasks,
setups=setups,
flows=flows,
runs=runs,
uploaders=uploaders,
tag=tag,
study=study,
sort_order=sort_order,
per_fold=per_fold_str,
)
eval_collection = openml.utils._list_all(listing_call, offset=offset, limit=size)
flattened = list(chain.from_iterable(eval_collection))
if output_format == "dataframe":
records = [item._to_dict() for item in flattened]
return pd.DataFrame.from_records(records) # No index...
return {e.run_id: e for e in flattened}
def _list_evaluations( # noqa: C901
limit: int,
offset: int,
*,
function: str,
tasks: list | None = None,
setups: list | None = None,
flows: list | None = None,
runs: list | None = None,
uploaders: list | None = None,
study: int | None = None,
sort_order: str | None = None,
**kwargs: Any,
) -> list[OpenMLEvaluation]:
"""
Perform API call ``/evaluation/function{function}/{filters}``
Parameters
----------
The arguments that are lists are separated from the single value
ones which are put into the kwargs.
limit : int
the number of evaluations to return
offset : int
the number of evaluations to skip, starting from the first
function : str
the evaluation function. e.g., predictive_accuracy
tasks : list[int,str], optional
the list of task IDs
setups: list[int,str], optional
the list of setup IDs
flows : list[int,str], optional
the list of flow IDs
runs :list[int,str], optional
the list of run IDs
uploaders : list[int,str], optional
the list of uploader IDs
study : int, optional
kwargs: dict, optional
Legal filter operators: tag, per_fold
sort_order : str, optional
order of sorting evaluations, ascending ("asc") or descending ("desc")
Returns
-------
list of OpenMLEvaluation objects
"""
api_call = f"evaluation/list/function/{function}"
if limit is not None:
api_call += f"/limit/{limit}"
if offset is not None:
api_call += f"/offset/{offset}"
if kwargs is not None:
for operator, value in kwargs.items():
if value is not None:
api_call += f"/{operator}/{value}"
if tasks is not None:
api_call += f"/task/{','.join([str(int(i)) for i in tasks])}"
if setups is not None:
api_call += f"/setup/{','.join([str(int(i)) for i in setups])}"
if flows is not None:
api_call += f"/flow/{','.join([str(int(i)) for i in flows])}"
if runs is not None:
api_call += f"/run/{','.join([str(int(i)) for i in runs])}"
if uploaders is not None:
api_call += f"/uploader/{','.join([str(int(i)) for i in uploaders])}"
if study is not None:
api_call += f"/study/{study}"
if sort_order is not None:
api_call += f"/sort_order/{sort_order}"
return __list_evaluations(api_call)
def __list_evaluations(api_call: str) -> list[OpenMLEvaluation]:
"""Helper function to parse API calls which are lists of runs"""
xml_string = openml._api_calls._perform_api_call(api_call, "get")
evals_dict = xmltodict.parse(xml_string, force_list=("oml:evaluation",))
# Minimalistic check if the XML is useful
if "oml:evaluations" not in evals_dict:
raise ValueError(
"Error in return XML, does not contain " f'"oml:evaluations": {evals_dict!s}',
)
assert isinstance(evals_dict["oml:evaluations"]["oml:evaluation"], list), type(
evals_dict["oml:evaluations"],
)
uploader_ids = list(
{eval_["oml:uploader"] for eval_ in evals_dict["oml:evaluations"]["oml:evaluation"]},
)
api_users = "user/list/user_id/" + ",".join(uploader_ids)
xml_string_user = openml._api_calls._perform_api_call(api_users, "get")
users = xmltodict.parse(xml_string_user, force_list=("oml:user",))
user_dict = {user["oml:id"]: user["oml:username"] for user in users["oml:users"]["oml:user"]}
evals = []
for eval_ in evals_dict["oml:evaluations"]["oml:evaluation"]:
run_id = int(eval_["oml:run_id"])
value = float(eval_["oml:value"]) if "oml:value" in eval_ else None
values = json.loads(eval_["oml:values"]) if eval_.get("oml:values", None) else None
array_data = eval_.get("oml:array_data")
evals.append(
OpenMLEvaluation(
run_id=run_id,
task_id=int(eval_["oml:task_id"]),
setup_id=int(eval_["oml:setup_id"]),
flow_id=int(eval_["oml:flow_id"]),
flow_name=eval_["oml:flow_name"],
data_id=int(eval_["oml:data_id"]),
data_name=eval_["oml:data_name"],
function=eval_["oml:function"],
upload_time=eval_["oml:upload_time"],
uploader=int(eval_["oml:uploader"]),
uploader_name=user_dict[eval_["oml:uploader"]],
value=value,
values=values,
array_data=array_data,
)
)
return evals
def list_evaluation_measures() -> list[str]:
"""Return list of evaluation measures available.
The function performs an API call to retrieve the entire list of
evaluation measures that are available.
Returns
-------
list
"""
api_call = "evaluationmeasure/list"
xml_string = openml._api_calls._perform_api_call(api_call, "get")
qualities = xmltodict.parse(xml_string, force_list=("oml:measures"))
# Minimalistic check if the XML is useful
if "oml:evaluation_measures" not in qualities:
raise ValueError('Error in return XML, does not contain "oml:evaluation_measures"')
if not isinstance(qualities["oml:evaluation_measures"]["oml:measures"][0]["oml:measure"], list):
raise TypeError('Error in return XML, does not contain "oml:measure" as a list')
return qualities["oml:evaluation_measures"]["oml:measures"][0]["oml:measure"]
def list_estimation_procedures() -> list[str]:
"""Return list of evaluation procedures available.
The function performs an API call to retrieve the entire list of
evaluation procedures' names that are available.
Returns
-------
list
"""
api_call = "estimationprocedure/list"
xml_string = openml._api_calls._perform_api_call(api_call, "get")
api_results = xmltodict.parse(xml_string)
# Minimalistic check if the XML is useful
if "oml:estimationprocedures" not in api_results:
raise ValueError('Error in return XML, does not contain "oml:estimationprocedures"')
if "oml:estimationprocedure" not in api_results["oml:estimationprocedures"]:
raise ValueError('Error in return XML, does not contain "oml:estimationprocedure"')
if not isinstance(api_results["oml:estimationprocedures"]["oml:estimationprocedure"], list):
raise TypeError('Error in return XML, does not contain "oml:estimationprocedure" as a list')
return [
prod["oml:name"]
for prod in api_results["oml:estimationprocedures"]["oml:estimationprocedure"]
]
def list_evaluations_setups(
function: str,
offset: int | None = None,
size: int | None = None,
tasks: list | None = None,
setups: list | None = None,
flows: list | None = None,
runs: list | None = None,
uploaders: list | None = None,
tag: str | None = None,
per_fold: bool | None = None,
sort_order: str | None = None,
parameters_in_separate_columns: bool = False, # noqa: FBT001, FBT002
) -> pd.DataFrame:
"""List all run-evaluation pairs matching all of the given filters
and their hyperparameter settings.
Parameters
----------
function : str
the evaluation function. e.g., predictive_accuracy
offset : int, optional
the number of runs to skip, starting from the first
size : int, optional
the maximum number of runs to show
tasks : list[int], optional
the list of task IDs
setups: list[int], optional
the list of setup IDs
flows : list[int], optional
the list of flow IDs
runs : list[int], optional
the list of run IDs
uploaders : list[int], optional
the list of uploader IDs
tag : str, optional
filter evaluation based on given tag
per_fold : bool, optional
sort_order : str, optional
order of sorting evaluations, ascending ("asc") or descending ("desc")
parameters_in_separate_columns: bool, optional (default= False)
Returns hyperparameters in separate columns if set to True.
Valid only for a single flow
Returns
-------
dataframe with hyperparameter settings as a list of tuples.
"""
if parameters_in_separate_columns and (flows is None or len(flows) != 1):
raise ValueError("Can set parameters_in_separate_columns to true only for single flow_id")
# List evaluations
evals = list_evaluations(
function=function,
offset=offset,
size=size,
runs=runs,
tasks=tasks,
setups=setups,
flows=flows,
uploaders=uploaders,
tag=tag,
per_fold=per_fold,
sort_order=sort_order,
output_format="dataframe",
)
# List setups
# list_setups by setup id does not support large sizes (exceeds URL length limit)
# Hence we split the list of unique setup ids returned by list_evaluations into chunks of size N
_df = pd.DataFrame()
if len(evals) != 0:
N = 100 # size of section
uniq = np.asarray(evals["setup_id"].unique())
length = len(uniq)
# array_split - allows indices_or_sections to not equally divide the array
# array_split -length % N sub-arrays of size length//N + 1 and the rest of size length//N.
split_size = ((length - 1) // N) + 1
setup_chunks = np.array_split(uniq, split_size)
setup_data = pd.DataFrame()
for _setups in setup_chunks:
result = openml.setups.list_setups(setup=_setups, output_format="dataframe")
assert isinstance(result, pd.DataFrame)
result = result.drop("flow_id", axis=1)
# concat resulting setup chunks into single datframe
setup_data = pd.concat([setup_data, result])
parameters = []
# Convert parameters of setup into dict of (hyperparameter, value)
for parameter_dict in setup_data["parameters"]:
if parameter_dict is not None:
parameters.append(
{param["full_name"]: param["value"] for param in parameter_dict.values()},
)
else:
parameters.append({})
setup_data["parameters"] = parameters
# Merge setups with evaluations
_df = evals.merge(setup_data, on="setup_id", how="left")
if parameters_in_separate_columns:
_df = pd.concat(
[_df.drop("parameters", axis=1), _df["parameters"].apply(pd.Series)],
axis=1,
)
return _df