Skip to content

1) Capture no wavelength UserWarning for test_q_to_thh method 2) discuss passing variables to @pytest.mark.parametrize #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 14, 2024
Prev Previous commit
Next Next commit
Use variable names within parametrize for transforms
  • Loading branch information
bobleesj committed Dec 14, 2024
commit 6bb822f574b7189687eb46d4a3a1ce0c62a87281
61 changes: 35 additions & 26 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

import numpy as np
import pytest

Expand All @@ -6,7 +8,7 @@
params_q_to_tth = [
# UC1: Empty q values, no wavelength, return empty arrays
(None, np.empty((0)), np.empty((0))),
# # UC2: Empty q values, wavelength specified, return empty arrays
# UC2: Empty q values, wavelength specified, return empty arrays
(4 * np.pi, np.empty((0)), np.empty(0)),
# UC3: User specified valid q values, no wavelength, return empty arrays
(
Expand All @@ -23,8 +25,15 @@
@pytest.mark.parametrize("wavelength, q, expected_tth", params_q_to_tth)
def test_q_to_tth(wavelength, q, expected_tth):

wavelength_warning_emsg = (
"No wavelength has been specified. You can continue to use the DiffractionObject, but "
"some of its powerful features will not be available. "
"To specify a wavelength, if you have do = DiffractionObject(xarray, yarray, 'tth'), "
"you may set do.wavelength = 1.54 with the unit in angstroms."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see the small tweak above

)
if wavelength is None:
with pytest.warns(UserWarning, match="INFO: no wavelength has been specified"):
with pytest.warns(UserWarning, match=re.escape(wavelength_warning_emsg)):
actual_tth = q_to_tth(q, wavelength)
actual_tth = q_to_tth(q, wavelength)
else:
actual_tth = q_to_tth(q, wavelength)
Expand All @@ -35,54 +44,54 @@ def test_q_to_tth(wavelength, q, expected_tth):
params_q_to_tth_bad = [
# UC1: user specified invalid q values that result in tth > 180 degrees
(
[4 * np.pi, np.array([0.2, 0.4, 0.6, 0.8, 1, 1.2])],
[
ValueError,
"The supplied input array and wavelength will result in an impossible two-theta. "
"Please check these values and re-instantiate the DiffractionObject with correct values.",
],
4 * np.pi,
np.array([0.2, 0.4, 0.6, 0.8, 1, 1.2]),
ValueError,
"The supplied input array and wavelength will result in an impossible two-theta. "
"Please check these values and re-instantiate the DiffractionObject with correct values.",
),
# UC2: user specified a wrong wavelength that result in tth > 180 degrees
(
[100, np.array([0, 0.2, 0.4, 0.6, 0.8, 1])],
[
ValueError,
"The supplied input array and wavelength will result in an impossible two-theta. "
"Please check these values and re-instantiate the DiffractionObject with correct values.",
],
100,
np.array([0, 0.2, 0.4, 0.6, 0.8, 1]),
ValueError,
"The supplied input array and wavelength will result in an impossible two-theta. "
"Please check these values and re-instantiate the DiffractionObject with correct values.",
),
]


@pytest.mark.parametrize("inputs, expected", params_q_to_tth_bad)
def test_q_to_tth_bad(inputs, expected):
with pytest.raises(expected[0], match=expected[1]):
q_to_tth(inputs[1], inputs[0])
@pytest.mark.parametrize("q, wavelength, expected_error_type, expected_error_msg", params_q_to_tth_bad)
def test_q_to_tth_bad(q, wavelength, expected_error_type, expected_error_msg):
with pytest.raises(expected_error_type, match=expected_error_msg):
q_to_tth(wavelength, q)


params_tth_to_q = [
# UC0: User specified empty tth values (without wavelength)
([None, np.array([])], np.array([])),
(None, np.array([]), np.array([])),
# UC1: User specified empty tth values (with wavelength)
([4 * np.pi, np.array([])], np.array([])),
(4 * np.pi, np.array([]), np.array([])),
# UC2: User specified valid tth values between 0-180 degrees (without wavelength)
(
[None, np.array([0, 30, 60, 90, 120, 180])],
None,
np.array([0, 30, 60, 90, 120, 180]),
np.array([0, 1, 2, 3, 4, 5]),
),
# UC3: User specified valid tth values between 0-180 degrees (with wavelength)
# expected q values are sin15, sin30, sin45, sin60, sin90
(
[4 * np.pi, np.array([0, 30.0, 60.0, 90.0, 120.0, 180.0])],
4 * np.pi,
np.array([0, 30.0, 60.0, 90.0, 120.0, 180.0]),
np.array([0, 0.258819, 0.5, 0.707107, 0.866025, 1]),
),
]


@pytest.mark.parametrize("inputs, expected", params_tth_to_q)
def test_tth_to_q(inputs, expected):
actual = tth_to_q(inputs[1], inputs[0])
assert np.allclose(actual, expected)
@pytest.mark.parametrize("wavelength, tth, expected_q", params_tth_to_q)
def test_tth_to_q(wavelength, tth, expected_q):
actual_q = tth_to_q(tth, wavelength)
assert np.allclose(actual_q, expected_q)


params_tth_to_q_bad = [
Expand Down
Loading