Skip to content

Commit 630011a

Browse files
refactor: reorder docstring and tests, change offset default to None, update tests for flexible inputs
1 parent 2c8335c commit 630011a

File tree

2 files changed

+34
-66
lines changed

2 files changed

+34
-66
lines changed

src/diffpy/utils/diffraction_objects.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,32 +396,35 @@ def on_tth(self):
396396
def on_d(self):
397397
return [self.all_arrays[:, 3], self.all_arrays[:, 0]]
398398

399-
def scale_to(self, target_diff_object, q=None, tth=None, d=None, offset=0):
399+
def scale_to(self, target_diff_object, q=None, tth=None, d=None, offset=None):
400400
"""Returns a new diffraction object which is the current object but
401401
rescaled in y to the target.
402402
403+
By default, if none of `q`, `tth`, or `d` are provided,
404+
the scaling is based on the maximal x-array value from both objects.
403405
The y-value in the target at the closest specified x-value will be used as the factor to scale to.
404406
The entire array is scaled by this factor so that one object places on top of the other at that point.
405-
If none of `q`, `tth`, or `d` are provided,
406-
the scaling will be based on the maximal x-array value from both objects.
407407
If multiple values of `q`, `tth`, or `d` are provided, an error will be raised.
408408
409409
Parameters
410410
----------
411411
target_diff_object: DiffractionObject
412412
the diffraction object you want to scale the current one onto
413413
414-
q, tth, d : float, optional, default is q with the maximal x-array value of the current object
414+
q, tth, d : float, optional, default is q with the maximal x-array value from each object
415415
The value of the x-array where you want the curves to line up vertically.
416416
Specify a value on one of the allowed grids, q, tth, or d), e.g., q=10.
417417
418-
offset : float, optional, default is 0
418+
offset : float, optional, default is None
419419
an offset to add to the scaled y-values
420420
421421
Returns
422422
-------
423423
the rescaled DiffractionObject as a new object
424424
"""
425+
if offset is None:
426+
offset = 0
427+
425428
scaled = self.copy()
426429
count = sum([q is not None, tth is not None, d is not None])
427430
if count > 1:

tests/test_diffraction_objects.py

