From 15a6d261f9516ce1d18e550f6240c73f60265a7c Mon Sep 17 00:00:00 2001 From: Guilherme Leobas Date: Tue, 24 Mar 2020 23:00:58 -0300 Subject: [PATCH 01/11] add asfarray impl --- numba/core/types/__init__.py | 2 +- numba/np/arraymath.py | 3 +++ numba/tests/test_np_functions.py | 23 +++++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/numba/core/types/__init__.py b/numba/core/types/__init__.py index 4b37577c098..d08a56ff10d 100644 --- a/numba/core/types/__init__.py +++ b/numba/core/types/__init__.py @@ -90,7 +90,7 @@ c8 = complex64 c16 = complex128 -float_ = float32 +float_ = float64 double = float64 void = none diff --git a/numba/np/arraymath.py b/numba/np/arraymath.py index 0eaf41eaa76..6647fb238bb 100644 --- a/numba/np/arraymath.py +++ b/numba/np/arraymath.py @@ -3885,6 +3885,9 @@ def impl(a, dtype=None): return impl +# @overload(np.asanyarray) +# def np_asanyarray(a, dtype=None): +# return np_asarray(a, dtype=dtype) @overload(np.extract) def np_extract(condition, arr): diff --git a/numba/tests/test_np_functions.py b/numba/tests/test_np_functions.py index 228f9c7f1a5..1a402ff5229 100644 --- a/numba/tests/test_np_functions.py +++ b/numba/tests/test_np_functions.py @@ -236,6 +236,10 @@ def asarray_kws(a, dtype): return np.asarray(a, dtype=dtype) +def asfarray(a, dtype=np.float_): + return np.asfarray(a, dtype=dtype) + + def extract(condition, arr): return np.extract(condition, arr) @@ -3060,6 +3064,25 @@ def check_pass_through(jitted, expect_same, params): else: check_pass_through(cfunc, True, params) + def test_asfarray(self): + def inputs(): + yield np.array([1, 2, 3]), None + yield np.array([2, 3], dtype=np.float32), np.float32 + yield np.array([2, 3], dtype=np.int8), np.int8 + + pyfunc = asfarray + cfunc = jit(nopython=True)(pyfunc) + + for arr, dt in inputs(): + if dt is None: + expected = pyfunc(arr) + got = cfunc(arr) + else: + expected = pyfunc(arr, dtype=dt) + got = cfunc(arr, dtype=dt) + self.assertPreciseEqual(expected, got) + self.assertTrue(np.issubdtype(got.dtype, np.floating), got.dtype) + def test_repeat(self): # np.repeat(a, repeats) np_pyfunc = np_repeat From 28c2bf46bbfaebc8439521ff92dd629253a8418a Mon Sep 17 00:00:00 2001 From: Guilherme Leobas Date: Tue, 24 Mar 2020 23:53:40 -0300 Subject: [PATCH 02/11] add impl --- numba/np/arraymath.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/numba/np/arraymath.py b/numba/np/arraymath.py index 6647fb238bb..4bd026ab501 100644 --- a/numba/np/arraymath.py +++ b/numba/np/arraymath.py @@ -3885,9 +3885,20 @@ def impl(a, dtype=None): return impl -# @overload(np.asanyarray) -# def np_asanyarray(a, dtype=None): -# return np_asarray(a, dtype=dtype) +@overload(np.asfarray) +def np_asfarray(a, dtype=types.float_): + if is_nonelike(dtype): + dt = types.float_ + else: + dt = dtype.dtype + if not isinstance(dt, types.Float): + dt = types.float_ + + def impl(a, dtype=types.float_): + return np.asarray(a, dt) + return impl + + @overload(np.extract) def np_extract(condition, arr): From 7310b0f065e4515ce15bd00e3693465ec99668e1 Mon Sep 17 00:00:00 2001 From: Guilherme Leobas Date: Wed, 25 Mar 2020 11:21:17 -0300 Subject: [PATCH 03/11] undo float_ changes --- numba/core/types/__init__.py | 2 +- numba/np/arraymath.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/numba/core/types/__init__.py b/numba/core/types/__init__.py index d08a56ff10d..4b37577c098 100644 --- a/numba/core/types/__init__.py +++ b/numba/core/types/__init__.py @@ -90,7 +90,7 @@ c8 = complex64 c16 = complex128 -float_ = float64 +float_ = float32 double = float64 void = none diff --git a/numba/np/arraymath.py b/numba/np/arraymath.py index 4bd026ab501..fe1f8272c65 100644 --- a/numba/np/arraymath.py +++ b/numba/np/arraymath.py @@ -3886,15 +3886,15 @@ def impl(a, dtype=None): @overload(np.asfarray) -def np_asfarray(a, dtype=types.float_): +def np_asfarray(a, dtype=types.float64): if is_nonelike(dtype): - dt = types.float_ + dt = types.float64 else: dt = dtype.dtype if not isinstance(dt, types.Float): - dt = types.float_ + dt = types.float64 - def impl(a, dtype=types.float_): + def impl(a, dtype=types.float64): return np.asarray(a, dt) return impl From eeb051b8be4dffe322d34cdd38ef000ee6a645d0 Mon Sep 17 00:00:00 2001 From: Guilherme Leobas Date: Thu, 26 Mar 2020 20:06:18 -0300 Subject: [PATCH 04/11] Update numpysupported.rst --- docs/source/reference/numpysupported.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/source/reference/numpysupported.rst b/docs/source/reference/numpysupported.rst index d24c19564bc..83369c74b71 100644 --- a/docs/source/reference/numpysupported.rst +++ b/docs/source/reference/numpysupported.rst @@ -356,6 +356,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.asfarray` * :func:`numpy.asfortranarray` (only the first argument) * :func:`numpy.atleast_1d` * :func:`numpy.atleast_2d` From 08bf31d704e0b877c3f362ed1198f4dd90248d55 Mon Sep 17 00:00:00 2001 From: Guilherme Leobas Date: Tue, 19 May 2020 22:52:10 -0300 Subject: [PATCH 05/11] add complex to np.asfarray --- numba/np/arraymath.py | 11 +++++------ numba/tests/test_np_functions.py | 4 +++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/numba/np/arraymath.py b/numba/np/arraymath.py index ccc10bf0d32..730363fecea 100644 --- a/numba/np/arraymath.py +++ b/numba/np/arraymath.py @@ -3901,15 +3901,14 @@ def impl(a, dtype=None): @overload(np.asfarray) def np_asfarray(a, dtype=types.float64): - if is_nonelike(dtype): - dt = types.float64 + dtype = as_dtype(dtype) + if not np.issubdtype(dtype, np.inexact): + dx = types.float64 else: - dt = dtype.dtype - if not isinstance(dt, types.Float): - dt = types.float64 + dx = dtype def impl(a, dtype=types.float64): - return np.asarray(a, dt) + return np.asarray(a, dx) return impl diff --git a/numba/tests/test_np_functions.py b/numba/tests/test_np_functions.py index 22e2942d982..7c31c7ba436 100644 --- a/numba/tests/test_np_functions.py +++ b/numba/tests/test_np_functions.py @@ -3157,6 +3157,7 @@ def inputs(): yield np.array([1, 2, 3]), None yield np.array([2, 3], dtype=np.float32), np.float32 yield np.array([2, 3], dtype=np.int8), np.int8 + yield np.array([2, 3], dtype=np.int8), np.complex64 pyfunc = asfarray cfunc = jit(nopython=True)(pyfunc) @@ -3168,8 +3169,9 @@ def inputs(): else: expected = pyfunc(arr, dtype=dt) got = cfunc(arr, dtype=dt) + self.assertPreciseEqual(expected, got) - self.assertTrue(np.issubdtype(got.dtype, np.floating), got.dtype) + self.assertTrue(np.issubdtype(got.dtype, np.inexact), got.dtype) def test_repeat(self): # np.repeat(a, repeats) From a87a601c3dddf94644cd0d1045eae262f25cc1cb Mon Sep 17 00:00:00 2001 From: Guilherme Leobas Date: Tue, 19 May 2020 22:52:56 -0300 Subject: [PATCH 06/11] Update test_np_functions.py --- numba/tests/test_np_functions.py | 1 + 1 file changed, 1 insertion(+) diff --git a/numba/tests/test_np_functions.py b/numba/tests/test_np_functions.py index 7c31c7ba436..a93d2fe2fb1 100644 --- a/numba/tests/test_np_functions.py +++ b/numba/tests/test_np_functions.py @@ -3158,6 +3158,7 @@ def inputs(): yield np.array([2, 3], dtype=np.float32), np.float32 yield np.array([2, 3], dtype=np.int8), np.int8 yield np.array([2, 3], dtype=np.int8), np.complex64 + yield np.array([2, 3], dtype=np.int8), np.complex128 pyfunc = asfarray cfunc = jit(nopython=True)(pyfunc) From 9eb79586d4b0ecc6aee124fa080c779d0f5f8b87 Mon Sep 17 00:00:00 2001 From: Guilherme Leobas Date: Mon, 10 Aug 2020 13:23:12 -0300 Subject: [PATCH 07/11] Update arraymath.py --- numba/np/arraymath.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numba/np/arraymath.py b/numba/np/arraymath.py index 730363fecea..f7b00e3818b 100644 --- a/numba/np/arraymath.py +++ b/numba/np/arraymath.py @@ -3900,7 +3900,7 @@ def impl(a, dtype=None): @overload(np.asfarray) -def np_asfarray(a, dtype=types.float64): +def np_asfarray(a, dtype=np.float64): dtype = as_dtype(dtype) if not np.issubdtype(dtype, np.inexact): dx = types.float64 From 8865d74d5517099f2b091923abcf4460b57c38c9 Mon Sep 17 00:00:00 2001 From: Guilherme Leobas Date: Mon, 10 Aug 2020 15:04:41 -0300 Subject: [PATCH 08/11] Update arraymath.py --- numba/np/arraymath.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numba/np/arraymath.py b/numba/np/arraymath.py index 605da7aca1b..4cf973f7c1b 100644 --- a/numba/np/arraymath.py +++ b/numba/np/arraymath.py @@ -4078,7 +4078,7 @@ def np_asfarray(a, dtype=np.float64): else: dx = dtype - def impl(a, dtype=types.float64): + def impl(a, dtype=np.float64): return np.asarray(a, dx) return impl From b0a55486614b80af174cd40a9ae16e9f24f79329 Mon Sep 17 00:00:00 2001 From: Guilherme Leobas Date: Wed, 26 Aug 2020 14:04:11 -0300 Subject: [PATCH 09/11] Update test_np_functions.py --- numba/tests/test_np_functions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numba/tests/test_np_functions.py b/numba/tests/test_np_functions.py index 95ec9b85728..a197fff0a97 100644 --- a/numba/tests/test_np_functions.py +++ b/numba/tests/test_np_functions.py @@ -245,7 +245,7 @@ def asarray_kws(a, dtype): return np.asarray(a, dtype=dtype) -def asfarray(a, dtype=np.float_): +def asfarray(a, dtype=np.float64): return np.asfarray(a, dtype=dtype) From 742860f86b006f983eb33cc1bbdc48b985633a23 Mon Sep 17 00:00:00 2001 From: Guilherme Leobas Date: Wed, 26 Aug 2020 14:09:46 -0300 Subject: [PATCH 10/11] Update test_np_functions.py --- numba/tests/test_np_functions.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/numba/tests/test_np_functions.py b/numba/tests/test_np_functions.py index a197fff0a97..0c302b77da0 100644 --- a/numba/tests/test_np_functions.py +++ b/numba/tests/test_np_functions.py @@ -3215,6 +3215,8 @@ def make_unicode_list(): def test_asfarray(self): def inputs(): yield np.array([1, 2, 3]), None + yield np.array([1, 2, 3]), int # dtype will be ignored + yield np.array([1, 2, 3]), str # dtype will be ignored yield np.array([2, 3], dtype=np.float32), np.float32 yield np.array([2, 3], dtype=np.int8), np.int8 yield np.array([2, 3], dtype=np.int8), np.complex64 From af1765680ac41717246ed3b4135c08cd763336ba Mon Sep 17 00:00:00 2001 From: Guilherme Leobas Date: Wed, 26 Aug 2020 14:27:14 -0300 Subject: [PATCH 11/11] Revert "Update test_np_functions.py" This reverts commit 742860f86b006f983eb33cc1bbdc48b985633a23. --- numba/tests/test_np_functions.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/numba/tests/test_np_functions.py b/numba/tests/test_np_functions.py index 0c302b77da0..a197fff0a97 100644 --- a/numba/tests/test_np_functions.py +++ b/numba/tests/test_np_functions.py @@ -3215,8 +3215,6 @@ def make_unicode_list(): def test_asfarray(self): def inputs(): yield np.array([1, 2, 3]), None - yield np.array([1, 2, 3]), int # dtype will be ignored - yield np.array([1, 2, 3]), str # dtype will be ignored yield np.array([2, 3], dtype=np.float32), np.float32 yield np.array([2, 3], dtype=np.int8), np.int8 yield np.array([2, 3], dtype=np.int8), np.complex64