Skip to content

Commit f6e6516

Browse files
committed
remove old api calls from OpenMLRun classes
1 parent 229592f commit f6e6516

3 files changed

Lines changed: 58 additions & 9 deletions

File tree

openml/_api/resources/base/resources.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,21 @@ def list( # type: ignore[valid-type] # noqa: PLR0913
114114
task_type: TaskType | int | None = None,
115115
) -> pd.DataFrame: ...
116116

117+
@abstractmethod
118+
def download_text_file(
119+
self,
120+
source: str,
121+
*,
122+
md5_checksum: str | None = None,
123+
) -> str: ...
124+
125+
@abstractmethod
126+
def file_id_to_url(
127+
self,
128+
file_id: int,
129+
filename: str | None = None,
130+
) -> str: ...
131+
117132

118133
class SetupAPI(ResourceAPI):
119134
"""Abstract API interface for setup resources."""

openml/_api/resources/run.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,30 @@ def _parse_list_xml(self, xml_string: str) -> pd.DataFrame:
194194
}
195195
return pd.DataFrame.from_dict(runs, orient="index")
196196

197+
def download_text_file(
198+
self,
199+
source: str,
200+
*,
201+
md5_checksum: str | None = None,
202+
) -> str:
203+
response = self._http.get(
204+
source,
205+
use_api_key=False,
206+
md5_checksum=md5_checksum,
207+
)
208+
return response.text
209+
210+
def file_id_to_url(
211+
self,
212+
file_id: int,
213+
filename: str | None = None,
214+
) -> str:
215+
server_base = self._http.server.split("/api/", 1)[0].rstrip("/")
216+
url = f"{server_base}/data/download/{file_id}"
217+
if filename is not None:
218+
url += f"/{filename}"
219+
return url
220+
197221

198222
class RunV2API(ResourceV2API, RunAPI):
199223
"""V2 API resource for runs. Currently read-only until V2 server supports POST."""
@@ -248,3 +272,18 @@ def list( # type: ignore[valid-type] # noqa: PLR0913
248272
V2 server API not yet available for this operation.
249273
"""
250274
self._not_supported(method="list")
275+
276+
def download_text_file(
277+
self,
278+
source: str, # noqa: ARG002
279+
*,
280+
md5_checksum: str | None = None, # noqa: ARG002
281+
) -> str:
282+
self._not_supported(method="download_text_file")
283+
284+
def file_id_to_url(
285+
self,
286+
file_id: int, # noqa: ARG002
287+
filename: str | None = None, # noqa: ARG002
288+
) -> str:
289+
self._not_supported(method="file_id_to_url")

openml/runs/run.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import pandas as pd
1717

1818
import openml
19-
import openml._api_calls
2019
from openml.base import OpenMLBase
2120
from openml.exceptions import PyOpenMLError
2221
from openml.flows import OpenMLFlow, get_flow
@@ -158,9 +157,7 @@ def predictions(self) -> pd.DataFrame:
158157
if self.data_content:
159158
arff_dict = self._generate_arff_dict()
160159
elif self.predictions_url:
161-
arff_text = openml._api_calls._download_text_file(self.predictions_url)
162-
if arff_text is None:
163-
raise RuntimeError("Could not download predictions ARFF content.")
160+
arff_text = openml._backend.run.download_text_file(self.predictions_url)
164161
arff_dict = arff.loads(arff_text)
165162
else:
166163
raise RuntimeError("Run has no predictions.")
@@ -532,14 +529,12 @@ def get_metric_fn(self, sklearn_fn: Callable, kwargs: dict | None = None) -> np.
532529
if self.data_content is not None and self.task_id is not None:
533530
predictions_arff = self._generate_arff_dict()
534531
elif (self.output_files is not None) and ("predictions" in self.output_files):
535-
predictions_file_url = openml._api_calls._file_id_to_url(
532+
predictions_file_url = openml._backend.run.file_id_to_url(
536533
self.output_files["predictions"],
537534
"predictions.arff",
538535
)
539-
response = openml._api_calls._download_text_file(predictions_file_url)
540-
if response is None:
541-
raise ValueError("Could not download predictions ARFF content.")
542-
predictions_arff = arff.loads(response)
536+
predictions_text = openml._backend.run.download_text_file(predictions_file_url)
537+
predictions_arff = arff.loads(predictions_text)
543538
# TODO: make this a stream reader
544539
else:
545540
raise ValueError(

0 commit comments

Comments
 (0)