Skip to content

Commit

Permalink
refactors RUN_PY as a file
Browse files Browse the repository at this point in the history
Makes RUN_PY a part of linting and testing,
as suggested by Sebastian Björkqvist
  • Loading branch information
yashasvi-ranawat committed Nov 8, 2023
1 parent 3e5ee4e commit 727e4cf
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 38 deletions.
20 changes: 10 additions & 10 deletions runner/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from abc import ABC, abstractmethod
from ase import db
from ase import Atoms
from runner.utils import Cd, RUN_PY
from runner.utils import Cd, run
from runner.utils.runnerdata import RunnerData

logger = logging.getLogger(__name__)
Expand All @@ -27,9 +27,6 @@
logger.addHandler(stream_handler)


default_files = ["run.sh", "batch.slrm", "atoms.pkl"]


class BaseRunner(ABC):
"""
Runner runs tasks
Expand Down Expand Up @@ -494,6 +491,9 @@ def _write_run_data(self, atoms, tasks, files, status, log_msg):
with open("atoms.pkl", "wb") as file_o:
pickle.dump(atoms, file_o)

# copy run file
shutil.copyfile(run.__file__, "run.py")

# write run scripts
run_scripts = []
py_run = 0
Expand All @@ -509,29 +509,29 @@ def _write_run_data(self, atoms, tasks, files, status, log_msg):
shell_run = "python"
if len(task) > 3:
shell_run = task[3]
shell_run += " run{}.py".format(py_run)
shell_run += " > run{}.out".format(py_run)
run_scripts.append(shell_run)

if len(task) > 2:
params = task[2]
else:
params = {}

# write params
try:
with open("params{}.json".format(py_run), "w") as file_o:
json.dump(params, file_o)
except TypeError as err:
status = "failed"
log_msg = "{}\n Error writing params: " "{}\n".format(
log_msg = "{}\n Error writing params: {}\n".format(
datetime.now(), err.args[0]
)
break
# making python executable
func_name = task[1]
func_name = func_name[:-3] if func_name.endswith(".py") else func_name
with open("run{}.py".format(py_run), "w") as file_o:
file_o.write(RUN_PY.format(func=func_name, ind=py_run))

# add to run_scripts
shell_run += f" run.py {func_name} {py_run} > run{py_run}.out"
run_scripts.append(shell_run)
py_run += 1

return run_scripts, status, log_msg
Expand Down
2 changes: 0 additions & 2 deletions runner/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from runner.utils.utils import (
Cd,
json_keys2int,
RUN_PY,
get_status,
submit,
cancel,
Expand All @@ -14,7 +13,6 @@


__all__ = [
"RUN_PY",
"Cd",
"json_keys2int",
"get_status",
Expand Down
35 changes: 35 additions & 0 deletions runner/utils/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
python file to run python tasks
"""

import json
import pickle
import argparse


def main():
# parse args
parser = argparse.ArgumentParser(description="Run function with params.")
parser.add_argument("func", type=str)
parser.add_argument("indx", type=int)
args = parser.parse_args()

# import module
func = __import__(args.func)

# open params and atoms
with open(f"params{args.indx}.json") as fio:
params = json.load(fio)
with open("atoms.pkl", "rb") as fio:
atoms = pickle.load(fio)

# run func
atoms = func.main(atoms, **params)

# write atoms
with open("atoms.pkl", "wb") as fio:
pickle.dump(atoms, fio)


if __name__ == "__main__":
main()
5 changes: 5 additions & 0 deletions runner/utils/runnerdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from runner.utils.utils import json_keys2int, get_db_connect


default_files = ["run.sh", "batch.slrm", "atoms.pkl", "run.py", "status.txt", "job.id"]


class RunnerData:
"""Class to handle runner data using helper function
Expand Down Expand Up @@ -372,6 +375,8 @@ def _test_files(files, log_msg=""):
err = log_msg + "Runner: files should be a dictionary\n"
raise RuntimeError(err)
for filename, content in files.items():
if filename in default_files:
raise RuntimeError(log_msg + f"Runner: {filename=} in {default_files=}")
if not isinstance(filename, str):
err = log_msg + "Runner: filenames should be str\n"
raise RuntimeError(err)
Expand Down
26 changes: 0 additions & 26 deletions runner/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,6 @@
import ase.db as db


RUN_PY = """
import json
import pickle
from ase.atoms import Atoms
from {func} import main
def json_keys2int(x):
# if dict key can be converted to int
# then convert to int
if isinstance(x, dict):
try:
return {{int(k):v for k,v in x.items()}}
except ValueError:
pass
return x
with open("params{ind}.json") as f:
params = json.load(f, object_hook=json_keys2int)
with open("atoms.pkl", "rb") as f:
atoms = pickle.load(f)
atoms = main(atoms, **params)
with open("atoms.pkl", "wb") as f:
pickle.dump(atoms, f)
"""


class Cd:
"""Context manager for changing the current working directory
Expand Down

0 comments on commit 727e4cf

Please sign in to comment.