Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename id to uuid in DiffractionObject #271

Merged
merged 8 commits into from
Dec 26, 2024
Merged
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.
Copy link
Contributor Author

@bobleesj bobleesj Dec 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are marked with @property - docstrings are provided separately (please see below)

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add "To access specific arrays individually, use these slices:"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2024-12-26 at 2 46 56 PM

Yes, great. done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a stray y-array at the end of the line :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry - fixed now:


        The shape (len(data), 4) 2D array with columns containing the `yarray` (intensity)
        and the `xarray` values in q, tth, and 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

@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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while User still has access to uuid, we make it non-settable still.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's check. Logically, this should return an int not a list. Let's check and maybe update the function/tesnts, or understand why it returns a list and change the name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I created a separate issue for this since it's beyond the scope of this PR. I will look into this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 @@ -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 == {}

Expand Down Expand Up @@ -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():
Expand Down
Loading