Skip to content

Commit

Permalink
Merge pull request #48 from TREX-CoE/improve-overwriting
Browse files Browse the repository at this point in the history
Improve force overwriting for existing output files
  • Loading branch information
scemama authored Nov 11, 2024
2 parents 9d7b794 + f3d3ecd commit 7e73d06
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 45 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ jobs:
trexio convert-to -t cartesian -o trexio_orca_h2o.h5 trexio_orca_h2o_sph.h5
trexio convert-to -t cartesian -o trexio_pyscf_h2o.h5 trexio_pyscf_h2o_sph.h5
echo "== Done conversion =="
# dummy checks for overwriting
trexio convert-from -t gaussian -i data/chbrclf.log -b hdf5 -w False trexio_gaussi.h5 || echo "Failure as it should"
trexio convert-from -t gaussian -i data/chbrclf.log -b hdf5 -w True trexio_gaussi.h5 && echo "Success as it should"
echo "=== Check TREXIO file converted from GAMESS ==="
trexio check-mos -n 50 trexio_gamess.h5 > mos-res
grep "Norm of the error" < mos-res | grep -Eo "([0-9]+\.[0-9]*|\.?[0-9]+)([eE][+-][0-9]+)?" > error-res
Expand Down
8 changes: 4 additions & 4 deletions src/trexio_tools/converters/convert_back_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ def run_converter(filename_from, filename_to, back_end_to, back_end_from=None, o
"""The high-level converter function."""

import os
try:
import trexio
except ImportError as exc:
raise ImportError("trexio Python package is not installed.") from exc
import trexio

try:
# For proper python package
Expand All @@ -115,6 +112,9 @@ def run_converter(filename_from, filename_to, back_end_to, back_end_from=None, o
else:
raise ValueError(f'Output file {filename_to} already exists. Consider using the `-w` argument.')

if back_end_from is None:
back_end_from = trexio.TREXIO_AUTO

with trexio.File(filename_from, 'r', back_end_from) as trexio_file_from:
with trexio.File(filename_to, 'w', back_end_to) as trexio_file_to:
data_handler(trexio_file_from, trexio_file_to)
Expand Down
12 changes: 1 addition & 11 deletions src/trexio_tools/converters/convert_from.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
from .orca_to_trexio import orca_to_trexio as run_orca
from .crystal_to_trexio import crystal_to_trexio as run_crystal

try:
import trexio
except ImportError as exc:
raise ImportError("trexio Python package is not installed.") from exc
import trexio

try:
from resultsFile import getFile, a0, get_lm
Expand Down Expand Up @@ -759,13 +756,6 @@ def run_molden(trexio_file, filename, normalized_basis=True, multiplicity=None,

def run(trexio_filename, filename, filetype, back_end, spin=None, motype=None):

if os.path.exists(trexio_filename):
print(f"TREXIO file {trexio_filename} already exists and will be removed before conversion.")
if back_end == trexio.TREXIO_HDF5:
os.remove(trexio_filename)
else:
raise NotImplementedError(f"Please remove the {trexio_filename} directory manually.")

if "pyscf" not in filetype.lower():
trexio_file = trexio.File(trexio_filename, mode='w', back_end=back_end)

Expand Down
9 changes: 1 addition & 8 deletions src/trexio_tools/converters/convert_to.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
from . import cart_sphe as cart_sphe
import numpy as np

try:
import trexio
except:
print("Error: The TREXIO Python library is not installed")
sys.exit(1)
import trexio

"""
Converter from trexio to fcidump
Expand Down Expand Up @@ -606,11 +602,8 @@ def run(trexio_filename, filename, filetype, spin_order):
run_spherical(trexio_file, filename)
# elif filetype.lower() == "normalized_aos":
# run_normalized_aos(trexio_file, filename)
# elif filetype.lower() == "gamess":
# run_resultsFile(trexio_file, filename)
elif filetype.lower() == "fcidump":
run_fcidump(trexio_file, filename, spin_order)
# elif filetype.lower() == "molden":
else:
raise NotImplementedError(f"Conversion from TREXIO to {filetype} is not supported.")

6 changes: 0 additions & 6 deletions src/trexio_tools/converters/orca_to_trexio.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ def orca_to_trexio(
with open(orca_json, 'r') as f:
data = json.load(f)

import os
try:
os.remove(filename)
except:
print(f"File {filename} does not exist.")

# trexio back end handling
if back_end.lower() == "hdf5":
trexio_back_end = trexio.TREXIO_HDF5
Expand Down
51 changes: 35 additions & 16 deletions src/trexio_tools/trexio_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
trexio check-basis [-n N_POINTS] [-b BACK_END] TREXIO_FILE
trexio check-mos [-n N_POINTS] [-b BACK_END] TREXIO_FILE
trexio convert-to -t TYPE -o OUTPUT_FILE [-y SPIN_ORDER] TREXIO_FILE
trexio convert-from -t TYPE -i INPUT_FILE [-b BACK_END] [-x MO_TYPE] [-m MULTIPLICITY] TREXIO_FILE
trexio convert-from -t TYPE -i INPUT_FILE [-b BACK_END] [-x MO_TYPE] [-m MULTIPLICITY] [-w OVERWRITE] TREXIO_FILE
trexio convert-backend -i INPUT_FILE -o OUTPUT_FILE -b BACK_END -j TREX_JSON_FILE [-s BACK_END_FROM] [-w OVERWRITE]
trexio (-h | --help)
Expand All @@ -19,7 +19,7 @@
-s, --back_end_from=BACK_END [hdf5 | text | auto] The input TREXIO back end. [default: auto]
-m, --multiplicity=MULTIPLICITY Spin multiplicity for the Crystal converter.
-j, --json=TREX_JSON_FILE TREX configuration file (in JSON format).
-w, --overwrite=OVERWRITE Overwrite flag for the conversion of back ends. [default: True]
-w, --overwrite=OVERWRITE Overwrite the output TREXIO file if it already exists. [default: True]
-t, --type=TYPE [gaussian | gamess | pyscf | orca | crystal | fcidump | molden | cartesian ] File format.
-x, --motype=MO_TYPE Type of the molecular orbitals. For example, GAMESS has RHF, MCSCF, GUGA, and Natural as possible MO types.
-y, --spin_order=TYPE [block | interleave] How to organize spin orbitals when converting to FCIDUMP [default: block]
Expand All @@ -30,24 +30,46 @@
import os


def main(filename=None, args=None):
def remove_trexio_file(filename:str, overwrite:bool) -> None:
"""Remove the TREXIO file/directory if it exists."""
if os.path.exists(filename):
if overwrite:
# dummy check
if '*' in filename:
raise ValueError(f'TREXIO filename {filename} contains * symbol. Are you sure?')
# check that the file is actually TREXIO file
try:
is_trexio = False
with trexio.File(filename, 'r', trexio.TREXIO_AUTO) as tfile:
if trexio.has_metadata_package_version(tfile):
is_trexio = True

if is_trexio: os.system(f'rm -rf -- {filename}')

except:
raise Exception(f'Output file {filename} exists but it is not a TREXIO file. Are you sure?')
else:
raise Exception(f'Output file {filename} already exists but overwrite option is not provided. Consider using the `-w` CLI argument.')

return


def main(filename=None, args=None) -> None:
"""Main entry point"""

if filename is None and args is None:
args = docopt(__doc__)
filename = args["TREXIO_FILE"]

if args["--n_points"]:
n_points = int(args["--n_points"])
else:
n_points = 81
n_points = int(args["--n_points"]) if args["--n_points"] else 81

overwrite = not str(args["--overwrite"]).lower() in ['false', '0']

if args["convert-backend"]:
if str(args["--overwrite"]).lower() in ['false', '0']:
overwrite = False
else:
overwrite = True
print(f'File {args["--output"]} will be overwritten.')
remove_trexio_file(args["--output"], overwrite)

if args["convert-from"]:
remove_trexio_file(args["TREXIO_FILE"], overwrite)

if args["--back_end"]:
if str(args["--back_end"]).lower() == "hdf5":
Expand All @@ -74,9 +96,7 @@ def main(filename=None, args=None):
else:
back_end_from = trexio.TREXIO_AUTO

spin = None
if args["--multiplicity"]:
spin = int(args["--multiplicity"])
spin = int(args["--multiplicity"]) if args["--multiplicity"] else None

if args["check-basis"]:
trexio_file = trexio.File(filename, 'r', back_end=back_end)
Expand Down Expand Up @@ -109,7 +129,6 @@ def main(filename=None, args=None):
args["--output"],
back_end_to=back_end,
back_end_from=back_end_from,
overwrite=overwrite,
json_filename=args["--json"]
)

Expand Down

0 comments on commit 7e73d06

Please sign in to comment.