1
1
import numpy as np
2
2
import pytest
3
3
4
- from diffpy .utils .transforms import d_to_q , q_to_d , q_to_tth , tth_to_q
4
+ from diffpy .utils .transforms import d_to_q , d_to_tth , q_to_d , q_to_tth , tth_to_d , tth_to_q
5
5
6
6
params_q_to_tth = [
7
7
# UC1: Empty q values, no wavelength, return empty arrays
@@ -31,7 +31,7 @@ def test_q_to_tth(inputs, expected):
31
31
[4 * np .pi , np .array ([0.2 , 0.4 , 0.6 , 0.8 , 1 , 1.2 ])],
32
32
[
33
33
ValueError ,
34
- "The supplied q- array and wavelength will result in an impossible two-theta. "
34
+ "The supplied input array and wavelength will result in an impossible two-theta. "
35
35
"Please check these values and re-instantiate the DiffractionObject with correct values." ,
36
36
],
37
37
),
@@ -40,7 +40,7 @@ def test_q_to_tth(inputs, expected):
40
40
[100 , np .array ([0 , 0.2 , 0.4 , 0.6 , 0.8 , 1 ])],
41
41
[
42
42
ValueError ,
43
- "The supplied q- array and wavelength will result in an impossible two-theta. "
43
+ "The supplied input array and wavelength will result in an impossible two-theta. "
44
44
"Please check these values and re-instantiate the DiffractionObject with correct values." ,
45
45
],
46
46
),
@@ -103,8 +103,8 @@ def test_tth_to_q_bad(inputs, expected):
103
103
([np .array ([])], np .array ([])),
104
104
# UC2: User specified valid q values
105
105
(
106
- [np .array ([5 * np .pi , 4 * np .pi , 3 * np .pi , 2 * np .pi , np .pi , 0 ])],
107
- np .array ([0.4 , 0.5 , 0.66667 , 1 , 2 , np . inf ]),
106
+ [np .array ([0 , 1 * np .pi , 2 * np .pi , 3 * np .pi , 4 * np .pi , 5 * np .pi ])],
107
+ np .array ([np . inf , 2 , 1 , 0.66667 , 0.5 , 0.4 ]),
108
108
),
109
109
]
110
110
@@ -120,8 +120,8 @@ def test_q_to_d(inputs, expected):
120
120
([np .array ([])], np .array ([])),
121
121
# UC2: User specified valid d values
122
122
(
123
- [np .array ([0 , 1 * np .pi , 2 * np .pi , 3 * np .pi , 4 * np .pi , 5 * np .pi ])],
124
- np .array ([np . inf , 2 , 1 , 0.66667 , 0.5 , 0.4 ]),
123
+ [np .array ([5 * np .pi , 4 * np .pi , 3 * np .pi , 2 * np .pi , np .pi , 0 ])],
124
+ np .array ([0.4 , 0.5 , 0.66667 , 1 , 2 , np . inf ]),
125
125
),
126
126
]
127
127
@@ -130,3 +130,99 @@ def test_q_to_d(inputs, expected):
130
130
def test_d_to_q (inputs , expected ):
131
131
actual = d_to_q (inputs [0 ])
132
132
assert np .allclose (actual , expected )
133
+
134
+
135
+ params_tth_to_d = [
136
+ # UC0: User specified empty tth values (without wavelength)
137
+ ([None , np .array ([])], np .array ([])),
138
+ # UC1: User specified empty tth values (with wavelength)
139
+ ([4 * np .pi , np .array ([])], np .array ([])),
140
+ # UC2: User specified valid tth values between 0-180 degrees (without wavelength)
141
+ (
142
+ [None , np .array ([0 , 30 , 60 , 90 , 120 , 180 ])],
143
+ np .array ([0 , 1 , 2 , 3 , 4 , 5 ]),
144
+ ),
145
+ # UC3: User specified valid tth values between 0-180 degrees (with wavelength)
146
+ (
147
+ [4 * np .pi , np .array ([0 , 30.0 , 60.0 , 90.0 , 120.0 , 180.0 ])],
148
+ np .array ([np .inf , 24.27636 , 12.56637 , 8.88577 , 7.25520 , 6.28319 ]),
149
+ ),
150
+ ]
151
+
152
+
153
+ @pytest .mark .parametrize ("inputs, expected" , params_tth_to_d )
154
+ def test_tth_to_d (inputs , expected ):
155
+ actual = tth_to_d (inputs [1 ], inputs [0 ])
156
+ assert np .allclose (actual , expected )
157
+
158
+
159
+ params_tth_to_d_bad = [
160
+ # UC1: user specified an invalid tth value of > 180 degrees (without wavelength)
161
+ (
162
+ [None , np .array ([0 , 30 , 60 , 90 , 120 , 181 ])],
163
+ [ValueError , "Two theta exceeds 180 degrees. Please check the input values for errors." ],
164
+ ),
165
+ # UC2: user specified an invalid tth value of > 180 degrees (with wavelength)
166
+ (
167
+ [4 * np .pi , np .array ([0 , 30 , 60 , 90 , 120 , 181 ])],
168
+ [ValueError , "Two theta exceeds 180 degrees. Please check the input values for errors." ],
169
+ ),
170
+ ]
171
+
172
+
173
+ @pytest .mark .parametrize ("inputs, expected" , params_tth_to_d_bad )
174
+ def test_tth_to_d_bad (inputs , expected ):
175
+ with pytest .raises (expected [0 ], match = expected [1 ]):
176
+ tth_to_d (inputs [1 ], inputs [0 ])
177
+
178
+
179
+ params_d_to_tth = [
180
+ # UC1: Empty d values, no wavelength, return empty arrays
181
+ ([None , np .empty ((0 ))], np .empty ((0 ))),
182
+ # UC2: Empty d values, wavelength specified, return empty arrays
183
+ ([4 * np .pi , np .empty ((0 ))], np .empty (0 )),
184
+ # UC3: User specified valid d values, no wavelength, return empty arrays
185
+ (
186
+ [None , np .array ([1 , 0.8 , 0.6 , 0.4 , 0.2 , 0 ])],
187
+ np .array ([0 , 1 , 2 , 3 , 4 , 5 ]),
188
+ ),
189
+ # UC4: User specified valid d values (with wavelength)
190
+ (
191
+ [4 * np .pi , np .array ([4 * np .pi , 4 / np .sqrt (2 ) * np .pi , 4 / np .sqrt (3 ) * np .pi ])],
192
+ np .array ([60.0 , 90.0 , 120.0 ]),
193
+ ),
194
+ ]
195
+
196
+
197
+ @pytest .mark .parametrize ("inputs, expected" , params_d_to_tth )
198
+ def test_d_to_tth (inputs , expected ):
199
+ actual = d_to_tth (inputs [1 ], inputs [0 ])
200
+ assert np .allclose (expected , actual )
201
+
202
+
203
+ params_d_to_tth_bad = [
204
+ # UC1: user specified invalid d values that result in tth > 180 degrees
205
+ (
206
+ [4 * np .pi , np .array ([1.2 , 1 , 0.8 , 0.6 , 0.4 , 0.2 ])],
207
+ [
208
+ ValueError ,
209
+ "The supplied input array and wavelength will result in an impossible two-theta. "
210
+ "Please check these values and re-instantiate the DiffractionObject with correct values." ,
211
+ ],
212
+ ),
213
+ # UC2: user specified a wrong wavelength that result in tth > 180 degrees
214
+ (
215
+ [100 , np .array ([1 , 0.8 , 0.6 , 0.4 , 0.2 , 0 ])],
216
+ [
217
+ ValueError ,
218
+ "The supplied input array and wavelength will result in an impossible two-theta. "
219
+ "Please check these values and re-instantiate the DiffractionObject with correct values." ,
220
+ ],
221
+ ),
222
+ ]
223
+
224
+
225
+ @pytest .mark .parametrize ("inputs, expected" , params_d_to_tth_bad )
226
+ def test_d_to_tth_bad (inputs , expected ):
227
+ with pytest .raises (expected [0 ], match = expected [1 ]):
228
+ d_to_tth (inputs [1 ], inputs [0 ])
0 commit comments