11import numpy as np
22import pytest
33
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
55
66params_q_to_tth = [
77 # UC1: Empty q values, no wavelength, return empty arrays
@@ -31,7 +31,7 @@ def test_q_to_tth(inputs, expected):
3131 [4 * np .pi , np .array ([0.2 , 0.4 , 0.6 , 0.8 , 1 , 1.2 ])],
3232 [
3333 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. "
3535 "Please check these values and re-instantiate the DiffractionObject with correct values." ,
3636 ],
3737 ),
@@ -40,7 +40,7 @@ def test_q_to_tth(inputs, expected):
4040 [100 , np .array ([0 , 0.2 , 0.4 , 0.6 , 0.8 , 1 ])],
4141 [
4242 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. "
4444 "Please check these values and re-instantiate the DiffractionObject with correct values." ,
4545 ],
4646 ),
@@ -103,8 +103,8 @@ def test_tth_to_q_bad(inputs, expected):
103103 ([np .array ([])], np .array ([])),
104104 # UC2: User specified valid q values
105105 (
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 ]),
108108 ),
109109]
110110
@@ -120,8 +120,8 @@ def test_q_to_d(inputs, expected):
120120 ([np .array ([])], np .array ([])),
121121 # UC2: User specified valid d values
122122 (
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 ]),
125125 ),
126126]
127127
@@ -130,3 +130,99 @@ def test_q_to_d(inputs, expected):
130130def test_d_to_q (inputs , expected ):
131131 actual = d_to_q (inputs [0 ])
132132 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