Skip to content

Commit

Permalink
Run black on custodian.
Browse files Browse the repository at this point in the history
  • Loading branch information
shyuep committed Jan 31, 2021
1 parent 0c12654 commit 64c4be6
Show file tree
Hide file tree
Showing 33 changed files with 323 additions and 738 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ jobs:
max-parallel: 1
matrix:
python-version: [3.9]

steps:
- uses: actions/checkout@v2
with:
Expand All @@ -22,9 +21,6 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -r requirements-ci.txt --quiet
- name: pycodestyle
run: |
pycodestyle custodian
- name: mypy
run: |
mypy custodian
Expand Down
4 changes: 1 addition & 3 deletions custodian/ansible/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,7 @@ def file_create(filename, settings):
settings (dict): Must be {"content": actual_content}
"""
if len(settings) != 1:
raise ValueError(
"Settings must only contain one item with key " "'content'."
)
raise ValueError("Settings must only contain one item with key " "'content'.")
for k, v in settings.items():
if k == "content":
with open(filename, "w") as f:
Expand Down
1 change: 1 addition & 0 deletions custodian/ansible/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@ def modify_object(self, modification, obj):

if __name__ == "__main__":
import doctest

doctest.testmod()
12 changes: 3 additions & 9 deletions custodian/ansible/tests/test_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,13 @@ def test_dict_modify(self):
self.assertEqual(d, {"Bye": "World", "List": [], "number": 10})
mod = {"_push_all": {"List": list(range(10))}}
modder.modify(mod, d)
self.assertEqual(
d, {"Bye": "World", "List": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], "number": 10}
)
self.assertEqual(d, {"Bye": "World", "List": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], "number": 10})
mod = {"_pop": {"List": 1}}
modder.modify(mod, d)
self.assertEqual(
d, {"Bye": "World", "List": [0, 1, 2, 3, 4, 5, 6, 7, 8], "number": 10}
)
self.assertEqual(d, {"Bye": "World", "List": [0, 1, 2, 3, 4, 5, 6, 7, 8], "number": 10})
mod = {"_pop": {"List": -1}}
modder.modify(mod, d)
self.assertEqual(
d, {"Bye": "World", "List": [1, 2, 3, 4, 5, 6, 7, 8], "number": 10}
)
self.assertEqual(d, {"Bye": "World", "List": [1, 2, 3, 4, 5, 6, 7, 8], "number": 10})
d = {}
mod = {"_set": {"a->b->c": 100}}
modder.modify(mod, d)
Expand Down
3 changes: 1 addition & 2 deletions custodian/cli/converge_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ def do_run(args):
nargs="?",
default="pvasp",
type=str,
help="VASP command. Defaults to pvasp. If you are using mpirun, "
'set this to something like "mpirun pvasp".',
help="VASP command. Defaults to pvasp. If you are using mpirun, " 'set this to something like "mpirun pvasp".',
)

parser.add_argument(
Expand Down
5 changes: 2 additions & 3 deletions custodian/cli/converge_kpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def main():
default convergence criteria is 1meV/atom, but this can be set using the
--target option.
""",
epilog="""Author: Shyue Ping Ong"""
epilog="""Author: Shyue Ping Ong""",
)

parser.add_argument(
Expand All @@ -103,8 +103,7 @@ def main():
nargs="?",
default="pvasp",
type=str,
help="VASP command. Defaults to pvasp. If you are using mpirun, "
'set this to something like "mpirun pvasp".',
help="VASP command. Defaults to pvasp. If you are using mpirun, " 'set this to something like "mpirun pvasp".',
)

