@@ -182,6 +182,158 @@ def test_init_invalid_xtype():
182
182
DiffractionObject (xtype = "invalid_type" )
183
183
184
184
185
+ params_scale_to = [
186
+ # UC1: same x-array and y-array, check offset
187
+ (
188
+ {
189
+ "xarray" : np .array ([10 , 15 , 25 , 30 , 60 , 140 ]),
190
+ "yarray" : np .array ([2 , 3 , 4 , 5 , 6 , 7 ]),
191
+ "xtype" : "tth" ,
192
+ "wavelength" : 2 * np .pi ,
193
+ "target_xarray" : np .array ([10 , 15 , 25 , 30 , 60 , 140 ]),
194
+ "target_yarray" : np .array ([2 , 3 , 4 , 5 , 6 , 7 ]),
195
+ "target_xtype" : "tth" ,
196
+ "target_wavelength" : 2 * np .pi ,
197
+ "q" : None ,
198
+ "tth" : 60 ,
199
+ "d" : None ,
200
+ "offset" : 2.1 ,
201
+ },
202
+ {"xtype" : "tth" , "yarray" : np .array ([4.1 , 5.1 , 6.1 , 7.1 , 8.1 , 9.1 ])},
203
+ ),
204
+ # UC2: same length x-arrays with exact x-value match
205
+ (
206
+ {
207
+ "xarray" : np .array ([10 , 15 , 25 , 30 , 60 , 140 ]),
208
+ "yarray" : np .array ([10 , 20 , 25 , 30 , 60 , 100 ]),
209
+ "xtype" : "tth" ,
210
+ "wavelength" : 2 * np .pi ,
211
+ "target_xarray" : np .array ([10 , 20 , 25 , 30 , 60 , 140 ]),
212
+ "target_yarray" : np .array ([2 , 3 , 4 , 5 , 6 , 7 ]),
213
+ "target_xtype" : "tth" ,
214
+ "target_wavelength" : 2 * np .pi ,
215
+ "q" : None ,
216
+ "tth" : 60 ,
217
+ "d" : None ,
218
+ "offset" : 0 ,
219
+ },
220
+ {"xtype" : "tth" , "yarray" : np .array ([1 , 2 , 2.5 , 3 , 6 , 10 ])},
221
+ ),
222
+ # UC3: same length x-arrays with approximate x-value match
223
+ (
224
+ {
225
+ "xarray" : np .array ([0.12 , 0.24 , 0.31 , 0.4 ]),
226
+ "yarray" : np .array ([10 , 20 , 40 , 60 ]),
227
+ "xtype" : "q" ,
228
+ "wavelength" : 2 * np .pi ,
229
+ "target_xarray" : np .array ([0.14 , 0.24 , 0.31 , 0.4 ]),
230
+ "target_yarray" : np .array ([1 , 3 , 4 , 5 ]),
231
+ "target_xtype" : "q" ,
232
+ "target_wavelength" : 2 * np .pi ,
233
+ "q" : 0.1 ,
234
+ "tth" : None ,
235
+ "d" : None ,
236
+ "offset" : 0 ,
237
+ },
238
+ {"xtype" : "q" , "yarray" : np .array ([1 , 2 , 4 , 6 ])},
239
+ ),
240
+ # UC4: different x-array lengths with approximate x-value match
241
+ (
242
+ {
243
+ "xarray" : np .array ([10 , 25 , 30.1 , 40.2 , 61 , 120 , 140 ]),
244
+ "yarray" : np .array ([10 , 20 , 30 , 40 , 50 , 60 , 100 ]),
245
+ "xtype" : "tth" ,
246
+ "wavelength" : 2 * np .pi ,
247
+ "target_xarray" : np .array ([20 , 25.5 , 32 , 45 , 50 , 62 , 100 , 125 , 140 ]),
248
+ "target_yarray" : np .array ([1.1 , 2 , 3 , 3.5 , 4 , 5 , 10 , 12 , 13 ]),
249
+ "target_xtype" : "tth" ,
250
+ "target_wavelength" : 2 * np .pi ,
251
+ "q" : None ,
252
+ "tth" : 60 ,
253
+ "d" : None ,
254
+ "offset" : 0 ,
255
+ },
256
+ # scaling factor is calculated at index = 4 (tth=61) for self and index = 5 for target (tth=62)
257
+ {"xtype" : "tth" , "yarray" : np .array ([1 , 2 , 3 , 4 , 5 , 6 , 10 ])},
258
+ ),
259
+ ]
260
+
261
+
262
+ @pytest .mark .parametrize ("inputs, expected" , params_scale_to )
263
+ def test_scale_to (inputs , expected ):
264
+ orig_diff_object = DiffractionObject (
265
+ xarray = inputs ["xarray" ], yarray = inputs ["yarray" ], xtype = inputs ["xtype" ], wavelength = inputs ["wavelength" ]
266
+ )
267
+ target_diff_object = DiffractionObject (
268
+ xarray = inputs ["target_xarray" ],
269
+ yarray = inputs ["target_yarray" ],
270
+ xtype = inputs ["target_xtype" ],
271
+ wavelength = inputs ["target_wavelength" ],
272
+ )
273
+ scaled_diff_object = orig_diff_object .scale_to (
274
+ target_diff_object , q = inputs ["q" ], tth = inputs ["tth" ], d = inputs ["d" ], offset = inputs ["offset" ]
275
+ )
276
+ # Check the intensity data is the same as expected
277
+ assert np .allclose (scaled_diff_object .on_xtype (expected ["xtype" ])[1 ], expected ["yarray" ])
278
+
279
+
280
+ params_scale_to_bad = [
281
+ # UC1: user did not specify anything
282
+ (
283
+ {
284
+ "xarray" : np .array ([0.1 , 0.2 , 0.3 ]),
285
+ "yarray" : np .array ([1 , 2 , 3 ]),
286
+ "xtype" : "q" ,
287
+ "wavelength" : 2 * np .pi ,
288
+ "target_xarray" : np .array ([0.05 , 0.1 , 0.2 , 0.3 ]),
289
+ "target_yarray" : np .array ([5 , 10 , 20 , 30 ]),
290
+ "target_xtype" : "q" ,
291
+ "target_wavelength" : 2 * np .pi ,
292
+ "q" : None ,
293
+ "tth" : None ,
294
+ "d" : None ,
295
+ "offset" : 0 ,
296
+ }
297
+ ),
298
+ # UC2: user specified more than one of q, tth, and d
299
+ (
300
+ {
301
+ "xarray" : np .array ([10 , 25 , 30.1 , 40.2 , 61 , 120 , 140 ]),
302
+ "yarray" : np .array ([10 , 20 , 30 , 40 , 50 , 60 , 100 ]),
303
+ "xtype" : "tth" ,
304
+ "wavelength" : 2 * np .pi ,
305
+ "target_xarray" : np .array ([20 , 25.5 , 32 , 45 , 50 , 62 , 100 , 125 , 140 ]),
306
+ "target_yarray" : np .array ([1.1 , 2 , 3 , 3.5 , 4 , 5 , 10 , 12 , 13 ]),
307
+ "target_xtype" : "tth" ,
308
+ "target_wavelength" : 2 * np .pi ,
309
+ "q" : None ,
310
+ "tth" : 60 ,
311
+ "d" : 10 ,
312
+ "offset" : 0 ,
313
+ }
314
+ ),
315
+ ]
316
+
317
+
318
+ @pytest .mark .parametrize ("inputs" , params_scale_to_bad )
319
+ def test_scale_to_bad (inputs ):
320
+ orig_diff_object = DiffractionObject (
321
+ xarray = inputs ["xarray" ], yarray = inputs ["yarray" ], xtype = inputs ["xtype" ], wavelength = inputs ["wavelength" ]
322
+ )
323
+ target_diff_object = DiffractionObject (
324
+ xarray = inputs ["target_xarray" ],
325
+ yarray = inputs ["target_yarray" ],
326
+ xtype = inputs ["target_xtype" ],
327
+ wavelength = inputs ["target_wavelength" ],
328
+ )
329
+ with pytest .raises (
330
+ ValueError , match = "You must specify exactly one of 'q', 'tth', or 'd'. Please rerun specifying only one."
331
+ ):
332
+ orig_diff_object .scale_to (
333
+ target_diff_object , q = inputs ["q" ], tth = inputs ["tth" ], d = inputs ["d" ], offset = inputs ["offset" ]
334
+ )
335
+
336
+
185
337
params_index = [
186
338
# UC1: exact match
187
339
(4 * np .pi , np .array ([30.005 , 60 ]), np .array ([1 , 2 ]), "tth" , "tth" , 30.005 , [0 ]),
0 commit comments