Skip to content

Commit

Permalink
Update typer and Optional parameters (stfc#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElliottKasoar authored Sep 3, 2024
1 parent 9a51a55 commit 1861b04
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 48 deletions.
8 changes: 4 additions & 4 deletions janus_core/cli/descriptors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Set up MLIP descriptors commandline interface."""

from pathlib import Path
from typing import Annotated
from typing import Annotated, Optional

from typer import Context, Option, Typer
from typer_config import use_config
Expand Down Expand Up @@ -53,7 +53,7 @@ def descriptors(
device: Device = "cpu",
model_path: ModelPath = None,
out: Annotated[
Path,
Optional[Path],
Option(
help=(
"Path to save structure with calculated descriptors. Default is "
Expand Down Expand Up @@ -101,10 +101,10 @@ def descriptors(
Keyword arguments to pass to ase.io.write when saving results. Default is {}.
log : Optional[Path]
Path to write logs to. Default is inferred from the name of the structure file.
summary : Path
summary : Optional[Path]
Path to save summary of inputs, start/end time, and carbon emissions. Default
is inferred from the name of the structure file.
config : Path
config : Optional[Path]
Path to yaml configuration file to define the above options. Default is None.
"""
# Check options from configuration file are all valid
Expand Down
4 changes: 2 additions & 2 deletions janus_core/cli/eos.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ def eos(
chemical formula.
log : Optional[Path]
Path to write logs to. Default is inferred from the name of the structure file.
summary : Path
summary : Optional[Path]
Path to save summary of inputs, start/end time, and carbon emissions. Default
is inferred from the name of the structure file.
config : Path
config : Optional[Path]
Path to yaml configuration file to define the above options. Default is None.
"""
# Check options from configuration file are all valid
Expand Down
12 changes: 6 additions & 6 deletions janus_core/cli/geomopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def geomopt(
ctx: Context,
struct: StructPath,
optimizer: Annotated[
str,
Optional[str],
Option(help="Name of ASE optimizer function to use."),
] = "LBFGS",
fmax: Annotated[
Expand All @@ -117,7 +117,7 @@ def geomopt(
),
] = False,
filter_func: Annotated[
str,
Optional[str],
Option(
help=(
"Name of ASE filter/constraint function to use. If using "
Expand All @@ -130,7 +130,7 @@ def geomopt(
float, Option(help="Scalar pressure when optimizing cell geometry, in GPa.")
] = 0.0,
out: Annotated[
Path,
Optional[Path],
Option(
help=(
"Path to save optimized structure. Default is inferred from name "
Expand All @@ -140,7 +140,7 @@ def geomopt(
] = None,
traj: Annotated[
str,
Option(help="Path if saving optimization frames. [default: None]"),
Option(help="Path if saving optimization frames."),
] = None,
read_kwargs: ReadKwargsLast = None,
calc_kwargs: CalcKwargs = None,
Expand Down Expand Up @@ -202,10 +202,10 @@ def geomopt(
Default is {}.
log : Optional[Path]
Path to write logs to. Default is inferred from the name of the structure file.
summary : Path
summary : Optional[Path]
Path to save summary of inputs, start/end time, and carbon emissions. Default
is inferred from the name of the structure file.
config : Path
config : Optional[Path]
Path to yaml configuration file to define the above options. Default is None.
"""
# Check options from configuration file are all valid
Expand Down
18 changes: 9 additions & 9 deletions janus_core/cli/md.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def md(
int, Option(help="Restart files to keep if rotating.")
] = 4,
final_file: Annotated[
Path,
Optional[Path],
Option(
help=(
"""
Expand All @@ -140,7 +140,7 @@ def md(
),
] = None,
stats_file: Annotated[
Path,
Optional[Path],
Option(
help=(
"""
Expand All @@ -152,7 +152,7 @@ def md(
] = None,
stats_every: Annotated[int, Option(help="Frequency to output statistics.")] = 100,
traj_file: Annotated[
Path,
Optional[Path],
Option(help="File to save trajectory. Default inferred from `file_prefix`."),
] = None,
traj_append: Annotated[bool, Option(help="Whether to append trajectory.")] = False,
Expand All @@ -162,17 +162,17 @@ def md(
] = 100,
temp_start: Annotated[
Optional[float],
Option(help="Temperature to start heating, in K. [default: None]"),
Option(help="Temperature to start heating, in K."),
] = None,
temp_end: Annotated[
Optional[float],
Option(help="Maximum temperature for heating, in K. [default: None]"),
Option(help="Maximum temperature for heating, in K."),
] = None,
temp_step: Annotated[
float, Option(help="Size of temperature steps when heating, in K.")
Optional[float], Option(help="Size of temperature steps when heating, in K.")
] = None,
temp_time: Annotated[
float, Option(help="Time between heating steps, in fs.")
Optional[float], Option(help="Time between heating steps, in fs.")
] = None,
write_kwargs: WriteKwargs = None,
post_process_kwargs: PostProcessKwargs = None,
Expand Down Expand Up @@ -292,10 +292,10 @@ def md(
Default is None.
log : Optional[Path]
Path to write logs to. Default is inferred from the name of the structure file.
summary : Path
summary : Optional[Path]
Path to save summary of inputs, start/end time, and carbon emissions. Default
is inferred from the name of the structure file.
config : Path
config : Optional[Path]
Path to yaml configuration file to define the above options. Default is None.
"""
# Check options from configuration file are all valid
Expand Down
4 changes: 2 additions & 2 deletions janus_core/cli/phonons.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ def phonons(
chemical formula.
log : Optional[Path]
Path to write logs to. Default is inferred from the name of the structure file.
summary : Path
summary : Optional[Path]
Path to save summary of inputs, start/end time, and carbon emissions. Default
is inferred from the name of the structure file.
config : Path
config : Optional[Path]
Path to yaml configuration file to define the above options. Default is None.
"""
# Check options from configuration file are all valid
Expand Down
10 changes: 5 additions & 5 deletions janus_core/cli/singlepoint.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Set up singlepoint commandline interface."""

from pathlib import Path
from typing import Annotated
from typing import Annotated, Optional

from typer import Context, Option, Typer
from typer_config import use_config
Expand Down Expand Up @@ -41,7 +41,7 @@ def singlepoint(
device: Device = "cpu",
model_path: ModelPath = None,
properties: Annotated[
list[str],
Optional[list[str]],
Option(
help=(
"Properties to calculate. If not specified, 'energy', 'forces' "
Expand All @@ -50,7 +50,7 @@ def singlepoint(
),
] = None,
out: Annotated[
Path,
Optional[Path],
Option(
help=(
"Path to save structure with calculated results. Default is inferred "
Expand Down Expand Up @@ -94,10 +94,10 @@ def singlepoint(
Keyword arguments to pass to ase.io.write when saving results. Default is {}.
log : Optional[Path]
Path to write logs to. Default is inferred from the name of the structure file.
summary : Path
summary : Optional[Path]
Path to save summary of inputs, start/end time, and carbon emissions. Default
is inferred from the name of the structure file.
config : Path
config : Optional[Path]
Path to yaml configuration file to define the above options. Default is None.
"""
# Check options from configuration file are all valid
Expand Down
2 changes: 1 addition & 1 deletion janus_core/cli/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def train(
Whether to fine-tune a foundational model. Default is False.
log : Optional[Path]
Path to write logs to. Default is Path("train-log.yml").
summary : Path
summary : Optional[Path]
Path to save summary of inputs, start/end time, and carbon emissions. Default
is Path("train-summary.yml").
"""
Expand Down
39 changes: 21 additions & 18 deletions janus_core/cli/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import ast
from pathlib import Path
from typing import Annotated, Union
from typing import Annotated, Optional, Union

from typer import Option

Expand Down Expand Up @@ -63,40 +63,44 @@ def __str__(self):

StructPath = Annotated[Path, Option(help="Path of structure to simulate.")]

Architecture = Annotated[str, Option(help="MLIP architecture to use for calculations.")]
Device = Annotated[str, Option(help="Device to run calculations on.")]
ModelPath = Annotated[str, Option(help="Path to MLIP model. [default: None]")]
Architecture = Annotated[
Optional[str], Option(help="MLIP architecture to use for calculations.")
]
Device = Annotated[Optional[str], Option(help="Device to run calculations on.")]
ModelPath = Annotated[Optional[str], Option(help="Path to MLIP model.")]

ReadKwargsAll = Annotated[
TyperDict,
Optional[TyperDict],
Option(
parser=parse_dict_class,
help=(
"""
Keyword arguments to pass to ase.io.read. Must be passed as a dictionary
wrapped in quotes, e.g. "{'key' : value}". [default: "{'index': ':'}"]
wrapped in quotes, e.g. "{'key' : value}". By default,
read_kwargs['index'] = ':', so all structures are read.
"""
),
metavar="DICT",
),
]

ReadKwargsLast = Annotated[
TyperDict,
Optional[TyperDict],
Option(
parser=parse_dict_class,
help=(
"""
Keyword arguments to pass to ase.io.read. Must be passed as a dictionary
wrapped in quotes, e.g. "{'key' : value}". [default: "{'index': -1}"]
wrapped in quotes, e.g. "{'key' : value}". By default,
read_kwargs['index'] = -1, so only the last structure is read.
"""
),
metavar="DICT",
),
]

CalcKwargs = Annotated[
TyperDict,
Optional[TyperDict],
Option(
parser=parse_dict_class,
help=(
Expand All @@ -111,36 +115,35 @@ def __str__(self):
]

WriteKwargs = Annotated[
TyperDict,
Optional[TyperDict],
Option(
parser=parse_dict_class,
help=(
"""
Keyword arguments to pass to ase.io.write when saving results. Must be
passed as a dictionary wrapped in quotes, e.g. "{'key' : value}".
[default: "{}"]
"""
),
metavar="DICT",
),
]

OptKwargs = Annotated[
TyperDict,
Optional[TyperDict],
Option(
parser=parse_dict_class,
help=(
"""
Keyword arguments to pass to optimizer. Must be passed as a dictionary
wrapped in quotes, e.g. "{'key' : value}". [default: "{}"]
wrapped in quotes, e.g. "{'key' : value}".
"""
),
metavar="DICT",
),
]

MinimizeKwargs = Annotated[
TyperDict,
Optional[TyperDict],
Option(
parser=parse_dict_class,
help=(
Expand All @@ -154,7 +157,7 @@ def __str__(self):
]

EnsembleKwargs = Annotated[
TyperDict,
Optional[TyperDict],
Option(
parser=parse_dict_class,
help=(
Expand All @@ -168,7 +171,7 @@ def __str__(self):
]

PostProcessKwargs = Annotated[
TyperDict,
Optional[TyperDict],
Option(
parser=parse_dict_class,
help=(
Expand All @@ -182,7 +185,7 @@ def __str__(self):
]

LogPath = Annotated[
Path,
Optional[Path],
Option(
help=(
"Path to save logs to. Default is inferred from the name of the structure "
Expand All @@ -192,7 +195,7 @@ def __str__(self):
]

Summary = Annotated[
Path,
Optional[Path],
Option(
help=(
"Path to save summary of inputs, start/end time, and carbon emissions. "
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ seekpath = "^1.9.7"
spglib = "^2.3.0"
torch = ">= 2.1, <= 2.2" # Range required for dgl
torch-dftd = "0.4.0"
typer = "^0.9.0"
typer = "^0.12.5"
typer-config = "^1.4.0"

alignn = { version = "2024.5.27", optional = true }
Expand Down

0 comments on commit 1861b04

Please sign in to comment.