Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions src/diffpy/utils/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
"The supplied q-array and wavelength will result in an impossible two-theta. "
"Please check these values and re-instantiate the DiffractionObject with correct values."
)
invalid_input_emsg = (
"Input values have resulted in an infinite output. Please ensure there are no zeros in the input."
)
inf_output_msg = "WARNING: The largest output is infinite and cannot be plotted."
Copy link
Contributor

Choose a reason for hiding this comment

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

@sbillinge out of curiosity,

for both error and warning messages, do we still want to maintain our convention of 1) reason for error, (2) what to do to fix it ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I tweaked this a bit and downgraded it to an INFO from a WARNING....don't want users to freak out....



def _validate_inputs(q, wavelength):
Expand Down Expand Up @@ -61,7 +59,6 @@ def q_to_tth(q, wavelength):
-------
tth : 1D array
The array of :math:`2\theta` values in degrees numpy.array([tths]).
This is the correct format for loading into diffpy.utils.DiffractionObject.on_tth.
"""
_validate_inputs(q, wavelength)
q.astype(float)
Expand Down Expand Up @@ -107,7 +104,6 @@ def tth_to_q(tth, wavelength):
q : 1D array
The array of :math:`q` values np.array([qs]).
The units for the q-values are the inverse of the units of the provided wavelength.
This is the correct format for loading into diffpy.utils.DiffractionObject.on_q.
"""
tth.astype(float)
if np.any(np.deg2rad(tth) > np.pi):
Expand Down Expand Up @@ -138,8 +134,8 @@ def q_to_d(q):
The array of :math:`d` values np.array([ds]).
"""
if 0 in q:
raise ValueError(invalid_input_emsg)
return 2.0 * np.pi / copy(q)[::-1]
warnings.warn(inf_output_msg)
return 2.0 * np.pi / copy(q)


def tth_to_d(ttharray, wavelength):
Expand All @@ -163,8 +159,8 @@ def d_to_q(d):
The units of q must be reciprocal of the units of wavelength.
"""
if 0 in d:
raise ValueError(invalid_input_emsg)
return 2.0 * np.pi / copy(d)[::-1]
warnings.warn(inf_output_msg)
return 2.0 * np.pi / copy(d)


def d_to_tth(darray, wavelength):
Expand Down
44 changes: 4 additions & 40 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ def test_tth_to_q_bad(inputs, expected):
([np.array([])], np.array([])),
# UC2: User specified valid q values
(
[np.array([np.pi / 6, 1 * np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi])],
np.array([0.4, 0.5, 0.66667, 1, 2, 12]),
[np.array([5 * np.pi, 4 * np.pi, 3 * np.pi, 2 * np.pi, np.pi, 0])],
np.array([0.4, 0.5, 0.66667, 1, 2, np.inf]),
),
]

Expand All @@ -115,31 +115,13 @@ def test_q_to_d(inputs, expected):
assert np.allclose(actual, expected)


params_q_to_d_bad = [
# UC1: user specified an invalid q value that results in an infinite d value
(
[np.array([0, 1, 2, 3, 4])],
[
ValueError,
"Input values have resulted in an infinite output. Please ensure there are no zeros in the input.",
],
),
]


@pytest.mark.parametrize("inputs, expected", params_q_to_d_bad)
def test_q_to_d_bad(inputs, expected):
with pytest.raises(expected[0], match=expected[1]):
q_to_d(inputs[0])


params_d_to_q = [
# UC1: User specified empty d values
([np.array([])], np.array([])),
# UC2: User specified valid d values
(
[np.array([np.pi / 6, 1 * np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi])],
np.array([0.4, 0.5, 0.66667, 1, 2, 12]),
[np.array([0, 1 * np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi])],
np.array([np.inf, 2, 1, 0.66667, 0.5, 0.4]),
),
]

Expand All @@ -148,21 +130,3 @@ def test_q_to_d_bad(inputs, expected):
def test_d_to_q(inputs, expected):
actual = d_to_q(inputs[0])
assert np.allclose(actual, expected)


params_d_to_q_bad = [
# UC1: user specified an invalid d value that results in an infinite q value
(
[np.array([0, 1, 2, 3, 4])],
[
ValueError,
"Input values have resulted in an infinite output. Please ensure there are no zeros in the input.",
],
),
]


@pytest.mark.parametrize("inputs, expected", params_d_to_q_bad)
def test_d_to_q_bad(inputs, expected):
with pytest.raises(expected[0], match=expected[1]):
d_to_q(inputs[0])
Loading