question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MAINT: Replace np.rollaxis with np.moveaxis

See original GitHub issue

Context

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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
ogauthecommented, Jul 28, 2021

moveaxis is nothing but a wrapper around transpose (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, while transpose returns a non-contiguous view. I see no other option since the wrapper part is very short.

I personnally find transpose syntax more intuitive than moveaxis (always get confused between source and destination), however moveaxis is more robust with input check.

1reaction
rgommerscommented, Jul 28, 2021

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 up moveaxis.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reason why numpy rollaxis is so confusing? - Stack Overflow
Since the number of axes doesn't change, it doesn't matter if start refers to the original or the resulting axis order. Basically calling...
Read more >
documentation of rollaxis · Issue #15926 · numpy ... - GitHub
@eric-wieser, given that np.moveaxis exists, is it possible to ... to replace an existing call to rollaxis with a call to moveaxis (as ......
Read more >
numpy.moveaxis — NumPy v1.24 Manual
Returns: resultnp.ndarray. Array with moved axes. This array is a view of the input array.
Read more >
Writing a 3-band image with rasterio - GIS Stack Exchange
To rearrange the axis (i.e., move the bands channel to make it the first axis), you can use np.rollaxis . with rasterio.open('image.tif', ...
Read more >
SciPy 1.8.0 Release Notes — SciPy v1.9.3 Manual
All users are encouraged to upgrade to this release, as there are a large number of bug-fixes and ... #14491: MAINT: Replace np.rollaxis...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found