Skip to content

Commit 56541e5

Browse files
committed
Refactor and remove input_data func
1 parent 92a2c4c commit 56541e5

File tree

1 file changed

+26
-74
lines changed

1 file changed

+26
-74
lines changed

src/diffpy/utils/diffraction_objects.py

Lines changed: 26 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,36 @@ def _setter_wmsg(attribute):
3636

3737
class DiffractionObject:
3838
def __init__(
39-
self, name=None, wavelength=None, scat_quantity=None, metadata=None, xarray=None, yarray=None, xtype=None
39+
self,
40+
xarray,
41+
yarray,
42+
xtype,
43+
wavelength,
44+
scat_quantity="",
45+
name="",
46+
metadata={},
4047
):
41-
if name is None:
42-
name = ""
43-
self.name = name
44-
if metadata is None:
45-
metadata = {}
46-
self.metadata = metadata
47-
if xtype is None:
48-
xtype = ""
48+
# Check xtype is valid. An empty string is the default value.
49+
if xtype not in XQUANTITIES:
50+
raise ValueError(_xtype_wmsg(xtype))
51+
52+
# Check xarray and yarray have the same length
53+
if len(xarray) != len(yarray):
54+
raise ValueError(
55+
"'xarray' and 'yarray' must have the same length. "
56+
"Please re-initialize 'DiffractionObject' or re-run the method 'input_data' "
57+
"with 'xarray' and 'yarray' of identical length."
58+
)
59+
4960
self.scat_quantity = scat_quantity
5061
self.wavelength = wavelength
62+
self.metadata = metadata
63+
self.name = name
5164

52-
if xarray is None:
53-
xarray = np.empty(0)
54-
if yarray is None:
55-
yarray = np.empty(0)
56-
65+
self._input_xtype = xtype
5766
self._id = uuid.uuid4()
58-
self.input_data(xarray, yarray, xtype)
67+
self._set_xarrays(xarray, xtype)
68+
self._all_arrays[:, 0] = yarray
5969

6070
def __eq__(self, other):
6171
if not isinstance(other, DiffractionObject):
@@ -298,8 +308,7 @@ def get_array_index(self, value, xtype=None):
298308
the index of the value in the array
299309
"""
300310

301-
if xtype is None:
302-
xtype = self._input_xtype
311+
xtype = self._input_xtype
303312
array = self.on_xtype(xtype)[0]
304313
if len(array) == 0:
305314
raise ValueError(f"The '{xtype}' array is empty. Please ensure it is initialized.")
@@ -327,63 +336,6 @@ def _set_xarrays(self, xarray, xtype):
327336
self.dmin = np.nanmin(self._all_arrays[:, 3], initial=np.inf)
328337
self.dmax = np.nanmax(self._all_arrays[:, 3], initial=0.0)
329338

330-
def input_data(
331-
self,
332-
xarray,
333-
yarray,
334-
xtype,
335-
metadata={},
336-
scat_quantity=None,
337-
name=None,
338-
wavelength=None,
339-
):
340-
f"""
341-
insert a new scattering quantity into the scattering object
342-
343-
Parameters
344-
----------
345-
xarray array-like of floats
346-
the independent variable array
347-
yarray array-like of floats
348-
the dependent variable array
349-
xtype string
350-
the type of quantity for the independent variable from {*XQUANTITIES, }
351-
metadata, scat_quantity, name and wavelength are optional. They have the same
352-
meaning as in the constructor. Values will only be overwritten if non-empty values are passed.
353-
354-
Returns
355-
-------
356-
Nothing. Updates the object in place.
357-
358-
"""
359-
360-
# Check xarray and yarray have the same length
361-
if len(xarray) != len(yarray):
362-
raise ValueError(
363-
"'xarray' and 'yarray' must have the same length. "
364-
"Please re-initialize 'DiffractionObject' or re-run the method 'input_data' "
365-
"with 'xarray' and 'yarray' of identical length."
366-
)
367-
368-
self._set_xarrays(xarray, xtype)
369-
self._all_arrays[:, 0] = yarray
370-
self._input_xtype = xtype
371-
# only update these optional values if non-empty quantities are passed to avoid overwriting
372-
# valid data inadvertently
373-
if metadata:
374-
self.metadata = metadata
375-
if scat_quantity is not None:
376-
self.scat_quantity = scat_quantity
377-
if name is not None:
378-
self.name = name
379-
if wavelength is not None:
380-
self.wavelength = wavelength
381-
382-
# Check xtype is valid. An empty string is the default value.
383-
if xtype != "":
384-
if xtype not in XQUANTITIES:
385-
raise ValueError(_xtype_wmsg(xtype))
386-
387339
def _get_original_array(self):
388340
if self._input_xtype in QQUANTITIES:
389341
return self.on_q(), "q"

0 commit comments

Comments
 (0)