From 26b828b326d206f1472178e67444bbb397958b88 Mon Sep 17 00:00:00 2001 From: "Andrew S. Rosen" Date: Mon, 11 Mar 2024 21:59:35 -0700 Subject: [PATCH] Add missing `directory` kwarg on QCJob `run()` method (#323) * Add missing `directory` kwarg on QCJob `run()` method * add doc strings and type annos --------- Co-authored-by: Janosh Riebesell --- .github/workflows/{linting.yml => lint.yml} | 2 ++ .github/workflows/test.yml | 2 +- custodian/qchem/jobs.py | 36 ++++++++++++++++----- 3 files changed, 31 insertions(+), 9 deletions(-) rename .github/workflows/{linting.yml => lint.yml} (99%) diff --git a/.github/workflows/linting.yml b/.github/workflows/lint.yml similarity index 99% rename from .github/workflows/linting.yml rename to .github/workflows/lint.yml index 503c0d34..b81e478b 100644 --- a/.github/workflows/linting.yml +++ b/.github/workflows/lint.yml @@ -13,11 +13,13 @@ jobs: - uses: actions/checkout@v4 with: fetch-depth: 0 + - name: Set up Python uses: actions/setup-python@v5 with: python-version: 3.11 cache: pip + - name: Run pre-commit run: | pip install pre-commit diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d3459831..5d8cd672 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -51,7 +51,7 @@ jobs: - name: pytest env: PMG_MAPI_KEY: ${{ secrets.PMG_MAPI_KEY }} - MPLBACKEND: "Agg" + MPLBACKEND: Agg run: pytest --cov=custodian --color=yes tests - name: Upload coverage reports to Codecov diff --git a/custodian/qchem/jobs.py b/custodian/qchem/jobs.py index bf965ae8..399310f9 100644 --- a/custodian/qchem/jobs.py +++ b/custodian/qchem/jobs.py @@ -1,5 +1,7 @@ """This module implements basic kinds of jobs for QChem runs.""" +from __future__ import annotations + import copy import os import shutil @@ -100,8 +102,15 @@ def __init__( print("SLURM_CPUS_ON_NODE not in environment") @property - def current_command(self, directory="./"): - """The command to run QChem.""" + def current_command(self, directory: str | Path = "./"): + """The command to run QChem. + + Args: + directory (str): The directory to run in. Defaults to "./". + + Returns: + (str) The command to run QChem. + """ self._input_path = os.path.join(directory, self.input_file) self._output_path = os.path.join(directory, self.output_file) multi = {"openmp": "-nt", "mpi": "-np"} @@ -111,8 +120,12 @@ def current_command(self, directory="./"): command = self.qchem_command + command return " ".join(command) - def setup(self, directory="./"): - """Sets up environment variables necessary to efficiently run QChem.""" + def setup(self, directory: str | Path = "./"): + """Sets up environment variables necessary to efficiently run QChem. + + Args: + directory (str): The directory to run in. Defaults to "./". + """ self._input_path = os.path.join(directory, self.input_file) if self.backup: shutil.copy(self._input_path, os.path.join(directory, f"{self.input_file}.orig")) @@ -132,8 +145,12 @@ def setup(self, directory="./"): raise RuntimeError("Trying to run NBO7 without providing NBOEXE in fworker! Exiting...") os.environ["NBOEXE"] = self.nboexe - def postprocess(self, directory="./"): - """Renames and removes scratch files after running QChem.""" + def postprocess(self, directory: str | Path = "./"): + """Renames and removes scratch files after running QChem. + + Args: + directory (str): The directory to run in. Defaults to "./". + """ self._input_path = os.path.join(directory, self.input_file) self._output_path = os.path.join(directory, self.output_file) self._qclog_path = os.path.join(directory, self.qclog_file) @@ -155,10 +172,13 @@ def postprocess(self, directory="./"): except FileNotFoundError: pass - def run(self): + def run(self, directory: str | Path = "./"): """ Perform the actual QChem run. + Args: + directory (str): The directory to run in. Defaults to "./". + Returns: (subprocess.Popen) Used for monitoring. """ @@ -172,7 +192,7 @@ def run(self): os.makedirs(local_scratch, exist_ok=True) shutil.move(os.path.join(os.environ["QCSCRATCH"], "53.0"), local_scratch) with open(self.qclog_file, "w") as qclog: - return subprocess.Popen(self.current_command, stdout=qclog, shell=True) # pylint: disable=R1732 + return subprocess.Popen(self.current_command, cwd=directory, stdout=qclog, shell=True) # pylint: disable=R1732 @classmethod def opt_with_frequency_flattener(