Skip to content

Commit 9fa7ce7

Browse files
authored
Merge pull request #225 from bobleesj/warnings
1) Capture no wavelength `UserWarning` for `test_q_to_thh` method 2) discuss passing variables to `@pytest.mark.parametrize`
2 parents 8fe6b99 + 69aa7c7 commit 9fa7ce7

File tree

3 files changed

+72
-59
lines changed

3 files changed

+72
-59
lines changed

Diff for: src/diffpy/utils/transforms.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
import numpy as np
55

66
wavelength_warning_emsg = (
7-
"INFO: no wavelength has been specified. You can continue "
8-
"to use the DiffractionObject but some of its powerful features "
9-
"will not be available. To specify a wavelength, set "
10-
"diffraction_object.wavelength = [number], "
11-
"where diffraction_object is the variable name of you Diffraction Object, "
12-
"and number is the wavelength in angstroms."
7+
"No wavelength has been specified. You can continue to use the DiffractionObject, but "
8+
"some of its powerful features will not be available. "
9+
"To specify a wavelength, if you have do = DiffractionObject(xarray, yarray, 'tth'), "
10+
"you may set do.wavelength = 1.54 for a wavelength of 1.54 angstroms."
1311
)
1412
invalid_tth_emsg = "Two theta exceeds 180 degrees. Please check the input values for errors."
1513
invalid_q_or_d_or_wavelength_emsg = (

Diff for: tests/test_diffraction_objects.py

+21-21
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,11 @@ def test_diffraction_objects_equality(inputs1, inputs2, expected):
164164

165165

166166
def test_on_xtype():
167-
test = DiffractionObject(wavelength=2 * np.pi, xarray=np.array([30, 60]), yarray=np.array([1, 2]), xtype="tth")
168-
assert np.allclose(test.on_xtype("tth"), [np.array([30, 60]), np.array([1, 2])])
169-
assert np.allclose(test.on_xtype("2theta"), [np.array([30, 60]), np.array([1, 2])])
170-
assert np.allclose(test.on_xtype("q"), [np.array([0.51764, 1]), np.array([1, 2])])
171-
assert np.allclose(test.on_xtype("d"), [np.array([12.13818, 6.28319]), np.array([1, 2])])
167+
do = DiffractionObject(wavelength=2 * np.pi, xarray=np.array([30, 60]), yarray=np.array([1, 2]), xtype="tth")
168+
assert np.allclose(do.on_xtype("tth"), [np.array([30, 60]), np.array([1, 2])])
169+
assert np.allclose(do.on_xtype("2theta"), [np.array([30, 60]), np.array([1, 2])])
170+
assert np.allclose(do.on_xtype("q"), [np.array([0.51764, 1]), np.array([1, 2])])
171+
assert np.allclose(do.on_xtype("d"), [np.array([12.13818, 6.28319]), np.array([1, 2])])
172172

173173

174174
def test_init_invalid_xtype():
@@ -184,34 +184,34 @@ def test_init_invalid_xtype():
184184

185185
params_index = [
186186
# UC1: exact match
187-
([4 * np.pi, np.array([30.005, 60]), np.array([1, 2]), "tth", "tth", 30.005], [0]),
187+
(4 * np.pi, np.array([30.005, 60]), np.array([1, 2]), "tth", "tth", 30.005, [0]),
188188
# UC2: target value lies in the array, returns the (first) closest index
189-
([4 * np.pi, np.array([30, 60]), np.array([1, 2]), "tth", "tth", 45], [0]),
190-
([4 * np.pi, np.array([30, 60]), np.array([1, 2]), "tth", "q", 0.25], [0]),
189+
(4 * np.pi, np.array([30, 60]), np.array([1, 2]), "tth", "tth", 45, [0]),
190+
(4 * np.pi, np.array([30, 60]), np.array([1, 2]), "tth", "q", 0.25, [0]),
191191
# UC3: target value out of the range, returns the closest index
192-
([4 * np.pi, np.array([0.25, 0.5, 0.71]), np.array([1, 2, 3]), "q", "q", 0.1], [0]),
193-
([4 * np.pi, np.array([30, 60]), np.array([1, 2]), "tth", "tth", 63], [1]),
192+
(4 * np.pi, np.array([0.25, 0.5, 0.71]), np.array([1, 2, 3]), "q", "q", 0.1, [0]),
193+
(4 * np.pi, np.array([30, 60]), np.array([1, 2]), "tth", "tth", 63, [1]),
194194
]
195195

196196

197-
@pytest.mark.parametrize("inputs, expected", params_index)
198-
def test_get_array_index(inputs, expected):
199-
test = DiffractionObject(wavelength=inputs[0], xarray=inputs[1], yarray=inputs[2], xtype=inputs[3])
200-
actual = test.get_array_index(value=inputs[5], xtype=inputs[4])
201-
assert actual == expected[0]
197+
@pytest.mark.parametrize("wavelength, xarray, yarray, xtype_1, xtype_2, value, expected_index", params_index)
198+
def test_get_array_index(wavelength, xarray, yarray, xtype_1, xtype_2, value, expected_index):
199+
do = DiffractionObject(wavelength=wavelength, xarray=xarray, yarray=yarray, xtype=xtype_1)
200+
actual_index = do.get_array_index(value=value, xtype=xtype_2)
201+
assert actual_index == expected_index
202202

203203

204204
def test_get_array_index_bad():
205-
test = DiffractionObject(wavelength=2 * np.pi, xarray=np.array([]), yarray=np.array([]), xtype="tth")
205+
do = DiffractionObject(wavelength=2 * np.pi, xarray=np.array([]), yarray=np.array([]), xtype="tth")
206206
with pytest.raises(ValueError, match=re.escape("The 'tth' array is empty. Please ensure it is initialized.")):
207-
test.get_array_index(value=30)
207+
do.get_array_index(value=30)
208208

209209

210210
def test_dump(tmp_path, mocker):
211211
x, y = np.linspace(0, 5, 6), np.linspace(0, 5, 6)
212212
directory = Path(tmp_path)
213213
file = directory / "testfile"
214-
test = DiffractionObject(
214+
do = DiffractionObject(
215215
wavelength=1.54,
216216
name="test",
217217
scat_quantity="x-ray",
@@ -222,7 +222,7 @@ def test_dump(tmp_path, mocker):
222222
)
223223
mocker.patch("importlib.metadata.version", return_value="3.3.0")
224224
with freeze_time("2012-01-14"):
225-
test.dump(file, "q")
225+
do.dump(file, "q")
226226
with open(file, "r") as f:
227227
actual = f.read()
228228
expected = (
@@ -360,15 +360,15 @@ def test_all_array_getter():
360360

361361

362362
def test_all_array_setter():
363-
actual_do = DiffractionObject()
363+
do = DiffractionObject()
364364

365365
# Attempt to directly modify the property
366366
with pytest.raises(
367367
AttributeError,
368368
match="Direct modification of attribute 'all_arrays' is not allowed. "
369369
"Please use 'input_data' to modify 'all_arrays'.",
370370
):
371-
actual_do.all_arrays = np.empty((4, 4))
371+
do.all_arrays = np.empty((4, 4))
372372

373373

374374
def test_id_getter():

Diff for: tests/test_transforms.py

+47-32
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,96 @@
1+
import re
2+
13
import numpy as np
24
import pytest
35

46
from diffpy.utils.transforms import d_to_q, d_to_tth, q_to_d, q_to_tth, tth_to_d, tth_to_q
57

68
params_q_to_tth = [
79
# UC1: Empty q values, no wavelength, return empty arrays
8-
([None, np.empty((0))], np.empty((0))),
10+
(None, np.empty((0)), np.empty((0))),
911
# UC2: Empty q values, wavelength specified, return empty arrays
10-
([4 * np.pi, np.empty((0))], np.empty(0)),
12+
(4 * np.pi, np.empty((0)), np.empty(0)),
1113
# UC3: User specified valid q values, no wavelength, return empty arrays
1214
(
13-
[None, np.array([0, 0.2, 0.4, 0.6, 0.8, 1])],
15+
None,
16+
np.array([0, 0.2, 0.4, 0.6, 0.8, 1]),
1417
np.array([0, 1, 2, 3, 4, 5]),
1518
),
1619
# UC4: User specified valid q values (with wavelength)
1720
# expected tth values are 2*arcsin(q) in degrees
18-
([4 * np.pi, np.array([0, 1 / np.sqrt(2), 1.0])], np.array([0, 90.0, 180.0])),
21+
(4 * np.pi, np.array([0, 1 / np.sqrt(2), 1.0]), np.array([0, 90.0, 180.0])),
1922
]
2023

2124

22-
@pytest.mark.parametrize("inputs, expected", params_q_to_tth)
23-
def test_q_to_tth(inputs, expected):
24-
actual = q_to_tth(inputs[1], inputs[0])
25-
assert np.allclose(expected, actual)
25+
@pytest.mark.parametrize("wavelength, q, expected_tth", params_q_to_tth)
26+
def test_q_to_tth(wavelength, q, expected_tth):
27+
28+
wavelength_warning_emsg = (
29+
"No wavelength has been specified. You can continue to use the DiffractionObject, but "
30+
"some of its powerful features will not be available. "
31+
"To specify a wavelength, if you have do = DiffractionObject(xarray, yarray, 'tth'), "
32+
"you may set do.wavelength = 1.54 for a wavelength of 1.54 angstroms."
33+
)
34+
if wavelength is None:
35+
with pytest.warns(UserWarning, match=re.escape(wavelength_warning_emsg)):
36+
actual_tth = q_to_tth(q, wavelength)
37+
else:
38+
actual_tth = q_to_tth(q, wavelength)
39+
40+
assert np.allclose(expected_tth, actual_tth)
2641

2742

2843
params_q_to_tth_bad = [
2944
# UC1: user specified invalid q values that result in tth > 180 degrees
3045
(
31-
[4 * np.pi, np.array([0.2, 0.4, 0.6, 0.8, 1, 1.2])],
32-
[
33-
ValueError,
34-
"The supplied input array and wavelength will result in an impossible two-theta. "
35-
"Please check these values and re-instantiate the DiffractionObject with correct values.",
36-
],
46+
4 * np.pi,
47+
np.array([0.2, 0.4, 0.6, 0.8, 1, 1.2]),
48+
ValueError,
49+
"The supplied input array and wavelength will result in an impossible two-theta. "
50+
"Please check these values and re-instantiate the DiffractionObject with correct values.",
3751
),
3852
# UC2: user specified a wrong wavelength that result in tth > 180 degrees
3953
(
40-
[100, np.array([0, 0.2, 0.4, 0.6, 0.8, 1])],
41-
[
42-
ValueError,
43-
"The supplied input array and wavelength will result in an impossible two-theta. "
44-
"Please check these values and re-instantiate the DiffractionObject with correct values.",
45-
],
54+
100,
55+
np.array([0, 0.2, 0.4, 0.6, 0.8, 1]),
56+
ValueError,
57+
"The supplied input array and wavelength will result in an impossible two-theta. "
58+
"Please check these values and re-instantiate the DiffractionObject with correct values.",
4659
),
4760
]
4861

4962

50-
@pytest.mark.parametrize("inputs, expected", params_q_to_tth_bad)
51-
def test_q_to_tth_bad(inputs, expected):
52-
with pytest.raises(expected[0], match=expected[1]):
53-
q_to_tth(inputs[1], inputs[0])
63+
@pytest.mark.parametrize("q, wavelength, expected_error_type, expected_error_msg", params_q_to_tth_bad)
64+
def test_q_to_tth_bad(q, wavelength, expected_error_type, expected_error_msg):
65+
with pytest.raises(expected_error_type, match=expected_error_msg):
66+
q_to_tth(wavelength, q)
5467

5568

5669
params_tth_to_q = [
5770
# UC0: User specified empty tth values (without wavelength)
58-
([None, np.array([])], np.array([])),
71+
(None, np.array([]), np.array([])),
5972
# UC1: User specified empty tth values (with wavelength)
60-
([4 * np.pi, np.array([])], np.array([])),
73+
(4 * np.pi, np.array([]), np.array([])),
6174
# UC2: User specified valid tth values between 0-180 degrees (without wavelength)
6275
(
63-
[None, np.array([0, 30, 60, 90, 120, 180])],
76+
None,
77+
np.array([0, 30, 60, 90, 120, 180]),
6478
np.array([0, 1, 2, 3, 4, 5]),
6579
),
6680
# UC3: User specified valid tth values between 0-180 degrees (with wavelength)
6781
# expected q values are sin15, sin30, sin45, sin60, sin90
6882
(
69-
[4 * np.pi, np.array([0, 30.0, 60.0, 90.0, 120.0, 180.0])],
83+
4 * np.pi,
84+
np.array([0, 30.0, 60.0, 90.0, 120.0, 180.0]),
7085
np.array([0, 0.258819, 0.5, 0.707107, 0.866025, 1]),
7186
),
7287
]
7388

7489

75-
@pytest.mark.parametrize("inputs, expected", params_tth_to_q)
76-
def test_tth_to_q(inputs, expected):
77-
actual = tth_to_q(inputs[1], inputs[0])
78-
assert np.allclose(actual, expected)
90+
@pytest.mark.parametrize("wavelength, tth, expected_q", params_tth_to_q)
91+
def test_tth_to_q(wavelength, tth, expected_q):
92+
actual_q = tth_to_q(tth, wavelength)
93+
assert np.allclose(actual_q, expected_q)
7994

8095

8196
params_tth_to_q_bad = [

0 commit comments

Comments
 (0)