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
3 changes: 2 additions & 1 deletion examples/datasets_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'" %
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions examples/flows_and_runs_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
46 changes: 33 additions & 13 deletions openml/tasks/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,35 +277,51 @@ def __list_tasks(api_call):
return tasks


def get_tasks(task_ids):
def get_tasks(task_ids, download_data=True):
"""Download tasks.

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 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
----------
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)
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,
Expand All @@ -317,14 +333,18 @@ 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
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,
Expand Down
21 changes: 21 additions & 0 deletions tests/test_tasks/test_task_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,27 @@ 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)
Comment thread
mfeurer marked this conversation as resolved.
Resolved
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"
)))
# 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"
)))

task.download_split()
self.assertTrue(os.path.exists(os.path.join(
self.workdir, 'org', 'openml', 'test', "tasks", "2", "datasplits.arff"
)))

@mock.patch('openml.tasks.functions.get_dataset')
def test_removal_upon_download_failure(self, get_dataset):
class WeirdException(Exception):
Expand Down