Skip to content

Commit de6f12d

Browse files
committed
make wavelength optional, test invalid init args
1 parent db89596 commit de6f12d

File tree

1 file changed

+52
-35
lines changed

1 file changed

+52
-35
lines changed

src/diffpy/utils/diffraction_objects.py

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -35,49 +35,64 @@ def _setter_wmsg(attribute):
3535

3636

3737
class DiffractionObject:
38+
"""
39+
Initialize a DiffractionObject instance.
40+
41+
Parameters
42+
----------
43+
xarray : array-like
44+
The independent variable array containing "q", "tth", or "d" values.
45+
yarray : array-like
46+
The dependent variable array corresponding to intensity values.
47+
xtype : str
48+
The type of the independent variable in `xarray`. Must be one of {*XQUANTITIES}.
49+
wavelength : float, optional
50+
The wavelength of the incoming beam, specified in angstroms (Å). Default is none.
51+
scat_quantity : str, optional
52+
The type of scattering experiment (e.g., "x-ray", "neutron"). Default is an empty string "".
53+
name : str, optional
54+
The name or label for the scattering data. Default is an empty string "".
55+
metadata : dict, optional
56+
The additional metadata associated with the diffraction object. Default is {}.
57+
58+
Examples
59+
--------
60+
Create a DiffractionObject for X-ray scattering data:
61+
62+
>>> import numpy as np
63+
>>> from diffpy.utils.diffraction_objects import DiffractionObject
64+
...
65+
>>> x = np.array([0.12, 0.24, 0.31, 0.4]) # independent variable (e.g., q)
66+
>>> y = np.array([10, 20, 40, 60]) # intensity valuester
67+
>>> metadata = {
68+
... "package_info": {"version": "3.6.0"}
69+
... }
70+
>>> do = DiffractionObject(
71+
... xarray=x,
72+
... yarray=y,
73+
... xtype="q",
74+
... wavelength=1.54,
75+
... scat_quantity="x-ray",
76+
... metadata=metadata
77+
... )
78+
>>> print(do.metadata)
79+
"""
80+
3881
def __init__(
3982
self,
4083
xarray,
4184
yarray,
4285
xtype,
43-
wavelength,
86+
wavelength=None,
4487
scat_quantity="",
4588
name="",
4689
metadata={},
4790
):
4891

4992
self._id = uuid.uuid4()
50-
self.input_data(xarray, yarray, xtype, wavelength, scat_quantity, name, metadata)
51-
52-
def input_data(self, xarray, yarray, xtype, wavelength, scat_quantity="", name="", metadata={}):
53-
"""
54-
Insert a new scattering quantity into the scattering object.
55-
56-
Parameters
57-
----------
58-
xarray : array-like
59-
The independent variable array (e.g., "q", "tth", or "d").
60-
yarray : array-like
61-
The dependent variable array corresponding to intensity values.
62-
xtype : str
63-
The type of the independent variable in `xarray`. Must be one of {*XQUANTITIES},
64-
such as "q", "tth", or "d".
65-
wavelength : float
66-
The wavelength of the incoming beam, specified in angstroms (Å).
67-
scat_quantity : str, optional
68-
The type of scattering experiment (e.g., "x-ray", "neutron"). Default is an empty string "".
69-
name : str, optional
70-
The name or label for the scattering data. Default is an empty string "".
71-
metadata : dict, optional
72-
The additional metadata associated with the diffraction object. Default is {}.
73-
74-
Returns
75-
-------
76-
None
77-
This method updates the object in place and does not return a value.
78-
"""
93+
self._input_data(xarray, yarray, xtype, wavelength, scat_quantity, name, metadata)
7994

80-
# Check xtype is valid. An empty string is the default value.
95+
def _input_data(self, xarray, yarray, xtype, wavelength, scat_quantity, name, metadata):
8196
if xtype not in XQUANTITIES:
8297
raise ValueError(_xtype_wmsg(xtype))
8398

@@ -93,10 +108,9 @@ def input_data(self, xarray, yarray, xtype, wavelength, scat_quantity="", name="
93108
self.wavelength = wavelength
94109
self.metadata = metadata
95110
self.name = name
96-
97111
self._input_xtype = xtype
98-
self._set_xarrays(xarray, xtype)
99-
self._all_arrays[:, 0] = yarray
112+
self._set_arrays(xarray, xtype, yarray)
113+
self._set_min_max_xarray()
100114

101115
def __eq__(self, other):
102116
if not isinstance(other, DiffractionObject):
@@ -346,8 +360,9 @@ def get_array_index(self, value, xtype=None):
346360
i = (np.abs(array - value)).argmin()
347361
return i
348362

349-
def _set_xarrays(self, xarray, xtype):
363+
def _set_arrays(self, xarray, xtype, yarray):
350364
self._all_arrays = np.empty(shape=(len(xarray), 4))
365+
self._all_arrays[:, 0] = yarray
351366
if xtype.lower() in QQUANTITIES:
352367
self._all_arrays[:, 1] = xarray
353368
self._all_arrays[:, 2] = q_to_tth(xarray, self.wavelength)
@@ -360,6 +375,8 @@ def _set_xarrays(self, xarray, xtype):
360375
self._all_arrays[:, 3] = xarray
361376
self._all_arrays[:, 1] = d_to_q(xarray)
362377
self._all_arrays[:, 2] = d_to_tth(xarray, self.wavelength)
378+
379+
def _set_min_max_xarray(self):
363380
self.qmin = np.nanmin(self._all_arrays[:, 1], initial=np.inf)
364381
self.qmax = np.nanmax(self._all_arrays[:, 1], initial=0.0)
365382
self.tthmin = np.nanmin(self._all_arrays[:, 2], initial=np.inf)

0 commit comments

Comments
 (0)