Skip to content

Commit 2fc3ddd

Browse files
merged updates from wavelength
2 parents 87628f9 + 0e2ab8c commit 2fc3ddd

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

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

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

@@ -73,7 +73,8 @@ def get_args():
7373
def main():
7474
args = get_args()
7575
args = load_user_metadata(args)
76-
wavelength = WAVELENGTHS[args.anode_type]
76+
args.wavelength = set_wavelength(args)
77+
7778
filepath = Path(args.input_file)
7879
outfilestem = filepath.stem + "_corrected"
7980
corrfilestem = filepath.stem + "_cve"
@@ -91,7 +92,7 @@ def main():
9192
f"exists. Please rerun specifying -f if you want to overwrite it"
9293
)
9394

94-
input_pattern = Diffraction_object(wavelength=wavelength)
95+
input_pattern = Diffraction_object(wavelength=args.wavelength)
9596
xarray, yarray = loadData(args.input_file, unpack=True)
9697
input_pattern.insert_scattering_quantity(
9798
xarray,
@@ -102,7 +103,7 @@ def main():
102103
metadata={"muD": args.mud, "anode_type": args.anode_type},
103104
)
104105

105-
absorption_correction = compute_cve(input_pattern, args.mud, wavelength)
106+
absorption_correction = compute_cve(input_pattern, args.mud, args.wavelength)
106107
corrected_data = apply_corr(input_pattern, absorption_correction)
107108
corrected_data.name = f"Absorption corrected input_data: {input_pattern.name}"
108109
corrected_data.dump(f"{outfile}", xtype="tth")

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import argparse
12
from argparse import ArgumentParser
23

34
import pytest
45

5-
from diffpy.labpdfproc.tools import load_user_metadata
6+
from diffpy.labpdfproc.tools import load_user_metadata, set_wavelength
67

78
params5 = [
89
([[]], []),
@@ -25,3 +26,33 @@ def test_load_user_metadata(inputs, expected):
2526
for expected_pair in expected:
2627
setattr(expected_args, expected_pair[0], expected_pair[1])
2728
assert actual_args == expected_args
29+
30+
31+
params2 = [
32+
([None, None], [0.71]),
33+
([None, "Ag"], [0.59]),
34+
([0.25, "Ag"], [0.25]),
35+
([0.25, None], [0.25]),
36+
]
37+
38+
39+
@pytest.mark.parametrize("inputs, expected", params2)
40+
def test_set_wavelength(inputs, expected):
41+
expected_wavelength = expected[0]
42+
actual_args = argparse.Namespace(wavelength=inputs[0], anode_type=inputs[1])
43+
actual_wavelength = set_wavelength(actual_args)
44+
assert actual_wavelength == expected_wavelength
45+
46+
47+
params3 = [
48+
([None, "invalid"]),
49+
([0, None]),
50+
([-1, "Mo"]),
51+
]
52+
53+
54+
@pytest.mark.parametrize("inputs", params3)
55+
def test_set_wavelength_bad(inputs):
56+
with pytest.raises(ValueError):
57+
actual_args = argparse.Namespace(wavelength=inputs[0], anode_type=inputs[1])
58+
actual_args.wavelength = set_wavelength(actual_args)

src/diffpy/labpdfproc/tools.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
WAVELENGTHS = {"Mo": 0.71, "Ag": 0.59, "Cu": 1.54}
2+
known_sources = [key for key in WAVELENGTHS.keys()]
3+
4+
15
def load_key_value_pair(s):
26
items = s.split("=")
37
key = items[0].strip()
@@ -29,3 +33,37 @@ def load_user_metadata(args):
2933
# question 2: more than one =?
3034
# if i have key is hello, value is hello=world, then i can have hello=hello=world to process okay
3135
# if i have = in key then it's still processing as value => would this be an issue?
36+
37+
38+
def set_wavelength(args):
39+
"""
40+
Set the wavelength based on the given input arguments
41+
42+
Parameters
43+
----------
44+
args argparse.Namespace
45+
the arguments from the parser
46+
47+
Returns
48+
-------
49+
float: the wavelength value
50+
51+
we raise an ValueError if the input wavelength is non-positive
52+
or if the input anode_type is not one of the known sources
53+
54+
"""
55+
if args.wavelength is not None and args.wavelength <= 0:
56+
raise ValueError("Please rerun the program specifying a positive float number.")
57+
if not args.wavelength and args.anode_type and args.anode_type not in WAVELENGTHS:
58+
raise ValueError(
59+
f"Invalid anode type {args.anode_type}. "
60+
f"Please rerun the program to either specify a wavelength as a positive float number "
61+
f"or specify anode_type as one of {known_sources}."
62+
)
63+
64+
if args.wavelength:
65+
return args.wavelength
66+
elif args.anode_type:
67+
return WAVELENGTHS[args.anode_type]
68+
else:
69+
return WAVELENGTHS["Mo"]

0 commit comments

Comments
 (0)