pwtools.num.slicetake

pwtools.num.slicetake(a, sl, axis=None, copy=False)[source]

The equivalent of numpy.take(a, …, axis=<axis>), but accepts slice objects instead of an index array. Also by default, it returns a view and no copy.

Parameters:
  • a (numpy ndarray)

  • sl (slice object, list or tuple of slice objects) –

    axis=<int>

    one slice object for that axis

    axis=None

    sl is a list or tuple of slice objects, one for each axis. It must index the whole array, i.e. len(sl) == len(a.shape).

  • axis ({None, int})

  • copy (bool, return a copy instead of a view)

Return type:

A view into a or copy of a slice of a.

Examples

>>> from numpy import s_
>>> a = np.random.rand(20,20,20)
>>> b1 = a[:,:,10:]
>>> # single slice for axis 2
>>> b2 = slicetake(a, s_[10:], axis=2)
>>> # tuple of slice objects
>>> b3 = slicetake(a, s_[:,:,10:])
>>> (b2 == b1).all()
True
>>> (b3 == b1).all()
True
>>> # simple extraction too, sl = integer
>>> (a[...,5] == slicetake(a, 5, axis=-1))
True