Skip to content

Commit 2a5a819

Browse files
add functionality for handling error messages
1 parent a53461f commit 2a5a819

File tree

2 files changed

+48
-44
lines changed

2 files changed

+48
-44
lines changed

src/diffpy/utils/scattering_objects/diffraction_objects.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,23 @@
1717
"and specifying how to handle the mismatch."
1818
)
1919

20+
wavelength_warning_emsg = (
21+
"INFO: no wavelength has been specified. You can continue "
22+
"to use the DiffractionObject but some of its powerful features "
23+
"will not be available. To specify a wavelength, set "
24+
"diffraction_object.wavelength = [number], "
25+
"where diffraction_object is the variable name of you Diffraction Object, "
26+
"and number is the wavelength in angstroms."
27+
)
28+
29+
length_mismatch_emsg = "Please ensure {array_name} array and intensity array are of the same length."
30+
non_numeric_value_emsg = "Invalid value found in {array_name} array. Please ensure all values are numeric."
31+
invalid_tth_emsg = "Two theta exceeds 180 degrees. Please check the input values for errors."
32+
invalid_q_or_wavelength_emsg = (
33+
"The supplied q-array and wavelength will result in an impossible two-theta. "
34+
"Please check these values and re-instantiate the DiffractionObject with correct values."
35+
)
36+
2037

2138
class Diffraction_object:
2239
"""A class to represent and manipulate data associated with diffraction experiments.
@@ -778,12 +795,20 @@ def q_to_tth(self):
778795
two_theta : array
779796
The array of :math:`2\theta` values in radians
780797
"""
798+
for i, value in enumerate(self.on_q[0]):
799+
if not isinstance(value, (int, float)):
800+
raise TypeError(non_numeric_value_emsg.format(array_name="q"))
801+
if len(self.on_q[0]) != len(self.on_q[1]):
802+
raise RuntimeError(length_mismatch_emsg.format(array_name="q"))
803+
if self.wavelength is None:
804+
warnings.warn(wavelength_warning_emsg, UserWarning)
805+
return np.empty(0)
781806
q = self.on_q[0]
782807
q = np.asarray(q)
783808
wavelength = float(self.wavelength)
784809
pre_factor = wavelength / (4 * np.pi)
785810
if np.any(np.abs(q * pre_factor) > 1):
786-
raise ValueError("Please check if you entered an incorrect wavelength or q value.")
811+
raise ValueError(invalid_q_or_wavelength_emsg)
787812
return np.rad2deg(2.0 * np.arcsin(q * pre_factor))
788813

