On Thu, Sep 23, 2010 at 12:20 AM, 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.


It is a common enough operation that it would be nice to have a less cumbersome way to specify it.

Chuck