forked from diffpy/diffpy.utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_diffraction_objects.py
441 lines (412 loc) · 13.7 KB
/
test_diffraction_objects.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
from pathlib import Path
import numpy as np
import pytest
from freezegun import freeze_time
from diffpy.utils.scattering_objects.diffraction_objects import DiffractionObject
params = [
( # Default
[
"",
None,
"",
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
{},
],
[
"",
None,
"",
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
{},
],
True,
),
( # Compare same attributes
[
"test",
0.71,
"x-ray",
[np.array([1, 2]), np.array([3, 4])],
[np.array([1, 2]), np.array([3, 4])],
[np.array([1, 2]), np.array([3, 4])],
{"thing1": 1, "thing2": "thing2"},
],
[
"test",
0.7100001,
"x-ray",
[np.array([1.00001, 2.00001]), np.array([3.00001, 4.00001])],
[np.array([1.00001, 2.00001]), np.array([3.00001, 4.00001])],
[np.array([1.00001, 2.00001]), np.array([3.00001, 4.00001])],
{"thing1": 1, "thing2": "thing2"},
],
True,
),
( # Different names
[
"test1",
None,
"",
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
{},
],
[
"test2",
None,
"",
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
{},
],
False,
),
( # Different wavelengths
[
"",
0.71,
"",
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
{},
],
[
"",
0.711,
"",
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
{},
],
False,
),
( # Different wavelengths
[
"",
0.71,
"",
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
{},
],
[
"",
None,
"",
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
{},
],
False,
),
( # Different scat_quantity
[
"",
None,
"",
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
{},
],
[
"",
None,
"x-ray",
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
{},
],
False,
),
( # Different on_q
[
"",
None,
"",
[np.array([1, 2]), np.array([3, 4])],
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
{},
],
[
"",
None,
"",
[np.array([1.01, 2]), np.array([3, 4])],
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
{},
],
False,
),
( # Different on_tth
[
"",
None,
"",
[np.empty(0), np.empty(0)],
[np.array([1, 2]), np.array([3, 4])],
[np.empty(0), np.empty(0)],
{},
],
[
"",
None,
"",
[np.empty(0), np.empty(0)],
[np.array([1.01, 2]), np.array([3, 4])],
[np.empty(0), np.empty(0)],
{},
],
False,
),
( # Different on_d
[
"",
None,
"",
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
[np.array([1, 2]), np.array([3, 4])],
{},
],
[
"",
None,
"",
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
[np.array([1.01, 2]), np.array([3, 4])],
{},
],
False,
),
( # Different metadata
[
"",
None,
"",
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
{"thing1": 0, "thing2": "thing2"},
],
[
"",
None,
"",
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
[np.empty(0), np.empty(0)],
{"thing1": 1, "thing2": "thing2"},
],
False,
),
]
@pytest.mark.parametrize("inputs1, inputs2, expected", params)
def test_diffraction_objects_equality(inputs1, inputs2, expected):
diffraction_object1 = DiffractionObject()
diffraction_object2 = DiffractionObject()
diffraction_object1_attributes = [key for key in diffraction_object1.__dict__ if not key.startswith("_")]
for i, attribute in enumerate(diffraction_object1_attributes):
setattr(diffraction_object1, attribute, inputs1[i])
setattr(diffraction_object2, attribute, inputs2[i])
assert (diffraction_object1 == diffraction_object2) == expected
def _test_valid_diffraction_objects(actual_diffraction_object, function, expected_array):
"""Checks the behavior of the DiffractionObject:
when there is no wavelength, we expect the correct warning message and output,
otherwise, we only check the output matches the expected array."""
if actual_diffraction_object.wavelength is None:
with pytest.warns(UserWarning) as warn_record:
getattr(actual_diffraction_object, function)()
assert str(warn_record[0].message) == (
"INFO: no wavelength has been specified. You can continue "
"to use the DiffractionObject but some of its powerful features "
"will not be available. To specify a wavelength, you can use "
"DiffractionObject(wavelength=0.71)."
)
actual_array = getattr(actual_diffraction_object, function)()
return np.allclose(actual_array, expected_array)
params_q_to_tth = [
# UC1: User specified empty q values (without wavelength)
(
[None, [], []],
[[]],
),
# UC2: User specified empty q values (with wavelength)
(
[4 * np.pi, [], []],
[[]],
),
# UC3: User specified valid q values (without wavelength)
# expected tth values are 2*arcsin(q) in degrees
(
[None, [0, 0.2, 0.4, 0.6, 0.8, 1], [1, 2, 3, 4, 5, 6]],
[[]],
),
# UC4: User specified valid q values (with wavelength)
# expected tth values are 2*arcsin(q) in degrees
(
[4 * np.pi, [0, 0.2, 0.4, 0.6, 0.8, 1], [1, 2, 3, 4, 5, 6]],
[[0, 23.07392, 47.15636, 73.73980, 106.26020, 180]],
),
]
@pytest.mark.parametrize("inputs, expected", params_q_to_tth)
def test_q_to_tth(inputs, expected):
actual = DiffractionObject(wavelength=inputs[0])
actual.on_q = [inputs[1], inputs[2]]
expected_tth = expected[0]
assert _test_valid_diffraction_objects(actual, "q_to_tth", expected_tth)
params_q_to_tth_bad = [
# UC1: user specified invalid q values that result in tth > 180 degrees
(
[4 * np.pi, [0.2, 0.4, 0.6, 0.8, 1, 1.2], [1, 2, 3, 4, 5, 6]],
[
ValueError,
"The supplied q-array and wavelength will result in an impossible two-theta. "
"Please check these values and re-instantiate the DiffractionObject.",
],
),
# UC2: user specified a wrong wavelength that result in tth > 180 degrees
(
[100, [0, 0.2, 0.4, 0.6, 0.8, 1], [1, 2, 3, 4, 5, 6]],
[
ValueError,
"The supplied q-array and wavelength will result in an impossible two-theta. "
"Please check these values and re-instantiate the DiffractionObject.",
],
),
# UC3: user specified a q array that does not match the length of intensity array (without wavelength)
(
[None, [0, 0.2, 0.4, 0.6, 0.8, 1], [1, 2, 3, 4, 5]],
[IndexError, "Please ensure q array and intensity array are the same length."],
),
# UC4: user specified a q array that does not match the length of intensity array (with wavelength)
(
[4 * np.pi, [0, 0.2, 0.4, 0.6, 0.8, 1], [1, 2, 3, 4, 5]],
[IndexError, "Please ensure q array and intensity array are the same length."],
),
# UC5: user specified a non-numeric value in q array (without wavelength)
(
[None, [0, 0.2, 0.4, 0.6, 0.8, "invalid"], [1, 2, 3, 4, 5, 6]],
[TypeError, "Invalid value found in q array. Please ensure all values are numeric."],
),
# UC5: user specified a non-numeric value in q array (with wavelength)
(
[4 * np.pi, [0, 0.2, 0.4, 0.6, 0.8, "invalid"], [1, 2, 3, 4, 5, 6]],
[TypeError, "Invalid value found in q array. Please ensure all values are numeric."],
),
]
@pytest.mark.parametrize("inputs, expected", params_q_to_tth_bad)
def test_q_to_tth_bad(inputs, expected):
actual = DiffractionObject(wavelength=inputs[0])
actual.on_q = [inputs[1], inputs[2]]
with pytest.raises(expected[0], match=expected[1]):
actual.q_to_tth()
params_tth_to_q = [
# UC1: User specified empty tth values (without wavelength)
(
[None, [], []],
[[]],
),
# UC2: User specified empty tth values (with wavelength)
(
[4 * np.pi, [], []],
[[]],
),
# UC3: User specified valid tth values between 0-180 degrees (without wavelength)
(
[None, [0, 30, 60, 90, 120, 180], [1, 2, 3, 4, 5, 6]],
[[]],
),
# UC4: User specified valid tth values between 0-180 degrees (with wavelength)
# expected q vales are sin15, sin30, sin45, sin60, sin90
(
[4 * np.pi, [0, 30, 60, 90, 120, 180], [1, 2, 3, 4, 5, 6]],
[[0, 0.258819, 0.5, 0.707107, 0.866025, 1]],
),
]
@pytest.mark.parametrize("inputs, expected", params_tth_to_q)
def test_tth_to_q(inputs, expected):
actual = DiffractionObject(wavelength=inputs[0])
actual.on_tth = [inputs[1], inputs[2]]
expected_q = expected[0]
assert _test_valid_diffraction_objects(actual, "tth_to_q", expected_q)
params_tth_to_q_bad = [
# UC1: user specified an invalid tth value of > 180 degrees (without wavelength)
(
[None, [0, 30, 60, 90, 120, 181], [1, 2, 3, 4, 5, 6]],
[ValueError, "Two theta exceeds 180 degrees. Please check the input values for errors."],
),
# UC2: user specified an invalid tth value of > 180 degrees (with wavelength)
(
[4 * np.pi, [0, 30, 60, 90, 120, 181], [1, 2, 3, 4, 5, 6]],
[ValueError, "Two theta exceeds 180 degrees. Please check the input values for errors."],
),
# UC3: user specified a two theta array that does not match the length of intensity array (without wavelength)
(
[None, [0, 30, 60, 90, 120], [1, 2, 3, 4, 5, 6]],
[IndexError, "Please ensure two theta array and intensity array are the same length."],
),
# UC4: user specified a two theta array that does not match the length of intensity array (with wavelength)
(
[4 * np.pi, [0, 30, 60, 90, 120], [1, 2, 3, 4, 5, 6]],
[IndexError, "Please ensure two theta array and intensity array are the same length."],
),
# UC5: user specified a non-numeric value in two theta array (without wavelength)
(
[None, [0, 30, 60, 90, 120, "invalid"], [1, 2, 3, 4, 5, 6]],
[TypeError, "Invalid value found in two theta array. Please ensure all values are numeric."],
),
# UC6: user specified a non-numeric value in two theta array (with wavelength)
(
[4 * np.pi, [0, 30, 60, 90, 120, "invalid"], [1, 2, 3, 4, 5, 6]],
[TypeError, "Invalid value found in two theta array. Please ensure all values are numeric."],
),
]
@pytest.mark.parametrize("inputs, expected", params_tth_to_q_bad)
def test_tth_to_q_bad(inputs, expected):
actual = DiffractionObject(wavelength=inputs[0])
actual.on_tth = [inputs[1], inputs[2]]
with pytest.raises(expected[0], match=expected[1]):
actual.tth_to_q()
def test_dump(tmp_path, mocker):
x, y = np.linspace(0, 5, 6), np.linspace(0, 5, 6)
directory = Path(tmp_path)
file = directory / "testfile"
test = DiffractionObject()
test.wavelength = 1.54
test.name = "test"
test.scat_quantity = "x-ray"
test.insert_scattering_quantity(
x, y, "q", metadata={"thing1": 1, "thing2": "thing2", "package_info": {"package2": "3.4.5"}}
)
mocker.patch("importlib.metadata.version", return_value="3.3.0")
with freeze_time("2012-01-14"):
test.dump(file, "q")
with open(file, "r") as f:
actual = f.read()
expected = (
"[DiffractionObject]\nname = test\nwavelength = 1.54\nscat_quantity = x-ray\nthing1 = 1\n"
"thing2 = thing2\npackage_info = {'package2': '3.4.5', 'diffpy.utils': '3.3.0'}\n"
"creation_time = 2012-01-14 00:00:00\n\n"
"#### start data\n0.000000000000000000e+00 0.000000000000000000e+00\n"
"1.000000000000000000e+00 1.000000000000000000e+00\n"
"2.000000000000000000e+00 2.000000000000000000e+00\n"
"3.000000000000000000e+00 3.000000000000000000e+00\n"
"4.000000000000000000e+00 4.000000000000000000e+00\n"
"5.000000000000000000e+00 5.000000000000000000e+00\n"
)
assert actual == expected