2121
2222def _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 'insert_scattering_quantity' to modify '{ attribute } '." ,
2633 )
2734
2835
@@ -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 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." )
@@ -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 'insert_scattering_quantity' "
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 ):
@@ -365,8 +392,8 @@ def on_d(self):
365392 return [self .all_arrays [:, 3 ], self .all_arrays [:, 0 ]]
366393
367394 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
370397
371398 Parameters
372399 ----------
@@ -401,8 +428,8 @@ def scale_to(self, target_diff_object, xtype=None, xvalue=None):
401428 return scaled
402429
403430 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
406433
407434 Parameters
408435 ----------
0 commit comments