Skip to content

Commit c22a95b

Browse files
remove default value in the function
1 parent 1b7aa14 commit c22a95b

File tree

2 files changed

+26
-29
lines changed

2 files changed

+26
-29
lines changed

src/diffpy/utils/diffraction_objects.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -397,15 +397,14 @@ def scale_to(self, target_diff_object, q=None, tth=None, d=None, offset=0):
397397
398398
The y-value in the target at the closest specified x-value will be used as the factor to scale to.
399399
The entire array is scaled by this factor so that one object places on top of the other at that point.
400-
If multiple values of `q`, `tth`, or `d` are provided, an error will be raised.
401-
If none are provided, the midpoint of the current object's `q`-array will be used.
400+
If multiple values of `q`, `tth`, or `d` are provided, or none are provided, an error will be raised.
402401
403402
Parameters
404403
----------
405404
target_diff_object: DiffractionObject
406405
the diffraction object you want to scale the current one onto
407406
408-
q, tth, d : float, optional, default is the midpoint of the current object's `q`-array
407+
q, tth, d : float, optional, must specify exactly one of them
409408
the xvalue (in `q`, `tth`, or `d` space) to align the current and target objects
410409
411410
offset : float, optional, default is 0
@@ -417,19 +416,19 @@ def scale_to(self, target_diff_object, q=None, tth=None, d=None, offset=0):
417416
"""
418417
scaled = self.copy()
419418
count = sum([q is not None, tth is not None, d is not None])
420-
if count > 1:
421-
raise ValueError("You can only specify one of 'q', 'tth', or 'd'. Please rerun specifying only one.")
419+
if count != 1:
420+
raise ValueError(
421+
"You must specify exactly one of 'q', 'tth', or 'd'. Please rerun specifying only one."
422+
)
422423

423424
xtype = "q" if q is not None else "tth" if tth is not None else "d" if d is not None else "q"
424425
data, target = self.on_xtype(xtype), target_diff_object.on_xtype(xtype)
425426

426427
xvalue = q if xtype == "q" else tth if xtype == "tth" else d
427-
if xvalue is None:
428-
xvalue = (data[0][0] + data[0][-1]) / 2.0
429428

430-
x_data, x_target = (np.abs(data[0] - xvalue)).argmin(), (np.abs(target[0] - xvalue)).argmin()
431-
y_data, y_target = data[1][x_data], target[1][x_target]
432-
scaled._all_arrays[:, 0] = data[1] * y_target / y_data + offset
429+
xindex_data = (np.abs(data[0] - xvalue)).argmin()
430+
xindex_target = (np.abs(target[0] - xvalue)).argmin()
431+
scaled._all_arrays[:, 0] = data[1] * target[1][xindex_target] / data[1][xindex_data] + offset
433432
return scaled
434433

435434
def on_xtype(self, xtype):

tests/test_diffraction_objects.py

+17-19
Original file line numberDiff line numberDiff line change
@@ -254,24 +254,6 @@ def test_init_invalid_xtype():
254254
# scaling factor is calculated at index = 5 for self and index = 6 for target
255255
["tth", np.array([1, 2, 3, 4, 5, 6, 10])],
256256
),
257-
# UC5: user did not specify anything, use the midpoint of the current object's q-array
258-
(
259-
[
260-
np.array([0.1, 0.2, 0.3]),
261-
np.array([1, 2, 3]),
262-
"q",
263-
2 * np.pi,
264-
np.array([0.05, 0.1, 0.2, 0.3]),
265-
np.array([5, 10, 20, 30]),
266-
"q",
267-
2 * np.pi,
268-
None,
269-
None,
270-
None,
271-
0,
272-
],
273-
["q", np.array([10, 20, 30])],
274-
),
275257
]
276258

277259

@@ -289,6 +271,22 @@ def test_scale_to(inputs, expected):
289271

290272

291273
params_scale_to_bad = [
274+
# UC1: user did not specify anything
275+
(
276+
np.array([0.1, 0.2, 0.3]),
277+
np.array([1, 2, 3]),
278+
"q",
279+
2 * np.pi,
280+
np.array([0.05, 0.1, 0.2, 0.3]),
281+
np.array([5, 10, 20, 30]),
282+
"q",
283+
2 * np.pi,
284+
None,
285+
None,
286+
None,
287+
0,
288+
),
289+
# UC2: user specified more than one of q, tth, and d
292290
(
293291
np.array([10, 25, 30.1, 40.2, 61, 120, 140]),
294292
np.array([10, 20, 30, 40, 50, 60, 100]),
@@ -313,7 +311,7 @@ def test_scale_to_bad(inputs):
313311
xarray=inputs[4], yarray=inputs[5], xtype=inputs[6], wavelength=inputs[7]
314312
)
315313
with pytest.raises(
316-
ValueError, match="You can only specify one of 'q', 'tth', or 'd'. Please rerun specifying only one."
314+
ValueError, match="You must specify exactly one of 'q', 'tth', or 'd'. Please rerun specifying only one."
317315
):
318316
orig_diff_object.scale_to(target_diff_object, q=inputs[8], tth=inputs[9], d=inputs[10], offset=inputs[11])
319317

0 commit comments

Comments
 (0)