Skip to content

Commit 0e2ab8c

Browse files
authored
Merge pull request #36 from yucongalicechen/wavelength
closes #25: overload wavelength
2 parents 842ea89 + 2a3c567 commit 0e2ab8c

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pathlib import Path
44

55
from diffpy.labpdfproc.functions import apply_corr, compute_cve
6+
from diffpy.labpdfproc.tools import set_wavelength
67
from diffpy.utils.parsers.loaddata import loadData
78
from diffpy.utils.scattering_objects.diffraction_objects import XQUANTITIES, Diffraction_object
89

@@ -62,7 +63,7 @@ def get_args():
6263

6364
def main():
6465
args = get_args()
65-
wavelength = WAVELENGTHS[args.anode_type]
66+
args.wavelength = set_wavelength(args)
6667
filepath = Path(args.input_file)
6768
outfilestem = filepath.stem + "_corrected"
6869
corrfilestem = filepath.stem + "_cve"
@@ -80,7 +81,7 @@ def main():
8081
f"exists. Please rerun specifying -f if you want to overwrite it"
8182
)
8283

83-
input_pattern = Diffraction_object(wavelength=wavelength)
84+
input_pattern = Diffraction_object(wavelength=args.wavelength)
8485
xarray, yarray = loadData(args.input_file, unpack=True)
8586
input_pattern.insert_scattering_quantity(
8687
xarray,
@@ -91,7 +92,7 @@ def main():
9192
metadata={"muD": args.mud, "anode_type": args.anode_type},
9293
)
9394

94-
absorption_correction = compute_cve(input_pattern, args.mud, wavelength)
95+
absorption_correction = compute_cve(input_pattern, args.mud, args.wavelength)
9596
corrected_data = apply_corr(input_pattern, absorption_correction)
9697
corrected_data.name = f"Absorption corrected input_data: {input_pattern.name}"
9798
corrected_data.dump(f"{outfile}", xtype="tth")
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import argparse
2+
3+
import pytest
4+
5+
from diffpy.labpdfproc.tools import set_wavelength
6+
7+
params2 = [
8+
([None, None], [0.71]),
9+
([None, "Ag"], [0.59]),
10+
([0.25, "Ag"], [0.25]),
11+
([0.25, None], [0.25]),
12+
]
13+
14+
15+
@pytest.mark.parametrize("inputs, expected", params2)
16+
def test_set_wavelength(inputs, expected):
17+
expected_wavelength = expected[0]
18+
actual_args = argparse.Namespace(wavelength=inputs[0], anode_type=inputs[1])
19+
actual_wavelength = set_wavelength(actual_args)
20+
assert actual_wavelength == expected_wavelength
21+
22+
23+
params3 = [
24+
([None, "invalid"]),
25+
([0, None]),
26+
([-1, "Mo"]),
27+
]
28+
29+
30+
@pytest.mark.parametrize("inputs", params3)
31+
def test_set_wavelength_bad(inputs):
32+
with pytest.raises(ValueError):
33+
actual_args = argparse.Namespace(wavelength=inputs[0], anode_type=inputs[1])
34+
actual_args.wavelength = set_wavelength(actual_args)

src/diffpy/labpdfproc/tools.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
WAVELENGTHS = {"Mo": 0.71, "Ag": 0.59, "Cu": 1.54}
2+
known_sources = [key for key in WAVELENGTHS.keys()]
3+
4+
5+
def set_wavelength(args):
6+
"""
7+
Set the wavelength based on the given input arguments
8+
9+
Parameters
10+
----------
11+
args argparse.Namespace
12+
the arguments from the parser
13+
14+
Returns
15+
-------
16+
float: the wavelength value
17+
18+
we raise an ValueError if the input wavelength is non-positive
19+
or if the input anode_type is not one of the known sources
20+
21+
"""
22+
if args.wavelength is not None and args.wavelength <= 0:
23+
raise ValueError("Please rerun the program specifying a positive float number.")
24+
if not args.wavelength and args.anode_type and args.anode_type not in WAVELENGTHS:
25+
raise ValueError(
26+
f"Invalid anode type {args.anode_type}. "
27+
f"Please rerun the program to either specify a wavelength as a positive float number "
28+
f"or specify anode_type as one of {known_sources}."
29+
)
30+
31+
if args.wavelength:
32+
return args.wavelength
33+
elif args.anode_type:
34+
return WAVELENGTHS[args.anode_type]
35+
else:
36+
return WAVELENGTHS["Mo"]

0 commit comments

Comments
 (0)