Skip to content

Commit e4e06ad

Browse files
authored
Merge pull request #160 from daavid00/dev
Small fixes for sh docs and cmdargs
2 parents c2fe1c9 + 19b27b2 commit e4e06ad

4 files changed

Lines changed: 86 additions & 79 deletions

File tree

src/pyopmspe11/core/pyopmspe11.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ def main(argv: list[str] | None = None) -> None:
2020
args = load_parser(argv)
2121
check_cmdargs(args)
2222

23-
if args["compare"]:
23+
if args.compare:
2424
print("\nCompare: Generating common plots to compare results, please wait.")
25-
plot_results({"compare": args["compare"]})
25+
plot_results({"compare": args.compare})
2626
print(f"\nThe figures have been written to {os.getcwd()}/compare/")
2727
return
2828

@@ -60,7 +60,7 @@ def make_dir(path: str) -> None:
6060
subprocess.run(["mkdir", "-p", path], check=True)
6161

6262

63-
def load_parser(argv: list[str] | None) -> dict:
63+
def load_parser(argv: list[str] | None) -> argparse.Namespace:
6464
"""CLI arguments"""
6565
parser = argparse.ArgumentParser(
6666
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
@@ -167,10 +167,10 @@ def load_parser(argv: list[str] | None) -> dict:
167167
default="",
168168
help="Region to model (the default '' means the whole system)",
169169
)
170-
return vars(parser.parse_known_args(argv)[0])
170+
return parser.parse_args(argv)
171171

172172

