New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support of np.swapaxes #4074 #6883
Conversation
@braniii thank you for submitting this, I have added it to the queue for review. |
I can't find reference to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this feature. I've given this an initial review, there's a few small things to resolve but the patch is generally good. Thanks again!
|
||
axes_tuple = tuple_setitem(axes_list, axis1, axis2) | ||
axes_tuple = tuple_setitem(axes_tuple, axis2, axis1) | ||
return np.transpose(arr, axes_tuple) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm reasonably convinced that this will break the "swapaxes returns a view" notion as per the docs, https://numpy.org/doc/stable/reference/generated/numpy.swapaxes.html:
For NumPy >= 1.10.0, if a is an ndarray, then a view of a is returned; otherwise a new array is created.
Asserting that a is expected
and a is got
in the unittests might highlight this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a is expected
and a is got
should always be false (except when exchanging with the same axis). For being true, not only the data but also the view needs to be the same (which gets changed) or one needs to use the base
or np.share_memory
.
You are right, that the definition breaks the "swapaxes returns a view". But the reason is, that the numba version of np.transpose
does not return a view. Here a short code snippet:
import numpy as np
from numba import njit
@njita: False
def transpose(a):
return np.transpose(a)
# reference arraype command to continue
a = np.arange(8).reshape(2, -1)
b = np.transpose(a)
c = transpose(a)
# this is always false
print(f'b is a: {b is a}') # false
print(f'c is a: {c is a}') # false
# using base it works
print(f'b.base is a.base: {b.base is a.base}') # true
print(f'c.base is a.base: {c.base is a.base}') # false
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think there's much we can do about this right now? Do you feel that this behaviour is prohibitive to it being useful as-is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least from my side there is not much I can do, I have not enough inside in the code base to propose a fix. But I think the function is useful as-is. But in general it could be helpful to document difference from numpy functions, maybe simple at
https://numba.readthedocs.io/en/stable/reference/numpysupported.html
- `np.transpose()`
+ `np.transpose()` (a copy is returned)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least from my side there is not much I can do, I have not enough inside in the code base to propose a fix. But I think the function is useful as-is. But in general it could be helpful to document difference from numpy functions, maybe simple at
https://numba.readthedocs.io/en/stable/reference/numpysupported.html- `np.transpose()` + `np.transpose()` (a copy is returned)
Agree, this is probably the best thing to do for now.
When I searched for the function I found multiple results, and I did not consider that numba could be wrong ;) But you are right, I corrected my comment. And thx for the review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the patch and fixes!
This PR is adds
np.swapaxes
, which is currently unimplemented according to #4074 (ornp.swapaxis
which is the mis-spelling version). I had help by @stuartarchibald on discourse: (https://numba.discourse.group/t/how-to-use-types-integer-for-indexing-list/647)This function can be used to simplify the implementation of
np.rot90
from #6822, which is also the first step of implementing full further arguments of it.