Skip to content

Commit 672c3fb

Browse files
committed
TST: ndimage: avoid implicit dtype conversions in vectorized_filter tests
This tests does this: `vectorized_filter(input, xp.sum, output)`, with output being `xp.empty_like(input)`. This is problematic for short integers because - the output of xp.sum(int8_array) is int64 (the default int dtype), and - internally, vectorized_filter does roughly, `output[some_indices] = xp.sum(input[some_indices])` So what happens here is that the output of the `sum` in the r.h.s. gets silently downcast to the dtype of the l.h.s. by `__setitem__`. This kind of sort of works in many array libraries, but is technically unspecified by the Array API standard. And indeed this starts failing with array-api-strict in data-apis/array-api-strict#157 Thus, a test only change to make downcasting explicit.
1 parent a465e2c commit 672c3fb

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

scipy/ndimage/tests/test_filters.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2864,7 +2864,14 @@ def test_dtype_batch_memory(self, dtype, batch_memory, use_footprint, xp):
28642864
assert res.dtype == sum_dtype
28652865

28662866
output = xp.empty_like(input)
2867-
res = ndimage.vectorized_filter(input, xp.sum, output=output, **kwargs)
2867+
res = ndimage.vectorized_filter(
2868+
input,
2869+
lambda x, *args, **kw: xp.astype(
2870+
xp.sum(x, *args, **kw), x.dtype, copy=False
2871+
),
2872+
output=output,
2873+
**kwargs
2874+
)
28682875
xp_assert_close(res, xp.astype(xp.stack(ref), dtype))
28692876
assert res.dtype == dtype
28702877

0 commit comments

Comments
 (0)