Skip to content

Commit 3aef608

Browse files
authored
Merge pull request diffpy#191 from yucongalicechen/angle-test
test function for get_angle_index
2 parents e6827a6 + 3dac06a commit 3aef608

File tree

3 files changed

+72
-9
lines changed

3 files changed

+72
-9
lines changed

news/array_index.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* function to return the index of the closest value to the specified value in an array.
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+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

src/diffpy/utils/diffraction_objects.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,29 @@ def _set_array_from_range(self, begin, end, step_size=None, n_steps=None):
259259
array = np.linspace(begin, end, n_steps)
260260
return array
261261

262-
def get_angle_index(self, angle):
263-
count = 0
264-
for i, target in enumerate(self.angles):
265-
if angle == target:
266-
return i
267-
else:
268-
count += 1
269-
if count >= len(self.angles):
270-
raise IndexError(f"WARNING: no angle {angle} found in angles list")
262+
def get_array_index(self, value, xtype=None):
263+
"""
264+
returns the index of the closest value in the array associated with the specified xtype
265+
266+
Parameters
267+
----------
268+
xtype str
269+
the xtype used to access the array
270+
value float
271+
the target value to search for
272+
273+
Returns
274+
-------
275+
the index of the value in the array
276+
"""
277+
278+
if xtype is None:
279+
xtype = self.input_xtype
280+
array = self.on_xtype(xtype)[0]
281+
if len(array) == 0:
282+
raise ValueError(f"The '{xtype}' array is empty. Please ensure it is initialized.")
283+
i = (np.abs(array - value)).argmin()
284+
return i
271285

272286
def _set_xarrays(self, xarray, xtype):
273287
self._all_arrays = np.empty(shape=(len(xarray), 4))

tests/test_diffraction_objects.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
from pathlib import Path
23

34
import numpy as np
@@ -212,6 +213,31 @@ def _test_valid_diffraction_objects(actual_diffraction_object, function, expecte
212213
return np.allclose(actual_array, expected_array)
213214

214215

216+
params_index = [
217+
# UC1: exact match
218+
([4 * np.pi, np.array([30.005, 60]), np.array([1, 2]), "tth", "tth", 30.005], [0]),
219+
# UC2: target value lies in the array, returns the (first) closest index
220+
([4 * np.pi, np.array([30, 60]), np.array([1, 2]), "tth", "tth", 45], [0]),
221+
([4 * np.pi, np.array([30, 60]), np.array([1, 2]), "tth", "q", 0.25], [0]),
222+
# UC3: target value out of the range, returns the closest index
223+
([4 * np.pi, np.array([0.25, 0.5, 0.71]), np.array([1, 2, 3]), "q", "q", 0.1], [0]),
224+
([4 * np.pi, np.array([30, 60]), np.array([1, 2]), "tth", "tth", 63], [1]),
225+
]
226+
227+
228+
@pytest.mark.parametrize("inputs, expected", params_index)
229+
def test_get_array_index(inputs, expected):
230+
test = DiffractionObject(wavelength=inputs[0], xarray=inputs[1], yarray=inputs[2], xtype=inputs[3])
231+
actual = test.get_array_index(value=inputs[5], xtype=inputs[4])
232+
assert actual == expected[0]
233+
234+
235+
def test_get_array_index_bad():
236+
test = DiffractionObject(wavelength=2 * np.pi, xarray=np.array([]), yarray=np.array([]), xtype="tth")
237+
with pytest.raises(ValueError, match=re.escape("The 'tth' array is empty. Please ensure it is initialized.")):
238+
test.get_array_index(value=30)
239+
240+
215241
def test_dump(tmp_path, mocker):
216242
x, y = np.linspace(0, 5, 6), np.linspace(0, 5, 6)
217243
directory = Path(tmp_path)

0 commit comments

Comments
 (0)