parser.add_argument(
Expand Down
4 changes: 1 addition & 3 deletions custodian/cli/cstdn.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ def main():
subparsers = parser.add_subparsers()

prun = subparsers.add_parser("run", help="Run custodian.")
prun.add_argument(
"spec_file", metavar="spec_file", type=str, nargs=1, help="YAML/JSON spec file."
)
prun.add_argument("spec_file", metavar="spec_file", type=str, nargs=1, help="YAML/JSON spec file.")
prun.set_defaults(func=run)

prun = subparsers.add_parser(
Expand Down
6 changes: 2 additions & 4 deletions custodian/cli/run_nwchem.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ def do_run(args):
"""
Do the run.
"""
logging.basicConfig(
format="%(asctime)s %(message)s", level=logging.INFO, filename="run.log"
)
logging.basicConfig(format="%(asctime)s %(message)s", level=logging.INFO, filename="run.log")
job = NwchemJob(
nwchem_cmd=args.command.split(),
input_file=args.infile,
Expand All @@ -44,7 +42,7 @@ def main():
description="""
run_nwchem is a master script to perform various kinds of Nwchem runs.
""",
epilog="""Author: Shyue Ping Ong"""
epilog="""Author: Shyue Ping Ong""",
)

parser.add_argument(
Expand Down
33 changes: 9 additions & 24 deletions custodian/cli/run_vasp.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ def get_jobs(args):
copy_magmom = False
vinput = VaspInput.from_directory(".")
if i > 0:
settings.append(
{"file": "CONTCAR", "action": {"_file_copy": {"dest": "POSCAR"}}}
)
settings.append({"file": "CONTCAR", "action": {"_file_copy": {"dest": "POSCAR"}}})

job_type = job.lower()
auto_npar = True
Expand Down Expand Up @@ -123,9 +121,7 @@ def get_jobs(args):
elif job_type.startswith("nonscf_derived"):
from pymatgen.io.vasp.sets import MPNonSCFSet

vis = MPNonSCFSet.from_prev_calc(
".", copy_chgcar=False, user_incar_settings={"LWAVE": True}
)
vis = MPNonSCFSet.from_prev_calc(".", copy_chgcar=False, user_incar_settings={"LWAVE": True})
settings.extend(
[
{"dict": "INCAR", "action": {"_set": dict(vis.incar)}},
Expand Down Expand Up @@ -178,20 +174,12 @@ def get_jobs(args):
incar = vinput["INCAR"]
structure = vinput["POSCAR"].structure
if "ISMEAR" in incar:
post_settings.append(
{"dict": "INCAR", "action": {"_set": {"ISMEAR": incar["ISMEAR"]}}}
)
post_settings.append({"dict": "INCAR", "action": {"_set": {"ISMEAR": incar["ISMEAR"]}}})
else:
post_settings.append(
{"dict": "INCAR", "action": {"_unset": {"ISMEAR": 1}}}
)
post_settings.append(
{"dict": "KPOINTS", "action": {"_set": kpoints.as_dict()}}
)
post_settings.append({"dict": "INCAR", "action": {"_unset": {"ISMEAR": 1}}})
post_settings.append({"dict": "KPOINTS", "action": {"_set": kpoints.as_dict()}})
# lattice vectors with length < 9 will get >1 KPOINT
low_kpoints = Kpoints.gamma_automatic(
[max(int(18 / l), 1) for l in structure.lattice.abc]
)
low_kpoints = Kpoints.gamma_automatic([max(int(18 / l), 1) for l in structure.lattice.abc])
settings.extend(
[
{"dict": "INCAR", "action": {"_set": {"ISMEAR": 0}}},
Expand All @@ -202,9 +190,7 @@ def get_jobs(args):
# let vasp determine encut (will be lower than
# needed for compatibility with other runs)
if "ENCUT" in incar:
post_settings.append(
{"dict": "INCAR", "action": {"_set": {"ENCUT": incar["ENCUT"]}}}
)
post_settings.append({"dict": "INCAR", "action": {"_set": {"ENCUT": incar["ENCUT"]}}})
settings.append({"dict": "INCAR", "action": {"_unset": {"ENCUT": 1}}})

elif job_type.startswith("relax"):
Expand Down Expand Up @@ -258,7 +244,7 @@ def main():

parser = argparse.ArgumentParser(
description="run_vasp is a master script to perform various kinds of VASP runs.",
epilog="Author: Shyue Ping Ong"
epilog="Author: Shyue Ping Ong",
)

parser.add_argument(
Expand All @@ -268,8 +254,7 @@ def main():
nargs="?",
default="pvasp",
type=str,
help="VASP command. Defaults to pvasp. If you are using mpirun, "
'set this to something like "mpirun pvasp".',
help="VASP command. Defaults to pvasp. If you are using mpirun, " 'set this to something like "mpirun pvasp".',
)

parser.add_argument(
Expand Down
56 changes: 15 additions & 41 deletions custodian/custodian.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ def _save_checkpoint(cwd, index):
except Exception:
logger.info("Checkpointing failed")
import traceback

logger.error(traceback.format_exc())

@classmethod
Expand Down Expand Up @@ -328,9 +329,7 @@ def process_params(d):

custodian_params = process_params(spec.get("custodian_params", {}))

return cls(
jobs=jobs, handlers=handlers, validators=validators, **custodian_params
)
return cls(jobs=jobs, handlers=handlers, validators=validators, **custodian_params)

def run(self):
"""
Expand Down Expand Up @@ -432,10 +431,7 @@ def _run_job(self, job_n, job):
job.setup()

attempt = 0
while (
self.total_errors < self.max_errors
and self.errors_current_job < self.max_errors_per_job
):
while self.total_errors < self.max_errors and self.errors_current_job < self.max_errors_per_job:
attempt += 1
logger.info(
"Starting job no. {} ({}) attempt no. {}. Total errors and "
Expand Down Expand Up @@ -467,18 +463,13 @@ def _run_job(self, job_n, job):
time.sleep(self.polling_time_step)
else:
p.wait()
if (
self.terminate_func is not None
and self.terminate_func != p.terminate
):
if self.terminate_func is not None and self.terminate_func != p.terminate:
self.terminate_func()
time.sleep(self.polling_time_step)

zero_return_code = p.returncode == 0

logger.info(
"{}.run has completed. " "Checking remaining handlers".format(job.name)
)
logger.info("{}.run has completed. " "Checking remaining handlers".format(job.name))
# Check for errors again, since in some cases non-monitor
# handlers fix the problems detected by monitors
# if an error has been found, not all handlers need to run
Expand All @@ -505,10 +496,7 @@ def _run_job(self, job_n, job):
s = "Job return code is %d. Terminating..." % p.returncode
logger.info(s)
raise ReturnCodeError(s, True)
warnings.warn(
"subprocess returned a non-zero return "
"code. Check outputs carefully..."
)
warnings.warn("subprocess returned a non-zero return " "code. Check outputs carefully...")
job.postprocess()
return

Expand Down Expand Up @@ -555,9 +543,7 @@ def run_interrupted(self):
try:
cwd = os.getcwd()
v = sys.version.replace("\n", " ")
logger.info(
"Custodian started in singleshot mode at {} in {}.".format(start, cwd)
)
logger.info("Custodian started in singleshot mode at {} in {}.".format(start, cwd))
logger.info("Custodian running on Python version {}".format(v))

# load run log
Expand All @@ -570,9 +556,7 @@ def run_interrupted(self):
job = self.jobs[job_n]
logger.info("Setting up job no. 1 ({}) ".format(job.name))
job.setup()
self.run_log.append(
{"job": job.as_dict(), "corrections": [], "job_n": job_n}
)
self.run_log.append({"job": job.as_dict(), "corrections": [], "job_n": job_n})
return len(self.jobs)

# Continuing after running calculation
Expand All @@ -592,10 +576,7 @@ def run_interrupted(self):
for x in self.run_log[-1]["corrections"]:
if not x["actions"] and x["handler"].raises_runtime_error:
self.run_log[-1]["handler"] = x["handler"]
s = (
"Unrecoverable error for handler: {}. "
"Raising RuntimeError".format(x["handler"])
)
s = "Unrecoverable error for handler: {}. " "Raising RuntimeError".format(x["handler"])
raise NonRecoverableError(s, True, x["handler"])
logger.info("Corrected input based on error handlers")
# Return with more jobs to run if recoverable error caught
Expand Down Expand Up @@ -623,9 +604,7 @@ def run_interrupted(self):
# Setup next job_n
job_n += 1
job = self.jobs[job_n]
self.run_log.append(
{"job": job.as_dict(), "corrections": [], "job_n": job_n}
)
self.run_log.append({"job": job.as_dict(), "corrections": [], "job_n": job_n})
job.setup()
return len(self.jobs) - job_n

Expand Down Expand Up @@ -653,20 +632,14 @@ def _do_check(self, handlers, terminate_func=None):
for h in handlers:
try:
if h.check():
if (
h.max_num_corrections is not None
and h.n_applied_corrections >= h.max_num_corrections
):
msg = (
"Maximum number of corrections {} reached "
"for handler {}".format(h.max_num_corrections, h)
if h.max_num_corrections is not None and h.n_applied_corrections >= h.max_num_corrections:
msg = "Maximum number of corrections {} reached " "for handler {}".format(
h.max_num_corrections, h
)
if h.raise_on_max:
self.run_log[-1]["handler"] = h
self.run_log[-1]["max_errors_per_handler"] = True
raise MaxCorrectionsPerHandlerError(
msg, True, h.max_num_corrections, h
)
raise MaxCorrectionsPerHandlerError(msg, True, h.max_num_corrections, h)
logger.warning(msg + " Correction not applied.")
continue
if terminate_func is not None and h.is_terminating:
Expand All @@ -683,6 +656,7 @@ def _do_check(self, handlers, terminate_func=None):
if not self.skip_over_errors:
raise
import traceback

logger.error("Bad handler %s " % h)
logger.error(traceback.format_exc())
corrections.append({"errors": ["Bad handler %s " % h], "actions": []})
Expand Down
20 changes: 5 additions & 15 deletions custodian/feff/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,47 +91,37 @@ def correct(self):
if nscmt < 100 and ca == 0.2:
scf_values[2] = 100
scf_values[4] = 3 # Set nmix = 3
actions.append(
{"dict": "PARAMETERS", "action": {"_set": {"SCF": scf_values}}}
)
actions.append({"dict": "PARAMETERS", "action": {"_set": {"SCF": scf_values}}})
FeffModder().apply_actions(actions)
return {"errors": ["Non-converging job"], "actions": actions}

if nscmt == 100 and nmix == 3 and ca > 0.01:
# Reduce the convergence accelerator factor
scf_values[3] = round(ca / 2, 2)
actions.append(
{"dict": "PARAMETERS", "action": {"_set": {"SCF": scf_values}}}
)
actions.append({"dict": "PARAMETERS", "action": {"_set": {"SCF": scf_values}}})
FeffModder().apply_actions(actions)
return {"errors": ["Non-converging job"], "actions": actions}

if nmix == 3 and ca == 0.01:
# Set ca = 0.05 and set nmix
scf_values[3] = 0.05
scf_values[4] = 5
actions.append(
{"dict": "PARAMETERS", "action": {"_set": {"SCF": scf_values}}}
)
actions.append({"dict": "PARAMETERS", "action": {"_set": {"SCF": scf_values}}})
FeffModder().apply_actions(actions)
return {"errors": ["Non-converging job"], "actions": actions}

if nmix == 5 and ca == 0.05:
# Set ca = 0.05 and set nmix
scf_values[3] = 0.05
scf_values[4] = 10
actions.append(
{"dict": "PARAMETERS", "action": {"_set": {"SCF": scf_values}}}
)
actions.append({"dict": "PARAMETERS", "action": {"_set": {"SCF": scf_values}}})
FeffModder().apply_actions(actions)
return {"errors": ["Non-converging job"], "actions": actions}

if nmix == 10 and ca < 0.2:
# loop through ca with nmix = 10
scf_values[3] = round(ca * 2, 2)
actions.append(
{"dict": "PARAMETERS", "action": {"_set": {"SCF": scf_values}}}
)
actions.append({"dict": "PARAMETERS", "action": {"_set": {"SCF": scf_values}}})
FeffModder().apply_actions(actions)
return {"errors": ["Non-converging job"], "actions": actions}

Expand Down
Loading

0 comments on commit 64c4be6

Please sign in to comment.