-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 for np.asarray_chkfinite [WIP] [need help] #6279
Changes from 8 commits
01b5c6b
cb78e0e
bc5beb4
b9491c1
a50fa05
0ef86c9
1be9be5
a578490
a05d6b1
c24e3aa
c9960c4
8398763
4a448ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4159,6 +4159,28 @@ def np_select_arr_impl(condlist, choicelist, default=0): | |
|
||
return np_select_arr_impl | ||
|
||
|
||
@overload(np.asarray_chkfinite) | ||
def np_asarray_chkfinite(a, dtype=None, order='C'): | ||
|
||
msg = "The argument to np.asarray_chkfinite must be array-like" | ||
if not isinstance(a, (types.Array, types.Sequence, types.Tuple)): | ||
raise TypingError(msg) | ||
|
||
if is_nonelike(dtype): | ||
dt = a.dtype | ||
else: | ||
dt = dtype.dtype | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @esc Should this check that the |
||
|
||
def impl(a, dtype=None, order='C'): | ||
rishabhvarshney14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
a = np.asarray(a, dtype=dt) | ||
for i in np.nditer(a): | ||
if not np.isfinite(i): | ||
raise ValueError("array must not contain infs or NaNs") | ||
return a | ||
|
||
return impl | ||
|
||
#---------------------------------------------------------------------------- | ||
# Windowing functions | ||
# - translated from the numpy implementations found in: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -321,6 +321,10 @@ def flip_ud(a): | |
return np.flipud(a) | ||
|
||
|
||
def np_asarray_chkfinite(a, dtype=None, order='C'): | ||
return np.asarray_chkfinite(a, dtype, order) | ||
|
||
|
||
class TestNPFunctions(MemoryLeakMixin, TestCase): | ||
""" | ||
Tests for various Numpy functions. | ||
|
@@ -3707,6 +3711,82 @@ def test_cross2d_exceptions(self): | |
str(raises.exception) | ||
) | ||
|
||
def test_asarray_chkfinite(self): | ||
pyfunc = np_asarray_chkfinite | ||
cfunc = jit(nopython=True)(pyfunc) | ||
self.disable_leak_check() | ||
|
||
pairs = [ | ||
#1D array with all args | ||
( | ||
np.array([1, 2, 3]), | ||
np.float32, | ||
'C' | ||
), | ||
#1D array | ||
( | ||
np.array([1, 2, 3]), | ||
), | ||
#1D array-like | ||
( | ||
[1, 2, 3, 4], | ||
), | ||
# 2x2 (n-dims) | ||
( | ||
np.array([[1, 2], [3, 4]]), | ||
np.float32, | ||
'C' | ||
), | ||
# 2x2 array-like (n-dims) | ||
( | ||
((1, 2), (3, 4)), | ||
np.int64 | ||
), | ||
# 2x2 (1-dim) with type promotion | ||
( | ||
np.array([1, 2], dtype=np.int64), | ||
), | ||
# 3x2 (with higher order broadcasting) | ||
( | ||
np.arange(36).reshape(6, 2, 3), | ||
) | ||
] | ||
|
||
for pair in pairs: | ||
expected = pyfunc(*pair) | ||
got = cfunc(*pair) | ||
self.assertPreciseEqual(expected, got) | ||
|
||
def test_asarray_chkfinite_exceptions(self): | ||
cfunc = jit(nopython=True)(np_asarray_chkfinite) | ||
self.disable_leak_check() | ||
|
||
#test for single value | ||
with self.assertRaises(TypingError) as e: | ||
cfunc(2) | ||
msg = "The argument to np.asarray_chkfinite must be array-like" | ||
self.assertIn(msg, str(e.exception)) | ||
|
||
#test for NaNs | ||
with self.assertRaises(ValueError) as e: | ||
cfunc(np.array([2, 4, np.nan, 5])) | ||
self.assertIn("array must not contain infs or NaNs", str(e.exception)) | ||
|
||
#test for infs | ||
with self.assertRaises(ValueError) as e: | ||
cfunc(np.array([1, 2, np.inf, 4])) | ||
self.assertIn("array must not contain infs or NaNs", str(e.exception)) | ||
|
||
#test for both inf and NaNs | ||
with self.assertRaises(ValueError) as e: | ||
cfunc(np.array([np.inf, np.nan])) | ||
self.assertIn("array must not contain infs or NaNs", str(e.exception)) | ||
|
||
#test for NaNs | ||
with self.assertRaises(ValueError) as e: | ||
cfunc(np.array([np.nan, np.nan, np.nan])) | ||
self.assertIn("array must not contain infs or NaNs", str(e.exception)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @esc I'm not sure what these tests gain on the previous two given the alg present? |
||
|
||
|
||
class TestNPMachineParameters(TestCase): | ||
# tests np.finfo, np.iinfo, np.MachAr | ||
|
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.
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.
Yeah, either that or
only the first two arguments
, but I guess that would be inconsistent with the rest of the doc?