Skip to content

Commit e55e44d

Browse files
committed
public functions now operate on 1D x-arrays only for ease of user use
1 parent 114cb65 commit e55e44d

File tree

2 files changed

+40
-42
lines changed

2 files changed

+40
-42
lines changed

src/diffpy/utils/transforms.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
)
1919

2020

21-
def _validate_inputs(on_q, wavelength):
21+
def _validate_inputs(q, wavelength):
2222
if wavelength is None:
2323
warnings.warn(wavelength_warning_emsg, UserWarning)
2424
return np.empty(0)
2525
pre_factor = wavelength / (4 * np.pi)
26-
if np.any(np.abs(on_q[0] * pre_factor) > 1.0):
26+
if np.any(np.abs(q * pre_factor) > 1.0):
2727
raise ValueError(invalid_q_or_wavelength_emsg)
2828

2929

30-
def q_to_tth(on_q, wavelength):
30+
def q_to_tth(q, wavelength):
3131
r"""
3232
Helper function to convert q to two-theta.
3333
@@ -47,34 +47,33 @@ def q_to_tth(on_q, wavelength):
4747
4848
Parameters
4949
----------
50-
on_q : 2D array
51-
The array of :math:`q` values and :math: 'i' intensity values, np.array([[qs], [is]]).
52-
This is the same format as, and so can accept, diffpy.utils.DiffractionOject.on_q
50+
q : 1D array
51+
The array of :math:`q` values numpy.array([qs]).
5352
The units of q must be reciprocal of the units of wavelength.
5453
5554
wavelength : float
5655
Wavelength of the incoming x-rays/neutrons/electrons
5756
5857
Returns
5958
-------
60-
on_tth : 2D array
61-
The array of :math:`2\theta` values in degrees and :math: 'i' intensity values unchanged,
62-
np.array([[tths], [is]]).
59+
tth : 1D array
60+
The array of :math:`2\theta` values in degrees numpy.array([tths]).
6361
This is the correct format for loading into diffpy.utils.DiffractionOject.on_tth
6462
"""
65-
_validate_inputs(on_q, wavelength)
66-
on_q.astype(np.float64)
67-
on_tth = copy(on_q) # initialize output array of same shape
63+
_validate_inputs(q, wavelength)
64+
q.astype(np.float64)
65+
tth = copy(q) # initialize output array of same shape
6866
if wavelength is not None:
69-
on_tth[0] = np.rad2deg(2.0 * np.arcsin(on_q[0] * wavelength / (4 * np.pi)))
67+
tth = np.rad2deg(2.0 * np.arcsin(q * wavelength / (4 * np.pi)))
7068
else: # return intensities vs. an x-array that is just the index
71-
for i, _ in enumerate(on_q[0]):
72-
on_tth[0][i] = i
73-
return on_tth
69+
for i, _ in enumerate(q):
70+
tth[i] = i
71+
return tth
7472

7573

76-
def tth_to_q(on_tth, wavelength):
74+
def tth_to_q(tth, wavelength):
7775
r"""
76+
7877
Helper function to convert two-theta to q on independent variable axis.
7978
8079
If wavelength is missing, returns independent variable axis as integer indexes.
@@ -93,7 +92,7 @@ def tth_to_q(on_tth, wavelength):
9392
9493
Parameters
9594
----------
96-
on_tth : 2D array
95+
tth : 2D array
9796
The array of :math:`2\theta` values and :math: 'i' intensity values, np.array([[tths], [is]]).
9897
This is the same format as, and so can accept, diffpy.utils.DiffractionOject.on_tth
9998
The units of tth are expected in degrees.
@@ -109,14 +108,14 @@ def tth_to_q(on_tth, wavelength):
109108
The units for the q-values are the inverse of the units of the provided wavelength.
110109
This is the correct format for loading into diffpy.utils.DiffractionOject.on_q
111110
"""
112-
on_tth.astype(np.float64)
113-
if np.any(np.deg2rad(on_tth[0]) > np.pi):
111+
tth.astype(np.float64)
112+
if np.any(np.deg2rad(tth) > np.pi):
114113
raise ValueError(invalid_tth_emsg)
115-
on_q = copy(on_tth)
114+
q = copy(tth)
116115
if wavelength is not None:
117116
pre_factor = (4.0 * np.pi) / wavelength
118-
on_q[0] = pre_factor * np.sin(np.deg2rad(on_tth[0] / 2))
117+
q = pre_factor * np.sin(np.deg2rad(tth / 2))
119118
else: # return intensities vs. an x-array that is just the index
120-
for i, _ in enumerate(on_q[0]):
121-
on_q[0][i] = i
122-
return on_q
119+
for i, _ in enumerate(q):
120+
q[i] = i
121+
return q

tests/test_transforms.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,30 @@
55

