|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import warnings |
| 4 | + |
| 5 | +import xmltodict |
| 6 | + |
| 7 | +from openml.estimation_procedures.estimation_procedure import OpenMLEstimationProcedure |
| 8 | +from openml.tasks.task import TaskType |
| 9 | + |
3 | 10 | from .base import EstimationProcedureAPI, ResourceV1API, ResourceV2API |
4 | 11 |
|
5 | 12 |
|
6 | 13 | class EstimationProcedureV1API(ResourceV1API, EstimationProcedureAPI): |
7 | | - """Version 1 API implementation for estimation procedure resources.""" |
| 14 | + """V1 API implementation for estimation procedures. |
| 15 | +
|
| 16 | + Fetches estimation procedures from the v1 XML API endpoint. |
| 17 | + """ |
| 18 | + |
| 19 | + def list(self) -> list[OpenMLEstimationProcedure]: |
| 20 | + """Return a list of all estimation procedures which are on OpenML. |
| 21 | +
|
| 22 | + Returns |
| 23 | + ------- |
| 24 | + procedures : list |
| 25 | + A list of all estimation procedures. Every procedure is represented by |
| 26 | + a dictionary containing the following information: id, task type id, |
| 27 | + name, type, repeats, folds, stratified. |
| 28 | + """ |
| 29 | + path = "estimationprocedure/list" |
| 30 | + response = self._http.get(path) |
| 31 | + xml_content = response.text |
| 32 | + |
| 33 | + procs_dict = xmltodict.parse(xml_content) |
| 34 | + |
| 35 | + # Minimalistic check if the XML is useful |
| 36 | + if "oml:estimationprocedures" not in procs_dict: |
| 37 | + raise ValueError("Error in return XML, does not contain tag oml:estimationprocedures.") |
| 38 | + |
| 39 | + if "@xmlns:oml" not in procs_dict["oml:estimationprocedures"]: |
| 40 | + raise ValueError( |
| 41 | + "Error in return XML, does not contain tag " |
| 42 | + "@xmlns:oml as a child of oml:estimationprocedures.", |
| 43 | + ) |
| 44 | + |
| 45 | + if procs_dict["oml:estimationprocedures"]["@xmlns:oml"] != "http://openml.org/openml": |
| 46 | + raise ValueError( |
| 47 | + "Error in return XML, value of " |
| 48 | + "oml:estimationprocedures/@xmlns:oml is not " |
| 49 | + "http://openml.org/openml, but {}".format( |
| 50 | + str(procs_dict["oml:estimationprocedures"]["@xmlns:oml"]) |
| 51 | + ), |
| 52 | + ) |
| 53 | + |
| 54 | + procs: list[OpenMLEstimationProcedure] = [] |
| 55 | + for proc_ in procs_dict["oml:estimationprocedures"]["oml:estimationprocedure"]: |
| 56 | + task_type_int = int(proc_["oml:ttid"]) |
| 57 | + try: |
| 58 | + task_type_id = TaskType(task_type_int) |
| 59 | + procs.append( |
| 60 | + OpenMLEstimationProcedure( |
| 61 | + id=int(proc_["oml:id"]), |
| 62 | + task_type_id=task_type_id, |
| 63 | + name=proc_["oml:name"], |
| 64 | + type=proc_["oml:type"], |
| 65 | + ) |
| 66 | + ) |
| 67 | + except ValueError as e: |
| 68 | + warnings.warn( |
| 69 | + f"Could not create task type id for {task_type_int} due to error {e}", |
| 70 | + RuntimeWarning, |
| 71 | + stacklevel=2, |
| 72 | + ) |
| 73 | + |
| 74 | + return procs |
8 | 75 |
|
9 | 76 |
|
10 | 77 | class EstimationProcedureV2API(ResourceV2API, EstimationProcedureAPI): |
11 | | - """Version 2 API implementation for estimation procedure resources.""" |
| 78 | + """V2 API implementation for estimation procedures. |
| 79 | +
|
| 80 | + Fetches estimation procedures from the v2 JSON API endpoint. |
| 81 | + """ |
| 82 | + |
| 83 | + def list(self) -> list[OpenMLEstimationProcedure]: |
| 84 | + self._not_supported(method="list") |
0 commit comments