diff --git a/.travis.yml b/.travis.yml index ed2c4e235d..f0cecf80da 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,13 +15,15 @@ env: - TEST_DIR=/tmp/test_dir/ - MODULE=openml matrix: - - DISTRIB="conda" PYTHON_VERSION="2.7" SKLEARN_VERSION="0.18.2" - - DISTRIB="conda" PYTHON_VERSION="3.5" SKLEARN_VERSION="0.18.2" - - DISTRIB="conda" PYTHON_VERSION="3.6" COVERAGE="true" DOCPUSH="true" SKLEARN_VERSION="0.18.2" - - DISTRIB="conda" PYTHON_VERSION="3.6" EXAMPLES="true" SKLEARN_VERSION="0.18.2" - - DISTRIB="conda" PYTHON_VERSION="3.6" DOCTEST="true" SKLEARN_VERSION="0.18.2" - - DISTRIB="conda" PYTHON_VERSION="3.6" SKLEARN_VERSION="0.18.2" RUN_FLAKE8="true" SKIP_TESTS="true" - - DISTRIB="conda" PYTHON_VERSION="3.7" SKLEARN_VERSION="0.19.2" + - DISTRIB="conda" PYTHON_VERSION="2.7" SKLEARN_VERSION="0.20.0" + - DISTRIB="conda" PYTHON_VERSION="3.5" SKLEARN_VERSION="0.20.0" + - DISTRIB="conda" PYTHON_VERSION="3.6" SKLEARN_VERSION="0.20.0" + - DISTRIB="conda" PYTHON_VERSION="3.7" SKLEARN_VERSION="0.20.0" RUN_FLAKE8="true" SKIP_TESTS="true" + - DISTRIB="conda" PYTHON_VERSION="3.7" SKLEARN_VERSION="0.20.0" COVERAGE="true" DOCPUSH="true" + # Checks for older scikit-learn versions (which also don't nicely work with + # Python3.7) + - DISTRIB="conda" PYTHON_VERSION="3.6" SKLEARN_VERSION="0.19.2" + - DISTRIB="conda" PYTHON_VERSION="3.6" SKLEARN_VERSION="0.18.2" install: source ci_scripts/install.sh script: bash ci_scripts/test.sh @@ -42,4 +44,4 @@ deploy: on: all_branches: true condition: $doc_result = "success" - local_dir: doc/$TRAVIS_BRANCH \ No newline at end of file + local_dir: doc/$TRAVIS_BRANCH diff --git a/doc/api.rst b/doc/api.rst index 4939cd99e8..17294f8bb8 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -30,6 +30,7 @@ Top-level Classes :template: function.rst check_datasets_active + create_dataset get_dataset get_datasets list_datasets diff --git a/openml/runs/run.py b/openml/runs/run.py index 598dbeb48b..83d12e655b 100644 --- a/openml/runs/run.py +++ b/openml/runs/run.py @@ -59,7 +59,7 @@ def __init__(self, task_id, flow_id, dataset_id, setup_string=None, def __str__(self): flow_name = self.flow_name - if len(flow_name) > 26: + if flow_name is not None and len(flow_name) > 26: # long enough to show sklearn.pipeline.Pipeline flow_name = flow_name[:26] + "..." return "[run id: {}, task id: {}, flow id: {}, flow name: {}]".format( diff --git a/openml/tasks/__init__.py b/openml/tasks/__init__.py index 3784c32a72..2cf210dec9 100644 --- a/openml/tasks/__init__.py +++ b/openml/tasks/__init__.py @@ -1,5 +1,23 @@ -from .task import OpenMLTask +from .task import ( + OpenMLTask, + OpenMLSupervisedTask, + OpenMLClassificationTask, + OpenMLRegressionTask, + OpenMLClusteringTask, + OpenMLLearningCurveTask, +) from .split import OpenMLSplit from .functions import (get_task, get_tasks, list_tasks) -__all__ = ['OpenMLTask', 'get_task', 'get_tasks', 'list_tasks', 'OpenMLSplit'] +__all__ = [ + 'OpenMLTask', + 'OpenMLSupervisedTask', + 'OpenMLClusteringTask', + 'OpenMLRegressionTask', + 'OpenMLClassificationTask', + 'OpenMLLearningCurveTask', + 'get_task', + 'get_tasks', + 'list_tasks', + 'OpenMLSplit', +] diff --git a/openml/tasks/functions.py b/openml/tasks/functions.py index 23283d3647..2c35325940 100644 --- a/openml/tasks/functions.py +++ b/openml/tasks/functions.py @@ -8,23 +8,25 @@ from ..exceptions import OpenMLCacheException from ..datasets import get_dataset -from .task import OpenMLTask +from .task import ( + OpenMLClassificationTask, + OpenMLRegressionTask, + OpenMLClusteringTask, + OpenMLLearningCurveTask, +) import openml.utils import openml._api_calls TASKS_CACHE_DIR_NAME = 'tasks' - def _get_cached_tasks(): """Return a dict of all the tasks which are cached locally. - Returns ------- tasks : OrderedDict A dict of all the cached tasks. Each task is an instance of OpenMLTask. """ - tasks = OrderedDict() task_cache_dir = openml.utils._create_cache_directory(TASKS_CACHE_DIR_NAME) @@ -43,6 +45,7 @@ def _get_cached_tasks(): return tasks + def _get_cached_task(tid): """Return a cached task based on the given id. @@ -71,7 +74,6 @@ def _get_cached_task(tid): def _get_estimation_procedure_list(): """Return a list of all estimation procedures which are on OpenML. - Returns ------- procedures : list @@ -113,17 +115,14 @@ def _get_estimation_procedure_list(): def list_tasks(task_type_id=None, offset=None, size=None, tag=None, **kwargs): """ Return a number of tasks having the given tag and task_type_id - Parameters ---------- Filter task_type_id is separated from the other filters because it is used as task_type_id in the task description, but it is named type when used as a filter in list tasks call. - task_type_id : int, optional ID of the task type as detailed `here `_. - - Supervised classification: 1 - Supervised regression: 2 - Learning curve: 3 @@ -138,11 +137,9 @@ def list_tasks(task_type_id=None, offset=None, size=None, tag=None, **kwargs): the maximum number of tasks to show tag : str, optional the tag to include - kwargs: dict, optional Legal filter operators: data_tag, status, data_id, data_name, number_instances, number_features, number_classes, number_missing_values. - Returns ------- dict @@ -157,17 +154,14 @@ def list_tasks(task_type_id=None, offset=None, size=None, tag=None, **kwargs): def _list_tasks(task_type_id=None, **kwargs): """ Perform the api call to return a number of tasks having the given filters. - Parameters ---------- Filter task_type_id is separated from the other filters because it is used as task_type_id in the task description, but it is named type when used as a filter in list tasks call. - task_type_id : int, optional ID of the task type as detailed `here `_. - - Supervised classification: 1 - Supervised regression: 2 - Learning curve: 3 @@ -176,12 +170,10 @@ def _list_tasks(task_type_id=None, **kwargs): - Machine Learning Challenge: 6 - Survival Analysis: 7 - Subgroup Discovery: 8 - kwargs: dict, optional Legal filter operators: tag, data_tag, status, limit, offset, data_id, data_name, number_instances, number_features, number_classes, number_missing_values. - Returns ------- dict @@ -265,14 +257,11 @@ def __list_tasks(api_call): def get_tasks(task_ids): """Download tasks. - This function iterates :meth:`openml.tasks.get_task`. - Parameters ---------- task_ids : iterable Integers representing task ids. - Returns ------- list @@ -285,7 +274,6 @@ def get_tasks(task_ids): def get_task(task_id): """Download the OpenML task for a given task ID. - Parameters ---------- task_id : int @@ -307,7 +295,10 @@ def get_task(task_id): task.class_labels = class_labels task.download_split() except Exception as e: - openml.utils._remove_cache_dir_for_id(TASKS_CACHE_DIR_NAME, tid_cache_dir) + openml.utils._remove_cache_dir_for_id( + TASKS_CACHE_DIR_NAME, + tid_cache_dir, + ) raise e return task @@ -319,7 +310,10 @@ def _get_task_description(task_id): return _get_cached_task(task_id) except OpenMLCacheException: xml_file = os.path.join( - openml.utils._create_cache_directory_for_id(TASKS_CACHE_DIR_NAME, task_id), + openml.utils._create_cache_directory_for_id( + TASKS_CACHE_DIR_NAME, + task_id, + ), "task.xml", ) task_xml = openml._api_calls._perform_api_call("task/%d" % task_id) @@ -328,7 +322,6 @@ def _get_task_description(task_id): fh.write(task_xml) return _create_task_from_xml(task_xml) - def _create_task_from_xml(xml): """Create a task given a xml string. @@ -354,8 +347,8 @@ def _create_task_from_xml(xml): evaluation_measures = None if 'evaluation_measures' in inputs: - evaluation_measures = inputs["evaluation_measures"]["oml:evaluation_measures"]["oml:evaluation_measure"] - + evaluation_measures = inputs["evaluation_measures"][ + "oml:evaluation_measures"]["oml:evaluation_measure"] # Convert some more parameters for parameter in \ @@ -365,12 +358,34 @@ def _create_task_from_xml(xml): text = parameter.get("#text", "") estimation_parameters[name] = text - return OpenMLTask( - dic["oml:task_id"], dic['oml:task_type_id'], dic["oml:task_type"], - inputs["source_data"]["oml:data_set"]["oml:data_set_id"], - inputs["source_data"]["oml:data_set"]["oml:target_feature"], - inputs["estimation_procedure"]["oml:estimation_procedure"][ - "oml:type"], - inputs["estimation_procedure"]["oml:estimation_procedure"][ - "oml:data_splits_url"], estimation_parameters, - evaluation_measures, None) + task_type = dic["oml:task_type"] + common_kwargs = { + 'task_id': dic["oml:task_id"], + 'task_type': task_type, + 'task_type_id': dic["oml:task_type_id"], + 'data_set_id': inputs["source_data"][ + "oml:data_set"]["oml:data_set_id"], + 'estimation_procedure_type': inputs["estimation_procedure"][ + "oml:estimation_procedure"]["oml:type"], + 'estimation_parameters': estimation_parameters, + 'evaluation_measure': evaluation_measures, + } + if task_type in ( + "Supervised Classification", + "Supervised Regression", + "Learning Curve" + ): + common_kwargs['target_name'] = inputs[ + "source_data"]["oml:data_set"]["oml:target_feature"] + common_kwargs['data_splits_url'] = inputs["estimation_procedure"][ + "oml:estimation_procedure"]["oml:data_splits_url"] + + cls = { + "Supervised Classification": OpenMLClassificationTask, + "Supervised Regression": OpenMLRegressionTask, + "Clustering": OpenMLClusteringTask, + "Learning Curve": OpenMLLearningCurveTask, + }.get(task_type) + if cls is None: + raise NotImplementedError('Task type %s not supported.') + return cls(**common_kwargs) diff --git a/openml/tasks/task.py b/openml/tasks/task.py index 26ff261618..a17f0a059a 100644 --- a/openml/tasks/task.py +++ b/openml/tasks/task.py @@ -1,7 +1,6 @@ import io import os -from .. import config from .. import datasets from .split import OpenMLSplit import openml._api_calls @@ -10,35 +9,66 @@ class OpenMLTask(object): def __init__(self, task_id, task_type_id, task_type, data_set_id, - target_name, estimation_procedure_type, data_splits_url, - estimation_parameters, evaluation_measure, cost_matrix, - class_labels=None): + estimation_procedure_type, estimation_parameters, + evaluation_measure): self.task_id = int(task_id) self.task_type_id = int(task_type_id) self.task_type = task_type self.dataset_id = int(data_set_id) - self.target_name = target_name self.estimation_procedure = dict() self.estimation_procedure["type"] = estimation_procedure_type - self.estimation_procedure["data_splits_url"] = data_splits_url self.estimation_procedure["parameters"] = estimation_parameters # self.estimation_parameters = estimation_parameters self.evaluation_measure = evaluation_measure - self.cost_matrix = cost_matrix - self.class_labels = class_labels - self.split = None - - if cost_matrix is not None: - raise NotImplementedError("Costmatrix") def get_dataset(self): """Download dataset associated with task""" return datasets.get_dataset(self.dataset_id) + def push_tag(self, tag): + """Annotates this task with a tag on the server. + + Parameters + ---------- + tag : str + Tag to attach to the task. + """ + data = {'task_id': self.task_id, 'tag': tag} + openml._api_calls._perform_api_call("/task/tag", data=data) + + def remove_tag(self, tag): + """Removes a tag from this task on the server. + + Parameters + ---------- + tag : str + Tag to attach to the task. + """ + data = {'task_id': self.task_id, 'tag': tag} + openml._api_calls._perform_api_call("/task/untag", data=data) + + +class OpenMLSupervisedTask(OpenMLTask): + def __init__(self, task_id, task_type_id, task_type, data_set_id, + estimation_procedure_type, estimation_parameters, + evaluation_measure, target_name, data_splits_url): + super(OpenMLSupervisedTask, self).__init__( + task_id=task_id, + task_type_id=task_type_id, + task_type=task_type, + data_set_id=data_set_id, + estimation_procedure_type=estimation_procedure_type, + estimation_parameters=estimation_parameters, + evaluation_measure=evaluation_measure, + ) + self.target_name = target_name + self.estimation_procedure["data_splits_url"] = data_splits_url + self.split = None + def get_X_and_y(self): """Get data associated with the current task. - + Returns ------- tuple - X and y @@ -55,7 +85,11 @@ def get_train_test_split_indices(self, fold=0, repeat=0, sample=0): if self.split is None: self.split = self.download_split() - train_indices, test_indices = self.split.get(repeat=repeat, fold=fold, sample=sample) + train_indices, test_indices = self.split.get( + repeat=repeat, + fold=fold, + sample=sample, + ) return train_indices, test_indices def _download_split(self, cache_file): @@ -93,24 +127,87 @@ def get_split_dimensions(self): return self.split.repeats, self.split.folds, self.split.samples - def push_tag(self, tag): - """Annotates this task with a tag on the server. - Parameters - ---------- - tag : str - Tag to attach to the task. - """ - data = {'task_id': self.task_id, 'tag': tag} - openml._api_calls._perform_api_call("/task/tag", data=data) +class OpenMLClassificationTask(OpenMLSupervisedTask): + def __init__(self, task_id, task_type_id, task_type, data_set_id, + estimation_procedure_type, estimation_parameters, + evaluation_measure, target_name, data_splits_url, + class_labels=None, cost_matrix=None): + super(OpenMLClassificationTask, self).__init__( + task_id=task_id, + task_type_id=task_type_id, + task_type=task_type, + data_set_id=data_set_id, + estimation_procedure_type=estimation_procedure_type, + estimation_parameters=estimation_parameters, + evaluation_measure=evaluation_measure, + target_name=target_name, + data_splits_url=data_splits_url, + ) + self.target_name = target_name + self.class_labels = class_labels + self.cost_matrix = cost_matrix + self.estimation_procedure["data_splits_url"] = data_splits_url + self.split = None - def remove_tag(self, tag): - """Removes a tag from this task on the server. + if cost_matrix is not None: + raise NotImplementedError("Costmatrix") - Parameters - ---------- - tag : str - Tag to attach to the task. - """ - data = {'task_id': self.task_id, 'tag': tag} - openml._api_calls._perform_api_call("/task/untag", data=data) + +class OpenMLRegressionTask(OpenMLSupervisedTask): + def __init__(self, task_id, task_type_id, task_type, data_set_id, + estimation_procedure_type, estimation_parameters, + evaluation_measure, target_name, data_splits_url): + super(OpenMLRegressionTask, self).__init__( + task_id=task_id, + task_type_id=task_type_id, + task_type=task_type, + data_set_id=data_set_id, + estimation_procedure_type=estimation_procedure_type, + estimation_parameters=estimation_parameters, + evaluation_measure=evaluation_measure, + target_name=target_name, + data_splits_url=data_splits_url, + ) + + +class OpenMLClusteringTask(OpenMLTask): + def __init__(self, task_id, task_type_id, task_type, data_set_id, + estimation_procedure_type, estimation_parameters, + evaluation_measure, number_of_clusters=None): + super(OpenMLClusteringTask, self).__init__( + task_id=task_id, + task_type_id=task_type_id, + task_type=task_type, + data_set_id=data_set_id, + estimation_procedure_type=estimation_procedure_type, + estimation_parameters=estimation_parameters, + evaluation_measure=evaluation_measure, + ) + self.number_of_clusters = number_of_clusters + + +class OpenMLLearningCurveTask(OpenMLSupervisedTask): + def __init__(self, task_id, task_type_id, task_type, data_set_id, + estimation_procedure_type, estimation_parameters, + evaluation_measure, target_name, data_splits_url, + class_labels=None, cost_matrix=None): + super(OpenMLLearningCurveTask, self).__init__( + task_id=task_id, + task_type_id=task_type_id, + task_type=task_type, + data_set_id=data_set_id, + estimation_procedure_type=estimation_procedure_type, + estimation_parameters=estimation_parameters, + evaluation_measure=evaluation_measure, + target_name=target_name, + data_splits_url=data_splits_url, + ) + self.target_name = target_name + self.class_labels = class_labels + self.cost_matrix = cost_matrix + self.estimation_procedure["data_splits_url"] = data_splits_url + self.split = None + + if cost_matrix is not None: + raise NotImplementedError("Costmatrix")