1+ import re
2+
13import numpy as np
24import pytest
35
68params_q_to_tth = [
79 # UC1: Empty q values, no wavelength, return empty arrays
810 (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
1012 (4 * np .pi , np .empty ((0 )), np .empty (0 )),
1113 # UC3: User specified valid q values, no wavelength, return empty arrays
1214 (
2325@pytest .mark .parametrize ("wavelength, q, expected_tth" , params_q_to_tth )
2426def test_q_to_tth (wavelength , q , expected_tth ):
2527
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+ )
2634 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 )
2837 actual_tth = q_to_tth (q , wavelength )
2938 else :
3039 actual_tth = q_to_tth (q , wavelength )
@@ -35,54 +44,54 @@ def test_q_to_tth(wavelength, q, expected_tth):
3544params_q_to_tth_bad = [
3645 # UC1: user specified invalid q values that result in tth > 180 degrees
3746 (
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." ,
4452 ),
4553 # UC2: user specified a wrong wavelength that result in tth > 180 degrees
4654 (
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." ,
5360 ),
5461]
5562
5663
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 )
6168
6269
6370params_tth_to_q = [
6471 # UC0: User specified empty tth values (without wavelength)
65- ([ None , np .array ([])] , np .array ([])),
72+ (None , np .array ([]), np .array ([])),
6673 # UC1: User specified empty tth values (with wavelength)
67- ([ 4 * np .pi , np .array ([])] , np .array ([])),
74+ (4 * np .pi , np .array ([]), np .array ([])),
6875 # UC2: User specified valid tth values between 0-180 degrees (without wavelength)
6976 (
70- [None , np .array ([0 , 30 , 60 , 90 , 120 , 180 ])],
77+ None ,
78+ np .array ([0 , 30 , 60 , 90 , 120 , 180 ]),
7179 np .array ([0 , 1 , 2 , 3 , 4 , 5 ]),
7280 ),
7381 # UC3: User specified valid tth values between 0-180 degrees (with wavelength)
7482 # expected q values are sin15, sin30, sin45, sin60, sin90
7583 (
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 ]),
7786 np .array ([0 , 0.258819 , 0.5 , 0.707107 , 0.866025 , 1 ]),
7887 ),
7988]
8089
8190
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 )
8695
8796
8897params_tth_to_q_bad = [
0 commit comments