Skip to content

Commit 651248d

Browse files
authored
Merge pull request diffpy#120 from yucongalicechen/xtype
Implement xtype other than tth
2 parents 79a32e7 + b558f18 commit 651248d

File tree

6 files changed

+102
-25
lines changed

6 files changed

+102
-25
lines changed

news/xtype.rst

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* Support for independent variables other than two-theta.
4+
5+
**Changed:**
6+
7+
* <news item>
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/functions.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pandas as pd
66
from scipy.interpolate import interp1d
77

8-
from diffpy.utils.scattering_objects.diffraction_objects import Diffraction_object
8+
from diffpy.utils.scattering_objects.diffraction_objects import XQUANTITIES, Diffraction_object
99

1010
RADIUS_MM = 1
1111
N_POINTS_ON_DIAMETER = 300
@@ -198,7 +198,6 @@ def _cve_brute_force(diffraction_data, mud):
198198
"tth",
199199
metadata=diffraction_data.metadata,
200200
name=f"absorption correction, cve, for {diffraction_data.name}",
201-
wavelength=diffraction_data.wavelength,
202201
scat_quantity="cve",
203202
)
204203
return cve_do
@@ -227,7 +226,6 @@ def _cve_polynomial_interpolation(diffraction_data, mud):
227226
"tth",
228227
metadata=diffraction_data.metadata,
229228
name=f"absorption correction, cve, for {diffraction_data.name}",
230-
wavelength=diffraction_data.wavelength,
231229
scat_quantity="cve",
232230
)
233231
return cve_do
@@ -246,15 +244,18 @@ def _cve_method(method):
246244
return methods[method]
247245

248246

249-
def compute_cve(diffraction_data, mud, method="polynomial_interpolation"):
247+
def compute_cve(diffraction_data, mud, method="polynomial_interpolation", xtype="tth"):
250248
f"""
251249
compute and interpolate the cve for the given diffraction data and mud using the selected method
250+
252251
Parameters
253252
----------
254253
diffraction_data Diffraction_object
255254
the diffraction pattern
256255
mud float
257256
the mu*D of the diffraction object, where D is the diameter of the circle
257+
xtype str
258+
the quantity on the independent variable axis, allowed values are {*XQUANTITIES, }
258259
method str
259260
the method used to calculate cve, must be one of {* CVE_METHODS, }
260261
@@ -264,22 +265,20 @@ def compute_cve(diffraction_data, mud, method="polynomial_interpolation"):
264265
"""
265266

266267
cve_function = _cve_method(method)
267-
abdo_on_global_tth = cve_function(diffraction_data, mud)
268-
global_tth = abdo_on_global_tth.on_tth[0]
269-
cve_on_global_tth = abdo_on_global_tth.on_tth[1]
270-
orig_grid = diffraction_data.on_tth[0]
271-
newcve = np.interp(orig_grid, global_tth, cve_on_global_tth)
268+
cve_do_on_global_grid = cve_function(diffraction_data, mud)
269+
orig_grid = diffraction_data.on_xtype(xtype)[0]
270+
global_xtype = cve_do_on_global_grid.on_xtype(xtype)[0]
271+
cve_on_global_xtype = cve_do_on_global_grid.on_xtype(xtype)[1]
272+
newcve = np.interp(orig_grid, global_xtype, cve_on_global_xtype)
272273
cve_do = Diffraction_object(wavelength=diffraction_data.wavelength)
273274
cve_do.insert_scattering_quantity(
274275
orig_grid,
275276
newcve,
276-
"tth",
277+
xtype,
277278
metadata=diffraction_data.metadata,
278279
name=f"absorption correction, cve, for {diffraction_data.name}",
279-
wavelength=diffraction_data.wavelength,
280280
scat_quantity="cve",
281281
)
282-
283282
return cve_do
284283

285284

src/diffpy/labpdfproc/labpdfprocapp.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ def get_args(override_cli_inputs=None):
6363
help=(
6464
f"The quantity on the independent variable axis. Allowed "
6565
f"values: {*XQUANTITIES, }. If not specified then two-theta "
66-
f"is assumed for the independent variable. Only implemented for "
67-
f"tth currently."
66+
f"is assumed for the independent variable."
6867
),
6968
default="tth",
7069
)
@@ -160,19 +159,19 @@ def main():
160159
input_pattern.insert_scattering_quantity(
161160
xarray,
162161
yarray,
163-
"tth",
162+
args.xtype,
164163
scat_quantity="x-ray",
165164
name=filepath.stem,
166165
metadata=load_metadata(args, filepath),
167166
)
168167

169-
absorption_correction = compute_cve(input_pattern, args.mud, args.method)
168+
absorption_correction = compute_cve(input_pattern, args.mud, args.method, args.xtype)
170169
corrected_data = apply_corr(input_pattern, absorption_correction)
171170
corrected_data.name = f"Absorption corrected input_data: {input_pattern.name}"
172-
corrected_data.dump(f"{outfile}", xtype="tth")
171+
corrected_data.dump(f"{outfile}", xtype=args.xtype)
173172

174173
if args.output_correction:
175-
absorption_correction.dump(f"{corrfile}", xtype="tth")
174+
absorption_correction.dump(f"{corrfile}", xtype=args.xtype)
176175

177176

178177
if __name__ == "__main__":

src/diffpy/labpdfproc/tools.py

+21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from pathlib import Path
33

44
from diffpy.labpdfproc.mud_calculator import compute_mud
5+
from diffpy.utils.scattering_objects.diffraction_objects import QQUANTITIES, XQUANTITIES
56
from diffpy.utils.tools import get_package_info, get_user_info
67

