MAINT: Replace np.rollaxis with np.moveaxis
See original GitHub issueContext
NumPy initially added np.rollaxis
, but later, several users reported confusing API/working of rollaxis
(see this SO discussion or numpy/numpy#15926 or numpy/numpy#9473). Thus, a more clear and crisp function to achieve the same goal was added i.e named np.moveaxis
.
Quoting @shoyer from the StackOverflow discussion.
NumPy v1.11 and newer includes a new function, moveaxis, that I recommend using instead of rollaxis (disclaimer: I wrote it!). The source axis always ends up at the destination, without any funny off-by-one issues depending on whether start is greater or less than end.
NumPy has already removed any use of rollaxis
internally in favour of moveaxis
with numpy/numpy#9475.
According to rollaxis
documentation:
This function continues to be supported for backward compatibility, but you should prefer moveaxis.
In fact, array/tensor libraries like torch
don’t even have rollaxis
, instead, they have the moveaxis
method. (See docs).
Having old functions which are not present in other libraries and are not very user friendly make it difficult to read/understand the source code.
Pitch
I propose to replace the use of rollaxis
with moveaxis
in SciPy internally and refactor accordingly, making things a little more coherent. This was added in numpy v1.11.0
, hence it is easy to move away since SciPy claims support for only numpy>=1.16.5
.
I can send a PR if there aren’t any issues that I’m missing.
cc @rgommers
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:7 (7 by maintainers)
moveaxis
is nothing but a wrapper aroundtranspose
(see https://github.com/numpy/numpy/blob/v1.21.0/numpy/core/numeric.py#L1398-L1466).Since there are several ways to write
moveaxis
input for the same transpose operation, I suppose in some cases it returns a (copied) contiguous array, whiletranspose
returns a non-contiguous view. I see no other option since the wrapper part is very short.I personnally find
transpose
syntax more intuitive thanmoveaxis
(always get confused betweensource
anddestination
), howevermoveaxis
is more robust with input check.If it’s an actual transpose (i.e. a 2-D array) that sounds good. Please don’t use
transpose
for other operations, because it’s non-portable and unintuitive. If speed actually matters, it’d be much better to speed upmoveaxis
.