@@ -231,94 +231,179 @@ def test_diffraction_objects_equality(inputs1, inputs2, expected):
231231 assert (diffraction_object1 == diffraction_object2 ) == expected
232232
233233
234- def test_q_to_tth ():
235- # Valid q values that should result in 0-180 tth values after conversion
234+ def _test_valid_diffraction_objects (actual_diffraction_object , function , expected_array ):
235+ """Checks the behavior of the DiffractionObject:
236+ when there is no wavelength, we expect the correct warning message and output,
237+ otherwise, we only check the output matches the expected array."""
238+ if actual_diffraction_object .wavelength is None :
239+ with pytest .warns (UserWarning ) as warn_record :
240+ getattr (actual_diffraction_object , function )()
241+ assert str (warn_record [0 ].message ) == (
242+ "INFO: no wavelength has been specified. You can continue "
243+ "to use the DiffractionObject but some of its powerful features "
244+ "will not be available. To specify a wavelength, you can use "
245+ "DiffractionObject(wavelength=0.71)."
246+ )
247+ actual_array = getattr (actual_diffraction_object , function )()
248+ return np .allclose (actual_array , expected_array )
249+
250+
251+ params_q_to_tth = [
252+ # UC1: User specified empty q values (without wavelength)
253+ (
254+ [None , [], []],
255+ [[]],
256+ ),
257+ # UC2: User specified empty q values (with wavelength)
258+ (
259+ [4 * np .pi , [], []],
260+ [[]],
261+ ),
262+ # UC3: User specified valid q values (without wavelength)
236263 # expected tth values are 2*arcsin(q) in degrees
237- actual = DiffractionObject (wavelength = 4 * np .pi )
238- setattr (actual , "on_q" , [[0 , 0.2 , 0.4 , 0.6 , 0.8 , 1 ], [1 , 2 , 3 , 4 , 5 , 6 ]])
239- actual_tth = actual .q_to_tth ()
240- expected_tth = [0 , 23.07392 , 47.15636 , 73.73980 , 106.26020 , 180 ]
241- assert np .allclose (actual_tth , expected_tth )
264+ (
265+ [None , [0 , 0.2 , 0.4 , 0.6 , 0.8 , 1 ], [1 , 2 , 3 , 4 , 5 , 6 ]],
266+ [[]],
267+ ),
268+ # UC4: User specified valid q values (with wavelength)
269+ # expected tth values are 2*arcsin(q) in degrees
270+ (
271+ [4 * np .pi , [0 , 0.2 , 0.4 , 0.6 , 0.8 , 1 ], [1 , 2 , 3 , 4 , 5 , 6 ]],
272+ [[0 , 23.07392 , 47.15636 , 73.73980 , 106.26020 , 180 ]],
273+ ),
274+ ]
275+
276+
277+ @pytest .mark .parametrize ("inputs, expected" , params_q_to_tth )
278+ def test_q_to_tth (inputs , expected ):
279+ actual = DiffractionObject (wavelength = inputs [0 ])
280+ actual .on_q = [inputs [1 ], inputs [2 ]]
281+ expected_tth = expected [0 ]
282+ assert _test_valid_diffraction_objects (actual , "q_to_tth" , expected_tth )
242283
243284
244285params_q_to_tth_bad = [
245- # UC1: user did not specify wavelength
286+ # UC1: user specified invalid q values that result in tth > 180 degrees
246287 (
247- [None , [0 , 0.2 , 0.4 , 0.6 , 0.8 , 1 ]],
248- "Wavelength is not specified. Please provide a valid wavelength, "
249- "e.g., DiffractionObject(wavelength=0.71)." ,
288+ [4 * np .pi , [0.2 , 0.4 , 0.6 , 0.8 , 1 , 1.2 ], [1 , 2 , 3 , 4 , 5 , 6 ]],
289+ [
290+ ValueError ,
291+ "The supplied q-array and wavelength will result in an impossible two-theta. "
292+ "Please check these values and re-instantiate the DiffractionObject." ,
293+ ],
250294 ),
251- # UC2: user specified invalid q values that result in tth > 180 degrees
295+ # UC2: user specified a wrong wavelength that result in tth > 180 degrees
252296 (
253- [4 * np .pi , [0.2 , 0.4 , 0.6 , 0.8 , 1 , 1.2 ]],
254- "Wavelength * q > 4 * pi. Please check if you entered an incorrect wavelength or q value." ,
297+ [100 , [0 , 0.2 , 0.4 , 0.6 , 0.8 , 1 ], [1 , 2 , 3 , 4 , 5 , 6 ]],
298+ [
299+ ValueError ,
300+ "The supplied q-array and wavelength will result in an impossible two-theta. "
301+ "Please check these values and re-instantiate the DiffractionObject." ,
302+ ],
255303 ),
256- # UC3: user specified a wrong wavelength that result in tth > 180 degrees
304+ # UC3: user specified a q array that does not match the length of intensity array (without wavelength)
257305 (
258- [100 , [0 , 0.2 , 0.4 , 0.6 , 0.8 , 1 ]],
259- "Wavelength * q > 4 * pi. Please check if you entered an incorrect wavelength or q value." ,
306+ [None , [0 , 0.2 , 0.4 , 0.6 , 0.8 , 1 ], [ 1 , 2 , 3 , 4 , 5 ]],
307+ [ IndexError , "Please ensure q array and intensity array are the same length." ] ,
260308 ),
261- # UC4: user specified an empty q array
262- ([4 * np .pi , []], "Q array is empty. Please provide valid q values." ),
263- # UC5: user specified a non-numeric value in q array
309+ # UC4: user specified a q array that does not match the length of intensity array (with wavelength)
264310 (
265- [4 * np .pi , [0 , 0.2 , 0.4 , 0.6 , 0.8 , "invalid" ]],
266- "Invalid value found in q array. Please ensure all values are numeric." ,
311+ [4 * np .pi , [0 , 0.2 , 0.4 , 0.6 , 0.8 , 1 ], [1 , 2 , 3 , 4 , 5 ]],
312+ [IndexError , "Please ensure q array and intensity array are the same length." ],
313+ ),
314+ # UC5: user specified a non-numeric value in q array (without wavelength)
315+ (
316+ [None , [0 , 0.2 , 0.4 , 0.6 , 0.8 , "invalid" ], [1 , 2 , 3 , 4 , 5 , 6 ]],
317+ [TypeError , "Invalid value found in q array. Please ensure all values are numeric." ],
318+ ),
319+ # UC5: user specified a non-numeric value in q array (with wavelength)
320+ (
321+ [4 * np .pi , [0 , 0.2 , 0.4 , 0.6 , 0.8 , "invalid" ], [1 , 2 , 3 , 4 , 5 , 6 ]],
322+ [TypeError , "Invalid value found in q array. Please ensure all values are numeric." ],
267323 ),
268324]
269325
270326
271327@pytest .mark .parametrize ("inputs, expected" , params_q_to_tth_bad )
272328def test_q_to_tth_bad (inputs , expected ):
273329 actual = DiffractionObject (wavelength = inputs [0 ])
274- setattr ( actual , " on_q" , [inputs [1 ], [ 1 , 2 , 3 , 4 , 5 , 6 ]])
275- with pytest .raises (ValueError ):
330+ actual . on_q = [inputs [1 ], inputs [ 2 ]]
331+ with pytest .raises (expected [ 0 ], match = expected [ 1 ] ):
276332 actual .q_to_tth ()
277333
278334
279- def test_tth_to_q ():
280- # Valid tth values between 0-180 degrees
335+ params_tth_to_q = [
336+ # UC1: User specified empty tth values (without wavelength)
337+ (
338+ [None , [], []],
339+ [[]],
340+ ),
341+ # UC2: User specified empty tth values (with wavelength)
342+ (
343+ [4 * np .pi , [], []],
344+ [[]],
345+ ),
346+ # UC3: User specified valid tth values between 0-180 degrees (without wavelength)
347+ (
348+ [None , [0 , 30 , 60 , 90 , 120 , 180 ], [1 , 2 , 3 , 4 , 5 , 6 ]],
349+ [[]],
350+ ),
351+ # UC4: User specified valid tth values between 0-180 degrees (with wavelength)
281352 # expected q vales are sin15, sin30, sin45, sin60, sin90
282- actual = DiffractionObject (wavelength = 4 * np .pi )
283- setattr (actual , "on_tth" , [[0 , 30 , 60 , 90 , 120 , 180 ], [1 , 2 , 3 , 4 , 5 , 6 ]])
284- actual_q = actual .tth_to_q ()
285- expected_q = [0 , 0.258819 , 0.5 , 0.707107 , 0.866025 , 1 ]
286- assert np .allclose (actual_q , expected_q )
353+ (
354+ [4 * np .pi , [0 , 30 , 60 , 90 , 120 , 180 ], [1 , 2 , 3 , 4 , 5 , 6 ]],
355+ [[0 , 0.258819 , 0.5 , 0.707107 , 0.866025 , 1 ]],
356+ ),
357+ ]
358+
359+
360+ @pytest .mark .parametrize ("inputs, expected" , params_tth_to_q )
361+ def test_tth_to_q (inputs , expected ):
362+ actual = DiffractionObject (wavelength = inputs [0 ])
363+ actual .on_tth = [inputs [1 ], inputs [2 ]]
364+ expected_q = expected [0 ]
365+ assert _test_valid_diffraction_objects (actual , "tth_to_q" , expected_q )
287366
288367
289368params_tth_to_q_bad = [
290- # UC1: user did not specify wavelength
369+ # UC1: user specified an invalid tth value of > 180 degrees (without wavelength)
370+ (
371+ [None , [0 , 30 , 60 , 90 , 120 , 181 ], [1 , 2 , 3 , 4 , 5 , 6 ]],
372+ [ValueError , "Two theta exceeds 180 degrees. Please check the input values for errors." ],
373+ ),
374+ # UC2: user specified an invalid tth value of > 180 degrees (with wavelength)
375+ (
376+ [4 * np .pi , [0 , 30 , 60 , 90 , 120 , 181 ], [1 , 2 , 3 , 4 , 5 , 6 ]],
377+ [ValueError , "Two theta exceeds 180 degrees. Please check the input values for errors." ],
378+ ),
379+ # UC3: user specified a two theta array that does not match the length of intensity array (without wavelength)
291380 (
292- [None , [0 , 30 , 60 , 90 , 120 , 180 ]],
293- "Wavelength is not specified. Please provide a valid wavelength, "
294- "e.g., DiffractionObject(wavelength=0.71)." ,
381+ [None , [0 , 30 , 60 , 90 , 120 ], [1 , 2 , 3 , 4 , 5 , 6 ]],
382+ [IndexError , "Please ensure two theta array and intensity array are the same length." ],
295383 ),
296- # UC2 : user specified an invalid tth value of > 180 degrees
384+ # UC4 : user specified a two theta array that does not match the length of intensity array (with wavelength)
297385 (
298- [4 * np .pi , [0 , 30 , 60 , 90 , 120 , 181 ]],
299- "Two theta exceeds 180 degrees. Please check the input values for errors." ,
386+ [4 * np .pi , [0 , 30 , 60 , 90 , 120 ], [ 1 , 2 , 3 , 4 , 5 , 6 ]],
387+ [ IndexError , "Please ensure two theta array and intensity array are the same length." ] ,
300388 ),
301- # UC3 : user did not specify wavelength and specified invalid tth values
389+ # UC5 : user specified a non-numeric value in two theta array (without wavelength)
302390 (
303- [None , [0 , 30 , 60 , 90 , 120 , 181 ]],
304- "Wavelength is not specified. Please provide a valid wavelength, "
305- "e.g., DiffractionObject(wavelength=0.71)." ,
391+ [None , [0 , 30 , 60 , 90 , 120 , "invalid" ], [1 , 2 , 3 , 4 , 5 , 6 ]],
392+ [TypeError , "Invalid value found in two theta array. Please ensure all values are numeric." ],
306393 ),
307- # UC4: user specified an empty two theta array
308- ([4 * np .pi , []], "Two theta array is empty. Please provide valid two theta values." ),
309- # UC5: user specified a non-numeric value in two theta array
394+ # UC6: user specified a non-numeric value in two theta array (with wavelength)
310395 (
311- [4 * np .pi , [0 , 30 , 60 , 90 , 120 , "invalid" ]],
312- "Invalid value found in two theta array. Please ensure all values are numeric." ,
396+ [4 * np .pi , [0 , 30 , 60 , 90 , 120 , "invalid" ], [ 1 , 2 , 3 , 4 , 5 , 6 ] ],
397+ [ TypeError , "Invalid value found in two theta array. Please ensure all values are numeric." ] ,
313398 ),
314399]
315400
316401
317402@pytest .mark .parametrize ("inputs, expected" , params_tth_to_q_bad )
318403def test_tth_to_q_bad (inputs , expected ):
319404 actual = DiffractionObject (wavelength = inputs [0 ])
320- setattr ( actual , " on_tth" , [inputs [1 ], [ 1 , 2 , 3 , 4 , 5 , 6 ]])
321- with pytest .raises (ValueError , match = expected ):
405+ actual . on_tth = [inputs [1 ], inputs [ 2 ]]
406+ with pytest .raises (expected [ 0 ] , match = expected [ 1 ] ):
322407 actual .tth_to_q ()
323408
324409
0 commit comments