Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some refactor improvments and bug fixes #17

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/neuralxc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ if __name__ == '__main__':
adddat.set_defaults(func=add_data_driver)

def inspectdat_driver(args):
subprocess.Popen('h5dump -n ' + args.hdf5, shell=True)
subprocess.Popen(f'h5dump -n {args.hdf5}', shell=True)

#================ Inspect data ================
inspectdat = datsub.add_parser('inspect', description='Inspect data in hdf5 file')
Expand Down
15 changes: 9 additions & 6 deletions devtools/scripts/create_conda_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def temp_cd():
with open(args.conda_file, "r") as handle:
yaml_script = loader(handle.read())

python_replacement_string = "python {}*".format(args.python)
python_replacement_string = f"python {args.python}*"

try:
for dep_index, dep_value in enumerate(yaml_script['dependencies']):
Expand All @@ -79,14 +79,17 @@ def temp_cd():
if conda_path is None:
raise RuntimeError("Could not find a conda binary in CONDA_EXE variable or in executable search path")

print("CONDA ENV NAME {}".format(args.name))
print("PYTHON VERSION {}".format(args.python))
print("CONDA FILE NAME {}".format(args.conda_file))
print("CONDA PATH {}".format(conda_path))
print(f"CONDA ENV NAME {args.name}")
print(f"PYTHON VERSION {args.python}")
print(f"CONDA FILE NAME {args.conda_file}")
print(f"CONDA PATH {conda_path}")

# Write to a temp directory which will always be cleaned up
with temp_cd():
temp_file_name = "temp_script.yaml"
with open(temp_file_name, 'w') as f:
f.write(yaml.dump(yaml_script))
sp.call("{} env create -n {} -f {}".format(conda_path, args.name, temp_file_name), shell=True)
sp.call(
f"{conda_path} env create -n {args.name} -f {temp_file_name}",
shell=True,
)
16 changes: 7 additions & 9 deletions examples/scripts/apply_subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@ def get_structures_energies(path, unit=1):

if __name__ == '__main__':

if len(sys.argv) == 5:
unit = float(sys.argv[4])
else:
unit = kcalpmol

unit = float(sys.argv[4]) if len(sys.argv) == 5 else kcalpmol
print("using unit", unit)
atoms = get_structures_energies(sys.argv[1], unit=unit)
if not sys.argv[2] == 'all':
subset = np.genfromtxt(sys.argv[2]).astype(int)
else:
subset = np.arange(len(atoms))
subset = (
np.arange(len(atoms))
if sys.argv[2] == 'all'
else np.genfromtxt(sys.argv[2]).astype(int)
)

atoms_subset = [atoms[s] for s in subset]
write(sys.argv[3], atoms_subset)
6 changes: 1 addition & 5 deletions examples/scripts/fix_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
import os
import sys

if len(sys.argv) > 1:
path = sys.argv[1]
else:
path = 'basis_sgdml_benzene.json'

path = sys.argv[1] if len(sys.argv) > 1 else 'basis_sgdml_benzene.json'
print('FILEPATH', path)

basis = json.load(open(path, 'r'))
Expand Down
113 changes: 54 additions & 59 deletions neuralxc/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def 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
return {"refnames": git_refnames, "full": git_full, "date": git_date}


