From 5a32de45f573b423066222af49555eda26e8efb5 Mon Sep 17 00:00:00 2001 From: yucongalicechen Date: Mon, 2 Dec 2024 18:13:17 -0500 Subject: [PATCH 1/7] initial commit, raise value error if xtype is invalid --- .../scattering_objects/diffraction_objects.py | 3 +- .../test_diffraction_objects.py | 51 +++++++++++++++---- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/diffpy/utils/scattering_objects/diffraction_objects.py b/src/diffpy/utils/scattering_objects/diffraction_objects.py index 4351c537..a4909af0 100644 --- a/src/diffpy/utils/scattering_objects/diffraction_objects.py +++ b/src/diffpy/utils/scattering_objects/diffraction_objects.py @@ -742,7 +742,8 @@ def on_xtype(self, xtype): return self.on_q elif xtype.lower() in DQUANTITIES: return self.on_d - pass + else: + raise ValueError(f"Unknown xtype: {xtype}. Allowed xtypes are {*XQUANTITIES, }.") def dump(self, filepath, xtype=None): if xtype is None: diff --git a/tests/diffpy/utils/scattering_objects/test_diffraction_objects.py b/tests/diffpy/utils/scattering_objects/test_diffraction_objects.py index ada14ff1..570404fe 100644 --- a/tests/diffpy/utils/scattering_objects/test_diffraction_objects.py +++ b/tests/diffpy/utils/scattering_objects/test_diffraction_objects.py @@ -1,11 +1,17 @@ +import re from pathlib import Path import numpy as np import pytest from freezegun import freeze_time -from diffpy.utils.scattering_objects.diffraction_objects import DiffractionObject -from diffpy.utils.transforms import wavelength_warning_emsg +from diffpy.utils.scattering_objects.diffraction_objects import ( + ANGLEQUANTITIES, + DQUANTITIES, + QQUANTITIES, + XQUANTITIES, + DiffractionObject, +) params = [ ( # Default @@ -232,13 +238,40 @@ def test_diffraction_objects_equality(inputs1, inputs2, expected): assert (diffraction_object1 == diffraction_object2) == 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) +params_on_xtype = [ + ( + [ + np.array([1, 2, 3, 4, 5, 6]), # intensity array + np.array([0, 30, 60, 90, 120, 180]), # tth array + np.array([1, 2, 3, 4, 5, 6]), # q array + np.array([10, 20, 30, 40, 50, 60]), # d array + ], + [ + np.array([[0, 30, 60, 90, 120, 180], [1, 2, 3, 4, 5, 6]]), # expected on_tth + np.array([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]]), # expected on_q + np.array([[10, 20, 30, 40, 50, 60], [1, 2, 3, 4, 5, 6]]), # expected on_d + ], + ) +] + + +@pytest.mark.parametrize("inputs, expected", params_on_xtype) +def test_on_xtype(inputs, expected): + test = DiffractionObject() + test.on_tth = np.array([inputs[1], inputs[0]]) + test.on_q = np.array([inputs[2], inputs[0]]) + test.on_d = np.array([inputs[3], inputs[0]]) + for xtype_list, expected_value in zip([ANGLEQUANTITIES, QQUANTITIES, DQUANTITIES], expected): + for xtype in xtype_list: + assert np.allclose(test.on_xtype(xtype), expected_value) + + +def test_on_xtype_bad(): + test = DiffractionObject() + with pytest.raises( + ValueError, match=re.escape(f"Unknown xtype: invalid. Allowed xtypes are {*XQUANTITIES, }.") + ): + test.on_xtype("invalid") def test_dump(tmp_path, mocker): From bf5207878b186e52e11c67259a9ff2fe3795860f Mon Sep 17 00:00:00 2001 From: yucongalicechen Date: Tue, 3 Dec 2024 14:28:27 -0500 Subject: [PATCH 2/7] change test intensity array to avoid duplication with x-arrays --- tests/test_diffraction_objects.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_diffraction_objects.py b/tests/test_diffraction_objects.py index 3271023b..a7e4ea04 100644 --- a/tests/test_diffraction_objects.py +++ b/tests/test_diffraction_objects.py @@ -241,15 +241,15 @@ def test_diffraction_objects_equality(inputs1, inputs2, expected): params_on_xtype = [ ( [ - np.array([1, 2, 3, 4, 5, 6]), # intensity array + np.array([100, 200, 300, 400, 500, 600]), # intensity array np.array([0, 30, 60, 90, 120, 180]), # tth array np.array([1, 2, 3, 4, 5, 6]), # q array np.array([10, 20, 30, 40, 50, 60]), # d array ], [ - np.array([[0, 30, 60, 90, 120, 180], [1, 2, 3, 4, 5, 6]]), # expected on_tth - np.array([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]]), # expected on_q - np.array([[10, 20, 30, 40, 50, 60], [1, 2, 3, 4, 5, 6]]), # expected on_d + np.array([[0, 30, 60, 90, 120, 180], [100, 200, 300, 400, 500, 600]]), # expected on_tth + np.array([[1, 2, 3, 4, 5, 6], [100, 200, 300, 400, 500, 600]]), # expected on_q + np.array([[10, 20, 30, 40, 50, 60], [100, 200, 300, 400, 500, 600]]), # expected on_d ], ) ] From 1f9f142f3e7f00e0da46d84fd4c8b8d442ba9900 Mon Sep 17 00:00:00 2001 From: yucongalicechen Date: Wed, 4 Dec 2024 16:44:37 -0500 Subject: [PATCH 3/7] edit tests --- src/diffpy/utils/diffraction_objects.py | 2 +- tests/test_diffraction_objects.py | 45 ++++++------------------- 2 files changed, 12 insertions(+), 35 deletions(-) diff --git a/src/diffpy/utils/diffraction_objects.py b/src/diffpy/utils/diffraction_objects.py index 54374fa7..c213b5df 100644 --- a/src/diffpy/utils/diffraction_objects.py +++ b/src/diffpy/utils/diffraction_objects.py @@ -394,7 +394,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 09506fec..beecab56 100644 --- a/tests/test_diffraction_objects.py +++ b/tests/test_diffraction_objects.py @@ -5,13 +5,7 @@ import pytest from freezegun import freeze_time -from diffpy.utils.diffraction_objects import ( - ANGLEQUANTITIES, - DQUANTITIES, - QQUANTITIES, - XQUANTITIES, - DiffractionObject, -) +from diffpy.utils.diffraction_objects import XQUANTITIES, DiffractionObject def compare_dicts(dict1, dict2): @@ -208,38 +202,21 @@ def test_diffraction_objects_equality(inputs1, inputs2, expected): assert dicts_equal(diffraction_object1.__dict__, diffraction_object2.__dict__) == expected -params_on_xtype = [ - ( - [ - np.array([100, 200, 300, 400, 500, 600]), # intensity array - np.array([0, 30, 60, 90, 120, 180]), # tth array - np.array([1, 2, 3, 4, 5, 6]), # q array - np.array([10, 20, 30, 40, 50, 60]), # d array - ], - [ - np.array([[0, 30, 60, 90, 120, 180], [100, 200, 300, 400, 500, 600]]), # expected on_tth - np.array([[1, 2, 3, 4, 5, 6], [100, 200, 300, 400, 500, 600]]), # expected on_q - np.array([[10, 20, 30, 40, 50, 60], [100, 200, 300, 400, 500, 600]]), # expected on_d - ], - ) -] - - -@pytest.mark.parametrize("inputs, expected", params_on_xtype) -def test_on_xtype(inputs, expected): - test = DiffractionObject() - test.on_tth = np.array([inputs[1], inputs[0]]) - test.on_q = np.array([inputs[2], inputs[0]]) - test.on_d = np.array([inputs[3], inputs[0]]) - for xtype_list, expected_value in zip([ANGLEQUANTITIES, QQUANTITIES, DQUANTITIES], expected): - for xtype in xtype_list: - assert np.allclose(test.on_xtype(xtype), expected_value) +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("q"), [np.array([0.51763809, 1]), np.array([1, 2])]) + assert np.allclose(test.on_xtype("d"), [np.array([12.13818192, 6.28318531]), np.array([1, 2])]) def test_on_xtype_bad(): test = DiffractionObject() with pytest.raises( - ValueError, match=re.escape(f"Unknown xtype: invalid. Allowed xtypes are {*XQUANTITIES, }.") + ValueError, + match=re.escape( + f"WARNING: I don't know how to handle the xtype, 'invalid'. Please rerun specifying an " + f"xtype from {*XQUANTITIES, }" + ), ): test.on_xtype("invalid") From 11a15ee8f939b7df8dcdb18112c4624a985e1516 Mon Sep 17 00:00:00 2001 From: yucongalicechen Date: Wed, 4 Dec 2024 19:15:17 -0500 Subject: [PATCH 4/7] add docstring and news --- news/xtype.rst | 23 +++++++++++++++++++++++ src/diffpy/utils/diffraction_objects.py | 7 ++++--- 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 news/xtype.rst 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 c213b5df..b1ed5a78 100644 --- a/src/diffpy/utils/diffraction_objects.py +++ b/src/diffpy/utils/diffraction_objects.py @@ -376,16 +376,17 @@ def scale_to(self, target_diff_object, xtype=None, xvalue=None): return scaled def on_xtype(self, xtype): - """ + f""" return a 2D np array with x in the first column and y in the second for x of type type Parameters ---------- - xtype + xtype str + the type of quantity for the independent variable from {*XQUANTITIES, } Returns ------- - + a 2D np array with x and y data """ if xtype.lower() in ANGLEQUANTITIES: return self.on_tth() From 57465e2563090b88807f46762b43ca9deaf53b7a Mon Sep 17 00:00:00 2001 From: yucongalicechen Date: Thu, 5 Dec 2024 11:08:12 -0500 Subject: [PATCH 5/7] edit tests --- tests/test_diffraction_objects.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_diffraction_objects.py b/tests/test_diffraction_objects.py index beecab56..95848111 100644 --- a/tests/test_diffraction_objects.py +++ b/tests/test_diffraction_objects.py @@ -205,8 +205,9 @@ def test_diffraction_objects_equality(inputs1, inputs2, expected): 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("q"), [np.array([0.51763809, 1]), np.array([1, 2])]) - assert np.allclose(test.on_xtype("d"), [np.array([12.13818192, 6.28318531]), 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(): From 2e1853af96d0dd2b13d5bd3e89df12afade2fd0f Mon Sep 17 00:00:00 2001 From: yucongalicechen Date: Fri, 6 Dec 2024 17:35:33 -0500 Subject: [PATCH 6/7] edit docstring --- src/diffpy/utils/diffraction_objects.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/diffpy/utils/diffraction_objects.py b/src/diffpy/utils/diffraction_objects.py index b1ed5a78..d8e86f58 100644 --- a/src/diffpy/utils/diffraction_objects.py +++ b/src/diffpy/utils/diffraction_objects.py @@ -377,7 +377,7 @@ def scale_to(self, target_diff_object, xtype=None, xvalue=None): def on_xtype(self, xtype): f""" - return a 2D np array with x in the first column and y in the second for x of type type + return a list of two 1D np array with x and y data, raise an error if the specified xtype is invalid Parameters ---------- @@ -386,7 +386,7 @@ def on_xtype(self, xtype): Returns ------- - a 2D np array with x and y data + a list of two 1D np array with x and y data """ if xtype.lower() in ANGLEQUANTITIES: return self.on_tth() From 7eb0c0d237392553b79530de63b778b8bce19b3d Mon Sep 17 00:00:00 2001 From: yucongalicechen Date: Fri, 6 Dec 2024 20:11:28 -0500 Subject: [PATCH 7/7] remove warning in error msg --- src/diffpy/utils/diffraction_objects.py | 2 +- tests/test_diffraction_objects.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/diffpy/utils/diffraction_objects.py b/src/diffpy/utils/diffraction_objects.py index d8e86f58..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, }" ) diff --git a/tests/test_diffraction_objects.py b/tests/test_diffraction_objects.py index 95848111..2371838f 100644 --- a/tests/test_diffraction_objects.py +++ b/tests/test_diffraction_objects.py @@ -215,7 +215,7 @@ def test_on_xtype_bad(): with pytest.raises( ValueError, match=re.escape( - f"WARNING: I don't know how to handle the xtype, 'invalid'. Please rerun specifying an " + f"I don't know how to handle the xtype, 'invalid'. Please rerun specifying an " f"xtype from {*XQUANTITIES, }" ), ):