From 82078abc9694c475b9abbf2ed7e6c08f8004d2f3 Mon Sep 17 00:00:00 2001 From: yucongalicechen Date: Fri, 14 Jun 2024 09:32:21 -0400 Subject: [PATCH 1/4] initial commit, tests pass locally with a minor tweak in function --- src/diffpy/labpdfproc/functions.py | 2 +- src/diffpy/labpdfproc/tests/test_functions.py | 30 ++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/diffpy/labpdfproc/functions.py b/src/diffpy/labpdfproc/functions.py index 494be9b..30f833d 100644 --- a/src/diffpy/labpdfproc/functions.py +++ b/src/diffpy/labpdfproc/functions.py @@ -197,7 +197,7 @@ def compute_cve(diffraction_data, mud, wavelength): """ mu_sample_invmm = mud / 2 - abs_correction = Gridded_circle(mu=mu_sample_invmm) + abs_correction = Gridded_circle(n_points_on_diameter=N_POINTS_ON_DIAMETER, mu=mu_sample_invmm) distances, muls = [], [] for angle in TTH_GRID: abs_correction.set_distances_at_angle(angle) diff --git a/src/diffpy/labpdfproc/tests/test_functions.py b/src/diffpy/labpdfproc/tests/test_functions.py index c51baf3..35283e2 100644 --- a/src/diffpy/labpdfproc/tests/test_functions.py +++ b/src/diffpy/labpdfproc/tests/test_functions.py @@ -1,6 +1,8 @@ +import numpy as np import pytest -from diffpy.labpdfproc.functions import Gridded_circle +from diffpy.labpdfproc.functions import Gridded_circle, compute_cve +from diffpy.utils.scattering_objects.diffraction_objects import Diffraction_object params1 = [ ([0.5, 3, 1], {(0.0, -0.5), (0.0, 0.0), (0.5, 0.0), (-0.5, 0.0), (0.0, 0.5)}), @@ -52,3 +54,29 @@ def test_set_muls_at_angle(inputs, expected): actual_muls_sorted = sorted(actual_gs.muls) expected_muls_sorted = sorted(expected_muls) assert actual_muls_sorted == pytest.approx(expected_muls_sorted, rel=1e-4, abs=1e-6) + + +def test_compute_cve(monkeypatch): + monkeypatch.setattr("diffpy.labpdfproc.functions.N_POINTS_ON_DIAMETER", int(4)) + monkeypatch.setattr("diffpy.labpdfproc.functions.TTH_GRID", np.array([45, 60, 90])) + input_pattern = Diffraction_object(wavelength=1.54) + input_pattern.insert_scattering_quantity( + np.array([45, 60, 90]), + np.array([2.2, 3, 4]), + "tth", + scat_quantity="x-ray", + name="test", + metadata={"thing1": 1, "thing2": "thing2"}, + ) + actual_abdo = compute_cve(input_pattern, mud=1, wavelength=1.54) + expected_abdo = Diffraction_object() + expected_abdo.insert_scattering_quantity( + np.array([45, 60, 90]), + np.array([2.54253048, 2.52852515, 2.49717207]), + "tth", + metadata={"thing1": 1, "thing2": "thing2"}, + name="absorption correction, cve, for test", + wavelength=1.54, + scat_quantity="cve", + ) + assert actual_abdo == expected_abdo From 3bc2ac2da42323b0778efa44a70ce7582a31d994 Mon Sep 17 00:00:00 2001 From: yucongalicechen Date: Fri, 14 Jun 2024 11:23:49 -0400 Subject: [PATCH 2/4] change mocking to pytest mocker --- requirements/requirements-dev.txt | 1 + src/diffpy/labpdfproc/tests/test_functions.py | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/requirements/requirements-dev.txt b/requirements/requirements-dev.txt index 3b388e1..c601d11 100644 --- a/requirements/requirements-dev.txt +++ b/requirements/requirements-dev.txt @@ -9,6 +9,7 @@ nbstripout pre-commit pre-commit-hooks pytest +pytest-mock sphinx twine # These are dependencies of various sphinx extensions for documentation. diff --git a/src/diffpy/labpdfproc/tests/test_functions.py b/src/diffpy/labpdfproc/tests/test_functions.py index 35283e2..310cbc6 100644 --- a/src/diffpy/labpdfproc/tests/test_functions.py +++ b/src/diffpy/labpdfproc/tests/test_functions.py @@ -56,9 +56,9 @@ def test_set_muls_at_angle(inputs, expected): assert actual_muls_sorted == pytest.approx(expected_muls_sorted, rel=1e-4, abs=1e-6) -def test_compute_cve(monkeypatch): - monkeypatch.setattr("diffpy.labpdfproc.functions.N_POINTS_ON_DIAMETER", int(4)) - monkeypatch.setattr("diffpy.labpdfproc.functions.TTH_GRID", np.array([45, 60, 90])) +def test_compute_cve(mocker): + mocker.patch("diffpy.labpdfproc.functions.N_POINTS_ON_DIAMETER", 4) + mocker.patch("diffpy.labpdfproc.functions.TTH_GRID", np.array([45, 60, 90])) input_pattern = Diffraction_object(wavelength=1.54) input_pattern.insert_scattering_quantity( np.array([45, 60, 90]), From 2c336a2f09793c48eb86c52ca1fee5e7d9dcc9be Mon Sep 17 00:00:00 2001 From: yucongalicechen Date: Mon, 17 Jun 2024 15:48:52 -0400 Subject: [PATCH 3/4] tweaked test for tolerance --- src/diffpy/labpdfproc/tests/test_functions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/diffpy/labpdfproc/tests/test_functions.py b/src/diffpy/labpdfproc/tests/test_functions.py index 310cbc6..f969c83 100644 --- a/src/diffpy/labpdfproc/tests/test_functions.py +++ b/src/diffpy/labpdfproc/tests/test_functions.py @@ -72,7 +72,7 @@ def test_compute_cve(mocker): expected_abdo = Diffraction_object() expected_abdo.insert_scattering_quantity( np.array([45, 60, 90]), - np.array([2.54253048, 2.52852515, 2.49717207]), + np.array([2.54253, 2.52852, 2.49717]), "tth", metadata={"thing1": 1, "thing2": "thing2"}, name="absorption correction, cve, for test", From 085bdfc605ee5ed280e601058b47101234d6bcbb Mon Sep 17 00:00:00 2001 From: yucongalicechen Date: Wed, 19 Jun 2024 09:55:29 -0400 Subject: [PATCH 4/4] update CI workflow to include development requirements --- .github/workflows/testing.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index fc2023b..c73ebb6 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -43,7 +43,7 @@ jobs: conda config --add channels conda-forge conda activate test conda install --file requirements/requirements.txt - conda install pytest coverage codecov + conda install --file requirements/requirements-dev.txt pip install . - name: Validate diffpy.labpdfproc