Skip to content
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

Closed
wants to merge 13 commits into from
1 change: 1 addition & 0 deletions docs/source/reference/numpysupported.rst
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ The following top-level functions are supported:
* :func:`numpy.array` (only the 2 first arguments)
* :func:`numpy.array_equal`
* :func:`numpy.asarray` (only the 2 first arguments)
* :func:`numpy.asarray_chkfinite` (only the first first arguments)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* :func:`numpy.asarray_chkfinite` (only the first first arguments)
* :func:`numpy.asarray_chkfinite` (only the 2 first arguments)

Copy link
Member

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?

* :func:`numpy.asfarray`
* :func:`numpy.asfortranarray` (only the first argument)
* :func:`numpy.atleast_1d`
Expand Down
22 changes: 22 additions & 0 deletions numba/np/arraymath.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@esc Should this check that the dtype variable as actually a Numba dtype type instance? e.g. if a user passes in a string like 'float32' they are going to get an attribute error.


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:
Expand Down
80 changes: 80 additions & 0 deletions numba/tests/test_np_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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))
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down