From aa44f789dd6e22dcaa59362407d514eaa8d02ab6 Mon Sep 17 00:00:00 2001 From: Kevin Jablonka <32935233+kjappelbaum@users.noreply.github.com> Date: Thu, 12 May 2022 18:14:08 +0200 Subject: [PATCH] chore: migrate to `src` layout, config in `setup.cfg`, drop `versioneer` (#206) * migrate to `src` layout and configuration in `setup.cfg` , drop `versioneer` * drop devcontainer * drop 3.7 support Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .bumpversion.cfg | 28 + .coveragerc | 4 - .deepsource.toml | 24 + .devcontainer/Dockerfile | 23 - .devcontainer/devcontainer.json | 44 - .github/workflows/python_package.yml | 2 +- .lgtm.yml | 6 - .pre-commit-config.yaml | 45 +- .pylintrc | 2 +- docs/conf.py | 4 +- mypy.ini | 5 - pyepal/_version.py | 558 ----- pyproject.toml | 18 + requirements.txt | 3 - setup.cfg | 151 +- setup.py | 97 +- {pyepal => src/pyepal}/__init__.py | 12 +- {pyepal => src/pyepal}/models/__init__.py | 0 .../pyepal}/models/coregionalized.py | 8 +- {pyepal => src/pyepal}/models/gbdt.py | 2 +- {pyepal => src/pyepal}/models/gpr.py | 0 {pyepal => src/pyepal}/models/nt.py | 24 +- {pyepal => src/pyepal}/pal/__init__.py | 0 {pyepal => src/pyepal}/pal/_hypervolume.py | 26 +- {pyepal => src/pyepal}/pal/core.py | 26 +- {pyepal => src/pyepal}/pal/pal_base.py | 58 +- .../pyepal}/pal/pal_coregionalized.py | 16 +- .../pyepal}/pal/pal_finite_ensemble.py | 20 +- {pyepal => src/pyepal}/pal/pal_gbdt.py | 0 {pyepal => src/pyepal}/pal/pal_gpflowgpr.py | 0 {pyepal => src/pyepal}/pal/pal_gpy.py | 12 +- .../pyepal}/pal/pal_neural_tangent.py | 2 +- {pyepal => src/pyepal}/pal/pal_sklearn.py | 4 +- {pyepal => src/pyepal}/pal/schedules.py | 0 {pyepal => src/pyepal}/pal/utils.py | 6 +- {pyepal => src/pyepal}/pal/validate_inputs.py | 28 +- {pyepal => src/pyepal}/plotting/__init__.py | 28 +- src/pyepal/version.py | 41 + tests/conftest.py | 8 +- tests/test_pal_core.py | 50 +- tests/test_pal_coregionalized.py | 16 +- tests/test_pal_gpflowgpr.py | 4 +- tests/test_pal_gpy.py | 14 +- tests/test_pal_neural_tangent.py | 8 +- tests/test_pal_sklearn.py | 16 +- tests/test_plotting.py | 4 +- tests/test_validate_inputs.py | 25 +- versioneer.py | 1823 ----------------- 48 files changed, 378 insertions(+), 2917 deletions(-) create mode 100644 .bumpversion.cfg create mode 100644 .deepsource.toml delete mode 100644 .devcontainer/Dockerfile delete mode 100644 .devcontainer/devcontainer.json delete mode 100644 .lgtm.yml delete mode 100644 mypy.ini delete mode 100644 pyepal/_version.py create mode 100644 pyproject.toml delete mode 100644 requirements.txt rename {pyepal => src/pyepal}/__init__.py (86%) rename {pyepal => src/pyepal}/models/__init__.py (100%) rename {pyepal => src/pyepal}/models/coregionalized.py (93%) rename {pyepal => src/pyepal}/models/gbdt.py (96%) rename {pyepal => src/pyepal}/models/gpr.py (100%) rename {pyepal => src/pyepal}/models/nt.py (88%) rename {pyepal => src/pyepal}/pal/__init__.py (100%) rename {pyepal => src/pyepal}/pal/_hypervolume.py (94%) rename {pyepal => src/pyepal}/pal/core.py (96%) rename {pyepal => src/pyepal}/pal/pal_base.py (94%) rename {pyepal => src/pyepal}/pal/pal_coregionalized.py (88%) rename {pyepal => src/pyepal}/pal/pal_finite_ensemble.py (93%) rename {pyepal => src/pyepal}/pal/pal_gbdt.py (100%) rename {pyepal => src/pyepal}/pal/pal_gpflowgpr.py (100%) rename {pyepal => src/pyepal}/pal/pal_gpy.py (91%) rename {pyepal => src/pyepal}/pal/pal_neural_tangent.py (100%) rename {pyepal => src/pyepal}/pal/pal_sklearn.py (97%) rename {pyepal => src/pyepal}/pal/schedules.py (100%) rename {pyepal => src/pyepal}/pal/utils.py (99%) rename {pyepal => src/pyepal}/pal/validate_inputs.py (94%) rename {pyepal => src/pyepal}/plotting/__init__.py (95%) create mode 100644 src/pyepal/version.py delete mode 100644 versioneer.py diff --git a/.bumpversion.cfg b/.bumpversion.cfg new file mode 100644 index 0000000..d15d33f --- /dev/null +++ b/.bumpversion.cfg @@ -0,0 +1,28 @@ +[bumpversion] +current_version = 0.8.0-dev0 +commit = True +tag = False +parse = (?P\d+)\.(?P\d+)\.(?P\d+)(?:-(?P[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+(?P[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))? +serialize = + {major}.{minor}.{patch}-{release}+{build} + {major}.{minor}.{patch}+{build} + {major}.{minor}.{patch}-{release} + {major}.{minor}.{patch} + +[bumpversion:part:release] +optional_value = production +first_value = dev +values = + dev + production + +[bumpverion:part:build] +values = [0-9A-Za-z-]+ + +[bumpversion:file:setup.cfg] +search = version = {current_version} +replace = version = {new_version} + +[bumpversion:file:src/pyepal/version.py] +search = VERSION = "{current_version}" +replace = VERSION = "{new_version}" diff --git a/.coveragerc b/.coveragerc index 4440d08..a6a5e4b 100644 --- a/.coveragerc +++ b/.coveragerc @@ -5,8 +5,6 @@ omit = *__init__* */usr/local/lib* *test* - *_version - *versioneer *setup* *examples/* *_hypervolume.py* @@ -16,8 +14,6 @@ omit = *__init__* */usr/local/lib* *test* - *_version* - *versioneer* *setup* *examples/* *_hypervolume.py* diff --git a/.deepsource.toml b/.deepsource.toml new file mode 100644 index 0000000..e02b45a --- /dev/null +++ b/.deepsource.toml @@ -0,0 +1,24 @@ +version = 1 + +test_patterns = ["tests/**"] + +exclude_patterns = [ + "docs/", + "dev/", + "binder/" +] + +[[analyzers]] +name = "python" +enabled = true + + [analyzers.meta] + runtime_version = "3.x.x" + +[[transformers]] +name = "black" +enabled = true + +[[transformers]] +name = "isort" +enabled = true diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile deleted file mode 100644 index 126b0e9..0000000 --- a/.devcontainer/Dockerfile +++ /dev/null @@ -1,23 +0,0 @@ -# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.145.1/containers/python-3/.devcontainer/base.Dockerfile - -# [Choice] Python version: 3, 3.9, 3.8, 3.7, 3.6 -ARG VARIANT="3" -FROM mcr.microsoft.com/vscode/devcontainers/python:0-${VARIANT} - -# [Option] Install Node.js -ARG INSTALL_NODE="true" -ARG NODE_VERSION="lts/*" -RUN if [ "${INSTALL_NODE}" = "true" ]; then su vscode -c "source /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi - -COPY requirements.txt /tmp/pip-tmp/ -RUN pip3 --disable-pip-version-check --no-cache-dir install -r /tmp/pip-tmp/requirements.txt \ - && rm -rf /tmp/pip-tmp -RUN pip3 --disable-pip-version-check --no-cache-dir install pre-commit black prospector pylint versioneer isort pytest \ - "pytest-cov<2.11" sphinx sphinx-book-theme sphinx-autodoc-typehints sphinx-copybutton "GPy==1.9.9" matplotlib "lightgbm>=3.0.0" - -# [Optional] Uncomment this section to install additional OS packages. -# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ -# && apt-get -y install --no-install-recommends - -# [Optional] Uncomment this line to install global node packages. -# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g " 2>&1 diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json deleted file mode 100644 index 83a8c88..0000000 --- a/.devcontainer/devcontainer.json +++ /dev/null @@ -1,44 +0,0 @@ -{ - "name": "Python 3", - "build": { - "dockerfile": "Dockerfile", - "context": "..", - "args": { - // Update 'VARIANT' to pick a Python version: 3, 3.6, 3.7, 3.8, 3.9 - "VARIANT": "3.8", - // Options - "INSTALL_NODE": "false", - "NODE_VERSION": "lts/*" - } - }, - - // Set *default* container specific settings.json values on container create. - "settings": { - "terminal.integrated.shell.linux": "/bin/bash", - "python.pythonPath": "/usr/local/bin/python", - "python.linting.enabled": true, - "python.linting.pylintEnabled": true, - "python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8", - "python.formatting.blackPath": "/usr/local/py-utils/bin/black", - "python.linting.banditPath": "/usr/local/py-utils/bin/bandit", - "python.linting.flake8Path": "/usr/local/py-utils/bin/flake8", - "python.linting.mypyPath": "/usr/local/py-utils/bin/mypy", - "python.linting.pycodestylePath": "/usr/local/py-utils/bin/pycodestyle", - "python.linting.pydocstylePath": "/usr/local/py-utils/bin/pydocstyle", - "python.linting.pylintPath": "/usr/local/py-utils/bin/pylint" - }, - - // Add the IDs of extensions you want installed when the container is created. - "extensions": [ - "ms-python.python" - ], - - // Use 'forwardPorts' to make a list of ports inside the container available locally. - // "forwardPorts": [], - - // Use 'postCreateCommand' to run commands after the container is created. - "postCreateCommand": "pip3 install -e .", - - // Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root. - // "remoteUser": "vscode" -} diff --git a/.github/workflows/python_package.yml b/.github/workflows/python_package.yml index 0c472c8..8f84509 100644 --- a/.github/workflows/python_package.yml +++ b/.github/workflows/python_package.yml @@ -14,7 +14,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, macOS-latest] - python-version: [3.7, 3.8] + python-version: [3.8] runs-on: ${{ matrix.os }} steps: diff --git a/.lgtm.yml b/.lgtm.yml deleted file mode 100644 index 1917200..0000000 --- a/.lgtm.yml +++ /dev/null @@ -1,6 +0,0 @@ -extraction: - python: - index: - filters: - - exclude: versioneer.py - - exclude: "**/_version.py" diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9263d63..076d9ac 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -34,38 +34,31 @@ repos: language: python types: [text] - # - repo: https://github.com/pre-commit/mirrors-mypy - # rev: "" # Use the sha / tag you want to point at - # hooks: - # - id: mypy - # exclude: (versioneer.py|_version.py|^docs/) - - - - repo: https://github.com/psf/black - rev: 22.3.0 - hooks: - - id: black - language_version: python3 - name: black - exclude: versioneer.py|pyepal/_version.py|^docs/ - entry: black - types: [python] + - repo: https://github.com/psf/black + rev: 22.3.0 + hooks: + - id: black + language_version: python3 + name: black + exclude: ^docs/ + entry: black + types: [python] - - repo: https://github.com/PyCQA/isort - rev: '5.10.1' # Use the revision sha / tag you want to point at - hooks: - - id: isort - name: isort - language: python - exclude: versioneer.py|pyepal/_version.py|^docs/ - types: [python] - entry: isort + - repo: https://github.com/PyCQA/isort + rev: "5.10.1" # Use the revision sha / tag you want to point at + hooks: + - id: isort + name: isort + language: python + exclude: ^docs/ + types: [python] + entry: isort - repo: local hooks: - id: pylint language: python types: [file, python] - exclude: versioneer.py|pyepal/_version.py|^docs|pyepal/pal/_hypervolume.py|pyepal/models/coregionalized.py/tests + exclude: ^docs|pyepal/pal/_hypervolume.py|pyepal/models/coregionalized.py/tests name: pylint entry: pylint diff --git a/.pylintrc b/.pylintrc index 9c9c674..bd30099 100644 --- a/.pylintrc +++ b/.pylintrc @@ -1,6 +1,6 @@ [MASTER] ignore=docs -ignore_patterns=versioneer.py,*version.py,*hypervolume.py,models.coregionalized.py +ignore_patterns=*hypervolume.py,models.coregionalized.py [MESSAGES CONTROL] diff --git a/docs/conf.py b/docs/conf.py index a58575a..f7a486e 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -14,7 +14,7 @@ import os import sys -from pyepal import __version__ +from pyepal.version import VERSION sys.path.insert(0, os.path.abspath("..")) @@ -26,7 +26,7 @@ author = "Kevin Maik Jablonka, Brian Yoo, Berend Smit" # The full version, including alpha/beta/rc tags -release = __version__ +release = VERSION # -- General configuration --------------------------------------------------- diff --git a/mypy.ini b/mypy.ini deleted file mode 100644 index b1289e7..0000000 --- a/mypy.ini +++ /dev/null @@ -1,5 +0,0 @@ -[mypy-pyepal._version] -ignore_errors = True - -[mypy-versioneer] -ignore_errors = True diff --git a/pyepal/_version.py b/pyepal/_version.py deleted file mode 100644 index 727cc73..0000000 --- a/pyepal/_version.py +++ /dev/null @@ -1,558 +0,0 @@ -# -*- coding: utf-8 -*- - -# This file helps to compute a version number in source trees obtained from -# git-archive tarball (such as those provided by githubs download-from-tag -# feature). Distribution tarballs (built by setup.py sdist) and build -# directories (produced by setup.py build) will contain a much shorter file -# that just contains the computed version number. - -# This file is released into the public domain. Generated by -# versioneer-0.18 (https://github.com/warner/python-versioneer) - -"""Git implementation of _version.py.""" - -import errno -import os -import re -import subprocess -import sys - - -def get_keywords(): - """Get the keywords needed to look up the version information.""" - # these strings will be replaced by git during git-archive. - # setup.py/versioneer.py will grep for the variable names, so they must - # each be defined on a line of their own. _version.py will just call - # get_keywords(). - git_refnames = "$Format:%d$" - git_full = "$Format:%H$" - git_date = "$Format:%ci$" - keywords = {"refnames": git_refnames, "full": git_full, "date": git_date} - return keywords - - -class VersioneerConfig: - """Container for Versioneer configuration parameters.""" - - -def get_config(): - """Create, populate and return the VersioneerConfig() object.""" - # these strings are filled in when 'setup.py versioneer' creates - # _version.py - cfg = VersioneerConfig() - cfg.VCS = "git" - cfg.style = "pep440" - cfg.tag_prefix = "" - cfg.parentdir_prefix = "" - cfg.versionfile_source = "pyepal/_version.py" - cfg.verbose = False - return cfg - - -class NotThisMethod(Exception): - """Exception raised if a method is not valid for the current scenario.""" - - -LONG_VERSION_PY = {} -HANDLERS = {} - - -def register_vcs_handler(vcs, method): # decorator - """Decorator to mark a method as the handler for a particular VCS.""" - - def decorate(f): - """Store f in HANDLERS[vcs][method].""" - if vcs not in HANDLERS: - HANDLERS[vcs] = {} - HANDLERS[vcs][method] = f - return f - - return decorate - - -def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, env=None): - """Call the given command(s).""" - assert isinstance(commands, list) - p = None - for c in commands: - try: - dispcmd = str([c] + args) - # remember shell=False, so use git.cmd on windows, not just git - p = subprocess.Popen( - [c] + args, - cwd=cwd, - env=env, - stdout=subprocess.PIPE, - stderr=(subprocess.PIPE if hide_stderr else None), - ) - break - except EnvironmentError: - e = sys.exc_info()[1] - if e.errno == errno.ENOENT: - continue - if verbose: - print("unable to run %s" % dispcmd) - print(e) - return None, None - else: - if verbose: - print("unable to find command, tried %s" % (commands,)) - return None, None - stdout = p.communicate()[0].strip() - if sys.version_info[0] >= 3: - stdout = stdout.decode() - if p.returncode != 0: - if verbose: - print("unable to run %s (error)" % dispcmd) - print("stdout was %s" % stdout) - return None, p.returncode - return stdout, p.returncode - - -def versions_from_parentdir(parentdir_prefix, root, verbose): - """Try to determine the version from the parent directory name. - - Source tarballs conventionally unpack into a directory that includes both - the project name and a version string. We will also support searching up - two directory levels for an appropriately named parent directory - """ - rootdirs = [] - - for i in range(3): - dirname = os.path.basename(root) - if dirname.startswith(parentdir_prefix): - return { - "version": dirname[len(parentdir_prefix) :], - "full-revisionid": None, - "dirty": False, - "error": None, - "date": None, - } - else: - rootdirs.append(root) - root = os.path.dirname(root) # up a level - - if verbose: - print( - "Tried directories %s but none started with prefix %s" - % (str(rootdirs), parentdir_prefix) - ) - raise NotThisMethod("rootdir doesn't start with parentdir_prefix") - - -@register_vcs_handler("git", "get_keywords") -def git_get_keywords(versionfile_abs): - """Extract version information from the given file.""" - # the code embedded in _version.py can just fetch the value of these - # keywords. When used from setup.py, we don't want to import _version.py, - # so we do it with a regexp instead. This function is not used from - # _version.py. - keywords = {} - try: - f = open(versionfile_abs, "r") - for line in f.readlines(): - if line.strip().startswith("git_refnames ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["refnames"] = mo.group(1) - if line.strip().startswith("git_full ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["full"] = mo.group(1) - if line.strip().startswith("git_date ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["date"] = mo.group(1) - f.close() - except EnvironmentError: - pass - return keywords - - -@register_vcs_handler("git", "keywords") -def git_versions_from_keywords(keywords, tag_prefix, verbose): - """Get version information from git keywords.""" - if not keywords: - raise NotThisMethod("no keywords at all, weird") - date = keywords.get("date") - if date is not None: - # git-2.2.0 added "%cI", which expands to an ISO-8601 -compliant - # datestamp. However we prefer "%ci" (which expands to an "ISO-8601 - # -like" string, which we must then edit to make compliant), because - # it's been around since git-1.5.3, and it's too difficult to - # discover which version we're using, or to work around using an - # older one. - date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) - refnames = keywords["refnames"].strip() - if refnames.startswith("$Format"): - if verbose: - print("keywords are unexpanded, not using") - raise NotThisMethod("unexpanded keywords, not a git-archive tarball") - refs = set([r.strip() for r in refnames.strip("()").split(",")]) - # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of - # just "foo-1.0". If we see a "tag: " prefix, prefer those. - TAG = "tag: " - tags = set([r[len(TAG) :] for r in refs if r.startswith(TAG)]) - if not tags: - # Either we're using git < 1.8.3, or there really are no tags. We use - # a heuristic: assume all version tags have a digit. The old git %d - # expansion behaves like git log --decorate=short and strips out the - # refs/heads/ and refs/tags/ prefixes that would let us distinguish - # between branches and tags. By ignoring refnames without digits, we - # filter out many common branch names like "release" and - # "stabilization", as well as "HEAD" and "master". - tags = set([r for r in refs if re.search(r"\d", r)]) - if verbose: - print("discarding '%s', no digits" % ",".join(refs - tags)) - if verbose: - print("likely tags: %s" % ",".join(sorted(tags))) - for ref in sorted(tags): - # sorting will prefer e.g. "2.0" over "2.0rc1" - if ref.startswith(tag_prefix): - r = ref[len(tag_prefix) :] - if verbose: - print("picking %s" % r) - return { - "version": r, - "full-revisionid": keywords["full"].strip(), - "dirty": False, - "error": None, - "date": date, - } - # no suitable tags, so version is "0+unknown", but full hex is still there - if verbose: - print("no suitable tags, using unknown + full revision id") - return { - "version": "0+unknown", - "full-revisionid": keywords["full"].strip(), - "dirty": False, - "error": "no suitable tags", - "date": None, - } - - -@register_vcs_handler("git", "pieces_from_vcs") -def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): - """Get version from 'git describe' in the root of the source tree. - - This only gets called if the git-archive 'subst' keywords were *not* - expanded, and _version.py hasn't already been rewritten with a short - version string, meaning we're inside a checked out source tree. - """ - GITS = ["git"] - if sys.platform == "win32": - GITS = ["git.cmd", "git.exe"] - - out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root, hide_stderr=True) - if rc != 0: - if verbose: - print("Directory %s not under git control" % root) - raise NotThisMethod("'git rev-parse --git-dir' returned error") - - # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] - # if there isn't one, this yields HEX[-dirty] (no NUM) - describe_out, rc = run_command( - GITS, - [ - "describe", - "--tags", - "--dirty", - "--always", - "--long", - "--match", - "%s*" % tag_prefix, - ], - cwd=root, - ) - # --long was added in git-1.5.5 - if describe_out is None: - raise NotThisMethod("'git describe' failed") - describe_out = describe_out.strip() - full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root) - if full_out is None: - raise NotThisMethod("'git rev-parse' failed") - full_out = full_out.strip() - - pieces = {} - pieces["long"] = full_out - pieces["short"] = full_out[:7] # maybe improved later - pieces["error"] = None - - # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] - # TAG might have hyphens. - git_describe = describe_out - - # look for -dirty suffix - dirty = git_describe.endswith("-dirty") - pieces["dirty"] = dirty - if dirty: - git_describe = git_describe[: git_describe.rindex("-dirty")] - - # now we have TAG-NUM-gHEX or HEX - - if "-" in git_describe: - # TAG-NUM-gHEX - mo = re.search(r"^(.+)-(\d+)-g([0-9a-f]+)$", git_describe) - if not mo: - # unparseable. Maybe git-describe is misbehaving? - pieces["error"] = "unable to parse git-describe output: '%s'" % describe_out - return pieces - - # tag - full_tag = mo.group(1) - if not full_tag.startswith(tag_prefix): - if verbose: - fmt = "tag '%s' doesn't start with prefix '%s'" - print(fmt % (full_tag, tag_prefix)) - pieces["error"] = "tag '%s' doesn't start with prefix '%s'" % ( - full_tag, - tag_prefix, - ) - return pieces - pieces["closest-tag"] = full_tag[len(tag_prefix) :] - - # distance: number of commits since tag - pieces["distance"] = int(mo.group(2)) - - # commit: short hex revision ID - pieces["short"] = mo.group(3) - - else: - # HEX: no tags - pieces["closest-tag"] = None - count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"], cwd=root) - pieces["distance"] = int(count_out) # total number of commits - - # commit date: see ISO-8601 comment in git_versions_from_keywords() - date = run_command(GITS, ["show", "-s", "--format=%ci", "HEAD"], cwd=root)[ - 0 - ].strip() - pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) - - return pieces - - -def plus_or_dot(pieces): - """Return a + if we don't already have one, else return a .""" - if "+" in pieces.get("closest-tag", ""): - return "." - return "+" - - -def render_pep440(pieces): - """Build up version string, with post-release "local version identifier". - - Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you - get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty - - Exceptions: - 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += plus_or_dot(pieces) - rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - else: - # exception #1 - rendered = "0+untagged.%d.g%s" % (pieces["distance"], pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - return rendered - - -def render_pep440_pre(pieces): - """TAG[.post.devDISTANCE] -- No -dirty. - - Exceptions: - 1: no tags. 0.post.devDISTANCE - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"]: - rendered += ".post.dev%d" % pieces["distance"] - else: - # exception #1 - rendered = "0.post.dev%d" % pieces["distance"] - return rendered - - -def render_pep440_post(pieces): - """TAG[.postDISTANCE[.dev0]+gHEX] . - - The ".dev0" means dirty. Note that .dev0 sorts backwards - (a dirty tree will appear "older" than the corresponding clean one), - but you shouldn't be releasing software with -dirty anyways. - - Exceptions: - 1: no tags. 0.postDISTANCE[.dev0] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += ".post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - rendered += plus_or_dot(pieces) - rendered += "g%s" % pieces["short"] - else: - # exception #1 - rendered = "0.post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - rendered += "+g%s" % pieces["short"] - return rendered - - -def render_pep440_old(pieces): - """TAG[.postDISTANCE[.dev0]] . - - The ".dev0" means dirty. - - Eexceptions: - 1: no tags. 0.postDISTANCE[.dev0] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += ".post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - else: - # exception #1 - rendered = "0.post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - return rendered - - -def render_git_describe(pieces): - """TAG[-DISTANCE-gHEX][-dirty]. - - Like 'git describe --tags --dirty --always'. - - Exceptions: - 1: no tags. HEX[-dirty] (note: no 'g' prefix) - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"]: - rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) - else: - # exception #1 - rendered = pieces["short"] - if pieces["dirty"]: - rendered += "-dirty" - return rendered - - -def render_git_describe_long(pieces): - """TAG-DISTANCE-gHEX[-dirty]. - - Like 'git describe --tags --dirty --always -long'. - The distance/hash is unconditional. - - Exceptions: - 1: no tags. HEX[-dirty] (note: no 'g' prefix) - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) - else: - # exception #1 - rendered = pieces["short"] - if pieces["dirty"]: - rendered += "-dirty" - return rendered - - -def render(pieces, style): - """Render the given version pieces into the requested style.""" - if pieces["error"]: - return { - "version": "unknown", - "full-revisionid": pieces.get("long"), - "dirty": None, - "error": pieces["error"], - "date": None, - } - - if not style or style == "default": - style = "pep440" # the default - - if style == "pep440": - rendered = render_pep440(pieces) - elif style == "pep440-pre": - rendered = render_pep440_pre(pieces) - elif style == "pep440-post": - rendered = render_pep440_post(pieces) - elif style == "pep440-old": - rendered = render_pep440_old(pieces) - elif style == "git-describe": - rendered = render_git_describe(pieces) - elif style == "git-describe-long": - rendered = render_git_describe_long(pieces) - else: - raise ValueError("unknown style '%s'" % style) - - return { - "version": rendered, - "full-revisionid": pieces["long"], - "dirty": pieces["dirty"], - "error": None, - "date": pieces.get("date"), - } - - -def get_versions(): - """Get version information or return default if unable to do so.""" - # I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have - # __file__, we can work backwards from there to the root. Some - # py2exe/bbfreeze/non-CPython implementations don't do __file__, in which - # case we can only use expanded keywords. - - cfg = get_config() - verbose = cfg.verbose - - try: - return git_versions_from_keywords(get_keywords(), cfg.tag_prefix, verbose) - except NotThisMethod: - pass - - try: - root = os.path.realpath(__file__) - # versionfile_source is the relative path from the top of the source - # tree (where the .git directory might live) to this file. Invert - # this to find the root from __file__. - for i in cfg.versionfile_source.split("/"): - root = os.path.dirname(root) - except NameError: - return { - "version": "0+unknown", - "full-revisionid": None, - "dirty": None, - "error": "unable to find root of source tree", - "date": None, - } - - try: - pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose) - return render(pieces, cfg.style) - except NotThisMethod: - pass - - try: - if cfg.parentdir_prefix: - return versions_from_parentdir(cfg.parentdir_prefix, root, verbose) - except NotThisMethod: - pass - - return { - "version": "0+unknown", - "full-revisionid": None, - "dirty": None, - "error": "unable to compute version", - "date": None, - } diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..db68452 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,18 @@ +# See https://setuptools.readthedocs.io/en/latest/build_meta.html +[build-system] +requires = [ + "setuptools >= 35.0.2", + "setuptools_scm >= 2.0.0, <3" +] +build-backend = "setuptools.build_meta" + +[tool.black] +line-length = 100 +target-version = ["py37", "py38", "py39"] + +[tool.isort] +profile = "black" +multi_line_output = 3 +line_length = 100 +include_trailing_comma = true +reverse_relative = true diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index ea2cb5e..0000000 --- a/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -numpy==1.19.* -numba==0.54.* -scikit-learn==1.0 diff --git a/setup.cfg b/setup.cfg index 7399638..1d7dc6a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,15 +1,136 @@ -[versioneer] -VCS = git -style = pep440 -versionfile_source = pyepal/_version.py -versionfile_build = pyepal/_version.py -tag_prefix = -parentdir_prefix = - -[isort] -multi_line_output = 3 -include_trailing_comma = True -force_grid_wrap = 0 -use_parentheses = True -ensure_newline_before_comments = True -line_length = 88 +[metadata] +name = pyepal +version = 0.8.0-dev0 +description = PyePAL implements the epsilon-PAL active learning algorithm +author = Kevin Maik Jablonka, Brian Yoo +author_email = mail@kjablonka.com, brian.yoo@basf.com +maintainer_email = mail@kjablonka.com +long_description = file: README.md +long_description_content_type = text/markdown + +# URLs associated with the project +url = https://github.com/kjappelbaum/pyepal +download_url =https://github.com/kjappelbaum/pyepal/releases +project_urls = + Bug Tracker = https://github.com/kjappelbaum/pyepal/issues + Source Code = https://github.com/kjappelbaum/pyepal/ + + +license = Apache +license_file = LICENSE + +# Search tags +classifiers = + Development Status :: 4 - Beta + Environment :: Console + Intended Audience :: Developers + License :: OSI Approved :: Apache Software License + Operating System :: OS Independent + Framework :: Pytest + Framework :: tox + Framework :: Sphinx + Programming Language :: Python + Programming Language :: Python :: 3.8 + Programming Language :: Python :: 3 :: Only +keywords = + active-learning + machine-learning + + +[options] +install_requires = + numpy < 1.20 + numba < 0.54 + scikit-learn < 2.0 + + +# Random options +zip_safe = false +include_package_data = True +python_requires = >=3.8, < 3.9 + +# Where is my code +packages = find: +package_dir = + = src + + + +[options.extras_require] +gpy = + GPy < 1.10 + matplotlib +GBDT = + lightgbm <= 3 +neural_tangents = + jaxlib <= 0.1.65 + jax <= 0.2.12 + neural_tangents < 0.4.0 +gpflow = + tensorflow >= 2.5, < 2.6 + tensorflow-probability >= 0.12, < 0.13 + gpflow < 2.3.0, >= 2.2.0 +all = + GPy < 1.10 + matplotlib + lightgbm <= 3 + jaxlib <= 0.1.65 + jax <= 0.2.12 + neural_tangents < 0.4.0 + tensorflow >= 2.5, < 2.6 + tensorflow-probability >= 0.12, < 0.13 + gpflow < 3.0.0, >= 2.2.0 +docs = + sphinx + sphinx-book-theme + sphinx-autodoc-typehints + sphinx-copybutton +testing = + pytest + pytest-cov +pre-commit = + pre-commit + pylint + isort + +[options.packages.find] +where = src + +###################### +# Doc8 Configuration # +# (doc8.ini) # +###################### +[doc8] +max-line-length = 120 + +########################## +# Coverage Configuration # +# (.coveragerc) # +########################## +[coverage:run] +branch = True +source = pyepal +omit = + tests/* + docs/* + +[coverage:paths] +source = + src/pyepal + .tox/*/lib/python*/site-packages/pyepal + +[coverage:report] +show_missing = True +exclude_lines = + pragma: no cover + raise NotImplementedError + if __name__ == .__main__.: + def __str__ + def __repr__ + +########################## +# Darglint Configuration # +########################## +[darglint] +docstring_style = sphinx +strictness = short diff --git a/setup.py b/setup.py index d883511..4d071c6 100644 --- a/setup.py +++ b/setup.py @@ -1,97 +1,8 @@ # -*- coding: utf-8 -*- -# Copyright 2020 PyePAL authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""Model agnostic Python implementation of the epsilon-PAL algorithm""" -from setuptools import find_packages, setup +"""Setup module""" -import versioneer +import setuptools -with open("requirements.txt", "r", encoding="utf-8") as fh: - REQUIREMENTS = fh.readlines() - - -with open("README.md", "r", encoding="utf-8") as fh: - LONG_DESCRIPTION = fh.read() - -gpy_requirements = [ - "GPy~=1.9.9", - "matplotlib==3.*", -] -gbdt_requirements = ["lightgbm==3.*"] -neural_tangents_requirements = [ - "jaxlib<=0.1.65", - "jax<=0.2.12", - "neural_tangents==0.3.*", -] -gpflow_requirements = [ - "tensorflow==2.5.*", - "tensorflow-probability==0.12.*", - "gpflow==2.2.*", -] -setup( - name="pyepal", - version=versioneer.get_version(), - cmdclass=versioneer.get_cmdclass(), - description="PyePAL implemented the epsilon-PAL active learning algorithm", - long_description=LONG_DESCRIPTION, - long_description_content_type="text/markdown", - packages=find_packages(include=["pyepal", "pyepal.*"]), - url="https://github.com/kjappelbaum/PyePAL", - license="Apache 2.0", - install_requires=REQUIREMENTS, - extras_require={ - "testing": ["pytest==6.*", "pytest-cov==2.*"], - "docs": [ - "sphinx==3.*", - "sphinx-book-theme==0.*", - "sphinx-autodoc-typehints==1.*", - "sphinx-copybutton==0.*", - ], - "pre-commit": [ - "pre-commit==2.*", - "pylint==2.*", - "isort==5.*", - ], - "dev": [ - "versioneer==0.*", - "black==20.*", - ], - "GPy": gpy_requirements, - "GBDT": gbdt_requirements, - "neural_tangents": neural_tangents_requirements, - "all": neural_tangents_requirements - + gbdt_requirements - + gpy_requirements - + gpflow_requirements, - "gpflow": gpflow_requirements, - }, - author="PyePAL authors", - author_email="kevin.jablonka@epfl.ch, brian.yoo@basf.com", - classifiers=[ - "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3 :: Only", - "Programming Language :: Python :: 3.7", - "Programming Language :: Python :: 3.8", - "Development Status :: 4 - Beta", - "Intended Audience :: Science/Research", - "License :: OSI Approved :: Apache Software License", - "Operating System :: OS Independent", - "Topic :: Scientific/Engineering", - "Topic :: Scientific/Engineering :: Physics", - "Topic :: Scientific/Engineering :: Chemistry", - "Topic :: Software Development :: Libraries :: Python Modules", - "Topic :: Scientific/Engineering :: Artificial Intelligence", - ], -) +if __name__ == "__main__": + setuptools.setup() diff --git a/pyepal/__init__.py b/src/pyepal/__init__.py similarity index 86% rename from pyepal/__init__.py rename to src/pyepal/__init__.py index 98eeac7..e096b81 100644 --- a/pyepal/__init__.py +++ b/src/pyepal/__init__.py @@ -15,7 +15,6 @@ """PyePAL""" -from ._version import get_versions from .models.nt import JaxOptimizer, NTModel from .pal.pal_base import PALBase from .pal.pal_coregionalized import PALCoregionalized @@ -25,15 +24,10 @@ from .pal.pal_gpy import PALGPy from .pal.pal_neural_tangent import PALNT from .pal.pal_sklearn import PALSklearn -from .pal.utils import ( - exhaust_loop, - get_hypervolume, - get_kmeans_samples, - get_maxmin_samples, -) +from .pal.utils import exhaust_loop, get_hypervolume, get_kmeans_samples, get_maxmin_samples +from .version import VERSION -__version__ = get_versions()["version"] -del get_versions +__version__ = VERSION __all__ = [ "PALBase", diff --git a/pyepal/models/__init__.py b/src/pyepal/models/__init__.py similarity index 100% rename from pyepal/models/__init__.py rename to src/pyepal/models/__init__.py diff --git a/pyepal/models/coregionalized.py b/src/pyepal/models/coregionalized.py similarity index 93% rename from pyepal/models/coregionalized.py rename to src/pyepal/models/coregionalized.py index 719814b..a399245 100644 --- a/pyepal/models/coregionalized.py +++ b/src/pyepal/models/coregionalized.py @@ -65,13 +65,9 @@ def __init__( # pylint:disable=too-many-arguments ) # Likelihood - likelihood = util.multioutput.build_likelihood( - Y_list, self.output_index, likelihoods_list - ) + likelihood = util.multioutput.build_likelihood(Y_list, self.output_index, likelihoods_list) - super( # pylint:disable=super-with-arguments - GPCoregionalizedRegression, self - ).__init__( + super(GPCoregionalizedRegression, self).__init__( # pylint:disable=super-with-arguments X, Y, kernel, diff --git a/pyepal/models/gbdt.py b/src/pyepal/models/gbdt.py similarity index 96% rename from pyepal/models/gbdt.py rename to src/pyepal/models/gbdt.py index f5eb50c..e10ca7a 100644 --- a/pyepal/models/gbdt.py +++ b/src/pyepal/models/gbdt.py @@ -28,7 +28,7 @@ def build_gbdt_tuple( 0.5, 0.75, ], - **kwargs # pylint:disable=dangerous-default-value + **kwargs, # pylint:disable=dangerous-default-value ) -> Tuple[LGBMRegressor, LGBMRegressor, LGBMRegressor]: """Build a Tuple of LGBMRegressors in the correct format for PALGBDT diff --git a/pyepal/models/gpr.py b/src/pyepal/models/gpr.py similarity index 100% rename from pyepal/models/gpr.py rename to src/pyepal/models/gpr.py diff --git a/pyepal/models/nt.py b/src/pyepal/models/nt.py similarity index 88% rename from pyepal/models/nt.py rename to src/pyepal/models/nt.py index 1c36a1f..f74b3f3 100644 --- a/pyepal/models/nt.py +++ b/src/pyepal/models/nt.py @@ -97,9 +97,7 @@ def build_dense_network( activations = [stax.Erf() for _ in hidden_layers] else: for activation in activations: - assert callable( - activation - ), "You need to provide `neural_tangents.stax` activations" + assert callable(activation), "You need to provide `neural_tangents.stax` activations" assert len(activations) == len( hidden_layers @@ -142,25 +140,15 @@ def get_optimizer( optimizer_kwargs = {} optimizer = optimizer.lower() if optimizer == "adam": - opt_init, opt_update, get_params = optimizers.adam( - learning_rate, **optimizer_kwargs - ) + opt_init, opt_update, get_params = optimizers.adam(learning_rate, **optimizer_kwargs) elif optimizer == "adagrad": - opt_init, opt_update, get_params = optimizers.adagrad( - learning_rate, **optimizer_kwargs - ) + opt_init, opt_update, get_params = optimizers.adagrad(learning_rate, **optimizer_kwargs) elif optimizer == "adamax": - opt_init, opt_update, get_params = optimizers.adamax( - learning_rate, **optimizer_kwargs - ) + opt_init, opt_update, get_params = optimizers.adamax(learning_rate, **optimizer_kwargs) elif optimizer == "rmsprop": - opt_init, opt_update, get_params = optimizers.rmsprop( - learning_rate, **optimizer_kwargs - ) + opt_init, opt_update, get_params = optimizers.rmsprop(learning_rate, **optimizer_kwargs) else: - opt_init, opt_update, get_params = optimizers.sgd( - learning_rate, **optimizer_kwargs - ) + opt_init, opt_update, get_params = optimizers.sgd(learning_rate, **optimizer_kwargs) opt_update = jit(opt_update) diff --git a/pyepal/pal/__init__.py b/src/pyepal/pal/__init__.py similarity index 100% rename from pyepal/pal/__init__.py rename to src/pyepal/pal/__init__.py diff --git a/pyepal/pal/_hypervolume.py b/src/pyepal/pal/_hypervolume.py similarity index 94% rename from pyepal/pal/_hypervolume.py rename to src/pyepal/pal/_hypervolume.py index 734b04d..6f00fbe 100644 --- a/pyepal/pal/_hypervolume.py +++ b/src/pyepal/pal/_hypervolume.py @@ -98,9 +98,7 @@ def create_sorted(cls, dimension: int, points: tp.Any) -> "VectorLinkedList": return linked_list @staticmethod - def sort_by_index( - node_list: tp.List[VectorNode], dimension_index: int - ) -> tp.List[VectorNode]: + def sort_by_index(node_list: tp.List[VectorNode], dimension_index: int) -> tp.List[VectorNode]: """Returns a sorted list of `VectorNode`, with the sorting key defined by the `dimension_index`-th coordinates of the nodes in the `node_list`.""" return sorted(node_list, key=lambda node: node.coordinates[dimension_index]) @@ -135,9 +133,7 @@ def extend(self, nodes: tp.List[VectorNode], index: int) -> None: self.append(node, index) @staticmethod - def update_coordinate_bounds( - bounds: np.ndarray, node: VectorNode, index: int - ) -> np.ndarray: + def update_coordinate_bounds(bounds: np.ndarray, node: VectorNode, index: int) -> np.ndarray: for i in range(index): if bounds[i] > node.coordinates[i]: bounds[i] = node.coordinates[i] @@ -162,9 +158,7 @@ def reinsert(self, node: VectorNode, index: int) -> None: node.prev[i].next[i] = node node.next[i].prev[i] = node - def iterate( - self, index: int, start: tp.Optional[VectorNode] = None - ) -> tp.Iterator[VectorNode]: + def iterate(self, index: int, start: tp.Optional[VectorNode] = None) -> tp.Iterator[VectorNode]: if start is None: node = self.sentinel.next[index] else: @@ -231,9 +225,7 @@ def plane_hypervolume(self) -> float: next_node = node.next[dimension] if next_node is self.multilist.sentinel: break - hypervolume += h * ( - node.coordinates[dimension] - next_node.coordinates[dimension] - ) + hypervolume += h * (node.coordinates[dimension] - next_node.coordinates[dimension]) h = min(h, next_node.coordinates[dimension - 1]) last_node = self.multilist.sentinel.prev[dimension] hypervolume += h * last_node.coordinates[dimension] @@ -266,8 +258,7 @@ def recursive_hypervolume(self, dimension: int) -> float: current_node = node if self.multilist.chain_length(dimension - 1) > 1 and ( node.coordinates[dimension] > self.reference_bounds[dimension] - or node.prev[dimension].coordinates[dimension] - >= self.reference_bounds[dimension] + or node.prev[dimension].coordinates[dimension] >= self.reference_bounds[dimension] ): # Line 9 self.reference_bounds = self.multilist.update_coordinate_bounds( @@ -297,14 +288,11 @@ def recursive_hypervolume(self, dimension: int) -> float: self.skip_dominated_points(current_node, dimension) # Line 17 - for node in self.multilist.iterate( - dimension, start=current_node.next[dimension] - ): + for node in self.multilist.iterate(dimension, start=current_node.next[dimension]): assert node is not None # Line 18 hypervolume += node.prev[dimension].area[dimension] * ( - node.coordinates[dimension] - - node.prev[dimension].coordinates[dimension] + node.coordinates[dimension] - node.prev[dimension].coordinates[dimension] ) # Line 19 self.reference_bounds[dimension] = node.coordinates[dimension] diff --git a/pyepal/pal/core.py b/src/pyepal/pal/core.py similarity index 96% rename from pyepal/pal/core.py rename to src/pyepal/pal/core.py index c7509a3..b92f493 100644 --- a/pyepal/pal/core.py +++ b/src/pyepal/pal/core.py @@ -21,11 +21,7 @@ import numpy as np from numba import jit -from .utils import ( - dominance_check_jitted_2, - dominance_check_jitted_3, - is_pareto_efficient, -) +from .utils import dominance_check_jitted_2, dominance_check_jitted_3, is_pareto_efficient __all__: List[str] = [] @@ -215,18 +211,14 @@ def _pareto_classify( # pylint:disable=too-many-arguments, too-many-locals, too pareto_unclassified_lows = rectangle_lows[pareto_unclassified_indices] # assuming maximization - pareto_unclassified_pessimistic_mask = is_pareto_efficient( - -pareto_unclassified_lows - ) + pareto_unclassified_pessimistic_mask = is_pareto_efficient(-pareto_unclassified_lows) original_indices = pareto_unclassified_indices[pareto_unclassified_pessimistic_mask] pareto_unclassified_pessimistic_points = pareto_unclassified_lows[ pareto_unclassified_pessimistic_mask ] if is_fixed_epsilon: - tolerances_1 = np.tile( - epsilon, (len(pareto_unclassified_pessimistic_points), 1) - ) + tolerances_1 = np.tile(epsilon, (len(pareto_unclassified_pessimistic_points), 1)) else: tolerances_1 = epsilon * np.abs(pareto_unclassified_pessimistic_points) @@ -316,14 +308,10 @@ def _get_max_wt( # pylint:disable=too-many-arguments # is chosen as the next sample xt to be evaluated. # Intuitively, this rule biases the sampling towards exploring, # and thus improving the model for, the points most likely to be Pareto-optimal. - if ((unclassified_t[i] == 1) or (pareto_optimal_t[i] == 1)) and not sampled[ - i - ] == 1: + if ((unclassified_t[i] == 1) or (pareto_optimal_t[i] == 1)) and not sampled[i] == 1: # weight is the length of the diagonal of the uncertainty region if use_coef_var: - uncer = np.divide( - rectangle_ups[i, :] - rectangle_lows[i, :], means[i, :] - ) + uncer = np.divide(rectangle_ups[i, :] - rectangle_lows[i, :], means[i, :]) else: uncer = rectangle_ups[i, :] - rectangle_lows[i, :] @@ -378,9 +366,7 @@ def _get_max_wt_all( # pylint:disable=too-many-arguments if not sampled[i] == 1: # weight is the length of the diagonal of the uncertainty region if use_coef_var: - uncer = np.divide( - rectangle_ups[i, :] - rectangle_lows[i, :], means[i, :] - ) + uncer = np.divide(rectangle_ups[i, :] - rectangle_lows[i, :], means[i, :]) else: uncer = rectangle_ups[i, :] - rectangle_lows[i, :] uncertainty = _pool(uncer, pooling_method) diff --git a/pyepal/pal/pal_base.py b/src/pyepal/pal/pal_base.py similarity index 94% rename from pyepal/pal/pal_base.py rename to src/pyepal/pal/pal_base.py index 198ef08..fcf44de 100644 --- a/pyepal/pal/pal_base.py +++ b/src/pyepal/pal/pal_base.py @@ -167,9 +167,7 @@ def _reset(self): # self.y is what needs to be used for train/predict # as there the data has been turned into maximization # self._y contains the data as provided by the user - self.y = np.zeros( - (self.number_design_points, self.ndim) - ) # pylint:disable=invalid-name + self.y = np.zeros((self.number_design_points, self.ndim)) # pylint:disable=invalid-name self._y = self.y # measurement_uncertainty is provided in update_train_set by the user self.measurement_uncertainty = np.zeros((self.number_design_points, self.ndim)) @@ -340,9 +338,7 @@ def _crossvalidate(self): self._set_hyperparameters() self._train() self._predict() - error = mean_absolute_error( - self.y[sampled_idx], self._means[sampled_idx] - ) + error = mean_absolute_error(self.y[sampled_idx], self._means[sampled_idx]) errors.append(error) self.sampled = sampled_original @@ -378,11 +374,7 @@ def _update_hyperrectangles(self, new_indices: np.ndarray = None): ) else: not_new = np.array( - [ - i - for i in range(self.number_design_points) - if i not in new_indices - ] + [i for i in range(self.number_design_points) if i not in new_indices] ) self.rectangle_lows[new_indices] = lows[new_indices] self.rectangle_ups[new_indices] = ups[new_indices] @@ -403,14 +395,10 @@ def _update_coef_var_mask(self): if self._means.sum() != 0: _means_no_zero = self._means.copy() _means_no_zero[_means_no_zero == 0] = np.median(_means_no_zero) - self.coef_var_mask = ( - np.max(self.std / _means_no_zero, axis=1) < self.coef_var_threshold - ) + self.coef_var_mask = np.max(self.std / _means_no_zero, axis=1) < self.coef_var_threshold else: mean_variation = self.std.mean() - self.coef_var_mask = ( - np.max(self.std / mean_variation, axis=1) < self.coef_var_threshold - ) + self.coef_var_mask = np.max(self.std / mean_variation, axis=1) < self.coef_var_threshold def _classify(self): self._update_coef_var_mask() @@ -441,9 +429,7 @@ def _classify(self): self.discarded[self.coef_var_mask] = discarded self.unclassified[self.coef_var_mask] = unclassified - def _replace_by_measurements( - self, replace_mean: bool = True, replace_std: bool = True - ): + def _replace_by_measurements(self, replace_mean: bool = True, replace_std: bool = True): """Implements one "trick". Instead of using the GPR predictions for the sampled points we use the data that was actually measured and the actual uncertainty.""" @@ -503,9 +489,7 @@ def run_one_step( # pylint:disable=too-many-arguments self._predict() self._update_beta() - self._replace_by_measurements( - replace_mean=replace_mean, replace_std=replace_std - ) + self._replace_by_measurements(replace_mean=replace_mean, replace_std=replace_std) self._update_hyperrectangles() self._classify() samples = np.array([], dtype=np.int) @@ -620,16 +604,12 @@ def augment_design_space( # pylint: disable=invalid-name """ if self.iteration <= 1: - raise ValueError( - "You must run a iteration before you augment the design space" - ) + raise ValueError("You must run a iteration before you augment the design space") number_old_points = self.number_design_points number_new_points = len(X_design) - assert isinstance( - X_design, np.ndarray - ), "You must provide a two-dimensional numpy array" + assert isinstance(X_design, np.ndarray), "You must provide a two-dimensional numpy array" assert X_design.ndim == 2, "You must provide a two-dimensional numpy array" if X_design.shape[1] != self.design_space.shape[1]: @@ -653,32 +633,22 @@ def augment_design_space( # pylint: disable=invalid-name self.pareto_optimal = np.append( self.pareto_optimal, np.array([False] * number_new_points), 0 ) - self.discarded = np.append( - self.discarded, np.array([False] * number_new_points), 0 - ) + self.discarded = np.append(self.discarded, np.array([False] * number_new_points), 0) self.sampled = np.append( self.sampled, np.array([[False] * self.ndim] * number_new_points), 0 ) - self.unclassified = np.append( - self.unclassified, np.array([True] * number_new_points), 0 - ) + self.unclassified = np.append(self.unclassified, np.array([True] * number_new_points), 0) self.rectangle_ups = np.append( self.rectangle_ups, np.full([number_new_points, self.ndim], np.nan), 0 ) self.rectangle_lows = np.append( self.rectangle_lows, np.full([number_new_points, self.ndim], np.nan), 0 ) - self.coef_var_mask = np.append( - self.coef_var_mask, np.array([True] * number_new_points), 0 - ) + self.coef_var_mask = np.append(self.coef_var_mask, np.array([True] * number_new_points), 0) # _means/std are the model predictions - self._means = np.append( - self._means, np.full([number_new_points, self.ndim], np.nan), 0 - ) - self.std = np.append( - self.std, np.full([number_new_points, self.ndim], np.nan), 0 - ) + self._means = np.append(self._means, np.full([number_new_points, self.ndim], np.nan), 0) + self.std = np.append(self.std, np.full([number_new_points, self.ndim], np.nan), 0) # self.y is what needs to be used for train/predict # as there the data has been turned into maximization diff --git a/pyepal/pal/pal_coregionalized.py b/src/pyepal/pal/pal_coregionalized.py similarity index 88% rename from pyepal/pal/pal_coregionalized.py rename to src/pyepal/pal/pal_coregionalized.py index 3f67255..f81f34f 100644 --- a/pyepal/pal/pal_coregionalized.py +++ b/src/pyepal/pal/pal_coregionalized.py @@ -59,19 +59,13 @@ def __init__(self, *args, **kwargs): self.restarts = kwargs.pop("restarts", 20) self.parallel = kwargs.pop("parallel", False) - assert isinstance( - self.parallel, bool - ), "the parallel keyword must be of type bool" - assert isinstance( - self.restarts, int - ), "the restarts keyword must be of type int" + assert isinstance(self.parallel, bool), "the parallel keyword must be of type bool" + assert isinstance(self.restarts, int), "the restarts keyword must be of type int" super().__init__(*args, **kwargs) validate_coregionalized_gpy(self.models) def _set_data(self): - from ..models.gpr import ( # pylint:disable=import-outside-toplevel - set_xy_coregionalized, - ) + from ..models.gpr import set_xy_coregionalized # pylint:disable=import-outside-toplevel self.models[0] = set_xy_coregionalized( self.models[0], @@ -84,9 +78,7 @@ def _train(self): pass def _predict(self): - from ..models.gpr import ( # pylint:disable=import-outside-toplevel - predict_coregionalized, - ) + from ..models.gpr import predict_coregionalized # pylint:disable=import-outside-toplevel means, stds = [], [] for i in range(self.ndim): diff --git a/pyepal/pal/pal_finite_ensemble.py b/src/pyepal/pal/pal_finite_ensemble.py similarity index 93% rename from pyepal/pal/pal_finite_ensemble.py rename to src/pyepal/pal/pal_finite_ensemble.py index d18fae0..3d75a89 100644 --- a/pyepal/pal/pal_finite_ensemble.py +++ b/src/pyepal/pal/pal_finite_ensemble.py @@ -23,13 +23,9 @@ import numpy as np from sklearn.preprocessing import StandardScaler -from ..models.nt import JaxOptimizer, NTModel from .pal_base import PALBase -from .validate_inputs import ( - validate_nt_models, - validate_optimizers, - validate_positive_integer_list, -) +from .validate_inputs import validate_nt_models, validate_optimizers, validate_positive_integer_list +from ..models.nt import JaxOptimizer, NTModel # Again, the idea of having the core as pure functions outside of the class is that @@ -63,9 +59,7 @@ def train_network(key): opt_state = optimizer.opt_init(params) for j in range(training_steps[i]): - opt_state = optimizer.opt_update( - j, grad_loss(opt_state, x_train, y_train), opt_state - ) + opt_state = optimizer.opt_update(j, grad_loss(opt_state, x_train, y_train), opt_state) return optimizer.get_params(opt_state) @@ -139,9 +133,7 @@ def __init__(self, *args, **kwargs): """ from jax import random # pylint:disable=import-outside-toplevel - self.optimizers = validate_optimizers( - kwargs.pop("optimizers"), kwargs.get("ndim") - ) + self.optimizers = validate_optimizers(kwargs.pop("optimizers"), kwargs.get("ndim")) self.training_steps = validate_positive_integer_list( kwargs.pop("training_steps", 500), kwargs.get("ndim") @@ -177,9 +169,7 @@ def _train(self): def _predict(self): means, stds = [], [] for i in range(len(self.models)): - mean, std = _ensemble_predict_one_finite_width( - i, self.models, self.design_space - ) + mean, std = _ensemble_predict_one_finite_width(i, self.models, self.design_space) means.append(mean.reshape(-1, 1)) stds.append(std.reshape(-1, 1)) diff --git a/pyepal/pal/pal_gbdt.py b/src/pyepal/pal/pal_gbdt.py similarity index 100% rename from pyepal/pal/pal_gbdt.py rename to src/pyepal/pal/pal_gbdt.py diff --git a/pyepal/pal/pal_gpflowgpr.py b/src/pyepal/pal/pal_gpflowgpr.py similarity index 100% rename from pyepal/pal/pal_gpflowgpr.py rename to src/pyepal/pal/pal_gpflowgpr.py diff --git a/pyepal/pal/pal_gpy.py b/src/pyepal/pal/pal_gpy.py similarity index 91% rename from pyepal/pal/pal_gpy.py rename to src/pyepal/pal/pal_gpy.py index 088b4a0..283cb4f 100644 --- a/pyepal/pal/pal_gpy.py +++ b/src/pyepal/pal/pal_gpy.py @@ -61,16 +61,12 @@ def __init__(self, *args, **kwargs): n_jobs (int): Number of parallel processes that are used to fit the GPR models. Defaults to 1. """ - from .validate_inputs import ( # pylint:disable=import-outside-toplevel - validate_gpy_model, - ) + from .validate_inputs import validate_gpy_model # pylint:disable=import-outside-toplevel self.restarts = kwargs.pop("restarts", 20) self.n_jobs = validate_njobs(kwargs.pop("n_jobs", 1)) - assert isinstance( - self.restarts, int - ), "the restarts keyword must be of type int" + assert isinstance(self.restarts, int), "the restarts keyword must be of type int" super().__init__(*args, **kwargs) validate_number_models(self.models, self.ndim) @@ -105,9 +101,7 @@ def _set_hyperparameters(self): train_model_pickleable_partial = partial( _train_model_picklable, models=self.models, restarts=self.restarts ) - with concurrent.futures.ProcessPoolExecutor( - max_workers=self.n_jobs - ) as executor: + with concurrent.futures.ProcessPoolExecutor(max_workers=self.n_jobs) as executor: for model in executor.map(train_model_pickleable_partial, range(self.ndim)): models.append(model) self.models = models diff --git a/pyepal/pal/pal_neural_tangent.py b/src/pyepal/pal/pal_neural_tangent.py similarity index 100% rename from pyepal/pal/pal_neural_tangent.py rename to src/pyepal/pal/pal_neural_tangent.py index 6415ee7..46dc2c3 100644 --- a/pyepal/pal/pal_neural_tangent.py +++ b/src/pyepal/pal/pal_neural_tangent.py @@ -31,9 +31,9 @@ import numpy as np from sklearn.preprocessing import StandardScaler -from ..models.nt import NTModel from .pal_base import PALBase from .validate_inputs import validate_nt_models +from ..models.nt import NTModel __all__ = ["PALNT", "NTModel"] diff --git a/pyepal/pal/pal_sklearn.py b/src/pyepal/pal/pal_sklearn.py similarity index 97% rename from pyepal/pal/pal_sklearn.py rename to src/pyepal/pal/pal_sklearn.py index 5647cd9..44bd46e 100644 --- a/pyepal/pal/pal_sklearn.py +++ b/src/pyepal/pal/pal_sklearn.py @@ -82,9 +82,7 @@ def _train(self): sampled=self.sampled, ) models = [] - with concurrent.futures.ProcessPoolExecutor( - max_workers=self.n_jobs - ) as executor: + with concurrent.futures.ProcessPoolExecutor(max_workers=self.n_jobs) as executor: for model in executor.map(train_single_partial, range(self.ndim)): models.append(model) self.models = models diff --git a/pyepal/pal/schedules.py b/src/pyepal/pal/schedules.py similarity index 100% rename from pyepal/pal/schedules.py rename to src/pyepal/pal/schedules.py diff --git a/pyepal/pal/utils.py b/src/pyepal/pal/utils.py similarity index 99% rename from pyepal/pal/utils.py rename to src/pyepal/pal/utils.py index bc6a35d..aa07e82 100644 --- a/pyepal/pal/utils.py +++ b/src/pyepal/pal/utils.py @@ -110,9 +110,7 @@ def is_pareto_efficient(costs: np.array, return_mask: bool = True) -> np.array: return is_efficient -def exhaust_loop( - palinstance, y: np.array, batch_size: int = 1 -): # pylint:disable=invalid-name +def exhaust_loop(palinstance, y: np.array, batch_size: int = 1): # pylint:disable=invalid-name """Helper function that takes an initialized PAL instance and loops the sampling until there is no unclassified point left. This is useful if all measurements are already taken and one @@ -177,7 +175,7 @@ def get_maxmin_samples( # pylint:disable=invalid-name metric: str = "euclidean", init: str = "mean", seed: int = None, - **kwargs + **kwargs, ) -> np.array: """Greedy maxmin sampling, also known as Kennard-Stone sampling (1). Note that a greedy sampling is not guaranteed to give the ideal solution diff --git a/pyepal/pal/validate_inputs.py b/src/pyepal/pal/validate_inputs.py similarity index 94% rename from pyepal/pal/validate_inputs.py rename to src/pyepal/pal/validate_inputs.py index 199f064..db309b1 100644 --- a/pyepal/pal/validate_inputs.py +++ b/src/pyepal/pal/validate_inputs.py @@ -152,9 +152,7 @@ def validate_epsilon(epsilon: Any, ndim: int) -> np.ndarray: return np.array([epsilon] * ndim) -def validate_goals( # pylint:disable=too-many-branches - goals: Any, ndim: int -) -> np.ndarray: +def validate_goals(goals: Any, ndim: int) -> np.ndarray: # pylint:disable=too-many-branches """Create a valid array of goals. 1 for maximization, -1 for objectives that are to be minimized. @@ -256,9 +254,7 @@ def validate_coregionalized_gpy(models: Any): ) if not isinstance(models[0], GPCoregionalizedRegression): - raise ValueError( - "Model must be a GPCoregionalized regression object from this package!" - ) + raise ValueError("Model must be a GPCoregionalized regression object from this package!") def validate_njobs(njobs: Any) -> int: @@ -301,9 +297,7 @@ def _validate_sklearn_gpr_model(model: Any) -> GaussianProcessRegressor: raise ValueError("You need to provide a GaussianProcessRegressor instance.") -def validate_sklearn_gpr_models( - models: Any, ndim: int -) -> List[GaussianProcessRegressor]: +def validate_sklearn_gpr_models(models: Any, ndim: int) -> List[GaussianProcessRegressor]: """Make sure that there is a list of GPR models, one model per objective""" validate_number_models(models, ndim) models_validated = [] @@ -385,15 +379,11 @@ def validate_optimizers(optimizers: Any, ndim: int) -> Sequence: if not isinstance(optimizers, Sequence): raise ValueError("You must have one optimizer per objective.") if not len(optimizers) == ndim: - raise ValueError( - "If you provide a sequence it must have one optimizer per objective." - ) + raise ValueError("If you provide a sequence it must have one optimizer per objective.") for optimizer in optimizers: if not _is_jaxoptimizer(optimizer): - raise ValueError( - "You need to provide a `pyepal.models.nt.JaxOptimizer` instance" - ) + raise ValueError("You need to provide a `pyepal.models.nt.JaxOptimizer` instance") return optimizers @@ -403,9 +393,7 @@ def validate_nt_models(models: Any, ndim: int) -> Sequence: from pyepal.models.nt import NTModel # pylint:disable=import-outside-toplevel if not isinstance(models, collections.Sequence): - raise ValueError( - "You need to provide a sequence of `pyepal.models.nt.NTModel` instances" - ) + raise ValueError("You need to provide a sequence of `pyepal.models.nt.NTModel` instances") for model in models: if not len(models) == ndim: @@ -448,9 +436,7 @@ def validate_ranges(ranges: Any, ndim: int) -> Union[None, np.ndarray]: return None if not len(ranges) == ndim: - raise ValueError( - "The number of elements in ranges must match the number of objectives." - ) + raise ValueError("The number of elements in ranges must match the number of objectives.") for elem in ranges: if not elem > 0: raise ValueError("Ranges must be positive.") diff --git a/pyepal/plotting/__init__.py b/src/pyepal/plotting/__init__.py similarity index 95% rename from pyepal/plotting/__init__.py rename to src/pyepal/plotting/__init__.py index d855604..84be7cd 100644 --- a/pyepal/plotting/__init__.py +++ b/src/pyepal/plotting/__init__.py @@ -65,12 +65,8 @@ def plot_bar_iterations( # pylint:disable=invalid-name # We need numpy arrays as we assume that we can add the arrays # ToDo: We could potentially cast any iterable assert isinstance(pareto_optimal, np.ndarray), "The arguments must be numpy arrays" - assert isinstance( - non_pareto_points, np.ndarray - ), "The arguments must be numpy arrays" - assert isinstance( - unclassified_points, np.ndarray - ), "The arguments must be numpy arrays" + assert isinstance(non_pareto_points, np.ndarray), "The arguments must be numpy arrays" + assert isinstance(unclassified_points, np.ndarray), "The arguments must be numpy arrays" if ax is None: _, ax = plt.subplots(1, 1) @@ -120,11 +116,7 @@ def plot_pareto_front_2d( # pylint:disable=too-many-arguments, invalid-name for array in [y_0, y_1, std_0, std_1]: assert isinstance(array, np.ndarray), "array must be a numpy array" assert ( - len(y_0) - == len(y_1) - == len(std_0) - == len(std_1) - == palinstance.number_design_points + len(y_0) == len(y_1) == len(std_0) == len(std_1) == palinstance.number_design_points ), "Make sure that the arrays have the same length" if ax is None: @@ -172,9 +164,7 @@ def plot_pareto_front_2d( # pylint:disable=too-many-arguments, invalid-name return ax -def plot_histogram( - y: np.ndarray, palinstance: PALBase, ax=None -): # pylint:disable=invalid-name +def plot_histogram(y: np.ndarray, palinstance: PALBase, ax=None): # pylint:disable=invalid-name """Plot histograms, with maxima scaled to one and different categories indicated in color for one objective @@ -328,11 +318,7 @@ def plot_jointplot( # pylint:disable=invalid-name y.shape[1] == palinstance.ndim ), "y needs to be a two-dimensional array which column number \ equals the number of targets" - if ( - (palinstance.means is None) - or (palinstance.std is None) - or (palinstance.beta is None) - ): + if (palinstance.means is None) or (palinstance.std is None) or (palinstance.beta is None): raise ValueError( "Predicted means is None. Execute run_one_step() \ to obtain predicted means for each model." @@ -414,9 +400,7 @@ def plot_learning_curve( # pylint:disable=dangerous-default-value, too-many-arg """ if indices is None: indices = np.arange(0, len(observations)) - assert len(indices) == len( - observations - ), "The number of indices and observations must be equal" + assert len(indices) == len(observations), "The number of indices and observations must be equal" assert len(indices) > 5, "You need to use at least five points" grid = np.linspace(5, len(observations), num_steps, dtype=np.int8) grid = np.unique(grid) diff --git a/src/pyepal/version.py b/src/pyepal/version.py new file mode 100644 index 0000000..859427a --- /dev/null +++ b/src/pyepal/version.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- + +"""Version information for :mod:`pyepal`. + +Run with ``python -m pyepal.version`` +""" + +import os +from subprocess import CalledProcessError, check_output # noqa: S404 + +__all__ = [ + "VERSION", + "get_version", + "get_git_hash", +] + +VERSION = "0.7.0-dev" + + +def get_git_hash() -> str: + """Get the :mod:`pyepal` git hash.""" + with open(os.devnull, "w") as devnull: + try: + ret = check_output( # noqa: S603,S607 + ["git", "rev-parse", "HEAD"], + cwd=os.path.dirname(__file__), + stderr=devnull, + ) + except CalledProcessError: + return "UNHASHED" + else: + return ret.strip().decode("utf-8")[:8] + + +def get_version(with_git_hash: bool = False): + """Get the :mod:`pyepal` version string, including a git hash.""" + return f"{VERSION}-{get_git_hash()}" if with_git_hash else VERSION + + +if __name__ == "__main__": + print(get_version(with_git_hash=True)) # noqa:T001 diff --git a/tests/conftest.py b/tests/conftest.py index ee3d1f6..e7ccb2f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -47,14 +47,10 @@ def binh_korn_points_finer(): @pytest.fixture() def make_random_dataset(targets=3): """Make a dataset with three targets""" - return make_regression( - n_samples=100, n_features=10, n_informative=8, n_targets=targets - ) + return make_regression(n_samples=100, n_features=10, n_informative=8, n_targets=targets) @pytest.fixture() def make_one_dim_test(): """Make a dataset with one target""" - return make_regression( - n_samples=100, n_features=1, n_informative=1, n_targets=1, noise=0 - ) + return make_regression(n_samples=100, n_features=1, n_informative=1, n_targets=1, noise=0) diff --git a/tests/test_pal_core.py b/tests/test_pal_core.py index aacfd90..8353fcb 100644 --- a/tests/test_pal_core.py +++ b/tests/test_pal_core.py @@ -212,9 +212,7 @@ def test_pareto_classify(binh_korn_points): # pylint:disable=too-many-locals discarded_points = np.array([[0.5, 0.5]]) unclassified_points = np.array([[3.8, 2.1], [2.4, 0.5], [2.4, 0.5], [0.5, 0.5]]) - design_space = np.vstack( - [pareto_optimal_points, discarded_points, unclassified_points] - ) + design_space = np.vstack([pareto_optimal_points, discarded_points, unclassified_points]) is_pareto_optimal = np.array( [True] * len(pareto_optimal_points) @@ -262,15 +260,11 @@ def test_pareto_classify(binh_korn_points): # pylint:disable=too-many-locals ) assert ( - pareto_optimal_t - == np.array([True, True, True, False, True, False, False, False]) - ).all() - assert ( - discarded_t == np.array([False, False, False, True, False, True, True, False]) + pareto_optimal_t == np.array([True, True, True, False, True, False, False, False]) ).all() + assert (discarded_t == np.array([False, False, False, True, False, True, True, False])).all() assert ( - unclassified_t - == np.array([False, False, False, False, False, False, False, True]) + unclassified_t == np.array([False, False, False, False, False, False, False, True]) ).all() pareto_optimal_t, discarded_t, unclassified_t = _pareto_classify( @@ -284,28 +278,20 @@ def test_pareto_classify(binh_korn_points): # pylint:disable=too-many-locals ) assert ( - pareto_optimal_t - == np.array([True, True, True, False, True, False, False, False]) + pareto_optimal_t == np.array([True, True, True, False, True, False, False, False]) ).all() + assert (discarded_t == np.array([False, False, False, True, False, True, True, False])).all() assert ( - discarded_t == np.array([False, False, False, True, False, True, True, False]) - ).all() - assert ( - unclassified_t - == np.array([False, False, False, False, False, False, False, True]) + unclassified_t == np.array([False, False, False, False, False, False, False, True]) ).all() # 3D arrays, but 3rd dimenension alsways 0 pareto_optimal_points = np.array([[0.5, 2, 0], [3, 1, 0], [4, 0.5, 0]]) discarded_points = np.array([[0.5, 0.5, 0]]) - unclassified_points = np.array( - [[3.8, 2.1, 0], [2.4, 0.5, 0], [2.4, 0.5, 0], [0.5, 0.5, 0]] - ) + unclassified_points = np.array([[3.8, 2.1, 0], [2.4, 0.5, 0], [2.4, 0.5, 0], [0.5, 0.5, 0]]) - design_space = np.vstack( - [pareto_optimal_points, discarded_points, unclassified_points] - ) + design_space = np.vstack([pareto_optimal_points, discarded_points, unclassified_points]) epsilon = np.array([0, 0, 0]) @@ -335,15 +321,11 @@ def test_pareto_classify(binh_korn_points): # pylint:disable=too-many-locals ) assert ( - pareto_optimal_t - == np.array([True, True, True, False, True, False, False, False]) - ).all() - assert ( - discarded_t == np.array([False, False, False, True, False, True, True, False]) + pareto_optimal_t == np.array([True, True, True, False, True, False, False, False]) ).all() + assert (discarded_t == np.array([False, False, False, True, False, True, True, False])).all() assert ( - unclassified_t - == np.array([False, False, False, False, False, False, False, True]) + unclassified_t == np.array([False, False, False, False, False, False, False, True]) ).all() # Now, also test it on a real Pareto frontier @@ -404,9 +386,7 @@ def test_pareto_classify(binh_korn_points): # pylint:disable=too-many-locals ) scale = np.array([1, 1000, 0.0001, 2]) - means = np.array( - [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] - ) + means = np.array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]) pareto_optimal = np.array([False, False, True, True, True]) sampled = np.array([False, False, False, False, False]) unclassified = np.array([True, True, False, False, False]) @@ -442,9 +422,7 @@ def test__uncertainty(): [2.0, 2.0, 2.0, 2.0], ] ) - means = np.array( - [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] - ) + means = np.array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]) uncertainites = _uncertainty(highs, lows, means) assert len(uncertainites) == len(means) diff --git a/tests/test_pal_coregionalized.py b/tests/test_pal_coregionalized.py index 6ad7288..6a99fa8 100644 --- a/tests/test_pal_coregionalized.py +++ b/tests/test_pal_coregionalized.py @@ -33,19 +33,13 @@ def test_pal_coregionalized(make_random_dataset): slice_idx = np.array([0, 1, 2, 3, 4]) pal_coregionalized.update_train_set(slice_idx, y[slice_idx]) - assert ( - pal_coregionalized.models[0].kern.B.kappa.values == np.array([0.5, 0.5, 0.5]) - ).all() + assert (pal_coregionalized.models[0].kern.B.kappa.values == np.array([0.5, 0.5, 0.5])).all() pal_coregionalized._set_hyperparameters() # pylint:disable=protected-access - assert ( - pal_coregionalized._should_optimize_hyperparameters() # pylint:disable=protected-access - ) + assert pal_coregionalized._should_optimize_hyperparameters() # pylint:disable=protected-access - assert ( - pal_coregionalized.models[0].kern.B.kappa.values != np.array([0.5, 0.5, 0.5]) - ).any() + assert (pal_coregionalized.models[0].kern.B.kappa.values != np.array([0.5, 0.5, 0.5])).any() def test_orchestration_run_one_step(make_random_dataset, binh_korn_points): @@ -113,9 +107,7 @@ def test_orchestration_run_one_step_missing_data(binh_korn_points): sample_idx = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 50, 60, 70]) - model = build_coregionalized_model( - X_binh_korn[sample_idx], y_binh_korn[sample_idx], 0 - ) + model = build_coregionalized_model(X_binh_korn[sample_idx], y_binh_korn[sample_idx], 0) palinstance = PALCoregionalized( X_binh_korn, [model], 2, beta_scale=1, epsilon=0.01, delta=0.01, restarts=3 diff --git a/tests/test_pal_gpflowgpr.py b/tests/test_pal_gpflowgpr.py index 6f7d0b5..90807f1 100644 --- a/tests/test_pal_gpflowgpr.py +++ b/tests/test_pal_gpflowgpr.py @@ -27,9 +27,7 @@ def test_pal_gpflow(binh_korn_points): X_binh_korn = ( # pylint:disable=invalid-name X_binh_korn - X_binh_korn.mean() ) / X_binh_korn.std() # pylint:disable=invalid-name - y_binh_korn = ( - y_binh_korn - y_binh_korn.mean() - ) / y_binh_korn.std() + 0.01 * np.random.rand() + y_binh_korn = (y_binh_korn - y_binh_korn.mean()) / y_binh_korn.std() + 0.01 * np.random.rand() def build_model(x, y): # pylint:disable=invalid-name k = gpflow.kernels.RationalQuadratic() diff --git a/tests/test_pal_gpy.py b/tests/test_pal_gpy.py index 82003c8..8ecb77a 100644 --- a/tests/test_pal_gpy.py +++ b/tests/test_pal_gpy.py @@ -42,9 +42,7 @@ def test_pal_gpy(make_random_dataset): palgpy_instance.cross_val_points = 0 assert palgpy_instance.restarts == 20 - palgpy_instance.update_train_set( - np.array([1, 2, 3, 4, 5]), y[np.array([1, 2, 3, 4, 5]), :] - ) + palgpy_instance.update_train_set(np.array([1, 2, 3, 4, 5]), y[np.array([1, 2, 3, 4, 5]), :]) assert palgpy_instance.models[0].kern.variance.values[0] == 1 palgpy_instance._train() # pylint:disable=protected-access assert palgpy_instance.models[0].kern.variance.values[0] == 1 @@ -371,11 +369,5 @@ def test_epsilon_sensitivity(binh_korn_points): assert palinstance1.number_discarded_points == 0 assert palinstance2.number_discarded_points == 0 - assert ( - palinstance0.number_unclassified_points - > palinstance1.number_unclassified_points - ) - assert ( - palinstance1.number_unclassified_points - > palinstance2.number_unclassified_points - ) + assert palinstance0.number_unclassified_points > palinstance1.number_unclassified_points + assert palinstance1.number_unclassified_points > palinstance2.number_unclassified_points diff --git a/tests/test_pal_neural_tangent.py b/tests/test_pal_neural_tangent.py index 4aa856b..5b0ab82 100644 --- a/tests/test_pal_neural_tangent.py +++ b/tests/test_pal_neural_tangent.py @@ -30,9 +30,7 @@ def test_run_one_step(binh_korn_points): # We create one model per objective model_tuple_1 = build_dense_network([128]) model_tuple_2 = build_dense_network([128]) - palinstance = PALNT( - X, [model_tuple_1, model_tuple_2], 2, beta_scale=1, kernel="ntk" - ) + palinstance = PALNT(X, [model_tuple_1, model_tuple_2], 2, beta_scale=1, kernel="ntk") palinstance.cross_val_points = 0 sample_idx = get_kmeans_samples(X, 5) @@ -48,9 +46,7 @@ def test_run_one_step(binh_korn_points): # We create one model per objective model_tuple_1 = build_dense_network([128]) model_tuple_2 = build_dense_network([128]) - palinstance = PALNT( - X, [model_tuple_1, model_tuple_2], 2, beta_scale=1, kernel="nngp" - ) + palinstance = PALNT(X, [model_tuple_1, model_tuple_2], 2, beta_scale=1, kernel="nngp") palinstance.cross_val_points = 0 sample_idx = get_kmeans_samples(X, 5) diff --git a/tests/test_pal_sklearn.py b/tests/test_pal_sklearn.py index 23c8e28..6cc5a65 100644 --- a/tests/test_pal_sklearn.py +++ b/tests/test_pal_sklearn.py @@ -43,24 +43,16 @@ def test_gridsearch_object(binh_korn_points): """Test the initialization of PALSklearn with a GridsearchCV object""" X_binh_korn, y_binh_korn = binh_korn_points # pylint:disable=invalid-name sample_idx = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 50, 60, 70]) - grid_search_0 = GridSearchCV( - GaussianProcessRegressor(), {"kernel": [RBF(), Matern()]} - ) - grid_search_1 = GridSearchCV( - GaussianProcessRegressor(), {"kernel": [RBF(), Matern()]} - ) + grid_search_0 = GridSearchCV(GaussianProcessRegressor(), {"kernel": [RBF(), Matern()]}) + grid_search_1 = GridSearchCV(GaussianProcessRegressor(), {"kernel": [RBF(), Matern()]}) with pytest.raises(ValueError): - palinstance = PALSklearn( - X_binh_korn, [grid_search_0, grid_search_1], 2, beta_scale=1 - ) + palinstance = PALSklearn(X_binh_korn, [grid_search_0, grid_search_1], 2, beta_scale=1) grid_search_0.fit(X_binh_korn, y_binh_korn[:, 0]) grid_search_1.fit(X_binh_korn, y_binh_korn[:, 1]) - palinstance = PALSklearn( - X_binh_korn, [grid_search_0, grid_search_1], 2, beta_scale=1 - ) + palinstance = PALSklearn(X_binh_korn, [grid_search_0, grid_search_1], 2, beta_scale=1) palinstance.cross_val_points = 0 palinstance.update_train_set(sample_idx, y_binh_korn[sample_idx]) diff --git a/tests/test_plotting.py b/tests/test_plotting.py index ee8601b..ee6b625 100644 --- a/tests/test_plotting.py +++ b/tests/test_plotting.py @@ -40,9 +40,7 @@ def test_plot_bar_iterations(): with pytest.raises(AssertionError): plot_bar_iterations(np.array([1, 1, 2]), np.array([2, 2, 2]), np.array([3, 3])) - axis = plot_bar_iterations( - np.array([1, 1, 2]), np.array([1, 1, 2]), np.array([1, 1, 2]) - ) + axis = plot_bar_iterations(np.array([1, 1, 2]), np.array([1, 1, 2]), np.array([1, 1, 2])) assert isinstance(axis, matplotlib.axes.Axes) diff --git a/tests/test_validate_inputs.py b/tests/test_validate_inputs.py index b1f029d..6da05ad 100644 --- a/tests/test_validate_inputs.py +++ b/tests/test_validate_inputs.py @@ -186,13 +186,9 @@ def test__validate_sklearn_gpr_model(make_random_dataset): assert _validate_sklearn_gpr_model(gpr) == gpr - grid_search = GridSearchCV( - GaussianProcessRegressor(), {"kernel": [RBF(), Matern()]} - ) + grid_search = GridSearchCV(GaussianProcessRegressor(), {"kernel": [RBF(), Matern()]}) - random_search = RandomizedSearchCV( - GaussianProcessRegressor(), {"kernel": [RBF(), Matern()]} - ) + random_search = RandomizedSearchCV(GaussianProcessRegressor(), {"kernel": [RBF(), Matern()]}) grid_search.fit(X, y) random_search.fit(X, y) @@ -205,9 +201,7 @@ def test__validate_sklearn_gpr_model(make_random_dataset): y_classification, ) = make_classification() - grid_search_class = GridSearchCV( - GaussianProcessClassifier(), {"kernel": [RBF(), Matern()]} - ) + grid_search_class = GridSearchCV(GaussianProcessClassifier(), {"kernel": [RBF(), Matern()]}) random_search_class = RandomizedSearchCV( GaussianProcessClassifier(), {"kernel": [RBF(), Matern()]} ) @@ -280,9 +274,7 @@ def test_validate_optimizers(): def test_validate_nt_models(): """Test that we correctly validate the sequence of NTModel""" - from pyepal.models.nt import ( # pylint:disable=import-outside-toplevel - build_dense_network, - ) + from pyepal.models.nt import build_dense_network # pylint:disable=import-outside-toplevel with pytest.raises(ValueError): validate_nt_models(["a", "b"], 2) @@ -290,14 +282,7 @@ def test_validate_nt_models(): with pytest.raises(ValueError): validate_nt_models([build_dense_network([512])], 2) - assert ( - len( - validate_nt_models( - [build_dense_network([512]), build_dense_network([512])], 2 - ) - ) - == 2 - ) + assert len(validate_nt_models([build_dense_network([512]), build_dense_network([512])], 2)) == 2 def test_validate_positive_integer_list(): diff --git a/versioneer.py b/versioneer.py deleted file mode 100644 index 37f868a..0000000 --- a/versioneer.py +++ /dev/null @@ -1,1823 +0,0 @@ -# -*- coding: utf-8 -*- - -# Version: 0.18 - -"""The Versioneer - like a rocketeer, but for versions. - -The Versioneer -============== - -* like a rocketeer, but for versions! -* https://github.com/warner/python-versioneer -* Brian Warner -* License: Public Domain -* Compatible With: python2.6, 2.7, 3.2, 3.3, 3.4, 3.5, 3.6, and pypy -* [![Latest Version] -(https://pypip.in/version/versioneer/badge.svg?style=flat) -](https://pypi.python.org/pypi/versioneer/) -* [![Build Status] -(https://travis-ci.org/warner/python-versioneer.png?branch=master) -](https://travis-ci.org/warner/python-versioneer) - -This is a tool for managing a recorded version number in distutils-based -python projects. The goal is to remove the tedious and error-prone "update -the embedded version string" step from your release process. Making a new -release should be as easy as recording a new tag in your version-control -system, and maybe making new tarballs. - - -## Quick Install - -* `pip install versioneer` to somewhere to your $PATH -* add a `[versioneer]` section to your setup.cfg (see below) -* run `versioneer install` in your source tree, commit the results - -## Version Identifiers - -Source trees come from a variety of places: - -* a version-control system checkout (mostly used by developers) -* a nightly tarball, produced by build automation -* a snapshot tarball, produced by a web-based VCS browser, like github's - "tarball from tag" feature -* a release tarball, produced by "setup.py sdist", distributed through PyPI - -Within each source tree, the version identifier (either a string or a number, -this tool is format-agnostic) can come from a variety of places: - -* ask the VCS tool itself, e.g. "git describe" (for checkouts), which knows - about recent "tags" and an absolute revision-id -* the name of the directory into which the tarball was unpacked -* an expanded VCS keyword ($Id$, etc) -* a `_version.py` created by some earlier build step - -For released software, the version identifier is closely related to a VCS -tag. Some projects use tag names that include more than just the version -string (e.g. "myproject-1.2" instead of just "1.2"), in which case the tool -needs to strip the tag prefix to extract the version identifier. For -unreleased software (between tags), the version identifier should provide -enough information to help developers recreate the same tree, while also -giving them an idea of roughly how old the tree is (after version 1.2, before -version 1.3). Many VCS systems can report a description that captures this, -for example `git describe --tags --dirty --always` reports things like -"0.7-1-g574ab98-dirty" to indicate that the checkout is one revision past the -0.7 tag, has a unique revision id of "574ab98", and is "dirty" (it has -uncommitted changes. - -The version identifier is used for multiple purposes: - -* to allow the module to self-identify its version: `myproject.__version__` -* to choose a name and prefix for a 'setup.py sdist' tarball - -## Theory of Operation - -Versioneer works by adding a special `_version.py` file into your source -tree, where your `__init__.py` can import it. This `_version.py` knows how to -dynamically ask the VCS tool for version information at import time. - -`_version.py` also contains `$Revision$` markers, and the installation -process marks `_version.py` to have this marker rewritten with a tag name -during the `git archive` command. As a result, generated tarballs will -contain enough information to get the proper version. - -To allow `setup.py` to compute a version too, a `versioneer.py` is added to -the top level of your source tree, next to `setup.py` and the `setup.cfg` -that configures it. This overrides several distutils/setuptools commands to -compute the version when invoked, and changes `setup.py build` and `setup.py -sdist` to replace `_version.py` with a small static file that contains just -the generated version data. - -## Installation - -See [INSTALL.md](./INSTALL.md) for detailed installation instructions. - -## Version-String Flavors - -Code which uses Versioneer can learn about its version string at runtime by -importing `_version` from your main `__init__.py` file and running the -`get_versions()` function. From the "outside" (e.g. in `setup.py`), you can -import the top-level `versioneer.py` and run `get_versions()`. - -Both functions return a dictionary with different flavors of version -information: - -* `['version']`: A condensed version string, rendered using the selected - style. This is the most commonly used value for the project's version - string. The default "pep440" style yields strings like `0.11`, - `0.11+2.g1076c97`, or `0.11+2.g1076c97.dirty`. See the "Styles" section - below for alternative styles. - -* `['full-revisionid']`: detailed revision identifier. For Git, this is the - full SHA1 commit id, e.g. "1076c978a8d3cfc70f408fe5974aa6c092c949ac". - -* `['date']`: Date and time of the latest `HEAD` commit. For Git, it is the - commit date in ISO 8601 format. This will be None if the date is not - available. - -* `['dirty']`: a boolean, True if the tree has uncommitted changes. Note that - this is only accurate if run in a VCS checkout, otherwise it is likely to - be False or None - -* `['error']`: if the version string could not be computed, this will be set - to a string describing the problem, otherwise it will be None. It may be - useful to throw an exception in setup.py if this is set, to avoid e.g. - creating tarballs with a version string of "unknown". - -Some variants are more useful than others. Including `full-revisionid` in a -bug report should allow developers to reconstruct the exact code being tested -(or indicate the presence of local changes that should be shared with the -developers). `version` is suitable for display in an "about" box or a CLI -`--version` output: it can be easily compared against release notes and lists -of bugs fixed in various releases. - -The installer adds the following text to your `__init__.py` to place a basic -version in `YOURPROJECT.__version__`: - - from ._version import get_versions - __version__ = get_versions()['version'] - del get_versions - -## Styles - -The setup.cfg `style=` configuration controls how the VCS information is -rendered into a version string. - -The default style, "pep440", produces a PEP440-compliant string, equal to the -un-prefixed tag name for actual releases, and containing an additional "local -version" section with more detail for in-between builds. For Git, this is -TAG[+DISTANCE.gHEX[.dirty]] , using information from `git describe --tags ---dirty --always`. For example "0.11+2.g1076c97.dirty" indicates that the -tree is like the "1076c97" commit but has uncommitted changes (".dirty"), and -that this commit is two revisions ("+2") beyond the "0.11" tag. For released -software (exactly equal to a known tag), the identifier will only contain the -stripped tag, e.g. "0.11". - -Other styles are available. See [details.md](details.md) in the Versioneer -source tree for descriptions. - -## Debugging - -Versioneer tries to avoid fatal errors: if something goes wrong, it will tend -to return a version of "0+unknown". To investigate the problem, run `setup.py -version`, which will run the version-lookup code in a verbose mode, and will -display the full contents of `get_versions()` (including the `error` string, -which may help identify what went wrong). - -## Known Limitations - -Some situations are known to cause problems for Versioneer. This details the -most significant ones. More can be found on Github -[issues page](https://github.com/warner/python-versioneer/issues). - -### Subprojects - -Versioneer has limited support for source trees in which `setup.py` is not in -the root directory (e.g. `setup.py` and `.git/` are *not* siblings). The are -two common reasons why `setup.py` might not be in the root: - -* Source trees which contain multiple subprojects, such as - [Buildbot](https://github.com/buildbot/buildbot), which contains both - "master" and "slave" subprojects, each with their own `setup.py`, - `setup.cfg`, and `tox.ini`. Projects like these produce multiple PyPI - distributions (and upload multiple independently-installable tarballs). -* Source trees whose main purpose is to contain a C library, but which also - provide bindings to Python (and perhaps other langauges) in subdirectories. - -Versioneer will look for `.git` in parent directories, and most operations -should get the right version string. However `pip` and `setuptools` have bugs -and implementation details which frequently cause `pip install .` from a -subproject directory to fail to find a correct version string (so it usually -defaults to `0+unknown`). - -`pip install --editable .` should work correctly. `setup.py install` might -work too. - -Pip-8.1.1 is known to have this problem, but hopefully it will get fixed in -some later version. - -[Bug #38](https://github.com/warner/python-versioneer/issues/38) is tracking -this issue. The discussion in -[PR #61](https://github.com/warner/python-versioneer/pull/61) describes the -issue from the Versioneer side in more detail. -[pip PR#3176](https://github.com/pypa/pip/pull/3176) and -[pip PR#3615](https://github.com/pypa/pip/pull/3615) contain work to improve -pip to let Versioneer work correctly. - -Versioneer-0.16 and earlier only looked for a `.git` directory next to the -`setup.cfg`, so subprojects were completely unsupported with those releases. - -### Editable installs with setuptools <= 18.5 - -`setup.py develop` and `pip install --editable .` allow you to install a -project into a virtualenv once, then continue editing the source code (and -test) without re-installing after every change. - -"Entry-point scripts" (`setup(entry_points={"console_scripts": ..})`) are a -convenient way to specify executable scripts that should be installed along -with the python package. - -These both work as expected when using modern setuptools. When using -setuptools-18.5 or earlier, however, certain operations will cause -`pkg_resources.DistributionNotFound` errors when running the entrypoint -script, which must be resolved by re-installing the package. This happens -when the install happens with one version, then the egg_info data is -regenerated while a different version is checked out. Many setup.py commands -cause egg_info to be rebuilt (including `sdist`, `wheel`, and installing into -a different virtualenv), so this can be surprising. - -[Bug #83](https://github.com/warner/python-versioneer/issues/83) describes -this one, but upgrading to a newer version of setuptools should probably -resolve it. - -### Unicode version strings - -While Versioneer works (and is continually tested) with both Python 2 and -Python 3, it is not entirely consistent with bytes-vs-unicode distinctions. -Newer releases probably generate unicode version strings on py2. It's not -clear that this is wrong, but it may be surprising for applications when then -write these strings to a network connection or include them in bytes-oriented -APIs like cryptographic checksums. - -[Bug #71](https://github.com/warner/python-versioneer/issues/71) investigates -this question. - - -## Updating Versioneer - -To upgrade your project to a new release of Versioneer, do the following: - -* install the new Versioneer (`pip install -U versioneer` or equivalent) -* edit `setup.cfg`, if necessary, to include any new configuration settings - indicated by the release notes. See [UPGRADING](./UPGRADING.md) for details. -* re-run `versioneer install` in your source tree, to replace - `SRC/_version.py` -* commit any changed files - -## Future Directions - -This tool is designed to make it easily extended to other version-control -systems: all VCS-specific components are in separate directories like -src/git/ . The top-level `versioneer.py` script is assembled from these -components by running make-versioneer.py . In the future, make-versioneer.py -will take a VCS name as an argument, and will construct a version of -`versioneer.py` that is specific to the given VCS. It might also take the -configuration arguments that are currently provided manually during -installation by editing setup.py . Alternatively, it might go the other -direction and include code from all supported VCS systems, reducing the -number of intermediate scripts. - - -## License - -To make Versioneer easier to embed, all its code is dedicated to the public -domain. The `_version.py` that it creates is also in the public domain. -Specifically, both are released under the Creative Commons "Public Domain -Dedication" license (CC0-1.0), as described in -https://creativecommons.org/publicdomain/zero/1.0/ . - -""" - -from __future__ import print_function -try: - import configparser -except ImportError: - import ConfigParser as configparser -import errno -import json -import os -import re -import subprocess -import sys - - -class VersioneerConfig: - """Container for Versioneer configuration parameters.""" - - -def get_root(): - """Get the project root directory. - - We require that all commands are run from the project root, i.e. the - directory that contains setup.py, setup.cfg, and versioneer.py . - """ - root = os.path.realpath(os.path.abspath(os.getcwd())) - setup_py = os.path.join(root, "setup.py") - versioneer_py = os.path.join(root, "versioneer.py") - if not (os.path.exists(setup_py) or os.path.exists(versioneer_py)): - # allow 'python path/to/setup.py COMMAND' - root = os.path.dirname(os.path.realpath(os.path.abspath(sys.argv[0]))) - setup_py = os.path.join(root, "setup.py") - versioneer_py = os.path.join(root, "versioneer.py") - if not (os.path.exists(setup_py) or os.path.exists(versioneer_py)): - err = ("Versioneer was unable to run the project root directory. " - "Versioneer requires setup.py to be executed from " - "its immediate directory (like 'python setup.py COMMAND'), " - "or in a way that lets it use sys.argv[0] to find the root " - "(like 'python path/to/setup.py COMMAND').") - raise VersioneerBadRootError(err) - try: - # Certain runtime workflows (setup.py install/develop in a setuptools - # tree) execute all dependencies in a single python process, so - # "versioneer" may be imported multiple times, and python's shared - # module-import table will cache the first one. So we can't use - # os.path.dirname(__file__), as that will find whichever - # versioneer.py was first imported, even in later projects. - me = os.path.realpath(os.path.abspath(__file__)) - me_dir = os.path.normcase(os.path.splitext(me)[0]) - vsr_dir = os.path.normcase(os.path.splitext(versioneer_py)[0]) - if me_dir != vsr_dir: - print("Warning: build in %s is using versioneer.py from %s" - % (os.path.dirname(me), versioneer_py)) - except NameError: - pass - return root - - -def get_config_from_root(root): - """Read the project setup.cfg file to determine Versioneer config.""" - # This might raise EnvironmentError (if setup.cfg is missing), or - # configparser.NoSectionError (if it lacks a [versioneer] section), or - # configparser.NoOptionError (if it lacks "VCS="). See the docstring at - # the top of versioneer.py for instructions on writing your setup.cfg . - setup_cfg = os.path.join(root, "setup.cfg") - parser = configparser.SafeConfigParser() - with open(setup_cfg, "r") as f: - parser.readfp(f) - VCS = parser.get("versioneer", "VCS") # mandatory - - def get(parser, name): - if parser.has_option("versioneer", name): - return parser.get("versioneer", name) - return None - cfg = VersioneerConfig() - cfg.VCS = VCS - cfg.style = get(parser, "style") or "" - cfg.versionfile_source = get(parser, "versionfile_source") - cfg.versionfile_build = get(parser, "versionfile_build") - cfg.tag_prefix = get(parser, "tag_prefix") - if cfg.tag_prefix in ("''", '""'): - cfg.tag_prefix = "" - cfg.parentdir_prefix = get(parser, "parentdir_prefix") - cfg.verbose = get(parser, "verbose") - return cfg - - -class NotThisMethod(Exception): - """Exception raised if a method is not valid for the current scenario.""" - - -# these dictionaries contain VCS-specific tools -LONG_VERSION_PY = {} -HANDLERS = {} - - -def register_vcs_handler(vcs, method): # decorator - """Decorator to mark a method as the handler for a particular VCS.""" - def decorate(f): - """Store f in HANDLERS[vcs][method].""" - if vcs not in HANDLERS: - HANDLERS[vcs] = {} - HANDLERS[vcs][method] = f - return f - return decorate - - -def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, - env=None): - """Call the given command(s).""" - assert isinstance(commands, list) - p = None - for c in commands: - try: - dispcmd = str([c] + args) - # remember shell=False, so use git.cmd on windows, not just git - p = subprocess.Popen([c] + args, cwd=cwd, env=env, - stdout=subprocess.PIPE, - stderr=(subprocess.PIPE if hide_stderr - else None)) - break - except EnvironmentError: - e = sys.exc_info()[1] - if e.errno == errno.ENOENT: - continue - if verbose: - print("unable to run %s" % dispcmd) - print(e) - return None, None - else: - if verbose: - print("unable to find command, tried %s" % (commands,)) - return None, None - stdout = p.communicate()[0].strip() - if sys.version_info[0] >= 3: - stdout = stdout.decode() - if p.returncode != 0: - if verbose: - print("unable to run %s (error)" % dispcmd) - print("stdout was %s" % stdout) - return None, p.returncode - return stdout, p.returncode - - -LONG_VERSION_PY['git'] = ''' -# This file helps to compute a version number in source trees obtained from -# git-archive tarball (such as those provided by githubs download-from-tag -# feature). Distribution tarballs (built by setup.py sdist) and build -# directories (produced by setup.py build) will contain a much shorter file -# that just contains the computed version number. - -# This file is released into the public domain. Generated by -# versioneer-0.18 (https://github.com/warner/python-versioneer) - -"""Git implementation of _version.py.""" - -import errno -import os -import re -import subprocess -import sys - - -def get_keywords(): - """Get the keywords needed to look up the version information.""" - # these strings will be replaced by git during git-archive. - # setup.py/versioneer.py will grep for the variable names, so they must - # each be defined on a line of their own. _version.py will just call - # get_keywords(). - git_refnames = "%(DOLLAR)sFormat:%%d%(DOLLAR)s" - git_full = "%(DOLLAR)sFormat:%%H%(DOLLAR)s" - git_date = "%(DOLLAR)sFormat:%%ci%(DOLLAR)s" - keywords = {"refnames": git_refnames, "full": git_full, "date": git_date} - return keywords - - -class VersioneerConfig: - """Container for Versioneer configuration parameters.""" - - -def get_config(): - """Create, populate and return the VersioneerConfig() object.""" - # these strings are filled in when 'setup.py versioneer' creates - # _version.py - cfg = VersioneerConfig() - cfg.VCS = "git" - cfg.style = "%(STYLE)s" - cfg.tag_prefix = "%(TAG_PREFIX)s" - cfg.parentdir_prefix = "%(PARENTDIR_PREFIX)s" - cfg.versionfile_source = "%(VERSIONFILE_SOURCE)s" - cfg.verbose = False - return cfg - - -class NotThisMethod(Exception): - """Exception raised if a method is not valid for the current scenario.""" - - -LONG_VERSION_PY = {} -HANDLERS = {} - - -def register_vcs_handler(vcs, method): # decorator - """Decorator to mark a method as the handler for a particular VCS.""" - def decorate(f): - """Store f in HANDLERS[vcs][method].""" - if vcs not in HANDLERS: - HANDLERS[vcs] = {} - HANDLERS[vcs][method] = f - return f - return decorate - - -def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, - env=None): - """Call the given command(s).""" - assert isinstance(commands, list) - p = None - for c in commands: - try: - dispcmd = str([c] + args) - # remember shell=False, so use git.cmd on windows, not just git - p = subprocess.Popen([c] + args, cwd=cwd, env=env, - stdout=subprocess.PIPE, - stderr=(subprocess.PIPE if hide_stderr - else None)) - break - except EnvironmentError: - e = sys.exc_info()[1] - if e.errno == errno.ENOENT: - continue - if verbose: - print("unable to run %%s" %% dispcmd) - print(e) - return None, None - else: - if verbose: - print("unable to find command, tried %%s" %% (commands,)) - return None, None - stdout = p.communicate()[0].strip() - if sys.version_info[0] >= 3: - stdout = stdout.decode() - if p.returncode != 0: - if verbose: - print("unable to run %%s (error)" %% dispcmd) - print("stdout was %%s" %% stdout) - return None, p.returncode - return stdout, p.returncode - - -def versions_from_parentdir(parentdir_prefix, root, verbose): - """Try to determine the version from the parent directory name. - - Source tarballs conventionally unpack into a directory that includes both - the project name and a version string. We will also support searching up - two directory levels for an appropriately named parent directory - """ - rootdirs = [] - - for i in range(3): - dirname = os.path.basename(root) - if dirname.startswith(parentdir_prefix): - return {"version": dirname[len(parentdir_prefix):], - "full-revisionid": None, - "dirty": False, "error": None, "date": None} - else: - rootdirs.append(root) - root = os.path.dirname(root) # up a level - - if verbose: - print("Tried directories %%s but none started with prefix %%s" %% - (str(rootdirs), parentdir_prefix)) - raise NotThisMethod("rootdir doesn't start with parentdir_prefix") - - -@register_vcs_handler("git", "get_keywords") -def git_get_keywords(versionfile_abs): - """Extract version information from the given file.""" - # the code embedded in _version.py can just fetch the value of these - # keywords. When used from setup.py, we don't want to import _version.py, - # so we do it with a regexp instead. This function is not used from - # _version.py. - keywords = {} - try: - f = open(versionfile_abs, "r") - for line in f.readlines(): - if line.strip().startswith("git_refnames ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["refnames"] = mo.group(1) - if line.strip().startswith("git_full ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["full"] = mo.group(1) - if line.strip().startswith("git_date ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["date"] = mo.group(1) - f.close() - except EnvironmentError: - pass - return keywords - - -@register_vcs_handler("git", "keywords") -def git_versions_from_keywords(keywords, tag_prefix, verbose): - """Get version information from git keywords.""" - if not keywords: - raise NotThisMethod("no keywords at all, weird") - date = keywords.get("date") - if date is not None: - # git-2.2.0 added "%%cI", which expands to an ISO-8601 -compliant - # datestamp. However we prefer "%%ci" (which expands to an "ISO-8601 - # -like" string, which we must then edit to make compliant), because - # it's been around since git-1.5.3, and it's too difficult to - # discover which version we're using, or to work around using an - # older one. - date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) - refnames = keywords["refnames"].strip() - if refnames.startswith("$Format"): - if verbose: - print("keywords are unexpanded, not using") - raise NotThisMethod("unexpanded keywords, not a git-archive tarball") - refs = set([r.strip() for r in refnames.strip("()").split(",")]) - # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of - # just "foo-1.0". If we see a "tag: " prefix, prefer those. - TAG = "tag: " - tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)]) - if not tags: - # Either we're using git < 1.8.3, or there really are no tags. We use - # a heuristic: assume all version tags have a digit. The old git %%d - # expansion behaves like git log --decorate=short and strips out the - # refs/heads/ and refs/tags/ prefixes that would let us distinguish - # between branches and tags. By ignoring refnames without digits, we - # filter out many common branch names like "release" and - # "stabilization", as well as "HEAD" and "master". - tags = set([r for r in refs if re.search(r'\d', r)]) - if verbose: - print("discarding '%%s', no digits" %% ",".join(refs - tags)) - if verbose: - print("likely tags: %%s" %% ",".join(sorted(tags))) - for ref in sorted(tags): - # sorting will prefer e.g. "2.0" over "2.0rc1" - if ref.startswith(tag_prefix): - r = ref[len(tag_prefix):] - if verbose: - print("picking %%s" %% r) - return {"version": r, - "full-revisionid": keywords["full"].strip(), - "dirty": False, "error": None, - "date": date} - # no suitable tags, so version is "0+unknown", but full hex is still there - if verbose: - print("no suitable tags, using unknown + full revision id") - return {"version": "0+unknown", - "full-revisionid": keywords["full"].strip(), - "dirty": False, "error": "no suitable tags", "date": None} - - -@register_vcs_handler("git", "pieces_from_vcs") -def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): - """Get version from 'git describe' in the root of the source tree. - - This only gets called if the git-archive 'subst' keywords were *not* - expanded, and _version.py hasn't already been rewritten with a short - version string, meaning we're inside a checked out source tree. - """ - GITS = ["git"] - if sys.platform == "win32": - GITS = ["git.cmd", "git.exe"] - - out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root, - hide_stderr=True) - if rc != 0: - if verbose: - print("Directory %%s not under git control" %% root) - raise NotThisMethod("'git rev-parse --git-dir' returned error") - - # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] - # if there isn't one, this yields HEX[-dirty] (no NUM) - describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty", - "--always", "--long", - "--match", "%%s*" %% tag_prefix], - cwd=root) - # --long was added in git-1.5.5 - if describe_out is None: - raise NotThisMethod("'git describe' failed") - describe_out = describe_out.strip() - full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root) - if full_out is None: - raise NotThisMethod("'git rev-parse' failed") - full_out = full_out.strip() - - pieces = {} - pieces["long"] = full_out - pieces["short"] = full_out[:7] # maybe improved later - pieces["error"] = None - - # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] - # TAG might have hyphens. - git_describe = describe_out - - # look for -dirty suffix - dirty = git_describe.endswith("-dirty") - pieces["dirty"] = dirty - if dirty: - git_describe = git_describe[:git_describe.rindex("-dirty")] - - # now we have TAG-NUM-gHEX or HEX - - if "-" in git_describe: - # TAG-NUM-gHEX - mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe) - if not mo: - # unparseable. Maybe git-describe is misbehaving? - pieces["error"] = ("unable to parse git-describe output: '%%s'" - %% describe_out) - return pieces - - # tag - full_tag = mo.group(1) - if not full_tag.startswith(tag_prefix): - if verbose: - fmt = "tag '%%s' doesn't start with prefix '%%s'" - print(fmt %% (full_tag, tag_prefix)) - pieces["error"] = ("tag '%%s' doesn't start with prefix '%%s'" - %% (full_tag, tag_prefix)) - return pieces - pieces["closest-tag"] = full_tag[len(tag_prefix):] - - # distance: number of commits since tag - pieces["distance"] = int(mo.group(2)) - - # commit: short hex revision ID - pieces["short"] = mo.group(3) - - else: - # HEX: no tags - pieces["closest-tag"] = None - count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"], - cwd=root) - pieces["distance"] = int(count_out) # total number of commits - - # commit date: see ISO-8601 comment in git_versions_from_keywords() - date = run_command(GITS, ["show", "-s", "--format=%%ci", "HEAD"], - cwd=root)[0].strip() - pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) - - return pieces - - -def plus_or_dot(pieces): - """Return a + if we don't already have one, else return a .""" - if "+" in pieces.get("closest-tag", ""): - return "." - return "+" - - -def render_pep440(pieces): - """Build up version string, with post-release "local version identifier". - - Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you - get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty - - Exceptions: - 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += plus_or_dot(pieces) - rendered += "%%d.g%%s" %% (pieces["distance"], pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - else: - # exception #1 - rendered = "0+untagged.%%d.g%%s" %% (pieces["distance"], - pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - return rendered - - -def render_pep440_pre(pieces): - """TAG[.post.devDISTANCE] -- No -dirty. - - Exceptions: - 1: no tags. 0.post.devDISTANCE - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"]: - rendered += ".post.dev%%d" %% pieces["distance"] - else: - # exception #1 - rendered = "0.post.dev%%d" %% pieces["distance"] - return rendered - - -def render_pep440_post(pieces): - """TAG[.postDISTANCE[.dev0]+gHEX] . - - The ".dev0" means dirty. Note that .dev0 sorts backwards - (a dirty tree will appear "older" than the corresponding clean one), - but you shouldn't be releasing software with -dirty anyways. - - Exceptions: - 1: no tags. 0.postDISTANCE[.dev0] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += ".post%%d" %% pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - rendered += plus_or_dot(pieces) - rendered += "g%%s" %% pieces["short"] - else: - # exception #1 - rendered = "0.post%%d" %% pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - rendered += "+g%%s" %% pieces["short"] - return rendered - - -def render_pep440_old(pieces): - """TAG[.postDISTANCE[.dev0]] . - - The ".dev0" means dirty. - - Eexceptions: - 1: no tags. 0.postDISTANCE[.dev0] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += ".post%%d" %% pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - else: - # exception #1 - rendered = "0.post%%d" %% pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - return rendered - - -def render_git_describe(pieces): - """TAG[-DISTANCE-gHEX][-dirty]. - - Like 'git describe --tags --dirty --always'. - - Exceptions: - 1: no tags. HEX[-dirty] (note: no 'g' prefix) - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"]: - rendered += "-%%d-g%%s" %% (pieces["distance"], pieces["short"]) - else: - # exception #1 - rendered = pieces["short"] - if pieces["dirty"]: - rendered += "-dirty" - return rendered - - -def render_git_describe_long(pieces): - """TAG-DISTANCE-gHEX[-dirty]. - - Like 'git describe --tags --dirty --always -long'. - The distance/hash is unconditional. - - Exceptions: - 1: no tags. HEX[-dirty] (note: no 'g' prefix) - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - rendered += "-%%d-g%%s" %% (pieces["distance"], pieces["short"]) - else: - # exception #1 - rendered = pieces["short"] - if pieces["dirty"]: - rendered += "-dirty" - return rendered - - -def render(pieces, style): - """Render the given version pieces into the requested style.""" - if pieces["error"]: - return {"version": "unknown", - "full-revisionid": pieces.get("long"), - "dirty": None, - "error": pieces["error"], - "date": None} - - if not style or style == "default": - style = "pep440" # the default - - if style == "pep440": - rendered = render_pep440(pieces) - elif style == "pep440-pre": - rendered = render_pep440_pre(pieces) - elif style == "pep440-post": - rendered = render_pep440_post(pieces) - elif style == "pep440-old": - rendered = render_pep440_old(pieces) - elif style == "git-describe": - rendered = render_git_describe(pieces) - elif style == "git-describe-long": - rendered = render_git_describe_long(pieces) - else: - raise ValueError("unknown style '%%s'" %% style) - - return {"version": rendered, "full-revisionid": pieces["long"], - "dirty": pieces["dirty"], "error": None, - "date": pieces.get("date")} - - -def get_versions(): - """Get version information or return default if unable to do so.""" - # I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have - # __file__, we can work backwards from there to the root. Some - # py2exe/bbfreeze/non-CPython implementations don't do __file__, in which - # case we can only use expanded keywords. - - cfg = get_config() - verbose = cfg.verbose - - try: - return git_versions_from_keywords(get_keywords(), cfg.tag_prefix, - verbose) - except NotThisMethod: - pass - - try: - root = os.path.realpath(__file__) - # versionfile_source is the relative path from the top of the source - # tree (where the .git directory might live) to this file. Invert - # this to find the root from __file__. - for i in cfg.versionfile_source.split('/'): - root = os.path.dirname(root) - except NameError: - return {"version": "0+unknown", "full-revisionid": None, - "dirty": None, - "error": "unable to find root of source tree", - "date": None} - - try: - pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose) - return render(pieces, cfg.style) - except NotThisMethod: - pass - - try: - if cfg.parentdir_prefix: - return versions_from_parentdir(cfg.parentdir_prefix, root, verbose) - except NotThisMethod: - pass - - return {"version": "0+unknown", "full-revisionid": None, - "dirty": None, - "error": "unable to compute version", "date": None} -''' - - -@register_vcs_handler("git", "get_keywords") -def git_get_keywords(versionfile_abs): - """Extract version information from the given file.""" - # the code embedded in _version.py can just fetch the value of these - # keywords. When used from setup.py, we don't want to import _version.py, - # so we do it with a regexp instead. This function is not used from - # _version.py. - keywords = {} - try: - f = open(versionfile_abs, "r") - for line in f.readlines(): - if line.strip().startswith("git_refnames ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["refnames"] = mo.group(1) - if line.strip().startswith("git_full ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["full"] = mo.group(1) - if line.strip().startswith("git_date ="): - mo = re.search(r'=\s*"(.*)"', line) - if mo: - keywords["date"] = mo.group(1) - f.close() - except EnvironmentError: - pass - return keywords - - -@register_vcs_handler("git", "keywords") -def git_versions_from_keywords(keywords, tag_prefix, verbose): - """Get version information from git keywords.""" - if not keywords: - raise NotThisMethod("no keywords at all, weird") - date = keywords.get("date") - if date is not None: - # git-2.2.0 added "%cI", which expands to an ISO-8601 -compliant - # datestamp. However we prefer "%ci" (which expands to an "ISO-8601 - # -like" string, which we must then edit to make compliant), because - # it's been around since git-1.5.3, and it's too difficult to - # discover which version we're using, or to work around using an - # older one. - date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) - refnames = keywords["refnames"].strip() - if refnames.startswith("$Format"): - if verbose: - print("keywords are unexpanded, not using") - raise NotThisMethod("unexpanded keywords, not a git-archive tarball") - refs = set([r.strip() for r in refnames.strip("()").split(",")]) - # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of - # just "foo-1.0". If we see a "tag: " prefix, prefer those. - TAG = "tag: " - tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)]) - if not tags: - # Either we're using git < 1.8.3, or there really are no tags. We use - # a heuristic: assume all version tags have a digit. The old git %d - # expansion behaves like git log --decorate=short and strips out the - # refs/heads/ and refs/tags/ prefixes that would let us distinguish - # between branches and tags. By ignoring refnames without digits, we - # filter out many common branch names like "release" and - # "stabilization", as well as "HEAD" and "master". - tags = set([r for r in refs if re.search(r'\d', r)]) - if verbose: - print("discarding '%s', no digits" % ",".join(refs - tags)) - if verbose: - print("likely tags: %s" % ",".join(sorted(tags))) - for ref in sorted(tags): - # sorting will prefer e.g. "2.0" over "2.0rc1" - if ref.startswith(tag_prefix): - r = ref[len(tag_prefix):] - if verbose: - print("picking %s" % r) - return {"version": r, - "full-revisionid": keywords["full"].strip(), - "dirty": False, "error": None, - "date": date} - # no suitable tags, so version is "0+unknown", but full hex is still there - if verbose: - print("no suitable tags, using unknown + full revision id") - return {"version": "0+unknown", - "full-revisionid": keywords["full"].strip(), - "dirty": False, "error": "no suitable tags", "date": None} - - -@register_vcs_handler("git", "pieces_from_vcs") -def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): - """Get version from 'git describe' in the root of the source tree. - - This only gets called if the git-archive 'subst' keywords were *not* - expanded, and _version.py hasn't already been rewritten with a short - version string, meaning we're inside a checked out source tree. - """ - GITS = ["git"] - if sys.platform == "win32": - GITS = ["git.cmd", "git.exe"] - - out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root, - hide_stderr=True) - if rc != 0: - if verbose: - print("Directory %s not under git control" % root) - raise NotThisMethod("'git rev-parse --git-dir' returned error") - - # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] - # if there isn't one, this yields HEX[-dirty] (no NUM) - describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty", - "--always", "--long", - "--match", "%s*" % tag_prefix], - cwd=root) - # --long was added in git-1.5.5 - if describe_out is None: - raise NotThisMethod("'git describe' failed") - describe_out = describe_out.strip() - full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root) - if full_out is None: - raise NotThisMethod("'git rev-parse' failed") - full_out = full_out.strip() - - pieces = {} - pieces["long"] = full_out - pieces["short"] = full_out[:7] # maybe improved later - pieces["error"] = None - - # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] - # TAG might have hyphens. - git_describe = describe_out - - # look for -dirty suffix - dirty = git_describe.endswith("-dirty") - pieces["dirty"] = dirty - if dirty: - git_describe = git_describe[:git_describe.rindex("-dirty")] - - # now we have TAG-NUM-gHEX or HEX - - if "-" in git_describe: - # TAG-NUM-gHEX - mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe) - if not mo: - # unparseable. Maybe git-describe is misbehaving? - pieces["error"] = ("unable to parse git-describe output: '%s'" - % describe_out) - return pieces - - # tag - full_tag = mo.group(1) - if not full_tag.startswith(tag_prefix): - if verbose: - fmt = "tag '%s' doesn't start with prefix '%s'" - print(fmt % (full_tag, tag_prefix)) - pieces["error"] = ("tag '%s' doesn't start with prefix '%s'" - % (full_tag, tag_prefix)) - return pieces - pieces["closest-tag"] = full_tag[len(tag_prefix):] - - # distance: number of commits since tag - pieces["distance"] = int(mo.group(2)) - - # commit: short hex revision ID - pieces["short"] = mo.group(3) - - else: - # HEX: no tags - pieces["closest-tag"] = None - count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"], - cwd=root) - pieces["distance"] = int(count_out) # total number of commits - - # commit date: see ISO-8601 comment in git_versions_from_keywords() - date = run_command(GITS, ["show", "-s", "--format=%ci", "HEAD"], - cwd=root)[0].strip() - pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) - - return pieces - - -def do_vcs_install(manifest_in, versionfile_source, ipy): - """Git-specific installation logic for Versioneer. - - For Git, this means creating/changing .gitattributes to mark _version.py - for export-subst keyword substitution. - """ - GITS = ["git"] - if sys.platform == "win32": - GITS = ["git.cmd", "git.exe"] - files = [manifest_in, versionfile_source] - if ipy: - files.append(ipy) - try: - me = __file__ - if me.endswith(".pyc") or me.endswith(".pyo"): - me = os.path.splitext(me)[0] + ".py" - versioneer_file = os.path.relpath(me) - except NameError: - versioneer_file = "versioneer.py" - files.append(versioneer_file) - present = False - try: - f = open(".gitattributes", "r") - for line in f.readlines(): - if line.strip().startswith(versionfile_source): - if "export-subst" in line.strip().split()[1:]: - present = True - f.close() - except EnvironmentError: - pass - if not present: - f = open(".gitattributes", "a+") - f.write("%s export-subst\n" % versionfile_source) - f.close() - files.append(".gitattributes") - run_command(GITS, ["add", "--"] + files) - - -def versions_from_parentdir(parentdir_prefix, root, verbose): - """Try to determine the version from the parent directory name. - - Source tarballs conventionally unpack into a directory that includes both - the project name and a version string. We will also support searching up - two directory levels for an appropriately named parent directory - """ - rootdirs = [] - - for i in range(3): - dirname = os.path.basename(root) - if dirname.startswith(parentdir_prefix): - return {"version": dirname[len(parentdir_prefix):], - "full-revisionid": None, - "dirty": False, "error": None, "date": None} - else: - rootdirs.append(root) - root = os.path.dirname(root) # up a level - - if verbose: - print("Tried directories %s but none started with prefix %s" % - (str(rootdirs), parentdir_prefix)) - raise NotThisMethod("rootdir doesn't start with parentdir_prefix") - - -SHORT_VERSION_PY = """ -# This file was generated by 'versioneer.py' (0.18) from -# revision-control system data, or from the parent directory name of an -# unpacked source archive. Distribution tarballs contain a pre-generated copy -# of this file. - -import json - -version_json = ''' -%s -''' # END VERSION_JSON - - -def get_versions(): - return json.loads(version_json) -""" - - -def versions_from_file(filename): - """Try to determine the version from _version.py if present.""" - try: - with open(filename) as f: - contents = f.read() - except EnvironmentError: - raise NotThisMethod("unable to read _version.py") - mo = re.search(r"version_json = '''\n(.*)''' # END VERSION_JSON", - contents, re.M | re.S) - if not mo: - mo = re.search(r"version_json = '''\r\n(.*)''' # END VERSION_JSON", - contents, re.M | re.S) - if not mo: - raise NotThisMethod("no version_json in _version.py") - return json.loads(mo.group(1)) - - -def write_to_version_file(filename, versions): - """Write the given version number to the given _version.py file.""" - os.unlink(filename) - contents = json.dumps(versions, sort_keys=True, - indent=1, separators=(",", ": ")) - with open(filename, "w") as f: - f.write(SHORT_VERSION_PY % contents) - - print("set %s to '%s'" % (filename, versions["version"])) - - -def plus_or_dot(pieces): - """Return a + if we don't already have one, else return a .""" - if "+" in pieces.get("closest-tag", ""): - return "." - return "+" - - -def render_pep440(pieces): - """Build up version string, with post-release "local version identifier". - - Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you - get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty - - Exceptions: - 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += plus_or_dot(pieces) - rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - else: - # exception #1 - rendered = "0+untagged.%d.g%s" % (pieces["distance"], - pieces["short"]) - if pieces["dirty"]: - rendered += ".dirty" - return rendered - - -def render_pep440_pre(pieces): - """TAG[.post.devDISTANCE] -- No -dirty. - - Exceptions: - 1: no tags. 0.post.devDISTANCE - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"]: - rendered += ".post.dev%d" % pieces["distance"] - else: - # exception #1 - rendered = "0.post.dev%d" % pieces["distance"] - return rendered - - -def render_pep440_post(pieces): - """TAG[.postDISTANCE[.dev0]+gHEX] . - - The ".dev0" means dirty. Note that .dev0 sorts backwards - (a dirty tree will appear "older" than the corresponding clean one), - but you shouldn't be releasing software with -dirty anyways. - - Exceptions: - 1: no tags. 0.postDISTANCE[.dev0] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += ".post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - rendered += plus_or_dot(pieces) - rendered += "g%s" % pieces["short"] - else: - # exception #1 - rendered = "0.post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - rendered += "+g%s" % pieces["short"] - return rendered - - -def render_pep440_old(pieces): - """TAG[.postDISTANCE[.dev0]] . - - The ".dev0" means dirty. - - Eexceptions: - 1: no tags. 0.postDISTANCE[.dev0] - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"] or pieces["dirty"]: - rendered += ".post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - else: - # exception #1 - rendered = "0.post%d" % pieces["distance"] - if pieces["dirty"]: - rendered += ".dev0" - return rendered - - -def render_git_describe(pieces): - """TAG[-DISTANCE-gHEX][-dirty]. - - Like 'git describe --tags --dirty --always'. - - Exceptions: - 1: no tags. HEX[-dirty] (note: no 'g' prefix) - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - if pieces["distance"]: - rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) - else: - # exception #1 - rendered = pieces["short"] - if pieces["dirty"]: - rendered += "-dirty" - return rendered - - -def render_git_describe_long(pieces): - """TAG-DISTANCE-gHEX[-dirty]. - - Like 'git describe --tags --dirty --always -long'. - The distance/hash is unconditional. - - Exceptions: - 1: no tags. HEX[-dirty] (note: no 'g' prefix) - """ - if pieces["closest-tag"]: - rendered = pieces["closest-tag"] - rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) - else: - # exception #1 - rendered = pieces["short"] - if pieces["dirty"]: - rendered += "-dirty" - return rendered - - -def render(pieces, style): - """Render the given version pieces into the requested style.""" - if pieces["error"]: - return {"version": "unknown", - "full-revisionid": pieces.get("long"), - "dirty": None, - "error": pieces["error"], - "date": None} - - if not style or style == "default": - style = "pep440" # the default - - if style == "pep440": - rendered = render_pep440(pieces) - elif style == "pep440-pre": - rendered = render_pep440_pre(pieces) - elif style == "pep440-post": - rendered = render_pep440_post(pieces) - elif style == "pep440-old": - rendered = render_pep440_old(pieces) - elif style == "git-describe": - rendered = render_git_describe(pieces) - elif style == "git-describe-long": - rendered = render_git_describe_long(pieces) - else: - raise ValueError("unknown style '%s'" % style) - - return {"version": rendered, "full-revisionid": pieces["long"], - "dirty": pieces["dirty"], "error": None, - "date": pieces.get("date")} - - -class VersioneerBadRootError(Exception): - """The project root directory is unknown or missing key files.""" - - -def get_versions(verbose=False): - """Get the project version from whatever source is available. - - Returns dict with two keys: 'version' and 'full'. - """ - if "versioneer" in sys.modules: - # see the discussion in cmdclass.py:get_cmdclass() - del sys.modules["versioneer"] - - root = get_root() - cfg = get_config_from_root(root) - - assert cfg.VCS is not None, "please set [versioneer]VCS= in setup.cfg" - handlers = HANDLERS.get(cfg.VCS) - assert handlers, "unrecognized VCS '%s'" % cfg.VCS - verbose = verbose or cfg.verbose - assert cfg.versionfile_source is not None, \ - "please set versioneer.versionfile_source" - assert cfg.tag_prefix is not None, "please set versioneer.tag_prefix" - - versionfile_abs = os.path.join(root, cfg.versionfile_source) - - # extract version from first of: _version.py, VCS command (e.g. 'git - # describe'), parentdir. This is meant to work for developers using a - # source checkout, for users of a tarball created by 'setup.py sdist', - # and for users of a tarball/zipball created by 'git archive' or github's - # download-from-tag feature or the equivalent in other VCSes. - - get_keywords_f = handlers.get("get_keywords") - from_keywords_f = handlers.get("keywords") - if get_keywords_f and from_keywords_f: - try: - keywords = get_keywords_f(versionfile_abs) - ver = from_keywords_f(keywords, cfg.tag_prefix, verbose) - if verbose: - print("got version from expanded keyword %s" % ver) - return ver - except NotThisMethod: - pass - - try: - ver = versions_from_file(versionfile_abs) - if verbose: - print("got version from file %s %s" % (versionfile_abs, ver)) - return ver - except NotThisMethod: - pass - - from_vcs_f = handlers.get("pieces_from_vcs") - if from_vcs_f: - try: - pieces = from_vcs_f(cfg.tag_prefix, root, verbose) - ver = render(pieces, cfg.style) - if verbose: - print("got version from VCS %s" % ver) - return ver - except NotThisMethod: - pass - - try: - if cfg.parentdir_prefix: - ver = versions_from_parentdir(cfg.parentdir_prefix, root, verbose) - if verbose: - print("got version from parentdir %s" % ver) - return ver - except NotThisMethod: - pass - - if verbose: - print("unable to compute version") - - return {"version": "0+unknown", "full-revisionid": None, - "dirty": None, "error": "unable to compute version", - "date": None} - - -def get_version(): - """Get the short version string for this project.""" - return get_versions()["version"] - - -def get_cmdclass(): - """Get the custom setuptools/distutils subclasses used by Versioneer.""" - if "versioneer" in sys.modules: - del sys.modules["versioneer"] - # this fixes the "python setup.py develop" case (also 'install' and - # 'easy_install .'), in which subdependencies of the main project are - # built (using setup.py bdist_egg) in the same python process. Assume - # a main project A and a dependency B, which use different versions - # of Versioneer. A's setup.py imports A's Versioneer, leaving it in - # sys.modules by the time B's setup.py is executed, causing B to run - # with the wrong versioneer. Setuptools wraps the sub-dep builds in a - # sandbox that restores sys.modules to it's pre-build state, so the - # parent is protected against the child's "import versioneer". By - # removing ourselves from sys.modules here, before the child build - # happens, we protect the child from the parent's versioneer too. - # Also see https://github.com/warner/python-versioneer/issues/52 - - cmds = {} - - # we add "version" to both distutils and setuptools - from distutils.core import Command - - class cmd_version(Command): - description = "report generated version string" - user_options = [] - boolean_options = [] - - def initialize_options(self): - pass - - def finalize_options(self): - pass - - def run(self): - vers = get_versions(verbose=True) - print("Version: %s" % vers["version"]) - print(" full-revisionid: %s" % vers.get("full-revisionid")) - print(" dirty: %s" % vers.get("dirty")) - print(" date: %s" % vers.get("date")) - if vers["error"]: - print(" error: %s" % vers["error"]) - cmds["version"] = cmd_version - - # we override "build_py" in both distutils and setuptools - # - # most invocation pathways end up running build_py: - # distutils/build -> build_py - # distutils/install -> distutils/build ->.. - # setuptools/bdist_wheel -> distutils/install ->.. - # setuptools/bdist_egg -> distutils/install_lib -> build_py - # setuptools/install -> bdist_egg ->.. - # setuptools/develop -> ? - # pip install: - # copies source tree to a tempdir before running egg_info/etc - # if .git isn't copied too, 'git describe' will fail - # then does setup.py bdist_wheel, or sometimes setup.py install - # setup.py egg_info -> ? - - # we override different "build_py" commands for both environments - if "setuptools" in sys.modules: - from setuptools.command.build_py import build_py as _build_py - else: - from distutils.command.build_py import build_py as _build_py - - class cmd_build_py(_build_py): - def run(self): - root = get_root() - cfg = get_config_from_root(root) - versions = get_versions() - _build_py.run(self) - # now locate _version.py in the new build/ directory and replace - # it with an updated value - if cfg.versionfile_build: - target_versionfile = os.path.join(self.build_lib, - cfg.versionfile_build) - print("UPDATING %s" % target_versionfile) - write_to_version_file(target_versionfile, versions) - cmds["build_py"] = cmd_build_py - - if "cx_Freeze" in sys.modules: # cx_freeze enabled? - from cx_Freeze.dist import build_exe as _build_exe - # nczeczulin reports that py2exe won't like the pep440-style string - # as FILEVERSION, but it can be used for PRODUCTVERSION, e.g. - # setup(console=[{ - # "version": versioneer.get_version().split("+", 1)[0], # FILEVERSION - # "product_version": versioneer.get_version(), - # ... - - class cmd_build_exe(_build_exe): - def run(self): - root = get_root() - cfg = get_config_from_root(root) - versions = get_versions() - target_versionfile = cfg.versionfile_source - print("UPDATING %s" % target_versionfile) - write_to_version_file(target_versionfile, versions) - - _build_exe.run(self) - os.unlink(target_versionfile) - with open(cfg.versionfile_source, "w") as f: - LONG = LONG_VERSION_PY[cfg.VCS] - f.write(LONG % - {"DOLLAR": "$", - "STYLE": cfg.style, - "TAG_PREFIX": cfg.tag_prefix, - "PARENTDIR_PREFIX": cfg.parentdir_prefix, - "VERSIONFILE_SOURCE": cfg.versionfile_source, - }) - cmds["build_exe"] = cmd_build_exe - del cmds["build_py"] - - if 'py2exe' in sys.modules: # py2exe enabled? - try: - from py2exe.distutils_buildexe import py2exe as _py2exe # py3 - except ImportError: - from py2exe.build_exe import py2exe as _py2exe # py2 - - class cmd_py2exe(_py2exe): - def run(self): - root = get_root() - cfg = get_config_from_root(root) - versions = get_versions() - target_versionfile = cfg.versionfile_source - print("UPDATING %s" % target_versionfile) - write_to_version_file(target_versionfile, versions) - - _py2exe.run(self) - os.unlink(target_versionfile) - with open(cfg.versionfile_source, "w") as f: - LONG = LONG_VERSION_PY[cfg.VCS] - f.write(LONG % - {"DOLLAR": "$", - "STYLE": cfg.style, - "TAG_PREFIX": cfg.tag_prefix, - "PARENTDIR_PREFIX": cfg.parentdir_prefix, - "VERSIONFILE_SOURCE": cfg.versionfile_source, - }) - cmds["py2exe"] = cmd_py2exe - - # we override different "sdist" commands for both environments - if "setuptools" in sys.modules: - from setuptools.command.sdist import sdist as _sdist - else: - from distutils.command.sdist import sdist as _sdist - - class cmd_sdist(_sdist): - def run(self): - versions = get_versions() - self._versioneer_generated_versions = versions - # unless we update this, the command will keep using the old - # version - self.distribution.metadata.version = versions["version"] - return _sdist.run(self) - - def make_release_tree(self, base_dir, files): - root = get_root() - cfg = get_config_from_root(root) - _sdist.make_release_tree(self, base_dir, files) - # now locate _version.py in the new base_dir directory - # (remembering that it may be a hardlink) and replace it with an - # updated value - target_versionfile = os.path.join(base_dir, cfg.versionfile_source) - print("UPDATING %s" % target_versionfile) - write_to_version_file(target_versionfile, - self._versioneer_generated_versions) - cmds["sdist"] = cmd_sdist - - return cmds - - -CONFIG_ERROR = """ -setup.cfg is missing the necessary Versioneer configuration. You need -a section like: - - [versioneer] - VCS = git - style = pep440 - versionfile_source = src/myproject/_version.py - versionfile_build = myproject/_version.py - tag_prefix = - parentdir_prefix = myproject- - -You will also need to edit your setup.py to use the results: - - import versioneer - setup(version=versioneer.get_version(), - cmdclass=versioneer.get_cmdclass(), ...) - -Please read the docstring in ./versioneer.py for configuration instructions, -edit setup.cfg, and re-run the installer or 'python versioneer.py setup'. -""" - -SAMPLE_CONFIG = """ -# See the docstring in versioneer.py for instructions. Note that you must -# re-run 'versioneer.py setup' after changing this section, and commit the -# resulting files. - -[versioneer] -#VCS = git -#style = pep440 -#versionfile_source = -#versionfile_build = -#tag_prefix = -#parentdir_prefix = - -""" - -INIT_PY_SNIPPET = """ -from ._version import get_versions -__version__ = get_versions()['version'] -del get_versions -""" - - -def do_setup(): - """Main VCS-independent setup function for installing Versioneer.""" - root = get_root() - try: - cfg = get_config_from_root(root) - except (EnvironmentError, configparser.NoSectionError, - configparser.NoOptionError) as e: - if isinstance(e, (EnvironmentError, configparser.NoSectionError)): - print("Adding sample versioneer config to setup.cfg", - file=sys.stderr) - with open(os.path.join(root, "setup.cfg"), "a") as f: - f.write(SAMPLE_CONFIG) - print(CONFIG_ERROR, file=sys.stderr) - return 1 - - print(" creating %s" % cfg.versionfile_source) - with open(cfg.versionfile_source, "w") as f: - LONG = LONG_VERSION_PY[cfg.VCS] - f.write(LONG % {"DOLLAR": "$", - "STYLE": cfg.style, - "TAG_PREFIX": cfg.tag_prefix, - "PARENTDIR_PREFIX": cfg.parentdir_prefix, - "VERSIONFILE_SOURCE": cfg.versionfile_source, - }) - - ipy = os.path.join(os.path.dirname(cfg.versionfile_source), - "__init__.py") - if os.path.exists(ipy): - try: - with open(ipy, "r") as f: - old = f.read() - except EnvironmentError: - old = "" - if INIT_PY_SNIPPET not in old: - print(" appending to %s" % ipy) - with open(ipy, "a") as f: - f.write(INIT_PY_SNIPPET) - else: - print(" %s unmodified" % ipy) - else: - print(" %s doesn't exist, ok" % ipy) - ipy = None - - # Make sure both the top-level "versioneer.py" and versionfile_source - # (PKG/_version.py, used by runtime code) are in MANIFEST.in, so - # they'll be copied into source distributions. Pip won't be able to - # install the package without this. - manifest_in = os.path.join(root, "MANIFEST.in") - simple_includes = set() - try: - with open(manifest_in, "r") as f: - for line in f: - if line.startswith("include "): - for include in line.split()[1:]: - simple_includes.add(include) - except EnvironmentError: - pass - # That doesn't cover everything MANIFEST.in can do - # (http://docs.python.org/2/distutils/sourcedist.html#commands), so - # it might give some false negatives. Appending redundant 'include' - # lines is safe, though. - if "versioneer.py" not in simple_includes: - print(" appending 'versioneer.py' to MANIFEST.in") - with open(manifest_in, "a") as f: - f.write("include versioneer.py\n") - else: - print(" 'versioneer.py' already in MANIFEST.in") - if cfg.versionfile_source not in simple_includes: - print(" appending versionfile_source ('%s') to MANIFEST.in" % - cfg.versionfile_source) - with open(manifest_in, "a") as f: - f.write("include %s\n" % cfg.versionfile_source) - else: - print(" versionfile_source already in MANIFEST.in") - - # Make VCS-specific changes. For git, this means creating/changing - # .gitattributes to mark _version.py for export-subst keyword - # substitution. - do_vcs_install(manifest_in, cfg.versionfile_source, ipy) - return 0 - - -def scan_setup_py(): - """Validate the contents of setup.py against Versioneer's expectations.""" - found = set() - setters = False - errors = 0 - with open("setup.py", "r") as f: - for line in f.readlines(): - if "import versioneer" in line: - found.add("import") - if "versioneer.get_cmdclass()" in line: - found.add("cmdclass") - if "versioneer.get_version()" in line: - found.add("get_version") - if "versioneer.VCS" in line: - setters = True - if "versioneer.versionfile_source" in line: - setters = True - if len(found) != 3: - print("") - print("Your setup.py appears to be missing some important items") - print("(but I might be wrong). Please make sure it has something") - print("roughly like the following:") - print("") - print(" import versioneer") - print(" setup( version=versioneer.get_version(),") - print(" cmdclass=versioneer.get_cmdclass(), ...)") - print("") - errors += 1 - if setters: - print("You should remove lines like 'versioneer.VCS = ' and") - print("'versioneer.versionfile_source = ' . This configuration") - print("now lives in setup.cfg, and should be removed from setup.py") - print("") - errors += 1 - return errors - - -if __name__ == "__main__": - cmd = sys.argv[1] - if cmd == "setup": - errors = do_setup() - errors += scan_setup_py() - if errors: - sys.exit(1)