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
Support for np.roll #3563
Support for np.roll #3563
Conversation
I think the CI failure is unrelated; I reckon it would pass on retry. I have implemented the first two (mandatory) arguments. Let me know what you think if you get a mo. |
Codecov Report
@@ Coverage Diff @@
## master #3563 +/- ##
=========================================
Coverage ? 80.49%
=========================================
Files ? 395
Lines ? 81347
Branches ? 9261
=========================================
Hits ? 65479
Misses ? 14437
Partials ? 1431 |
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 contribution. In general the implementation etc looks good. I've made a few minor comments, once addressed this should be good to merge. Thanks again.
@@ -292,6 +292,7 @@ The following top-level functions are supported: | |||
* :func:`numpy.partition` (only the 2 first arguments) | |||
* :func:`numpy.ravel` (no order argument; 'C' order only) | |||
* :func:`numpy.reshape` (no order argument; 'C' order only) | |||
* :func:`numpy.roll` (only the 2 first arguments) |
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.
Whilst implied by lack of axis
support, perhaps this should also explicitly mention that only integer value for shift
is supported?
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.
Done
yield np.array([]) | ||
yield (9,) | ||
yield np.asfortranarray(np.array([[1.1, np.nan], [np.inf, 7.8]])) | ||
yield np.array([]) |
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.
this input is duplicated from about 3 lines up.
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.
Fixed
numba/targets/arraymath.py
Outdated
raise TypingError('shift must be an integer') | ||
|
||
def np_roll_impl(a, shift): | ||
arr = _asarray(a) |
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.
seems like this makes scalar inputs into arrays, but then this is not converted back to a scalar on return? e.g.
from numba import njit
import numpy as np
z = 1
def foo(z):
ff = np.roll(z, 1)
return ff
expect = foo(z)
got = njit(foo)(z)
print(type(expect), expect.ndim, expect)
print(type(got), got.ndim, got)
np.testing.assert_allclose(got, expect)
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.
Bah - it's a 0D array regardless of shift.
I thought this would be a one-liner but my tests fail depending on what order I run them in, which I appreciate is somewhat suboptimal :)
Here's a simple repro:
from numba import njit
from numba.extending import overload, types
import numpy as np
def foo(a, shift):
pass
@overload(foo)
def foo_impl(a, shift):
def foo_impl_0d(a, shift):
return np.array(a)
if isinstance(a, (types.Boolean, types.Number)):
return foo_impl_0d
@njit
def foo_quickly(a, shift):
return foo(a, shift)
def check_stuff(a_values):
for idx, a in enumerate(a_values):
got = foo_quickly(a, 1).item(0)
assert got is a, 'got {0} but expected {1}'.format(got, a)
if __name__ == '__main__':
# this passes
check_stuff([False, True])
check_stuff([0, 1])
# comment out the above and un-comment the below: fails
# check_stuff([0, 1])
# check_stuff([False, True])
Per the comments, the assertions pass if I run the tests in one order, but fail if I switch the order.
If you inspect the signatures of foo_quickly
then the former case ends up with [(bool, int64), (int64, int64)] whilst the latter only has [(int64, int64)]).
Have I made some festive mishap?
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 think this needs #3612
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 simply swapped the test order - #3612 would mean I didn't have to, but I figured just as easy to order the tests.
Thanks vm for the review - I will address these points asap |
Think that this needs a conflict resolve and then perhaps a sprinkling of |
Yep, leave it with me (the same is probably true of most / all of my other PRs). I will ping you once done. |
Initial commit for CI.