|
| 1 | +import re |
| 2 | + |
1 | 3 | import numpy as np
|
2 | 4 | import pytest
|
3 | 5 |
|
4 | 6 | from diffpy.utils.transforms import d_to_q, d_to_tth, q_to_d, q_to_tth, tth_to_d, tth_to_q
|
5 | 7 |
|
6 | 8 | params_q_to_tth = [
|
7 | 9 | # 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))), |
9 | 11 | # 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)), |
11 | 13 | # UC3: User specified valid q values, no wavelength, return empty arrays
|
12 | 14 | (
|
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]), |
14 | 17 | np.array([0, 1, 2, 3, 4, 5]),
|
15 | 18 | ),
|
16 | 19 | # UC4: User specified valid q values (with wavelength)
|
17 | 20 | # 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])), |
19 | 22 | ]
|
20 | 23 |
|
21 | 24 |
|
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) |
26 | 41 |
|
27 | 42 |
|
28 | 43 | params_q_to_tth_bad = [
|
29 | 44 | # UC1: user specified invalid q values that result in tth > 180 degrees
|
30 | 45 | (
|
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.", |
37 | 51 | ),
|
38 | 52 | # UC2: user specified a wrong wavelength that result in tth > 180 degrees
|
39 | 53 | (
|
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.", |
46 | 59 | ),
|
47 | 60 | ]
|
48 | 61 |
|
49 | 62 |
|
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) |
54 | 67 |
|
55 | 68 |
|
56 | 69 | params_tth_to_q = [
|
57 | 70 | # UC0: User specified empty tth values (without wavelength)
|
58 |
| - ([None, np.array([])], np.array([])), |
| 71 | + (None, np.array([]), np.array([])), |
59 | 72 | # UC1: User specified empty tth values (with wavelength)
|
60 |
| - ([4 * np.pi, np.array([])], np.array([])), |
| 73 | + (4 * np.pi, np.array([]), np.array([])), |
61 | 74 | # UC2: User specified valid tth values between 0-180 degrees (without wavelength)
|
62 | 75 | (
|
63 |
| - [None, np.array([0, 30, 60, 90, 120, 180])], |
| 76 | + None, |
| 77 | + np.array([0, 30, 60, 90, 120, 180]), |
64 | 78 | np.array([0, 1, 2, 3, 4, 5]),
|
65 | 79 | ),
|
66 | 80 | # UC3: User specified valid tth values between 0-180 degrees (with wavelength)
|
67 | 81 | # expected q values are sin15, sin30, sin45, sin60, sin90
|
68 | 82 | (
|
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]), |
70 | 85 | np.array([0, 0.258819, 0.5, 0.707107, 0.866025, 1]),
|
71 | 86 | ),
|
72 | 87 | ]
|
73 | 88 |
|
74 | 89 |
|
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) |
79 | 94 |
|
80 | 95 |
|
81 | 96 | params_tth_to_q_bad = [
|
|
0 commit comments