Skip to content

Commit 3dac06a

Browse files
simplify tests
1 parent 56e7278 commit 3dac06a

File tree

3 files changed

+30
-59
lines changed

3 files changed

+30
-59
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: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -266,26 +266,10 @@ def get_array_index(self, value, xtype=None):
266266

267267
if xtype is None:
268268
xtype = self.input_xtype
269-
if self.on_xtype(xtype) is None or len(self.on_xtype(xtype)[0]) == 0:
270-
raise ValueError(
271-
f"The '{xtype}' array is empty. " "Please ensure it is initialized and the correct xtype is used."
272-
)
273269
array = self.on_xtype(xtype)[0]
270+
if len(array) == 0:
271+
raise ValueError(f"The '{xtype}' array is empty. Please ensure it is initialized.")
274272
i = (np.abs(array - value)).argmin()
275-
nearest_value = np.abs(array[i] - value)
276-
distance = min(np.abs(value - array.min()), np.abs(value - array.max()))
277-
threshold = 0.5 * (array.max() - array.min())
278-
279-
if nearest_value != 0 and (array.min() <= value <= array.max() or distance <= threshold):
280-
warnings.warn(
281-
f"WARNING: The value {value} is not an exact match of the '{xtype}' array. "
282-
f"Returning the index of the closest value."
283-
)
284-
elif distance > threshold:
285-
raise IndexError(
286-
f"The value {value} is too far from any value in the '{xtype}' array. "
287-
f"Please check if you have specified the correct xtype. "
288-
)
289273
return i
290274

291275
def _set_xarrays(self, xarray, xtype):

tests/test_diffraction_objects.py

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def _test_valid_diffraction_objects(actual_diffraction_object, function, expecte
218218
# UC2: target value lies in the array, returns the (first) closest index
219219
([4 * np.pi, np.array([30, 60]), np.array([1, 2]), "tth", "tth", 45], [0]),
220220
([4 * np.pi, np.array([30, 60]), np.array([1, 2]), "tth", "q", 0.25], [0]),
221-
# UC3: target value out of the range but within reasonable distance, returns the closest index
221+
# UC3: target value out of the range, returns the closest index
222222
([4 * np.pi, np.array([0.25, 0.5, 0.71]), np.array([1, 2, 3]), "q", "q", 0.1], [0]),
223223
([4 * np.pi, np.array([30, 60]), np.array([1, 2]), "tth", "tth", 63], [1]),
224224
]
@@ -231,46 +231,10 @@ def test_get_array_index(inputs, expected):
231231
assert actual == expected[0]
232232

233233

234-
params_index_bad = [
235-
# UC0: empty array
236-
(
237-
[2 * np.pi, np.array([]), np.array([]), "tth", "tth", 30],
238-
[ValueError, "The 'tth' array is empty. Please ensure it is initialized and the correct xtype is used."],
239-
),
240-
# UC1: empty array (because of invalid xtype)
241-
(
242-
[2 * np.pi, np.array([30, 60]), np.array([1, 2]), "tth", "invalid", 30],
243-
[
244-
ValueError,
245-
"The 'invalid' array is empty. Please ensure it is initialized and the correct xtype is used.",
246-
],
247-
),
248-
# UC3: value is too far from any element in the array
249-
(
250-
[2 * np.pi, np.array([30, 60, 90]), np.array([1, 2, 3]), "tth", "tth", 140],
251-
[
252-
IndexError,
253-
"The value 140 is too far from any value in the 'tth' array. "
254-
"Please check if you have specified the correct xtype.",
255-
],
256-
),
257-
# UC4: value is too far from any element in the array (because of wrong xtype)
258-
(
259-
[2 * np.pi, np.array([30, 60, 90]), np.array([1, 2, 3]), "tth", "q", 30],
260-
[
261-
IndexError,
262-
"The value 30 is too far from any value in the 'q' array. "
263-
"Please check if you have specified the correct xtype.",
264-
],
265-
),
266-
]
267-
268-
269-
@pytest.mark.parametrize("inputs, expected", params_index_bad)
270-
def test_get_array_index_bad(inputs, expected):
271-
test = DiffractionObject(wavelength=inputs[0], xarray=inputs[1], yarray=inputs[2], xtype=inputs[3])
272-
with pytest.raises(expected[0], match=re.escape(expected[1])):
273-
test.get_array_index(value=inputs[5], xtype=inputs[4])
234+
def test_get_array_index_bad():
235+
test = DiffractionObject(wavelength=2 * np.pi, xarray=np.array([]), yarray=np.array([]), xtype="tth")
236+
with pytest.raises(ValueError, match=re.escape("The 'tth' array is empty. Please ensure it is initialized.")):
237+
test.get_array_index(value=30)
274238

275239

276240
def test_dump(tmp_path, mocker):

0 commit comments

Comments
 (0)