class VersioneerConfig:
Expand Down Expand Up @@ -85,20 +84,20 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, env=
if e.errno == errno.ENOENT:
continue
if verbose:
print("unable to run %s" % dispcmd)
print(f"unable to run {dispcmd}")
print(e)
return None, None
else:
if verbose:
print("unable to find command, tried %s" % (commands, ))
print(f"unable to find command, tried {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)
print(f"unable to run {dispcmd} (error)")
print(f"stdout was {stdout}")
return None, p.returncode
return stdout, p.returncode

Expand All @@ -112,7 +111,7 @@ def versions_from_parentdir(parentdir_prefix, root, verbose):
"""
rootdirs = []

for i in range(3):
for _ in range(3):
dirname = os.path.basename(root)
if dirname.startswith(parentdir_prefix):
return {
Expand All @@ -122,12 +121,14 @@ def versions_from_parentdir(parentdir_prefix, root, verbose):
"error": None,
"date": None
}
else:
rootdirs.append(root)
root = os.path.dirname(root) # up a level
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))
print(
f"Tried directories {rootdirs} but none started with prefix {parentdir_prefix}"
)

raise NotThisMethod("rootdir doesn't start with parentdir_prefix")


Expand All @@ -140,21 +141,17 @@ def git_get_keywords(versionfile_abs):
# _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()
with open(versionfile_abs, "r") as f:
for line in f:
if line.strip().startswith("git_refnames ="):
if mo := re.search(r'=\s*"(.*)"', line):
keywords["refnames"] = mo[1]
if line.strip().startswith("git_full ="):
if mo := re.search(r'=\s*"(.*)"', line):
keywords["full"] = mo[1]
if line.strip().startswith("git_date ="):
if mo := re.search(r'=\s*"(.*)"', line):
keywords["date"] = mo[1]
except EnvironmentError:
pass
return keywords
Expand All @@ -179,11 +176,11 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
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(",")])
refs = {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)])
tags = {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
Expand All @@ -192,17 +189,17 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
# 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)])
tags = {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)))
print(f'likely tags: {",".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)
print(f"picking {r}")
return {
"version": r,
"full-revisionid": keywords["full"].strip(),
Expand Down Expand Up @@ -230,21 +227,29 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
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"]

GITS = ["git.cmd", "git.exe"] if sys.platform == "win32" else ["git"]
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)
print(f"Directory {root} not under git control")
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)
GITS,
[
"describe",
"--tags",
"--dirty",
"--always",
"--long",
"--match",
f"{tag_prefix}*",
],
cwd=root,
)

# --long was added in git-1.5.5
if describe_out is None:
raise NotThisMethod("'git describe' failed")
Expand All @@ -254,11 +259,7 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
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

pieces = {"long": full_out, "short": full_out[:7], "error": None}
# parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty]
# TAG might have hyphens.
git_describe = describe_out
Expand All @@ -280,7 +281,7 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
return pieces

# tag
full_tag = mo.group(1)
full_tag = mo[1]
if not full_tag.startswith(tag_prefix):
if verbose:
fmt = "tag '%s' doesn't start with prefix '%s'"
Expand All @@ -290,10 +291,10 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
pieces["closest-tag"] = full_tag[len(tag_prefix):]

# distance: number of commits since tag
pieces["distance"] = int(mo.group(2))
pieces["distance"] = int(mo[2])

# commit: short hex revision ID
pieces["short"] = mo.group(3)
pieces["short"] = mo[3]

else:
# HEX: no tags
Expand All @@ -310,9 +311,7 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):

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 "+"
return "." if "+" in pieces.get("closest-tag", "") else "+"


def render_pep440(pieces):
Expand All @@ -329,13 +328,11 @@ def render_pep440(pieces):
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"
if pieces["dirty"]:
rendered += ".dirty"
return rendered


Expand Down Expand Up @@ -372,13 +369,13 @@ def render_pep440_post(pieces):
if pieces["dirty"]:
rendered += ".dev0"
rendered += plus_or_dot(pieces)
rendered += "g%s" % pieces["short"]
rendered += f'g{pieces["short"]}'
else:
# exception #1
rendered = "0.post%d" % pieces["distance"]
if pieces["dirty"]:
rendered += ".dev0"
rendered += "+g%s" % pieces["short"]
rendered += f'+g{pieces["short"]}'
return rendered


Expand All @@ -394,13 +391,11 @@ def render_pep440_old(pieces):
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"
if pieces["dirty"]:
rendered += ".dev0"
return rendered


Expand Down
Loading