Skip to content

Commit 409cd42

Browse files
add error msg
2 parents 3c5a426 + 5f67f6c commit 409cd42

File tree

4 files changed

+175
-81
lines changed

4 files changed

+175
-81
lines changed

news/mv-input-scattering-quan.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* <news item>
4+
5+
**Changed:**
6+
7+
* Rename `input_scattering_quantity` to `input_data` in `DiffractionObject` init
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

news/scattering-obj-valid.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* validate xtype belongs to XQUANTITIES during DiffractionObject init
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

src/diffpy/utils/diffraction_objects.py

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,15 @@
2121

2222
def _xtype_wmsg(xtype):
2323
return (
24-
f"I don't know how to handle the xtype, '{xtype}'. Please rerun specifying an "
25-
f"xtype from {*XQUANTITIES, }"
24+
f"I don't know how to handle the xtype, '{xtype}'. "
25+
f"Please rerun specifying an xtype from {*XQUANTITIES, }"
26+
)
27+
28+
29+
def _setter_wmsg(attribute):
30+
return (
31+
f"Direct modification of attribute '{attribute}' is not allowed. "
32+
f"Please use 'input_data' to modify '{attribute}'.",
2633
)
2734

2835

@@ -45,7 +52,8 @@ def __init__(
4552
xarray = np.empty(0)
4653
if yarray is None:
4754
yarray = np.empty(0)
48-
self.insert_scattering_quantity(xarray, yarray, xtype)
55+
56+
self.input_data(xarray, yarray, xtype)
4957

5058
def __eq__(self, other):
5159
if not isinstance(other, DiffractionObject):
@@ -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 input_xtype(self):
202+
return self._input_xtype
203+
204+
@input_xtype.setter
205+
def input_xtype(self, _):
206+
raise AttributeError(_setter_wmsg("input_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._input_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.")
@@ -304,7 +317,7 @@ def _set_xarrays(self, xarray, xtype):
304317
self.dmin = np.nanmin(self._all_arrays[:, 3], initial=np.inf)
305318
self.dmax = np.nanmax(self._all_arrays[:, 3], initial=0.0)
306319

307-
def insert_scattering_quantity(
320+
def input_data(
308321
self,
309322
xarray,
310323
yarray,
@@ -333,9 +346,18 @@ def insert_scattering_quantity(
333346
Nothing. Updates the object in place.
334347
335348
"""
349+
350+
# Check xarray and yarray have the same length
351+
if len(xarray) != len(yarray):
352+
raise ValueError(
353+
"'xarray' and 'yarray' must have the same length. "
354+
"Please re-initialize 'DiffractionObject' or re-run the method 'input_data' "
355+
"with 'xarray' and 'yarray' of identical length."
356+
)
357+
336358
self._set_xarrays(xarray, xtype)
337359
self._all_arrays[:, 0] = yarray
338-
self.input_xtype = xtype
360+
self._input_xtype = xtype
339361
# only update these optional values if non-empty quantities are passed to avoid overwriting
340362
# valid data inadvertently
341363
if metadata:
@@ -347,12 +369,17 @@ def insert_scattering_quantity(
347369
if wavelength is not None:
348370
self.wavelength = wavelength
349371

372+
# Check xtype is valid. An empty string is the default value.
373+
if xtype != "":
374+
if xtype not in XQUANTITIES:
375+
raise ValueError(_xtype_wmsg(xtype))
376+
350377
def _get_original_array(self):
351-
if self.input_xtype in QQUANTITIES:
378+
if self._input_xtype in QQUANTITIES:
352379
return self.on_q(), "q"
353-
elif self.input_xtype in ANGLEQUANTITIES:
380+
elif self._input_xtype in ANGLEQUANTITIES:
354381
return self.on_tth(), "tth"
355-
elif self.input_xtype in DQUANTITIES:
382+
elif self._input_xtype in DQUANTITIES:
356383
return self.on_d(), "d"
357384

358385
def on_q(self):
@@ -389,6 +416,10 @@ def scale_to(self, target_diff_object, q=None, tth=None, d=None, offset=0):
389416
the rescaled DiffractionObject as a new object
390417
"""
391418
scaled = self.copy()
419+
count = sum([q is not None, tth is not None, d is not None])
420+
if count > 1:
421+
raise ValueError("You can only specify one of 'q', 'tth', or 'd'. Please rerun specifying only one.")
422+
392423
xtype = "q" if q is not None else "tth" if tth is not None else "d" if d is not None else "q"
393424
data, target = self.on_xtype(xtype), target_diff_object.on_xtype(xtype)
394425

@@ -402,8 +433,8 @@ def scale_to(self, target_diff_object, q=None, tth=None, d=None, offset=0):
402433
return scaled
403434

404435
def on_xtype(self, xtype):
405-
f"""
406-
return a list of two 1D np array with x and y data, raise an error if the specified xtype is invalid
436+
"""
437+
Return a list of two 1D np array with x and y data, raise an error if the specified xtype is invalid
407438
408439
Parameters
409440
----------

0 commit comments

Comments
 (0)