kwcoco.util.util_slice module

kwcoco.util.util_slice.padded_slice(data, in_slice, ndim=None, pad_slice=None, pad_mode='constant', **padkw)[source]

Allows slices with out-of-bound coordinates. Any out of bounds coordinate will be sampled via padding.

Note

Negative slices have a different meaning here then they usually do. Normally, they indicate a wrap-around or a reversed stride, but here they index into out-of-bounds space (which depends on the pad mode). For example a slice of -2:1 literally samples two pixels to the left of the data and one pixel from the data, so you get two padded values and one data value.

Parameters:
  • data (Sliceable[T]) – data to slice into. Any channels must be the last dimension.
  • in_slice (Tuple[slice, …]) – slice for each dimensions
  • ndim (int) – number of spatial dimensions
  • pad_slice (List[int|Tuple]) – additional padding of the slice
Returns:

data_sliced: subregion of the input data (possibly with padding,

depending on if the original slice went out of bounds)

transform : information on how to return to the original coordinates

Currently a dict containing:
st_dims: a list indicating the low and high space-time

coordinate values of the returned data slice.

Return type:

Tuple[Sliceable, Dict]

Example

>>> data = np.arange(5)
>>> in_slice = [slice(-2, 7)]
>>> data_sliced, transform = padded_slice(data, in_slice)
>>> print(ub.repr2(data_sliced, with_dtype=False))
np.array([0, 0, 0, 1, 2, 3, 4, 0, 0])
>>> data_sliced, transform = padded_slice(data, in_slice, pad_slice=(3, 3))
>>> print(ub.repr2(data_sliced, with_dtype=False))
np.array([0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, 0, 0])
>>> data_sliced, transform = padded_slice(data, slice(3, 4), pad_slice=[(1, 0)])
>>> print(ub.repr2(data_sliced, with_dtype=False))
np.array([2, 3])