Skip to content

Commit b733b94

Browse files
fixed functions and tests for q_to_tth, tth_to_q, q_to_d, and d_to_q
1 parent 07274d4 commit b733b94

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

src/diffpy/utils/scattering_objects/diffraction_objects.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
DQUANTITIES = ["d", "dspace"]
1111
XQUANTITIES = ANGLEQUANTITIES + DQUANTITIES + QQUANTITIES
1212
XUNITS = ["degrees", "radians", "rad", "deg", "inv_angs", "inv_nm", "nm-1", "A-1"]
13+
QMAX = 40
14+
DMAX = 100
1315

1416
x_grid_emsg = (
1517
"objects are not on the same x-grid. You may add them using the self.add method "
@@ -378,10 +380,10 @@ def q_to_d(self):
378380
d : array
379381
The array of :math:`d` values in the inverse of the units of ``wavelength``
380382
"""
381-
epsilon = 1e-10
382383
q = np.asarray(self.on_q[0])
383-
q = np.where(np.abs(q) < epsilon, q + epsilon, q)
384-
return (2 * np.pi) / q
384+
d = np.where(q != 0, (2 * np.pi) / q, np.inf)
385+
d = np.minimum(d, DMAX)
386+
return d[::-1]
385387

386388
def d_to_q(self):
387389
r"""
@@ -399,10 +401,10 @@ def d_to_q(self):
399401
q : array
400402
The array of :math:`q` values in the inverse of the units of ``wavelength``
401403
"""
402-
epsilon = 1e-10
403404
d = np.asarray(self.on_d[0])
404-
d = np.where(np.abs(d) < epsilon, d + epsilon, d)
405-
return (2 * np.pi) / d
405+
q = np.where(d != 0, (2 * np.pi) / d, np.inf)
406+
q = np.minimum(q, QMAX)
407+
return q[::-1]
406408

407409
def tth_to_d(self):
408410
r"""

tests/test_diffraction_objects.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -232,34 +232,38 @@ def test_diffraction_objects_equality(inputs1, inputs2, expected):
232232

233233

234234
def test_q_to_tth():
235-
actual = Diffraction_object(wavelength=0.71)
236-
setattr(actual, "on_q", [[0, 4.58087], [1, 1]])
235+
actual = Diffraction_object(wavelength=4 * np.pi)
236+
setattr(actual, "on_q", [[0, 0.2, 0.4, 0.6, 0.8, 1], [1, 2, 3, 4, 5, 6]])
237237
actual_tth = actual.q_to_tth()
238-
expected_tth = [0, 30]
238+
# expected tth values are 2 * arcsin(q)
239+
expected_tth = [0, 23.07392, 47.15636, 73.73980, 106.26020, 180]
239240
assert np.allclose(actual_tth, expected_tth)
240241

241242

242243
def test_tth_to_q():
243-
actual = Diffraction_object(wavelength=0.71)
244-
setattr(actual, "on_tth", [[0, 30], [1, 1]])
244+
actual = Diffraction_object(wavelength=4 * np.pi)
245+
setattr(actual, "on_tth", [[0, 30, 60, 90, 120, 180], [1, 2, 3, 4, 5, 6]])
245246
actual_q = actual.tth_to_q()
246-
expected_q = [0, 4.58087]
247+
# expected q vales are sin15, sin30, sin45, sin60, sin90
248+
expected_q = [0, 0.258819, 0.5, 0.707107, 0.866025, 1]
247249
assert np.allclose(actual_q, expected_q)
248250

249251

250252
def test_q_to_d():
251253
actual = Diffraction_object(wavelength=0.71)
252-
setattr(actual, "on_q", [[0, 4.58087], [1, 1]])
254+
setattr(actual, "on_q", [[0, np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi], [1, 2, 3, 4, 5, 6]])
253255
actual_d = actual.q_to_d()
254-
expected_d = [62831853071.8, 1.37161]
256+
# expected d values are DMAX=100, 2/1, 2/2, 2/3, 2/4, 2/5, and in reverse order
257+
expected_d = [0.4, 0.5, 0.66667, 1, 2, 100]
255258
assert np.allclose(actual_d, expected_d)
256259

257260

258261
def test_d_to_q():
259-
actual = Diffraction_object(wavelength=0.71)
260-
setattr(actual, "on_d", [[1e10, 1.37161], [1, 1]])
262+
actual = Diffraction_object(wavelength=1)
263+
setattr(actual, "on_d", [[0, np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi], [1, 2, 3, 4, 5, 6]])
261264
actual_q = actual.d_to_q()
262-
expected_q = [0, 4.58087]
265+
# expected q values are QMAX=40, 2/1, 2/2, 2/3, 2/4, 2/5, and in reverse order
266+
expected_q = [0.4, 0.5, 0.66667, 1, 2, 40]
263267
assert np.allclose(actual_q, expected_q)
264268

265269

0 commit comments

Comments
 (0)