From 5e48645cb1bb725768f433403d42111e9fea9751 Mon Sep 17 00:00:00 2001 From: bries Date: Tue, 9 May 2023 10:40:26 +0200 Subject: [PATCH 01/20] updating pypi packaging, removing lgtm --- .lgtm.yml | 19 - README.md | 2 - ensembler/__init__.py | 14 - ensembler/__version__.py | 4 - ensembler/_version.py | 521 ----- ensembler/ensembler.py | 6 - pyproject.toml | 64 + setup.cfg | 35 - setup.py | 163 -- src/ensembler/__init__.py | 6 + src/ensembler/_version.py | 1 + .../ensembler}/analysis/__init__.py | 0 .../analysis/freeEnergyCalculation.py | 0 .../ensembler}/conditions/__init__.py | 0 .../ensembler}/conditions/_basicCondition.py | 0 .../ensembler}/conditions/box_conditions.py | 0 .../conditions/restrain_conditions.py | 150 +- .../ensembler}/conditions/thermostats.py | 0 {ensembler => src/ensembler}/data/README.md | 0 .../ensembler}/ensemble/__init__.py | 0 .../ensembler}/ensemble/_replica_graph.py | 0 .../ensembler}/ensemble/exchange_pattern.py | 0 .../ensembler}/ensemble/replica_exchange.py | 0 .../ensemble/replicas_dynamic_parameters.py | 0 {ensembler => src/ensembler}/potentials/ND.py | 0 .../ensembler}/potentials/OneD.py | 0 .../ensembler}/potentials/TwoD.py | 0 .../ensembler}/potentials/__init__.py | 10 +- .../ensembler}/potentials/_basicPotentials.py | 0 .../ensembler}/samplers/__init__.py | 0 .../ensembler}/samplers/_basicSamplers.py | 0 .../ensembler}/samplers/newtonian.py | 0 .../ensembler}/samplers/optimizers.py | 0 .../ensembler}/samplers/stochastic.py | 0 .../ensembler}/system/__init__.py | 10 +- .../ensembler}/system/basic_system.py | 0 .../ensembler}/system/eds_system.py | 458 ++--- .../ensembler}/system/perturbed_system.py | 0 .../ensembler}/tests/__init__.py | 0 .../ensembler}/tests/test_analysis.py | 270 +-- .../ensembler}/tests/test_conditions.py | 466 ++--- .../ensembler}/tests/test_conveyorBelt.py | 0 .../ensembler}/tests/test_ensemble.py | 0 .../ensembler}/tests/test_ensembler.py | 0 .../ensembler}/tests/test_potentials.py | 0 .../ensembler}/tests/test_sampler.py | 0 .../ensembler}/tests/test_system.py | 0 .../ensembler}/tests/test_visualization.py | 0 {ensembler => src/ensembler}/util/__init__.py | 0 .../ensembler}/util/basic_class.py | 190 +- .../ensembler}/util/dataStructure.py | 0 .../ensembler}/util/ensemblerTypes.py | 68 +- .../ensembler}/visualisation/__init__.py | 0 .../visualisation/animationSimulation.py | 0 .../visualisation/interactive_plots.py | 0 .../visualisation/plotConveyorBelt.py | 0 .../visualisation/plotPotentials.py | 0 .../visualisation/plotSimulations.py | 0 .../ensembler}/visualisation/style.py | 0 versioneer.py | 1830 ----------------- 60 files changed, 882 insertions(+), 3405 deletions(-) delete mode 100644 .lgtm.yml delete mode 100644 ensembler/__init__.py delete mode 100644 ensembler/__version__.py delete mode 100644 ensembler/_version.py delete mode 100644 ensembler/ensembler.py create mode 100644 pyproject.toml delete mode 100644 setup.cfg delete mode 100644 setup.py create mode 100644 src/ensembler/__init__.py create mode 100644 src/ensembler/_version.py rename {ensembler => src/ensembler}/analysis/__init__.py (100%) rename {ensembler => src/ensembler}/analysis/freeEnergyCalculation.py (100%) rename {ensembler => src/ensembler}/conditions/__init__.py (100%) rename {ensembler => src/ensembler}/conditions/_basicCondition.py (100%) rename {ensembler => src/ensembler}/conditions/box_conditions.py (100%) rename {ensembler => src/ensembler}/conditions/restrain_conditions.py (97%) rename {ensembler => src/ensembler}/conditions/thermostats.py (100%) rename {ensembler => src/ensembler}/data/README.md (100%) rename {ensembler => src/ensembler}/ensemble/__init__.py (100%) rename {ensembler => src/ensembler}/ensemble/_replica_graph.py (100%) rename {ensembler => src/ensembler}/ensemble/exchange_pattern.py (100%) rename {ensembler => src/ensembler}/ensemble/replica_exchange.py (100%) rename {ensembler => src/ensembler}/ensemble/replicas_dynamic_parameters.py (100%) rename {ensembler => src/ensembler}/potentials/ND.py (100%) rename {ensembler => src/ensembler}/potentials/OneD.py (100%) rename {ensembler => src/ensembler}/potentials/TwoD.py (100%) rename {ensembler => src/ensembler}/potentials/__init__.py (96%) rename {ensembler => src/ensembler}/potentials/_basicPotentials.py (100%) rename {ensembler => src/ensembler}/samplers/__init__.py (100%) rename {ensembler => src/ensembler}/samplers/_basicSamplers.py (100%) rename {ensembler => src/ensembler}/samplers/newtonian.py (100%) rename {ensembler => src/ensembler}/samplers/optimizers.py (100%) rename {ensembler => src/ensembler}/samplers/stochastic.py (100%) rename {ensembler => src/ensembler}/system/__init__.py (97%) rename {ensembler => src/ensembler}/system/basic_system.py (100%) rename {ensembler => src/ensembler}/system/eds_system.py (97%) rename {ensembler => src/ensembler}/system/perturbed_system.py (100%) rename {ensembler => src/ensembler}/tests/__init__.py (100%) rename {ensembler => src/ensembler}/tests/test_analysis.py (96%) rename {ensembler => src/ensembler}/tests/test_conditions.py (97%) rename {ensembler => src/ensembler}/tests/test_conveyorBelt.py (100%) rename {ensembler => src/ensembler}/tests/test_ensemble.py (100%) rename {ensembler => src/ensembler}/tests/test_ensembler.py (100%) rename {ensembler => src/ensembler}/tests/test_potentials.py (100%) rename {ensembler => src/ensembler}/tests/test_sampler.py (100%) rename {ensembler => src/ensembler}/tests/test_system.py (100%) rename {ensembler => src/ensembler}/tests/test_visualization.py (100%) rename {ensembler => src/ensembler}/util/__init__.py (100%) rename {ensembler => src/ensembler}/util/basic_class.py (96%) rename {ensembler => src/ensembler}/util/dataStructure.py (100%) rename {ensembler => src/ensembler}/util/ensemblerTypes.py (96%) rename {ensembler => src/ensembler}/visualisation/__init__.py (100%) rename {ensembler => src/ensembler}/visualisation/animationSimulation.py (100%) rename {ensembler => src/ensembler}/visualisation/interactive_plots.py (100%) rename {ensembler => src/ensembler}/visualisation/plotConveyorBelt.py (100%) rename {ensembler => src/ensembler}/visualisation/plotPotentials.py (100%) rename {ensembler => src/ensembler}/visualisation/plotSimulations.py (100%) rename {ensembler => src/ensembler}/visualisation/style.py (100%) delete mode 100644 versioneer.py diff --git a/.lgtm.yml b/.lgtm.yml deleted file mode 100644 index 3f1ece5..0000000 --- a/.lgtm.yml +++ /dev/null @@ -1,19 +0,0 @@ -# Configure LGTM for this package - -extraction: - python: # Configure Python - python_setup: # Configure the setup - version: 3 # Specify Version 3 - -path_classifiers: - exclude: - - ensembler/test - - examples - - setup.py - - versioneer.py - - library: - - versioneer.py # Set Versioneer.py to an external "library" (3rd party code) - - devtools/* - generated: - - ensembler/_version.py diff --git a/README.md b/README.md index 147c91a..7663717 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,6 @@ Welcome to Ensembler [//]: # (Badges) [![GitHub Actions Build Status](https://github.com/rinikerlab/ensembler/workflows/CI/badge.svg)](https://github.com/rinikerlab/ensembler/actions?query=branch%3Amaster+workflow%3ACI) [![codecov](https://codecov.io/gh/rinikerlab/Ensembler/branch/master/graph/badge.svg)](https://codecov.io/gh/rinikerlab/Ensembler/branch/master) -[![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/rinikerlab/Ensembler.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/rinikerlab/Ensembler/context:python) -![Build package](https://github.com/rinikerlab/Ensembler/workflows/Python%20package/badge.svg) [![Documentation](https://img.shields.io/badge/Documentation-here-white.svg)](https://rinikerlab.github.io/Ensembler/index.html) ## Description diff --git a/ensembler/__init__.py b/ensembler/__init__.py deleted file mode 100644 index 6281c13..0000000 --- a/ensembler/__init__.py +++ /dev/null @@ -1,14 +0,0 @@ -""" -Ensembler -Code to sample ensembles of 1D and 2D models with various algorithms. -""" - -# Handle versioneer -from ._version import get_versions -# Add imports here -from .ensembler import * - -versions = get_versions() -__version__ = versions['version'] -__git_revision__ = versions['full-revisionid'] -del get_versions, versions diff --git a/ensembler/__version__.py b/ensembler/__version__.py deleted file mode 100644 index d39354e..0000000 --- a/ensembler/__version__.py +++ /dev/null @@ -1,4 +0,0 @@ -# current code Version -VERSION = (1, 0, 4) - -__version__ = '.'.join(map(str, VERSION)) diff --git a/ensembler/_version.py b/ensembler/_version.py deleted file mode 100644 index a8474f8..0000000 --- a/ensembler/_version.py +++ /dev/null @@ -1,521 +0,0 @@ -# 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 = "None" - cfg.versionfile_source = "ensembler/_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/ensembler/ensembler.py b/ensembler/ensembler.py deleted file mode 100644 index 5da5309..0000000 --- a/ensembler/ensembler.py +++ /dev/null @@ -1,6 +0,0 @@ -""" -ensembler.py -Code to sample ensembles of simple (toy) models with various algorithms. - -Handles the primary functions -""" diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..76e0010 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,64 @@ +[build-system] +requires=[ + "setuptools>=61.0", + "versioningit", +] +build-backend = "setuptools.build_meta" + +[project] +name = "ensembler" +dynamic = ["version"] +authors=[ + {name="Benjamin Ries", email="benjamin-ries@outlook.com"}, + {name="David Friedrich Hahn"}, + {name="Stephanie Linker"}, + {name="Sereina Riniker", email="sriniker@ethz.com"}, + +] + +dependencies = [ + 'typing', #Code: used for type declarations + 'pytest', #Code: used for testing + 'pandas', #Code: core functionality + 'numpy', #Code: core functionality + 'sympy', #Code: core functionality + 'scipy', #Code: core functionality + 'matplotlib', #Visualization + 'jupyter', # Tutorial/Examples: basics + 'ipywidgets',# Tutorial/Examples: Interactive widgets + 'tqdm',# Tutorial/Examples: nice progressbar + 'ffmpeg' #Visualizations: for animations in general +] + +description="Ensembler is a tool for fast and efficient development of simulation methods or for teaching purposes." +keywords=["teaching", "method_development", "statistical_mechanics", "statistical_thermodynamics", "physics", "chemistry", + "free_energy_calculations", "free energy", "enhanced sampling", "RE-EDS", "EDS", "Conveyor_Belt_TI", "numerical_integration", "science"] +readme="README.md" +requires-python = ">=3.7" +classifiers = [ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", +] + +[tool.poetry.extras] +docs = ["sphinx", "sphinx_rtd_theme", "nbsphinx", "m2r"] + +[project.urls] +"Homepage" = "https://github.com/rinikerlab/Ensembler" + +[tool.versioningit] +default-version = "1+unknown" + +[tool.versioningit.format] +distance = "{base_version}+{distance}.{vcs}{rev}" +dirty = "{base_version}+{distance}.{vcs}{rev}.dirty" +distance-dirty = "{base_version}+{distance}.{vcs}{rev}.dirty" + +[tool.versioningit.vcs] +method = "git" +match = ["*"] +default-tag = "0.0.0" + +[tool.versioningit.write] +file = "src/ensembler/_version.py" diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index f961e05..0000000 --- a/setup.cfg +++ /dev/null @@ -1,35 +0,0 @@ -# Helper file to handle all configs - -[metadata] -description-file = README.md - -[coverage:run] -# .coveragerc to control coverage.py and pytest-cov -omit = -# Omit the tests - */tests/* -# Omit generated versioneer - ensembler/_version.py -# Omit visualizations: - */visualisations/* - -[yapf] -# YAPF, in .style.yapf files this shows up as "[style]" header -COLUMN_LIMIT = 119 -INDENT_WIDTH = 4 -USE_TABS = False - -[flake8] -# Flake8, PyFlakes, etc -max-line-length = 119 - -[versioneer] -# Automatic version numbering scheme -VCS = git -style = pep440 -versionfile_source = ensembler/_version.py -versionfile_build = ensembler/_version.py -tag_prefix = '' - -[aliases] -test = pytest diff --git a/setup.py b/setup.py deleted file mode 100644 index ba877c2..0000000 --- a/setup.py +++ /dev/null @@ -1,163 +0,0 @@ -""" -Ensembler -Code to sample ensembles of simple (toy) models with various algorithms. -""" -import sys -import versioneer - -short_description = __doc__.split("\n") - -needs_pytest = {'pytest', 'test', 'ptr'}.intersection(sys.argv) -pytest_runner = ['pytest-runner'] if needs_pytest else [] - -try: - with open("README.md", "r") as handle: - long_description = handle.read() -except: - long_description = "\n".join(short_description[2:]) - -import io -import os -import sys - -from setuptools import find_packages, setup, Command - -# Package meta-data. -NAME = 'ensembler-rinikerlab' -DESCRIPTION = 'Ensembler is a tool for fast and efficient development of simulation methods or for teaching purposes.' -URL = 'https://github.com/rinikerlab/Ensembler' -DOWNLOAD_URL = "https://github.com/rinikerlab/Ensembler/archive/1.0.tar.gz" -EMAIL = 'bschroed@ethz.ch' -AUTHOR = 'Benjamin Ries; David Friedrich Hahn; Stephanie Linker' -REQUIRES_PYTHON = '>=3.6.0' -VERSION = None #"1.0.0" -KEYWORDS = " ".join(["teaching", "method_development", "statistical_mechanics", "statistical_thermodynamics", "physics", "chemistry", - "free_energy_calculations", "free energy", "enhanced sampling", "RE-EDS", "EDS", "Conveyor_Belt_TI", "numerical_integration", "science"]) - -# What packages are required for this module to be executed? - for minimal package version, please check the requirement files -REQUIRED = ['typing', #Code: used for type declarations - 'pytest', #Code: used for testing - 'pandas', #Code: core functionality - 'numpy', #Code: core functionality - 'sympy', #Code: core functionality - 'scipy', #Code: core functionality - 'matplotlib', #Visualization - 'jupyter', # Tutorial/Examples: basics - 'ipywidgets',# Tutorial/Examples: Interactive widgets - 'tqdm',# Tutorial/Examples: nice progressbar - 'ffmpeg' #Visualizations: for animations in general - ] - -# What packages are optional? -EXTRAS = {"docs":[ - 'sphinx', #Documentation: autodocu tool - 'sphinx_rtd_theme', #Documentation: style - 'nbsphinx', #Documentation: for inclusion of jupyter notebooks - 'm2r', #Documentation: converts markdown to rst - ] - } - -# The rest you shouldn't have to touch too much :) -# ------------------------------------------------ -# Except, perhaps the License and Trove Classifiers! -# If you do change the License, remember to change the Trove Classifier for that! - -here = os.path.abspath(os.path.dirname(__file__)) - -# Import the README.md and use it as the long-description. -# Note: this will only work if 'README.md.md' is present in your MANIFEST.in file! -try: - #with io.open(os.path.join(here, 'README.md'), encoding='utf-8') as f: - with io.open(os.path.join(here, 'docs/sphinx_project/introduction.rst'), encoding='utf-8') as f: - - long_description = '\n' + f.read() -except FileNotFoundError: - long_description = DESCRIPTION - -# Load the package's __version__.py module as a dictionary. -about = {} -if not VERSION: - with open(os.path.join(here, "ensembler", '__version__.py')) as f: - exec(f.read(), about) -else: - about['__version__'] = VERSION -VERSION = about['__version__'] - -class UploadCommand(Command): - """Support setup.py upload.""" - - description = 'Build and publish the package.' - user_options = [] - - @staticmethod - def status(s): - """Prints things in bold.""" - print('\033[1m{0}\033[0m'.format(s)) - - def initialize_options(self): - pass - - def finalize_options(self): - pass - - def run(self): - try: - self.status('Removing previous builds…') - import shutil - shutil.rmtree(os.path.join(here, 'dist')) - except OSError: - pass - - self.status('Building Source and Wheel (universal) distribution…') - os.system('{0} setup.py sdist bdist_wheel --universal'.format(sys.executable)) - - self.status('Uploading the package to PyPI via Twine…') - os.system('twine upload dist/*') - - self.status('Pushing git tags…') - os.system('git tag v{0}'.format(VERSION)) - os.system('git push --tags') - - sys.exit() - - -# Where the magic happens: -setup( - # Self-descriptive entries which should always be present - name=NAME, - author=AUTHOR, - author_email=EMAIL, - description=short_description[0], - long_description=long_description, - #long_description_content_type="text/markdown", - version=VERSION, #versioneer.get_version(), - #cmdclass=versioneer.get_cmdclass(), - license='MIT', - keywords=KEYWORDS, - # Which Python importable modules should be included when your package is installed - # Handled automatically by setuptools. Use 'exclude' to prevent some specific - # subpackage(s) from being added, if needed - packages=find_packages(), - - # Optional include package data to ship with your package - # Customize MANIFEST.in if the general case does not suit your needs - # Comment out this line to prevent the files from being packaged with your software - include_package_data=True, - - # Allows `setup.py test` to work correctly with pytest - install_requires=REQUIRED, - setup_requires=REQUIRED, - extras_require=EXTRAS, - - # Additional entries you may want simply uncomment the lines you want and fill in the data - url=URL, # Website - download_url=DOWNLOAD_URL, #current version - # install_requires=[REQUIRED], # Required packages, pulls from pip if needed; do not use for Conda deployment - platforms=['Linux', - 'Mac OS-X', - 'Unix', - 'Windows'], # Valid platforms your code works on, adjust to your flavor - python_requires=">=3.5", # Python version restrictions - # Manual control if final package is compressible or not, set False to prevent the .egg from being made - # zip_safe=False, -) diff --git a/src/ensembler/__init__.py b/src/ensembler/__init__.py new file mode 100644 index 0000000..7ca6afa --- /dev/null +++ b/src/ensembler/__init__.py @@ -0,0 +1,6 @@ +""" +Ensembler +Code to sample ensembles of 1D and 2D models with various algorithms. +""" + +# Add imports here diff --git a/src/ensembler/_version.py b/src/ensembler/_version.py new file mode 100644 index 0000000..9f87518 --- /dev/null +++ b/src/ensembler/_version.py @@ -0,0 +1 @@ +__version__ = "1.0.0+15.g467f3eb.dirty" diff --git a/ensembler/analysis/__init__.py b/src/ensembler/analysis/__init__.py similarity index 100% rename from ensembler/analysis/__init__.py rename to src/ensembler/analysis/__init__.py diff --git a/ensembler/analysis/freeEnergyCalculation.py b/src/ensembler/analysis/freeEnergyCalculation.py similarity index 100% rename from ensembler/analysis/freeEnergyCalculation.py rename to src/ensembler/analysis/freeEnergyCalculation.py diff --git a/ensembler/conditions/__init__.py b/src/ensembler/conditions/__init__.py similarity index 100% rename from ensembler/conditions/__init__.py rename to src/ensembler/conditions/__init__.py diff --git a/ensembler/conditions/_basicCondition.py b/src/ensembler/conditions/_basicCondition.py similarity index 100% rename from ensembler/conditions/_basicCondition.py rename to src/ensembler/conditions/_basicCondition.py diff --git a/ensembler/conditions/box_conditions.py b/src/ensembler/conditions/box_conditions.py similarity index 100% rename from ensembler/conditions/box_conditions.py rename to src/ensembler/conditions/box_conditions.py diff --git a/ensembler/conditions/restrain_conditions.py b/src/ensembler/conditions/restrain_conditions.py similarity index 97% rename from ensembler/conditions/restrain_conditions.py rename to src/ensembler/conditions/restrain_conditions.py index 907437f..14980e3 100644 --- a/ensembler/conditions/restrain_conditions.py +++ b/src/ensembler/conditions/restrain_conditions.py @@ -1,75 +1,75 @@ -from ensembler.conditions._basicCondition import _conditionCls -from ensembler.potentials import OneD -from ensembler.util.ensemblerTypes import systemCls as systemType, potentialCls as potentialType, Iterable, Number, \ - Union, Tuple, NoReturn - - -class Restraint(_conditionCls): - pass - - -class positionRestraintCondition(Restraint): - """ - The position restraint is adding a bias to a certain position selected in the coordinate space - """ - name: str = "position restraint" - position_0: Union[Number, Iterable[Number]] - functional: potentialType - - def __init__(self, position_0: Union[Number, Iterable[Number]], every_step: int = 1, - restraint_functional: potentialType = OneD.harmonicOscillatorPotential, system: systemType = None, - verbose: bool = False): - """ - __init__ - builds the bias to a certain position_0 in the coordinate space, which is applied every_step - Parameters - ---------- - position_0: Union[Number, Iterable[Number]] - coordinate space position - every_step: int, optional - apply bias every_step. (default=1) - restraint_functional: potentialType, optional - potential function for the bias (default: harmonic oscillator) - system: systemType, optional - system to couple to (default: None -> not coupled) - verbose: bool, optional - shall I tell you a story? - """ - self.position_0 = position_0 - self.functional = restraint_functional(x_shift=position_0) - - super().__init__(system=system, tau=every_step, verbose=verbose) - - def __str__(self) -> str: - msg = self.name + "\n" - msg += "\tDimensions: " + str(self.nDimensions) + "\n" - msg += "\tReference Position: " + str(self.position_0) + "\n" - msg += "\tapply every step: " + str(self._tau) + "\n" - msg += "\tfunctional: " + str(self.functional.name) + "\n" - return msg - - def apply(self, current_position: Union[Iterable[Number], Number]) -> Tuple[Number, Number]: - """ - apply - applies the condition - - Parameters - ---------- - current_position: Union[Iterable[Number], Number] - the current to position to bias - - Returns - ------- - Tuple[Number, Number] - the potential energy bias, the force bias. - """ - return self.functional.ene(current_position), self.functional.force(current_position) - - def apply_coupled(self) -> NoReturn: - """ - apply the condition to the coupled system. - """ - if self.system.step % self._tau == 0: - new_current_position, new_current_velocity = self.apply(current_position=self.system._currentPosition) - self.system._currentPosition += new_current_position - self.system._currentForce += new_current_velocity +from ensembler.conditions._basicCondition import _conditionCls +from ensembler.potentials import OneD +from ensembler.util.ensemblerTypes import systemCls as systemType, potentialCls as potentialType, Iterable, Number, \ + Union, Tuple, NoReturn + + +class Restraint(_conditionCls): + pass + + +class positionRestraintCondition(Restraint): + """ + The position restraint is adding a bias to a certain position selected in the coordinate space + """ + name: str = "position restraint" + position_0: Union[Number, Iterable[Number]] + functional: potentialType + + def __init__(self, position_0: Union[Number, Iterable[Number]], every_step: int = 1, + restraint_functional: potentialType = OneD.harmonicOscillatorPotential, system: systemType = None, + verbose: bool = False): + """ + __init__ + builds the bias to a certain position_0 in the coordinate space, which is applied every_step + Parameters + ---------- + position_0: Union[Number, Iterable[Number]] + coordinate space position + every_step: int, optional + apply bias every_step. (default=1) + restraint_functional: potentialType, optional + potential function for the bias (default: harmonic oscillator) + system: systemType, optional + system to couple to (default: None -> not coupled) + verbose: bool, optional + shall I tell you a story? + """ + self.position_0 = position_0 + self.functional = restraint_functional(x_shift=position_0) + + super().__init__(system=system, tau=every_step, verbose=verbose) + + def __str__(self) -> str: + msg = self.name + "\n" + msg += "\tDimensions: " + str(self.nDimensions) + "\n" + msg += "\tReference Position: " + str(self.position_0) + "\n" + msg += "\tapply every step: " + str(self._tau) + "\n" + msg += "\tfunctional: " + str(self.functional.name) + "\n" + return msg + + def apply(self, current_position: Union[Iterable[Number], Number]) -> Tuple[Number, Number]: + """ + apply + applies the condition + + Parameters + ---------- + current_position: Union[Iterable[Number], Number] + the current to position to bias + + Returns + ------- + Tuple[Number, Number] + the potential energy bias, the force bias. + """ + return self.functional.ene(current_position), self.functional.force(current_position) + + def apply_coupled(self) -> NoReturn: + """ + apply the condition to the coupled system. + """ + if self.system.step % self._tau == 0: + new_current_position, new_current_velocity = self.apply(current_position=self.system._currentPosition) + self.system._currentPosition += new_current_position + self.system._currentForce += new_current_velocity diff --git a/ensembler/conditions/thermostats.py b/src/ensembler/conditions/thermostats.py similarity index 100% rename from ensembler/conditions/thermostats.py rename to src/ensembler/conditions/thermostats.py diff --git a/ensembler/data/README.md b/src/ensembler/data/README.md similarity index 100% rename from ensembler/data/README.md rename to src/ensembler/data/README.md diff --git a/ensembler/ensemble/__init__.py b/src/ensembler/ensemble/__init__.py similarity index 100% rename from ensembler/ensemble/__init__.py rename to src/ensembler/ensemble/__init__.py diff --git a/ensembler/ensemble/_replica_graph.py b/src/ensembler/ensemble/_replica_graph.py similarity index 100% rename from ensembler/ensemble/_replica_graph.py rename to src/ensembler/ensemble/_replica_graph.py diff --git a/ensembler/ensemble/exchange_pattern.py b/src/ensembler/ensemble/exchange_pattern.py similarity index 100% rename from ensembler/ensemble/exchange_pattern.py rename to src/ensembler/ensemble/exchange_pattern.py diff --git a/ensembler/ensemble/replica_exchange.py b/src/ensembler/ensemble/replica_exchange.py similarity index 100% rename from ensembler/ensemble/replica_exchange.py rename to src/ensembler/ensemble/replica_exchange.py diff --git a/ensembler/ensemble/replicas_dynamic_parameters.py b/src/ensembler/ensemble/replicas_dynamic_parameters.py similarity index 100% rename from ensembler/ensemble/replicas_dynamic_parameters.py rename to src/ensembler/ensemble/replicas_dynamic_parameters.py diff --git a/ensembler/potentials/ND.py b/src/ensembler/potentials/ND.py similarity index 100% rename from ensembler/potentials/ND.py rename to src/ensembler/potentials/ND.py diff --git a/ensembler/potentials/OneD.py b/src/ensembler/potentials/OneD.py similarity index 100% rename from ensembler/potentials/OneD.py rename to src/ensembler/potentials/OneD.py diff --git a/ensembler/potentials/TwoD.py b/src/ensembler/potentials/TwoD.py similarity index 100% rename from ensembler/potentials/TwoD.py rename to src/ensembler/potentials/TwoD.py diff --git a/ensembler/potentials/__init__.py b/src/ensembler/potentials/__init__.py similarity index 96% rename from ensembler/potentials/__init__.py rename to src/ensembler/potentials/__init__.py index 42fb98e..b63223b 100644 --- a/ensembler/potentials/__init__.py +++ b/src/ensembler/potentials/__init__.py @@ -1,5 +1,5 @@ -""" -Definition of the potential energy functions, that can be explored by the simulations. -""" - -from . import OneD, TwoD, ND +""" +Definition of the potential energy functions, that can be explored by the simulations. +""" + +from . import OneD, TwoD, ND diff --git a/ensembler/potentials/_basicPotentials.py b/src/ensembler/potentials/_basicPotentials.py similarity index 100% rename from ensembler/potentials/_basicPotentials.py rename to src/ensembler/potentials/_basicPotentials.py diff --git a/ensembler/samplers/__init__.py b/src/ensembler/samplers/__init__.py similarity index 100% rename from ensembler/samplers/__init__.py rename to src/ensembler/samplers/__init__.py diff --git a/ensembler/samplers/_basicSamplers.py b/src/ensembler/samplers/_basicSamplers.py similarity index 100% rename from ensembler/samplers/_basicSamplers.py rename to src/ensembler/samplers/_basicSamplers.py diff --git a/ensembler/samplers/newtonian.py b/src/ensembler/samplers/newtonian.py similarity index 100% rename from ensembler/samplers/newtonian.py rename to src/ensembler/samplers/newtonian.py diff --git a/ensembler/samplers/optimizers.py b/src/ensembler/samplers/optimizers.py similarity index 100% rename from ensembler/samplers/optimizers.py rename to src/ensembler/samplers/optimizers.py diff --git a/ensembler/samplers/stochastic.py b/src/ensembler/samplers/stochastic.py similarity index 100% rename from ensembler/samplers/stochastic.py rename to src/ensembler/samplers/stochastic.py diff --git a/ensembler/system/__init__.py b/src/ensembler/system/__init__.py similarity index 97% rename from ensembler/system/__init__.py rename to src/ensembler/system/__init__.py index ad92739..2ddabad 100644 --- a/ensembler/system/__init__.py +++ b/src/ensembler/system/__init__.py @@ -1,5 +1,5 @@ -""" -Systems wrap the sampler and the potential class to allow easy orchestration of simulations. -""" -from . import basic_system, eds_system, perturbed_system -from .basic_system import system +""" +Systems wrap the sampler and the potential class to allow easy orchestration of simulations. +""" +from . import basic_system, eds_system, perturbed_system +from .basic_system import system diff --git a/ensembler/system/basic_system.py b/src/ensembler/system/basic_system.py similarity index 100% rename from ensembler/system/basic_system.py rename to src/ensembler/system/basic_system.py diff --git a/ensembler/system/eds_system.py b/src/ensembler/system/eds_system.py similarity index 97% rename from ensembler/system/eds_system.py rename to src/ensembler/system/eds_system.py index 2a6df9a..529e50f 100644 --- a/ensembler/system/eds_system.py +++ b/src/ensembler/system/eds_system.py @@ -1,229 +1,229 @@ -""" -Module: System - This module shall be used to implement subclasses of system. It wraps all information needed and generated by a simulation. -""" - -import numpy as np -import pandas as pd - -pd.options.mode.use_inf_as_na = True - -from ensembler.util.ensemblerTypes import samplerCls, conditionCls, Number, Iterable, Union - -from ensembler.util import dataStructure as data -from ensembler.potentials import OneD as pot -from ensembler.samplers.stochastic import metropolisMonteCarloIntegrator - -from ensembler.system.basic_system import system - - -class edsSystem(system): - """ - The EDS-System is collecting and providing information essential to EDS. - The Trajectory contains s and Eoff values for each step. - Functions like set_s (or simply access s) or set_eoff (or simply access Eoff) give direct acces to the EDS potential. - - """ - name = "eds system" - # EDS Dependend Settings - state = data.envelopedPStstate - - current_state: data.envelopedPStstate - potential: pot.envelopedPotential - - # current lambda - _currentEdsS: Number = np.nan - _currentEdsEoffs: Iterable[Number] = np.nan - - """ - Attributes - """ - - @property - def s(self) -> Number: - """ - s - smoothing parameter for the EDS-potential - """ - return self._currentEdsS - - @s.setter - def s(self, s: Number): - self._currentEdsS = s - self.potential.set_s(self._currentEdsS) - self.update_system_properties() - - def set_s(self, s: Number): - """ - set_s - setting a new s-value to the system. - - Parameters - ---------- - s: Number - the new smoothing parameter s - - """ - self.s = s - - @property - def eoff(self): - """ - Eoff - Energy Offsets for the EDS-potential - - """ - return self._currentEdsEoffs - - @eoff.setter - def eoff(self, eoff: Iterable[Number]): - self._currentEdsEoffs = eoff - self.potential.Eoff_i = self._currentEdsEoffs - self.update_system_properties() - - def set_eoff(self, eoff: Iterable[Number]): - """ - set_Eoff - setting new Energy offsets. - - Parameters - ---------- - eoff: Iterable[Number] - vector of new energy offsets - - """ - self.eoff = eoff - - """ - Magics - """ - - def __init__(self, potential: pot.envelopedPotential = pot.envelopedPotential( - V_is=[pot.harmonicOscillatorPotential(x_shift=2), pot.harmonicOscillatorPotential(x_shift=-2)], eoff=[0, 0]), - sampler: samplerCls = metropolisMonteCarloIntegrator(), - conditions: Iterable[conditionCls] = [], - temperature: float = 298.0, start_position: Union[Number, Iterable[Number]] = None, - eds_s: float = 1, eds_Eoff: Iterable[Number] = [0, 0]): - """ - __init__ - construct a eds-System that can be used to manage a simulation. - - Parameters - ---------- - potential: pot.envelopedPotential, optional - potential function class to be explored by sampling - sampler: sampler, optional - sampling method, that allows exploring the potential function - conditions: Iterable[condition], optional - conditions that shall be applied to the system. - temperature: float, optional - The temperature of the system (default: 298K) - start_position: - starting position for the simulation and setup of the system. - eds_s: float, optional - is the S-value of the EDS-Potential - eds_Eoff: Iterable[Number], optional - giving the energy offsets for the - - """ - ################################ - # Declare Attributes - ################################# - - self._currentEdsS = eds_s - self._currentEdsEoffs = eds_Eoff - self.state = data.envelopedPStstate - - super().__init__(potential=potential, sampler=sampler, conditions=conditions, temperature=temperature, - start_position=start_position) - - # Output - self.set_s(self._currentEdsS) - self.set_eoff(self._currentEdsEoffs) - - """ - Overwrite Functions to adapt to EDS - """ - - def set_current_state(self, current_position: Union[Number, Iterable[Number]], - current_velocities: Union[Number, Iterable[Number]] = 0, - current_force: Union[Number, Iterable[Number]] = 0, - current_temperature: Union[Number, Iterable[Number]] = 298, - current_s: Union[Number, Iterable[Number]] = 1.0, - current_eoff: Iterable[Number] = None): - """ - set_current_state - set s the current state to the given variables. - - Parameters - ---------- - current_position: Union[Number, Iterable[Number]] - The new Position - current_velocities: Union[Number, Iterable[Number]], - The new Velocities - current_force: Union[Number, Iterable[Number]], - The new Forces of the system - current_temperature: Union[Number, Iterable[Number]], - The new S_value - current_s: Union[Number, Iterable[Number]], - the new s values. - current_eoff: Iterable[Number], - the new eoff values. - """ - self._currentPosition = current_position - self._currentForce = current_force - self._currentVelocities = current_velocities - self._currentTemperature = current_temperature - - self._currentEdsS = current_s - if (current_eoff is None): - self._currentEdsEoffs = [0 for x in range(self.nStates)] - else: - self._currentEdsEoffs = current_eoff - - self.update_system_properties() - self.update_current_state() - - def update_current_state(self): - """ - updateCurrentState - This function updates the current state from the _current Variables. - """ - self._currentState = self.state(position=self._currentPosition, temperature=self._currentTemperature, - total_system_energy=self._currentTotE, - total_potential_energy=self._currentTotPot, - total_kinetic_energy=self._currentTotKin, - dhdpos=self._currentForce, velocity=self._currentVelocities, - s=self._currentEdsS, eoff=self._currentEdsEoffs) - - def append_state(self, new_position: Union[Number, Iterable[Number]], new_velocity: Union[Number, Iterable[Number]], - new_forces: Union[Number, Iterable[Number]], new_s: Union[Number, Iterable[Number]], - new_eoff: Iterable[Number]): - """ - append_state - Append a new state to the trajectory. - - Parameters - ---------- - new_position: Union[Number, Iterable[Number]] - new position for the system - new_velocity: Union[Number, Iterable[Number]] - new velocity for the system - new_forces: Union[Number, Iterable[Number]] - new forces for the system - new_s: Union[Number, Iterable[Number]] - new s-value for the system - new_eoff: Iterable[Number] - new eoffs for the system - """ - - self._currentPosition = new_position - self._currentVelocities = new_velocity - self._currentForce = new_forces - self._currentEdsS = new_s - self._currentEdsEoffs = new_eoff - - self.update_system_properties() - self.update_current_state() - - self._trajectory.append(self.current_state) +""" +Module: System + This module shall be used to implement subclasses of system. It wraps all information needed and generated by a simulation. +""" + +import numpy as np +import pandas as pd + +pd.options.mode.use_inf_as_na = True + +from ensembler.util.ensemblerTypes import samplerCls, conditionCls, Number, Iterable, Union + +from ensembler.util import dataStructure as data +from ensembler.potentials import OneD as pot +from ensembler.samplers.stochastic import metropolisMonteCarloIntegrator + +from ensembler.system.basic_system import system + + +class edsSystem(system): + """ + The EDS-System is collecting and providing information essential to EDS. + The Trajectory contains s and Eoff values for each step. + Functions like set_s (or simply access s) or set_eoff (or simply access Eoff) give direct acces to the EDS potential. + + """ + name = "eds system" + # EDS Dependend Settings + state = data.envelopedPStstate + + current_state: data.envelopedPStstate + potential: pot.envelopedPotential + + # current lambda + _currentEdsS: Number = np.nan + _currentEdsEoffs: Iterable[Number] = np.nan + + """ + Attributes + """ + + @property + def s(self) -> Number: + """ + s + smoothing parameter for the EDS-potential + """ + return self._currentEdsS + + @s.setter + def s(self, s: Number): + self._currentEdsS = s + self.potential.set_s(self._currentEdsS) + self.update_system_properties() + + def set_s(self, s: Number): + """ + set_s + setting a new s-value to the system. + + Parameters + ---------- + s: Number + the new smoothing parameter s + + """ + self.s = s + + @property + def eoff(self): + """ + Eoff + Energy Offsets for the EDS-potential + + """ + return self._currentEdsEoffs + + @eoff.setter + def eoff(self, eoff: Iterable[Number]): + self._currentEdsEoffs = eoff + self.potential.Eoff_i = self._currentEdsEoffs + self.update_system_properties() + + def set_eoff(self, eoff: Iterable[Number]): + """ + set_Eoff + setting new Energy offsets. + + Parameters + ---------- + eoff: Iterable[Number] + vector of new energy offsets + + """ + self.eoff = eoff + + """ + Magics + """ + + def __init__(self, potential: pot.envelopedPotential = pot.envelopedPotential( + V_is=[pot.harmonicOscillatorPotential(x_shift=2), pot.harmonicOscillatorPotential(x_shift=-2)], eoff=[0, 0]), + sampler: samplerCls = metropolisMonteCarloIntegrator(), + conditions: Iterable[conditionCls] = [], + temperature: float = 298.0, start_position: Union[Number, Iterable[Number]] = None, + eds_s: float = 1, eds_Eoff: Iterable[Number] = [0, 0]): + """ + __init__ + construct a eds-System that can be used to manage a simulation. + + Parameters + ---------- + potential: pot.envelopedPotential, optional + potential function class to be explored by sampling + sampler: sampler, optional + sampling method, that allows exploring the potential function + conditions: Iterable[condition], optional + conditions that shall be applied to the system. + temperature: float, optional + The temperature of the system (default: 298K) + start_position: + starting position for the simulation and setup of the system. + eds_s: float, optional + is the S-value of the EDS-Potential + eds_Eoff: Iterable[Number], optional + giving the energy offsets for the + + """ + ################################ + # Declare Attributes + ################################# + + self._currentEdsS = eds_s + self._currentEdsEoffs = eds_Eoff + self.state = data.envelopedPStstate + + super().__init__(potential=potential, sampler=sampler, conditions=conditions, temperature=temperature, + start_position=start_position) + + # Output + self.set_s(self._currentEdsS) + self.set_eoff(self._currentEdsEoffs) + + """ + Overwrite Functions to adapt to EDS + """ + + def set_current_state(self, current_position: Union[Number, Iterable[Number]], + current_velocities: Union[Number, Iterable[Number]] = 0, + current_force: Union[Number, Iterable[Number]] = 0, + current_temperature: Union[Number, Iterable[Number]] = 298, + current_s: Union[Number, Iterable[Number]] = 1.0, + current_eoff: Iterable[Number] = None): + """ + set_current_state + set s the current state to the given variables. + + Parameters + ---------- + current_position: Union[Number, Iterable[Number]] + The new Position + current_velocities: Union[Number, Iterable[Number]], + The new Velocities + current_force: Union[Number, Iterable[Number]], + The new Forces of the system + current_temperature: Union[Number, Iterable[Number]], + The new S_value + current_s: Union[Number, Iterable[Number]], + the new s values. + current_eoff: Iterable[Number], + the new eoff values. + """ + self._currentPosition = current_position + self._currentForce = current_force + self._currentVelocities = current_velocities + self._currentTemperature = current_temperature + + self._currentEdsS = current_s + if (current_eoff is None): + self._currentEdsEoffs = [0 for x in range(self.nStates)] + else: + self._currentEdsEoffs = current_eoff + + self.update_system_properties() + self.update_current_state() + + def update_current_state(self): + """ + updateCurrentState + This function updates the current state from the _current Variables. + """ + self._currentState = self.state(position=self._currentPosition, temperature=self._currentTemperature, + total_system_energy=self._currentTotE, + total_potential_energy=self._currentTotPot, + total_kinetic_energy=self._currentTotKin, + dhdpos=self._currentForce, velocity=self._currentVelocities, + s=self._currentEdsS, eoff=self._currentEdsEoffs) + + def append_state(self, new_position: Union[Number, Iterable[Number]], new_velocity: Union[Number, Iterable[Number]], + new_forces: Union[Number, Iterable[Number]], new_s: Union[Number, Iterable[Number]], + new_eoff: Iterable[Number]): + """ + append_state + Append a new state to the trajectory. + + Parameters + ---------- + new_position: Union[Number, Iterable[Number]] + new position for the system + new_velocity: Union[Number, Iterable[Number]] + new velocity for the system + new_forces: Union[Number, Iterable[Number]] + new forces for the system + new_s: Union[Number, Iterable[Number]] + new s-value for the system + new_eoff: Iterable[Number] + new eoffs for the system + """ + + self._currentPosition = new_position + self._currentVelocities = new_velocity + self._currentForce = new_forces + self._currentEdsS = new_s + self._currentEdsEoffs = new_eoff + + self.update_system_properties() + self.update_current_state() + + self._trajectory.append(self.current_state) diff --git a/ensembler/system/perturbed_system.py b/src/ensembler/system/perturbed_system.py similarity index 100% rename from ensembler/system/perturbed_system.py rename to src/ensembler/system/perturbed_system.py diff --git a/ensembler/tests/__init__.py b/src/ensembler/tests/__init__.py similarity index 100% rename from ensembler/tests/__init__.py rename to src/ensembler/tests/__init__.py diff --git a/ensembler/tests/test_analysis.py b/src/ensembler/tests/test_analysis.py similarity index 96% rename from ensembler/tests/test_analysis.py rename to src/ensembler/tests/test_analysis.py index 68a7b84..f33c14a 100644 --- a/ensembler/tests/test_analysis.py +++ b/src/ensembler/tests/test_analysis.py @@ -1,135 +1,135 @@ -import numpy as np -import unittest - - -from ensembler.analysis.freeEnergyCalculation import zwanzigEquation, threeStateZwanzig, bennetAcceptanceRatio - -class test_ZwanzigEquation(unittest.TestCase): - feCalculation = zwanzigEquation - - def test_constructor(self): - print(self.feCalculation()) - - def test_free_Energy1(self): - feCalc = self.feCalculation(kT=True) - - # Ensemble Params - V1_min = 1 - V2_min = 2 - V1_noise = 0.1 - V2_noise = 0.1 - samples = 10000 - - V1 = np.random.normal(V1_min, V1_noise, samples) - V2 = np.random.normal(V2_min, V2_noise, samples) - - dF_ana = V2_min - V1_min - dF_zwanzig = feCalc.calculate(Vi=V1, Vj=V2) - - np.testing.assert_almost_equal(desired=dF_ana, actual=dF_zwanzig, decimal=2) - - -class test_BAR(test_ZwanzigEquation): - feCalculation = bennetAcceptanceRatio - - def test_free_Energy1(self): - feCalc = self.feCalculation(kT=True) - - # simulate Bar conditions - samples = 10000 - - # ensemble 1 - V1_min = 1 - V1_noise_1 = 0.1 - - V2_off = 2 - V2_noise_1 = 0.1 - - # ensemble 1 - V1_off = 2 - V1_noise_2 = 0.1 - V2_min = 1 - V2_noise_2 = 0.1 - - # Distributions - V1_1 = np.random.normal(V1_min, V1_noise_1, samples) - V2_1 = np.random.normal(V2_off, V2_noise_1, samples) - - V1_2 = np.random.normal(V1_off, V1_noise_2, samples) - V2_2 = np.random.normal(V2_min, V2_noise_2, samples) - - dF_bar = feCalc.calculate(Vi_i=V1_1, Vj_i=V2_1, Vi_j=V1_2, Vj_j=V2_2) - - print(dF_bar) - dF_ana = 1.000000000000 - np.testing.assert_almost_equal(desired=dF_ana, actual=dF_bar, decimal=2) - - -class test_BAR(test_ZwanzigEquation): - feCalculation = bennetAcceptanceRatio - - def test_free_Energy1(self): - feCalc = self.feCalculation(kT=True) - - # simulate Bar conditions - samples = 10000 - - # ensemble 1 - V1_min = 1 - V1_noise_1 = 0.01 - V2_off = 2 - V2_noise_1 = 0.01 - - # ensemble 1 - V1_off = 2 - V1_noise_2 = 0.01 - V2_min = 1 - V2_noise_2 = 0.01 - - # Distributions - V1_1 = np.random.normal(V1_min, V1_noise_1, samples) - V2_1 = np.random.normal(V2_off, V2_noise_1, samples) - - V1_2 = np.random.normal(V1_off, V1_noise_2, samples) - V2_2 = np.random.normal(V2_min, V2_noise_2, samples) - - dF_bar = feCalc.calculate(Vi_i=V1_1, Vj_i=V2_1, Vi_j=V1_2, Vj_j=V2_2, verbose=True) - - print(dF_bar) - dF_ana = 0.000000000000 - np.testing.assert_almost_equal(desired=dF_ana, actual=dF_bar, decimal=2) - - -class test_threeStateZwanzigReweighting(test_ZwanzigEquation): - feCalculation = threeStateZwanzig - - - def test_free_Energy1(self): - feCalc = self.feCalculation(kT=True) - - sample_state1 = 10000 - sample_state2 = 10000 - - # State 1 description - state_1 = 1 - state_1_noise = 0.01 - - # State 2 description - state_2 = 1 - state_2_noise = 0.01 - - # OffSampling - energy_off_state = 10 - noise_off_state = 0.01 - - V1 = np.concatenate([np.random.normal(state_1, state_1_noise, sample_state1), - np.random.normal(energy_off_state, noise_off_state, sample_state2)]) - V2 = np.concatenate([np.random.normal(energy_off_state, noise_off_state, sample_state1), - np.random.normal(state_2, state_2_noise, sample_state2)]) - Vr = np.concatenate([np.random.normal(state_1, state_1_noise, sample_state1), - np.random.normal(state_2, state_2_noise, sample_state2)]) - - dF_ana = state_2 - state_1 - dFRew_zwanz = feCalc.calculate(Vi=V1, Vj=V2, Vr=Vr) - - np.testing.assert_almost_equal(desired=dF_ana, actual=dFRew_zwanz, decimal=2) +import numpy as np +import unittest + + +from ensembler.analysis.freeEnergyCalculation import zwanzigEquation, threeStateZwanzig, bennetAcceptanceRatio + +class test_ZwanzigEquation(unittest.TestCase): + feCalculation = zwanzigEquation + + def test_constructor(self): + print(self.feCalculation()) + + def test_free_Energy1(self): + feCalc = self.feCalculation(kT=True) + + # Ensemble Params + V1_min = 1 + V2_min = 2 + V1_noise = 0.1 + V2_noise = 0.1 + samples = 10000 + + V1 = np.random.normal(V1_min, V1_noise, samples) + V2 = np.random.normal(V2_min, V2_noise, samples) + + dF_ana = V2_min - V1_min + dF_zwanzig = feCalc.calculate(Vi=V1, Vj=V2) + + np.testing.assert_almost_equal(desired=dF_ana, actual=dF_zwanzig, decimal=2) + + +class test_BAR(test_ZwanzigEquation): + feCalculation = bennetAcceptanceRatio + + def test_free_Energy1(self): + feCalc = self.feCalculation(kT=True) + + # simulate Bar conditions + samples = 10000 + + # ensemble 1 + V1_min = 1 + V1_noise_1 = 0.1 + + V2_off = 2 + V2_noise_1 = 0.1 + + # ensemble 1 + V1_off = 2 + V1_noise_2 = 0.1 + V2_min = 1 + V2_noise_2 = 0.1 + + # Distributions + V1_1 = np.random.normal(V1_min, V1_noise_1, samples) + V2_1 = np.random.normal(V2_off, V2_noise_1, samples) + + V1_2 = np.random.normal(V1_off, V1_noise_2, samples) + V2_2 = np.random.normal(V2_min, V2_noise_2, samples) + + dF_bar = feCalc.calculate(Vi_i=V1_1, Vj_i=V2_1, Vi_j=V1_2, Vj_j=V2_2) + + print(dF_bar) + dF_ana = 1.000000000000 + np.testing.assert_almost_equal(desired=dF_ana, actual=dF_bar, decimal=2) + + +class test_BAR(test_ZwanzigEquation): + feCalculation = bennetAcceptanceRatio + + def test_free_Energy1(self): + feCalc = self.feCalculation(kT=True) + + # simulate Bar conditions + samples = 10000 + + # ensemble 1 + V1_min = 1 + V1_noise_1 = 0.01 + V2_off = 2 + V2_noise_1 = 0.01 + + # ensemble 1 + V1_off = 2 + V1_noise_2 = 0.01 + V2_min = 1 + V2_noise_2 = 0.01 + + # Distributions + V1_1 = np.random.normal(V1_min, V1_noise_1, samples) + V2_1 = np.random.normal(V2_off, V2_noise_1, samples) + + V1_2 = np.random.normal(V1_off, V1_noise_2, samples) + V2_2 = np.random.normal(V2_min, V2_noise_2, samples) + + dF_bar = feCalc.calculate(Vi_i=V1_1, Vj_i=V2_1, Vi_j=V1_2, Vj_j=V2_2, verbose=True) + + print(dF_bar) + dF_ana = 0.000000000000 + np.testing.assert_almost_equal(desired=dF_ana, actual=dF_bar, decimal=2) + + +class test_threeStateZwanzigReweighting(test_ZwanzigEquation): + feCalculation = threeStateZwanzig + + + def test_free_Energy1(self): + feCalc = self.feCalculation(kT=True) + + sample_state1 = 10000 + sample_state2 = 10000 + + # State 1 description + state_1 = 1 + state_1_noise = 0.01 + + # State 2 description + state_2 = 1 + state_2_noise = 0.01 + + # OffSampling + energy_off_state = 10 + noise_off_state = 0.01 + + V1 = np.concatenate([np.random.normal(state_1, state_1_noise, sample_state1), + np.random.normal(energy_off_state, noise_off_state, sample_state2)]) + V2 = np.concatenate([np.random.normal(energy_off_state, noise_off_state, sample_state1), + np.random.normal(state_2, state_2_noise, sample_state2)]) + Vr = np.concatenate([np.random.normal(state_1, state_1_noise, sample_state1), + np.random.normal(state_2, state_2_noise, sample_state2)]) + + dF_ana = state_2 - state_1 + dFRew_zwanz = feCalc.calculate(Vi=V1, Vj=V2, Vr=Vr) + + np.testing.assert_almost_equal(desired=dF_ana, actual=dFRew_zwanz, decimal=2) diff --git a/ensembler/tests/test_conditions.py b/src/ensembler/tests/test_conditions.py similarity index 97% rename from ensembler/tests/test_conditions.py rename to src/ensembler/tests/test_conditions.py index b9fe0f0..caaffa2 100644 --- a/ensembler/tests/test_conditions.py +++ b/src/ensembler/tests/test_conditions.py @@ -1,233 +1,233 @@ -import numpy as np -import os -import tempfile -import unittest - -from ensembler.conditions.box_conditions import periodicBoundaryCondition, boxBoundaryCondition -from ensembler.conditions.restrain_conditions import positionRestraintCondition - - - -class boxBoundaryCondition(unittest.TestCase): - condition_class = boxBoundaryCondition - boundary1D = [0, 10] - boundary2D = [[0, 10], [0, 10]] - tmp_test_dir: str = None - - - def setUp(self) -> None: - test_dir = os.getcwd()+"/tests_out" - if(not os.path.exists(test_dir)): - os.mkdir(test_dir) - - if(__class__.tmp_test_dir is None): - __class__.tmp_test_dir = tempfile.mkdtemp(dir=test_dir, prefix="tmp_test_potentials") - _, self.tmp_out_path = tempfile.mkstemp(prefix="test_" + self.condition_class.name, suffix=".obj", dir=__class__.tmp_test_dir) - - def test_constructor(self): - print(self.condition_class(boundary=self.boundary1D)) - - def test_constructor2D(self): - print(self.condition_class(boundary=self.boundary2D)) - - def test_save_obj_str(self): - path = self.tmp_out_path - out_path = self.condition_class(self.boundary2D).save(path=path) - print(out_path) - - def test_load_str_path(self): - path = self.tmp_out_path - out_path = self.condition_class(self.boundary2D).save(path=path) - - cls = self.condition_class.load(path=out_path) - print(cls) - - def test_apply1D(self): - cond = self.condition_class(boundary=self.boundary2D) - cond.verbose = True - - expected_pos = [2] - expected_vel = [0.2] - position = [-2] - velocity = [-0.2] - - corr_pos, corr_vel = cond.apply(current_position=position, current_velocity=velocity) - - self.assertEqual(first=corr_pos, second=expected_pos, - msg="The position correction for the lower bound position was wrong.") - self.assertEqual(first=corr_vel, second=expected_vel, - msg="The position correction for the lower bound velocity was wrong.") - - expected_pos = [6] - expected_vel = [-2.2] - position = [14] - velocity = [2.2] - - corr_pos, corr_vel = cond.apply(current_position=position, current_velocity=velocity) - - self.assertEqual(first=corr_pos, second=expected_pos, - msg="The position correction for the lower bound position was wrong.") - self.assertEqual(first=corr_vel, second=expected_vel, - msg="The position correction for the lower bound velocity was wrong.") - - def test_apply2D(self): - cond = self.condition_class(boundary=self.boundary2D) - cond.verbose = True - expected_pos = [2, 1] - expected_vel = [-0.2, 0.5] - position = [-2, 1] - velocity = [0.2, 0.5] - - corr_pos, corr_vel = cond.apply(current_position=position, current_velocity=velocity) - - np.testing.assert_equal(corr_pos, expected_pos, - err_msg="The position correction for the lower bound position was wrong.") - np.testing.assert_equal(corr_vel, expected_vel, - err_msg="The position correction for the lower bound velocity was wrong.") - - expected_pos = [1, 2] - expected_vel = [0.2, -0.5] - position = [1, -2] - velocity = [0.2, 0.5] - - corr_pos, corr_vel = cond.apply(current_position=position, current_velocity=velocity) - - np.testing.assert_equal(corr_pos, expected_pos, - err_msg="The position correction for the lower bound position was wrong.") - np.testing.assert_equal(corr_vel, expected_vel, - err_msg="The position correction for the lower bound velocity was wrong.") - - -class periodicBoundaryCondition(unittest.TestCase): - condition_class = periodicBoundaryCondition - boundary1D = [0, 10] - boundary2D = [[0, 10], [0, 10]] - tmp_test_dir = None - - def setUp(self) -> None: - if(__class__.tmp_test_dir is None): - test_dir = os.getcwd() + "/tests_out" - if (not os.path.exists(test_dir)): - os.mkdir(test_dir) - __class__.tmp_test_dir = tempfile.mkdtemp(dir=test_dir, prefix="tmp_test_potentials") - _, self.tmp_out_path = tempfile.mkstemp(prefix="test_" + self.condition_class.name, suffix=".obj", dir=__class__.tmp_test_dir) - - def test_constructor(self): - print(self.condition_class(boundary=self.boundary1D)) - - def test_constructor2D(self): - print(self.condition_class(boundary=self.boundary2D)) - - def test_save_obj_str(self): - path = self.tmp_out_path - out_path = self.condition_class(self.boundary2D).save(path=path) - print(out_path) - - def test_load_str_path(self): - path = self.tmp_out_path - out_path = self.condition_class(self.boundary2D).save(path=path) - - cls = self.condition_class.load(path=out_path) - print(cls) - - def test_apply1D(self): - cond = self.condition_class(boundary=self.boundary1D) - cond.verbose = True - - expected_pos = np.array([8]) - expected_vel = np.array([3]) - position = [-2] - vel = [-3] - corr_pos = cond.apply(current_position=position) - - self.assertEqual(second=corr_pos, first=expected_pos, - msg="The position correction for the lower bound position was wrong.") - - def test_apply2D(self): - cond = self.condition_class(boundary=self.boundary2D) - cond.verbose = True - - expected_pos = [8, 1] - expected_vel = np.array([3, 3]) - position = [-2, 1] - vel = [-3, 3] - corr_pos = cond.apply(current_position=position) - - np.testing.assert_equal(corr_pos, expected_pos, - err_msg="The position correction for the lower bound position was wrong.") - - -class positionRestraintCondition(unittest.TestCase): - condition_class = positionRestraintCondition - tmp_test_dir = None - - def setUp(self) -> None: - test_dir = os.getcwd()+"/tests_out" - if(not os.path.exists(test_dir)): - os.mkdir(test_dir) - - if(__class__.tmp_test_dir is None): - __class__.tmp_test_dir = tempfile.mkdtemp(dir=test_dir, prefix="tmp_test_conditions") - _, self.tmp_out_path = tempfile.mkstemp(prefix="test_" + self.condition_class.name, suffix=".obj", dir=__class__.tmp_test_dir) - - def test_constructor(self): - print(self.condition_class(position_0=1)) - - def test_save_obj_str(self): - path = self.tmp_out_path - out_path = self.condition_class(position_0=1).save(path=path) - print(out_path) - - def test_load_str_path(self): - path = self.tmp_out_path - out_path = self.condition_class(position_0=1).save(path=path) - - cls = self.condition_class.load(path=out_path) - print(cls) - - def test_apply1D(self): - cond = self.condition_class(position_0=1) - cond.verbose = True - - expected_pos = [4.5] - expected_force = [-3] - position = [-2] - - corr_pos, corr_force = cond.apply(current_position=position) - self.assertEqual(second=corr_pos, first=expected_pos, - msg="The position correction for the lower bound position was wrong.") - self.assertEqual(second=corr_force, first=expected_force, - msg="The position correction for the lower bound force was wrong.") - - expected_pos = [84.5] - expected_force = [13] - position = [14] - - corr_pos, corr_force = cond.apply(current_position=position) - self.assertEqual(second=corr_pos, first=expected_pos, - msg="The position correction for the lower bound position was wrong.") - self.assertEqual(second=corr_force, first=expected_force, - msg="The position correction for the lower bound force was wrong.") - - def test_apply2D(self): - cond = self.condition_class(position_0=1) - cond.verbose = True - expected_pos = [4.5, 0] - expected_force = [-3, 0] - position = [-2, 1] - - corr_pos, corr_force = cond.apply(current_position=position) - np.testing.assert_equal(corr_pos, expected_pos, - err_msg="The position correction for the lower bound position was wrong.") - np.testing.assert_equal(corr_force, expected_force, - err_msg="The position correction for the lower bound force was wrong.") - - expected_pos = [0, 4.5] - expected_force = [0, -3] - position = [1, -2] - - corr_pos, corr_force = cond.apply(current_position=position) - np.testing.assert_equal(corr_pos, expected_pos, - err_msg="The position correction for the lower bound position was wrong.") - np.testing.assert_equal(corr_force, expected_force, - err_msg="The position correction for the lower bound force was wrong.") +import numpy as np +import os +import tempfile +import unittest + +from ensembler.conditions.box_conditions import periodicBoundaryCondition, boxBoundaryCondition +from ensembler.conditions.restrain_conditions import positionRestraintCondition + + + +class boxBoundaryCondition(unittest.TestCase): + condition_class = boxBoundaryCondition + boundary1D = [0, 10] + boundary2D = [[0, 10], [0, 10]] + tmp_test_dir: str = None + + + def setUp(self) -> None: + test_dir = os.getcwd()+"/tests_out" + if(not os.path.exists(test_dir)): + os.mkdir(test_dir) + + if(__class__.tmp_test_dir is None): + __class__.tmp_test_dir = tempfile.mkdtemp(dir=test_dir, prefix="tmp_test_potentials") + _, self.tmp_out_path = tempfile.mkstemp(prefix="test_" + self.condition_class.name, suffix=".obj", dir=__class__.tmp_test_dir) + + def test_constructor(self): + print(self.condition_class(boundary=self.boundary1D)) + + def test_constructor2D(self): + print(self.condition_class(boundary=self.boundary2D)) + + def test_save_obj_str(self): + path = self.tmp_out_path + out_path = self.condition_class(self.boundary2D).save(path=path) + print(out_path) + + def test_load_str_path(self): + path = self.tmp_out_path + out_path = self.condition_class(self.boundary2D).save(path=path) + + cls = self.condition_class.load(path=out_path) + print(cls) + + def test_apply1D(self): + cond = self.condition_class(boundary=self.boundary2D) + cond.verbose = True + + expected_pos = [2] + expected_vel = [0.2] + position = [-2] + velocity = [-0.2] + + corr_pos, corr_vel = cond.apply(current_position=position, current_velocity=velocity) + + self.assertEqual(first=corr_pos, second=expected_pos, + msg="The position correction for the lower bound position was wrong.") + self.assertEqual(first=corr_vel, second=expected_vel, + msg="The position correction for the lower bound velocity was wrong.") + + expected_pos = [6] + expected_vel = [-2.2] + position = [14] + velocity = [2.2] + + corr_pos, corr_vel = cond.apply(current_position=position, current_velocity=velocity) + + self.assertEqual(first=corr_pos, second=expected_pos, + msg="The position correction for the lower bound position was wrong.") + self.assertEqual(first=corr_vel, second=expected_vel, + msg="The position correction for the lower bound velocity was wrong.") + + def test_apply2D(self): + cond = self.condition_class(boundary=self.boundary2D) + cond.verbose = True + expected_pos = [2, 1] + expected_vel = [-0.2, 0.5] + position = [-2, 1] + velocity = [0.2, 0.5] + + corr_pos, corr_vel = cond.apply(current_position=position, current_velocity=velocity) + + np.testing.assert_equal(corr_pos, expected_pos, + err_msg="The position correction for the lower bound position was wrong.") + np.testing.assert_equal(corr_vel, expected_vel, + err_msg="The position correction for the lower bound velocity was wrong.") + + expected_pos = [1, 2] + expected_vel = [0.2, -0.5] + position = [1, -2] + velocity = [0.2, 0.5] + + corr_pos, corr_vel = cond.apply(current_position=position, current_velocity=velocity) + + np.testing.assert_equal(corr_pos, expected_pos, + err_msg="The position correction for the lower bound position was wrong.") + np.testing.assert_equal(corr_vel, expected_vel, + err_msg="The position correction for the lower bound velocity was wrong.") + + +class periodicBoundaryCondition(unittest.TestCase): + condition_class = periodicBoundaryCondition + boundary1D = [0, 10] + boundary2D = [[0, 10], [0, 10]] + tmp_test_dir = None + + def setUp(self) -> None: + if(__class__.tmp_test_dir is None): + test_dir = os.getcwd() + "/tests_out" + if (not os.path.exists(test_dir)): + os.mkdir(test_dir) + __class__.tmp_test_dir = tempfile.mkdtemp(dir=test_dir, prefix="tmp_test_potentials") + _, self.tmp_out_path = tempfile.mkstemp(prefix="test_" + self.condition_class.name, suffix=".obj", dir=__class__.tmp_test_dir) + + def test_constructor(self): + print(self.condition_class(boundary=self.boundary1D)) + + def test_constructor2D(self): + print(self.condition_class(boundary=self.boundary2D)) + + def test_save_obj_str(self): + path = self.tmp_out_path + out_path = self.condition_class(self.boundary2D).save(path=path) + print(out_path) + + def test_load_str_path(self): + path = self.tmp_out_path + out_path = self.condition_class(self.boundary2D).save(path=path) + + cls = self.condition_class.load(path=out_path) + print(cls) + + def test_apply1D(self): + cond = self.condition_class(boundary=self.boundary1D) + cond.verbose = True + + expected_pos = np.array([8]) + expected_vel = np.array([3]) + position = [-2] + vel = [-3] + corr_pos = cond.apply(current_position=position) + + self.assertEqual(second=corr_pos, first=expected_pos, + msg="The position correction for the lower bound position was wrong.") + + def test_apply2D(self): + cond = self.condition_class(boundary=self.boundary2D) + cond.verbose = True + + expected_pos = [8, 1] + expected_vel = np.array([3, 3]) + position = [-2, 1] + vel = [-3, 3] + corr_pos = cond.apply(current_position=position) + + np.testing.assert_equal(corr_pos, expected_pos, + err_msg="The position correction for the lower bound position was wrong.") + + +class positionRestraintCondition(unittest.TestCase): + condition_class = positionRestraintCondition + tmp_test_dir = None + + def setUp(self) -> None: + test_dir = os.getcwd()+"/tests_out" + if(not os.path.exists(test_dir)): + os.mkdir(test_dir) + + if(__class__.tmp_test_dir is None): + __class__.tmp_test_dir = tempfile.mkdtemp(dir=test_dir, prefix="tmp_test_conditions") + _, self.tmp_out_path = tempfile.mkstemp(prefix="test_" + self.condition_class.name, suffix=".obj", dir=__class__.tmp_test_dir) + + def test_constructor(self): + print(self.condition_class(position_0=1)) + + def test_save_obj_str(self): + path = self.tmp_out_path + out_path = self.condition_class(position_0=1).save(path=path) + print(out_path) + + def test_load_str_path(self): + path = self.tmp_out_path + out_path = self.condition_class(position_0=1).save(path=path) + + cls = self.condition_class.load(path=out_path) + print(cls) + + def test_apply1D(self): + cond = self.condition_class(position_0=1) + cond.verbose = True + + expected_pos = [4.5] + expected_force = [-3] + position = [-2] + + corr_pos, corr_force = cond.apply(current_position=position) + self.assertEqual(second=corr_pos, first=expected_pos, + msg="The position correction for the lower bound position was wrong.") + self.assertEqual(second=corr_force, first=expected_force, + msg="The position correction for the lower bound force was wrong.") + + expected_pos = [84.5] + expected_force = [13] + position = [14] + + corr_pos, corr_force = cond.apply(current_position=position) + self.assertEqual(second=corr_pos, first=expected_pos, + msg="The position correction for the lower bound position was wrong.") + self.assertEqual(second=corr_force, first=expected_force, + msg="The position correction for the lower bound force was wrong.") + + def test_apply2D(self): + cond = self.condition_class(position_0=1) + cond.verbose = True + expected_pos = [4.5, 0] + expected_force = [-3, 0] + position = [-2, 1] + + corr_pos, corr_force = cond.apply(current_position=position) + np.testing.assert_equal(corr_pos, expected_pos, + err_msg="The position correction for the lower bound position was wrong.") + np.testing.assert_equal(corr_force, expected_force, + err_msg="The position correction for the lower bound force was wrong.") + + expected_pos = [0, 4.5] + expected_force = [0, -3] + position = [1, -2] + + corr_pos, corr_force = cond.apply(current_position=position) + np.testing.assert_equal(corr_pos, expected_pos, + err_msg="The position correction for the lower bound position was wrong.") + np.testing.assert_equal(corr_force, expected_force, + err_msg="The position correction for the lower bound force was wrong.") diff --git a/ensembler/tests/test_conveyorBelt.py b/src/ensembler/tests/test_conveyorBelt.py similarity index 100% rename from ensembler/tests/test_conveyorBelt.py rename to src/ensembler/tests/test_conveyorBelt.py diff --git a/ensembler/tests/test_ensemble.py b/src/ensembler/tests/test_ensemble.py similarity index 100% rename from ensembler/tests/test_ensemble.py rename to src/ensembler/tests/test_ensemble.py diff --git a/ensembler/tests/test_ensembler.py b/src/ensembler/tests/test_ensembler.py similarity index 100% rename from ensembler/tests/test_ensembler.py rename to src/ensembler/tests/test_ensembler.py diff --git a/ensembler/tests/test_potentials.py b/src/ensembler/tests/test_potentials.py similarity index 100% rename from ensembler/tests/test_potentials.py rename to src/ensembler/tests/test_potentials.py diff --git a/ensembler/tests/test_sampler.py b/src/ensembler/tests/test_sampler.py similarity index 100% rename from ensembler/tests/test_sampler.py rename to src/ensembler/tests/test_sampler.py diff --git a/ensembler/tests/test_system.py b/src/ensembler/tests/test_system.py similarity index 100% rename from ensembler/tests/test_system.py rename to src/ensembler/tests/test_system.py diff --git a/ensembler/tests/test_visualization.py b/src/ensembler/tests/test_visualization.py similarity index 100% rename from ensembler/tests/test_visualization.py rename to src/ensembler/tests/test_visualization.py diff --git a/ensembler/util/__init__.py b/src/ensembler/util/__init__.py similarity index 100% rename from ensembler/util/__init__.py rename to src/ensembler/util/__init__.py diff --git a/ensembler/util/basic_class.py b/src/ensembler/util/basic_class.py similarity index 96% rename from ensembler/util/basic_class.py rename to src/ensembler/util/basic_class.py index 219aeb2..2ba1ca7 100644 --- a/ensembler/util/basic_class.py +++ b/src/ensembler/util/basic_class.py @@ -1,95 +1,95 @@ -""" -Module: basic_class - This file is giving the basic scaffold for saving & loading any Ensembler class with pickle. -""" - -import io -import pickle -import copy -from typing import Union, Callable - - -def notImplementedERR(): - raise NotImplementedError("This function needs to be implemented in sympy") - - -class _baseClass: - """ - This class is a scaffold, containing functionality all classes should have. - """ - name: str = "Unknown" - _verbose:bool =False - - def __name__(self) -> str: - return str(self.name) - - def __getstate__(self): - """ - preperation for pickling: - remove the non trivial pickling parts - """ - attribute_dict = self.__dict__ - new_dict ={} - for key in attribute_dict.keys(): - if (not isinstance(attribute_dict[key], Callable)): - new_dict.update({key:attribute_dict[key]}) - - return new_dict - - def __setstate__(self, state): - self.__dict__ = state - - def __deepcopy__(self, memo): - copy_obj = self.__class__() - copy_obj.__setstate__(copy.deepcopy(self.__getstate__())) - return copy_obj - - - - """ - Attributes - """ - @property - def verbose(self)->bool: - return self._verbose - - @verbose.setter - def verbose(self, verbose:bool): - self._verbose=verbose - - """ - Methods - """ - def save(self, path: Union[str, io.FileIO] = None) -> str: - """ - This method stores the Class as binary obj to a given path or fileBuffer. - """ - if (isinstance(path, str)): - bufferdWriter = open(path, "wb") - elif (isinstance(path, io.BufferedWriter)): - bufferdWriter = path - path = bufferdWriter.name - else: - raise IOError("Please give as parameter a path:str or a File Buffer. To " + str(self.__class__) + ".save") - - pickle.dump(obj=self, file=bufferdWriter) - bufferdWriter.close() - return path - - @classmethod - def load(cls, path: Union[str, io.FileIO] = None) -> object: - """ - This method stores the Class as binary obj to a given path or fileBuffer. - """ - if (isinstance(path, str)): - bufferedReader = open(path, "rb") - elif (isinstance(path, io.BufferedReader)): - bufferedReader = path - else: - raise IOError("Please give as parameter a path:str or a File Buffer.") - - obj = pickle.load(file=bufferedReader) - - bufferedReader.close() - - return obj +""" +Module: basic_class + This file is giving the basic scaffold for saving & loading any Ensembler class with pickle. +""" + +import io +import pickle +import copy +from typing import Union, Callable + + +def notImplementedERR(): + raise NotImplementedError("This function needs to be implemented in sympy") + + +class _baseClass: + """ + This class is a scaffold, containing functionality all classes should have. + """ + name: str = "Unknown" + _verbose:bool =False + + def __name__(self) -> str: + return str(self.name) + + def __getstate__(self): + """ + preperation for pickling: + remove the non trivial pickling parts + """ + attribute_dict = self.__dict__ + new_dict ={} + for key in attribute_dict.keys(): + if (not isinstance(attribute_dict[key], Callable)): + new_dict.update({key:attribute_dict[key]}) + + return new_dict + + def __setstate__(self, state): + self.__dict__ = state + + def __deepcopy__(self, memo): + copy_obj = self.__class__() + copy_obj.__setstate__(copy.deepcopy(self.__getstate__())) + return copy_obj + + + + """ + Attributes + """ + @property + def verbose(self)->bool: + return self._verbose + + @verbose.setter + def verbose(self, verbose:bool): + self._verbose=verbose + + """ + Methods + """ + def save(self, path: Union[str, io.FileIO] = None) -> str: + """ + This method stores the Class as binary obj to a given path or fileBuffer. + """ + if (isinstance(path, str)): + bufferdWriter = open(path, "wb") + elif (isinstance(path, io.BufferedWriter)): + bufferdWriter = path + path = bufferdWriter.name + else: + raise IOError("Please give as parameter a path:str or a File Buffer. To " + str(self.__class__) + ".save") + + pickle.dump(obj=self, file=bufferdWriter) + bufferdWriter.close() + return path + + @classmethod + def load(cls, path: Union[str, io.FileIO] = None) -> object: + """ + This method stores the Class as binary obj to a given path or fileBuffer. + """ + if (isinstance(path, str)): + bufferedReader = open(path, "rb") + elif (isinstance(path, io.BufferedReader)): + bufferedReader = path + else: + raise IOError("Please give as parameter a path:str or a File Buffer.") + + obj = pickle.load(file=bufferedReader) + + bufferedReader.close() + + return obj diff --git a/ensembler/util/dataStructure.py b/src/ensembler/util/dataStructure.py similarity index 100% rename from ensembler/util/dataStructure.py rename to src/ensembler/util/dataStructure.py diff --git a/ensembler/util/ensemblerTypes.py b/src/ensembler/util/ensemblerTypes.py similarity index 96% rename from ensembler/util/ensemblerTypes.py rename to src/ensembler/util/ensemblerTypes.py index ebcb206..fa0c25d 100644 --- a/ensembler/util/ensemblerTypes.py +++ b/src/ensembler/util/ensemblerTypes.py @@ -1,34 +1,34 @@ -""" - This file provides ensembler types, that are used to annotate and enforce certain types throughout the ensembler package. -""" - -# Generic Types - provided to all other files from here -from typing import TypeVar, Union, List, Tuple, Iterable, Dict, NoReturn -from numbers import Number - -# Dummy defs: -potentialCls = TypeVar("potential") -conditionCls = TypeVar("condition") -samplerCls = TypeVar("samplers") - -systemCls = TypeVar("system") - -ensembleCls = TypeVar("ensemble") - -# Ensembler specific Types: -""" -from ensembler.potentials._baseclasses import _potentialCls -potential = TypeVar("potential", bound=_potentialCls) - -#define here dummy type, so it is useable in sub classes -system = TypeVar("system") #dummyDef for samplers - -from ensembler.conditions._conditions import _conditionCls -condition = TypeVar("condition", bound=_conditionCls) - -from ensembler.samplers._basicIntegrators import _integratorCls -samplers = TypeVar("samplers", bound=_integratorCls) - -from ensembler.system.basic_system import system -system = TypeVar("system", bound=system) -""" +""" + This file provides ensembler types, that are used to annotate and enforce certain types throughout the ensembler package. +""" + +# Generic Types - provided to all other files from here +from typing import TypeVar, Union, List, Tuple, Iterable, Dict, NoReturn +from numbers import Number + +# Dummy defs: +potentialCls = TypeVar("potential") +conditionCls = TypeVar("condition") +samplerCls = TypeVar("samplers") + +systemCls = TypeVar("system") + +ensembleCls = TypeVar("ensemble") + +# Ensembler specific Types: +""" +from ensembler.potentials._baseclasses import _potentialCls +potential = TypeVar("potential", bound=_potentialCls) + +#define here dummy type, so it is useable in sub classes +system = TypeVar("system") #dummyDef for samplers + +from ensembler.conditions._conditions import _conditionCls +condition = TypeVar("condition", bound=_conditionCls) + +from ensembler.samplers._basicIntegrators import _integratorCls +samplers = TypeVar("samplers", bound=_integratorCls) + +from ensembler.system.basic_system import system +system = TypeVar("system", bound=system) +""" diff --git a/ensembler/visualisation/__init__.py b/src/ensembler/visualisation/__init__.py similarity index 100% rename from ensembler/visualisation/__init__.py rename to src/ensembler/visualisation/__init__.py diff --git a/ensembler/visualisation/animationSimulation.py b/src/ensembler/visualisation/animationSimulation.py similarity index 100% rename from ensembler/visualisation/animationSimulation.py rename to src/ensembler/visualisation/animationSimulation.py diff --git a/ensembler/visualisation/interactive_plots.py b/src/ensembler/visualisation/interactive_plots.py similarity index 100% rename from ensembler/visualisation/interactive_plots.py rename to src/ensembler/visualisation/interactive_plots.py diff --git a/ensembler/visualisation/plotConveyorBelt.py b/src/ensembler/visualisation/plotConveyorBelt.py similarity index 100% rename from ensembler/visualisation/plotConveyorBelt.py rename to src/ensembler/visualisation/plotConveyorBelt.py diff --git a/ensembler/visualisation/plotPotentials.py b/src/ensembler/visualisation/plotPotentials.py similarity index 100% rename from ensembler/visualisation/plotPotentials.py rename to src/ensembler/visualisation/plotPotentials.py diff --git a/ensembler/visualisation/plotSimulations.py b/src/ensembler/visualisation/plotSimulations.py similarity index 100% rename from ensembler/visualisation/plotSimulations.py rename to src/ensembler/visualisation/plotSimulations.py diff --git a/ensembler/visualisation/style.py b/src/ensembler/visualisation/style.py similarity index 100% rename from ensembler/visualisation/style.py rename to src/ensembler/visualisation/style.py diff --git a/versioneer.py b/versioneer.py deleted file mode 100644 index fccf3da..0000000 --- a/versioneer.py +++ /dev/null @@ -1,1830 +0,0 @@ -# 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) From 3965319f5a9b1baaf0696bceff4bc6e49cca3fda Mon Sep 17 00:00:00 2001 From: bries Date: Tue, 9 May 2023 10:47:12 +0200 Subject: [PATCH 02/20] update CLI --- .github/workflows/CI.yaml | 7 +------ .github/workflows/python-package.yml | 2 +- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/.github/workflows/CI.yaml b/.github/workflows/CI.yaml index 70479fe..6a55421 100644 --- a/.github/workflows/CI.yaml +++ b/.github/workflows/CI.yaml @@ -8,11 +8,6 @@ on: pull_request: branches: - "master" - schedule: - # Nightly tests run on master by default: - # Scheduled workflows run on the latest commit on the default or base branch. - # (from https://help.github.com/en/actions/reference/events-that-trigger-workflows#scheduled-events-schedule) - - cron: "0 0 * * *" jobs: test: @@ -60,7 +55,7 @@ jobs: shell: bash -l {0} run: | - pytest -v --cov=ensembler --cov-report=xml --color=yes ensembler/tests/ + pytest -v --cov=ensembler --cov-report=xml --color=yes src/ensembler/tests/ - name: Run example notebooks diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 77fe3b2..81cb9c8 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -33,7 +33,7 @@ jobs: flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | - pip install -r devtools/pip_requirements/requirements_unix.txt + pip install . pytest conda-unix-build: From c91ce1e99f64602c1634ce1c6109400d7435a3b5 Mon Sep 17 00:00:00 2001 From: bries Date: Tue, 9 May 2023 10:49:59 +0200 Subject: [PATCH 03/20] update CI --- .github/workflows/CI.yaml | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/.github/workflows/CI.yaml b/.github/workflows/CI.yaml index 6a55421..d96dc57 100644 --- a/.github/workflows/CI.yaml +++ b/.github/workflows/CI.yaml @@ -1,13 +1,9 @@ -name: CI - +name: tests on: push: - branches: - - "build_release_1" - - "master" + branches: [ master ] pull_request: - branches: - - "master" + branches: [ master ] jobs: test: From d5f277637660f13d58d432d6dea21f13424116cf Mon Sep 17 00:00:00 2001 From: bries Date: Tue, 9 May 2023 10:51:42 +0200 Subject: [PATCH 04/20] update CI --- .github/workflows/CI.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/CI.yaml b/.github/workflows/CI.yaml index d96dc57..8f9f6cd 100644 --- a/.github/workflows/CI.yaml +++ b/.github/workflows/CI.yaml @@ -1,4 +1,4 @@ -name: tests +name: Python tests on: push: branches: [ master ] @@ -33,7 +33,7 @@ jobs: channels: conda-forge,defaults activate-environment: test - auto-update-conda: truea + auto-update-conda: true auto-activate-base: false show-channel-urls: true From 4b263bb936850cb8cc2e42b28247f972074cc1a7 Mon Sep 17 00:00:00 2001 From: bries Date: Tue, 9 May 2023 10:55:12 +0200 Subject: [PATCH 05/20] update CI --- .github/workflows/CI.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/CI.yaml b/.github/workflows/CI.yaml index 8f9f6cd..35ef47b 100644 --- a/.github/workflows/CI.yaml +++ b/.github/workflows/CI.yaml @@ -1,4 +1,4 @@ -name: Python tests +name: CI on: push: branches: [ master ] From dc69b02e3b7862eb34fbacd652e2747f62f09af9 Mon Sep 17 00:00:00 2001 From: bries Date: Tue, 9 May 2023 11:04:15 +0200 Subject: [PATCH 06/20] update CI --- .github/workflows/CI.yaml | 96 +++++++++++++--------------- .github/workflows/codeql.yml | 6 +- .github/workflows/python-package.yml | 4 ++ .github/workflows/python-publish.yml | 4 ++ 4 files changed, 55 insertions(+), 55 deletions(-) diff --git a/.github/workflows/CI.yaml b/.github/workflows/CI.yaml index 35ef47b..283dd71 100644 --- a/.github/workflows/CI.yaml +++ b/.github/workflows/CI.yaml @@ -1,74 +1,64 @@ name: CI + on: push: - branches: [ master ] + branches: + - "master" pull_request: - branches: [ master ] + branches: + - "master" + +concurrency: + group: "${{ github.workflow }}-${{ github.ref }}" + cancel-in-progress: true + +defaults: + run: + shell: bash -l {0} jobs: - test: - name: Test on ${{ matrix.os }}, Python ${{ matrix.python-version }} - runs-on: ${{ matrix.os }} + tests: + runs-on: ${{ matrix.OS }}-latest + name: "tests" strategy: + fail-fast: false matrix: - os: [macOS-latest, ubuntu-latest, windows-latest] - python-version: [3.6, 3.7, 3.8] + os: ['ubuntu', 'macos'] + python-version: + - 3.9 steps: - uses: actions/checkout@v2 - - name: Additional info about the build - shell: bash - run: | - uname -a - df -h - ulimit -a - - # More info on options: https://github.com/goanpeca/setup-miniconda - uses: conda-incubator/setup-miniconda@v2 with: - python-version: ${{ matrix.python-version }} - environment-file: devtools/conda-envs/test_env.yaml - - channels: conda-forge,defaults + auto-update-conda: true + use-mamba: true + python-version: ${{ matrix.python-version }} + miniforge-variant: Mambaforge + environment-file: devtools/conda-envs/test_env.yml + activate-environment: test - activate-environment: test - auto-update-conda: true - auto-activate-base: false - show-channel-urls: true + - name: "Install" + run: pip install --no-deps -e . - - name: Install package - - # conda setup requires this special shell - shell: bash -l {0} + - name: "Test imports" run: | - python -m pip install . --no-deps - conda list - - - name: Run tests - - # conda setup requires this special shell - shell: bash -l {0} - + # if we add more to this, consider changing to for + env vars + python -Ic "import ensembler" + - name: "Environment Information" run: | - pytest -v --cov=ensembler --cov-report=xml --color=yes src/ensembler/tests/ - - - name: Run example notebooks - - # conda setup requires this special shell - shell: bash -l {0} - - continue-on-error: true - + mamba info -a + mamba list + - name: "Run tests" run: | - # add --ignore=XXX here to ignore file XXX - PYTEST_ARGS+=" --nbval-lax " - pytest $PYTEST_ARGS examples/ - - - name: CodeCov - uses: codecov/codecov-action@v1 + pytest -n 2 -v --cov=ensembler --cov-report=xml + - name: codecovg + if: ${{ github.repository == 'rinikerlab/ensembler' && github.event != 'schedule'}} + uses: codecov/codecov-action@v3 with: + token: ${{ secrets.CODECOV_TOKEN }} file: ./coverage.xml - flags: unittests - name: codecov-${{ matrix.os }}-py${{ matrix.python-version }} - + env_vars: OS,PYTHON + fail_ci_if_error: True + verbose: True diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index cec08b6..d40340b 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -5,8 +5,10 @@ on: branches: [ "master" ] pull_request: branches: [ "master" ] - #schedule: - # - cron: "8 21 * * 6" + +concurrency: + group: "${{ github.workflow }}-${{ github.ref }}" + cancel-in-progress: true jobs: analyze: diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 81cb9c8..34d13a8 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -7,6 +7,10 @@ on: pull_request: branches: [ master ] +concurrency: + group: "${{ github.workflow }}-${{ github.ref }}" + cancel-in-progress: true + jobs: unix-build: runs-on: ubuntu-latest diff --git a/.github/workflows/python-publish.yml b/.github/workflows/python-publish.yml index 4e1ef42..cd5819f 100644 --- a/.github/workflows/python-publish.yml +++ b/.github/workflows/python-publish.yml @@ -7,6 +7,10 @@ on: release: types: [created] +concurrency: + group: "${{ github.workflow }}-${{ github.ref }}" + cancel-in-progress: true + jobs: deploy: From 0b15f746f5fcd14caa246dbbca010340c5797cde Mon Sep 17 00:00:00 2001 From: bries Date: Tue, 9 May 2023 11:07:09 +0200 Subject: [PATCH 07/20] update CI --- .github/workflows/CI.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/CI.yaml b/.github/workflows/CI.yaml index 283dd71..f3eb6b9 100644 --- a/.github/workflows/CI.yaml +++ b/.github/workflows/CI.yaml @@ -36,7 +36,7 @@ jobs: use-mamba: true python-version: ${{ matrix.python-version }} miniforge-variant: Mambaforge - environment-file: devtools/conda-envs/test_env.yml + environment-file: ../devtools/conda-envs/test_env.yml activate-environment: test - name: "Install" From 5fab15f49f115eb5f057876e5be6a507a0f3cb95 Mon Sep 17 00:00:00 2001 From: Benjamin Ries Date: Wed, 10 May 2023 17:11:54 +0200 Subject: [PATCH 08/20] Update CI.yaml --- .github/workflows/CI.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/CI.yaml b/.github/workflows/CI.yaml index f3eb6b9..29c6b5c 100644 --- a/.github/workflows/CI.yaml +++ b/.github/workflows/CI.yaml @@ -36,7 +36,6 @@ jobs: use-mamba: true python-version: ${{ matrix.python-version }} miniforge-variant: Mambaforge - environment-file: ../devtools/conda-envs/test_env.yml activate-environment: test - name: "Install" From c89cc6b5b7dd53d576c9846b39e81125f39a4f5e Mon Sep 17 00:00:00 2001 From: Benjamin Ries Date: Wed, 10 May 2023 17:15:07 +0200 Subject: [PATCH 09/20] Update CI.yaml --- .github/workflows/CI.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/CI.yaml b/.github/workflows/CI.yaml index 29c6b5c..e076993 100644 --- a/.github/workflows/CI.yaml +++ b/.github/workflows/CI.yaml @@ -36,7 +36,7 @@ jobs: use-mamba: true python-version: ${{ matrix.python-version }} miniforge-variant: Mambaforge - activate-environment: test + - name: "Install" run: pip install --no-deps -e . From 9753e185735c7d5d41baf9a04a1e19a9eb38ed51 Mon Sep 17 00:00:00 2001 From: Benjamin Ries Date: Wed, 10 May 2023 17:26:40 +0200 Subject: [PATCH 10/20] Update pyproject.toml --- pyproject.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 76e0010..9f5f41c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,7 +27,8 @@ dependencies = [ 'jupyter', # Tutorial/Examples: basics 'ipywidgets',# Tutorial/Examples: Interactive widgets 'tqdm',# Tutorial/Examples: nice progressbar - 'ffmpeg' #Visualizations: for animations in general + 'ffmpeg', #Visualizations: for animations in general + 'pytest' ] description="Ensembler is a tool for fast and efficient development of simulation methods or for teaching purposes." From a52f416c502175d7bb1db1f34580df7fc004ef42 Mon Sep 17 00:00:00 2001 From: Benjamin Ries Date: Wed, 10 May 2023 17:34:55 +0200 Subject: [PATCH 11/20] Update CI.yaml --- .github/workflows/CI.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/CI.yaml b/.github/workflows/CI.yaml index e076993..321bb60 100644 --- a/.github/workflows/CI.yaml +++ b/.github/workflows/CI.yaml @@ -39,7 +39,7 @@ jobs: - name: "Install" - run: pip install --no-deps -e . + run: pip install -e . - name: "Test imports" run: | From b58a642f2710d208ffa93ef93cb07bbc5540b426 Mon Sep 17 00:00:00 2001 From: Benjamin Ries Date: Wed, 10 May 2023 17:47:59 +0200 Subject: [PATCH 12/20] Update pyproject.toml --- pyproject.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 9f5f41c..4ed4440 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,7 +28,8 @@ dependencies = [ 'ipywidgets',# Tutorial/Examples: Interactive widgets 'tqdm',# Tutorial/Examples: nice progressbar 'ffmpeg', #Visualizations: for animations in general - 'pytest' + 'pytest', + 'pytest-cov' ] description="Ensembler is a tool for fast and efficient development of simulation methods or for teaching purposes." From a05f0d14f8b8ba7e56dd505bcf5884240cff4b28 Mon Sep 17 00:00:00 2001 From: Benjamin Ries Date: Wed, 10 May 2023 18:04:55 +0200 Subject: [PATCH 13/20] Update CI.yaml --- .github/workflows/CI.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/CI.yaml b/.github/workflows/CI.yaml index 321bb60..e46ae84 100644 --- a/.github/workflows/CI.yaml +++ b/.github/workflows/CI.yaml @@ -51,7 +51,7 @@ jobs: mamba list - name: "Run tests" run: | - pytest -n 2 -v --cov=ensembler --cov-report=xml + pytest -v --cov=ensembler --cov-report=xml - name: codecovg if: ${{ github.repository == 'rinikerlab/ensembler' && github.event != 'schedule'}} uses: codecov/codecov-action@v3 From 324aa3d8250e0a8cd62d76f71f5357ad282bb632 Mon Sep 17 00:00:00 2001 From: Benjamin Ries Date: Thu, 11 May 2023 13:21:48 +0200 Subject: [PATCH 14/20] Update basic_system.py --- src/ensembler/system/basic_system.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ensembler/system/basic_system.py b/src/ensembler/system/basic_system.py index 8c96b71..af9d1e4 100644 --- a/src/ensembler/system/basic_system.py +++ b/src/ensembler/system/basic_system.py @@ -471,7 +471,7 @@ def random_position(self) -> Union[Number, Iterable[Number]]: random_pos = np.squeeze(np.array(np.subtract(np.multiply(np.random.rand(self.nDimensions), 20), 10))) if (len(random_pos.shape) == 0): - return np.float(random_pos) + return float(random_pos) else: return random_pos From 14f6aa3f4492a0c95698107bc0829496a7547436 Mon Sep 17 00:00:00 2001 From: bries Date: Thu, 11 May 2023 07:49:58 -0400 Subject: [PATCH 15/20] no np.float anymore --- src/ensembler/analysis/freeEnergyCalculation.py | 10 +++++----- src/ensembler/system/basic_system.py | 2 +- src/ensembler/tests/test_system.py | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ensembler/analysis/freeEnergyCalculation.py b/src/ensembler/analysis/freeEnergyCalculation.py index 154151a..03829fe 100644 --- a/src/ensembler/analysis/freeEnergyCalculation.py +++ b/src/ensembler/analysis/freeEnergyCalculation.py @@ -46,7 +46,7 @@ def _update_function(self): @classmethod def _prepare_type(self, *arrays): - return tuple(map(lambda arr: np.array(list(map(lambda x: np.float(x), arr)),ndmin=1), arrays)) + return tuple(map(lambda arr: np.array(list(map(lambda x: float(x), arr)),ndmin=1), arrays)) @classmethod def get_equation(cls) -> sp.Function: @@ -308,7 +308,7 @@ def _calculate_efficient(self, Vi: (Iterable, Number), Vj: (Iterable, Number)) - # Return free energy difference from scipy import special as s - dF = - np.float(1 / beta) * s.logsumexp(np.array(dVij, dtype=np.float), b=1 / len(dVij)) + dF = - float(1 / beta) * s.logsumexp(np.array(dVij, dtype=float), b=1 / len(dVij)) return dF def _calculate_mpmath(self, Vi: (Iterable, Number), Vj: (Iterable, Number))->float: @@ -331,9 +331,9 @@ def _calculate_mpmath(self, Vi: (Iterable, Number), Vj: (Iterable, Number))->fl free energy difference """ - beta = np.float(1 / (self.constants[self.k] * self.constants[self.T])) + beta = float(1 / (self.constants[self.k] * self.constants[self.T])) - return - (1 / beta) * np.float(mp.ln(np.mean(list(map(mp.exp, -beta*(np.array(Vj, ndmin=1) - np.array(Vi, ndmin=1))))))) + return - (1 / beta) * float(mp.ln(np.mean(list(map(mp.exp, -beta*(np.array(Vj, ndmin=1) - np.array(Vi, ndmin=1))))))) def set_parameters(self, T: float = None, k: float = None): """ @@ -655,7 +655,7 @@ def _calc_bar_mpmath(self, C: Number, Vj_i: np.array, Vi_i: np.array, Vi_j: np.a "BAR Error: Problems taking logarithm of the average exponentiated potential energy difference " + str( err.args)) - return np.float(ddF + C) + return float(ddF + C) def _calculate_optimize(self, Vi_i: (Iterable[Number], Number), Vj_i: (Iterable[Number], Number), diff --git a/src/ensembler/system/basic_system.py b/src/ensembler/system/basic_system.py index 8c96b71..af9d1e4 100644 --- a/src/ensembler/system/basic_system.py +++ b/src/ensembler/system/basic_system.py @@ -471,7 +471,7 @@ def random_position(self) -> Union[Number, Iterable[Number]]: random_pos = np.squeeze(np.array(np.subtract(np.multiply(np.random.rand(self.nDimensions), 20), 10))) if (len(random_pos.shape) == 0): - return np.float(random_pos) + return float(random_pos) else: return random_pos diff --git a/src/ensembler/tests/test_system.py b/src/ensembler/tests/test_system.py index 4b2e753..5f16d57 100644 --- a/src/ensembler/tests/test_system.py +++ b/src/ensembler/tests/test_system.py @@ -718,7 +718,7 @@ def test_initVel(self): cur_velocity = sys._currentVelocities # print(cur_velocity) - expected_vel = np.float64(-2.8014573319669176) + expected_vel = float64(-2.8014573319669176) self.assertEqual(type(cur_velocity), type(expected_vel), msg="Velocity has not the correcttype!") def test_updateTemp(self): From 013f9f61a323754c9dd96dae64fbcd1c7af66ee0 Mon Sep 17 00:00:00 2001 From: bries Date: Thu, 11 May 2023 07:54:18 -0400 Subject: [PATCH 16/20] no float64 --- src/ensembler/tests/test_system.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ensembler/tests/test_system.py b/src/ensembler/tests/test_system.py index 5f16d57..a3fa178 100644 --- a/src/ensembler/tests/test_system.py +++ b/src/ensembler/tests/test_system.py @@ -718,7 +718,7 @@ def test_initVel(self): cur_velocity = sys._currentVelocities # print(cur_velocity) - expected_vel = float64(-2.8014573319669176) + expected_vel = float(-2.8014573319669176) self.assertEqual(type(cur_velocity), type(expected_vel), msg="Velocity has not the correcttype!") def test_updateTemp(self): From 7d8752863c13970b3e32b87cbd51e030d30f3c1f Mon Sep 17 00:00:00 2001 From: bries Date: Thu, 11 May 2023 08:19:52 -0400 Subject: [PATCH 17/20] fix --- src/ensembler/tests/test_system.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ensembler/tests/test_system.py b/src/ensembler/tests/test_system.py index a3fa178..4b2e753 100644 --- a/src/ensembler/tests/test_system.py +++ b/src/ensembler/tests/test_system.py @@ -718,7 +718,7 @@ def test_initVel(self): cur_velocity = sys._currentVelocities # print(cur_velocity) - expected_vel = float(-2.8014573319669176) + expected_vel = np.float64(-2.8014573319669176) self.assertEqual(type(cur_velocity), type(expected_vel), msg="Velocity has not the correcttype!") def test_updateTemp(self): From 2dea431e590be443825f3cf2d9049364e439177a Mon Sep 17 00:00:00 2001 From: bries Date: Thu, 11 May 2023 10:28:22 -0400 Subject: [PATCH 18/20] float, check 2D fix --- examples/dev/exampleEnvelopedPotentials.ipynb | 2 +- src/ensembler/_version.py | 2 +- .../analysis/freeEnergyCalculation.py | 16 +++--- src/ensembler/ensemble/_replica_graph.py | 6 ++- src/ensembler/ensemble/exchange_pattern.py | 54 +++++++++++-------- .../ensemble/replicas_dynamic_parameters.py | 12 ++--- src/ensembler/potentials/TwoD.py | 7 ++- src/ensembler/potentials/_basicPotentials.py | 6 ++- src/ensembler/system/basic_system.py | 2 +- src/ensembler/tests/test_ensemble.py | 16 +++--- src/ensembler/tests/test_potentials.py | 4 +- .../visualisation/plotConveyorBelt.py | 12 ++--- src/ensembler/visualisation/plotPotentials.py | 6 +-- 13 files changed, 83 insertions(+), 62 deletions(-) diff --git a/examples/dev/exampleEnvelopedPotentials.ipynb b/examples/dev/exampleEnvelopedPotentials.ipynb index ecce2f5..699a5fd 100644 --- a/examples/dev/exampleEnvelopedPotentials.ipynb +++ b/examples/dev/exampleEnvelopedPotentials.ipynb @@ -215,7 +215,7 @@ "eds_pot = potN.envelopedPotential(V_is=V_is, s=s1, Eoff_i=Eoffs)\n", "\n", "##Parameters\n", - "positions = [x / float(10) for x in range(-250, 251)]\n", + "positions = [x / np.float64(10) for x in range(-250, 251)]\n", "energies = [V.ene(positions) for V in V_is]\n", "print(len(energies))\n", "plt.plot(positions, energies[0], \"steelblue\")\n", diff --git a/src/ensembler/_version.py b/src/ensembler/_version.py index 9f87518..c53d7d7 100644 --- a/src/ensembler/_version.py +++ b/src/ensembler/_version.py @@ -1 +1 @@ -__version__ = "1.0.0+15.g467f3eb.dirty" +__version__ = "1.0.0+33.g7d87528.dirty" diff --git a/src/ensembler/analysis/freeEnergyCalculation.py b/src/ensembler/analysis/freeEnergyCalculation.py index 03829fe..4c635c5 100644 --- a/src/ensembler/analysis/freeEnergyCalculation.py +++ b/src/ensembler/analysis/freeEnergyCalculation.py @@ -46,7 +46,7 @@ def _update_function(self): @classmethod def _prepare_type(self, *arrays): - return tuple(map(lambda arr: np.array(list(map(lambda x: float(x), arr)),ndmin=1), arrays)) + return tuple(map(lambda arr: np.array(list(map(lambda x: np.float64(x), arr)),ndmin=1), arrays)) @classmethod def get_equation(cls) -> sp.Function: @@ -142,7 +142,7 @@ def calculate(self, Vi: (Iterable[Number], Number), Vj: (Iterable[Number], Numbe free energy difference """ - return float(self._calculate_mpmath(Vi=Vi, Vj=Vj)) + return np.float64(self._calculate_mpmath(Vi=Vi, Vj=Vj)) def _calculate_implementation_bruteForce(self, Vi: (Iterable, Number), Vj: (Iterable, Number)) -> float: """ @@ -308,7 +308,7 @@ def _calculate_efficient(self, Vi: (Iterable, Number), Vj: (Iterable, Number)) - # Return free energy difference from scipy import special as s - dF = - float(1 / beta) * s.logsumexp(np.array(dVij, dtype=float), b=1 / len(dVij)) + dF = - np.float64(1 / beta) * s.logsumexp(np.array(dVij, dtype=float), b=1 / len(dVij)) return dF def _calculate_mpmath(self, Vi: (Iterable, Number), Vj: (Iterable, Number))->float: @@ -331,9 +331,9 @@ def _calculate_mpmath(self, Vi: (Iterable, Number), Vj: (Iterable, Number))->fl free energy difference """ - beta = float(1 / (self.constants[self.k] * self.constants[self.T])) + beta = np.float64(1 / (self.constants[self.k] * self.constants[self.T])) - return - (1 / beta) * float(mp.ln(np.mean(list(map(mp.exp, -beta*(np.array(Vj, ndmin=1) - np.array(Vi, ndmin=1))))))) + return - (1 / beta) * np.float64(mp.ln(np.mean(list(map(mp.exp, -beta*(np.array(Vj, ndmin=1) - np.array(Vi, ndmin=1))))))) def set_parameters(self, T: float = None, k: float = None): """ @@ -414,7 +414,7 @@ def calculate(self, Vi: (Iterable[Number], Number), Vj: (Iterable[Number], Numbe free energy difference """ - return float(self._calculate_implementation_useZwanzig(Vi=Vi, Vj=Vj, Vr=Vr)) + return np.float64(self._calculate_implementation_useZwanzig(Vi=Vi, Vj=Vj, Vr=Vr)) def _calculate_implementation_useZwanzig(self, Vi: (Iterable, Number), Vj: (Iterable, Number), Vr: (Iterable[Number], Number)) -> float: @@ -655,7 +655,7 @@ def _calc_bar_mpmath(self, C: Number, Vj_i: np.array, Vi_i: np.array, Vi_j: np.a "BAR Error: Problems taking logarithm of the average exponentiated potential energy difference " + str( err.args)) - return float(ddF + C) + return np.float64(ddF + C) def _calculate_optimize(self, Vi_i: (Iterable[Number], Number), Vj_i: (Iterable[Number], Number), @@ -721,7 +721,7 @@ def _calculate_optimize(self, Vi_i: (Iterable[Number], Number), Vj_i: (Iterable[ "BAR is not converged after " + str(iteration) + " steps. stopped at: " + str(self.constants[self.C])) print("Final Iterations: ", iteration, " Result: ", dF) - return float(dF) + return np.float64(dF) def set_parameters(self, C: float = None, T: float = None, k: float = None): """ diff --git a/src/ensembler/ensemble/_replica_graph.py b/src/ensembler/ensemble/_replica_graph.py index b3f3566..2b57a2e 100644 --- a/src/ensembler/ensemble/_replica_graph.py +++ b/src/ensembler/ensemble/_replica_graph.py @@ -600,15 +600,17 @@ def _init_exchanges(self)->NoReturn: """ initialise the exchanges """ + self._exchange_information = list(self.exchange_information) for replicaID in self.replicas: exchange = False - self._exchange_information = self.exchange_information.append( + self._exchange_information.append( {"nExchange": self._currentTrial, "replicaID": self.replicas[replicaID].replicaID, "replicaPositionI": replicaID, "exchangeCoordinateI": self.replicas[replicaID].exchange_parameters, "TotEI": self.replicas[replicaID].calculate_total_potential_energy(), "replicaPositionJ": replicaID, "exchangeCoordinateJ": self.replicas[replicaID].exchange_parameters, "TotEJ": self.replicas[replicaID].calculate_total_potential_energy(), - "doExchange": exchange}, ignore_index=True) + "doExchange": exchange}) + self._exchange_information = pd.DataFrame(self._exchange_information) def _adapt_system_to_exchange_coordinate(self) ->NoReturn: """ diff --git a/src/ensembler/ensemble/exchange_pattern.py b/src/ensembler/ensemble/exchange_pattern.py index 1f27464..5d43f59 100644 --- a/src/ensembler/ensemble/exchange_pattern.py +++ b/src/ensembler/ensemble/exchange_pattern.py @@ -1,4 +1,5 @@ import numpy as np +import pandas as pd from ensembler.util.ensemblerTypes import NoReturn, Dict, List, Tuple class Exchange_pattern: @@ -85,45 +86,56 @@ def update_exchange_information(self, original_exchange_coordinates:List, origin exchange = False IJ = original_exchange_coordinates[0] replicaIJ = self.replica_graph.replicas[IJ] - self.replica_graph._exchange_information = self.replica_graph.exchange_information.append( + + move_data = [ {"nExchange": self.replica_graph._currentTrial, "replicaID": replicaIJ.replicaID, "replicaPositionI": IJ, "exchangeCoordinateI": replicaIJ.exchange_parameters, "TotEI": swapped_totPots[IJ], "replicaPositionJ": IJ, "exchangeCoordinateJ": replicaIJ.exchange_parameters, "TotEJ": swapped_totPots[IJ], - "doExchange": exchange}, ignore_index=True) + "doExchange": exchange} + ] + self.replica_graph._exchange_information = pd.concat([self.replica_graph.exchange_information, pd.DataFrame(move_data)], + ignore_index=True) for (I, J), exchange in self.replica_graph._current_exchanges.items(): - replicaI = self.replica_graph.replicas[I] - replicaJ = self.replica_graph.replicas[J] - # add exchange info line here! - self.replica_graph._exchange_information = self.replica_graph.exchange_information.append( - {"nExchange": self.replica_graph._currentTrial, "replicaID": replicaI.replicaID, - "replicaPositionI": I, "exchangeCoordinateI": replicaI.exchange_parameters, - "TotEI": original_totPots[I], - "replicaPositionJ": J, "exchangeCoordinateJ": replicaJ.exchange_parameters, - "TotEJ": original_totPots[J], - "doExchange": exchange}, ignore_index=True) - self.replica_graph._exchange_information = self.replica_graph.exchange_information.append( - {"nExchange": self.replica_graph._currentTrial, "replicaID": replicaJ.replicaID, - "replicaPositionI": J, "exchangeCoordinateI": replicaJ.exchange_parameters, - "TotEI": swapped_totPots[J], - "replicaPositionJ": I, "exchangeCoordinateJ": replicaI.exchange_parameters, - "TotEJ": swapped_totPots[I], - "doExchange": exchange}, ignore_index=True) + replicaI = self.replica_graph.replicas[I] + replicaJ = self.replica_graph.replicas[J] + + # add exchange info line here! + move_data = [ + {"nExchange": self.replica_graph._currentTrial, "replicaID": replicaI.replicaID, + "replicaPositionI": I, "exchangeCoordinateI": replicaI.exchange_parameters, + "TotEI": original_totPots[I], + "replicaPositionJ": J, "exchangeCoordinateJ": replicaJ.exchange_parameters, + "TotEJ": original_totPots[J], + "doExchange": exchange}, + {"nExchange": self.replica_graph._currentTrial, "replicaID": replicaJ.replicaID, + "replicaPositionI": J, "exchangeCoordinateI": replicaJ.exchange_parameters, + "TotEI": swapped_totPots[J], + "replicaPositionJ": I, "exchangeCoordinateJ": replicaI.exchange_parameters, + "TotEJ": swapped_totPots[I], + "doExchange": exchange} + + ] + self.replica_graph._exchange_information = pd.concat([self.replica_graph.exchange_information, pd.DataFrame(move_data)], ignore_index=True) round_reps =len(self.replica_graph.replicas)%2==0 if ((self.exchange_offset == 0 and not round_reps) or (self.exchange_offset == 1 and round_reps)): exchange = False IJ = original_exchange_coordinates[-1] replicaIJ = self.replica_graph.replicas[IJ] - self.replica_graph._exchange_information = self.replica_graph.exchange_information.append( + + move_data = [ {"nExchange": self.replica_graph._currentTrial, "replicaID": replicaIJ.replicaID, "replicaPositionI": IJ, "exchangeCoordinateI": replicaIJ.exchange_parameters, "TotEI": swapped_totPots[IJ], "replicaPositionJ": IJ, "exchangeCoordinateJ": replicaIJ.exchange_parameters, "TotEJ": swapped_totPots[IJ], - "doExchange": exchange}, ignore_index=True) + "doExchange": exchange} + ] + self.replica_graph._exchange_information = pd.concat([self.replica_graph.exchange_information, pd.DataFrame(move_data)], ignore_index=True) + class localExchangeScheme(Exchange_pattern): diff --git a/src/ensembler/ensemble/replicas_dynamic_parameters.py b/src/ensembler/ensemble/replicas_dynamic_parameters.py index 0e19755..a5c45f5 100644 --- a/src/ensembler/ensemble/replicas_dynamic_parameters.py +++ b/src/ensembler/ensemble/replicas_dynamic_parameters.py @@ -50,7 +50,7 @@ def __str__(self): outstr += "-" * 25 + "\n" for i in self.replicas: outstr += '{:5d}{:10.2f}{:10.3f}\n'.format(i, self.replicas[i].lam, - float(self.replicas[i].total_system_energy)) + np.float64(self.replicas[i].total_system_energy)) return outstr def __repr__(self): @@ -221,7 +221,7 @@ def accept_move(self) -> NoReturn: for i in self.replicas: self.replicas[i]._update_dHdLambda() - self.__tmp_exchange_traj.append({"Step": self._currentTrial, "capital_lambda": self.capital_lambda, "TotE": float(newEne), + self.__tmp_exchange_traj.append({"Step": self._currentTrial, "capital_lambda": self.capital_lambda, "TotE": np.float64(newEne), "biasE": self.biasene, "doAccept": True}) else: self.reject += 1 @@ -230,8 +230,8 @@ def accept_move(self) -> NoReturn: for i in self.replicas: self.replicas[i]._update_dHdLambda() - self.__tmp_exchange_traj.append({"Step": self._currentTrial, "capital_lambda": oldBlam, "TotE": float(oldEne), - "biasE": float(oldBiasene), "doAccept": False}) + self.__tmp_exchange_traj.append({"Step": self._currentTrial, "capital_lambda": oldBlam, "TotE": np.float64(oldEne), + "biasE": np.float64(oldBiasene), "doAccept": False}) if self.build: self.build_mem() @@ -330,7 +330,7 @@ def init_mem(self) -> NoReturn: # self.mem=np.array([2.2991 , 2.00274, 1.84395, 1.83953, 2.0147]) # memory for perturbed hosc, alpha=10.0, gamma=0.0, 8 replica, num_gp=6, fc=0.00001, 1E6 steps self.mem = np.zeros(self.num_gp - 1) - self.gp_spacing = self.dis / float(self.num_gp - 1.0) + self.gp_spacing = self.dis / np.float64(self.num_gp - 1.0) self.biasene = 0.0 # print('Distance: ', self.dis, self.dis / np.pi) # print('GP Distance: ', self.gp_spacing, self.gp_spacing / np.pi) @@ -350,7 +350,7 @@ def apply_mem(self) -> NoReturn: """ active_gp = int(np.floor((self.capital_lambda % self.dis) / self.gp_spacing + 0.5)) - dg = (self.capital_lambda % self.dis) / self.gp_spacing - float(active_gp) + dg = (self.capital_lambda % self.dis) / self.gp_spacing - np.float64(active_gp) if dg < 0: self.biasene = self.mem[(active_gp - 1) % (self.num_gp - 1)] * self.spline(1.0 + dg) + self.mem[ active_gp % (self.num_gp - 1)] * self.spline(-dg) diff --git a/src/ensembler/potentials/TwoD.py b/src/ensembler/potentials/TwoD.py index 0fde750..dd35f53 100644 --- a/src/ensembler/potentials/TwoD.py +++ b/src/ensembler/potentials/TwoD.py @@ -610,8 +610,11 @@ def force(self, positions): current_bin_x = self._find_nearest(self.y_centers, x_vals) current_bin_y = self._find_nearest(self.y_centers, y_vals) - dvdpos = np.squeeze( - self._calculate_dVdpos(*np.hsplit(np.array(positions, ndmin=1), self.constants[self.nDimensions]))).T + arr = np.array(positions, ndmin=2) #Check is this a good solution? + print(arr) + f = [self._calculate_dVdpos(x,y) for x,y in arr] + dvdpos = np.squeeze(f).T + return np.squeeze(dvdpos + self.bias_grid_force[:, current_bin_y, current_bin_x].T) def _find_nearest(self, array, value): diff --git a/src/ensembler/potentials/_basicPotentials.py b/src/ensembler/potentials/_basicPotentials.py index 884ab74..fcb4fd5 100644 --- a/src/ensembler/potentials/_basicPotentials.py +++ b/src/ensembler/potentials/_basicPotentials.py @@ -177,7 +177,11 @@ def force(self, positions:Union[Number, Iterable[Number], Iterable[Iterable[Numb the calculated potential forces. """ - return np.squeeze(self._calculate_dVdpos(*np.hsplit(np.array(positions, ndmin=1), self.constants[self.nDimensions]))).T + arr = np.array(positions, ndmin=1) + comp = np.hsplit(arr, self.constants[self.nDimensions]) + f = self._calculate_dVdpos(*comp) + + return np.squeeze(f).T # just alternative name, same as force def dvdpos(self, positions:Union[Number, Iterable[Number], Iterable[Iterable[Number]]]) -> Union[Number, Iterable[Number], Iterable[Iterable[Number]]]: diff --git a/src/ensembler/system/basic_system.py b/src/ensembler/system/basic_system.py index af9d1e4..2fb4a75 100644 --- a/src/ensembler/system/basic_system.py +++ b/src/ensembler/system/basic_system.py @@ -471,7 +471,7 @@ def random_position(self) -> Union[Number, Iterable[Number]]: random_pos = np.squeeze(np.array(np.subtract(np.multiply(np.random.rand(self.nDimensions), 20), 10))) if (len(random_pos.shape) == 0): - return float(random_pos) + return np.float64(random_pos) else: return random_pos diff --git a/src/ensembler/tests/test_ensemble.py b/src/ensembler/tests/test_ensemble.py index 87afb67..1150d9a 100644 --- a/src/ensembler/tests/test_ensemble.py +++ b/src/ensembler/tests/test_ensemble.py @@ -1,5 +1,5 @@ import unittest - +import numpy as np from ensembler.ensemble import replica_exchange, _replica_graph from ensembler.samplers import stochastic from ensembler.potentials import OneD @@ -122,8 +122,8 @@ def test_exchange_all(self): T_range = range(1, 10) nReplicas = len(T_range) - positions = {x: float(1) for x in range(nReplicas)} - velocities = {x: float(0) for x in range(nReplicas)} + positions = {x: np.float64(1) for x in range(nReplicas)} + velocities = {x: np.float64(0) for x in range(nReplicas)} group = replica_exchange.temperatureReplicaExchange(system=sys, temperature_range=T_range) group.set_replicas_positions(positions) @@ -158,8 +158,8 @@ def test_exchange_none(self): T_range = [1, 200, 500] nReplicas = len(T_range) print("REPS", nReplicas) - positions = [float(x)*100 for x in range(nReplicas)] - velocities = list([float(1) for x in range(nReplicas)]) + positions = [np.float64(x)*100 for x in range(nReplicas)] + velocities = list([np.float64(1) for x in range(nReplicas)]) group = replica_exchange.TemperatureReplicaExchange(system=sys, temperature_Range=T_range) print("REPS", group.nReplicas) @@ -213,7 +213,7 @@ def test_simulate_bad_exchange(self): nsteps = 1 T_range = [1, 2000, 5000] - positions = [float(x)*100 for x in range(len(T_range))] + positions = [np.float64(x)*100 for x in range(len(T_range))] group = replica_exchange.TemperatureReplicaExchange(system=sys, temperature_Range=T_range) @@ -221,7 +221,7 @@ def test_simulate_bad_exchange(self): group._defaultRandomness= lambda x,y: False group.set_replicas_positions(positions) group.nSteps_between_trials = nsteps - group.set_replicas_velocities([float(-100) for x in range(replicas)]) + group.set_replicas_velocities([np.float64(-100) for x in range(replicas)]) print("STEP:\t", 0) print(group.get_replicas_positions()) @@ -233,7 +233,7 @@ def test_simulate_bad_exchange(self): for step in range(5): #group.run() group.simulate(1) - group.set_replicas_velocities([float(10) for x in range(replicas)]) + group.set_replicas_velocities([np.float64(10) for x in range(replicas)]) #group.exchange() #group.simulate(1) print("STEP:\t", step) diff --git a/src/ensembler/tests/test_potentials.py b/src/ensembler/tests/test_potentials.py index f0d7157..44fd6dc 100644 --- a/src/ensembler/tests/test_potentials.py +++ b/src/ensembler/tests/test_potentials.py @@ -1643,8 +1643,8 @@ def test_dVdpos(self): err_msg="The results of " + potential.name + " are not correct!") def test_dVdpos_1Pos(self): - positions = [0,0] - expected_result = [0,0] + positions = np.array([0,0]) + expected_result = np.array([0,0]) potential = self.potential_class() diff --git a/src/ensembler/visualisation/plotConveyorBelt.py b/src/ensembler/visualisation/plotConveyorBelt.py index 0f4a88a..f469abc 100644 --- a/src/ensembler/visualisation/plotConveyorBelt.py +++ b/src/ensembler/visualisation/plotConveyorBelt.py @@ -212,14 +212,14 @@ def plotEnsembler(x, y, CapLam=0.1, M=8, drawArrows=False): y.append(1.0) elif calc_lam(CapLam, i, numsys=M) < rx: alpha = np.arcsin((rx - calc_lam(CapLam, i, numsys=M)) / rx) - if (CapLam + i * 2 * np.pi / float(M)) % (2. * np.pi) < np.pi: + if (CapLam + i * 2 * np.pi / np.float64(M)) % (2. * np.pi) < np.pi: rotation.append(45 + alpha / np.pi * 180.0) else: rotation.append(45 - alpha / np.pi * 180.0) y.append(np.cos(alpha)) else: alpha = np.arcsin((rx - (1 - calc_lam(CapLam, i, numsys=M))) / rx) - if (CapLam + i * 2 * np.pi / float(M)) % (2. * np.pi) < np.pi: + if (CapLam + i * 2 * np.pi / np.float64(M)) % (2. * np.pi) < np.pi: rotation.append(45 - alpha / np.pi * 180.0) else: rotation.append(45 + alpha / np.pi * 180.0) @@ -268,7 +268,7 @@ def plotEnsembler(x, y, CapLam=0.1, M=8, drawArrows=False): rx -= np.sqrt(1 - y[i] ** 2) * shiftMarker elif x > 1 - rx: rx += np.sqrt(1 - y[i] ** 2) * shiftMarker - if (CapLam + i * 2 * np.pi / float(M)) % (2. * np.pi) < np.pi: + if (CapLam + i * 2 * np.pi / np.float64(M)) % (2. * np.pi) < np.pi: ax.add_patch( # Create triangle as arrow head patches.RegularPolygon( (x, shifty + ry + y[i] * ry + y[i] * shiftMarker), # (x,y) @@ -361,14 +361,14 @@ def updateEnsembler(x, y, ax, CapLam=0.1, M=8, drawArrows=False): y.append(1.0) elif calc_lam(CapLam, i, numsys=M) < rx: alpha = np.arcsin((rx - calc_lam(CapLam, i, numsys=M)) / rx) - if (CapLam + i * 2 * np.pi / float(M)) % (2. * np.pi) < np.pi: + if (CapLam + i * 2 * np.pi / np.float64(M)) % (2. * np.pi) < np.pi: rotation.append(45 + alpha / np.pi * 180.0) else: rotation.append(45 - alpha / np.pi * 180.0) y.append(np.cos(alpha)) else: alpha = np.arcsin((rx - (1 - calc_lam(CapLam, i, numsys=M))) / rx) - if (CapLam + i * 2 * np.pi / float(M)) % (2. * np.pi) < np.pi: + if (CapLam + i * 2 * np.pi / np.float64(M)) % (2. * np.pi) < np.pi: rotation.append(45 - alpha / np.pi * 180.0) else: rotation.append(45 + alpha / np.pi * 180.0) @@ -403,7 +403,7 @@ def updateEnsembler(x, y, ax, CapLam=0.1, M=8, drawArrows=False): rx -= np.sqrt(1 - y[i] ** 2) * shiftMarker elif x > 1 - rx: rx += np.sqrt(1 - y[i] ** 2) * shiftMarker - if (CapLam + i * 2 * np.pi / float(M)) % (2. * np.pi) < np.pi: + if (CapLam + i * 2 * np.pi / np.float64(M)) % (2. * np.pi) < np.pi: ax.add_patch( # Create triangle as arrow head patches.RegularPolygon( (x, shifty + ry + y[i] * ry + y[i] * shiftMarker), # (x,y) diff --git a/src/ensembler/visualisation/plotPotentials.py b/src/ensembler/visualisation/plotPotentials.py index 565b3cd..f3e3dcd 100644 --- a/src/ensembler/visualisation/plotPotentials.py +++ b/src/ensembler/visualisation/plotPotentials.py @@ -20,7 +20,7 @@ def significant_decimals(s: float) -> float: significant_decimal = 2 if (s % 1 != 0): - decimals = str(float(s)).split(".")[-1] + decimals = str(np.float64(s)).split(".")[-1] for digit in decimals: if (digit == "0"): significant_decimal += 1 @@ -509,7 +509,7 @@ def plot_envelopedPotential_2State_System(eds_potential: pot.envelopedPotential, for x in positions: row = eds_potential.ene(list(map(lambda y: [[x], [y]], list(positions)))) - row_cut = list(map(lambda x: V_max if (V_max != None and float(x) > V_max) else float(x), row)) + row_cut = list(map(lambda x: V_max if (V_max != None and np.float64(x) > V_max) else np.float64(x), row)) energy_map.append(row_cut) if (min(row) < min_e): min_e = min(row) @@ -567,7 +567,7 @@ def envPot_diffS_2stateMap_compare(eds_potential: pot.envelopedPotential, s_valu energy_map = [] for x in positions: row = eds_potential.ene(list(map(lambda y: [[x], [y]], list(positions)))) - row_cut = list(map(lambda x: V_max if (V_max != None and float(x) > V_max) else float(x), row)) + row_cut = list(map(lambda x: V_max if (V_max != None and np.float64(x) > V_max) else np.float64(x), row)) energy_map.append(row_cut) if (min(row) < min_e): min_e = min(row) From 9b92e1a335e8d57535499f417532618cfd74749d Mon Sep 17 00:00:00 2001 From: bries Date: Wed, 19 Jul 2023 21:12:03 -0400 Subject: [PATCH 19/20] float, check 2D fix --- src/ensembler/potentials/TwoD.py | 3 +-- src/ensembler/potentials/_basicPotentials.py | 7 ++++--- src/ensembler/tests/test_potentials.py | 13 ++++++++----- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/ensembler/potentials/TwoD.py b/src/ensembler/potentials/TwoD.py index dd35f53..0b35b5d 100644 --- a/src/ensembler/potentials/TwoD.py +++ b/src/ensembler/potentials/TwoD.py @@ -611,9 +611,8 @@ def force(self, positions): current_bin_y = self._find_nearest(self.y_centers, y_vals) arr = np.array(positions, ndmin=2) #Check is this a good solution? - print(arr) f = [self._calculate_dVdpos(x,y) for x,y in arr] - dvdpos = np.squeeze(f).T + dvdpos = np.squeeze(f) return np.squeeze(dvdpos + self.bias_grid_force[:, current_bin_y, current_bin_x].T) diff --git a/src/ensembler/potentials/_basicPotentials.py b/src/ensembler/potentials/_basicPotentials.py index fcb4fd5..320487f 100644 --- a/src/ensembler/potentials/_basicPotentials.py +++ b/src/ensembler/potentials/_basicPotentials.py @@ -162,6 +162,7 @@ def ene(self, positions: Union[Number, Iterable[Number], Iterable[Iterable[Numbe """ return np.squeeze(self._calculate_energies(*np.hsplit(np.array(positions, ndmin=1), self.constants[self.nDimensions]))) + def force(self, positions:Union[Number, Iterable[Number], Iterable[Iterable[Number]]]) -> Union[Number, Iterable[Number], Iterable[Iterable[Number]]]: """ force @@ -178,10 +179,10 @@ def force(self, positions:Union[Number, Iterable[Number], Iterable[Iterable[Numb """ arr = np.array(positions, ndmin=1) - comp = np.hsplit(arr, self.constants[self.nDimensions]) - f = self._calculate_dVdpos(*comp) + f = np.array(list(map(lambda c: self._calculate_dVdpos(*c), arr))) + + return np.squeeze(f) - return np.squeeze(f).T # just alternative name, same as force def dvdpos(self, positions:Union[Number, Iterable[Number], Iterable[Iterable[Number]]]) -> Union[Number, Iterable[Number], Iterable[Iterable[Number]]]: diff --git a/src/ensembler/tests/test_potentials.py b/src/ensembler/tests/test_potentials.py index 44fd6dc..b739af7 100644 --- a/src/ensembler/tests/test_potentials.py +++ b/src/ensembler/tests/test_potentials.py @@ -1507,12 +1507,14 @@ def test_energies(self): self.assertEqual(type(expected_result), type(energies), msg="returnType of potential was not correct! it should be an np.array") - self.assertListEqual(list(expected_result), list(energies), - msg="The results of " + potential.name + " are not correct!") + + np.testing.assert_almost_equal(desired=expected_result, actual=energies, + err_msg="The results of " + potential.name + " are not correct!") + def test_dVdpos(self): positions = [0, 0.5, 1, 2] - expected_result = np.array([0.0, 0.05875154870770227, 0.3934693402873666, 1.7293294335267746]) + expected_result = np.array([0.0, 0.05875154870770233, 0.3934693402873666, 1.7293294335267746]) potential = self.potential_class() @@ -1521,8 +1523,9 @@ def test_dVdpos(self): self.assertEqual(type(expected_result), type(energies), msg="returnType of potential was not correct! it should be an np.array") - self.assertListEqual(list(expected_result), list(energies), - msg="The results of " + potential.name + " are not correct!") + + np.testing.assert_almost_equal(desired=expected_result, actual=energies, + err_msg="The results of " + potential.name + " are not correct!") class potentialCls_metadynamics(test_potentialCls): From d962946c8d397efa33d196e98f97de9b6c721734 Mon Sep 17 00:00:00 2001 From: bries Date: Wed, 19 Jul 2023 21:21:57 -0400 Subject: [PATCH 20/20] refactor+ minor dim fix --- src/ensembler/analysis/freeEnergyCalculation.py | 2 +- src/ensembler/potentials/_basicPotentials.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ensembler/analysis/freeEnergyCalculation.py b/src/ensembler/analysis/freeEnergyCalculation.py index 4c635c5..396975e 100644 --- a/src/ensembler/analysis/freeEnergyCalculation.py +++ b/src/ensembler/analysis/freeEnergyCalculation.py @@ -46,7 +46,7 @@ def _update_function(self): @classmethod def _prepare_type(self, *arrays): - return tuple(map(lambda arr: np.array(list(map(lambda x: np.float64(x), arr)),ndmin=1), arrays)) + return tuple(map(lambda arr: np.array(arr,ndmin=1, dtype=np.float64), arrays)) @classmethod def get_equation(cls) -> sp.Function: diff --git a/src/ensembler/potentials/_basicPotentials.py b/src/ensembler/potentials/_basicPotentials.py index 320487f..aa40a53 100644 --- a/src/ensembler/potentials/_basicPotentials.py +++ b/src/ensembler/potentials/_basicPotentials.py @@ -178,7 +178,7 @@ def force(self, positions:Union[Number, Iterable[Number], Iterable[Iterable[Numb the calculated potential forces. """ - arr = np.array(positions, ndmin=1) + arr = np.array(positions, ndmin=2) f = np.array(list(map(lambda c: self._calculate_dVdpos(*c), arr))) return np.squeeze(f)