21
21
22
22
def _xtype_wmsg (xtype ):
23
23
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 'insert_scattering_quantity' to modify '{ attribute } '." ,
26
33
)
27
34
28
35
@@ -45,6 +52,7 @@ def __init__(
45
52
xarray = np .empty (0 )
46
53
if yarray is None :
47
54
yarray = np .empty (0 )
55
+
48
56
self .insert_scattering_quantity (xarray , yarray , xtype )
49
57
50
58
def __eq__ (self , other ):
@@ -186,11 +194,16 @@ def all_arrays(self):
186
194
return self ._all_arrays
187
195
188
196
@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" ))
194
207
195
208
def set_angles_from_list (self , angles_list ):
196
209
self .angles = angles_list
@@ -261,7 +274,7 @@ def _set_array_from_range(self, begin, end, step_size=None, n_steps=None):
261
274
262
275
def get_array_index (self , value , xtype = None ):
263
276
"""
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.
265
278
266
279
Parameters
267
280
----------
@@ -276,7 +289,7 @@ def get_array_index(self, value, xtype=None):
276
289
"""
277
290
278
291
if xtype is None :
279
- xtype = self .input_xtype
292
+ xtype = self ._input_xtype
280
293
array = self .on_xtype (xtype )[0 ]
281
294
if len (array ) == 0 :
282
295
raise ValueError (f"The '{ xtype } ' array is empty. Please ensure it is initialized." )
@@ -333,9 +346,18 @@ def insert_scattering_quantity(
333
346
Nothing. Updates the object in place.
334
347
335
348
"""
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 'insert_scattering_quantity' "
355
+ "with 'xarray' and 'yarray' of identical length."
356
+ )
357
+
336
358
self ._set_xarrays (xarray , xtype )
337
359
self ._all_arrays [:, 0 ] = yarray
338
- self .input_xtype = xtype
360
+ self ._input_xtype = xtype
339
361
# only update these optional values if non-empty quantities are passed to avoid overwriting
340
362
# valid data inadvertently
341
363
if metadata :
@@ -347,12 +369,17 @@ def insert_scattering_quantity(
347
369
if wavelength is not None :
348
370
self .wavelength = wavelength
349
371
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
+
350
377
def _get_original_array (self ):
351
- if self .input_xtype in QQUANTITIES :
378
+ if self ._input_xtype in QQUANTITIES :
352
379
return self .on_q (), "q"
353
- elif self .input_xtype in ANGLEQUANTITIES :
380
+ elif self ._input_xtype in ANGLEQUANTITIES :
354
381
return self .on_tth (), "tth"
355
- elif self .input_xtype in DQUANTITIES :
382
+ elif self ._input_xtype in DQUANTITIES :
356
383
return self .on_d (), "d"
357
384
358
385
def on_q (self ):
@@ -365,8 +392,8 @@ def on_d(self):
365
392
return [self .all_arrays [:, 3 ], self .all_arrays [:, 0 ]]
366
393
367
394
def scale_to (self , target_diff_object , xtype = None , xvalue = None ):
368
- f """
369
- returns a new diffraction object which is the current object but recaled in y to the target
395
+ """
396
+ Return a new diffraction object which is the current object but recaled in y to the target
370
397
371
398
Parameters
372
399
----------
@@ -401,8 +428,8 @@ def scale_to(self, target_diff_object, xtype=None, xvalue=None):
401
428
return scaled
402
429
403
430
def on_xtype (self , xtype ):
404
- f """
405
- return a list of two 1D np array with x and y data, raise an error if the specified xtype is invalid
431
+ """
432
+ Return a list of two 1D np array with x and y data, raise an error if the specified xtype is invalid
406
433
407
434
Parameters
408
435
----------
0 commit comments