From 8d591dd8498700adfd03062c3c1ccb349b1c0808 Mon Sep 17 00:00:00 2001 From: neeratyoy Date: Wed, 3 Apr 2019 16:14:50 +0200 Subject: [PATCH 1/8] Added comments in examples for dataset 68 belonging to only test server --- examples/datasets_tutorial.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/datasets_tutorial.py b/examples/datasets_tutorial.py index 4d5b7ad84f..9b4f8be369 100644 --- a/examples/datasets_tutorial.py +++ b/examples/datasets_tutorial.py @@ -45,6 +45,7 @@ # This is done based on the dataset ID ('did'). dataset = openml.datasets.get_dataset(68) +# NOTE: Dataset 68 exists on the test server https://test.openml.org/d/68 # Print a summary print("This is dataset '%s', the target feature is '%s'" % @@ -84,7 +85,7 @@ # Whenever you use any functionality that requires the data, # such as `get_data`, the data will be downloaded. dataset = openml.datasets.get_dataset(68, download_data=False) - +# NOTE: Dataset 68 exists on the test server https://test.openml.org/d/68 ############################################################################ # Exercise 2 From 6f3361294225c64a144c322b70ae1c860598ab48 Mon Sep 17 00:00:00 2001 From: neeratyoy Date: Wed, 3 Apr 2019 16:26:47 +0200 Subject: [PATCH 2/8] Added comment in flow and run example for dataset 68 belonging to only test server --- examples/flows_and_runs_tutorial.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/flows_and_runs_tutorial.py b/examples/flows_and_runs_tutorial.py index 648af813fa..57bed36d91 100644 --- a/examples/flows_and_runs_tutorial.py +++ b/examples/flows_and_runs_tutorial.py @@ -15,6 +15,7 @@ # # Train a scikit-learn model on the data manually. +# NOTE: Dataset 68 exists on the test server https://test.openml.org/d/68 dataset = openml.datasets.get_dataset(68) X, y = dataset.get_data( dataset_format='array', From f1a77f5ade760aee214dd9423df778f886c4504d Mon Sep 17 00:00:00 2001 From: neeratyoy Date: Wed, 3 Apr 2019 20:11:00 +0200 Subject: [PATCH 3/8] Making download of datasplits optional and adding a relevant unit test --- openml/tasks/functions.py | 40 +++++++++++++++++-------- tests/test_tasks/test_task_functions.py | 18 +++++++++++ 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/openml/tasks/functions.py b/openml/tasks/functions.py index 3c6dc1ff67..ce1879db08 100644 --- a/openml/tasks/functions.py +++ b/openml/tasks/functions.py @@ -275,29 +275,37 @@ def __list_tasks(api_call): return tasks -def get_tasks(task_ids): - """Download tasks. +def get_tasks(task_ids, download_data=True): + """Download task representation and data splits optionally. This function iterates :meth:`openml.tasks.get_task`. Parameters ---------- task_ids : iterable - Integers representing task ids. + Integers/Strings representing task ids. + download_data : bool + Option to trigger download of data along with the meta data. Returns ------- list """ tasks = [] for task_id in task_ids: - tasks.append(get_task(task_id)) + tasks.append(get_task(task_id, download_data)) return tasks -def get_task(task_id): - """Download the OpenML task for a given task ID. +def get_task(task_id, download_data=True): + """Download the OpenML task representation for a given task ID, optionally + also download actual data along with data splits if applicable. Parameters ---------- - task_id : int + task_id : int or str The OpenML task id. + download_data : bool + Option to trigger download of data along with the meta data. + Returns + ------- + task """ task_id = int(task_id) @@ -311,14 +319,20 @@ def get_task(task_id): try: task = _get_task_description(task_id) - dataset = get_dataset(task.dataset_id) + dataset = get_dataset(task.dataset_id, download_data) + # List of class labels availaible in dataset description + # Including class labels as part of task meta data handles + # the case where data download was initially disabled + # The other alternative would be to abstract away this check + # as part of download_split() + if isinstance(task, OpenMLClassificationTask): + task.class_labels = \ + dataset.retrieve_class_labels(task.target_name) # Clustering tasks do not have class labels # and do not offer download_split - if isinstance(task, OpenMLSupervisedTask): - task.download_split() - if isinstance(task, OpenMLClassificationTask): - task.class_labels = \ - dataset.retrieve_class_labels(task.target_name) + if download_data: + if isinstance(task, OpenMLSupervisedTask): + task.download_split() except Exception as e: openml.utils._remove_cache_dir_for_id( TASKS_CACHE_DIR_NAME, diff --git a/tests/test_tasks/test_task_functions.py b/tests/test_tasks/test_task_functions.py index 02b505fc6c..6fd8723392 100644 --- a/tests/test_tasks/test_task_functions.py +++ b/tests/test_tasks/test_task_functions.py @@ -129,6 +129,24 @@ def test_get_task(self): self.workdir, 'org', 'openml', 'test', "datasets", "1", "dataset.arff" ))) + def test_get_task_lazy(self): + task = openml.tasks.get_task(2, download_data=False) + self.assertIsInstance(task, OpenMLTask) + self.assertTrue(os.path.exists(os.path.join( + self.workdir, 'org', 'openml', 'test', "tasks", "2", "task.xml", + ))) + + self.assertFalse(os.path.exists(os.path.join( + self.workdir, 'org', 'openml', 'test', "tasks", "2", "datasplits.arff" + ))) + # Not checking datasets/ as other test cases might interfere + + task.download_split() + self.assertTrue(os.path.exists(os.path.join( + self.workdir, 'org', 'openml', 'test', "tasks", "2", "datasplits.arff" + ))) + # Not checking datasets/ as other test cases might interfere + @mock.patch('openml.tasks.functions.get_dataset') def test_removal_upon_download_failure(self, get_dataset): class WeirdException(Exception): From d8d4773a5669f011f5e270bc50217bc3dbd52ab5 Mon Sep 17 00:00:00 2001 From: neeratyoy Date: Thu, 4 Apr 2019 12:16:57 +0200 Subject: [PATCH 4/8] Adding error handling for task ID type --- openml/tasks/functions.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/openml/tasks/functions.py b/openml/tasks/functions.py index ce1879db08..82db5b82c3 100644 --- a/openml/tasks/functions.py +++ b/openml/tasks/functions.py @@ -307,7 +307,11 @@ def get_task(task_id, download_data=True): ------- task """ - task_id = int(task_id) + try: + task_id = int(task_id) + except (ValueError, TypeError): + raise ValueError("Dataset ID is neither an Integer nor can be " + "cast to an Integer.") with lockutils.external_lock( name='task.functions.get_task:%d' % task_id, @@ -323,8 +327,6 @@ def get_task(task_id, download_data=True): # List of class labels availaible in dataset description # Including class labels as part of task meta data handles # the case where data download was initially disabled - # The other alternative would be to abstract away this check - # as part of download_split() if isinstance(task, OpenMLClassificationTask): task.class_labels = \ dataset.retrieve_class_labels(task.target_name) From ea6fe99ef76b359e898aa0ad0081f8f7efa3e7d0 Mon Sep 17 00:00:00 2001 From: neeratyoy Date: Thu, 4 Apr 2019 15:05:11 +0200 Subject: [PATCH 5/8] Changes suggested by Matthias on PR #659 --- openml/tasks/functions.py | 6 ++++-- tests/test_tasks/test_task_functions.py | 9 +++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/openml/tasks/functions.py b/openml/tasks/functions.py index 82db5b82c3..9b2e1c3ba1 100644 --- a/openml/tasks/functions.py +++ b/openml/tasks/functions.py @@ -295,10 +295,12 @@ def get_tasks(task_ids, download_data=True): def get_task(task_id, download_data=True): - """Download the OpenML task representation for a given task ID, optionally - also download actual data along with data splits if applicable. + """Download task representation for a given task ID Parameters ---------- + The task representation is downloaded while the download of data + + splits is optionally controlled by the download_data parameter task_id : int or str The OpenML task id. download_data : bool diff --git a/tests/test_tasks/test_task_functions.py b/tests/test_tasks/test_task_functions.py index 6fd8723392..e7a8721d8e 100644 --- a/tests/test_tasks/test_task_functions.py +++ b/tests/test_tasks/test_task_functions.py @@ -135,17 +135,22 @@ def test_get_task_lazy(self): self.assertTrue(os.path.exists(os.path.join( self.workdir, 'org', 'openml', 'test', "tasks", "2", "task.xml", ))) + self.assertEqual(task.class_labels, ['1', '2', '3', '4', '5', 'U']) self.assertFalse(os.path.exists(os.path.join( self.workdir, 'org', 'openml', 'test', "tasks", "2", "datasplits.arff" ))) - # Not checking datasets/ as other test cases might interfere + self.assertFalse(os.path.exists(os.path.join( + self.workdir, 'org', 'openml', 'test', "datasets", "2", "dataset.arff" + ))) task.download_split() self.assertTrue(os.path.exists(os.path.join( self.workdir, 'org', 'openml', 'test', "tasks", "2", "datasplits.arff" ))) - # Not checking datasets/ as other test cases might interfere + self.assertTrue(os.path.exists(os.path.join( + self.workdir, 'org', 'openml', 'test', "datasets", "2", "dataset.arff" + ))) @mock.patch('openml.tasks.functions.get_dataset') def test_removal_upon_download_failure(self, get_dataset): From 90057fb732fa3e86ebda2d28d7e73de65ed85f12 Mon Sep 17 00:00:00 2001 From: neeratyoy Date: Thu, 4 Apr 2019 16:17:02 +0200 Subject: [PATCH 6/8] Removing inappropriate dataset check from test case --- tests/test_tasks/test_task_functions.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_tasks/test_task_functions.py b/tests/test_tasks/test_task_functions.py index e7a8721d8e..8bbf84f110 100644 --- a/tests/test_tasks/test_task_functions.py +++ b/tests/test_tasks/test_task_functions.py @@ -140,6 +140,7 @@ def test_get_task_lazy(self): self.assertFalse(os.path.exists(os.path.join( self.workdir, 'org', 'openml', 'test', "tasks", "2", "datasplits.arff" ))) + # Since the download_data=False is propagated to get_dataset self.assertFalse(os.path.exists(os.path.join( self.workdir, 'org', 'openml', 'test', "datasets", "2", "dataset.arff" ))) @@ -148,9 +149,6 @@ def test_get_task_lazy(self): self.assertTrue(os.path.exists(os.path.join( self.workdir, 'org', 'openml', 'test', "tasks", "2", "datasplits.arff" ))) - self.assertTrue(os.path.exists(os.path.join( - self.workdir, 'org', 'openml', 'test', "datasets", "2", "dataset.arff" - ))) @mock.patch('openml.tasks.functions.get_dataset') def test_removal_upon_download_failure(self, get_dataset): From 9d7ec52d65fb51c764f1b01141556009a93f5a18 Mon Sep 17 00:00:00 2001 From: neeratyoy Date: Fri, 5 Apr 2019 12:42:14 +0200 Subject: [PATCH 7/8] Fixing docstring --- openml/tasks/functions.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/openml/tasks/functions.py b/openml/tasks/functions.py index 9b2e1c3ba1..052b3c1724 100644 --- a/openml/tasks/functions.py +++ b/openml/tasks/functions.py @@ -276,14 +276,17 @@ def __list_tasks(api_call): def get_tasks(task_ids, download_data=True): - """Download task representation and data splits optionally. + """Download tasks. + This function iterates :meth:`openml.tasks.get_task`. + Parameters ---------- task_ids : iterable Integers/Strings representing task ids. download_data : bool Option to trigger download of data along with the meta data. + Returns ------- list @@ -295,16 +298,19 @@ def get_tasks(task_ids, download_data=True): def get_task(task_id, download_data=True): - """Download task representation for a given task ID + """Download OpenML task for a given task ID. + + Downloads the task representation, while the data splits can be + downloaded optionally based on the additional parameter. Else, + splits will either way be downloaded when the task is being used. + Parameters ---------- - The task representation is downloaded while the download of data - - splits is optionally controlled by the download_data parameter task_id : int or str The OpenML task id. download_data : bool Option to trigger download of data along with the meta data. + Returns ------- task From 76630ff93cff98406697c77e82170caf84f47ac3 Mon Sep 17 00:00:00 2001 From: neeratyoy Date: Fri, 5 Apr 2019 13:57:29 +0200 Subject: [PATCH 8/8] Fixing whitespace issue for PEP8 --- openml/tasks/functions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openml/tasks/functions.py b/openml/tasks/functions.py index 052b3c1724..25a9f15f51 100644 --- a/openml/tasks/functions.py +++ b/openml/tasks/functions.py @@ -286,7 +286,7 @@ def get_tasks(task_ids, download_data=True): Integers/Strings representing task ids. download_data : bool Option to trigger download of data along with the meta data. - + Returns ------- list