173-
def check_cmdargs(cmdargs: dict[str, str]) -> None:
173+
def check_cmdargs(cmdargs: argparse.Namespace) -> None:
174174
"""Validate command-line arguments and incompatible operations.
175175
176176
The checks cover configuration and output names, spatial resolution,
@@ -187,7 +187,7 @@ def check_cmdargs(cmdargs: dict[str, str]) -> None:
187187
SystemExit
188188
If an argument is invalid or an incompatible combination is requested.
189189
"""
190-
input_file = cmdargs["input"]
190+
input_file = cmdargs.input
191191
if not input_file:
192192
print("\nInvalid value for '-i', the input file cannot be empty.\n")
193193
raise SystemExit(1)
@@ -197,10 +197,10 @@ def check_cmdargs(cmdargs: dict[str, str]) -> None:
197197
"valid extensions are .toml or .txt.\n"
198198
)
199199
raise SystemExit(1)
200-
if not cmdargs["output"]:
200+
if not cmdargs.output:
201201
print("\nInvalid value for '-o', the output folder cannot be empty.\n")
202202
raise SystemExit(1)
203-
resolution = cmdargs["resolution"]
203+
resolution = cmdargs.resolution
204204
try:
205205
resolution_values = [int(value.strip()) for value in resolution.split(",")]
206206
except ValueError:
@@ -211,7 +211,7 @@ def check_cmdargs(cmdargs: dict[str, str]) -> None:
211211
"integers separated by commas, e.g., '-r 8,1,5'.\n"
212212
)
213213
raise SystemExit(1)
214-
time = cmdargs["time"]
214+
time = cmdargs.time
215215
try:
216216
time_values = [float(value.strip()) for value in time.split(",")]
217217
except ValueError:
@@ -222,15 +222,15 @@ def check_cmdargs(cmdargs: dict[str, str]) -> None:
222222
"separated by commas.\n"
223223
)
224224
raise SystemExit(1)
225-
write = cmdargs["write"]
225+
write = cmdargs.write
226226
try:
227227
write_value = float(write)
228228
except ValueError:
229229
write_value = 0
230230
if write_value <= 0:
231231
print(f"\nInvalid value '-w {write}', expected a positive number.\n")
232232
raise SystemExit(1)
233-
mode = cmdargs["mode"]
233+
mode = cmdargs.mode
234234
has_data = mode == "all" or "data" in mode
235235
data_options = {
236236
"-g": ("generate", "performance_sparse"),
@@ -242,7 +242,7 @@ def check_cmdargs(cmdargs: dict[str, str]) -> None:
242242
invalid_options = [
243243
option
244244
for option, (name, default) in data_options.items()
245-
if cmdargs[name] != default
245+
if getattr(cmdargs, name) != default
246246
]
247247
if invalid_options:
248248
print(
@@ -251,7 +251,7 @@ def check_cmdargs(cmdargs: dict[str, str]) -> None:
251251
"data.\n"
252252
)
253253
raise SystemExit(1)
254-
compare = cmdargs["compare"]
254+
compare = cmdargs.compare
255255
if compare:
256256
compare_options = {
257257
"-i": ("input", "input.toml"),
@@ -267,7 +267,7 @@ def check_cmdargs(cmdargs: dict[str, str]) -> None:
267267
invalid_options = [
268268
option
269269
for option, (name, default) in compare_options.items()
270-
if cmdargs[name] != default
270+
if getattr(cmdargs, name) != default
271271
]
272272
if invalid_options:
273273
print(

src/pyopmspe11/utils/inputvalues.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
"""Utility functions to set required input values for pyopmspe11."""
66

7+
import argparse
78
import csv
89
import os
910
import subprocess
@@ -18,7 +19,7 @@
1819
from pyopmspe11.config.config import Config
1920

2021

21-
def process_input(cli: dict) -> Config:
22+
def process_input(cli: argparse.Namespace) -> Config:
2223
"""Process configuration input.
2324
2425
The function constructs a ``Config`` object from CLI and file input, then
@@ -40,29 +41,31 @@ def process_input(cli: dict) -> Config:
4041
+ "the simulations. Please see the configuration files in the examples and "
4142
+ "online documentation, and update your configuration file accordingly.\n"
4243
)
43-
if cli["input"].endswith(".toml"):
44-
with open(cli["input"], "rb") as f:
45-
cfg_file = tomllib.load(f)
44+
if cli.input.lower().endswith(".toml"):
45+
with open(cli.input, "rb") as file:
46+
cfg_file = tomllib.load(file)
4647
else:
47-
lines = []
48-
with open(cli["input"], "r", encoding="utf8") as file:
48+
with open(cli.input, "r", encoding="utf8") as file:
4949
lines = list(csv.reader(file, delimiter="#"))
5050
cfg_file = load_config_txt(lines)
5151
cfg = Config(
52-
fol=os.path.abspath(cli["output"]),
53-
generate=cli["generate"],
54-
mode=cli["mode"],
55-
resolution=cli["resolution"],
56-
time_data=cli["time"],
57-
dt_data=float(cli["write"]),
58-
lower=cli["neighbourhood"],
59-
subfolders=cli["subfolders"],
52+
fol=os.path.abspath(cli.output),
53+
generate=cli.generate,
54+
mode=cli.mode,
55+
resolution=cli.resolution,
56+
time_data=cli.time,
57+
dt_data=float(cli.write),
58+
lower=cli.neighbourhood,
59+
subfolders=cli.subfolders,
6060
**cfg_file,
6161
)
6262
time = setcaseproperties(cfg)
6363
postprocesstoml(cfg, time, msg1, msg2)
6464
for value in cfg.flow.split():
65-
if "--enable-tuning" in value and value[16:] in ["true", "True", "1"]:
65+
if value.lower() in {
66+
"--enable-tuning=true",
67+
"--enable-tuning=1",
68+
}:
6669
cfg.tuning = True
6770
break
6871

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1-
files=(
2-
"test_outputs/docs_localized_lower_domain/lower_domain_pvtnum-1-satnum_i,14,k_t5.png"
3-
"test_outputs/docs_via_deck_hello_world/spe11b_performance.png"
4-
"test_outputs/docs_via_deck_hello_world/spe11b_tco2_2Dmaps.png"
5-
"test_outputs/docs_via_deck_hello_world/spe11b_performance_detailed.png"
6-
"test_outputs/docs_via_deck_hello_world/spe11b_time_series_csv.png"
7-
"test_outputs/docs_via_deck_hello_world/spe11b_sparse_data.png"
8-
"test_outputs/docs_via_deck_hello_world/isothermal_sgas_i,1,k_t5.png"
9-
"test_outputs/docs_cp_grids/11_levels_dz_i,1,k_t5.png"
10-
)
1+
files="
2+
test_outputs/docs_localized_lower_domain/lower_domain_pvtnum-1-satnum_i,14,k_t5.png
3+
test_outputs/docs_via_deck_hello_world/spe11b_performance.png
4+
test_outputs/docs_via_deck_hello_world/spe11b_tco2_2Dmaps.png
5+
test_outputs/docs_via_deck_hello_world/spe11b_performance_detailed.png
6+
test_outputs/docs_via_deck_hello_world/spe11b_time_series_csv.png
7+
test_outputs/docs_via_deck_hello_world/spe11b_sparse_data.png
8+
test_outputs/docs_via_deck_hello_world/isothermal_sgas_i,1,k_t5.png
9+
test_outputs/docs_cp_grids/11_levels_dz_i,1,k_t5.png
10+
"
1111

1212
missing_file="test_outputs/missing_docs_files.txt"
1313
missing=0
1414

1515
rm -f "$missing_file"
1616

17-
for f in "${files[@]}"; do
18-
if [[ ! -f "$f" ]]; then
17+
for f in $files; do
18+
if [ ! -f "$f" ]; then
1919
echo "$f" >> "$missing_file"
20-
((missing++))
20+
missing=$((missing + 1))
2121
fi
2222
done
2323

24-
if (( missing == 0 )); then
24+
if [ "$missing" -eq 0 ]; then
2525
echo "All figures and files exist."
26+
return 0
2627
else
2728
echo "$missing figure(s) or file(s) missing."
2829
echo "See $missing_file"
30+
return 1
2931
fi

tests/scripts/paper_convergence.sh

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,56 @@ cd $OUT
88
python3 convergence.py
99
cd ../..
1010

11-
files=(
12-
"test_outputs/paper_convergence/full_p1.png"
13-
"test_outputs/paper_convergence/full_tlinsol.png"
14-
"test_outputs/paper_convergence/full_immB.png"
15-
"test_outputs/paper_convergence/full_p2.png"
16-
"test_outputs/paper_convergence/full_immA.png"
17-
"test_outputs/paper_convergence/full_spatial_map_full_cp0-z40mish-x40m.png"
18-
"test_outputs/paper_convergence/full_mobA_adding_participants.png"
19-
"test_outputs/paper_convergence/full_spatial_map_full_cp2-z10mish-x10m.png"
20-
"test_outputs/paper_convergence/full_spatial_map_adding_participants.png"
21-
"test_outputs/paper_convergence/full_sealTot.png"
22-
"test_outputs/paper_convergence/full_nliter.png"
23-
"test_outputs/paper_convergence/full_nres.png"
24-
"test_outputs/paper_convergence/full_liniter.png"
25-
"test_outputs/paper_convergence/full_sealA.png"
26-
"test_outputs/paper_convergence/full_sealB.png"
27-
"test_outputs/paper_convergence/full_runtime.png"
28-
"test_outputs/paper_convergence/full_spatial_map_full_cp3-z5mish-x5m.png"
29-
"test_outputs/paper_convergence/full_dof.png"
30-
"test_outputs/paper_convergence/full_boundTot.png"
31-
"test_outputs/paper_convergence/full_mobA.png"
32-
"test_outputs/paper_convergence/full_fsteps.png"
33-
"test_outputs/paper_convergence/full_tcpu.png"
34-
"test_outputs/paper_convergence/full_mobB.png"
35-
"test_outputs/paper_convergence/full_spatial_map_full_cp1-z20mish-x20m.png"
36-
"test_outputs/paper_convergence/full_MC.png"
37-
"test_outputs/paper_convergence/full_tstep.png"
38-
"test_outputs/paper_convergence/full_spatial_map_all.png"
39-
"test_outputs/paper_convergence/full_dissB.png"
40-
"test_outputs/paper_convergence/full_mass.png"
41-
"test_outputs/paper_convergence/full_dissA.png"
42-
)
11+
files="
12+
test_outputs/paper_convergence/full_p1.png
13+
test_outputs/paper_convergence/full_tlinsol.png
14+
test_outputs/paper_convergence/full_immB.png
15+
test_outputs/paper_convergence/full_p2.png
16+
test_outputs/paper_convergence/full_immA.png
17+
test_outputs/paper_convergence/full_spatial_map_full_cp0-z40mish-x40m.png
18+
test_outputs/paper_convergence/full_mobA_adding_participants.png
19+
test_outputs/paper_convergence/full_spatial_map_full_cp2-z10mish-x10m.png
20+
test_outputs/paper_convergence/full_spatial_map_adding_participants.png
21+
test_outputs/paper_convergence/full_sealTot.png
22+
test_outputs/paper_convergence/full_nliter.png
23+
test_outputs/paper_convergence/full_nres.png
24+
test_outputs/paper_convergence/full_liniter.png
25+
test_outputs/paper_convergence/full_sealA.png
26+
test_outputs/paper_convergence/full_sealB.png
27+
test_outputs/paper_convergence/full_runtime.png
28+
test_outputs/paper_convergence/full_spatial_map_full_cp3-z5mish-x5m.png
29+
test_outputs/paper_convergence/full_dof.png
30+
test_outputs/paper_convergence/full_boundTot.png
31+
test_outputs/paper_convergence/full_mobA.png
32+
test_outputs/paper_convergence/full_fsteps.png
33+
test_outputs/paper_convergence/full_tcpu.png
34+
test_outputs/paper_convergence/full_mobB.png
35+
test_outputs/paper_convergence/full_spatial_map_full_cp1-z20mish-x20m.png
36+
test_outputs/paper_convergence/full_MC.png
37+
test_outputs/paper_convergence/full_tstep.png
38+
test_outputs/paper_convergence/full_spatial_map_all.png
39+
test_outputs/paper_convergence/full_dissB.png
40+
test_outputs/paper_convergence/full_mass.png
41+
test_outputs/paper_convergence/full_dissA.png
42+
"
4343

4444
missing_file="test_outputs/missing_publication_files.txt"
4545
missing=0
4646

4747
rm -f "$missing_file"
4848

49-
for f in "${files[@]}"; do
50-
if [[ ! -f "$f" ]]; then
49+
for f in $files; do
50+
if [ ! -f "$f" ]; then
5151
echo "$f" >> "$missing_file"
52-
((missing++))
52+
missing=$((missing + 1))
5353
fi
5454
done
5555

56-
if (( missing == 0 )); then
56+
if [ "$missing" -eq 0 ]; then
5757
echo "All figures and files exist."
58+
return 0
5859
else
5960
echo "$missing figure(s) or file(s) missing."
6061
echo "See $missing_file"
62+
return 1
6163
fi

0 commit comments

Comments
 (0)