14
14
XQUANTITIES = ANGLEQUANTITIES + DQUANTITIES + QQUANTITIES
15
15
XUNITS = ["degrees" , "radians" , "rad" , "deg" , "inv_angs" , "inv_nm" , "nm-1" , "A-1" ]
16
16
17
- x_grid_emsg = (
18
- "objects are not on the same x-grid. You may add them using the self.add method "
19
- "and specifying how to handle the mismatch."
17
+ y_grid_length_mismatch_emsg = (
18
+ "The two objects have different y-array lengths. "
19
+ "Please ensure the length of the y-value during initialization is identical."
20
+ )
21
+
22
+ invalid_add_type_emsg = (
23
+ "You may only add a DiffractionObject with another DiffractionObject or a scalar value. "
24
+ "Please rerun by adding another DiffractionObject instance or a scalar value. "
25
+ "e.g., my_do_1 + my_do_2 or my_do + 10 or 10 + my_do"
20
26
)
21
27
22
28
@@ -169,32 +175,56 @@ def __eq__(self, other):
169
175
return True
170
176
171
177
def __add__ (self , other ):
172
- summed = deepcopy (self )
173
- if isinstance (other , int ) or isinstance (other , float ) or isinstance (other , np .ndarray ):
174
- summed .on_tth [1 ] = self .on_tth [1 ] + other
175
- summed .on_q [1 ] = self .on_q [1 ] + other
176
- elif not isinstance (other , DiffractionObject ):
177
- raise TypeError ("I only know how to sum two DiffractionObject objects" )
178
- elif self .on_tth [0 ].all () != other .on_tth [0 ].all ():
179
- raise RuntimeError (x_grid_emsg )
180
- else :
181
- summed .on_tth [1 ] = self .on_tth [1 ] + other .on_tth [1 ]
182
- summed .on_q [1 ] = self .on_q [1 ] + other .on_q [1 ]
183
- return summed
178
+ """Add a scalar value or another DiffractionObject to the yarray of the
179
+ DiffractionObject.
184
180
185
- def __radd__ (self , other ):
186
- summed = deepcopy (self )
187
- if isinstance (other , int ) or isinstance (other , float ) or isinstance (other , np .ndarray ):
188
- summed .on_tth [1 ] = self .on_tth [1 ] + other
189
- summed .on_q [1 ] = self .on_q [1 ] + other
190
- elif not isinstance (other , DiffractionObject ):
191
- raise TypeError ("I only know how to sum two Scattering_object objects" )
192
- elif self .on_tth [0 ].all () != other .on_tth [0 ].all ():
193
- raise RuntimeError (x_grid_emsg )
194
- else :
195
- summed .on_tth [1 ] = self .on_tth [1 ] + other .on_tth [1 ]
196
- summed .on_q [1 ] = self .on_q [1 ] + other .on_q [1 ]
197
- return summed
181
+ Parameters
182
+ ----------
183
+ other : DiffractionObject or int or float
184
+ The object to add to the current DiffractionObject. If `other` is a scalar value,
185
+ it will be added to all yarray. The length of the yarray must match if `other` is
186
+ an instance of DiffractionObject.
187
+
188
+ Returns
189
+ -------
190
+ DiffractionObject
191
+ The new and deep-copied DiffractionObject instance after adding values to the yarray.
192
+
193
+ Raises
194
+ ------
195
+ ValueError
196
+ Raised when the length of the yarray of the two DiffractionObject instances do not match.
197
+ TypeError
198
+ Raised when the type of `other` is not an instance of DiffractionObject, int, or float.
199
+
200
+ Examples
201
+ --------
202
+ Add a scalar value to the yarray of the DiffractionObject instance:
203
+ >>> new_do = my_do + 10.1
204
+ >>> new_do = 10.1 + my_do
205
+
206
+ Add the yarray of two DiffractionObject instances:
207
+ >>> new_do = my_do_1 + my_do_2
208
+ """
209
+
210
+ self ._check_operation_compatibility (other )
211
+ summed_do = deepcopy (self )
212
+ if isinstance (other , (int , float )):
213
+ summed_do ._all_arrays [:, 0 ] += other
214
+ if isinstance (other , DiffractionObject ):
215
+ summed_do ._all_arrays [:, 0 ] += other .all_arrays [:, 0 ]
216
+ return summed_do
217
+
218
+ __radd__ = __add__
219
+
220
+ def _check_operation_compatibility (self , other ):
221
+ if not isinstance (other , (DiffractionObject , int , float )):
222
+ raise TypeError (invalid_add_type_emsg )
223
+ if isinstance (other , DiffractionObject ):
224
+ self_yarray = self .all_arrays [:, 0 ]
225
+ other_yarray = other .all_arrays [:, 0 ]
226
+ if len (self_yarray ) != len (other_yarray ):
227
+ raise ValueError (y_grid_length_mismatch_emsg )
198
228
199
229
def __sub__ (self , other ):
200
230
subtracted = deepcopy (self )
@@ -204,7 +234,7 @@ def __sub__(self, other):
204
234
elif not isinstance (other , DiffractionObject ):
205
235
raise TypeError ("I only know how to subtract two Scattering_object objects" )
206
236
elif self .on_tth [0 ].all () != other .on_tth [0 ].all ():
207
- raise RuntimeError (x_grid_emsg )
237
+ raise RuntimeError (y_grid_length_mismatch_emsg )
208
238
else :
209
239
subtracted .on_tth [1 ] = self .on_tth [1 ] - other .on_tth [1 ]
210
240
subtracted .on_q [1 ] = self .on_q [1 ] - other .on_q [1 ]
@@ -218,7 +248,7 @@ def __rsub__(self, other):
218
248
elif not isinstance (other , DiffractionObject ):
219
249
raise TypeError ("I only know how to subtract two Scattering_object objects" )
220
250
elif self .on_tth [0 ].all () != other .on_tth [0 ].all ():
221
- raise RuntimeError (x_grid_emsg )
251
+ raise RuntimeError (y_grid_length_mismatch_emsg )
222
252
else :
223
253
subtracted .on_tth [1 ] = other .on_tth [1 ] - self .on_tth [1 ]
224
254
subtracted .on_q [1 ] = other .on_q [1 ] - self .on_q [1 ]
@@ -232,7 +262,7 @@ def __mul__(self, other):
232
262
elif not isinstance (other , DiffractionObject ):
233
263
raise TypeError ("I only know how to multiply two Scattering_object objects" )
234
264
elif self .on_tth [0 ].all () != other .on_tth [0 ].all ():
235
- raise RuntimeError (x_grid_emsg )
265
+ raise RuntimeError (y_grid_length_mismatch_emsg )
236
266
else :
237
267
multiplied .on_tth [1 ] = self .on_tth [1 ] * other .on_tth [1 ]
238
268
multiplied .on_q [1 ] = self .on_q [1 ] * other .on_q [1 ]
@@ -244,7 +274,7 @@ def __rmul__(self, other):
244
274
multiplied .on_tth [1 ] = other * self .on_tth [1 ]
245
275
multiplied .on_q [1 ] = other * self .on_q [1 ]
246
276
elif self .on_tth [0 ].all () != other .on_tth [0 ].all ():
247
- raise RuntimeError (x_grid_emsg )
277
+ raise RuntimeError (y_grid_length_mismatch_emsg )
248
278
else :
249
279
multiplied .on_tth [1 ] = self .on_tth [1 ] * other .on_tth [1 ]
250
280
multiplied .on_q [1 ] = self .on_q [1 ] * other .on_q [1 ]
@@ -258,7 +288,7 @@ def __truediv__(self, other):
258
288
elif not isinstance (other , DiffractionObject ):
259
289
raise TypeError ("I only know how to multiply two Scattering_object objects" )
260
290
elif self .on_tth [0 ].all () != other .on_tth [0 ].all ():
261
- raise RuntimeError (x_grid_emsg )
291
+ raise RuntimeError (y_grid_length_mismatch_emsg )
262
292
else :
263
293
divided .on_tth [1 ] = self .on_tth [1 ] / other .on_tth [1 ]
264
294
divided .on_q [1 ] = self .on_q [1 ] / other .on_q [1 ]
@@ -270,7 +300,7 @@ def __rtruediv__(self, other):
270
300
divided .on_tth [1 ] = other / self .on_tth [1 ]
271
301
divided .on_q [1 ] = other / self .on_q [1 ]
272
302
elif self .on_tth [0 ].all () != other .on_tth [0 ].all ():
273
- raise RuntimeError (x_grid_emsg )
303
+ raise RuntimeError (y_grid_length_mismatch_emsg )
274
304
else :
275
305
divided .on_tth [1 ] = other .on_tth [1 ] / self .on_tth [1 ]
276
306
divided .on_q [1 ] = other .on_q [1 ] / self .on_q [1 ]
0 commit comments