Consider the following Python + NumPy code that executes without error:
a = np.array((1, 2, 3))
a[13:17] = 23
Using a slice beyond the limits of the array truncates the slice and even returns an empty view if start and stop are beyond the limits. Assigning to such a slice just drops the input.
In my use case the indices are calculated in a non-trivial way and are used to manipulate selected parts of an array. The above behavior means that I might silently skip parts of that manipultion if the indices are miscalculated. That can be hard to detect and can lead to "almost correct" results, i.e. the worst kind of programming errors.
For that reason I'd like to have strict checking for slices so that a start or stop outside the array bounds triggers an error. Is there a way to enable that in NumPy?
As additional information, the arrays are large and the operation is performed very often, i.e. there should be no performance penalty. Furthermore, the arrays are often multidimensional, including multidimensional slicing.
You could be using np.put_along_axis
instead, which seems to fit your needs:
>>> a = np.array((1, 2, 3))
>>> np.put_along_axis(a, indices=np.arange(13, 17), axis=0, values=23)
The above will raise the following error:
IndexError: index
13
is out of bounds for axis0
with size3
Parameter values
can either be a scalar value or another NumPy array.
Or in a shorter form:
>>> np.put_along_axis(a, np.r_[13:17], 23, 0)
Edit: Alternatively np.put
has a mode='raise'
option (which is set by default):
np.put(a, ind, v, mode='raise')
a
: ndarray - Target array.
ind
: array_like - Target indices, interpreted as integers.
v
: array_like - Values to place in a at target indices. [...]
mode: {'raise', 'wrap', 'clip'}
optional - Specifies how out-of-bounds indices will behave.
- 'raise' – raise an error (default)
- 'wrap' – wrap around
- 'clip' – clip to the range
The default behavior will be:
>>> np.put(a, np.r_[13:17], 23)
IndexError: index
13
is out of bounds for axis0
with size3
while with mode='clip'
, it remains silent:
>>> np.put(a, np.r_[13:17], 23, mode='clip')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With