Skip to content

Commit e6827a6

Browse files
authored
Merge pull request #197 from yucongalicechen/qd
d_to_q, q_to_d
2 parents 298e630 + 39f1c54 commit e6827a6

File tree

3 files changed

+103
-14
lines changed

3 files changed

+103
-14
lines changed

news/d-q.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* functions to convert between d and q
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

src/diffpy/utils/transforms.py

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
"The supplied q-array and wavelength will result in an impossible two-theta. "
1717
"Please check these values and re-instantiate the DiffractionObject with correct values."
1818
)
19+
inf_output_wmsg = (
20+
"INFO: The largest d-value in the array is infinite. This is allowed, but it will not be plotted."
21+
)
1922

2023

2124
def _validate_inputs(q, wavelength):
@@ -58,7 +61,6 @@ def q_to_tth(q, wavelength):
5861
-------
5962
tth : 1D array
6063
The array of :math:`2\theta` values in degrees numpy.array([tths]).
61-
This is the correct format for loading into diffpy.utils.DiffractionOject.on_tth
6264
"""
6365
_validate_inputs(q, wavelength)
6466
q.astype(float)
@@ -92,21 +94,18 @@ def tth_to_q(tth, wavelength):
9294
9395
Parameters
9496
----------
95-
tth : 2D array
96-
The array of :math:`2\theta` values and :math: 'i' intensity values, np.array([[tths], [is]]).
97-
This is the same format as, and so can accept, diffpy.utils.DiffractionOject.on_tth
97+
tth : 1D array
98+
The array of :math:`2\theta` values np.array([tths]).
9899
The units of tth are expected in degrees.
99100
100101
wavelength : float
101102
Wavelength of the incoming x-rays/neutrons/electrons
102103
103104
Returns
104105
-------
105-
on_q : 2D array
106-
The array of :math:`q` values and :math: 'i' intensity values unchanged,
107-
np.array([[qs], [is]]).
106+
q : 1D array
107+
The array of :math:`q` values np.array([qs]).
108108
The units for the q-values are the inverse of the units of the provided wavelength.
109-
This is the correct format for loading into diffpy.utils.DiffractionOject.on_q
110109
"""
111110
tth.astype(float)
112111
if np.any(np.deg2rad(tth) > np.pi):
@@ -116,22 +115,55 @@ def tth_to_q(tth, wavelength):
116115
pre_factor = (4.0 * np.pi) / wavelength
117116
q = pre_factor * np.sin(np.deg2rad(tth / 2))
118117
else: # return intensities vs. an x-array that is just the index
118+
warnings.warn(wavelength_warning_emsg, UserWarning)
119119
for i, _ in enumerate(q):
120120
q[i] = i
121121
return q
122122

123123

124-
def q_to_d(qarray):
125-
return 2.0 * np.pi / copy(qarray)
124+
def q_to_d(q):
125+
r"""
126+
Helper function to convert q to d on independent variable axis, using :math:`d = \frac{2 \pi}{q}`.
127+
128+
Parameters
129+
----------
130+
q : 1D array
131+
The array of :math:`q` values np.array([qs]).
132+
The units of q must be reciprocal of the units of wavelength.
133+
134+
Returns
135+
-------
136+
d : 1D array
137+
The array of :math:`d` values np.array([ds]).
138+
"""
139+
if 0 in q:
140+
print(inf_output_wmsg)
141+
return 2.0 * np.pi / copy(q)
126142

127143

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

132148

133-
def d_to_q(darray):
134-
return 2.0 * np.pi / copy(darray)
149+
def d_to_q(d):
150+
r"""
151+
Helper function to convert q to d using :math:`d = \frac{2 \pi}{q}`.
152+
153+
Parameters
154+
----------
155+
d : 1D array
156+
The array of :math:`d` values np.array([ds]).
157+
158+
Returns
159+
-------
160+
q : 1D array
161+
The array of :math:`q` values np.array([qs]).
162+
The units of q must be reciprocal of the units of wavelength.
163+
"""
164+
if 0 in d:
165+
warnings.warn(inf_output_wmsg)
166+
return 2.0 * np.pi / copy(d)
135167

136168

137169
def d_to_tth(darray, wavelength):

tests/test_transforms.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22
import pytest
33

4-
from diffpy.utils.transforms import q_to_tth, tth_to_q
4+
from diffpy.utils.transforms import d_to_q, q_to_d, q_to_tth, tth_to_q
55

66
params_q_to_tth = [
77
# UC1: Empty q values, no wavelength, return empty arrays
@@ -64,7 +64,7 @@ def test_q_to_tth_bad(inputs, expected):
6464
np.array([0, 1, 2, 3, 4, 5]),
6565
),
6666
# UC3: User specified valid tth values between 0-180 degrees (with wavelength)
67-
# expected q vales are sin15, sin30, sin45, sin60, sin90
67+
# expected q values are sin15, sin30, sin45, sin60, sin90
6868
(
6969
[4 * np.pi, np.array([0, 30.0, 60.0, 90.0, 120.0, 180.0])],
7070
np.array([0, 0.258819, 0.5, 0.707107, 0.866025, 1]),
@@ -96,3 +96,37 @@ def test_tth_to_q(inputs, expected):
9696
def test_tth_to_q_bad(inputs, expected):
9797
with pytest.raises(expected[0], match=expected[1]):
9898
tth_to_q(inputs[1], inputs[0])
99+
100+
101+
params_q_to_d = [
102+
# UC1: User specified empty q values
103+
([np.array([])], np.array([])),
104+
# UC2: User specified valid q values
105+
(
106+
[np.array([5 * np.pi, 4 * np.pi, 3 * np.pi, 2 * np.pi, np.pi, 0])],
107+
np.array([0.4, 0.5, 0.66667, 1, 2, np.inf]),
108+
),
109+
]
110+
111+
112+
@pytest.mark.parametrize("inputs, expected", params_q_to_d)
113+
def test_q_to_d(inputs, expected):
114+
actual = q_to_d(inputs[0])
115+
assert np.allclose(actual, expected)
116+
117+
118+
params_d_to_q = [
119+
# UC1: User specified empty d values
120+
([np.array([])], np.array([])),
121+
# UC2: User specified valid d values
122+
(
123+
[np.array([0, 1 * np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi])],
124+
np.array([np.inf, 2, 1, 0.66667, 0.5, 0.4]),
125+
),
126+
]
127+
128+
129+
@pytest.mark.parametrize("inputs, expected", params_d_to_q)
130+
def test_d_to_q(inputs, expected):
131+
actual = d_to_q(inputs[0])
132+
assert np.allclose(actual, expected)

0 commit comments

Comments
 (0)