Skip to content

d_to_q, q_to_d #197

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 7 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
53 changes: 41 additions & 12 deletions src/diffpy/utils/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +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."
)
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 @@ -58,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.DiffractionOject.on_tth
"""
_validate_inputs(q, wavelength)
q.astype(float)
Expand Down Expand Up @@ -92,21 +92,18 @@ def tth_to_q(tth, wavelength):

Parameters
----------
tth : 2D array
The array of :math:`2\theta` values and :math: 'i' intensity values, np.array([[tths], [is]]).
This is the same format as, and so can accept, diffpy.utils.DiffractionOject.on_tth
tth : 1D array
The array of :math:`2\theta` values np.array([tths]).
The units of tth are expected in degrees.

wavelength : float
Wavelength of the incoming x-rays/neutrons/electrons

Returns
-------
on_q : 2D array
The array of :math:`q` values and :math: 'i' intensity values unchanged,
np.array([[qs], [is]]).
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.DiffractionOject.on_q
"""
tth.astype(float)
if np.any(np.deg2rad(tth) > np.pi):
Expand All @@ -121,17 +118,49 @@ def tth_to_q(tth, wavelength):
return q


def q_to_d(qarray):
return 2.0 * np.pi / copy(qarray)
def q_to_d(q):
r"""
Helper function to convert q to d on independent variable axis, using :math:`d = \frac{2 \pi}{q}`.

Parameters
----------
q : 1D array
The array of :math:`q` values np.array([qs]).
The units of q must be reciprocal of the units of wavelength.

Returns
-------
d : 1D array
The array of :math:`d` values np.array([ds]).
"""
if 0 in q:
warnings.warn(inf_output_msg)
return 2.0 * np.pi / copy(q)


def tth_to_d(ttharray, wavelength):
qarray = tth_to_q(ttharray, wavelength)
return 2.0 * np.pi / copy(qarray)


def d_to_q(darray):
return 2.0 * np.pi / copy(darray)
def d_to_q(d):
r"""
Helper function to convert q to d using :math:`d = \frac{2 \pi}{q}`.

Parameters
----------
d : 1D array
The array of :math:`d` values np.array([ds]).

Returns
-------
q : 1D array
The array of :math:`q` values np.array([qs]).
The units of q must be reciprocal of the units of wavelength.
"""
if 0 in d:
warnings.warn(inf_output_msg)
return 2.0 * np.pi / copy(d)


def d_to_tth(darray, wavelength):
Expand Down
38 changes: 36 additions & 2 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import pytest

from diffpy.utils.transforms import q_to_tth, tth_to_q
from diffpy.utils.transforms import d_to_q, q_to_d, q_to_tth, tth_to_q

params_q_to_tth = [
# UC1: Empty q values, no wavelength, return empty arrays
Expand Down Expand Up @@ -64,7 +64,7 @@ def test_q_to_tth_bad(inputs, expected):
np.array([0, 1, 2, 3, 4, 5]),
),
# UC3: User specified valid tth values between 0-180 degrees (with wavelength)
# expected q vales are sin15, sin30, sin45, sin60, sin90
# 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])],
np.array([0, 0.258819, 0.5, 0.707107, 0.866025, 1]),
Expand Down Expand Up @@ -96,3 +96,37 @@ def test_tth_to_q(inputs, expected):
def test_tth_to_q_bad(inputs, expected):
with pytest.raises(expected[0], match=expected[1]):
tth_to_q(inputs[1], inputs[0])


params_q_to_d = [
# UC1: User specified empty q values
([np.array([])], np.array([])),
# UC2: User specified valid q values
(
[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]),
),
]


@pytest.mark.parametrize("inputs, expected", params_q_to_d)
def test_q_to_d(inputs, expected):
actual = q_to_d(inputs[0])
assert np.allclose(actual, expected)


params_d_to_q = [
# UC1: User specified empty d values
([np.array([])], np.array([])),
# UC2: User specified valid d values
(
[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]),
),
]


@pytest.mark.parametrize("inputs, expected", params_d_to_q)
def test_d_to_q(inputs, expected):
actual = d_to_q(inputs[0])
assert np.allclose(actual, expected)
Loading