Skip to content

Commit 53413fb

Browse files
make tests more readable
1 parent c22a95b commit 53413fb

File tree

2 files changed

+114
-96
lines changed

2 files changed

+114
-96
lines changed

src/diffpy/utils/diffraction_objects.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,8 @@ def scale_to(self, target_diff_object, q=None, tth=None, d=None, offset=0):
405405
the diffraction object you want to scale the current one onto
406406
407407
q, tth, d : float, optional, must specify exactly one of them
408-
the xvalue (in `q`, `tth`, or `d` space) to align the current and target objects
408+
The value of the x-array where you want the curves to line up vertically.
409+
Specify a value on one of the allowed grids, q, tth, or d), e.g., q=10.
409410
410411
offset : float, optional, default is 0
411412
an offset to add to the scaled y-values
@@ -421,8 +422,9 @@ def scale_to(self, target_diff_object, q=None, tth=None, d=None, offset=0):
421422
"You must specify exactly one of 'q', 'tth', or 'd'. Please rerun specifying only one."
422423
)
423424

424-
xtype = "q" if q is not None else "tth" if tth is not None else "d" if d is not None else "q"
425-
data, target = self.on_xtype(xtype), target_diff_object.on_xtype(xtype)
425+
xtype = "q" if q is not None else "tth" if tth is not None else "d"
426+
data = self.on_xtype(xtype)
427+
target = target_diff_object.on_xtype(xtype)
426428

427429
xvalue = q if xtype == "q" else tth if xtype == "tth" else d
428430

tests/test_diffraction_objects.py

