normalized_index_expression

odl.util.normalize.normalized_index_expression(indices, shape, int_to_slice=False)[source]

Enable indexing with almost Numpy-like capabilities.

Implements the following features:

  • Usage of general slices and sequences of slices

  • Conversion of Ellipsis into an adequate number of slice(None) objects

  • Fewer indices than axes by filling up with an Ellipsis

  • Error checking with respect to a given shape

  • Conversion of integer indices into corresponding slices

Parameters
indicesint, slice, Ellipsis or sequence of these

Index expression to be normalized.

shapesequence of ints

Target shape for error checking of out-of-bounds indices. Also needed to determine the number of axes.

int_to_slicebool, optional

If True, turn integers into corresponding slice objects.

Returns
normalizedtuple of ints or slice’s

Normalized index expression

Examples

Sequences are turned into tuples. We can have at most as many entries as the length of shape, but fewer are allowed - the remaining list places are filled up by slice(None):

>>> normalized_index_expression([1, 2, 3], shape=(3, 4, 5))
(1, 2, 3)
>>> normalized_index_expression([1, 2], shape=(3, 4, 5))
(1, 2, slice(None, None, None))
>>> normalized_index_expression([slice(2), 2], shape=(3, 4, 5))
(slice(None, 2, None), 2, slice(None, None, None))
>>> normalized_index_expression([1, Ellipsis], shape=(3, 4, 5))
(1, slice(None, None, None), slice(None, None, None))

By default, integer indices are kept. If they should be converted to slices, use int_to_slice=True. This can be useful to guarantee that the result of slicing with the returned object is of the same type as the array into which is sliced and has the same number of axes:

>>> x = np.zeros(shape=(3, 4, 5))
>>> idx1 = normalized_index_expression([1, 2, 3], shape=(3, 4, 5),
...                                   int_to_slice=True)
>>> idx1
(slice(1, 2, None), slice(2, 3, None), slice(3, 4, None))
>>> x[idx1]
array([[[ 0.]]])
>>> idx2 = normalized_index_expression([1, 2, 3], shape=(3, 4, 5),
...                                   int_to_slice=False)
>>> idx2
(1, 2, 3)
>>> x[idx2]
0.0