Skip to content

Commit fcbe6ba

Browse files
authored
Merge pull request #41 from yucongalicechen/output_dir
closes #22: set tmppath for output dir
2 parents 3089e1b + 9c2433e commit fcbe6ba

File tree

3 files changed

+92
-23
lines changed

3 files changed

+92
-23
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,82 +3,85 @@
33
from pathlib import Path
44

55
from diffpy.labpdfproc.functions import apply_corr, compute_cve
6-
from diffpy.labpdfproc.tools import known_sources, set_wavelength
6+
from diffpy.labpdfproc.tools import known_sources, 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 independnt 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 files unless --force is spacified",
59+
help="Outputs will not overwrite existing file unless --force is specified.",
5960
)
6061
args = p.parse_args()
6162
return args
6263

6364

6465
def main():
6566
args = get_args()
67+
args.output_directory = set_output_directory(args)
6668
args.wavelength = set_wavelength(args)
69+
6770
filepath = Path(args.input_file)
6871
outfilestem = filepath.stem + "_corrected"
6972
corrfilestem = filepath.stem + "_cve"
70-
outfile = Path(outfilestem + ".chi")
71-
corrfile = Path(corrfilestem + ".chi")
73+
outfile = args.output_directory / (outfilestem + ".chi")
74+
corrfile = args.output_directory / (corrfilestem + ".chi")
7275

7376
if outfile.exists() and not args.force_overwrite:
7477
sys.exit(
75-
f"output file {str(outfile)} already exists. Please rerun "
76-
f"specifying -f if you want to overwrite it"
78+
f"Output file {str(outfile)} already exists. Please rerun "
79+
f"specifying -f if you want to overwrite it."
7780
)
7881
if corrfile.exists() and args.output_correction and not args.force_overwrite:
7982
sys.exit(
80-
f"corrections file {str(corrfile)} was requested and already "
81-
f"exists. Please rerun specifying -f if you want to overwrite it"
83+
f"Corrections file {str(corrfile)} was requested and already "
84+
f"exists. Please rerun specifying -f if you want to overwrite it."
8285
)
8386

8487
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,9 +1,49 @@
11
import argparse
2+
import os
23
import re
4+
from pathlib import Path
35

46
import pytest
57

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

848
params2 = [
949
([None, None], [0.71]),
@@ -24,10 +64,10 @@ def test_set_wavelength(inputs, expected):
2464
params3 = [
2565
(
2666
[None, "invalid"],
27-
[f"Anode type not recognized. please rerun specifying an anode_type from {*known_sources, }"],
67+
[f"Anode type not recognized. Please rerun specifying an anode_type from {*known_sources, }."],
2868
),
29-
([0, None], ["No valid wavelength. Please rerun specifying a known anode_type or a positive wavelength"]),
30-
([-1, "Mo"], ["No valid wavelength. Please rerun specifying a known anode_type or a positive wavelength"]),
69+
([0, None], ["No valid wavelength. Please rerun specifying a known anode_type or a positive wavelength."]),
70+
([-1, "Mo"], ["No valid wavelength. Please rerun specifying a known anode_type or a positive wavelength."]),
3171
]
3272

3373

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)