Skip to content

Commit

Permalink
Update output file CLI options (#89)
Browse files Browse the repository at this point in the history
Co-authored-by: ElliottKasoar <ElliottKasoar@users.noreply.github.com>
  • Loading branch information
ElliottKasoar and ElliottKasoar committed Mar 22, 2024
1 parent b9d05bb commit 7090317
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
33 changes: 27 additions & 6 deletions janus_core/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ def singlepoint(
),
),
] = None,
out_file: Annotated[
Path,
typer.Option(
"--out",
help=(
"Path to save structure with calculated results. Default is inferred "
"from name of structure file."
),
),
] = None,
read_kwargs: ReadKwargs = None,
calc_kwargs: CalcKwargs = None,
write_kwargs: WriteKwargs = None,
Expand All @@ -174,6 +184,9 @@ def singlepoint(
Device to run model on. Default is "cpu".
properties : Optional[str]
Physical properties to calculate. Default is "energy".
out_file : Optional[Path]
Path to save structure with calculated results. Default is inferred from name
of the structure file.
read_kwargs : Optional[dict[str, Any]]
Keyword arguments to pass to ase.io.read. Default is {}.
calc_kwargs : Optional[dict[str, Any]]
Expand All @@ -187,6 +200,14 @@ def singlepoint(
[read_kwargs, calc_kwargs, write_kwargs]
)

# Check filename for results not duplicated
if "filename" in write_kwargs:
raise ValueError("'filename' must be passed through the --out option")

# Default filname for saving results determined in SinglePoint if not specified
if out_file:
write_kwargs["filename"] = out_file

s_point = SinglePoint(
struct_path=struct_path,
architecture=architecture,
Expand Down Expand Up @@ -225,10 +246,10 @@ def geomopt( # pylint: disable=too-many-arguments,too-many-locals
help="Fully optimize the cell vectors, angles, and atomic positions.",
),
] = False,
opt_file: Annotated[
out_file: Annotated[
Path,
typer.Option(
"--opt",
"--out",
help=(
"Path to save optimized structure. Default is inferred from name "
"of structure file."
Expand Down Expand Up @@ -282,7 +303,7 @@ def geomopt( # pylint: disable=too-many-arguments,too-many-locals
fully_opt : bool
Whether to fully optimize the cell vectors, angles, and atomic positions.
Default is False.
opt_file : Optional[Path]
out_file : Optional[Path]
Path to save optimized structure, or last structure if optimization did not
converge. Default is inferred from name of structure file.
traj_file : Optional[str]
Expand Down Expand Up @@ -315,15 +336,15 @@ def geomopt( # pylint: disable=too-many-arguments,too-many-locals

# Check optimized structure path not duplicated
if "filename" in write_kwargs:
raise ValueError("'filename' must be passed through the --opt option")
raise ValueError("'filename' must be passed through the --out option")

# Check trajectory path not duplicated
if "trajectory" in opt_kwargs:
raise ValueError("'trajectory' must be passed through the --traj option")

# Set default filname for writing optimized structure if not specified
if opt_file:
write_kwargs["filename"] = opt_file
if out_file:
write_kwargs["filename"] = out_file
else:
write_kwargs["filename"] = f"{s_point.struct_name}-opt.xyz"

Expand Down
14 changes: 7 additions & 7 deletions tests/test_geomopt_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_geomopt_log(tmp_path, caplog):
"geomopt",
"--struct",
DATA_PATH / "NaCl.cif",
"--opt",
"--out",
results_path,
"--log",
f"{tmp_path}/test.log",
Expand All @@ -75,7 +75,7 @@ def test_geomopt_traj(tmp_path):
"geomopt",
"--struct",
DATA_PATH / "NaCl.cif",
"--opt",
"--out",
results_path,
"--traj",
traj_path,
Expand All @@ -97,7 +97,7 @@ def test_fully_opt(tmp_path, caplog):
"geomopt",
"--struct",
DATA_PATH / "NaCl-deformed.cif",
"--opt",
"--out",
results_path,
"--fully-opt",
],
Expand All @@ -123,7 +123,7 @@ def test_fully_opt_and_vectors(tmp_path, caplog):
DATA_PATH / "NaCl-deformed.cif",
"--fully-opt",
"--vectors-only",
"--opt",
"--out",
results_path,
"--log",
f"{tmp_path}/test.log",
Expand All @@ -148,7 +148,7 @@ def test_vectors_not_fully_opt(tmp_path, caplog):
"geomopt",
"--struct",
DATA_PATH / "NaCl.cif",
"--opt",
"--out",
results_path,
"--vectors-only",
],
Expand Down Expand Up @@ -186,7 +186,7 @@ def test_restart(tmp_path):
"geomopt",
"--struct",
data_path,
"--opt",
"--out",
results_path,
"--opt-kwargs",
f"{{'restart': '{str(restart_path)}'}}",
Expand All @@ -204,7 +204,7 @@ def test_restart(tmp_path):
"geomopt",
"--struct",
DATA_PATH / "NaCl.cif",
"--opt",
"--out",
results_path,
"--opt-kwargs",
f"{{'restart': '{str(restart_path)}'}}",
Expand Down
20 changes: 10 additions & 10 deletions tests/test_singlepoint_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def test_singlepoint_properties(tmp_path):
DATA_PATH / "H2O.cif",
"--property",
"energy",
"--write-kwargs",
f"{{'filename': '{str(results_path_1)}'}}",
"--out",
results_path_1,
],
)
assert result.exit_code == 0
Expand All @@ -79,8 +79,8 @@ def test_singlepoint_properties(tmp_path):
DATA_PATH / "H2O.cif",
"--property",
"stress",
"--write-kwargs",
f"{{'filename': '{str(results_path_2)}'}}",
"--out",
results_path_2,
],
)
assert result.exit_code == 1
Expand All @@ -100,8 +100,8 @@ def test_singlepoint_read_kwargs(tmp_path):
DATA_PATH / "benzene-traj.xyz",
"--read-kwargs",
"{'index': ':'}",
"--write-kwargs",
f"{{'filename': '{str(results_path)}'}}",
"--out",
results_path,
"--property",
"energy",
],
Expand All @@ -124,8 +124,8 @@ def test_singlepoint_calc_kwargs(tmp_path):
DATA_PATH / "NaCl.cif",
"--calc-kwargs",
"{'default_dtype': 'float32'}",
"--write-kwargs",
f"{{'filename': '{str(results_path)}'}}",
"--out",
results_path,
"--property",
"energy",
],
Expand All @@ -146,8 +146,8 @@ def test_singlepoint_log(tmp_path, caplog):
DATA_PATH / "NaCl.cif",
"--property",
"energy",
"--write-kwargs",
f"{{'filename': '{str(results_path)}'}}",
"--out",
results_path,
"--log",
f"{tmp_path}/test.log",
],
Expand Down

0 comments on commit 7090317

Please sign in to comment.