|
| 1 | +import re |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | + |
| 6 | +from diffpy.labpdfproc.functions import CVE_METHODS, Gridded_circle, apply_corr, compute_cve |
| 7 | +from diffpy.utils.scattering_objects.diffraction_objects import Diffraction_object |
| 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 | + |
| 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]) |
| 19 | + actual_grid_sorted = sorted(actual_gs.grid) |
| 20 | + expected_grid_sorted = sorted(expected_grid) |
| 21 | + for actual_point, expected_point in zip(actual_grid_sorted, expected_grid_sorted): |
| 22 | + assert actual_point == pytest.approx(expected_point, rel=1e-4, abs=1e-6) |
| 23 | + |
| 24 | + |
| 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]) |
| 40 | + actual_distances_sorted = sorted(actual_gs.distances) |
| 41 | + expected_distances_sorted = sorted(expected_distances) |
| 42 | + assert actual_distances_sorted == pytest.approx(expected_distances_sorted, rel=1e-4, abs=1e-6) |
| 43 | + |
| 44 | + |
| 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]) |
| 55 | + actual_gs.set_muls_at_angle(120) |
| 56 | + actual_muls_sorted = sorted(actual_gs.muls) |
| 57 | + expected_muls_sorted = sorted(expected_muls) |
| 58 | + assert actual_muls_sorted == pytest.approx(expected_muls_sorted, rel=1e-4, abs=1e-6) |
| 59 | + |
| 60 | + |
| 61 | +def _instantiate_test_do(xarray, yarray, name="test", scat_quantity="x-ray"): |
| 62 | + test_do = Diffraction_object(wavelength=1.54) |
| 63 | + test_do.insert_scattering_quantity( |
| 64 | + xarray, |
| 65 | + yarray, |
| 66 | + "tth", |
| 67 | + scat_quantity=scat_quantity, |
| 68 | + name=name, |
| 69 | + metadata={"thing1": 1, "thing2": "thing2"}, |
| 70 | + ) |
| 71 | + return test_do |
| 72 | + |
| 73 | + |
| 74 | +def test_compute_cve(mocker): |
| 75 | + xarray, yarray = np.array([90, 90.1, 90.2]), np.array([2, 2, 2]) |
| 76 | + expected_cve = np.array([0.5, 0.5, 0.5]) |
| 77 | + mocker.patch("diffpy.labpdfproc.functions.TTH_GRID", xarray) |
| 78 | + mocker.patch("numpy.interp", return_value=expected_cve) |
| 79 | + input_pattern = _instantiate_test_do(xarray, yarray) |
| 80 | + actual_cve_do = compute_cve(input_pattern, mud=1) |
| 81 | + expected_cve_do = _instantiate_test_do( |
| 82 | + xarray, |
| 83 | + expected_cve, |
| 84 | + name="absorption correction, cve, for test", |
| 85 | + scat_quantity="cve", |
| 86 | + ) |
| 87 | + assert actual_cve_do == expected_cve_do |
| 88 | + |
| 89 | + |
| 90 | +params_cve_bad = [ |
| 91 | + ( |
| 92 | + [7, "polynomial_interpolation"], |
| 93 | + [ |
| 94 | + f"mu*D is out of the acceptable range (0.5 to 6) for polynomial interpolation. " |
| 95 | + f"Please rerun with a value within this range or specifying another method from {* CVE_METHODS, }." |
| 96 | + ], |
| 97 | + ), |
| 98 | + ([1, "invalid_method"], [f"Unknown method: invalid_method. Allowed methods are {*CVE_METHODS, }."]), |
| 99 | + ([7, "invalid_method"], [f"Unknown method: invalid_method. Allowed methods are {*CVE_METHODS, }."]), |
| 100 | +] |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.parametrize("inputs, msg", params_cve_bad) |
| 104 | +def test_compute_cve_bad(mocker, inputs, msg): |
| 105 | + xarray, yarray = np.array([90, 90.1, 90.2]), np.array([2, 2, 2]) |
| 106 | + expected_cve = np.array([0.5, 0.5, 0.5]) |
| 107 | + mocker.patch("diffpy.labpdfproc.functions.TTH_GRID", xarray) |
| 108 | + mocker.patch("numpy.interp", return_value=expected_cve) |
| 109 | + input_pattern = _instantiate_test_do(xarray, yarray) |
| 110 | + with pytest.raises(ValueError, match=re.escape(msg[0])): |
| 111 | + compute_cve(input_pattern, mud=inputs[0], method=inputs[1]) |
| 112 | + |
| 113 | + |
| 114 | +def test_apply_corr(mocker): |
| 115 | + xarray, yarray = np.array([90, 90.1, 90.2]), np.array([2, 2, 2]) |
| 116 | + expected_cve = np.array([0.5, 0.5, 0.5]) |
| 117 | + mocker.patch("diffpy.labpdfproc.functions.TTH_GRID", xarray) |
| 118 | + mocker.patch("numpy.interp", return_value=expected_cve) |
| 119 | + input_pattern = _instantiate_test_do(xarray, yarray) |
| 120 | + absorption_correction = _instantiate_test_do( |
| 121 | + xarray, |
| 122 | + expected_cve, |
| 123 | + name="absorption correction, cve, for test", |
| 124 | + scat_quantity="cve", |
| 125 | + ) |
| 126 | + actual_corr = apply_corr(input_pattern, absorption_correction) |
| 127 | + expected_corr = _instantiate_test_do(xarray, np.array([1, 1, 1])) |
| 128 | + assert actual_corr == expected_corr |
0 commit comments