From 1a2214fc14916eef8f1737ca70bd4ff38f82980d Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Mon, 23 Dec 2024 14:03:55 -0500 Subject: [PATCH 1/7] Rename id to uuid in DiffractionObjet --- news/uuid-rename.rst | 23 +++++++++++++++++++++++ src/diffpy/utils/diffraction_objects.py | 14 +++++++------- tests/test_diffraction_objects.py | 24 +++++++++++++----------- 3 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 news/uuid-rename.rst diff --git a/news/uuid-rename.rst b/news/uuid-rename.rst new file mode 100644 index 00000000..ecd7f22d --- /dev/null +++ b/news/uuid-rename.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* DiffractionObject's "id" property renamed to "uuid" + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/src/diffpy/utils/diffraction_objects.py b/src/diffpy/utils/diffraction_objects.py index 92ad17dc..2922cdbd 100644 --- a/src/diffpy/utils/diffraction_objects.py +++ b/src/diffpy/utils/diffraction_objects.py @@ -50,7 +50,7 @@ class DiffractionObject: The array containing the quantity of q, tth, d values. input_xtype : str The type of the independent variable in `xarray`. Must be one of {*XQUANTITIES} - id : uuid + _uuid : uuid The unique identifier for the diffraction object. scat_quantity : str The type of scattering experiment (e.g., "x-ray", "neutron"). Default is an empty string "". @@ -127,7 +127,7 @@ def __init__( >>> print(do.metadata) """ - self._id = uuid.uuid4() + self._uuid = uuid.uuid4() self._input_data(xarray, yarray, xtype, wavelength, scat_quantity, name, metadata) def _input_data(self, xarray, yarray, xtype, wavelength, scat_quantity, name, metadata): @@ -299,12 +299,12 @@ def input_xtype(self, _): raise AttributeError(_setter_wmsg("input_xtype")) @property - def id(self): - return self._id + def uuid(self): + return self._uuid - @id.setter - def id(self, _): - raise AttributeError(_setter_wmsg("id")) + @uuid.setter + def uuid(self, _): + raise AttributeError(_setter_wmsg("uuid")) def get_array_index(self, value, xtype=None): """Return the index of the closest value in the array associated with diff --git a/tests/test_diffraction_objects.py b/tests/test_diffraction_objects.py index 63f349eb..33675b9e 100644 --- a/tests/test_diffraction_objects.py +++ b/tests/test_diffraction_objects.py @@ -479,7 +479,7 @@ def test_init_valid(do_init_args, expected_do_dict, divide_by_zero_warning_expec else: actual_do_dict = DiffractionObject(**do_init_args).__dict__ diff = DeepDiff( - actual_do_dict, expected_do_dict, ignore_order=True, significant_digits=13, exclude_paths="root['_id']" + actual_do_dict, expected_do_dict, ignore_order=True, significant_digits=13, exclude_paths="root['_uuid']" ) assert diff == {} @@ -523,27 +523,29 @@ def test_all_array_setter(do_minimal): do.all_arrays = np.empty((4, 4)) -def test_id_getter(do_minimal): +def test_uuid_getter(do_minimal): do = do_minimal - assert hasattr(do, "id") - assert isinstance(do.id, UUID) - assert len(str(do.id)) == 36 + assert hasattr(do, "uuid") + assert isinstance(do.uuid, UUID) + assert len(str(do.uuid)) == 36 -def test_id_getter_with_mock(mocker, do_minimal): - mocker.patch.object(DiffractionObject, "id", new_callable=lambda: UUID("d67b19c6-3016-439f-81f7-cf20a04bee87")) +def test_uuid_getter_with_mock(mocker, do_minimal): + mocker.patch.object( + DiffractionObject, "uuid", new_callable=lambda: UUID("d67b19c6-3016-439f-81f7-cf20a04bee87") + ) do = do_minimal - assert do.id == UUID("d67b19c6-3016-439f-81f7-cf20a04bee87") + assert do.uuid == UUID("d67b19c6-3016-439f-81f7-cf20a04bee87") -def test_id_setter_error(do_minimal): +def test_uuid_setter_error(do_minimal): do = do_minimal with pytest.raises( AttributeError, - match="Direct modification of attribute 'id' is not allowed. Please use 'input_data' to modify 'id'.", + match="Direct modification of attribute 'uuid' is not allowed. Please use 'input_data' to modify 'uuid'.", ): - do.id = uuid.uuid4() + do.uuid = uuid.uuid4() def test_xarray_yarray_length_mismatch(): From 209a1945c33950ddfff1d1eaf8e086c16695995a Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Wed, 25 Dec 2024 23:10:38 -0500 Subject: [PATCH 2/7] Add separate docstring for @property methods --- src/diffpy/utils/diffraction_objects.py | 37 ++++++++++++++++++++----- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/src/diffpy/utils/diffraction_objects.py b/src/diffpy/utils/diffraction_objects.py index 2922cdbd..a1b1bd13 100644 --- a/src/diffpy/utils/diffraction_objects.py +++ b/src/diffpy/utils/diffraction_objects.py @@ -46,12 +46,6 @@ class DiffractionObject: Attributes ---------- - all_arrays : ndarray - The array containing the quantity of q, tth, d values. - input_xtype : str - The type of the independent variable in `xarray`. Must be one of {*XQUANTITIES} - _uuid : uuid - The unique identifier for the diffraction object. scat_quantity : str The type of scattering experiment (e.g., "x-ray", "neutron"). Default is an empty string "". wavelength : float @@ -284,6 +278,20 @@ def __rtruediv__(self, other): @property def all_arrays(self): + """The array containing `xarray` values in q, d, tth, and `yarray`. + + Returns + ------- + ndarray + The 2D matrix containing the `xarray` objects values and `yarray`. + + Examples + -------- + >>> my_do.all_arrays[:, 0] # yarray + >>> my_do.all_arrays[:, 1] # `xarray` in q + >>> my_do.all_arrays[:, 2] # `xarray` in tth + >>> my_do.all_arrays[:, 3] # `xarray` in d + """ return self._all_arrays @all_arrays.setter @@ -292,6 +300,13 @@ def all_arrays(self, _): @property def input_xtype(self): + """The type of the independent variable in `xarray`. + + Returns + ------- + str + The type of `xarray`, which must be one of {*XQUANTITIES}. + """ return self._input_xtype @input_xtype.setter @@ -300,6 +315,13 @@ def input_xtype(self, _): @property def uuid(self): + """The unique identifier for the DiffractionObject instance. + + Returns + ------- + uuid + The unique identifier of the DiffractionObject instance. + """ return self._uuid @uuid.setter @@ -319,7 +341,8 @@ def get_array_index(self, value, xtype=None): Returns ------- - the index of the value in the array + list + The list containing the index of the closest value in the array. """ xtype = self._input_xtype From 5824250c02fca1e067598615b48418eb24a48504 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Wed, 25 Dec 2024 23:15:40 -0500 Subject: [PATCH 3/7] Improve docstring for all_arrays --- 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 a1b1bd13..684c71b7 100644 --- a/src/diffpy/utils/diffraction_objects.py +++ b/src/diffpy/utils/diffraction_objects.py @@ -278,12 +278,12 @@ def __rtruediv__(self, other): @property def all_arrays(self): - """The array containing `xarray` values in q, d, tth, and `yarray`. + """The 2D array containing `xarray` and `yarray` values. Returns ------- ndarray - The 2D matrix containing the `xarray` objects values and `yarray`. + The 2D array containing the `xarray` values in q, tth, d, and `yarray`. Examples -------- From 2f293c09999cc1b8617cd39b2c9a0b07f1b2add3 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Wed, 25 Dec 2024 23:16:53 -0500 Subject: [PATCH 4/7] Remove extra singl quote around comment --- src/diffpy/utils/diffraction_objects.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/diffpy/utils/diffraction_objects.py b/src/diffpy/utils/diffraction_objects.py index 684c71b7..286154d7 100644 --- a/src/diffpy/utils/diffraction_objects.py +++ b/src/diffpy/utils/diffraction_objects.py @@ -288,9 +288,9 @@ def all_arrays(self): Examples -------- >>> my_do.all_arrays[:, 0] # yarray - >>> my_do.all_arrays[:, 1] # `xarray` in q - >>> my_do.all_arrays[:, 2] # `xarray` in tth - >>> my_do.all_arrays[:, 3] # `xarray` in d + >>> my_do.all_arrays[:, 1] # xarray in q + >>> my_do.all_arrays[:, 2] # xarray in tth + >>> my_do.all_arrays[:, 3] # xarray in d """ return self._all_arrays From ceba7f9dead7867476bdba5482caacfe77810de0 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Thu, 26 Dec 2024 14:49:52 -0500 Subject: [PATCH 5/7] Fix docstring for all_arrays per sbilling pr review --- src/diffpy/utils/diffraction_objects.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/diffpy/utils/diffraction_objects.py b/src/diffpy/utils/diffraction_objects.py index 286154d7..83d7f1cf 100644 --- a/src/diffpy/utils/diffraction_objects.py +++ b/src/diffpy/utils/diffraction_objects.py @@ -283,10 +283,13 @@ def all_arrays(self): Returns ------- ndarray - The 2D array containing the `xarray` values in q, tth, d, and `yarray`. + The shape (len(data), 4) 2D array with columns containing the `yarray` (intensity) + and the `xarray` values in q, tth, d, and `yarray`. Examples -------- + To access specific arrays individually, use these slices: + >>> my_do.all_arrays[:, 0] # yarray >>> my_do.all_arrays[:, 1] # xarray in q >>> my_do.all_arrays[:, 2] # xarray in tth From 2d1c104b4197b20c94c21d5fef6683f65f876d00 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 26 Dec 2024 19:50:06 +0000 Subject: [PATCH 6/7] [pre-commit.ci] auto fixes from pre-commit hooks --- src/diffpy/utils/diffraction_objects.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/diffpy/utils/diffraction_objects.py b/src/diffpy/utils/diffraction_objects.py index 83d7f1cf..87ed9d13 100644 --- a/src/diffpy/utils/diffraction_objects.py +++ b/src/diffpy/utils/diffraction_objects.py @@ -289,7 +289,7 @@ def all_arrays(self): Examples -------- To access specific arrays individually, use these slices: - + >>> my_do.all_arrays[:, 0] # yarray >>> my_do.all_arrays[:, 1] # xarray in q >>> my_do.all_arrays[:, 2] # xarray in tth From b198d66ae05bce8d6e01a5223e625bc58fafc0fb Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Thu, 26 Dec 2024 15:33:56 -0500 Subject: [PATCH 7/7] Remove extra mention of yarray --- 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 83d7f1cf..6118f46d 100644 --- a/src/diffpy/utils/diffraction_objects.py +++ b/src/diffpy/utils/diffraction_objects.py @@ -284,12 +284,12 @@ def all_arrays(self): ------- ndarray The shape (len(data), 4) 2D array with columns containing the `yarray` (intensity) - and the `xarray` values in q, tth, d, and `yarray`. + and the `xarray` values in q, tth, and d. Examples -------- To access specific arrays individually, use these slices: - + >>> my_do.all_arrays[:, 0] # yarray >>> my_do.all_arrays[:, 1] # xarray in q >>> my_do.all_arrays[:, 2] # xarray in tth