Skip to content

Commit d61ee4e

Browse files
authored
Merge pull request #196 from yucongalicechen/applymud-cli
feat: add ``applymud`` subcommand
2 parents c37b593 + 928cdc6 commit d61ee4e

File tree

6 files changed

+98
-46
lines changed

6 files changed

+98
-46
lines changed

.github/workflows/matrix-and-codecov-on-merge-to-main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
4545
- name: Install diffpy.labpdfproc and requirements
4646
run: |
47-
conda install --file requirements/test.txt
47+
conda install --file requirements/tests.txt
4848
conda install --file requirements/conda.txt
4949
pip install gooey
5050
python -m pip install . --no-deps

.github/workflows/tests-on-pr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
3535
- name: Install diffpy.labpdfproc and requirements
3636
run: |
37-
conda install --file requirements/test.txt
37+
conda install --file requirements/tests.txt
3838
conda install --file requirements/conda.txt
3939
pip install gooey
4040
python -m pip install . --no-deps

news/applymud.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* new subcommand ``applymud`` to run the original absorption correction process through CLI.
4+
5+
**Changed:**
6+
7+
* GitHub workflows for renamed test file.
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def _define_arguments():
7373
"If the specified directory doesn't exist it will be created."
7474
),
7575
"default": None,
76+
"widget": "DirChooser",
7677
},
7778
{
7879
"name": ["-x", "--xtype"],
@@ -158,7 +159,7 @@ def _define_arguments():
158159
return args
159160

160161

161-
def _add_mud_selection_group(p, is_gui=False):
162+
def _add_mud_selection_group(p, use_gui=False):
162163
"""Current Options:
163164
1. Manually enter muD (`--mud`).
164165
2. Estimate from a z-scan file (`-z` or `--z-scan-file`).
@@ -173,7 +174,7 @@ def _add_mud_selection_group(p, is_gui=False):
173174
"--mud",
174175
type=float,
175176
help="Enter the mu*D value manually.",
176-
**({"widget": "DecimalField"} if is_gui else {}),
177+
**({"widget": "DecimalField"} if use_gui else {}),
177178
)
178179
g.add_argument(
179180
"-z",
@@ -183,7 +184,7 @@ def _add_mud_selection_group(p, is_gui=False):
183184
"Specify the path to the file "
184185
"used to compute the mu*D value."
185186
),
186-
**({"widget": "FileChooser"} if is_gui else {}),
187+
**({"widget": "FileChooser"} if use_gui else {}),
187188
)
188189
g.add_argument(
189190
"-d",
@@ -207,39 +208,50 @@ def _add_mud_selection_group(p, is_gui=False):
207208
return p
208209

209210

210-
def get_args(override_cli_inputs=None):
211-
p = ArgumentParser()
212-
p = _add_mud_selection_group(p, is_gui=False)
211+
def _register_applymud_subparser(subp, use_gui=False):
212+
applymudp = subp.add_parser(
213+
"applymud", help="Apply absorption correction."
214+
)
215+
_add_mud_selection_group(applymudp, use_gui=use_gui)
213216
for arg in _define_arguments():
214-
kwargs = {
215-
key: value
216-
for key, value in arg.items()
217-
if key != "name" and key != "widget"
218-
}
219-
p.add_argument(*arg["name"], **kwargs)
220-
args = p.parse_args(override_cli_inputs)
221-
return args
217+
names = arg["name"]
218+
options = {k: v for k, v in arg.items() if k != "name"}
219+
if not use_gui and "widget" in options:
220+
options.pop("widget")
221+
applymudp.add_argument(*names, **options)
222222

223223

224-
@Gooey(required_cols=1, optional_cols=2, program_name="labpdfproc GUI")
225-
def gooey_parser():
226-
p = GooeyParser()
227-
p = _add_mud_selection_group(p, is_gui=True)
228-
for arg in _define_arguments():
229-
kwargs = {key: value for key, value in arg.items() if key != "name"}
230-
p.add_argument(*arg["name"], **kwargs)
224+
def create_parser(use_gui=False):
225+
p = GooeyParser() if use_gui else ArgumentParser()
226+
subp = p.add_subparsers(title="subcommand", dest="subcommand")
227+
_register_applymud_subparser(subp, use_gui)
228+
return p
229+
230+
231+
@Gooey(
232+
required_cols=1,
233+
optional_cols=1,
234+
show_sidebar=True,
235+
program_name="labpdfproc GUI",
236+
)
237+
def _get_args_gui():
238+
p = create_parser(use_gui=True)
231239
args = p.parse_args()
232240
return args
233241

234242

235-
def main():
236-
args = (
237-
gooey_parser()
238-
if len(sys.argv) == 1 or "--gui" in sys.argv
239-
else get_args()
240-
)
241-
args = preprocessing_args(args)
243+
def _get_args_cli(override_cli_inputs=None):
244+
p = create_parser(use_gui=False)
245+
args = p.parse_args(override_cli_inputs)
246+
return args
247+
242248

249+
def get_args(override_cli_inputs=None, use_gui=False):
250+
return _get_args_gui() if use_gui else _get_args_cli(override_cli_inputs)
251+
252+
253+
def applymud(args):
254+
args = preprocessing_args(args)
243255
for filepath in args.input_paths:
244256
outfilestem = filepath.stem + "_corrected"
245257
corrfilestem = filepath.stem + "_cve"
@@ -286,5 +298,18 @@ def main():
286298
absorption_correction.dump(f"{corrfile}", xtype=args.xtype)
287299

288300

301+
def run_subcommand(args):
302+
if args.subcommand == "applymud":
303+
return applymud(args)
304+
else:
305+
raise ValueError(f"Unknown subcommand: {args.subcommand}")
306+
307+
308+
def main():
309+
use_gui = len(sys.argv) == 1 or "--gui" in sys.argv
310+
args = get_args(use_gui=use_gui)
311+
return run_subcommand(args)
312+
313+
289314
if __name__ == "__main__":
290315
main()

src/diffpy/labpdfproc/tools.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"wavelength",
4646
"theoretical_from_density",
4747
"theoretical_from_packing",
48+
"subcommand",
4849
]
4950

5051

tests/test_tools.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def test_set_input_lists(inputs, expected, user_filesystem):
115115
base_dir.resolve() / expected_path for expected_path in expected
116116
]
117117

118-
cli_inputs = inputs + ["--mud", "2.5"]
118+
cli_inputs = ["applymud"] + inputs + ["--mud", "2.5"]
119119
actual_args = get_args(cli_inputs)
120120
actual_args = set_input_lists(actual_args)
121121
assert sorted(actual_args.input_paths) == sorted(expected_paths)
@@ -161,7 +161,7 @@ def test_set_input_lists(inputs, expected, user_filesystem):
161161
def test_set_input_files_bad(inputs, expected_error_msg, user_filesystem):
162162
base_dir = Path(user_filesystem)
163163
os.chdir(base_dir)
164-
cli_inputs = inputs + ["--mud", "2.5"]
164+
cli_inputs = ["applymud"] + inputs + ["--mud", "2.5"]
165165
actual_args = get_args(cli_inputs)
166166
with pytest.raises(FileNotFoundError, match=re.escape(expected_error_msg)):
167167
actual_args = set_input_lists(actual_args)
@@ -179,7 +179,7 @@ def test_set_input_files_bad(inputs, expected_error_msg, user_filesystem):
179179
def test_set_output_directory(inputs, expected, user_filesystem):
180180
os.chdir(user_filesystem)
181181
expected_output_directory = Path(user_filesystem) / expected[0]
182-
cli_inputs = ["data.xy", "--mud", "2.5"] + inputs
182+
cli_inputs = ["applymud", "data.xy", "--mud", "2.5"] + inputs
183183
actual_args = get_args(cli_inputs)
184184
actual_args = set_output_directory(actual_args)
185185
assert actual_args.output_directory == expected_output_directory
@@ -190,6 +190,7 @@ def test_set_output_directory(inputs, expected, user_filesystem):
190190
def test_set_output_directory_bad(user_filesystem):
191191
os.chdir(user_filesystem)
192192
cli_inputs = [
193+
"applymud",
193194
"data.xy",
194195
"--mud",
195196
"2.5",
@@ -235,7 +236,7 @@ def test_load_wavelength_from_config_file_with_home_conf_file(
235236
mocker.patch("pathlib.Path.home", lambda _: home_dir)
236237
os.chdir(cwd)
237238

238-
cli_inputs = ["data.xy", "--mud", "2.5"] + inputs
239+
cli_inputs = ["applymud", "data.xy", "--mud", "2.5"] + inputs
239240
actual_args = get_args(cli_inputs)
240241
actual_args = load_wavelength_from_config_file(actual_args)
241242
assert actual_args.wavelength == expected["wavelength"]
@@ -278,7 +279,7 @@ def test_load_wavelength_from_config_file_with_local_conf_file(
278279
with open(cwd / "diffpyconfig.json", "w") as f:
279280
json.dump(local_config_data, f)
280281

281-
cli_inputs = ["data.xy", "--mud", "2.5"] + inputs
282+
cli_inputs = ["applymud", "data.xy", "--mud", "2.5"] + inputs
282283
actual_args = get_args(cli_inputs)
283284
actual_args = load_wavelength_from_config_file(actual_args)
284285
assert actual_args.wavelength == expected["wavelength"]
@@ -321,7 +322,7 @@ def test_load_wavelength_from_config_file_without_conf_files(
321322
confile = home_dir / "diffpyconfig.json"
322323
os.remove(confile)
323324

324-
cli_inputs = ["data.xy", "--mud", "2.5"] + inputs
325+
cli_inputs = ["applymud", "data.xy", "--mud", "2.5"] + inputs
325326
actual_args = get_args(cli_inputs)
326327
actual_args = load_wavelength_from_config_file(actual_args)
327328
assert actual_args.wavelength == expected["wavelength"]
@@ -380,7 +381,7 @@ def test_load_wavelength_from_config_file_without_conf_files(
380381
],
381382
)
382383
def test_set_wavelength(inputs, expected):
383-
cli_inputs = ["data.xy", "--mud", "2.5"] + inputs
384+
cli_inputs = ["applymud", "data.xy", "--mud", "2.5"] + inputs
384385
actual_args = get_args(cli_inputs)
385386
actual_args = set_wavelength(actual_args)
386387
assert actual_args.wavelength == expected["wavelength"]
@@ -419,7 +420,7 @@ def test_set_wavelength(inputs, expected):
419420
],
420421
)
421422
def test_set_wavelength_bad(inputs, expected_error_msg):
422-
cli_inputs = ["data.xy", "--mud", "2.5"] + inputs
423+
cli_inputs = ["applymud", "data.xy", "--mud", "2.5"] + inputs
423424
actual_args = get_args(cli_inputs)
424425
with pytest.raises(ValueError, match=re.escape(expected_error_msg)):
425426
actual_args = set_wavelength(actual_args)
@@ -435,14 +436,14 @@ def test_set_wavelength_bad(inputs, expected_error_msg):
435436
],
436437
)
437438
def test_set_xtype(inputs, expected_xtype):
438-
cli_inputs = ["data.xy", "--mud", "2.5"] + inputs
439+
cli_inputs = ["applymud", "data.xy", "--mud", "2.5"] + inputs
439440
actual_args = get_args(cli_inputs)
440441
actual_args = set_xtype(actual_args)
441442
assert actual_args.xtype == expected_xtype
442443

443444

444445
def test_set_xtype_bad():
445-
cli_inputs = ["data.xy", "--mud", "2.5", "--xtype", "invalid"]
446+
cli_inputs = ["applymud", "data.xy", "--mud", "2.5", "--xtype", "invalid"]
446447
actual_args = get_args(cli_inputs)
447448
with pytest.raises(
448449
ValueError,
@@ -474,7 +475,7 @@ def test_set_xtype_bad():
474475
def test_set_mud(user_filesystem, inputs, expected_mud):
475476
cwd = Path(user_filesystem)
476477
os.chdir(cwd)
477-
cli_inputs = ["data.xy"] + inputs
478+
cli_inputs = ["applymud", "data.xy"] + inputs
478479
actual_args = get_args(cli_inputs)
479480
actual_args = set_mud(actual_args)
480481
assert actual_args.mud == pytest.approx(expected_mud, rel=1e-4, abs=0.1)
@@ -550,7 +551,7 @@ def test_set_mud_bad(user_filesystem, inputs, expected):
550551
expected_error, expected_error_msg = expected
551552
cwd = Path(user_filesystem)
552553
os.chdir(cwd)
553-
cli_inputs = ["data.xy"] + inputs
554+
cli_inputs = ["applymud", "data.xy"] + inputs
554555
actual_args = get_args(cli_inputs)
555556
with pytest.raises(expected_error, match=re.escape(expected_error_msg)):
556557
actual_args = set_mud(actual_args)
@@ -577,12 +578,12 @@ def test_set_mud_bad(user_filesystem, inputs, expected):
577578
],
578579
)
579580
def test_load_user_metadata(inputs, expected):
580-
expected_args = get_args(["data.xy", "--mud", "2.5"])
581+
expected_args = get_args(["applymud", "data.xy", "--mud", "2.5"])
581582
for expected_pair in expected:
582583
setattr(expected_args, expected_pair[0], expected_pair[1])
583584
delattr(expected_args, "user_metadata")
584585

585-
cli_inputs = ["data.xy", "--mud", "2.5"] + inputs
586+
cli_inputs = ["applymud", "data.xy", "--mud", "2.5"] + inputs
586587
actual_args = get_args(cli_inputs)
587588
actual_args = load_user_metadata(actual_args)
588589
assert actual_args == expected_args
@@ -618,7 +619,7 @@ def test_load_user_metadata(inputs, expected):
618619
],
619620
)
620621
def test_load_user_metadata_bad(inputs, expected_error_msg):
621-
cli_inputs = ["data.xy", "--mud", "2.5"] + inputs
622+
cli_inputs = ["applymud", "data.xy", "--mud", "2.5"] + inputs
622623
actual_args = get_args(cli_inputs)
623624
with pytest.raises(ValueError, match=re.escape(expected_error_msg)):
624625
actual_args = load_user_metadata(actual_args)
@@ -681,6 +682,7 @@ def test_load_user_info(monkeypatch, inputs, expected, user_filesystem):
681682
os.chdir(cwd)
682683

683684
cli_inputs = [
685+
"applymud",
684686
"data.xy",
685687
"--mud",
686688
"2.5",
@@ -705,7 +707,7 @@ def test_load_package_info(mocker):
705707
"3.3.0" if package_name == "diffpy.utils" else "1.2.3"
706708
),
707709
)
708-
cli_inputs = ["data.xy", "--mud", "2.5"]
710+
cli_inputs = ["applymud", "data.xy", "--mud", "2.5"]
709711
actual_args = get_args(cli_inputs)
710712
actual_args = load_package_info(actual_args)
711713
assert actual_args.package_info == {
@@ -731,6 +733,7 @@ def test_load_metadata(mocker, user_filesystem):
731733
),
732734
)
733735
cli_inputs = [
736+
"applymud",
734737
".",
735738
"--mud",
736739
"2.5",

0 commit comments

Comments
 (0)