Lines changed: 26 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,23 @@ def test_init_invalid_xtype():
191191
"org_do_args, target_do_args, scale_inputs, expected",
192192
[
193193
# Test whether the original y-array is scaled as expected
194-
( # C1: Same x-arrays
194+
( # C1: none of q, tth, d, provided, expect to scale on the maximal x-arrays
195+
{
196+
"xarray": np.array([0.1, 0.2, 0.3]),
197+
"yarray": np.array([1, 2, 3]),
198+
"xtype": "q",
199+
"wavelength": 2 * np.pi,
200+
},
201+
{
202+
"xarray": np.array([0.05, 0.1, 0.2, 0.3]),
203+
"yarray": np.array([5, 10, 20, 30]),
204+
"xtype": "q",
205+
"wavelength": 2 * np.pi,
206+
},
207+
{},
208+
{"xtype": "q", "yarray": np.array([10, 20, 30])},
209+
),
210+
( # C2: Same x-arrays
195211
# x-value has exact matches at tth=60 (y=60) and tth=60 (y=6),
196212
# for original and target diffraction objects,
197213
# expect original y-array to multiply by 6/60=1/10
@@ -207,15 +223,10 @@ def test_init_invalid_xtype():
207223
"xtype": "tth",
208224
"wavelength": 2 * np.pi,
209225
},
210-
{
211-
"q": None,
212-
"tth": 60,
213-
"d": None,
214-
"offset": 0,
215-
},
226+
{"tth": 60},
216227
{"xtype": "tth", "yarray": np.array([1, 2, 2.5, 3, 6, 10])},
217228
),
218-
( # C2: Different x-arrays with same length,
229+
( # C3: Different x-arrays with same length,
219230
# x-value has closest match at q=0.12 (y=10) and q=0.14 (y=1)
220231
# for original and target diffraction objects,
221232
# expect original y-array to multiply by 1/10
@@ -231,15 +242,10 @@ def test_init_invalid_xtype():
231242
"xtype": "q",
232243
"wavelength": 2 * np.pi,
233244
},
234-
{
235-
"q": 0.1,
236-
"tth": None,
237-
"d": None,
238-
"offset": 0,
239-
},
245+
{"q": 0.1},
240246
{"xtype": "q", "yarray": np.array([1, 2, 4, 6])},
241247
),
242-
( # C3: Different x-array lengths
248+
( # C4: Different x-array lengths
243249
# x-value has closest matches at tth=61 (y=50) and tth=62 (y=5),
244250
# for original and target diffraction objects,
245251
# expect original y-array to multiply by 5/50=1/10
@@ -255,15 +261,10 @@ def test_init_invalid_xtype():
255261
"xtype": "tth",
256262
"wavelength": 2 * np.pi,
257263
},
258-
{
259-
"q": None,
260-
"tth": 60,
261-
"d": None,
262-
"offset": 0,
263-
},
264+
{"tth": 60},
264265
{"xtype": "tth", "yarray": np.array([1, 2, 3, 4, 5, 6, 10])},
265266
),
266-
( # C4: Same x-array and y-array with 2.1 offset, expect y-array to shift up by 2.1
267+
( # C5: Same x-array and y-array with 2.1 offset, expect y-array to shift up by 2.1
267268
{
268269
"xarray": np.array([10, 15, 25, 30, 60, 140]),
269270
"yarray": np.array([2, 3, 4, 5, 6, 7]),
@@ -276,43 +277,15 @@ def test_init_invalid_xtype():
276277
"xtype": "tth",
277278
"wavelength": 2 * np.pi,
278279
},
279-
{
280-
"q": None,
281-
"tth": 60,
282-
"d": None,
283-
"offset": 2.1,
284-
},
280+
{"tth": 60, "offset": 2.1},
285281
{"xtype": "tth", "yarray": np.array([4.1, 5.1, 6.1, 7.1, 8.1, 9.1])},
286282
),
287-
( # C5: none of q, tth, d, provided, expect to scale on the maximal x-arrays
288-
{
289-
"xarray": np.array([0.1, 0.2, 0.3]),
290-
"yarray": np.array([1, 2, 3]),
291-
"xtype": "q",
292-
"wavelength": 2 * np.pi,
293-
},
294-
{
295-
"xarray": np.array([0.05, 0.1, 0.2, 0.3]),
296-
"yarray": np.array([5, 10, 20, 30]),
297-
"xtype": "q",
298-
"wavelength": 2 * np.pi,
299-
},
300-
{
301-
"q": None,
302-
"tth": None,
303-
"d": None,
304-
"offset": 0,
305-
},
306-
{"xtype": "q", "yarray": np.array([10, 20, 30])},
307-
),
308283
],
309284
)
310285
def test_scale_to(org_do_args, target_do_args, scale_inputs, expected):
311286
original_do = DiffractionObject(**org_do_args)
312287
target_do = DiffractionObject(**target_do_args)
313-
scaled_do = original_do.scale_to(
314-
target_do, q=scale_inputs["q"], tth=scale_inputs["tth"], d=scale_inputs["d"], offset=scale_inputs["offset"]
315-
)
288+
scaled_do = original_do.scale_to(target_do, **scale_inputs)
316289
# Check the intensity data is the same as expected
317290
assert np.allclose(scaled_do.on_xtype(expected["xtype"])[1], expected["yarray"])
318291

@@ -335,10 +308,8 @@ def test_scale_to(org_do_args, target_do_args, scale_inputs, expected):
335308
"wavelength": 2 * np.pi,
336309
},
337310
{
338-
"q": None,
339311
"tth": 60,
340312
"d": 10,
341-
"offset": 0,
342313
},
343314
),
344315
],
@@ -351,13 +322,7 @@ def test_scale_to_bad(org_do_args, target_do_args, scale_inputs):
351322
match="You must specify none or exactly one of 'q', 'tth', or 'd'. "
352323
"Please provide either none or one value.",
353324
):
354-
original_do.scale_to(
355-
target_do,
356-
q=scale_inputs["q"],
357-
tth=scale_inputs["tth"],
358-
d=scale_inputs["d"],
359-
offset=scale_inputs["offset"],
360-
)
325+
original_do.scale_to(target_do, **scale_inputs)
361326

362327

363328
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)