From 59fb25c65a97f21f1619fe7efb352c6ba99c81f4 Mon Sep 17 00:00:00 2001 From: sahithyaravi1493 Date: Sun, 14 Feb 2021 16:01:14 +0100 Subject: [PATCH 1/2] initial commit --- openml/datasets/__init__.py | 4 ++ openml/datasets/functions.py | 41 +++++++++++++++++++ tests/test_datasets/test_dataset_functions.py | 20 ++++++++- 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/openml/datasets/__init__.py b/openml/datasets/__init__.py index abde85c06..02940faae 100644 --- a/openml/datasets/__init__.py +++ b/openml/datasets/__init__.py @@ -11,6 +11,8 @@ list_qualities, edit_dataset, fork_dataset, + topic_add_dataset, + topic_delete_dataset, ) from .dataset import OpenMLDataset from .data_feature import OpenMLDataFeature @@ -28,4 +30,6 @@ "list_qualities", "edit_dataset", "fork_dataset", + "topic_add_dataset", + "topic_delete_dataset", ] diff --git a/openml/datasets/functions.py b/openml/datasets/functions.py index 3f930c2ea..2664d5235 100644 --- a/openml/datasets/functions.py +++ b/openml/datasets/functions.py @@ -869,6 +869,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. diff --git a/tests/test_datasets/test_dataset_functions.py b/tests/test_datasets/test_dataset_functions.py index 141535def..d1a45645c 100644 --- a/tests/test_datasets/test_dataset_functions.py +++ b/tests/test_datasets/test_dataset_functions.py @@ -35,7 +35,7 @@ _get_online_dataset_format, DATASETS_CACHE_DIR_NAME, ) -from openml.datasets import fork_dataset, edit_dataset +from openml.datasets import fork_dataset, edit_dataset, topic_add_dataset, topic_delete_dataset from openml.tasks import TaskType, create_task @@ -820,6 +820,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 From f14a8e9bd96cd4af6127dee5caa7c3f5f76e21b3 Mon Sep 17 00:00:00 2001 From: sahithyaravi1493 Date: Mon, 8 Mar 2021 09:37:34 +0100 Subject: [PATCH 2/2] private functions --- openml/datasets/__init__.py | 4 ---- openml/datasets/functions.py | 4 ++-- tests/test_datasets/test_dataset_functions.py | 8 +++++--- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/openml/datasets/__init__.py b/openml/datasets/__init__.py index 02940faae..abde85c06 100644 --- a/openml/datasets/__init__.py +++ b/openml/datasets/__init__.py @@ -11,8 +11,6 @@ list_qualities, edit_dataset, fork_dataset, - topic_add_dataset, - topic_delete_dataset, ) from .dataset import OpenMLDataset from .data_feature import OpenMLDataFeature @@ -30,6 +28,4 @@ "list_qualities", "edit_dataset", "fork_dataset", - "topic_add_dataset", - "topic_delete_dataset", ] diff --git a/openml/datasets/functions.py b/openml/datasets/functions.py index 5439d1c1f..746285650 100644 --- a/openml/datasets/functions.py +++ b/openml/datasets/functions.py @@ -873,7 +873,7 @@ def fork_dataset(data_id: int) -> int: return int(data_id) -def topic_add_dataset(data_id: int, topic: str): +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. @@ -893,7 +893,7 @@ def topic_add_dataset(data_id: int, topic: str): return int(data_id) -def topic_delete_dataset(data_id: int, topic: str): +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. diff --git a/tests/test_datasets/test_dataset_functions.py b/tests/test_datasets/test_dataset_functions.py index 4190bfc3e..ec9dd6c53 100644 --- a/tests/test_datasets/test_dataset_functions.py +++ b/tests/test_datasets/test_dataset_functions.py @@ -37,8 +37,10 @@ _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, topic_add_dataset, topic_delete_dataset +from openml.datasets import fork_dataset, edit_dataset from openml.tasks import TaskType, create_task @@ -916,7 +918,7 @@ def test_topic_api_error(self): self.assertRaisesRegex( OpenMLServerException, "Topic can only be added/removed by admin.", - topic_add_dataset, + _topic_add_dataset, data_id=31, topic="business", ) @@ -924,7 +926,7 @@ def test_topic_api_error(self): self.assertRaisesRegex( OpenMLServerException, "Topic can only be added/removed by admin.", - topic_delete_dataset, + _topic_delete_dataset, data_id=31, topic="business", )