11import numpy as np
22import pytest
33
4- from diffpy .utils .transforms import q_to_tth , tth_to_q
4+ from diffpy .utils .transforms import d_to_tth , 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 ),
@@ -96,3 +96,99 @@ def test_tth_to_q(inputs, expected):
9696def test_tth_to_q_bad (inputs , expected ):
9797 with pytest .raises (expected [0 ], match = expected [1 ]):
9898 tth_to_q (inputs [1 ], inputs [0 ])
99+
100+
101+ params_tth_to_d = [
102+ # UC0: User specified empty tth values (without wavelength)
103+ ([None , np .array ([])], np .array ([])),
104+ # UC1: User specified empty tth values (with wavelength)
105+ ([4 * np .pi , np .array ([])], np .array ([])),
106+ # UC2: User specified valid tth values between 0-180 degrees (without wavelength)
107+ (
108+ [None , np .array ([0 , 30 , 60 , 90 , 120 , 180 ])],
109+ np .array ([0 , 1 , 2 , 3 , 4 , 5 ]),
110+ ),
111+ # UC3: User specified valid tth values between 0-180 degrees (with wavelength)
112+ (
113+ [4 * np .pi , np .array ([0 , 30.0 , 60.0 , 90.0 , 120.0 , 180.0 ])],
114+ np .array ([np .inf , 24.27636 , 12.56637 , 8.88577 , 7.25520 , 6.28319 ]),
115+ ),
116+ ]
117+
118+
119+ @pytest .mark .parametrize ("inputs, expected" , params_tth_to_d )
120+ def test_tth_to_d (inputs , expected ):
121+ actual = tth_to_d (inputs [1 ], inputs [0 ])
122+ assert np .allclose (actual , expected )
123+
124+
125+ params_tth_to_d_bad = [
126+ # UC1: user specified an invalid tth value of > 180 degrees (without wavelength)
127+ (
128+ [None , np .array ([0 , 30 , 60 , 90 , 120 , 181 ])],
129+ [ValueError , "Two theta exceeds 180 degrees. Please check the input values for errors." ],
130+ ),
131+ # UC2: user specified an invalid tth value of > 180 degrees (with wavelength)
132+ (
133+ [4 * np .pi , np .array ([0 , 30 , 60 , 90 , 120 , 181 ])],
134+ [ValueError , "Two theta exceeds 180 degrees. Please check the input values for errors." ],
135+ ),
136+ ]
137+
138+
139+ @pytest .mark .parametrize ("inputs, expected" , params_tth_to_d_bad )
140+ def test_tth_to_d_bad (inputs , expected ):
141+ with pytest .raises (expected [0 ], match = expected [1 ]):
142+ tth_to_d (inputs [1 ], inputs [0 ])
143+
144+
145+ params_d_to_tth = [
146+ # UC1: Empty d values, no wavelength, return empty arrays
147+ ([None , np .empty ((0 ))], np .empty ((0 ))),
148+ # UC2: Empty d values, wavelength specified, return empty arrays
149+ ([4 * np .pi , np .empty ((0 ))], np .empty (0 )),
150+ # UC3: User specified valid d values, no wavelength, return empty arrays
151+ (
152+ [None , np .array ([0 , 0.2 , 0.4 , 0.6 , 0.8 , 1 ])],
153+ np .array ([0 , 1 , 2 , 3 , 4 , 5 ]),
154+ ),
155+ # UC4: User specified valid d values (with wavelength)
156+ (
157+ [4 * np .pi , np .array ([4 * np .pi , 4 / np .sqrt (2 ) * np .pi , 4 / np .sqrt (3 ) * np .pi ])],
158+ np .array ([60.0 , 90.0 , 120.0 ]),
159+ ),
160+ ]
161+
162+
163+ @pytest .mark .parametrize ("inputs, expected" , params_d_to_tth )
164+ def test_d_to_tth (inputs , expected ):
165+ actual = d_to_tth (inputs [1 ], inputs [0 ])
166+ assert np .allclose (expected , actual )
167+
168+
169+ params_d_to_tth_bad = [
170+ # UC1: user specified invalid d values that result in tth > 180 degrees
171+ (
172+ [4 * np .pi , np .array ([0.2 , 0.4 , 0.6 , 0.8 , 1 , 1.2 ])],
173+ [
174+ ValueError ,
175+ "The supplied input array and wavelength will result in an impossible two-theta. "
176+ "Please check these values and re-instantiate the DiffractionObject with correct values." ,
177+ ],
178+ ),
179+ # UC2: user specified a wrong wavelength that result in tth > 180 degrees
180+ (
181+ [100 , np .array ([0 , 0.2 , 0.4 , 0.6 , 0.8 , 1 ])],
182+ [
183+ ValueError ,
184+ "The supplied input array and wavelength will result in an impossible two-theta. "
185+ "Please check these values and re-instantiate the DiffractionObject with correct values." ,
186+ ],
187+ ),
188+ ]
189+
190+
191+ @pytest .mark .parametrize ("inputs, expected" , params_d_to_tth_bad )
192+ def test_d_to_tth_bad (inputs , expected ):
193+ with pytest .raises (expected [0 ], match = expected [1 ]):
194+ d_to_tth (inputs [1 ], inputs [0 ])
0 commit comments