1
+ import re
2
+
1
3
import numpy as np
2
4
import pytest
3
5
6
8
params_q_to_tth = [
7
9
# UC1: Empty q values, no wavelength, return empty arrays
8
10
(None , np .empty ((0 )), np .empty ((0 ))),
9
- # # UC2: Empty q values, wavelength specified, return empty arrays
11
+ # UC2: Empty q values, wavelength specified, return empty arrays
10
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
(
23
25
@pytest .mark .parametrize ("wavelength, q, expected_tth" , params_q_to_tth )
24
26
def test_q_to_tth (wavelength , q , expected_tth ):
25
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 with the unit in angstroms."
33
+ )
26
34
if wavelength is None :
27
- with pytest .warns (UserWarning , match = "INFO: no wavelength has been specified" ):
35
+ with pytest .warns (UserWarning , match = re .escape (wavelength_warning_emsg )):
36
+ actual_tth = q_to_tth (q , wavelength )
28
37
actual_tth = q_to_tth (q , wavelength )
29
38
else :
30
39
actual_tth = q_to_tth (q , wavelength )
@@ -35,54 +44,54 @@ def test_q_to_tth(wavelength, q, expected_tth):
35
44
params_q_to_tth_bad = [
36
45
# UC1: user specified invalid q values that result in tth > 180 degrees
37
46
(
38
- [4 * np .pi , np .array ([0.2 , 0.4 , 0.6 , 0.8 , 1 , 1.2 ])],
39
- [
40
- ValueError ,
41
- "The supplied input array and wavelength will result in an impossible two-theta. "
42
- "Please check these values and re-instantiate the DiffractionObject with correct values." ,
43
- ],
47
+ 4 * np .pi ,
48
+ np .array ([0.2 , 0.4 , 0.6 , 0.8 , 1 , 1.2 ]),
49
+ ValueError ,
50
+ "The supplied input array and wavelength will result in an impossible two-theta. "
51
+ "Please check these values and re-instantiate the DiffractionObject with correct values." ,
44
52
),
45
53
# UC2: user specified a wrong wavelength that result in tth > 180 degrees
46
54
(
47
- [100 , np .array ([0 , 0.2 , 0.4 , 0.6 , 0.8 , 1 ])],
48
- [
49
- ValueError ,
50
- "The supplied input array and wavelength will result in an impossible two-theta. "
51
- "Please check these values and re-instantiate the DiffractionObject with correct values." ,
52
- ],
55
+ 100 ,
56
+ np .array ([0 , 0.2 , 0.4 , 0.6 , 0.8 , 1 ]),
57
+ ValueError ,
58
+ "The supplied input array and wavelength will result in an impossible two-theta. "
59
+ "Please check these values and re-instantiate the DiffractionObject with correct values." ,
53
60
),
54
61
]
55
62
56
63
57
- @pytest .mark .parametrize ("inputs, expected " , params_q_to_tth_bad )
58
- def test_q_to_tth_bad (inputs , expected ):
59
- with pytest .raises (expected [ 0 ] , match = expected [ 1 ] ):
60
- q_to_tth (inputs [ 1 ], inputs [ 0 ] )
64
+ @pytest .mark .parametrize ("q, wavelength, expected_error_type, expected_error_msg " , params_q_to_tth_bad )
65
+ def test_q_to_tth_bad (q , wavelength , expected_error_type , expected_error_msg ):
66
+ with pytest .raises (expected_error_type , match = expected_error_msg ):
67
+ q_to_tth (wavelength , q )
61
68
62
69
63
70
params_tth_to_q = [
64
71
# UC0: User specified empty tth values (without wavelength)
65
- ([ None , np .array ([])] , np .array ([])),
72
+ (None , np .array ([]), np .array ([])),
66
73
# UC1: User specified empty tth values (with wavelength)
67
- ([ 4 * np .pi , np .array ([])] , np .array ([])),
74
+ (4 * np .pi , np .array ([]), np .array ([])),
68
75
# UC2: User specified valid tth values between 0-180 degrees (without wavelength)
69
76
(
70
- [None , np .array ([0 , 30 , 60 , 90 , 120 , 180 ])],
77
+ None ,
78
+ np .array ([0 , 30 , 60 , 90 , 120 , 180 ]),
71
79
np .array ([0 , 1 , 2 , 3 , 4 , 5 ]),
72
80
),
73
81
# UC3: User specified valid tth values between 0-180 degrees (with wavelength)
74
82
# expected q values are sin15, sin30, sin45, sin60, sin90
75
83
(
76
- [4 * np .pi , np .array ([0 , 30.0 , 60.0 , 90.0 , 120.0 , 180.0 ])],
84
+ 4 * np .pi ,
85
+ np .array ([0 , 30.0 , 60.0 , 90.0 , 120.0 , 180.0 ]),
77
86
np .array ([0 , 0.258819 , 0.5 , 0.707107 , 0.866025 , 1 ]),
78
87
),
79
88
]
80
89
81
90
82
- @pytest .mark .parametrize ("inputs, expected " , params_tth_to_q )
83
- def test_tth_to_q (inputs , expected ):
84
- actual = tth_to_q (inputs [ 1 ], inputs [ 0 ] )
85
- assert np .allclose (actual , expected )
91
+ @pytest .mark .parametrize ("wavelength, tth, expected_q " , params_tth_to_q )
92
+ def test_tth_to_q (wavelength , tth , expected_q ):
93
+ actual_q = tth_to_q (tth , wavelength )
94
+ assert np .allclose (actual_q , expected_q )
86
95
87
96
88
97
params_tth_to_q_bad = [
0 commit comments