[Numpy-discussion] Proposal of new function: iteraxis()

Phil Elson pelson.pub at gmail.com
Fri Apr 26 09:56:43 EDT 2013


I didn't find the rollaxis solution particularly obvious and also had to
think about what rollaxis did before understanding its usefulness for
iteration.
Now that I've understood it, I'm +1 for the statement that, as it stands,
the proposed iteraxis method doesn't add enough to warrant its inclusion.

That said, I do think array iteration could be made simpler (or the
function I've missed better documented!). I've put together an
implementation of a "slices" function which can return subsets of an array
based on the axes provided (a generalisation of iteraxis but implemented
slightly differently):

def slices(a, axes=-1):
    indices = np.repeat(slice(None), a.ndim)
    # turn axes into a 1d array of axes indices
    axes = np.array(axes).flatten()

    bad_indices = (axes < (-a.ndim + 1)) | axes > (a.ndim - 1)
    if np.any(bad_indices):
        raise ValueError('The axis index/indices were out of range.')

    # Turn negative indices into real indices
    axes[axes < 0] = a.ndim + axes[axes < 0]

    if np.unique(axes).shape != axes.shape:
        raise ValueError('Repeated axis indices were given.')

    indexing_shape = np.array(a.shape)[axes]

    for ind in np.ndindex(*indexing_shape):
        indices[axes] = ind
        yield a[tuple(indices)]


This can be used simply with:

>>> a = np.ones([2, 3, 4, 5])
>>> for s in slices(a, 2):
...  print s.shape
...
(2, 3, 5)
(2, 3, 5)
(2, 3, 5)
(2, 3, 5)


Or slightly with the slightly more complex:

>>> len(list(slices(a, [2, -1])))
20

Without focusing on my actual implementation, would this kind of interface
be more desirable?

Cheers,




On 26 April 2013 12:33, Robert Kern <robert.kern at gmail.com> wrote:

> On Fri, Apr 26, 2013 at 12:26 PM, Andrew Giessel
> <andrew_giessel at hms.harvard.edu> wrote:
> > I agree with Charles that rollaxis() isn't immediately intuitive.
> >
> > It seems to me that documentation like this doesn't belong in rollaxis()
> but
> > instead wherever people talk about indexing and/or iterating over an
> array.
> > Nothing about the iteration depends on rollaxis(),  rollaxis is just
> giving
> > you a different view of the array to call __getitem__() on, if I
> understand
> > correctly.
>
> Docstrings are perfect places to briefly describe and demonstrate
> common use cases for a function. There is no problem with including
> the example that I wrote in the rollaxis() docstring.
>
> In any case, whether you put the documentation in the rollaxis()
> docstring or in one of the indexing/iteration sections, or
> (preferably) both, I strongly encourage you to do that first and see
> how it goes before adding a new alias.
>
> --
> Robert Kern
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130426/28af3da2/attachment.html>


More information about the NumPy-Discussion mailing list