From 80dff771cd73a53ef77dd52f7f7d6b557fed17b3 Mon Sep 17 00:00:00 2001 From: PGijsbers Date: Tue, 16 Apr 2019 10:29:04 +0200 Subject: [PATCH 01/11] Some packages only required for tests. --- setup.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/setup.py b/setup.py index d90003c638..b8e5c89eef 100644 --- a/setup.py +++ b/setup.py @@ -38,11 +38,9 @@ install_requires=[ 'liac-arff>=2.2.2', 'xmltodict', - 'pytest', 'requests', 'scikit-learn>=0.18', - 'nbformat', - 'python-dateutil', + 'python-dateutil', # Installed through pandas anyway. 'oslo.concurrency', 'pandas>=0.19.2', 'scipy>=0.13.3', @@ -56,7 +54,7 @@ 'pytest', 'pytest-xdist', 'pytest-timeout', - + 'nbformat' ], 'examples': [ 'matplotlib', From 397f94deec86d85d8ca158ff36b98e83e4919ffc Mon Sep 17 00:00:00 2001 From: PGijsbers Date: Tue, 16 Apr 2019 10:59:25 +0200 Subject: [PATCH 02/11] Make oslo a test-only dependency. --- openml/datasets/functions.py | 65 ++++++++++++++++-------------------- openml/flows/functions.py | 7 ++-- openml/tasks/functions.py | 51 +++++++++++++--------------- openml/utils.py | 32 ++++++++++++++++++ setup.py | 4 +-- 5 files changed, 88 insertions(+), 71 deletions(-) diff --git a/openml/datasets/functions.py b/openml/datasets/functions.py index 5804eb78ef..7ac010e1e4 100644 --- a/openml/datasets/functions.py +++ b/openml/datasets/functions.py @@ -1,7 +1,6 @@ import io import os import re -import warnings from typing import List, Dict, Union import numpy as np @@ -10,11 +9,6 @@ import xmltodict from scipy.sparse import coo_matrix -# Currently, importing oslo raises a lot of warning that it will stop working -# under python3.8; remove this once they disappear -with warnings.catch_warnings(): - warnings.simplefilter("ignore") - from oslo_concurrency import lockutils from collections import OrderedDict import openml.utils @@ -334,6 +328,7 @@ def get_datasets( return datasets +@openml.utils.thread_safe_if_oslo_installed def get_dataset(dataset_id: Union[int, str], download_data: bool = True) -> OpenMLDataset: """ Download the OpenML dataset representation, optionally also download actual data file. @@ -361,38 +356,34 @@ def get_dataset(dataset_id: Union[int, str], download_data: bool = True) -> Open raise ValueError("Dataset ID is neither an Integer nor can be " "cast to an Integer.") - with lockutils.external_lock( - name='datasets.functions.get_dataset:%d' % dataset_id, - lock_path=_create_lockfiles_dir(), - ): - did_cache_dir = _create_cache_directory_for_id( - DATASETS_CACHE_DIR_NAME, dataset_id, - ) + did_cache_dir = _create_cache_directory_for_id( + DATASETS_CACHE_DIR_NAME, dataset_id, + ) - try: - remove_dataset_cache = True - description = _get_dataset_description(did_cache_dir, dataset_id) - features = _get_dataset_features(did_cache_dir, dataset_id) - qualities = _get_dataset_qualities(did_cache_dir, dataset_id) - - arff_file = _get_dataset_arff(description) if download_data else None - - remove_dataset_cache = False - except OpenMLServerException as e: - # if there was an exception, - # check if the user had access to the dataset - if e.code == 112: - raise OpenMLPrivateDatasetError(e.message) from None - else: - raise e - finally: - if remove_dataset_cache: - _remove_cache_dir_for_id(DATASETS_CACHE_DIR_NAME, - did_cache_dir) - - dataset = _create_dataset_from_description( - description, features, qualities, arff_file - ) + try: + remove_dataset_cache = True + description = _get_dataset_description(did_cache_dir, dataset_id) + features = _get_dataset_features(did_cache_dir, dataset_id) + qualities = _get_dataset_qualities(did_cache_dir, dataset_id) + + arff_file = _get_dataset_arff(description) if download_data else None + + remove_dataset_cache = False + except OpenMLServerException as e: + # if there was an exception, + # check if the user had access to the dataset + if e.code == 112: + raise OpenMLPrivateDatasetError(e.message) from None + else: + raise e + finally: + if remove_dataset_cache: + _remove_cache_dir_for_id(DATASETS_CACHE_DIR_NAME, + did_cache_dir) + + dataset = _create_dataset_from_description( + description, features, qualities, arff_file + ) return dataset diff --git a/openml/flows/functions.py b/openml/flows/functions.py index 6ac01ebded..06371eb5ae 100644 --- a/openml/flows/functions.py +++ b/openml/flows/functions.py @@ -70,6 +70,7 @@ def _get_cached_flow(fid: int) -> OpenMLFlow: "cached" % fid) +@openml.utils.thread_safe_if_oslo_installed def get_flow(flow_id: int, reinstantiate: bool = False) -> OpenMLFlow: """Download the OpenML flow for a given flow ID. @@ -87,11 +88,7 @@ def get_flow(flow_id: int, reinstantiate: bool = False) -> OpenMLFlow: the flow """ flow_id = int(flow_id) - with lockutils.external_lock( - name='flows.functions.get_flow:%d' % flow_id, - lock_path=openml.utils._create_lockfiles_dir(), - ): - flow = _get_flow_description(flow_id) + flow = _get_flow_description(flow_id) if reinstantiate: flow.model = flow.extension.flow_to_model(flow) diff --git a/openml/tasks/functions.py b/openml/tasks/functions.py index 705e5a25d0..0f22aa5985 100644 --- a/openml/tasks/functions.py +++ b/openml/tasks/functions.py @@ -300,6 +300,7 @@ def get_tasks(task_ids, download_data=True): return tasks +@openml.utils.thread_safe_if_oslo_installed def get_task(task_id: int, download_data: bool = True) -> OpenMLTask: """Download OpenML task for a given task ID. @@ -324,34 +325,30 @@ def get_task(task_id: int, download_data: bool = True) -> OpenMLTask: 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, - lock_path=openml.utils._create_lockfiles_dir(), - ): - tid_cache_dir = openml.utils._create_cache_directory_for_id( - TASKS_CACHE_DIR_NAME, task_id, - ) + tid_cache_dir = openml.utils._create_cache_directory_for_id( + TASKS_CACHE_DIR_NAME, task_id, + ) - try: - task = _get_task_description(task_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 download_data: - if isinstance(task, OpenMLSupervisedTask): - task.download_split() - except Exception as e: - openml.utils._remove_cache_dir_for_id( - TASKS_CACHE_DIR_NAME, - tid_cache_dir, - ) - raise e + try: + task = _get_task_description(task_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 download_data: + if isinstance(task, OpenMLSupervisedTask): + task.download_split() + except Exception as e: + openml.utils._remove_cache_dir_for_id( + TASKS_CACHE_DIR_NAME, + tid_cache_dir, + ) + raise e return task diff --git a/openml/utils.py b/openml/utils.py index 25e0582abf..992ae0a3fb 100644 --- a/openml/utils.py +++ b/openml/utils.py @@ -2,11 +2,23 @@ import hashlib import xmltodict import shutil +import warnings import openml._api_calls import openml.exceptions from . import config +oslo_installed = False +try: + # Currently, importing oslo raises a lot of warning that it will stop working + # under python3.8; remove this once they disappear + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + from oslo_concurrency import lockutils + oslo_installed = True +except ImportError: + pass + def extract_xml_tags(xml_tag_name, node, allow_none=True): """Helper to extract xml tags from xmltodict. @@ -279,6 +291,26 @@ def _remove_cache_dir_for_id(key, cache_dir): 'Please do this manually!' % (key, cache_dir)) +def thread_safe_if_oslo_installed(func, *args, **kwargs): + if oslo_installed: + # Lock directories use the id that is passed as either a first argument, or as a keyword. + id_parameters = ['_id' in parameter_name for parameter_name in kwargs] + if len(id_parameters) == 1: + id_ = kwargs[id_parameters[0]] + elif len(args) > 0: + id_ = args[0] + else: + raise RuntimeError("An id must be specified for {}, was passed: ({}, {}).".format( + func.__name__, args, kwargs + )) + # The [7:] gets rid of the 'openml.' prefix + lock_name = "{}.{}:{}".format(func.__module__[7:], func.__name__, id_) + with lockutils.external_lock(name=lock_name, lock_path=_create_lockfiles_dir()): + return func(*args, **kwargs) + else: + return func(*args, **kwargs) + + def _create_lockfiles_dir(): dir = os.path.join(config.get_cache_directory(), 'locks') try: diff --git a/setup.py b/setup.py index b8e5c89eef..dccb381cfd 100644 --- a/setup.py +++ b/setup.py @@ -41,7 +41,6 @@ 'requests', 'scikit-learn>=0.18', 'python-dateutil', # Installed through pandas anyway. - 'oslo.concurrency', 'pandas>=0.19.2', 'scipy>=0.13.3', 'numpy>=1.6.2' @@ -54,7 +53,8 @@ 'pytest', 'pytest-xdist', 'pytest-timeout', - 'nbformat' + 'nbformat', + 'oslo.concurrency' ], 'examples': [ 'matplotlib', From 6c00e23c0ebb2a93cf06c840d392b66e67a6e3a3 Mon Sep 17 00:00:00 2001 From: PGijsbers Date: Tue, 16 Apr 2019 11:04:49 +0200 Subject: [PATCH 03/11] Fix decorator. --- openml/utils.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/openml/utils.py b/openml/utils.py index 992ae0a3fb..949f14c18d 100644 --- a/openml/utils.py +++ b/openml/utils.py @@ -291,24 +291,26 @@ def _remove_cache_dir_for_id(key, cache_dir): 'Please do this manually!' % (key, cache_dir)) -def thread_safe_if_oslo_installed(func, *args, **kwargs): +def thread_safe_if_oslo_installed(func): if oslo_installed: - # Lock directories use the id that is passed as either a first argument, or as a keyword. - id_parameters = ['_id' in parameter_name for parameter_name in kwargs] - if len(id_parameters) == 1: - id_ = kwargs[id_parameters[0]] - elif len(args) > 0: - id_ = args[0] - else: - raise RuntimeError("An id must be specified for {}, was passed: ({}, {}).".format( - func.__name__, args, kwargs - )) - # The [7:] gets rid of the 'openml.' prefix - lock_name = "{}.{}:{}".format(func.__module__[7:], func.__name__, id_) - with lockutils.external_lock(name=lock_name, lock_path=_create_lockfiles_dir()): - return func(*args, **kwargs) + def safe_func(*args, **kwargs): + # Lock directories use the id that is passed as either a first argument, or as a keyword. + id_parameters = [parameter_name for parameter_name in kwargs if '_id' in parameter_name] + if len(id_parameters) == 1: + id_ = kwargs[id_parameters[0]] + elif len(args) > 0: + id_ = args[0] + else: + raise RuntimeError("An id must be specified for {}, was passed: ({}, {}).".format( + func.__name__, args, kwargs + )) + # The [7:] gets rid of the 'openml.' prefix + lock_name = "{}.{}:{}".format(func.__module__[7:], func.__name__, id_) + with lockutils.external_lock(name=lock_name, lock_path=_create_lockfiles_dir()): + return func(*args, **kwargs) + return safe_func else: - return func(*args, **kwargs) + return func def _create_lockfiles_dir(): From a0a584ac50409a8344e9874f3a16068f9da7b033 Mon Sep 17 00:00:00 2001 From: PGijsbers Date: Tue, 16 Apr 2019 11:10:21 +0200 Subject: [PATCH 04/11] Remove old oslo imports. --- openml/flows/functions.py | 1 - openml/tasks/functions.py | 7 ------- 2 files changed, 8 deletions(-) diff --git a/openml/flows/functions.py b/openml/flows/functions.py index 06371eb5ae..24dc10e43b 100644 --- a/openml/flows/functions.py +++ b/openml/flows/functions.py @@ -5,7 +5,6 @@ import re import xmltodict from typing import Union, Dict -from oslo_concurrency import lockutils from ..exceptions import OpenMLCacheException import openml._api_calls diff --git a/openml/tasks/functions.py b/openml/tasks/functions.py index 0f22aa5985..3aa852c179 100644 --- a/openml/tasks/functions.py +++ b/openml/tasks/functions.py @@ -2,13 +2,6 @@ import io import re import os -import warnings - -# Currently, importing oslo raises a lot of warning that it will stop working -# under python3.8; remove this once they disappear -with warnings.catch_warnings(): - warnings.simplefilter("ignore") - from oslo_concurrency import lockutils import xmltodict from ..exceptions import OpenMLCacheException From 1458ad166dfd0e2091359587340283545abae273 Mon Sep 17 00:00:00 2001 From: PGijsbers Date: Tue, 16 Apr 2019 11:24:00 +0200 Subject: [PATCH 05/11] Flake8. Add thread safety to `get_run`. --- openml/datasets/functions.py | 3 +-- openml/runs/functions.py | 1 + openml/utils.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/openml/datasets/functions.py b/openml/datasets/functions.py index 7ac010e1e4..e4759f85c7 100644 --- a/openml/datasets/functions.py +++ b/openml/datasets/functions.py @@ -23,8 +23,7 @@ from ..utils import ( _create_cache_directory, _remove_cache_dir_for_id, - _create_cache_directory_for_id, - _create_lockfiles_dir, + _create_cache_directory_for_id ) diff --git a/openml/runs/functions.py b/openml/runs/functions.py index 2d39ff67b3..6e89e40e1b 100644 --- a/openml/runs/functions.py +++ b/openml/runs/functions.py @@ -466,6 +466,7 @@ def get_runs(run_ids): return runs +@openml.utils.thread_safe_if_oslo_installed def get_run(run_id): """Gets run corresponding to run_id. diff --git a/openml/utils.py b/openml/utils.py index 949f14c18d..dc1d837f3a 100644 --- a/openml/utils.py +++ b/openml/utils.py @@ -294,7 +294,7 @@ def _remove_cache_dir_for_id(key, cache_dir): def thread_safe_if_oslo_installed(func): if oslo_installed: def safe_func(*args, **kwargs): - # Lock directories use the id that is passed as either a first argument, or as a keyword. + # Lock directories use the id that is passed as either positional or keyword argument. id_parameters = [parameter_name for parameter_name in kwargs if '_id' in parameter_name] if len(id_parameters) == 1: id_ = kwargs[id_parameters[0]] From 69c8892ba0d31dd81b50301b57fc3e23764e47a2 Mon Sep 17 00:00:00 2001 From: PGijsbers Date: Tue, 16 Apr 2019 12:19:58 +0200 Subject: [PATCH 06/11] Updated with this and previous PR. --- doc/progress.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/progress.rst b/doc/progress.rst index f3cffdf9f7..fc9906937d 100644 --- a/doc/progress.rst +++ b/doc/progress.rst @@ -12,6 +12,8 @@ Changelog 0.9.0 ~~~~~ +* MAINT #596: Fewer dependencies for regular pip install. +* MAINT #652: Numpy and Scipy are no longer required before installation. * ADD #560: OpenML-Python can now handle regression tasks as well. * MAINT #184: Dropping Python2 support. From a9b09986492ae7616df0bc22a56eade47a23551b Mon Sep 17 00:00:00 2001 From: PGijsbers Date: Tue, 16 Apr 2019 12:20:39 +0200 Subject: [PATCH 07/11] Fixed some documentation when I was checking if install dependencies where mentioned in docs.. --- CONTRIBUTING.md | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d68e6034e2..4457868d8a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -31,7 +31,8 @@ local disk: $ git checkout -b feature/my-feature ``` - Always use a ``feature`` branch. It's good practice to never work on the ``master`` or ``develop`` branch! To make the nature of your pull request easily visible, please perpend the name of the branch with the type of changes you want to merge, such as ``feature`` if it contains a new feature, ``fix`` for a bugfix, ``doc`` for documentation and ``maint`` for other maintenance on the package. + Always use a ``feature`` branch. It's good practice to never work on the ``master`` or ``develop`` branch! + To make the nature of your pull request easily visible, please prepend the name of the branch with the type of changes you want to merge, such as ``feature`` if it contains a new feature, ``fix`` for a bugfix, ``doc`` for documentation and ``maint`` for other maintenance on the package. 4. Develop the feature on your feature branch. Add changed files using ``git add`` and then ``git commit`` files: @@ -59,7 +60,15 @@ We recommended that your contribution complies with the following rules before you submit a pull request: - Follow the - [pep8 style guilde](https://www.python.org/dev/peps/pep-0008/). + [pep8 style guide](https://www.python.org/dev/peps/pep-0008/). + With the following exceptions or additions: + - The max line length is 100 characters instead of 80. + - When creating a multi-line expression with binary operators, break before the operator. + - Add type hints to all function signatures. + (note: not all functions have type hints yet, this is work in progress.) + - Use the [`str.format`](https://docs.python.org/3/library/stdtypes.html#str.format) over [`printf`](https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting) style formatting. + E.g. use `"{} {}".format('hello', 'world')` not `"%s %s" % ('hello', 'world')`. + (note: old code may still use `printf`-formatting, this is work in progress.) - If your pull request addresses an issue, please use the pull request title to describe the issue and mention the issue number in the pull request description. This will make sure a link back to the original issue is @@ -105,18 +114,18 @@ tools: $ pytest --cov=. path/to/tests_for_package ``` -- No pyflakes warnings, check with: +- No style warnings, check with: ```bash - $ pip install pyflakes - $ pyflakes path/to/module.py + $ pip install flake8 + $ flake8 --ignore E402,W503 --show-source --max-line-length 100 ``` -- No PEP8 warnings, check with: +- No mypy (typing) issues, check with: ```bash - $ pip install pep8 - $ pep8 path/to/module.py + $ pip install mypy + $ mypy openml --ignore-missing-imports --follow-imports skip ``` Filing bugs @@ -151,8 +160,8 @@ following rules before submitting: New contributor tips -------------------- -A great way to start contributing to scikit-learn is to pick an item -from the list of [Easy issues](https://github.com/openml/openml-python/issues?q=label%3Aeasy) +A great way to start contributing to openml-python is to pick an item +from the list of [Good First Issues](https://github.com/openml/openml-python/labels/Good%20first%20issue) in the issue tracker. Resolving these issues allow you to start contributing to the project without much prior knowledge. Your assistance in this area will be greatly appreciated by the more @@ -175,6 +184,14 @@ information. For building the documentation, you will need [sphinx](http://sphinx.pocoo.org/), -[matplotlib](http://matplotlib.org/), and -[pillow](http://pillow.readthedocs.io/en/latest/). -[sphinx-bootstrap-theme](https://ryan-roemer.github.io/sphinx-bootstrap-theme/) +[sphinx-bootstrap-theme](https://ryan-roemer.github.io/sphinx-bootstrap-theme/), +[sphinx-gallery](https://sphinx-gallery.github.io/) +and +[numpydoc](https://numpydoc.readthedocs.io/en/latest/). +```bash +$ pip install sphinx sphinx-bootstrap-theme sphinx-gallery numpydoc +``` +When dependencies are installed, run +```bash +$ sphinx-build -b html doc YOUR_PREFERRED_OUTPUT_DIRECTORY +``` From 1598922c904306bbae07ce66a8a05da7c4e47f68 Mon Sep 17 00:00:00 2001 From: PGijsbers Date: Tue, 16 Apr 2019 12:24:16 +0200 Subject: [PATCH 08/11] fix typo. --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 4457868d8a..01b1dc0616 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -19,7 +19,7 @@ local disk: $ cd openml-python ``` -3. Swith to the ``develop`` branch: +3. Switch to the ``develop`` branch: ```bash $ git checkout develop From 331e827c6daeab27b543d92e2ed5156336afe08b Mon Sep 17 00:00:00 2001 From: PGijsbers Date: Tue, 16 Apr 2019 12:26:30 +0200 Subject: [PATCH 09/11] Specify to install test dependencies for contributors. --- doc/contributing.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/contributing.rst b/doc/contributing.rst index d1369defa1..33b11dc6d7 100644 --- a/doc/contributing.rst +++ b/doc/contributing.rst @@ -95,7 +95,8 @@ execute .. code:: bash - python setup.py install + pip install -e ".[test]" + Testing ======= From 26d4c40f9094c9025c68d016415b3e4452385521 Mon Sep 17 00:00:00 2001 From: PGijsbers Date: Tue, 16 Apr 2019 12:30:57 +0200 Subject: [PATCH 10/11] Clarify for regular and contributors, since this install is also reached from the front page as 'advanced' installation. --- doc/contributing.rst | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/doc/contributing.rst b/doc/contributing.rst index 33b11dc6d7..e614c8a25d 100644 --- a/doc/contributing.rst +++ b/doc/contributing.rst @@ -90,8 +90,14 @@ The package source code is available from git clone https://github.com/openml/openml-python.git -Once you cloned the package, change into the new directory ``python`` and -execute +Once you cloned the package, change into the new directory. +If you are a regular user, install with + +.. code:: bash + + pip install -e . + +If you are a contributor, you will also need to install test dependencies .. code:: bash From 973d48a3c8f211e10bfcf12f5a5ae8d6e06d860b Mon Sep 17 00:00:00 2001 From: PGijsbers Date: Tue, 16 Apr 2019 16:24:40 +0200 Subject: [PATCH 11/11] Add note to update CONTRIBUTING.md if things in this script change. --- ci_scripts/flake8_diff.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ci_scripts/flake8_diff.sh b/ci_scripts/flake8_diff.sh index 8b6da89b04..d745773410 100755 --- a/ci_scripts/flake8_diff.sh +++ b/ci_scripts/flake8_diff.sh @@ -1,4 +1,7 @@ #!/bin/bash +# Update /CONTRIBUTING.md if these commands change. +# The reason for not advocating using this script directly is that it +# might not work out of the box on Windows. flake8 --ignore E402,W503 --show-source --max-line-length 100 $options mypy openml --ignore-missing-imports --follow-imports skip