Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
updates functions
  • Loading branch information
janvanrijn authored and eddiebergman committed Jan 12, 2024
commit 95a51ff5a45b8beae8cd670bde808fbf33b0c0d5
61 changes: 52 additions & 9 deletions openml/datasets/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1060,25 +1060,68 @@ def fork_dataset(data_id: int) -> int:
data_id = result["oml:data_fork"]["oml:id"]
return int(data_id)

def data_feature_add_ontology(did, index, ontology):
def data_feature_add_ontology(data_id, index, ontology):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requires type hints and return type

"""
Adds an ontology (URL) to a given dataset feature (defined by a dataset id
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a short description of the concept of ontology in the docstring.

and index). The dataset has to exists on OpenML and needs to have been
processed by the evaluation engine.

Parameters
----------
data_id : int
id of the dataset to which the feature belongs

index : int
index of the feature in dataset (0-based)
Comment thread
LennartPurucker marked this conversation as resolved.

ontology : str
URL to ontology (max. 256 characters)

Returns
-------
True or throws an OpenML server exception
"""
upload_data = {
'data_id': did,
'data_id': data_id,
'index': index,
'ontology': ontology
}
print(upload_data)
result_xml = openml._api_calls._perform_api_call("data/feature/ontology/add", "post", data=upload_data)
print(result_xml)
openml._api_calls._perform_api_call("data/feature/ontology/add", "post", data=upload_data)
# an error will be thrown in case the request was unsuccessful
return True


def data_feature_remove_ontology(data_id, index, ontology):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add typing

"""
Removes an existing ontology (URL) to a given dataset feature (defined
by a dataset id and index). The dataset has to exists on OpenML and needs
to have been processed by the evaluation engine. Ontology needs to be
attached to the specific fearure.

Parameters
----------
data_id : int
id of the dataset to which the feature belongs

index : int
index of the feature in dataset (0-based)

ontology : str
URL to ontology (max. 256 characters)

Returns
-------
True or throws an OpenML server exception
"""

def data_feature_remove_ontology(did, index, ontology):
upload_data = {
'data_id': did,
'data_id': data_id,
'index': index,
'ontology': ontology
}
result_xml = openml._api_calls._perform_api_call("data/feature/ontology/remove", "post", data=upload_data)
print(result_xml)
openml._api_calls._perform_api_call("data/feature/ontology/remove", "post", data=upload_data)
# an error will be thrown in case the request was unsuccessful
return True


def _topic_add_dataset(data_id: int, topic: str) -> int:
Expand Down
29 changes: 27 additions & 2 deletions tests/test_datasets/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,38 @@ def test_get_feature_with_ontology_data_id_11(self):
assert len(dataset.features[2].ontologies) >= 1
assert len(dataset.features[3].ontologies) >= 1

def test_add_ontology_to_dataset(self):
def test_add_remove_ontology_to_dataset(self):
did = 1
feature_index = 1
ontology = 'https://www.google.com/test9/'
ontology = 'https://www.openml.org/unittest/' + str(time())
openml.datasets.functions.data_feature_add_ontology(did, feature_index, ontology)
openml.datasets.functions.data_feature_remove_ontology(did, feature_index, ontology)

def test_add_same_ontology_multiple_features(self):
did = 1
ontology = 'https://www.openml.org/unittest/' + str(time())

for i in range(3):
openml.datasets.functions.data_feature_add_ontology(did, i, ontology)


def test_add_illegal_long_ontology(self):
did = 1
ontology = 'http://www.google.com/' + ('a' * 257)
try:
openml.datasets.functions.data_feature_add_ontology(did, 1, ontology)
assert False
except openml.exceptions.OpenMLServerException as e:
assert e.code == 1105

def test_add_illegal_url_ontology(self):
did = 1
ontology = 'not_a_url' + str(time())
try:
openml.datasets.functions.data_feature_add_ontology(did, 1, ontology)
assert False
except openml.exceptions.OpenMLServerException as e:
assert e.code == 1106

@pytest.mark.production()
class OpenMLDatasetTestSparse(TestBase):
Expand Down