+109-93
Original file line numberDiff line numberDiff line change
@@ -183,137 +183,153 @@ def test_init_invalid_xtype():
183183
params_scale_to = [
184184
# UC1: same x-array and y-array, check offset
185185
(
186-
[
187-
np.array([10, 15, 25, 30, 60, 140]),
188-
np.array([2, 3, 4, 5, 6, 7]),
189-
"tth",
190-
2 * np.pi,
191-
np.array([10, 15, 25, 30, 60, 140]),
192-
np.array([2, 3, 4, 5, 6, 7]),
193-
"tth",
194-
2 * np.pi,
195-
None,
196-
60,
197-
None,
198-
2.1,
199-
],
200-
["tth", np.array([4.1, 5.1, 6.1, 7.1, 8.1, 9.1])],
186+
{
187+
"xarray": np.array([10, 15, 25, 30, 60, 140]),
188+
"yarray": np.array([2, 3, 4, 5, 6, 7]),
189+
"xtype": "tth",
190+
"wavelength": 2 * np.pi,
191+
"target_xarray": np.array([10, 15, 25, 30, 60, 140]),
192+
"target_yarray": np.array([2, 3, 4, 5, 6, 7]),
193+
"target_xtype": "tth",
194+
"target_wavelength": 2 * np.pi,
195+
"q": None,
196+
"tth": 60,
197+
"d": None,
198+
"offset": 2.1,
199+
},
200+
{"xtype": "tth", "yarray": np.array([4.1, 5.1, 6.1, 7.1, 8.1, 9.1])},
201201
),
202202
# UC2: same length x-arrays with exact x-value match
203203
(
204-
[
205-
np.array([10, 15, 25, 30, 60, 140]),
206-
np.array([10, 20, 25, 30, 60, 100]),
207-
"tth",
208-
2 * np.pi,
209-
np.array([10, 20, 25, 30, 60, 140]),
210-
np.array([2, 3, 4, 5, 6, 7]),
211-
"tth",
212-
2 * np.pi,
213-
None,
214-
60,
215-
None,
216-
0,
217-
],
218-
["tth", np.array([1, 2, 2.5, 3, 6, 10])],
204+
{
205+
"xarray": np.array([10, 15, 25, 30, 60, 140]),
206+
"yarray": np.array([10, 20, 25, 30, 60, 100]),
207+
"xtype": "tth",
208+
"wavelength": 2 * np.pi,
209+
"target_xarray": np.array([10, 20, 25, 30, 60, 140]),
210+
"target_yarray": np.array([2, 3, 4, 5, 6, 7]),
211+
"target_xtype": "tth",
212+
"target_wavelength": 2 * np.pi,
213+
"q": None,
214+
"tth": 60,
215+
"d": None,
216+
"offset": 0,
217+
},
218+
{"xtype": "tth", "yarray": np.array([1, 2, 2.5, 3, 6, 10])},
219219
),
220220
# UC3: same length x-arrays with approximate x-value match
221221
(
222-
[
223-
np.array([0.12, 0.24, 0.31, 0.4]),
224-
np.array([10, 20, 40, 60]),
225-
"q",
226-
2 * np.pi,
227-
np.array([0.14, 0.24, 0.31, 0.4]),
228-
np.array([1, 3, 4, 5]),
229-
"q",
230-
2 * np.pi,
231-
0.1,
232-
None,
233-
None,
234-
0,
235-
],
236-
["q", np.array([1, 2, 4, 6])],
222+
{
223+
"xarray": np.array([0.12, 0.24, 0.31, 0.4]),
224+
"yarray": np.array([10, 20, 40, 60]),
225+
"xtype": "q",
226+
"wavelength": 2 * np.pi,
227+
"target_xarray": np.array([0.14, 0.24, 0.31, 0.4]),
228+
"target_yarray": np.array([1, 3, 4, 5]),
229+
"target_xtype": "q",
230+
"target_wavelength": 2 * np.pi,
231+
"q": 0.1,
232+
"tth": None,
233+
"d": None,
234+
"offset": 0,
235+
},
236+
{"xtype": "q", "yarray": np.array([1, 2, 4, 6])},
237237
),
238238
# UC4: different x-array lengths with approximate x-value match
239239
(
240-
[
241-
np.array([10, 25, 30.1, 40.2, 61, 120, 140]),
242-
np.array([10, 20, 30, 40, 50, 60, 100]),
243-
"tth",
244-
2 * np.pi,
245-
np.array([20, 25.5, 32, 45, 50, 62, 100, 125, 140]),
246-
np.array([1.1, 2, 3, 3.5, 4, 5, 10, 12, 13]),
247-
"tth",
248-
2 * np.pi,
249-
None,
250-
60,
251-
None,
252-
0,
253-
],
254-
# scaling factor is calculated at index = 5 for self and index = 6 for target
255-
["tth", np.array([1, 2, 3, 4, 5, 6, 10])],
240+
{
241+
"xarray": np.array([10, 25, 30.1, 40.2, 61, 120, 140]),
242+
"yarray": np.array([10, 20, 30, 40, 50, 60, 100]),
243+
"xtype": "tth",
244+
"wavelength": 2 * np.pi,
245+
"target_xarray": np.array([20, 25.5, 32, 45, 50, 62, 100, 125, 140]),
246+
"target_yarray": np.array([1.1, 2, 3, 3.5, 4, 5, 10, 12, 13]),
247+
"target_xtype": "tth",
248+
"target_wavelength": 2 * np.pi,
249+
"q": None,
250+
"tth": 60,
251+
"d": None,
252+
"offset": 0,
253+
},
254+
# scaling factor is calculated at index = 4 (tth=61) for self and index = 5 for target (tth=62)
255+
{"xtype": "tth", "yarray": np.array([1, 2, 3, 4, 5, 6, 10])},
256256
),
257257
]
258258

259259

260260
@pytest.mark.parametrize("inputs, expected", params_scale_to)
261261
def test_scale_to(inputs, expected):
262-
orig_diff_object = DiffractionObject(xarray=inputs[0], yarray=inputs[1], xtype=inputs[2], wavelength=inputs[3])
262+
orig_diff_object = DiffractionObject(
263+
xarray=inputs["xarray"], yarray=inputs["yarray"], xtype=inputs["xtype"], wavelength=inputs["wavelength"]
264+
)
263265
target_diff_object = DiffractionObject(
264-
xarray=inputs[4], yarray=inputs[5], xtype=inputs[6], wavelength=inputs[7]
266+
xarray=inputs["target_xarray"],
267+
yarray=inputs["target_yarray"],
268+
xtype=inputs["target_xtype"],
269+
wavelength=inputs["target_wavelength"],
265270
)
266271
scaled_diff_object = orig_diff_object.scale_to(
267-
target_diff_object, q=inputs[8], tth=inputs[9], d=inputs[10], offset=inputs[11]
272+
target_diff_object, q=inputs["q"], tth=inputs["tth"], d=inputs["d"], offset=inputs["offset"]
268273
)
269-
# Check the intensity data is same as expected
270-
assert np.allclose(scaled_diff_object.on_xtype(expected[0])[1], expected[1])
274+
# Check the intensity data is the same as expected
275+
assert np.allclose(scaled_diff_object.on_xtype(expected["xtype"])[1], expected["yarray"])
271276

