Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions openml/datasets/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,47 @@ def fork_dataset(data_id: int) -> int:
return int(data_id)


def _topic_add_dataset(data_id: int, topic: str):
"""
Adds a topic for a dataset.
This API is not available for all OpenML users and is accessible only by admins.
Parameters
----------
data_id : int
id of the dataset for which the topic needs to be added
topic : str
Topic to be added for the dataset
"""
if not isinstance(data_id, int):
raise TypeError("`data_id` must be of type `int`, not {}.".format(type(data_id)))
form_data = {"data_id": data_id, "topic": topic}
result_xml = openml._api_calls._perform_api_call("data/topicadd", "post", data=form_data)
result = xmltodict.parse(result_xml)
data_id = result["oml:data_topic"]["oml:id"]
return int(data_id)


def _topic_delete_dataset(data_id: int, topic: str):
"""
Removes a topic from a dataset.
This API is not available for all OpenML users and is accessible only by admins.
Parameters
----------
data_id : int
id of the dataset to be forked
topic : str
Topic to be deleted

"""
if not isinstance(data_id, int):
raise TypeError("`data_id` must be of type `int`, not {}.".format(type(data_id)))
form_data = {"data_id": data_id, "topic": topic}
result_xml = openml._api_calls._perform_api_call("data/topicdelete", "post", data=form_data)
result = xmltodict.parse(result_xml)
data_id = result["oml:data_topic"]["oml:id"]
return int(data_id)


def _get_dataset_description(did_cache_dir, dataset_id):
"""Get the dataset description as xml dictionary.

Expand Down
20 changes: 20 additions & 0 deletions tests/test_datasets/test_dataset_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
_get_online_dataset_format,
DATASETS_CACHE_DIR_NAME,
_get_dataset_parquet,
_topic_add_dataset,
_topic_delete_dataset,
)
from openml.datasets import fork_dataset, edit_dataset
from openml.tasks import TaskType, create_task
Expand Down Expand Up @@ -911,6 +913,24 @@ def test_get_online_dataset_arff(self):
"ARFF files are not equal",
)

def test_topic_api_error(self):
# Check server exception when non-admin accessses apis
self.assertRaisesRegex(
OpenMLServerException,
"Topic can only be added/removed by admin.",
_topic_add_dataset,
data_id=31,
topic="business",
)
# Check server exception when non-admin accessses apis
self.assertRaisesRegex(
OpenMLServerException,
"Topic can only be added/removed by admin.",
_topic_delete_dataset,
data_id=31,
topic="business",
)

def test_get_online_dataset_format(self):

# Phoneme dataset
Expand Down