[Numpy-discussion] A proposed change to rollaxis() behavior for negative 'start' values
Anne Archibald
aarchiba@physics.mcgill...
Thu Sep 23 10:32:09 CDT 2010
On 23 September 2010 02:20, Ralf Gommers <ralf.gommers@googlemail.com> wrote:
>
>
> On Wed, Sep 22, 2010 at 4:14 AM, Anne Archibald <aarchiba@physics.mcgill.ca>
> wrote:
>>
>> 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?
>
> Allowing "end" is an easy solution, but note that moving an axis to the end
> is already possible:
>
>>>> a = np.ones((3,4,5,6))
>>>> np.rollaxis(a, 2, len(a)+1).shape # roll axis to to last position
> (3, 4, 6, 5)
>
> Not consistent with insert though, there you would use len(a) instead of
> len(a)+1. It's a little ugly, but perhaps just documenting this is no worse
> than allowing a string or adding yet another function.
Just a quick correction: len(a) gives a.shape[0], while what you want
is actually len(a.shape). So:
In [1]: a = np.zeros((2,3,4,5,6))
In [2]: len(a)
Out[2]: 2
In [8]: np.rollaxis(a,0,len(a.shape)).shape
Out[8]: (3, 4, 5, 6, 2)
So it behaves just like insert. But "len(a.shape)" is rather
cumbersome, especially if you haven't given a a name yet:
d = (a+b*c).rollaxis(2,'end')
Anne
> Ralf
>
>
>>
>> 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
>> >
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion@scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
More information about the NumPy-Discussion
mailing list