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

Sebastian Berg sebastian@sipsolutions....
Thu Apr 25 15:38:52 CDT 2013


On Thu, 2013-04-25 at 14:04 -0600, Charles R Harris wrote:
> 
> 
> On Thu, Apr 25, 2013 at 1:51 PM, <josef.pktd@gmail.com> wrote:
>         On Thu, Apr 25, 2013 at 3:40 PM, Robert Kern
>         <robert.kern@gmail.com> wrote:
>         > On Thu, Apr 25, 2013 at 8:21 PM, Andrew Giessel
>         > <andrew_giessel@hms.harvard.edu> wrote:
>         >> I respect this opinion.  However (and maybe this is
>         legacy), while reading
>         >> through the numeric.py source file, I was surprised at how
>         short many of the
>         >> functions are, generally.  Functions like ones() and
>         zeros() are pretty
>         >> simple wrappers which call empty() and then copy over
>         values.
>         >
>         > Many of these are short, but they do tend to do at least two
>         things
>         > that someone would otherwise have to do. This really isn't
>         the case
>         > for iteraxis() and rollaxis(). One can use rollaxis() pretty
>         much
>         > everywhere you would use iteraxis(), but not vice-versa.
>         >
>         >> FWIW, I had used numpy for over two years before realizing
>         that the default
>         >> behavior of iterating on a numpy array was to return slices
>         over the first
>         >> axis (although, this makes sense because it makes a 1d
>         array like a list),
>         >> and I think it is generally left out of any tutorials or
>         guides.
>         
>         
>         That definitely sounds like a documentation problem.
>         I'm using often that it's a python iterator in the first
>         dimension,
>         and can be used with *args and tuple unpacking.
>         (I didn't need it with anything else than axis=0 or axis=-1
>         for matplotlib IIRC)
>         
>         I never used rollaxis, but I have seen it a lot when I was
>         still
>         reading the nipy source.
>         
>         In general, I think that there are already too many aliases in
>         numpy,
>         or function whether it's not really clear if they are aliases
>         or
>         something slightly different.
>         
>         It took me more than a year to remember what `expand_dims` is
>         called,
>         (I always tried, add_axis) until I bookmarked it for a while.
>         
> 
> After thinking about it, I'm in favor of this small function. Rollaxis
> takes a bit of thought and document reading to figure out how to use
> it, whereas this function covers a common use with an easy to
> understand API. I'm not completely satisfied with the name, it isn't
> as memorable as I'd like, but that is a small quibble.
> 
What I am not quite happy with is, that it feels that if we want to keep
it open to understanding multiple axes (and maybe also be a method of
that same name) at some point, defaulting to flat iteration may be
better. So from a future point of view, maybe it should have axes=None
as default?

I.e. (oh, evil code!) the long term goal could be something like this
(obviously it would be preferable and much faster in C...):

def iteraxes(arr, axis=None, order='C'):
    view_shape = []
    view_strides = []
    op_axes = []
    count_axes = 0
    if axis is None:
        op_axes = range(arr.ndim)
    else:
        if not isinstance(axis, tuple):
            axis = {axis}
        else:
            axis = set(axis) # ignores duplicates...
        for ax in range(arr.ndim):
            if ax in axis:
                axis.remove(ax)
                op_axes.append(ax)
            else:
                view_shape.append(arr.shape[ax])
                view_strides.append(arr.strides[ax])
        if len(axis) != 0:
            raise ValueError
        
    i = np.nditer(arr, op_axes=[op_axes], order=order)
    for s in i:
        view = np.lib.stride_tricks.as_strided(s, view_shape,
view_strides)
        yield view


> Chuck 
> 
> 
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion




More information about the NumPy-Discussion mailing list