forked from diffpy/diffpy.utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_diffraction_objects.py
547 lines (503 loc) · 16.8 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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
import re
from pathlib import Path
import numpy as np
import pytest
from deepdiff import DeepDiff
from freezegun import freeze_time
from diffpy.utils.diffraction_objects import XQUANTITIES, DiffractionObject
params = [
( # Default
{},
{},
True,
),
( # Compare same attributes
{
"name": "same",
"scat_quantity": "x-ray",
"wavelength": 0.71,
"xtype": "q",
"xarray": np.array([1.0, 2.0]),
"yarray": np.array([100.0, 200.0]),
"metadata": {"thing1": 1},
},
{
"name": "same",
"scat_quantity": "x-ray",
"wavelength": 0.71,
"xtype": "q",
"xarray": np.array([1.0, 2.0]),
"yarray": np.array([100.0, 200.0]),
"metadata": {"thing1": 1},
},
True,
),
( # Different names
{
"name": "something",
"scat_quantity": "",
"wavelength": None,
"xtype": "",
"xarray": np.empty(0),
"yarray": np.empty(0),
"metadata": {"thing1": 1, "thing2": "thing2"},
},
{
"name": "something else",
"scat_quantity": "",
"wavelength": None,
"xtype": "",
"xarray": np.empty(0),
"yarray": np.empty(0),
"metadata": {"thing1": 1, "thing2": "thing2"},
},
False,
),
( # Different wavelengths
{
"scat_quantity": "",
"wavelength": 0.71,
"xtype": "",
"xarray": np.empty(0),
"yarray": np.empty(0),
"metadata": {"thing1": 1, "thing2": "thing2"},
},
{
"scat_quantity": "",
"wavelength": None,
"xtype": "",
"xarray": np.empty(0),
"yarray": np.empty(0),
"metadata": {"thing1": 1, "thing2": "thing2"},
},
False,
),
( # Different wavelengths
{
"scat_quantity": "",
"wavelength": 0.71,
"xtype": "",
"xarray": np.empty(0),
"yarray": np.empty(0),
"metadata": {"thing1": 1, "thing2": "thing2"},
},
{
"scat_quantity": "",
"wavelength": 0.711,
"xtype": "",
"xarray": np.empty(0),
"yarray": np.empty(0),
"metadata": {"thing1": 1, "thing2": "thing2"},
},
False,
),
( # Different scat_quantity
{
"scat_quantity": "x-ray",
"wavelength": None,
"xtype": "",
"xarray": np.empty(0),
"yarray": np.empty(0),
"metadata": {"thing1": 1, "thing2": "thing2"},
},
{
"scat_quantity": "neutron",
"wavelength": None,
"xtype": "",
"xarray": np.empty(0),
"yarray": np.empty(0),
"metadata": {"thing1": 1, "thing2": "thing2"},
},
False,
),
( # Different on_q
{
"scat_quantity": "",
"wavelength": None,
"xtype": "q",
"xarray": np.array([1.0, 2.0]),
"yarray": np.array([100.0, 200.0]),
"metadata": {},
},
{
"scat_quantity": "",
"wavelength": None,
"xtype": "q",
"xarray": np.array([3.0, 4.0]),
"yarray": np.array([100.0, 200.0]),
"metadata": {"thing1": 1, "thing2": "thing2"},
},
False,
),
( # Different metadata
{
"scat_quantity": "",
"wavelength": None,
"xtype": "",
"xarray": np.empty(0),
"yarray": np.empty(0),
"metadata": {"thing1": 0, "thing2": "thing2"},
},
{
"scat_quantity": "",
"wavelength": None,
"xtype": "",
"xarray": np.empty(0),
"yarray": np.empty(0),
"metadata": {"thing1": 1, "thing2": "thing2"},
},
False,
),
]
@pytest.mark.parametrize("inputs1, inputs2, expected", params)
def test_diffraction_objects_equality(inputs1, inputs2, expected):
do_1 = DiffractionObject(**inputs1)
do_2 = DiffractionObject(**inputs2)
assert (do_1 == do_2) == expected
def test_on_xtype():
test = DiffractionObject(wavelength=2 * np.pi, xarray=np.array([30, 60]), yarray=np.array([1, 2]), xtype="tth")
assert np.allclose(test.on_xtype("tth"), [np.array([30, 60]), np.array([1, 2])])
assert np.allclose(test.on_xtype("2theta"), [np.array([30, 60]), np.array([1, 2])])
assert np.allclose(test.on_xtype("q"), [np.array([0.51764, 1]), np.array([1, 2])])
assert np.allclose(test.on_xtype("d"), [np.array([12.13818, 6.28319]), np.array([1, 2])])
def test_init_invalid_xtype():
with pytest.raises(
ValueError,
match=re.escape(
f"I don't know how to handle the xtype, 'invalid_type'. "
f"Please rerun specifying an xtype from {*XQUANTITIES, }"
),
):
DiffractionObject(xtype="invalid_type")
params_scale_to = [
# UC1: same x-array and y-array, check offset
(
[
np.array([10, 15, 25, 30, 60, 140]),
np.array([2, 3, 4, 5, 6, 7]),
"tth",
2 * np.pi,
np.array([10, 15, 25, 30, 60, 140]),
np.array([2, 3, 4, 5, 6, 7]),
"tth",
2 * np.pi,
None,
60,
None,
2.1,
],
["tth", np.array([4.1, 5.1, 6.1, 7.1, 8.1, 9.1])],
),
# UC2: same length x-arrays with exact x-value match
(
[
np.array([10, 15, 25, 30, 60, 140]),
np.array([10, 20, 25, 30, 60, 100]),
"tth",
2 * np.pi,
np.array([10, 20, 25, 30, 60, 140]),
np.array([2, 3, 4, 5, 6, 7]),
"tth",
2 * np.pi,
None,
60,
None,
0,
],
["tth", np.array([1, 2, 2.5, 3, 6, 10])],
),
# UC3: same length x-arrays with approximate x-value match
(
[
np.array([0.12, 0.24, 0.31, 0.4]),
np.array([10, 20, 40, 60]),
"q",
2 * np.pi,
np.array([0.14, 0.24, 0.31, 0.4]),
np.array([1, 3, 4, 5]),
"q",
2 * np.pi,
0.1,
None,
None,
0,
],
["q", np.array([1, 2, 4, 6])],
),
# UC4: different x-array lengths with approximate x-value match
(
[
np.array([10, 25, 30.1, 40.2, 61, 120, 140]),
np.array([10, 20, 30, 40, 50, 60, 100]),
"tth",
2 * np.pi,
np.array([20, 25.5, 32, 45, 50, 62, 100, 125, 140]),
np.array([1.1, 2, 3, 3.5, 4, 5, 10, 12, 13]),
"tth",
2 * np.pi,
None,
60,
None,
0,
],
# scaling factor is calculated at index = 5 for self and index = 6 for target
["tth", np.array([1, 2, 3, 4, 5, 6, 10])],
),
# UC5: user did not specify anything, use the midpoint of the current object's q-array
(
[
np.array([0.1, 0.2, 0.3]),
np.array([1, 2, 3]),
"q",
2 * np.pi,
np.array([0.05, 0.1, 0.2, 0.3]),
np.array([5, 10, 20, 30]),
"q",
2 * np.pi,
None,
None,
None,
0,
],
["q", np.array([10, 20, 30])],
),
]
@pytest.mark.parametrize("inputs, expected", params_scale_to)
def test_scale_to(inputs, expected):
orig_diff_object = DiffractionObject(xarray=inputs[0], yarray=inputs[1], xtype=inputs[2], wavelength=inputs[3])
target_diff_object = DiffractionObject(
xarray=inputs[4], yarray=inputs[5], xtype=inputs[6], wavelength=inputs[7]
)
scaled_diff_object = orig_diff_object.scale_to(
target_diff_object, q=inputs[8], tth=inputs[9], d=inputs[10], offset=inputs[11]
)
# Check the intensity data is same as expected
assert np.allclose(scaled_diff_object.on_xtype(expected[0])[1], expected[1])
params_scale_to_bad = [
(
np.array([10, 25, 30.1, 40.2, 61, 120, 140]),
np.array([10, 20, 30, 40, 50, 60, 100]),
"tth",
2 * np.pi,
np.array([20, 25.5, 32, 45, 50, 62, 100, 125, 140]),
np.array([1.1, 2, 3, 3.5, 4, 5, 10, 12, 13]),
"tth",
2 * np.pi,
None,
60,
10,
0,
),
]
@pytest.mark.parametrize("inputs", params_scale_to_bad)
def test_scale_to_bad(inputs):
orig_diff_object = DiffractionObject(xarray=inputs[0], yarray=inputs[1], xtype=inputs[2], wavelength=inputs[3])
target_diff_object = DiffractionObject(
xarray=inputs[4], yarray=inputs[5], xtype=inputs[6], wavelength=inputs[7]
)
with pytest.raises(
ValueError, match="You can only specify one of 'q', 'tth', or 'd'. Please rerun specifying only one."
):
orig_diff_object.scale_to(target_diff_object, q=inputs[8], tth=inputs[9], d=inputs[10], offset=inputs[11])
params_index = [
# UC1: exact match
([4 * np.pi, np.array([30.005, 60]), np.array([1, 2]), "tth", "tth", 30.005], [0]),
# UC2: target value lies in the array, returns the (first) closest index
([4 * np.pi, np.array([30, 60]), np.array([1, 2]), "tth", "tth", 45], [0]),
([4 * np.pi, np.array([30, 60]), np.array([1, 2]), "tth", "q", 0.25], [0]),
# UC3: target value out of the range, returns the closest index
([4 * np.pi, np.array([0.25, 0.5, 0.71]), np.array([1, 2, 3]), "q", "q", 0.1], [0]),
([4 * np.pi, np.array([30, 60]), np.array([1, 2]), "tth", "tth", 63], [1]),
]
@pytest.mark.parametrize("inputs, expected", params_index)
def test_get_array_index(inputs, expected):
test = DiffractionObject(wavelength=inputs[0], xarray=inputs[1], yarray=inputs[2], xtype=inputs[3])
actual = test.get_array_index(value=inputs[5], xtype=inputs[4])
assert actual == expected[0]
def test_get_array_index_bad():
test = DiffractionObject(wavelength=2 * np.pi, xarray=np.array([]), yarray=np.array([]), xtype="tth")
with pytest.raises(ValueError, match=re.escape("The 'tth' array is empty. Please ensure it is initialized.")):
test.get_array_index(value=30)
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(
wavelength=1.54,
name="test",
scat_quantity="x-ray",
xarray=np.array(x),
yarray=np.array(y),
xtype="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
tc_params = [
(
{},
{
"_all_arrays": np.empty(shape=(0, 4)), # instantiate empty
"metadata": {},
"_input_xtype": "",
"name": "",
"scat_quantity": None,
"qmin": np.float64(np.inf),
"qmax": np.float64(0.0),
"tthmin": np.float64(np.inf),
"tthmax": np.float64(0.0),
"dmin": np.float64(np.inf),
"dmax": np.float64(0.0),
"wavelength": None,
},
),
( # instantiate just non-array attributes
{"name": "test", "scat_quantity": "x-ray", "metadata": {"thing": "1", "another": "2"}},
{
"_all_arrays": np.empty(shape=(0, 4)),
"metadata": {"thing": "1", "another": "2"},
"_input_xtype": "",
"name": "test",
"scat_quantity": "x-ray",
"qmin": np.float64(np.inf),
"qmax": np.float64(0.0),
"tthmin": np.float64(np.inf),
"tthmax": np.float64(0.0),
"dmin": np.float64(np.inf),
"dmax": np.float64(0.0),
"wavelength": None,
},
),
( # instantiate just array attributes
{
"xarray": np.array([0.0, 90.0, 180.0]),
"yarray": np.array([1.0, 2.0, 3.0]),
"xtype": "tth",
"wavelength": 4.0 * np.pi,
},
{
"_all_arrays": np.array(
[
[1.0, 0.0, 0.0, np.float64(np.inf)],
[2.0, 1.0 / np.sqrt(2), 90.0, np.sqrt(2) * 2 * np.pi],
[3.0, 1.0, 180.0, 1.0 * 2 * np.pi],
]
),
"metadata": {},
"_input_xtype": "tth",
"name": "",
"scat_quantity": None,
"qmin": np.float64(0.0),
"qmax": np.float64(1.0),
"tthmin": np.float64(0.0),
"tthmax": np.float64(180.0),
"dmin": np.float64(2 * np.pi),
"dmax": np.float64(np.inf),
"wavelength": 4.0 * np.pi,
},
),
( # instantiate just array attributes
{
"xarray": np.array([np.inf, 2 * np.sqrt(2) * np.pi, 2 * np.pi]),
"yarray": np.array([1.0, 2.0, 3.0]),
"xtype": "d",
"wavelength": 4.0 * np.pi,
"scat_quantity": "x-ray",
},
{
"_all_arrays": np.array(
[
[1.0, 0.0, 0.0, np.float64(np.inf)],
[2.0, 1.0 / np.sqrt(2), 90.0, np.sqrt(2) * 2 * np.pi],
[3.0, 1.0, 180.0, 1.0 * 2 * np.pi],
]
),
"metadata": {},
"_input_xtype": "d",
"name": "",
"scat_quantity": "x-ray",
"qmin": np.float64(0.0),
"qmax": np.float64(1.0),
"tthmin": np.float64(0.0),
"tthmax": np.float64(180.0),
"dmin": np.float64(2 * np.pi),
"dmax": np.float64(np.inf),
"wavelength": 4.0 * np.pi,
},
),
]
@pytest.mark.parametrize("inputs, expected", tc_params)
def test_constructor(inputs, expected):
actual_do = DiffractionObject(**inputs)
diff = DeepDiff(actual_do.__dict__, expected, ignore_order=True, significant_digits=13)
assert diff == {}
def test_all_array_getter():
actual_do = DiffractionObject(
xarray=np.array([0.0, 90.0, 180.0]),
yarray=np.array([1.0, 2.0, 3.0]),
xtype="tth",
wavelength=4.0 * np.pi,
)
expected_all_arrays = np.array(
[
[1.0, 0.0, 0.0, np.float64(np.inf)],
[2.0, 1.0 / np.sqrt(2), 90.0, np.sqrt(2) * 2 * np.pi],
[3.0, 1.0, 180.0, 1.0 * 2 * np.pi],
]
)
assert np.allclose(actual_do.all_arrays, expected_all_arrays)
def test_all_array_setter():
actual_do = DiffractionObject()
# Attempt to directly modify the property
with pytest.raises(
AttributeError,
match="Direct modification of attribute 'all_arrays' is not allowed. "
"Please use 'input_data' to modify 'all_arrays'.",
):
actual_do.all_arrays = np.empty((4, 4))
def test_xarray_yarray_length_mismatch():
with pytest.raises(
ValueError,
match="'xarray' and 'yarray' must have the same length. "
"Please re-initialize 'DiffractionObject' or re-run the method 'input_data' "
"with 'xarray' and 'yarray' of identical length",
):
DiffractionObject(xarray=np.array([1.0, 2.0]), yarray=np.array([0.0, 0.0, 0.0]))
def test_input_xtype_getter():
do = DiffractionObject(xtype="tth")
assert do.input_xtype == "tth"
def test_input_xtype_setter():
do = DiffractionObject(xtype="tth")
# Attempt to directly modify the property
with pytest.raises(
AttributeError,
match="Direct modification of attribute 'input_xtype' is not allowed. "
"Please use 'input_data' to modify 'input_xtype'.",
):
do.input_xtype = "q"
def test_copy_object():
do = DiffractionObject(
name="test",
wavelength=4.0 * np.pi,
xarray=np.array([0.0, 90.0, 180.0]),
yarray=np.array([1.0, 2.0, 3.0]),
xtype="tth",
)
do_copy = do.copy()
assert do == do_copy
assert id(do) != id(do_copy)