diff --git a/news/xtype.rst b/news/xtype.rst new file mode 100644 index 00000000..24a78758 --- /dev/null +++ b/news/xtype.rst @@ -0,0 +1,23 @@ +**Added:** + +* functionality to return the 2D array based on the specified xtype + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/src/diffpy/utils/diffraction_objects.py b/src/diffpy/utils/diffraction_objects.py index 54374fa7..703baf03 100644 --- a/src/diffpy/utils/diffraction_objects.py +++ b/src/diffpy/utils/diffraction_objects.py @@ -21,7 +21,7 @@ def _xtype_wmsg(xtype): return ( - f"WARNING: I don't know how to handle the xtype, '{xtype}'. Please rerun specifying an " + f"I don't know how to handle the xtype, '{xtype}'. Please rerun specifying an " f"xtype from {*XQUANTITIES, }" ) @@ -376,16 +376,17 @@ def scale_to(self, target_diff_object, xtype=None, xvalue=None): return scaled def on_xtype(self, xtype): - """ - return a 2D np array with x in the first column and y in the second for x of type type + f""" + return a list of two 1D np array with x and y data, raise an error if the specified xtype is invalid Parameters ---------- - xtype + xtype str + the type of quantity for the independent variable from {*XQUANTITIES, } Returns ------- - + a list of two 1D np array with x and y data """ if xtype.lower() in ANGLEQUANTITIES: return self.on_tth() @@ -394,7 +395,7 @@ def on_xtype(self, xtype): elif xtype.lower() in DQUANTITIES: return self.on_d() else: - warnings.warn(_xtype_wmsg(xtype)) + raise ValueError(_xtype_wmsg(xtype)) def dump(self, filepath, xtype=None): if xtype is None: diff --git a/tests/test_diffraction_objects.py b/tests/test_diffraction_objects.py index 9db6932e..2371838f 100644 --- a/tests/test_diffraction_objects.py +++ b/tests/test_diffraction_objects.py @@ -1,11 +1,11 @@ +import re from pathlib import Path import numpy as np import pytest from freezegun import freeze_time -from diffpy.utils.diffraction_objects import DiffractionObject -from diffpy.utils.transforms import wavelength_warning_emsg +from diffpy.utils.diffraction_objects import XQUANTITIES, DiffractionObject def compare_dicts(dict1, dict2): @@ -202,13 +202,24 @@ def test_diffraction_objects_equality(inputs1, inputs2, expected): assert dicts_equal(diffraction_object1.__dict__, diffraction_object2.__dict__) == expected -def _test_valid_diffraction_objects(actual_diffraction_object, function, expected_array): - if actual_diffraction_object.wavelength is None: - with pytest.warns(UserWarning) as warn_record: - getattr(actual_diffraction_object, function)() - assert str(warn_record[0].message) == wavelength_warning_emsg - actual_array = getattr(actual_diffraction_object, function)() - return np.allclose(actual_array, expected_array) +def test_on_xtype(): + test = DiffractionObject(wavelength=2 * np.pi, xarray=np.array([30, 60]), yarray=np.array([1, 2]), xtype="tth") + assert np.allclose(test.on_xtype("tth"), [np.array([30, 60]), np.array([1, 2])]) + assert np.allclose(test.on_xtype("2theta"), [np.array([30, 60]), np.array([1, 2])]) + assert np.allclose(test.on_xtype("q"), [np.array([0.51764, 1]), np.array([1, 2])]) + assert np.allclose(test.on_xtype("d"), [np.array([12.13818, 6.28319]), np.array([1, 2])]) + + +def test_on_xtype_bad(): + test = DiffractionObject() + with pytest.raises( + ValueError, + match=re.escape( + f"I don't know how to handle the xtype, 'invalid'. Please rerun specifying an " + f"xtype from {*XQUANTITIES, }" + ), + ): + test.on_xtype("invalid") def test_dump(tmp_path, mocker):