Skip to content

Commit ee190b9

Browse files
committed
valid xtype and use getter/setting for xtype
1 parent d8d7fb3 commit ee190b9

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

src/diffpy/utils/diffraction_objects.py

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ def _xtype_wmsg(xtype):
2626
)
2727

2828

29+
def _setter_wmsg(attribute):
30+
return (
31+
f"Direct modification of attribute '{attribute}' is not allowed."
32+
f"Please use 'insert_scattering_quantity' to modify '{attribute}'.",
33+
)
34+
35+
2936
class DiffractionObject:
3037
def __init__(
3138
self, name=None, wavelength=None, scat_quantity=None, metadata=None, xarray=None, yarray=None, xtype=None
@@ -45,6 +52,7 @@ def __init__(
4552
xarray = np.empty(0)
4653
if yarray is None:
4754
yarray = np.empty(0)
55+
4856
self.insert_scattering_quantity(xarray, yarray, xtype)
4957

5058
def __eq__(self, other):
@@ -186,11 +194,16 @@ def all_arrays(self):
186194
return self._all_arrays
187195

188196
@all_arrays.setter
189-
def all_arrays(self, value):
190-
raise AttributeError(
191-
"Direct modification of attribute 'all_arrays' is not allowed."
192-
"Please use 'insert_scattering_quantity' to modify `all_arrays`."
193-
)
197+
def all_arrays(self, _):
198+
raise AttributeError(_setter_wmsg("all_arrays"))
199+
200+
@property
201+
def xtype(self):
202+
return self._xtype
203+
204+
@xtype.setter
205+
def xtype(self, _):
206+
raise AttributeError(_setter_wmsg("xtype"))
194207

195208
def set_angles_from_list(self, angles_list):
196209
self.angles = angles_list
@@ -261,7 +274,7 @@ def _set_array_from_range(self, begin, end, step_size=None, n_steps=None):
261274

262275
def get_array_index(self, value, xtype=None):
263276
"""
264-
returns the index of the closest value in the array associated with the specified xtype
277+
Return the index of the closest value in the array associated with the specified xtype.
265278
266279
Parameters
267280
----------
@@ -276,7 +289,7 @@ def get_array_index(self, value, xtype=None):
276289
"""
277290

278291
if xtype is None:
279-
xtype = self.input_xtype
292+
xtype = self._xtype
280293
array = self.on_xtype(xtype)[0]
281294
if len(array) == 0:
282295
raise ValueError(f"The '{xtype}' array is empty. Please ensure it is initialized.")
@@ -335,7 +348,7 @@ def insert_scattering_quantity(
335348
"""
336349
self._set_xarrays(xarray, xtype)
337350
self._all_arrays[:, 0] = yarray
338-
self.input_xtype = xtype
351+
self._xtype = xtype
339352
# only update these optional values if non-empty quantities are passed to avoid overwriting
340353
# valid data inadvertently
341354
if metadata:
@@ -347,12 +360,25 @@ def insert_scattering_quantity(
347360
if wavelength is not None:
348361
self.wavelength = wavelength
349362

363+
# Check xarray and yarray have the same length
364+
if len(xarray) != len(yarray):
365+
raise ValueError(
366+
"`xarray` and `yarray` must have the same length. "
367+
"Please re-initialize `DiffractionObject` or re-run the method `insert_scattering_quantity` "
368+
"with `xarray` and `yarray` of identical length."
369+
)
370+
371+
# Check xtype is valid. An empty string is the default value.
372+
if xtype != "":
373+
if xtype not in XQUANTITIES:
374+
raise ValueError(_xtype_wmsg(xtype))
375+
350376
def _get_original_array(self):
351-
if self.input_xtype in QQUANTITIES:
377+
if self._xtype in QQUANTITIES:
352378
return self.on_q(), "q"
353-
elif self.input_xtype in ANGLEQUANTITIES:
379+
elif self._xtype in ANGLEQUANTITIES:
354380
return self.on_tth(), "tth"
355-
elif self.input_xtype in DQUANTITIES:
381+
elif self._xtype in DQUANTITIES:
356382
return self.on_d(), "d"
357383

358384
def on_q(self):

0 commit comments

Comments
 (0)