Skip to content

Commit 4574364

Browse files
committed
address comments
cherry-pick
1 parent dffdc5a commit 4574364

File tree

5 files changed

+62
-104
lines changed

5 files changed

+62
-104
lines changed

dpnp/dpnp_array.py

-25
Original file line numberDiff line numberDiff line change
@@ -1033,31 +1033,6 @@ def prod(
10331033

10341034
return dpnp.prod(self, axis, dtype, out, keepdims, initial, where)
10351035

1036-
def ptp(
1037-
self,
1038-
axis=None,
1039-
out=None,
1040-
keepdims=numpy._NoValue,
1041-
device=None,
1042-
usm_type=None,
1043-
sycl_queue=None,
1044-
):
1045-
"""
1046-
Range of values (maximum - minimum) along an axis.
1047-
1048-
For full documentation refer to :obj:`numpy.ptp`.
1049-
"""
1050-
1051-
return dpnp.ptp(
1052-
self,
1053-
axis=axis,
1054-
out=out,
1055-
keepdims=keepdims,
1056-
device=device,
1057-
usm_type=usm_type,
1058-
sycl_queue=sycl_queue,
1059-
)
1060-
10611036
def put(self, indices, vals, /, *, axis=None, mode="wrap"):
10621037
"""
10631038
Puts values of an array into another array along a given axis.

dpnp/dpnp_iface_arraycreation.py

-64
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
"ogrid",
8181
"ones",
8282
"ones_like",
83-
"ptp",
8483
"trace",
8584
"tri",
8685
"tril",
@@ -1602,69 +1601,6 @@ def ones_like(
16021601
return call_origin(numpy.ones_like, x1, dtype, order, subok, shape)
16031602

16041603

1605-
def ptp(
1606-
arr,
1607-
/,
1608-
axis=None,
1609-
out=None,
1610-
keepdims=numpy._NoValue,
1611-
*,
1612-
device=None,
1613-
usm_type=None,
1614-
sycl_queue=None,
1615-
):
1616-
"""
1617-
Range of values (maximum - minimum) along an axis.
1618-
1619-
For full documentation refer to :obj:`numpy.ptp`.
1620-
1621-
Returns
1622-
-------
1623-
ptp : dpnp.ndarray
1624-
The range of a given array.
1625-
1626-
Limitations
1627-
-----------
1628-
Input array is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray`.
1629-
Parameters `out` and `keepdims` are supported only with default values.
1630-
Otherwise the function will be executed sequentially on CPU.
1631-
1632-
Examples
1633-
--------
1634-
>>> import dpnp as np
1635-
>>> x = np.array([[4, 9, 2, 10],[6, 9, 7, 12]])
1636-
>>> np.ptp(x, axis=1)
1637-
array([8, 6])
1638-
1639-
>>> np.ptp(x, axis=0)
1640-
array([2, 0, 5, 2])
1641-
1642-
>>> np.ptp(x)
1643-
array(10)
1644-
"""
1645-
if not isinstance(arr, (dpnp.ndarray, dpt.usm_ndarray)):
1646-
pass
1647-
elif axis is not None and not isinstance(axis, int):
1648-
pass
1649-
elif out is not None:
1650-
pass
1651-
elif keepdims is not numpy._NoValue:
1652-
pass
1653-
else:
1654-
max_array = dpnp.max(arr, axis=axis)
1655-
min_array = dpnp.min(arr, axis=axis)
1656-
1657-
_usm_type = arr.usm_type if usm_type is None else usm_type
1658-
_sycl_queue = dpnp.get_normalized_queue_device(
1659-
arr, sycl_queue=sycl_queue, device=device
1660-
)
1661-
return dpnp.array(
1662-
max_array - min_array, usm_type=_usm_type, sycl_queue=_sycl_queue
1663-
)
1664-
1665-
return call_origin(numpy.ptp, arr, axis, out, keepdims)
1666-
1667-
16681604
def trace(x1, offset=0, axis1=0, axis2=1, dtype=None, out=None):
16691605
"""
16701606
Return the sum along diagonals of the array.

dpnp/dpnp_iface_statistics.py

+50
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"mean",
6161
"median",
6262
"min",
63+
"ptp",
6364
"nanvar",
6465
"std",
6566
"var",
@@ -621,6 +622,55 @@ def min(a, axis=None, out=None, keepdims=False, initial=None, where=True):
621622
return call_origin(numpy.min, a, axis, out, keepdims, initial, where)
622623

623624

625+
def ptp(
626+
a,
627+
/,
628+
axis=None,
629+
out=None,
630+
keepdims=False,
631+
):
632+
"""
633+
Range of values (maximum - minimum) along an axis.
634+
635+
For full documentation refer to :obj:`numpy.ptp`.
636+
637+
Returns
638+
-------
639+
ptp : dpnp.ndarray
640+
The range of a given array.
641+
642+
Limitations
643+
-----------
644+
Input array is supported as :class:`dpnp.dpnp_array` or :class:`dpctl.tensor.usm_ndarray`.
645+
Otherwise the function will be executed sequentially on CPU.
646+
647+
Examples
648+
--------
649+
>>> import dpnp as np
650+
>>> x = np.array([[4, 9, 2, 10],[6, 9, 7, 12]])
651+
>>> np.ptp(x, axis=1)
652+
array([8, 6])
653+
654+
>>> np.ptp(x, axis=0)
655+
array([2, 0, 5, 2])
656+
657+
>>> np.ptp(x)
658+
array(10)
659+
660+
"""
661+
662+
if not isinstance(a, (dpnp.ndarray, dpt.usm_ndarray)):
663+
pass
664+
else:
665+
return dpnp.subtract(
666+
dpnp.max(a, axis=axis, keepdims=keepdims),
667+
dpnp.min(a, axis=axis, keepdims=keepdims),
668+
out=out,
669+
)
670+
671+
return call_origin(numpy.ptp, a, axis, out, keepdims)
672+
673+
624674
def nanvar(x1, axis=None, dtype=None, out=None, ddof=0, keepdims=False):
625675
"""
626676
Compute the variance along the specified axis, while ignoring NaNs.

tests/skipped_tests_gpu.tbl

+1-3
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,7 @@ tests/third_party/cupy/core_tests/test_ndarray_copy_and_view.py::TestArrayFlatte
244244
tests/third_party/cupy/core_tests/test_ndarray_copy_and_view.py::TestArrayFlatten::test_flatten_order_copied
245245
tests/third_party/cupy/core_tests/test_ndarray_copy_and_view.py::TestArrayFlatten::test_flatten_order_transposed
246246

247-
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestArrayReduction::test_ptp_nan
248-
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestArrayReduction::test_ptp_nan_imag
249-
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestArrayReduction::test_ptp_nan_real
247+
250248

251249
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestCubReduction_param_0_{order='C', shape=(10,)}::test_cub_max
252250
tests/third_party/cupy/core_tests/test_ndarray_reduction.py::TestCubReduction_param_0_{order='C', shape=(10,)}::test_cub_min

tests/third_party/cupy/core_tests/test_ndarray_reduction.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from tests.third_party.cupy import testing
1010

1111

12-
@pytest.mark.usefixtures("allow_fall_back_on_numpy")
1312
@testing.gpu
1413
class TestArrayReduction(unittest.TestCase):
1514
@testing.for_all_dtypes()
@@ -149,70 +148,70 @@ def test_min_nan_imag(self, xp, dtype):
149148
@testing.numpy_cupy_allclose()
150149
def test_ptp_all(self, xp, dtype):
151150
a = testing.shaped_random((2, 3), xp, dtype)
152-
return a.ptp()
151+
return xp.ptp(a)
153152

154153
@testing.with_requires("numpy>=1.15")
155154
@testing.for_all_dtypes(no_bool=True)
156155
@testing.numpy_cupy_allclose()
157156
def test_ptp_all_keepdims(self, xp, dtype):
158157
a = testing.shaped_random((2, 3), xp, dtype)
159-
return a.ptp(keepdims=True)
158+
return xp.ptp(a, keepdims=True)
160159

161160
@testing.for_all_dtypes(no_bool=True)
162161
@testing.numpy_cupy_allclose()
163162
def test_ptp_axis_large(self, xp, dtype):
164163
a = testing.shaped_random((3, 1000), xp, dtype)
165-
return a.ptp(axis=0)
164+
return xp.ptp(a, axis=0)
166165

167166
@testing.for_all_dtypes(no_bool=True)
168167
@testing.numpy_cupy_allclose()
169168
def test_ptp_axis0(self, xp, dtype):
170169
a = testing.shaped_random((2, 3, 4), xp, dtype)
171-
return a.ptp(axis=0)
170+
return xp.ptp(a, axis=0)
172171

173172
@testing.for_all_dtypes(no_bool=True)
174173
@testing.numpy_cupy_allclose()
175174
def test_ptp_axis1(self, xp, dtype):
176175
a = testing.shaped_random((2, 3, 4), xp, dtype)
177-
return a.ptp(axis=1)
176+
return xp.ptp(a, axis=1)
178177

179178
@testing.for_all_dtypes(no_bool=True)
180179
@testing.numpy_cupy_allclose()
181180
def test_ptp_axis2(self, xp, dtype):
182181
a = testing.shaped_random((2, 3, 4), xp, dtype)
183-
return a.ptp(axis=2)
182+
return xp.ptp(a, axis=2)
184183

185184
@testing.with_requires("numpy>=1.15")
186185
@testing.for_all_dtypes(no_bool=True)
187186
@testing.numpy_cupy_allclose()
188187
def test_ptp_multiple_axes(self, xp, dtype):
189188
a = testing.shaped_random((2, 3, 4), xp, dtype)
190-
return a.ptp(axis=(1, 2))
189+
return xp.ptp(a, axis=(1, 2))
191190

192191
@testing.with_requires("numpy>=1.15")
193192
@testing.for_all_dtypes(no_bool=True)
194193
@testing.numpy_cupy_allclose()
195194
def test_ptp_multiple_axes_keepdims(self, xp, dtype):
196195
a = testing.shaped_random((2, 3, 4), xp, dtype)
197-
return a.ptp(axis=(1, 2), keepdims=True)
196+
return xp.ptp(a, axis=(1, 2), keepdims=True)
198197

199198
@testing.for_float_dtypes()
200199
@testing.numpy_cupy_allclose()
201200
def test_ptp_nan(self, xp, dtype):
202201
a = xp.array([float("nan"), 1, -1], dtype)
203-
return a.ptp()
202+
return xp.ptp(a)
204203

205204
@testing.for_complex_dtypes()
206205
@testing.numpy_cupy_allclose()
207206
def test_ptp_nan_real(self, xp, dtype):
208207
a = xp.array([float("nan"), 1, -1], dtype)
209-
return a.ptp()
208+
return xp.ptp(a)
210209

211210
@testing.for_complex_dtypes()
212211
@testing.numpy_cupy_allclose()
213212
def test_ptp_nan_imag(self, xp, dtype):
214213
a = xp.array([float("nan") * 1.0j, 1.0j, -1.0j], dtype)
215-
return a.ptp()
214+
return xp.ptp(a)
216215

217216

218217
# This class compares CUB results against NumPy's

0 commit comments

Comments
 (0)