Skip to content

Commit b0b6676

Browse files
authored
Merge pull request #271 from bobleesj/_uuid
Rename `id` to `uuid` in DiffractionObject
2 parents 40c3446 + 98a3028 commit b0b6676

File tree

3 files changed

+75
-24
lines changed

3 files changed

+75
-24
lines changed

news/uuid-rename.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* <news item>
4+
5+
**Changed:**
6+
7+
* DiffractionObject's "id" property renamed to "uuid"
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

src/diffpy/utils/diffraction_objects.py

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ class DiffractionObject:
4646
4747
Attributes
4848
----------
49-
all_arrays : ndarray
50-
The array containing the quantity of q, tth, d values.
51-
input_xtype : str
52-
The type of the independent variable in `xarray`. Must be one of {*XQUANTITIES}
53-
id : uuid
54-
The unique identifier for the diffraction object.
5549
scat_quantity : str
5650
The type of scattering experiment (e.g., "x-ray", "neutron"). Default is an empty string "".
5751
wavelength : float
@@ -127,7 +121,7 @@ def __init__(
127121
>>> print(do.metadata)
128122
"""
129123

130-
self._id = uuid.uuid4()
124+
self._uuid = uuid.uuid4()
131125
self._input_data(xarray, yarray, xtype, wavelength, scat_quantity, name, metadata)
132126

133127
def _input_data(self, xarray, yarray, xtype, wavelength, scat_quantity, name, metadata):
@@ -284,6 +278,23 @@ def __rtruediv__(self, other):
284278

285279
@property
286280
def all_arrays(self):
281+
"""The 2D array containing `xarray` and `yarray` values.
282+
283+
Returns
284+
-------
285+
ndarray
286+
The shape (len(data), 4) 2D array with columns containing the `yarray` (intensity)
287+
and the `xarray` values in q, tth, and d.
288+
289+
Examples
290+
--------
291+
To access specific arrays individually, use these slices:
292+
293+
>>> my_do.all_arrays[:, 0] # yarray
294+
>>> my_do.all_arrays[:, 1] # xarray in q
295+
>>> my_do.all_arrays[:, 2] # xarray in tth
296+
>>> my_do.all_arrays[:, 3] # xarray in d
297+
"""
287298
return self._all_arrays
288299

289300
@all_arrays.setter
@@ -292,19 +303,33 @@ def all_arrays(self, _):
292303

293304
@property
294305
def input_xtype(self):
306+
"""The type of the independent variable in `xarray`.
307+
308+
Returns
309+
-------
310+
str
311+
The type of `xarray`, which must be one of {*XQUANTITIES}.
312+
"""
295313
return self._input_xtype
296314

297315
@input_xtype.setter
298316
def input_xtype(self, _):
299317
raise AttributeError(_setter_wmsg("input_xtype"))
300318

301319
@property
302-
def id(self):
303-
return self._id
320+
def uuid(self):
321+
"""The unique identifier for the DiffractionObject instance.
322+
323+
Returns
324+
-------
325+
uuid
326+
The unique identifier of the DiffractionObject instance.
327+
"""
328+
return self._uuid
304329

305-
@id.setter
306-
def id(self, _):
307-
raise AttributeError(_setter_wmsg("id"))
330+
@uuid.setter
331+
def uuid(self, _):
332+
raise AttributeError(_setter_wmsg("uuid"))
308333

309334
def get_array_index(self, value, xtype=None):
310335
"""Return the index of the closest value in the array associated with
@@ -319,7 +344,8 @@ def get_array_index(self, value, xtype=None):
319344
320345
Returns
321346
-------
322-
the index of the value in the array
347+
list
348+
The list containing the index of the closest value in the array.
323349
"""
324350

325351
xtype = self._input_xtype

tests/test_diffraction_objects.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ def test_init_valid(do_init_args, expected_do_dict, divide_by_zero_warning_expec
539539
else:
540540
actual_do_dict = DiffractionObject(**do_init_args).__dict__
541541
diff = DeepDiff(
542-
actual_do_dict, expected_do_dict, ignore_order=True, significant_digits=13, exclude_paths="root['_id']"
542+
actual_do_dict, expected_do_dict, ignore_order=True, significant_digits=13, exclude_paths="root['_uuid']"
543543
)
544544
assert diff == {}
545545

@@ -583,27 +583,29 @@ def test_all_array_setter(do_minimal):
583583
do.all_arrays = np.empty((4, 4))
584584

585585

586-
def test_id_getter(do_minimal):
586+
def test_uuid_getter(do_minimal):
587587
do = do_minimal
588-
assert hasattr(do, "id")
589-
assert isinstance(do.id, UUID)
590-
assert len(str(do.id)) == 36
588+
assert hasattr(do, "uuid")
589+
assert isinstance(do.uuid, UUID)
590+
assert len(str(do.uuid)) == 36
591591

592592

593-
def test_id_getter_with_mock(mocker, do_minimal):
594-
mocker.patch.object(DiffractionObject, "id", new_callable=lambda: UUID("d67b19c6-3016-439f-81f7-cf20a04bee87"))
593+
def test_uuid_getter_with_mock(mocker, do_minimal):
594+
mocker.patch.object(
595+
DiffractionObject, "uuid", new_callable=lambda: UUID("d67b19c6-3016-439f-81f7-cf20a04bee87")
596+
)
595597
do = do_minimal
596-
assert do.id == UUID("d67b19c6-3016-439f-81f7-cf20a04bee87")
598+
assert do.uuid == UUID("d67b19c6-3016-439f-81f7-cf20a04bee87")
597599

598600

599-
def test_id_setter_error(do_minimal):
601+
def test_uuid_setter_error(do_minimal):
600602
do = do_minimal
601603

602604
with pytest.raises(
603605
AttributeError,
604-
match="Direct modification of attribute 'id' is not allowed. Please use 'input_data' to modify 'id'.",
606+
match="Direct modification of attribute 'uuid' is not allowed. Please use 'input_data' to modify 'uuid'.",
605607
):
606-
do.id = uuid.uuid4()
608+
do.uuid = uuid.uuid4()
607609

608610

609611
def test_xarray_yarray_length_mismatch():

0 commit comments

Comments
 (0)