Skip to content

Commit 27da4de

Browse files
feat: add offset to default behavior and test case
1 parent 13f227d commit 27da4de

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/diffpy/utils/diffraction_objects.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,9 @@ def scale_to(self, target_diff_object, q=None, tth=None, d=None, offset=None):
433433
)
434434

435435
if count == 0:
436-
scaled._all_arrays[:, 0] *= max(target_diff_object.on_q()[1]) / max(self.on_q()[1])
436+
q_target_max = max(target_diff_object.on_q()[1])
437+
q_self_max = max(self.on_q()[1])
438+
scaled._all_arrays[:, 0] = scaled._all_arrays[:, 0] * q_target_max / q_self_max + offset
437439
return scaled
438440

439441
xtype = "q" if q is not None else "tth" if tth is not None else "d"

tests/test_diffraction_objects.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,21 +264,38 @@ def test_init_invalid_xtype():
264264
{"tth": 60},
265265
{"xtype": "tth", "yarray": np.array([1, 2, 3, 4, 5, 6, 10])},
266266
),
267-
( # C5: Same x-array and y-array with 2.1 offset, expect y-array to shift up by 2.1
267+
( # C5.1: Reuse test case from C1, none of q, tth, d, provided, but include an offset,
268+
# expect scaled y-array in C1 to shift up by 2
268269
{
269-
"xarray": np.array([10, 15, 25, 30, 60, 140]),
270-
"yarray": np.array([2, 3, 4, 5, 6, 7]),
270+
"xarray": np.array([0.1, 0.2, 0.3]),
271+
"yarray": np.array([1, 2, 3]),
272+
"xtype": "q",
273+
"wavelength": 2 * np.pi,
274+
},
275+
{
276+
"xarray": np.array([0.05, 0.1, 0.2, 0.3]),
277+
"yarray": np.array([5, 10, 20, 30]),
278+
"xtype": "q",
279+
"wavelength": 2 * np.pi,
280+
},
281+
{"offset": 2},
282+
{"xtype": "q", "yarray": np.array([12, 22, 32])},
283+
),
284+
( # C5.2: Reuse test case from C4, but include an offset, expect scaled y-array in C4 to shift up by 2
285+
{
286+
"xarray": np.array([10, 25, 30.1, 40.2, 61, 120, 140]),
287+
"yarray": np.array([10, 20, 30, 40, 50, 60, 100]),
271288
"xtype": "tth",
272289
"wavelength": 2 * np.pi,
273290
},
274291
{
275-
"xarray": np.array([10, 15, 25, 30, 60, 140]),
276-
"yarray": np.array([2, 3, 4, 5, 6, 7]),
292+
"xarray": np.array([20, 25.5, 32, 45, 50, 62, 100, 125, 140]),
293+
"yarray": np.array([1.1, 2, 3, 3.5, 4, 5, 10, 12, 13]),
277294
"xtype": "tth",
278295
"wavelength": 2 * np.pi,
279296
},
280-
{"tth": 60, "offset": 2.1},
281-
{"xtype": "tth", "yarray": np.array([4.1, 5.1, 6.1, 7.1, 8.1, 9.1])},
297+
{"tth": 60, "offset": 2},
298+
{"xtype": "tth", "yarray": np.array([3, 4, 5, 6, 7, 8, 12])},
282299
),
283300
],
284301
)

0 commit comments

Comments
 (0)