789814
def tth_to_q(self):
@@ -818,12 +843,17 @@ def tth_to_q(self):
818843
The array of :math:`q` values in the inverse of the units
819844
of ``wavelength``
820845
"""
846+
for i, value in enumerate(self.on_tth[0]):
847+
if not isinstance(value, (int, float)):
848+
raise TypeError(non_numeric_value_emsg.format(array_name="two theta"))
849+
if len(self.on_tth[0]) != len(self.on_tth[1]):
850+
raise RuntimeError(length_mismatch_emsg.format(array_name="two theta"))
821851
two_theta = np.asarray(np.deg2rad(self.on_tth[0]))
822852
if np.any(two_theta > np.pi):
823-
raise ValueError(
824-
"Two theta exceeds 180 degrees. Please check if invalid values were entered "
825-
"or if degrees were incorrectly specified as radians."
826-
)
853+
raise ValueError(invalid_tth_emsg)
854+
if self.wavelength is None:
855+
warnings.warn(wavelength_warning_emsg, UserWarning)
856+
return np.empty(0)
827857
wavelength = float(self.wavelength)
828858
pre_factor = (4 * np.pi) / wavelength
829859
return pre_factor * np.sin(two_theta / 2)

tests/diffpy/utils/scattering_objects/test_diffraction_objects.py

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55
from freezegun import freeze_time
66

7-
from diffpy.utils.scattering_objects.diffraction_objects import DiffractionObject
7+
from diffpy.utils.scattering_objects.diffraction_objects import DiffractionObject, wavelength_warning_emsg
88

99
params = [
1010
( # Default
@@ -238,35 +238,18 @@ def _test_valid_diffraction_objects(actual_diffraction_object, function, expecte
238238
if actual_diffraction_object.wavelength is None:
239239
with pytest.warns(UserWarning) as warn_record:
240240
getattr(actual_diffraction_object, function)()
241-
assert str(warn_record[0].message) == (
242-
"INFO: no wavelength has been specified. You can continue "
243-
"to use the DiffractionObject but some of its powerful features "
244-
"will not be available. To specify a wavelength, set "
245-
"diffraction_object.wavelength = [number], "
246-
"where diffraction_object is the variable name of you Diffraction Object, "
247-
"and number is the wavelength in angstroms."
248-
)
241+
assert str(warn_record[0].message) == wavelength_warning_emsg
249242
actual_array = getattr(actual_diffraction_object, function)()
250243
return np.allclose(actual_array, expected_array)
251244

252245

253246
params_q_to_tth = [
254247
# UC1: User specified empty q values (without wavelength)
255-
(
256-
[None, [], []],
257-
[[]],
258-
),
248+
([None, [], []], [[]]),
259249
# UC2: User specified empty q values (with wavelength)
260-
(
261-
[4 * np.pi, [], []],
262-
[[]],
263-
),
250+
([4 * np.pi, [], []], [[]]),
264251
# UC3: User specified valid q values (without wavelength)
265-
# expected tth values are 2*arcsin(q) in degrees
266-
(
267-
[None, [0, 0.2, 0.4, 0.6, 0.8, 1], [1, 2, 3, 4, 5, 6]],
268-
[[]],
269-
),
252+
([None, [0, 0.2, 0.4, 0.6, 0.8, 1], [1, 2, 3, 4, 5, 6]], [[]]),
270253
# UC4: User specified valid q values (with wavelength)
271254
# expected tth values are 2*arcsin(q) in degrees
272255
(
@@ -300,18 +283,18 @@ def test_q_to_tth(inputs, expected):
300283
[
301284
ValueError,
302285
"The supplied q-array and wavelength will result in an impossible two-theta. "
303-
"Please check these values and re-instantiate the DiffractionObject.",
286+
"Please check these values and re-instantiate the DiffractionObject with correct values.",
304287
],
305288
),
306289
# UC3: user specified a q array that does not match the length of intensity array (without wavelength)
307290
(
308291
[None, [0, 0.2, 0.4, 0.6, 0.8, 1], [1, 2, 3, 4, 5]],
309-
[RuntimeError, "Please ensure q array and intensity array are the same length."],
292+
[RuntimeError, "Please ensure q array and intensity array are of the same length."],
310293
),
311294
# UC4: user specified a q array that does not match the length of intensity array (with wavelength)
312295
(
313296
[4 * np.pi, [0, 0.2, 0.4, 0.6, 0.8, 1], [1, 2, 3, 4, 5]],
314-
[RuntimeError, "Please ensure q array and intensity array are the same length."],
297+
[RuntimeError, "Please ensure q array and intensity array are of the same length."],
315298
),
316299
# UC5: user specified a non-numeric value in q array (without wavelength)
317300
(
@@ -336,26 +319,17 @@ def test_q_to_tth_bad(inputs, expected):
336319

337320
params_tth_to_q = [
338321
# UC1: User specified empty tth values (without wavelength)
339-
(
340-
[None, [], []],
341-
[[]],
342-
),
322+
([None, [], []], [[]]),
343323
# UC2: User specified empty tth values (with wavelength)
344-
(
345-
[4 * np.pi, [], []],
346-
[[]],
347-
),
324+
([4 * np.pi, [], []], [[]]),
348325
# UC3: User specified valid tth values between 0-180 degrees (without wavelength)
349326
(
350327
[None, [0, 30, 60, 90, 120, 180], [1, 2, 3, 4, 5, 6]],
351328
[[]],
352329
),
353330
# UC4: User specified valid tth values between 0-180 degrees (with wavelength)
354331
# expected q vales are sin15, sin30, sin45, sin60, sin90
355-
(
356-
[4 * np.pi, [0, 30, 60, 90, 120, 180], [1, 2, 3, 4, 5, 6]],
357-
[[0, 0.258819, 0.5, 0.707107, 0.866025, 1]],
358-
),
332+
([4 * np.pi, [0, 30, 60, 90, 120, 180], [1, 2, 3, 4, 5, 6]], [[0, 0.258819, 0.5, 0.707107, 0.866025, 1]]),
359333
]
360334

361335

@@ -381,12 +355,12 @@ def test_tth_to_q(inputs, expected):
381355
# UC3: user specified a two theta array that does not match the length of intensity array (without wavelength)
382356
(
383357
[None, [0, 30, 60, 90, 120], [1, 2, 3, 4, 5, 6]],
384-
[RuntimeError, "Please ensure two theta array and intensity array are the same length."],
358+
[RuntimeError, "Please ensure two theta array and intensity array are of the same length."],
385359
),
386360
# UC4: user specified a two theta array that does not match the length of intensity array (with wavelength)
387361
(
388362
[4 * np.pi, [0, 30, 60, 90, 120], [1, 2, 3, 4, 5, 6]],
389-
[RuntimeError, "Please ensure two theta array and intensity array are the same length."],
363+
[RuntimeError, "Please ensure two theta array and intensity array are of the same length."],
390364
),
391365
# UC5: user specified a non-numeric value in two theta array (without wavelength)
392366
(

0 commit comments

Comments
 (0)