272277

273278
params_scale_to_bad = [
274279
# UC1: user did not specify anything
275280
(
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,
281+
{
282+
"xarray": np.array([0.1, 0.2, 0.3]),
283+
"yarray": np.array([1, 2, 3]),
284+
"xtype": "q",
285+
"wavelength": 2 * np.pi,
286+
"target_xarray": np.array([0.05, 0.1, 0.2, 0.3]),
287+
"target_yarray": np.array([5, 10, 20, 30]),
288+
"target_xtype": "q",
289+
"target_wavelength": 2 * np.pi,
290+
"q": None,
291+
"tth": None,
292+
"d": None,
293+
"offset": 0,
294+
}
288295
),
289296
# UC2: user specified more than one of q, tth, and d
290297
(
291-
np.array([10, 25, 30.1, 40.2, 61, 120, 140]),
292-
np.array([10, 20, 30, 40, 50, 60, 100]),
293-
"tth",
294-
2 * np.pi,
295-
np.array([20, 25.5, 32, 45, 50, 62, 100, 125, 140]),
296-
np.array([1.1, 2, 3, 3.5, 4, 5, 10, 12, 13]),
297-
"tth",
298-
2 * np.pi,
299-
None,
300-
60,
301-
10,
302-
0,
298+
{
299+
"xarray": np.array([10, 25, 30.1, 40.2, 61, 120, 140]),
300+
"yarray": np.array([10, 20, 30, 40, 50, 60, 100]),
301+
"xtype": "tth",
302+
"wavelength": 2 * np.pi,
303+
"target_xarray": np.array([20, 25.5, 32, 45, 50, 62, 100, 125, 140]),
304+
"target_yarray": np.array([1.1, 2, 3, 3.5, 4, 5, 10, 12, 13]),
305+
"target_xtype": "tth",
306+
"target_wavelength": 2 * np.pi,
307+
"q": None,
308+
"tth": 60,
309+
"d": 10,
310+
"offset": 0,
311+
}
303312
),
304313
]
305314

306315

307316
@pytest.mark.parametrize("inputs", params_scale_to_bad)
308317
def test_scale_to_bad(inputs):
309-
orig_diff_object = DiffractionObject(xarray=inputs[0], yarray=inputs[1], xtype=inputs[2], wavelength=inputs[3])
318+
orig_diff_object = DiffractionObject(
319+
xarray=inputs["xarray"], yarray=inputs["yarray"], xtype=inputs["xtype"], wavelength=inputs["wavelength"]
320+
)
310321
target_diff_object = DiffractionObject(
311-
xarray=inputs[4], yarray=inputs[5], xtype=inputs[6], wavelength=inputs[7]
322+
xarray=inputs["target_xarray"],
323+
yarray=inputs["target_yarray"],
324+
xtype=inputs["target_xtype"],
325+
wavelength=inputs["target_wavelength"],
312326
)
313327
with pytest.raises(
314328
ValueError, match="You must specify exactly one of 'q', 'tth', or 'd'. Please rerun specifying only one."
315329
):
316-
orig_diff_object.scale_to(target_diff_object, q=inputs[8], tth=inputs[9], d=inputs[10], offset=inputs[11])
330+
orig_diff_object.scale_to(
331+
target_diff_object, q=inputs["q"], tth=inputs["tth"], d=inputs["d"], offset=inputs["offset"]
332+
)
317333

318334

319335
params_index = [

0 commit comments

Comments
 (0)