Skip to content

Commit 5a31716

Browse files
authored
Merge pull request #283 from bobleesj/get-array-index-return
Fix `get_array_index` returns `int` instead of `list` in DiffractionObject
2 parents b0b6676 + faf0e2f commit 5a31716

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

news/get-index.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* <news item>
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* return type of `get_array_index` method in `DiffractionObject` to return integer instead of list
20+
21+
**Security:**
22+
23+
* <news item>

src/diffpy/utils/diffraction_objects.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -331,29 +331,29 @@ def uuid(self):
331331
def uuid(self, _):
332332
raise AttributeError(_setter_wmsg("uuid"))
333333

334-
def get_array_index(self, value, xtype=None):
334+
def get_array_index(self, xtype, xvalue):
335335
"""Return the index of the closest value in the array associated with
336-
the specified xtype.
336+
the specified xtype and the value provided.
337337
338338
Parameters
339339
----------
340-
xtype str
341-
the xtype used to access the array
342-
value float
343-
the target value to search for
340+
xtype : str
341+
The type of the independent variable in `xarray`. Must be one of {*XQUANTITIES}.
342+
xvalue : float
343+
The value of the xtype to find the closest index for.
344344
345345
Returns
346346
-------
347-
list
348-
The list containing the index of the closest value in the array.
347+
int
348+
The index of the closest value in the array associated with the specified xtype and the value provided.
349349
"""
350350

351351
xtype = self._input_xtype
352-
array = self.on_xtype(xtype)[0]
353-
if len(array) == 0:
352+
xarray = self.on_xtype(xtype)[0]
353+
if len(xarray) == 0:
354354
raise ValueError(f"The '{xtype}' array is empty. Please ensure it is initialized.")
355-
i = (np.abs(array - value)).argmin()
356-
return i
355+
index = (np.abs(xarray - xvalue)).argmin()
356+
return index
357357

358358
def _set_arrays(self, xarray, yarray, xtype):
359359
self._all_arrays = np.empty(shape=(len(xarray), 4))

tests/test_diffraction_objects.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ def test_scale_to_bad(org_do_args, target_do_args, scale_inputs):
364364
"xtype": "tth",
365365
"value": 30.005,
366366
},
367-
[0],
367+
0,
368368
),
369369
( # C2: Target value lies in the array, expect the (first) closest index
370370
{
@@ -377,7 +377,7 @@ def test_scale_to_bad(org_do_args, target_do_args, scale_inputs):
377377
"xtype": "tth",
378378
"value": 45,
379379
},
380-
[0],
380+
0,
381381
),
382382
(
383383
{
@@ -390,7 +390,7 @@ def test_scale_to_bad(org_do_args, target_do_args, scale_inputs):
390390
"xtype": "q",
391391
"value": 0.25,
392392
},
393-
[0],
393+
0,
394394
),
395395
# C3: Target value out of the range, expect the closest index
396396
( # 1. Test with xtype of "q"
@@ -404,7 +404,7 @@ def test_scale_to_bad(org_do_args, target_do_args, scale_inputs):
404404
"xtype": "q",
405405
"value": 0.1,
406406
},
407-
[0],
407+
0,
408408
),
409409
( # 2. Test with xtype of "tth"
410410
{
@@ -417,20 +417,20 @@ def test_scale_to_bad(org_do_args, target_do_args, scale_inputs):
417417
"xtype": "tth",
418418
"value": 63,
419419
},
420-
[1],
420+
1,
421421
),
422422
],
423423
)
424424
def test_get_array_index(do_args, get_array_index_inputs, expected_index):
425425
do = DiffractionObject(**do_args)
426-
actual_index = do.get_array_index(get_array_index_inputs["value"], get_array_index_inputs["xtype"])
426+
actual_index = do.get_array_index(get_array_index_inputs["xtype"], get_array_index_inputs["value"])
427427
assert actual_index == expected_index
428428

429429

430430
def test_get_array_index_bad():
431431
do = DiffractionObject(wavelength=2 * np.pi, xarray=np.array([]), yarray=np.array([]), xtype="tth")
432432
with pytest.raises(ValueError, match=re.escape("The 'tth' array is empty. Please ensure it is initialized.")):
433-
do.get_array_index(value=30)
433+
do.get_array_index(xtype="tth", xvalue=30)
434434

435435

436436
def test_dump(tmp_path, mocker):

0 commit comments

Comments
 (0)