Skip to content

Commit

Permalink
Merge pull request #271 from bobleesj/_uuid
Browse files Browse the repository at this point in the history
Rename `id` to `uuid` in DiffractionObject
  • Loading branch information
sbillinge authored Dec 26, 2024
2 parents 40c3446 + 98a3028 commit b0b6676
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 24 deletions.
23 changes: 23 additions & 0 deletions news/uuid-rename.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* <news item>

**Changed:**

* DiffractionObject's "id" property renamed to "uuid"

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
52 changes: 39 additions & 13 deletions src/diffpy/utils/diffraction_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
id : 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
Expand Down Expand Up @@ -127,7 +121,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):
Expand Down Expand Up @@ -284,6 +278,23 @@ def __rtruediv__(self, other):

@property
def all_arrays(self):
"""The 2D array containing `xarray` and `yarray` values.
Returns
-------
ndarray
The shape (len(data), 4) 2D array with columns containing the `yarray` (intensity)
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
>>> my_do.all_arrays[:, 3] # xarray in d
"""
return self._all_arrays

@all_arrays.setter
Expand All @@ -292,19 +303,33 @@ 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
def input_xtype(self, _):
raise AttributeError(_setter_wmsg("input_xtype"))

@property
def id(self):
return self._id
def uuid(self):
"""The unique identifier for the DiffractionObject instance.
Returns
-------
uuid
The unique identifier of the DiffractionObject instance.
"""
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
Expand All @@ -319,7 +344,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
Expand Down
24 changes: 13 additions & 11 deletions tests/test_diffraction_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,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 == {}

Expand Down Expand Up @@ -583,27 +583,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():
Expand Down

0 comments on commit b0b6676

Please sign in to comment.