66
params_q_to_tth = [
77
# UC1: Empty q values, no wavelength, return empty arrays
8-
([None, np.empty((2, 0))], np.empty((2, 0))),
8+
([None, np.empty((1,))], np.empty((1,))),
99
# UC2: Empty q values, wavelength specified, return empty arrays
10-
([4 * np.pi, np.empty((2, 0))], np.empty((2, 0))),
10+
([4 * np.pi, np.empty((1,))], np.empty((1,))),
1111
# UC3: User specified valid q values, no wavelength, return empty arrays
1212
(
13-
[None, np.array([[0, 0.2, 0.4, 0.6, 0.8, 1], [1, 2, 3, 4, 5, 6]])],
14-
np.array([[0, 1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6]]),
13+
[None, np.array([0, 0.2, 0.4, 0.6, 0.8, 1])],
14+
np.array([0, 1, 2, 3, 4, 5]),
1515
),
1616
# UC4: User specified valid q values (with wavelength)
1717
# expected tth values are 2*arcsin(q) in degrees
18-
([4 * np.pi, np.array([[0, 1 / np.sqrt(2), 1.0], [1, 2, 3]])], np.array([[0, 90.0, 180.0], [1, 2, 3]])),
18+
([4 * np.pi, np.array([0, 1 / np.sqrt(2), 1.0])], np.array([0, 90.0, 180.0])),
1919
]
2020

2121

2222
@pytest.mark.parametrize("inputs, expected", params_q_to_tth)
2323
def test_q_to_tth(inputs, expected):
2424
actual = q_to_tth(inputs[1], inputs[0])
25-
assert np.allclose(expected[0], actual[0])
26-
assert np.allclose(expected[1], actual[1])
25+
assert np.allclose(expected, actual)
2726

2827

2928
params_q_to_tth_bad = [
3029
# UC1: user specified invalid q values that result in tth > 180 degrees
3130
(
32-
[4 * np.pi, np.array([[0.2, 0.4, 0.6, 0.8, 1, 1.2], [1, 2, 3, 4, 5, 6]])],
31+
[4 * np.pi, np.array([0.2, 0.4, 0.6, 0.8, 1, 1.2])],
3332
[
3433
ValueError,
3534
"The supplied q-array and wavelength will result in an impossible two-theta. "
@@ -38,7 +37,7 @@ def test_q_to_tth(inputs, expected):
3837
),
3938
# UC2: user specified a wrong wavelength that result in tth > 180 degrees
4039
(
41-
[100, np.array([[0, 0.2, 0.4, 0.6, 0.8, 1], [1, 2, 3, 4, 5, 6]])],
40+
[100, np.array([0, 0.2, 0.4, 0.6, 0.8, 1])],
4241
[
4342
ValueError,
4443
"The supplied q-array and wavelength will result in an impossible two-theta. "
@@ -56,19 +55,19 @@ def test_q_to_tth_bad(inputs, expected):
5655

5756
params_tth_to_q = [
5857
# UC0: User specified empty tth values (without wavelength)
59-
([None, np.array([[], []])], np.array([[], []])),
58+
([None, np.array([])], np.array([])),
6059
# UC1: User specified empty tth values (with wavelength)
61-
([4 * np.pi, np.array([[], []])], np.array([[], []])),
60+
([4 * np.pi, np.array([])], np.array([])),
6261
# UC2: User specified valid tth values between 0-180 degrees (without wavelength)
6362
(
64-
[None, np.array([[0, 30, 60, 90, 120, 180], [1, 2, 3, 4, 5, 6]])],
65-
np.array([[0, 1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6]]),
63+
[None, np.array([0, 30, 60, 90, 120, 180])],
64+
np.array([0, 1, 2, 3, 4, 5]),
6665
),
6766
# UC3: User specified valid tth values between 0-180 degrees (with wavelength)
6867
# expected q vales are sin15, sin30, sin45, sin60, sin90
6968
(
70-
[4 * np.pi, np.array([[0, 30.0, 60.0, 90.0, 120.0, 180.0], [1, 2, 3, 4, 5, 6]])],
71-
np.array([[0, 0.258819, 0.5, 0.707107, 0.866025, 1], [1, 2, 3, 4, 5, 6]]),
69+
[4 * np.pi, np.array([0, 30.0, 60.0, 90.0, 120.0, 180.0])],
70+
np.array([0, 0.258819, 0.5, 0.707107, 0.866025, 1]),
7271
),
7372
]
7473

@@ -82,12 +81,12 @@ def test_tth_to_q(inputs, expected):
8281
params_tth_to_q_bad = [
8382
# UC0: user specified an invalid tth value of > 180 degrees (without wavelength)
8483
(
85-
[None, np.array([[0, 30, 60, 90, 120, 181], [1, 2, 3, 4, 5, 6]])],
84+
[None, np.array([0, 30, 60, 90, 120, 181])],
8685
[ValueError, "Two theta exceeds 180 degrees. Please check the input values for errors."],
8786
),
8887
# UC1: user specified an invalid tth value of > 180 degrees (with wavelength)
8988
(
90-
[4 * np.pi, np.array([[0, 30, 60, 90, 120, 181], [1, 2, 3, 4, 5, 6]])],
89+
[4 * np.pi, np.array([0, 30, 60, 90, 120, 181])],
9190
[ValueError, "Two theta exceeds 180 degrees. Please check the input values for errors."],
9291
),
9392
]

0 commit comments

Comments
 (0)