[Numpy-discussion] A proposed change to rollaxis() behavior for negative 'start' values
Anne Archibald
aarchiba@physics.mcgill...
Tue Sep 21 15:14:34 CDT 2010
Hi Ken,
This is a tricky one. The current behaviour of rollaxis is to remove
the requested axis from the list of axes and then insert it before the
axis specified. This is exactly how python's list insertion works:
In [1]: a = range(10)
In [3]: a.insert(-1,'a')
In [4]: a
Out[4]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 'a', 9]
And indeed, there's no clean way to add something to the end of a list
using insert (apart from the obvious a.insert(len(a),'b') ). For this
you have .append(). Unfortunately numpy's rollaxis, while it agrees
with insert in its behaviour, doesn't have a move_axis_to_end. The
situation is also somewhat muddied by the fact that rollaxis also
removes the axis from the original list of axes, so that the
interpretation of index numbers is a little more subtle. But I think
your suggested behaviour would be confusing because of the conflict
with python's insert. How about allowing the string "end" as an
argument to rollaxis to specify that the axis should go at the end?
Anne
On 21 September 2010 15:48, Ken Basye <kbasye1@jhu.edu> wrote:
> Hi Numpy Folks,
> A while back, I filed this ticket:
> http://projects.scipy.org/numpy/ticket/1441 suggesting a change to
> rollaxis() and some fixes to the doc and error reporting. Ralf Gommers
> suggested I float the behavior change here, so that's what I'm doing.
>
> The motivation for the change comes because it seems like there should
> be a simpler way to get some axis into the last position than to do this:
>
> >>> a = np.ones((3,4,5,6))
> >>> b = np.rollaxis(a, axis=0, start=len(a.shape))
> >>> b.shape
> (4, 5, 6, 3)
>
> But currently it seems there isn't because when you specify -1 as the
> 'start' argument, the axis is moved into the second-to-last position.
> My proposed change, which you can see on the ticket, would change that
> so that using -1 referred to the end position. Note that the use of
> negative 'start' arguments isn't currently documented and, in its
> current form, doesn't seem very useful. My proposal wouldn't change the
> behavior for positive 'start' values at all, and the interpretation of
> 'axis' arguments is also unaffected.
>
> If that's going to break too much code, here's a pathway that might be
> acceptable: Add a new function moveaxis() which works the way
> rollaxis() does for positive arguments but in the new way for negative
> arguments. Eventually, rollaxis could be deprecated to keep things
> tidy. This has the added advantage of using a name that seems to fit
> what the function does better - 'rollaxis' suggests a behavior like the
> roll() function which affects other axes, which isn't what happens.
>
> Thanks for listening; I'm a big fan of Numpy.
>
> Best,
> Ken Basye
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
More information about the NumPy-Discussion
mailing list