Skip to content

Commit 6c609b8

Browse files
authored
API for topics (#1023)
* initial commit * private functions
1 parent 38f9bf0 commit 6c609b8

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

openml/datasets/functions.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,47 @@ def fork_dataset(data_id: int) -> int:
873873
return int(data_id)
874874

875875

876+
def _topic_add_dataset(data_id: int, topic: str):
877+
"""
878+
Adds a topic for a dataset.
879+
This API is not available for all OpenML users and is accessible only by admins.
880+
Parameters
881+
----------
882+
data_id : int
883+
id of the dataset for which the topic needs to be added
884+
topic : str
885+
Topic to be added for the dataset
886+
"""
887+
if not isinstance(data_id, int):
888+
raise TypeError("`data_id` must be of type `int`, not {}.".format(type(data_id)))
889+
form_data = {"data_id": data_id, "topic": topic}
890+
result_xml = openml._api_calls._perform_api_call("data/topicadd", "post", data=form_data)
891+
result = xmltodict.parse(result_xml)
892+
data_id = result["oml:data_topic"]["oml:id"]
893+
return int(data_id)
894+
895+
896+
def _topic_delete_dataset(data_id: int, topic: str):
897+
"""
898+
Removes a topic from a dataset.
899+
This API is not available for all OpenML users and is accessible only by admins.
900+
Parameters
901+
----------
902+
data_id : int
903+
id of the dataset to be forked
904+
topic : str
905+
Topic to be deleted
906+
907+
"""
908+
if not isinstance(data_id, int):
909+
raise TypeError("`data_id` must be of type `int`, not {}.".format(type(data_id)))
910+
form_data = {"data_id": data_id, "topic": topic}
911+
result_xml = openml._api_calls._perform_api_call("data/topicdelete", "post", data=form_data)
912+
result = xmltodict.parse(result_xml)
913+
data_id = result["oml:data_topic"]["oml:id"]
914+
return int(data_id)
915+
916+
876917
def _get_dataset_description(did_cache_dir, dataset_id):
877918
"""Get the dataset description as xml dictionary.
878919

tests/test_datasets/test_dataset_functions.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
_get_online_dataset_format,
3838
DATASETS_CACHE_DIR_NAME,
3939
_get_dataset_parquet,
40+
_topic_add_dataset,
41+
_topic_delete_dataset,
4042
)
4143
from openml.datasets import fork_dataset, edit_dataset
4244
from openml.tasks import TaskType, create_task
@@ -911,6 +913,24 @@ def test_get_online_dataset_arff(self):
911913
"ARFF files are not equal",
912914
)
913915

916+
def test_topic_api_error(self):
917+
# Check server exception when non-admin accessses apis
918+
self.assertRaisesRegex(
919+
OpenMLServerException,
920+
"Topic can only be added/removed by admin.",
921+
_topic_add_dataset,
922+
data_id=31,
923+
topic="business",
924+
)
925+
# Check server exception when non-admin accessses apis
926+
self.assertRaisesRegex(
927+
OpenMLServerException,
928+
"Topic can only be added/removed by admin.",
929+
_topic_delete_dataset,
930+
data_id=31,
931+
topic="business",
932+
)
933+
914934
def test_get_online_dataset_format(self):
915935

916936
# Phoneme dataset

0 commit comments

Comments
 (0)