|
1 | 1 | import numpy as np |
2 | 2 | import pytest |
3 | 3 |
|
4 | | -from diffpy.utils.transforms import q_to_tth, tth_to_q |
| 4 | +from diffpy.utils.transforms import d_to_q, q_to_d, q_to_tth, tth_to_q |
5 | 5 |
|
6 | 6 | params_q_to_tth = [ |
7 | 7 | # UC1: Empty q values, no wavelength, return empty arrays |
@@ -64,7 +64,7 @@ def test_q_to_tth_bad(inputs, expected): |
64 | 64 | np.array([0, 1, 2, 3, 4, 5]), |
65 | 65 | ), |
66 | 66 | # UC3: User specified valid tth values between 0-180 degrees (with wavelength) |
67 | | - # expected q vales are sin15, sin30, sin45, sin60, sin90 |
| 67 | + # expected q values are sin15, sin30, sin45, sin60, sin90 |
68 | 68 | ( |
69 | 69 | [4 * np.pi, np.array([0, 30.0, 60.0, 90.0, 120.0, 180.0])], |
70 | 70 | np.array([0, 0.258819, 0.5, 0.707107, 0.866025, 1]), |
@@ -96,3 +96,73 @@ def test_tth_to_q(inputs, expected): |
96 | 96 | def test_tth_to_q_bad(inputs, expected): |
97 | 97 | with pytest.raises(expected[0], match=expected[1]): |
98 | 98 | tth_to_q(inputs[1], inputs[0]) |
| 99 | + |
| 100 | + |
| 101 | +params_q_to_d = [ |
| 102 | + # UC1: User specified empty q values |
| 103 | + ([np.array([])], np.array([])), |
| 104 | + # UC2: User specified valid q values |
| 105 | + ( |
| 106 | + [np.array([np.pi / 6, 1 * np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi])], |
| 107 | + np.array([0.4, 0.5, 0.66667, 1, 2, 12]), |
| 108 | + ), |
| 109 | +] |
| 110 | + |
| 111 | + |
| 112 | +@pytest.mark.parametrize("inputs, expected", params_q_to_d) |
| 113 | +def test_q_to_d(inputs, expected): |
| 114 | + actual = q_to_d(inputs[0]) |
| 115 | + assert np.allclose(actual, expected) |
| 116 | + |
| 117 | + |
| 118 | +params_q_to_d_bad = [ |
| 119 | + # UC1: user specified an invalid q value that results in an infinite d value |
| 120 | + ( |
| 121 | + [np.array([0, 1, 2, 3, 4])], |
| 122 | + [ |
| 123 | + ValueError, |
| 124 | + "Input values have resulted in an infinite output. Please ensure there are no zeros in the input.", |
| 125 | + ], |
| 126 | + ), |
| 127 | +] |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.parametrize("inputs, expected", params_q_to_d_bad) |
| 131 | +def test_q_to_d_bad(inputs, expected): |
| 132 | + with pytest.raises(expected[0], match=expected[1]): |
| 133 | + q_to_d(inputs[0]) |
| 134 | + |
| 135 | + |
| 136 | +params_d_to_q = [ |
| 137 | + # UC1: User specified empty d values |
| 138 | + ([np.array([])], np.array([])), |
| 139 | + # UC2: User specified valid d values |
| 140 | + ( |
| 141 | + [np.array([np.pi / 6, 1 * np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi])], |
| 142 | + np.array([0.4, 0.5, 0.66667, 1, 2, 12]), |
| 143 | + ), |
| 144 | +] |
| 145 | + |
| 146 | + |
| 147 | +@pytest.mark.parametrize("inputs, expected", params_d_to_q) |
| 148 | +def test_d_to_q(inputs, expected): |
| 149 | + actual = d_to_q(inputs[0]) |
| 150 | + assert np.allclose(actual, expected) |
| 151 | + |
| 152 | + |
| 153 | +params_d_to_q_bad = [ |
| 154 | + # UC1: user specified an invalid d value that results in an infinite q value |
| 155 | + ( |
| 156 | + [np.array([0, 1, 2, 3, 4])], |
| 157 | + [ |
| 158 | + ValueError, |
| 159 | + "Input values have resulted in an infinite output. Please ensure there are no zeros in the input.", |
| 160 | + ], |
| 161 | + ), |
| 162 | +] |
| 163 | + |
| 164 | + |
| 165 | +@pytest.mark.parametrize("inputs, expected", params_d_to_q_bad) |
| 166 | +def test_d_to_q_bad(inputs, expected): |
| 167 | + with pytest.raises(expected[0], match=expected[1]): |
| 168 | + d_to_q(inputs[0]) |
0 commit comments