From f96728a922b5169a61fdc12a09affce4ce86ed1e Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Sat, 21 Dec 2024 20:25:05 -0500 Subject: [PATCH 1/3] Add class docstring for diffraction object and news --- news/class-docstring.rst | 23 +++++ src/diffpy/utils/diffraction_objects.py | 113 +++++++++++++++--------- 2 files changed, 96 insertions(+), 40 deletions(-) create mode 100644 news/class-docstring.rst diff --git a/news/class-docstring.rst b/news/class-docstring.rst new file mode 100644 index 00000000..a31cc6d6 --- /dev/null +++ b/news/class-docstring.rst @@ -0,0 +1,23 @@ +**Added:** + +* class docstring for `DiffractionObject` + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/src/diffpy/utils/diffraction_objects.py b/src/diffpy/utils/diffraction_objects.py index d72a2889..602434f9 100644 --- a/src/diffpy/utils/diffraction_objects.py +++ b/src/diffpy/utils/diffraction_objects.py @@ -36,50 +36,38 @@ def _setter_wmsg(attribute): class DiffractionObject: """ - Initialize a DiffractionObject instance. + A class to represent diffraction data for various scientific experiments involving scattering + techniques such as X-ray, neutron, and electron diffraction. This object can manage diffraction + data including transformations between different scattering quantities like q (scattering vector), + 2θ (two-theta angle), and d (interplanar spacing), and perform various operations like scaling, addition, + and subtraction of diffraction patterns. - Parameters + Attributes ---------- - xarray : array-like - The independent variable array containing "q", "tth", or "d" values. - yarray : array-like - The dependent variable array corresponding to intensity values. - xtype : str - The type of the independent variable in `xarray`. Must be one of {*XQUANTITIES}. - wavelength : float, optional - The wavelength of the incoming beam, specified in angstroms (Å). Default is none. - scat_quantity : str, optional + 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 "". - name : str, optional + wavelength : float + The wavelength of the incoming beam, specified in angstroms (Å). Default is none. + name: str The name or label for the scattering data. Default is an empty string "". - metadata : dict, optional - The additional metadata associated with the diffraction object. Default is {}. - - Examples - -------- - Create a DiffractionObject for X-ray scattering data: - - >>> import numpy as np - >>> from diffpy.utils.diffraction_objects import DiffractionObject - ... - >>> x = np.array([0.12, 0.24, 0.31, 0.4]) # independent variable (e.g., q) - >>> y = np.array([10, 20, 40, 60]) # intensity values - >>> metadata = { - ... "sample": "rock salt from the beach", - ... "composition": "NaCl", - ... "temperature": "300 K,", - ... "experimenters": "Phill, Sally" - ... } - >>> do = DiffractionObject( - ... xarray=x, - ... yarray=y, - ... xtype="q", - ... wavelength=1.54, - ... scat_quantity="x-ray", - ... name="beach_rock_salt_1", - ... metadata=metadata - ... ) - >>> print(do.metadata) + qmin : float + The minimum q value. + qmax : float + The maximum q value. + tthmin : float + The minimum two-theta value. + tthmax : float + The maximum two-theta value. + dmin : float + The minimum d-spacing value. + dmax : float + The maximum d-spacing value. """ def __init__( @@ -92,6 +80,51 @@ def __init__( name="", metadata={}, ): + """ + Initialize a DiffractionObject instance. + + Parameters + ---------- + xarray : ndarray + The independent variable array containing "q", "tth", or "d" values. + yarray : ndarray + The dependent variable array corresponding to intensity values. + xtype : str + The type of the independent variable in `xarray`. Must be one of {*XQUANTITIES}. + wavelength : float, optional + The wavelength of the incoming beam, specified in angstroms (Å). Default is none. + scat_quantity : str, optional + The type of scattering experiment (e.g., "x-ray", "neutron"). Default is an empty string "". + name : str, optional + The name or label for the scattering data. Default is an empty string "". + metadata : dict, optional + The additional metadata associated with the diffraction object. Default is {}. + + Examples + -------- + Create a DiffractionObject for X-ray scattering data + >>> import numpy as np + >>> from diffpy.utils.diffraction_objects import DiffractionObject + ... + >>> x = np.array([0.12, 0.24, 0.31, 0.4]) # independent variable (e.g., q) + >>> y = np.array([10, 20, 40, 60]) # intensity values + >>> metadata = { + ... "sample": "rock salt from the beach", + ... "composition": "NaCl", + ... "temperature": "300 K,", + ... "experimenters": "Phill, Sally" + ... } + >>> do = DiffractionObject( + ... xarray=x, + ... yarray=y, + ... xtype="q", + ... wavelength=1.54, + ... scat_quantity="x-ray", + ... name="beach_rock_salt_1", + ... metadata=metadata + ... ) + >>> print(do.metadata) + """ self._id = uuid.uuid4() self._input_data(xarray, yarray, xtype, wavelength, scat_quantity, name, metadata) From 1af98b49dce1cd0c3e25ec82b4a756e6000c5345 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Sun, 22 Dec 2024 15:23:49 -0500 Subject: [PATCH 2/3] Fix docstring for DiffractionObject with PEP256 --- src/diffpy/utils/diffraction_objects.py | 33 ++++++++++++------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/diffpy/utils/diffraction_objects.py b/src/diffpy/utils/diffraction_objects.py index 602434f9..6870a68f 100644 --- a/src/diffpy/utils/diffraction_objects.py +++ b/src/diffpy/utils/diffraction_objects.py @@ -35,12 +35,13 @@ def _setter_wmsg(attribute): class DiffractionObject: - """ - A class to represent diffraction data for various scientific experiments involving scattering - techniques such as X-ray, neutron, and electron diffraction. This object can manage diffraction - data including transformations between different scattering quantities like q (scattering vector), - 2θ (two-theta angle), and d (interplanar spacing), and perform various operations like scaling, addition, - and subtraction of diffraction patterns. + """Class for storing and manipulating diffraction data. + + DiffractionObjct stores data produced from X-ray, neutron, and + electron scattering experiment. The object can transform between different + scattering quantities like q (scattering vector), 2θ (two-theta angle), + and d (interplanar spacing), and perform various operations like scaling, + addition, subtraction, and equality between diffraction objects. Attributes ---------- @@ -49,7 +50,7 @@ class DiffractionObject: 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. + 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 @@ -80,8 +81,7 @@ def __init__( name="", metadata={}, ): - """ - Initialize a DiffractionObject instance. + """Initialize a DiffractionObject instance. Parameters ---------- @@ -306,8 +306,8 @@ def id(self, _): raise AttributeError(_setter_wmsg("id")) def get_array_index(self, value, xtype=None): - """ - Return the index of the closest value in the array associated with the specified xtype. + """Return the index of the closest value in the array associated with + the specified xtype. Parameters ---------- @@ -370,8 +370,8 @@ def on_d(self): return [self.all_arrays[:, 3], self.all_arrays[:, 0]] def scale_to(self, target_diff_object, q=None, tth=None, d=None, offset=0): - """ - returns a new diffraction object which is the current object but rescaled in y to the target + """Returns a new diffraction object which is the current object but + rescaled in y to the target. The y-value in the target at the closest specified x-value will be used as the factor to scale to. The entire array is scaled by this factor so that one object places on top of the other at that point. @@ -412,8 +412,8 @@ def scale_to(self, target_diff_object, q=None, tth=None, d=None, offset=0): return scaled def on_xtype(self, xtype): - """ - Return a list of two 1D np array with x and y data, raise an error if the specified xtype is invalid + """Return a list of two 1D np array with x and y data, raise an error + if the specified xtype is invalid. Parameters ---------- @@ -458,8 +458,7 @@ def dump(self, filepath, xtype=None): np.savetxt(f, data_to_save, delimiter=" ") def copy(self): - """ - Create a deep copy of the DiffractionObject instance. + """Create a deep copy of the DiffractionObject instance. Returns ------- From 90672eed6dce72f62d712735ce803d6c8b221253 Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Sun, 22 Dec 2024 15:26:32 -0500 Subject: [PATCH 3/3] Fix small typo in diffractionobjet --- src/diffpy/utils/diffraction_objects.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/diffpy/utils/diffraction_objects.py b/src/diffpy/utils/diffraction_objects.py index 6870a68f..92ad17dc 100644 --- a/src/diffpy/utils/diffraction_objects.py +++ b/src/diffpy/utils/diffraction_objects.py @@ -37,11 +37,12 @@ def _setter_wmsg(attribute): class DiffractionObject: """Class for storing and manipulating diffraction data. - DiffractionObjct stores data produced from X-ray, neutron, and - electron scattering experiment. The object can transform between different - scattering quantities like q (scattering vector), 2θ (two-theta angle), - and d (interplanar spacing), and perform various operations like scaling, - addition, subtraction, and equality between diffraction objects. + DiffractionObject stores data produced from X-ray, neutron, + and electron scattering experiments. The object can transform + between different scattering quantities such as q (scattering vector), + 2θ (two-theta angle), and d (interplanar spacing), and perform various + operations like scaling, addition, subtraction, and comparison for equality + between diffraction objects. Attributes ----------