Skip to content

Commit 2c8335c

Browse files
add feature so that user can run scale_to without xvalue
1 parent 4a87d00 commit 2c8335c

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

src/diffpy/utils/diffraction_objects.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,14 +402,16 @@ def scale_to(self, target_diff_object, q=None, tth=None, d=None, offset=0):
402402
403403
The y-value in the target at the closest specified x-value will be used as the factor to scale to.
404404
The entire array is scaled by this factor so that one object places on top of the other at that point.
405-
If multiple values of `q`, `tth`, or `d` are provided, or none are provided, an error will be raised.
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.
407+
If multiple values of `q`, `tth`, or `d` are provided, an error will be raised.
406408
407409
Parameters
408410
----------
409411
target_diff_object: DiffractionObject
410412
the diffraction object you want to scale the current one onto
411413
412-
q, tth, d : float, optional, must specify exactly one of them
414+
q, tth, d : float, optional, default is q with the maximal x-array value of the current object
413415
The value of the x-array where you want the curves to line up vertically.
414416
Specify a value on one of the allowed grids, q, tth, or d), e.g., q=10.
415417
@@ -422,11 +424,16 @@ def scale_to(self, target_diff_object, q=None, tth=None, d=None, offset=0):
422424
"""
423425
scaled = self.copy()
424426
count = sum([q is not None, tth is not None, d is not None])
425-
if count != 1:
427+
if count > 1:
426428
raise ValueError(
427-
"You must specify exactly one of 'q', 'tth', or 'd'. Please rerun specifying only one."
429+
"You must specify none or exactly one of 'q', 'tth', or 'd'. "
430+
"Please provide either none or one value."
428431
)
429432

433+
if count == 0:
434+
scaled._all_arrays[:, 0] *= max(target_diff_object.on_q()[1]) / max(self.on_q()[1])
435+
return scaled
436+
430437
xtype = "q" if q is not None else "tth" if tth is not None else "d"
431438
data = self.on_xtype(xtype)
432439
target = target_diff_object.on_xtype(xtype)

tests/test_diffraction_objects.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -284,23 +284,7 @@ def test_init_invalid_xtype():
284284
},
285285
{"xtype": "tth", "yarray": np.array([4.1, 5.1, 6.1, 7.1, 8.1, 9.1])},
286286
),
287-
],
288-
)
289-
def test_scale_to(org_do_args, target_do_args, scale_inputs, expected):
290-
original_do = DiffractionObject(**org_do_args)
291-
target_do = DiffractionObject(**target_do_args)
292-
scaled_do = original_do.scale_to(
293-
target_do, q=scale_inputs["q"], tth=scale_inputs["tth"], d=scale_inputs["d"], offset=scale_inputs["offset"]
294-
)
295-
# Check the intensity data is the same as expected
296-
assert np.allclose(scaled_do.on_xtype(expected["xtype"])[1], expected["yarray"])
297-
298-
299-
@pytest.mark.parametrize(
300-
"org_do_args, target_do_args, scale_inputs",
301-
[
302-
# Test expected errors produced from scale_to() with invalid inputs
303-
( # C1: none of q, tth, d, provided, expect ValueError
287+
( # C5: none of q, tth, d, provided, expect to scale on the maximal x-arrays
304288
{
305289
"xarray": np.array([0.1, 0.2, 0.3]),
306290
"yarray": np.array([1, 2, 3]),
@@ -319,8 +303,25 @@ def test_scale_to(org_do_args, target_do_args, scale_inputs, expected):
319303
"d": None,
320304
"offset": 0,
321305
},
306+
{"xtype": "q", "yarray": np.array([10, 20, 30])},
322307
),
323-
( # C2: tth and d both provided, expect ValueErrort
308+
],
309+
)
310+
def test_scale_to(org_do_args, target_do_args, scale_inputs, expected):
311+
original_do = DiffractionObject(**org_do_args)
312+
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+
)
316+
# Check the intensity data is the same as expected
317+
assert np.allclose(scaled_do.on_xtype(expected["xtype"])[1], expected["yarray"])
318+
319+
320+
@pytest.mark.parametrize(
321+
"org_do_args, target_do_args, scale_inputs",
322+
[
323+
# Test expected errors produced from scale_to() with invalid inputs
324+
( # C2: tth and d both provided, expect ValueError
324325
{
325326
"xarray": np.array([10, 25, 30.1, 40.2, 61, 120, 140]),
326327
"yarray": np.array([10, 20, 30, 40, 50, 60, 100]),
@@ -346,7 +347,9 @@ def test_scale_to_bad(org_do_args, target_do_args, scale_inputs):
346347
original_do = DiffractionObject(**org_do_args)
347348
target_do = DiffractionObject(**target_do_args)
348349
with pytest.raises(
349-
ValueError, match="You must specify exactly one of 'q', 'tth', or 'd'. Please rerun specifying only one."
350+
ValueError,
351+
match="You must specify none or exactly one of 'q', 'tth', or 'd'. "
352+
"Please provide either none or one value.",
350353
):
351354
original_do.scale_to(
352355
target_do,

0 commit comments

Comments
 (0)