Skip to content

Commit bf6a286

Browse files
merge updated main
2 parents 4b60c0c + fcbe6ba commit bf6a286

File tree

3 files changed

+91
-23
lines changed

3 files changed

+91
-23
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,60 @@
33
from pathlib import Path
44

55
from diffpy.labpdfproc.functions import apply_corr, compute_cve
6-
from diffpy.labpdfproc.tools import known_sources, load_user_metadata, set_wavelength
6+
from diffpy.labpdfproc.tools import known_sources, load_user_metadata, set_output_directory, set_wavelength
77
from diffpy.utils.parsers.loaddata import loadData
88
from diffpy.utils.scattering_objects.diffraction_objects import XQUANTITIES, Diffraction_object
99

1010

1111
def get_args():
1212
p = ArgumentParser()
1313
p.add_argument("mud", help="Value of mu*D for your " "sample. Required.", type=float)
14-
p.add_argument("-i", "--input-file", help="The filename of the " "datafile to load")
14+
p.add_argument("-i", "--input-file", help="The filename of the " "datafile to load.")
1515
p.add_argument(
1616
"-a",
1717
"--anode-type",
1818
help=f"The type of the x-ray source. Allowed values are "
19-
f"{*[known_sources], }. Either specify a known x-ray source or specify wavelength",
19+
f"{*[known_sources], }. Either specify a known x-ray source or specify wavelength.",
2020
default="Mo",
2121
)
2222
p.add_argument(
2323
"-w",
2424
"--wavelength",
2525
help="X-ray source wavelength in angstroms. Not needed if the anode-type "
26-
"is specified. This wavelength will override the anode wavelength if both are specified",
26+
"is specified. This wavelength will override the anode wavelength if both are specified.",
2727
default=None,
2828
type=float,
2929
)
3030
p.add_argument(
3131
"-o",
3232
"--output-directory",
33-
help="the name of the output directory. If it doesn't exist it "
34-
"will be created. Not currently implemented",
33+
help="The name of the output directory. If not specified "
34+
"then corrected files will be written to the current directory."
35+
"If the specified directory doesn't exist it will be created.",
3536
default=None,
3637
)
3738
p.add_argument(
3839
"-x",
3940
"--xtype",
40-
help=f"the quantity on the independent variable axis. allowed "
41+
help=f"The quantity on the independent variable axis. Allowed "
4142
f"values: {*XQUANTITIES, }. If not specified then two-theta "
4243
f"is assumed for the independent variable. Only implemented for "
43-
f"tth currently",
44+
f"tth currently.",
4445
default="tth",
4546
)
4647
p.add_argument(
4748
"-c",
4849
"--output-correction",
4950
action="store_true",
50-
help="the absorption correction will be output to a file if this "
51-
"flag is set. Default is that it is not output",
51+
help="The absorption correction will be output to a file if this "
52+
"flag is set. Default is that it is not output.",
5253
default="tth",
5354
)
5455
p.add_argument(
5556
"-f",
5657
"--force-overwrite",
5758
action="store_true",
58-
help="outputs will not overwrite existing file unless --force is specified",
59+
help="Outputs will not overwrite existing file unless --force is specified.",
5960
)
6061
p.add_argument(
6162
"-u",
@@ -73,23 +74,24 @@ def get_args():
7374
def main():
7475
args = get_args()
7576
args = load_user_metadata(args)
77+
args.output_directory = set_output_directory(args)
7678
args.wavelength = set_wavelength(args)
7779

7880
filepath = Path(args.input_file)
7981
outfilestem = filepath.stem + "_corrected"
8082
corrfilestem = filepath.stem + "_cve"
81-
outfile = Path(outfilestem + ".chi")
82-
corrfile = Path(corrfilestem + ".chi")
83+
outfile = args.output_directory / (outfilestem + ".chi")
84+
corrfile = args.output_directory / (corrfilestem + ".chi")
8385

8486
if outfile.exists() and not args.force_overwrite:
8587
sys.exit(
86-
f"output file {str(outfile)} already exists. Please rerun "
87-
f"specifying -f if you want to overwrite it"
88+
f"Output file {str(outfile)} already exists. Please rerun "
89+
f"specifying -f if you want to overwrite it."
8890
)
8991
if corrfile.exists() and args.output_correction and not args.force_overwrite:
9092
sys.exit(
91-
f"corrections file {str(corrfile)} was requested and already "
92-
f"exists. Please rerun specifying -f if you want to overwrite it"
93+
f"Corrections file {str(corrfile)} was requested and already "
94+
f"exists. Please rerun specifying -f if you want to overwrite it."
9395
)
9496

9597
input_pattern = Diffraction_object(wavelength=args.wavelength)

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,50 @@
11
import argparse
2+
import os
23
import re
34
from argparse import ArgumentParser
5+
from pathlib import Path
46

57
import pytest
68

7-
from diffpy.labpdfproc.tools import known_sources, load_user_metadata, set_wavelength
9+
from diffpy.labpdfproc.tools import known_sources, load_user_metadata, set_output_directory, set_wavelength
10+
11+
params1 = [
12+
([None], ["."]),
13+
(["."], ["."]),
14+
(["new_dir"], ["new_dir"]),
15+
(["existing_dir"], ["existing_dir"]),
16+
]
17+
18+
19+
@pytest.mark.parametrize("inputs, expected", params1)
20+
def test_set_output_directory(inputs, expected, tmp_path):
21+
directory = Path(tmp_path)
22+
os.chdir(directory)
23+
24+
existing_dir = Path(tmp_path).resolve() / "existing_dir"
25+
existing_dir.mkdir(parents=True, exist_ok=True)
26+
27+
expected_output_directory = Path(tmp_path).resolve() / expected[0]
28+
actual_args = argparse.Namespace(output_directory=inputs[0])
29+
actual_args.output_directory = set_output_directory(actual_args)
30+
assert actual_args.output_directory == expected_output_directory
31+
assert Path(actual_args.output_directory).exists()
32+
assert Path(actual_args.output_directory).is_dir()
33+
34+
35+
def test_set_output_directory_bad(tmp_path):
36+
directory = Path(tmp_path)
37+
os.chdir(directory)
38+
39+
existing_file = Path(tmp_path).resolve() / "existing_file.py"
40+
existing_file.touch()
41+
42+
actual_args = argparse.Namespace(output_directory="existing_file.py")
43+
with pytest.raises(FileExistsError):
44+
actual_args.output_directory = set_output_directory(actual_args)
45+
assert Path(actual_args.output_directory).exists()
46+
assert not Path(actual_args.output_directory).is_dir()
47+
848

949
params2 = [
1050
([None, None], [0.71]),
@@ -25,10 +65,10 @@ def test_set_wavelength(inputs, expected):
2565
params3 = [
2666
(
2767
[None, "invalid"],
28-
[f"Anode type not recognized. please rerun specifying an anode_type from {*known_sources, }"],
68+
[f"Anode type not recognized. Please rerun specifying an anode_type from {*known_sources, }."],
2969
),
30-
([0, None], ["No valid wavelength. Please rerun specifying a known anode_type or a positive wavelength"]),
31-
([-1, "Mo"], ["No valid wavelength. Please rerun specifying a known anode_type or a positive wavelength"]),
70+
([0, None], ["No valid wavelength. Please rerun specifying a known anode_type or a positive wavelength."]),
71+
([-1, "Mo"], ["No valid wavelength. Please rerun specifying a known anode_type or a positive wavelength."]),
3272
]
3373

3474

src/diffpy/labpdfproc/tools.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
1+
from pathlib import Path
2+
13
WAVELENGTHS = {"Mo": 0.71, "Ag": 0.59, "Cu": 1.54}
24
known_sources = [key for key in WAVELENGTHS.keys()]
35

46

7+
def set_output_directory(args):
8+
"""
9+
set the output directory based on the given input arguments
10+
11+
Parameters
12+
----------
13+
args argparse.Namespace
14+
the arguments from the parser
15+
16+
Returns
17+
-------
18+
pathlib.PosixPath that contains the full path of the output directory
19+
20+
it is determined as follows:
21+
If user provides an output directory, use it.
22+
Otherwise, we set it to the current directory if nothing is provided.
23+
We then create the directory if it does not exist.
24+
25+
"""
26+
output_dir = Path(args.output_directory).resolve() if args.output_directory else Path.cwd().resolve()
27+
output_dir.mkdir(parents=True, exist_ok=True)
28+
return output_dir
29+
30+
531
def set_wavelength(args):
632
"""
733
Set the wavelength based on the given input arguments
@@ -21,11 +47,11 @@ def set_wavelength(args):
2147
"""
2248
if args.wavelength is not None and args.wavelength <= 0:
2349
raise ValueError(
24-
"No valid wavelength. Please rerun specifying a known anode_type or a positive wavelength"
50+
"No valid wavelength. Please rerun specifying a known anode_type or a positive wavelength."
2551
)
2652
if not args.wavelength and args.anode_type and args.anode_type not in WAVELENGTHS:
2753
raise ValueError(
28-
f"Anode type not recognized. please rerun specifying an anode_type from {*known_sources, }"
54+
f"Anode type not recognized. Please rerun specifying an anode_type from {*known_sources, }."
2955
)
3056

3157
if args.wavelength:

0 commit comments

Comments
 (0)