|
6 | 6 | from diffpy.labpdfproc.functions import CVE_METHODS, Gridded_circle, apply_corr, compute_cve
|
7 | 7 | from diffpy.utils.diffraction_objects import DiffractionObject
|
8 | 8 |
|
9 |
| -params1 = [ |
10 |
| - ([0.5, 3, 1], {(0.0, -0.5), (0.0, 0.0), (0.5, 0.0), (-0.5, 0.0), (0.0, 0.5)}), |
11 |
| - ([1, 4, 1], {(-0.333333, -0.333333), (0.333333, -0.333333), (-0.333333, 0.333333), (0.333333, 0.333333)}), |
12 |
| -] |
13 | 9 |
|
14 |
| - |
15 |
| -@pytest.mark.parametrize("inputs, expected", params1) |
16 |
| -def test_get_grid_points(inputs, expected): |
17 |
| - expected_grid = expected |
18 |
| - actual_gs = Gridded_circle(radius=inputs[0], n_points_on_diameter=inputs[1], mu=inputs[2]) |
| 10 | +@pytest.mark.parametrize( |
| 11 | + "inputs, expected_grid", |
| 12 | + [ |
| 13 | + ( |
| 14 | + {"radius": 0.5, "n_points_on_diameter": 3, "mu": 1}, |
| 15 | + {(0.0, -0.5), (0.0, 0.0), (0.5, 0.0), (-0.5, 0.0), (0.0, 0.5)}, |
| 16 | + ), |
| 17 | + ( |
| 18 | + {"radius": 1, "n_points_on_diameter": 4, "mu": 1}, |
| 19 | + {(-0.333333, -0.333333), (0.333333, -0.333333), (-0.333333, 0.333333), (0.333333, 0.333333)}, |
| 20 | + ), |
| 21 | + ], |
| 22 | +) |
| 23 | +def test_get_grid_points(inputs, expected_grid): |
| 24 | + actual_gs = Gridded_circle( |
| 25 | + radius=inputs["radius"], n_points_on_diameter=inputs["n_points_on_diameter"], mu=inputs["mu"] |
| 26 | + ) |
19 | 27 | actual_grid_sorted = sorted(actual_gs.grid)
|
20 | 28 | expected_grid_sorted = sorted(expected_grid)
|
21 | 29 | for actual_point, expected_point in zip(actual_grid_sorted, expected_grid_sorted):
|
22 | 30 | assert actual_point == pytest.approx(expected_point, rel=1e-4, abs=1e-6)
|
23 | 31 |
|
24 | 32 |
|
25 |
| -params2 = [ |
26 |
| - ([1, 3, 1, 45], [0, 1.4142135, 1.4142135, 2, 2]), |
27 |
| - ([1, 3, 1, 90], [0, 0, 2, 2, 2]), |
28 |
| - ([1, 3, 1, 120], [0, 0, 2, 3, 1.73205]), |
29 |
| - ([1, 4, 1, 30], [2.057347, 2.044451, 1.621801, 1.813330]), |
30 |
| - ([1, 4, 1, 90], [1.885618, 1.885618, 2.552285, 1.218951]), |
31 |
| - ([1, 4, 1, 140], [1.139021, 2.200102, 2.744909, 1.451264]), |
32 |
| -] |
33 |
| - |
34 |
| - |
35 |
| -@pytest.mark.parametrize("inputs, expected", params2) |
36 |
| -def test_set_distances_at_angle(inputs, expected): |
37 |
| - expected_distances = expected |
38 |
| - actual_gs = Gridded_circle(radius=inputs[0], n_points_on_diameter=inputs[1], mu=inputs[2]) |
39 |
| - actual_gs.set_distances_at_angle(inputs[3]) |
| 33 | +@pytest.mark.parametrize( |
| 34 | + "inputs, expected_distances", |
| 35 | + [ |
| 36 | + ({"radius": 1, "n_points_on_diameter": 3, "mu": 1, "angle": 45}, [0, 1.4142135, 1.4142135, 2, 2]), |
| 37 | + ({"radius": 1, "n_points_on_diameter": 3, "mu": 1, "angle": 90}, [0, 0, 2, 2, 2]), |
| 38 | + ({"radius": 1, "n_points_on_diameter": 3, "mu": 1, "angle": 120}, [0, 0, 2, 3, 1.73205]), |
| 39 | + ({"radius": 1, "n_points_on_diameter": 4, "mu": 1, "angle": 30}, [2.057347, 2.044451, 1.621801, 1.813330]), |
| 40 | + ({"radius": 1, "n_points_on_diameter": 4, "mu": 1, "angle": 90}, [1.885618, 1.885618, 2.552285, 1.218951]), |
| 41 | + ( |
| 42 | + {"radius": 1, "n_points_on_diameter": 4, "mu": 1, "angle": 140}, |
| 43 | + [1.139021, 2.200102, 2.744909, 1.451264], |
| 44 | + ), |
| 45 | + ], |
| 46 | +) |
| 47 | +def test_set_distances_at_angle(inputs, expected_distances): |
| 48 | + actual_gs = Gridded_circle( |
| 49 | + radius=inputs["radius"], n_points_on_diameter=inputs["n_points_on_diameter"], mu=inputs["mu"] |
| 50 | + ) |
| 51 | + actual_gs.set_distances_at_angle(inputs["angle"]) |
40 | 52 | actual_distances_sorted = sorted(actual_gs.distances)
|
41 | 53 | expected_distances_sorted = sorted(expected_distances)
|
42 | 54 | assert actual_distances_sorted == pytest.approx(expected_distances_sorted, rel=1e-4, abs=1e-6)
|
43 | 55 |
|
44 | 56 |
|
45 |
| -params3 = [ |
46 |
| - ([1], [1, 1, 0.135335, 0.049787, 0.176921]), |
47 |
| - ([2], [1, 1, 0.018316, 0.002479, 0.031301]), |
48 |
| -] |
49 |
| - |
50 |
| - |
51 |
| -@pytest.mark.parametrize("inputs, expected", params3) |
52 |
| -def test_set_muls_at_angle(inputs, expected): |
53 |
| - expected_muls = expected |
54 |
| - actual_gs = Gridded_circle(radius=1, n_points_on_diameter=3, mu=inputs[0]) |
| 57 | +@pytest.mark.parametrize( |
| 58 | + "input_mu, expected_muls", |
| 59 | + [ |
| 60 | + (1, [1, 1, 0.135335, 0.049787, 0.176921]), |
| 61 | + (2, [1, 1, 0.018316, 0.002479, 0.031301]), |
| 62 | + ], |
| 63 | +) |
| 64 | +def test_set_muls_at_angle(input_mu, expected_muls): |
| 65 | + actual_gs = Gridded_circle(radius=1, n_points_on_diameter=3, mu=input_mu) |
55 | 66 | actual_gs.set_muls_at_angle(120)
|
56 | 67 | actual_muls_sorted = sorted(actual_gs.muls)
|
57 | 68 | expected_muls_sorted = sorted(expected_muls)
|
58 | 69 | assert actual_muls_sorted == pytest.approx(expected_muls_sorted, rel=1e-4, abs=1e-6)
|
59 | 70 |
|
60 | 71 |
|
61 |
| -def _instantiate_test_do(xarray, yarray, xtype="tth", name="test", scat_quantity="x-ray"): |
62 |
| - test_do = DiffractionObject( |
| 72 | +@pytest.mark.parametrize( |
| 73 | + "input_xtype, expected", |
| 74 | + [ |
| 75 | + ("tth", {"xarray": np.array([90, 90.1, 90.2]), "yarray": np.array([0.5, 0.5, 0.5]), "xtype": "tth"}), |
| 76 | + ( |
| 77 | + "q", |
| 78 | + {"xarray": np.array([5.76998, 5.77501, 5.78004]), "yarray": np.array([0.5, 0.5, 0.5]), "xtype": "q"}, |
| 79 | + ), |
| 80 | + ], |
| 81 | +) |
| 82 | +def test_compute_cve(input_xtype, expected, mocker): |
| 83 | + xarray, yarray = np.array([90, 90.1, 90.2]), np.array([2, 2, 2]) |
| 84 | + expected_cve = np.array([0.5, 0.5, 0.5]) |
| 85 | + mocker.patch("numpy.interp", return_value=expected_cve) |
| 86 | + input_pattern = DiffractionObject( |
63 | 87 | xarray=xarray,
|
64 | 88 | yarray=yarray,
|
65 |
| - xtype=xtype, |
| 89 | + xtype="tth", |
66 | 90 | wavelength=1.54,
|
67 |
| - scat_quantity=scat_quantity, |
68 |
| - name=name, |
| 91 | + scat_quantity="x-ray", |
| 92 | + name="test", |
69 | 93 | metadata={"thing1": 1, "thing2": "thing2"},
|
70 | 94 | )
|
71 |
| - return test_do |
72 |
| - |
73 |
| - |
74 |
| -params4 = [ |
75 |
| - (["tth"], [np.array([90, 90.1, 90.2]), np.array([0.5, 0.5, 0.5]), "tth"]), |
76 |
| - (["q"], [np.array([5.76998, 5.77501, 5.78004]), np.array([0.5, 0.5, 0.5]), "q"]), |
77 |
| -] |
78 |
| - |
79 |
| - |
80 |
| -@pytest.mark.parametrize("inputs, expected", params4) |
81 |
| -def test_compute_cve(inputs, expected, mocker): |
82 |
| - xarray, yarray = np.array([90, 90.1, 90.2]), np.array([2, 2, 2]) |
83 |
| - expected_cve = np.array([0.5, 0.5, 0.5]) |
84 |
| - mocker.patch("numpy.interp", return_value=expected_cve) |
85 |
| - input_pattern = _instantiate_test_do(xarray, yarray) |
86 |
| - actual_cve_do = compute_cve(input_pattern, mud=1, method="polynomial_interpolation", xtype=inputs[0]) |
87 |
| - expected_cve_do = _instantiate_test_do( |
88 |
| - xarray=expected[0], |
89 |
| - yarray=expected[1], |
90 |
| - xtype=expected[2], |
91 |
| - name="absorption correction, cve, for test", |
| 95 | + actual_cve_do = compute_cve(input_pattern, mud=1, method="polynomial_interpolation", xtype=input_xtype) |
| 96 | + expected_cve_do = DiffractionObject( |
| 97 | + xarray=expected["xarray"], |
| 98 | + yarray=expected["yarray"], |
| 99 | + xtype=expected["xtype"], |
| 100 | + wavelength=1.54, |
92 | 101 | scat_quantity="cve",
|
| 102 | + name="absorption correction, cve, for test", |
| 103 | + metadata={"thing1": 1, "thing2": "thing2"}, |
93 | 104 | )
|
94 | 105 | assert actual_cve_do == expected_cve_do
|
95 | 106 |
|
96 | 107 |
|
97 |
| -params_cve_bad = [ |
98 |
| - ( |
99 |
| - [7, "polynomial_interpolation"], |
100 |
| - [ |
| 108 | +@pytest.mark.parametrize( |
| 109 | + "inputs, msg", |
| 110 | + [ |
| 111 | + ( |
| 112 | + {"mud": 7, "method": "polynomial_interpolation"}, |
101 | 113 | f"mu*D is out of the acceptable range (0.5 to 6) for polynomial interpolation. "
|
102 |
| - f"Please rerun with a value within this range or specifying another method from {*CVE_METHODS, }." |
103 |
| - ], |
104 |
| - ), |
105 |
| - ([1, "invalid_method"], [f"Unknown method: invalid_method. Allowed methods are {*CVE_METHODS, }."]), |
106 |
| - ([7, "invalid_method"], [f"Unknown method: invalid_method. Allowed methods are {*CVE_METHODS, }."]), |
107 |
| -] |
108 |
| - |
109 |
| - |
110 |
| -@pytest.mark.parametrize("inputs, msg", params_cve_bad) |
| 114 | + f"Please rerun with a value within this range or specifying another method from {*CVE_METHODS, }.", |
| 115 | + ), |
| 116 | + ( |
| 117 | + {"mud": 1, "method": "invalid_method"}, |
| 118 | + f"Unknown method: invalid_method. Allowed methods are {*CVE_METHODS, }.", |
| 119 | + ), |
| 120 | + ( |
| 121 | + {"mud": 7, "method": "invalid_method"}, |
| 122 | + f"Unknown method: invalid_method. Allowed methods are {*CVE_METHODS, }.", |
| 123 | + ), |
| 124 | + ], |
| 125 | +) |
111 | 126 | def test_compute_cve_bad(mocker, inputs, msg):
|
112 | 127 | xarray, yarray = np.array([90, 90.1, 90.2]), np.array([2, 2, 2])
|
113 | 128 | expected_cve = np.array([0.5, 0.5, 0.5])
|
114 | 129 | mocker.patch("diffpy.labpdfproc.functions.TTH_GRID", xarray)
|
115 | 130 | mocker.patch("numpy.interp", return_value=expected_cve)
|
116 |
| - input_pattern = _instantiate_test_do(xarray, yarray) |
117 |
| - with pytest.raises(ValueError, match=re.escape(msg[0])): |
118 |
| - compute_cve(input_pattern, mud=inputs[0], method=inputs[1]) |
| 131 | + input_pattern = DiffractionObject( |
| 132 | + xarray=xarray, |
| 133 | + yarray=yarray, |
| 134 | + xtype="tth", |
| 135 | + wavelength=1.54, |
| 136 | + scat_quantity="x-ray", |
| 137 | + name="test", |
| 138 | + metadata={"thing1": 1, "thing2": "thing2"}, |
| 139 | + ) |
| 140 | + with pytest.raises(ValueError, match=re.escape(msg)): |
| 141 | + compute_cve(input_pattern, mud=inputs["mud"], method=inputs["method"]) |
119 | 142 |
|
120 | 143 |
|
121 | 144 | def test_apply_corr(mocker):
|
122 | 145 | xarray, yarray = np.array([90, 90.1, 90.2]), np.array([2, 2, 2])
|
123 | 146 | expected_cve = np.array([0.5, 0.5, 0.5])
|
124 | 147 | mocker.patch("diffpy.labpdfproc.functions.TTH_GRID", xarray)
|
125 | 148 | mocker.patch("numpy.interp", return_value=expected_cve)
|
126 |
| - input_pattern = _instantiate_test_do(xarray, yarray) |
127 |
| - absorption_correction = _instantiate_test_do( |
| 149 | + input_pattern = DiffractionObject( |
| 150 | + xarray=xarray, |
| 151 | + yarray=yarray, |
| 152 | + xtype="tth", |
| 153 | + wavelength=1.54, |
| 154 | + scat_quantity="x-ray", |
| 155 | + name="test", |
| 156 | + metadata={"thing1": 1, "thing2": "thing2"}, |
| 157 | + ) |
| 158 | + absorption_correction = DiffractionObject( |
128 | 159 | xarray=xarray,
|
129 | 160 | yarray=expected_cve,
|
130 |
| - name="absorption correction, cve, for test", |
| 161 | + xtype="tth", |
| 162 | + wavelength=1.54, |
131 | 163 | scat_quantity="cve",
|
| 164 | + name="absorption correction, cve, for test", |
| 165 | + metadata={"thing1": 1, "thing2": "thing2"}, |
132 | 166 | )
|
133 | 167 | actual_corr = apply_corr(input_pattern, absorption_correction)
|
134 |
| - expected_corr = _instantiate_test_do(xarray, np.array([1, 1, 1])) |
| 168 | + expected_corr = DiffractionObject( |
| 169 | + xarray=xarray, |
| 170 | + yarray=np.array([1, 1, 1]), |
| 171 | + xtype="tth", |
| 172 | + wavelength=1.54, |
| 173 | + scat_quantity="x-ray", |
| 174 | + name="test", |
| 175 | + metadata={"thing1": 1, "thing2": "thing2"}, |
| 176 | + ) |
135 | 177 | assert actual_corr == expected_corr
|
0 commit comments