78
WAVELENGTHS = {"Mo": 0.71, "Ag": 0.59, "Cu": 1.54}
@@ -138,6 +139,25 @@ def set_wavelength(args):
138139
return args
139140

140141

142+
def set_xtype(args):
143+
f"""
144+
Set the xtype based on the given input arguments, raise an error if xtype is not one of {*XQUANTITIES, }
145+
146+
Parameters
147+
----------
148+
args argparse.Namespace
149+
the arguments from the parser
150+
151+
Returns
152+
-------
153+
args argparse.Namespace
154+
"""
155+
if args.xtype.lower() not in XQUANTITIES:
156+
raise ValueError(f"Unknown xtype: {args.xtype}. Allowed xtypes are {*XQUANTITIES, }.")
157+
args.xtype = "q" if args.xtype.lower() in QQUANTITIES else "tth"
158+
return args
159+
160+
141161
def set_mud(args):
142162
"""
143163
Set the mud based on the given input arguments
@@ -260,6 +280,7 @@ def preprocessing_args(args):
260280
args = set_input_lists(args)
261281
args.output_directory = set_output_directory(args)
262282
args = set_wavelength(args)
283+
args = set_xtype(args)
263284
args = set_mud(args)
264285
args = load_user_metadata(args)
265286
return args

tests/test_functions.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -58,29 +58,37 @@ def test_set_muls_at_angle(inputs, expected):
5858
assert actual_muls_sorted == pytest.approx(expected_muls_sorted, rel=1e-4, abs=1e-6)
5959

6060

61-
def _instantiate_test_do(xarray, yarray, name="test", scat_quantity="x-ray"):
61+
def _instantiate_test_do(xarray, yarray, xtype="tth", name="test", scat_quantity="x-ray"):
6262
test_do = Diffraction_object(wavelength=1.54)
6363
test_do.insert_scattering_quantity(
6464
xarray,
6565
yarray,
66-
"tth",
66+
xtype,
6767
scat_quantity=scat_quantity,
6868
name=name,
6969
metadata={"thing1": 1, "thing2": "thing2"},
7070
)
7171
return test_do
7272

7373

74-
def test_compute_cve(mocker):
74+
params4 = [
75+
(["tth"], [np.array([90, 90.1, 90.2]), np.array([0.5, 0.5, 0.5]), "tth"]),
76+
(["q"], [np.array([5.76998, 5.77501, 5.78004]), np.array([0.5, 0.5, 0.5]), "q"]),
77+
]
78+
79+
80+
@pytest.mark.parametrize("inputs, expected", params4)
81+
def test_compute_cve(inputs, expected, mocker):
7582
xarray, yarray = np.array([90, 90.1, 90.2]), np.array([2, 2, 2])
7683
expected_cve = np.array([0.5, 0.5, 0.5])
7784
mocker.patch("diffpy.labpdfproc.functions.TTH_GRID", xarray)
7885
mocker.patch("numpy.interp", return_value=expected_cve)
7986
input_pattern = _instantiate_test_do(xarray, yarray)
80-
actual_cve_do = compute_cve(input_pattern, mud=1)
87+
actual_cve_do = compute_cve(input_pattern, mud=1, method="polynomial_interpolation", xtype=inputs[0])
8188
expected_cve_do = _instantiate_test_do(
82-
xarray,
83-
expected_cve,
89+
expected[0],
90+
expected[1],
91+
expected[2],
8492
name="absorption correction, cve, for test",
8593
scat_quantity="cve",
8694
)
@@ -92,7 +100,7 @@ def test_compute_cve(mocker):
92100
[7, "polynomial_interpolation"],
93101
[
94102
f"mu*D is out of the acceptable range (0.5 to 6) for polynomial interpolation. "
95-
f"Please rerun with a value within this range or specifying another method from {* CVE_METHODS, }."
103+
f"Please rerun with a value within this range or specifying another method from {*CVE_METHODS, }."
96104
],
97105
),
98106
([1, "invalid_method"], [f"Unknown method: invalid_method. Allowed methods are {*CVE_METHODS, }."]),

tests/test_tools.py

+27
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
set_mud,
1717
set_output_directory,
1818
set_wavelength,
19+
set_xtype,
1920
)
21+
from diffpy.utils.scattering_objects.diffraction_objects import XQUANTITIES
2022

2123
# Use cases can be found here: https://github.com/diffpy/diffpy.labpdfproc/issues/48
2224

@@ -189,6 +191,31 @@ def test_set_wavelength_bad(inputs, msg):
189191
actual_args = set_wavelength(actual_args)
190192

191193

194+
params4 = [
195+
([], ["tth"]),
196+
(["--xtype", "2theta"], ["tth"]),
197+
(["--xtype", "d"], ["tth"]),
198+
(["--xtype", "q"], ["q"]),
199+
]
200+
201+
202+
@pytest.mark.parametrize("inputs, expected", params4)
203+
def test_set_xtype(inputs, expected):
204+
cli_inputs = ["2.5", "data.xy"] + inputs
205+
actual_args = get_args(cli_inputs)
206+
actual_args = set_xtype(actual_args)
207+
assert actual_args.xtype == expected[0]
208+
209+
210+
def test_set_xtype_bad():
211+
cli_inputs = ["2.5", "data.xy", "--xtype", "invalid"]
212+
actual_args = get_args(cli_inputs)
213+
with pytest.raises(
214+
ValueError, match=re.escape(f"Unknown xtype: invalid. Allowed xtypes are {*XQUANTITIES, }.")
215+
):
216+
actual_args = set_xtype(actual_args)
217+
218+
192219
def test_set_mud(user_filesystem):
193220
cli_inputs = ["2.5", "data.xy"]
194221
actual_args = get_args(cli_inputs)

0 commit comments

Comments
 (0)