-
-
Notifications
You must be signed in to change notification settings - Fork 272
[ENH] V1 → V2 API Migration - studies #1610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
88077a7
1dbc780
13acf35
e02e05b
4c75e16
5762185
9170edc
021a1e1
4c4a12c
1d91220
3e26ace
0060b2e
65ba66b
317c6e9
503ab82
8c980c9
fd7ea2b
fa3cd40
7e9bc1f
c603383
ff6a8b0
f01898f
5c4511e
9d0098f
43276d2
1206f69
bde5942
4948e99
a354167
1fe7e3e
54a3151
2b6fe65
fa53f8d
2b2db96
c9617f9
9d2e4e4
e05dec5
ad64cfb
5bc37b8
08d9916
8caba11
1913c10
7681949
01840a5
26ed4c1
c588d0c
b6ff720
80d5afc
f47112c
d44cf3e
ea7dda1
f0e5947
edcd006
cde0aae
aa1e560
06b8497
384da91
735a335
6b67c1f
1878138
dc26e01
e2d059b
d156ad4
d7a3788
b5b9ef6
68820fe
567eca4
b2287c3
b7e285e
fd43c48
f4aab6b
d43cf86
3e323ed
9195fa6
5342eec
adc0e74
bfb2d3e
ee10f59
9c0ad45
cabaecf
85c1113
0458929
39bf86a
7b66677
d2224c4
5e3fea8
fc32488
eda66ca
18dc72a
9608c36
f6bc7f7
baa3a38
647a5cd
52b93fe
ec9477f
ea8dae9
10d134a
935f0f4
9514df8
85ad3df
f96a34c
272b710
53bee94
33b4ca0
a6b9a45
f924b32
541b0f2
acb173f
3e8d1f0
f83bdb5
9a7aebe
0a6e836
2a42712
001caad
fb38a2d
03c4ca9
8d708fd
4f75bba
164f66f
c4dae43
36c20a2
ab3c1eb
2a488ca
599c7e1
2867862
f09f3cd
5f731ce
bad7842
aefdb38
0f40b02
7ac1672
6ac1dfe
62924c9
27696bb
190face
95daaa6
7841ea8
cc515aa
e6a92df
1b8c22a
fc839a6
1c922af
ffa9ce9
a7b2d21
27fe790
8965112
72ea1a4
a696c49
755636d
d07af34
2d9c8ec
002b989
045d896
c437966
e27470a
d04d956
9263f7f
79dea29
f6497c2
dce7f54
40dd460
0fc917c
3d86b18
aba3d3e
d99d54d
dc22e3a
7318573
29ef187
cf94c89
298fbda
9870502
33065c2
76b92bb
419edcb
cb6d937
8544c8a
d4c413b
fab1a15
276324a
73f7594
2ee7fa3
4be5bbd
9027c01
e5461a9
7d899a9
8587414
23a3450
ac28f82
3ce1095
a110fa0
18f31f7
b1ddc93
960e601
677051b
68f3ff6
2e1cdcf
96cee47
4a66245
c762fb4
77c21f2
eac24fc
2ed65fe
f3b07de
29db3f1
3b4e538
8ac886b
305f4f0
b2bf164
e97e6c2
c66d73c
aa54e8e
2d452d3
c235812
39eb823
50eed37
7a000eb
f120c19
79f6187
adea726
242e420
b1a9e7f
d716ecf
3c29e71
b4ff0b2
93155ee
d3cc9a7
a6b82f4
3419973
8de99b7
7d61107
1ecbbba
65472ed
a0a3b61
33858a7
a704bb0
3470cb5
44b48b5
62c3d3d
c93b97f
972987b
04bc83b
f926092
083194b
c224532
3953fdf
c2d8487
edf8524
ca2cdc5
6f30a45
03ea718
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,11 +1,162 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from __future__ import annotations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from .base import ResourceV1API, ResourceV2API, StudyAPI | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import builtins | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import pandas as pd | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import xmltodict | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from openml._api.resources.base import ResourceV1API, ResourceV2API, StudyAPI | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class StudyV1API(ResourceV1API, StudyAPI): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Version 1 API implementation for study resources.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def list( # noqa: PLR0913 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| limit: int | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| offset: int | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| status: str | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| main_entity_type: str | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| uploader: builtins.list[int] | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| benchmark_suite: int | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> pd.DataFrame: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """List studies using V1 API. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ---------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| limit : int, optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Maximum number of studies to return. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| offset : int, optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Number of studies to skip. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| status : str, optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Filter by status (active, in_preparation, deactivated, all). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| main_entity_type : str, optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Filter by main entity type (run, task). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| uploader : list[int], optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Filter by uploader IDs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| benchmark_suite : int, optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Filter by benchmark suite ID. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pd.DataFrame | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DataFrame containing study information. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| api_call = self._build_url( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| limit=limit, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| offset=offset, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| status=status, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| main_entity_type=main_entity_type, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| uploader=uploader, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| benchmark_suite=benchmark_suite, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| response = self._http.get(api_call) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| xml_string = response.content.decode("utf-8") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return self._parse_list_xml(xml_string) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @staticmethod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _build_url( # noqa: PLR0913 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| limit: int | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| offset: int | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| status: str | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| main_entity_type: str | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| uploader: builtins.list[int] | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| benchmark_suite: int | None = None, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> str: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PGijsbers marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Build the V1 API URL for listing studies. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ---------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| limit : int, optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Maximum number of studies to return. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| offset : int, optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Number of studies to skip. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| status : str, optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Filter by status (active, in_preparation, deactivated, all). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| main_entity_type : str, optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Filter by main entity type (run, task). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| uploader : list[int], optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Filter by uploader IDs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| benchmark_suite : int, optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Filter by benchmark suite ID. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| str | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The API call string with all filters applied. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| api_call = "study/list" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if limit is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| api_call += f"/limit/{limit}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if offset is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| api_call += f"/offset/{offset}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if status is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| api_call += f"/status/{status}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if main_entity_type is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| api_call += f"/main_entity_type/{main_entity_type}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if uploader is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| api_call += f"/uploader/{','.join(str(u) for u in uploader)}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if benchmark_suite is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| api_call += f"/benchmark_suite/{benchmark_suite}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return api_call | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @staticmethod | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _parse_list_xml(xml_string: str) -> pd.DataFrame: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Parse the XML response from study list API. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ---------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| xml_string : str | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The XML response from the API. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pd.DataFrame | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DataFrame containing study information. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| study_dict = xmltodict.parse(xml_string, force_list=("oml:study",)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Minimalistic check if the XML is useful | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert isinstance(study_dict["oml:study_list"]["oml:study"], list), type( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| study_dict["oml:study_list"], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert study_dict["oml:study_list"]["@xmlns:oml"] == "http://openml.org/openml", study_dict[ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "oml:study_list" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]["@xmlns:oml"] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| studies = {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for study_ in study_dict["oml:study_list"]["oml:study"]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+120
to
+128
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert isinstance(study_dict["oml:study_list"]["oml:study"], list), type( | |
| study_dict["oml:study_list"], | |
| ) | |
| assert study_dict["oml:study_list"]["@xmlns:oml"] == "http://openml.org/openml", study_dict[ | |
| "oml:study_list" | |
| ]["@xmlns:oml"] | |
| studies = {} | |
| for study_ in study_dict["oml:study_list"]["oml:study"]: | |
| study_list = study_dict.get("oml:study_list") | |
| if not isinstance(study_list, dict): | |
| raise ValueError( | |
| "Unexpected XML format: 'oml:study_list' element is missing or not a mapping.", | |
| ) | |
| studies_xml = study_list.get("oml:study") | |
| if not isinstance(studies_xml, list): | |
| raise ValueError( | |
| "Unexpected XML format: expected 'oml:study_list[\"oml:study\"]' to be a list, " | |
| f"got {type(studies_xml).__name__!r}.", | |
| ) | |
| expected_namespace = "http://openml.org/openml" | |
| namespace = study_list.get("@xmlns:oml") | |
| if namespace != expected_namespace: | |
| raise ValueError( | |
| "Unexpected XML namespace for 'oml:study_list': " | |
| f"{namespace!r} (expected {expected_namespace!r}).", | |
| ) | |
| studies = {} | |
| for study_ in studies_xml: |
Uh oh!
There was an error while loading. Please reload this page.