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

Implement of dpnp.clip() #1645

Merged
merged 5 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,18 @@ def choose(input, choices, out=None, mode="raise"):

return dpnp.choose(input, choices, out, mode)

# 'clip',
def clip(self, a_min, a_max, *, out=None, order="K"):
npolina4 marked this conversation as resolved.
Show resolved Hide resolved
"""
Clip (limit) the values in an array.

See Also
--------
:obj:`dpnp.clip`
npolina4 marked this conversation as resolved.
Show resolved Hide resolved

"""

return dpnp.clip(self, a_min, a_max, out=out, order=order)

# 'compress',

def conj(self):
Expand Down
66 changes: 66 additions & 0 deletions dpnp/dpnp_iface_mathematical.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"add",
"around",
"ceil",
"clip",
"conj",
"conjugate",
"convolve",
Expand Down Expand Up @@ -387,6 +388,71 @@ def ceil(
)


def clip(a, a_min, a_max, *, out=None, order="K"):
npolina4 marked this conversation as resolved.
Show resolved Hide resolved
"""
Clip (limit) the values in an array.

For full documentation refer to :obj:`numpy.clip`.

Parameters
----------
a : {dpnp_array, usm_ndarray}
Array containing elements to clip.
a_min, a_max : {dpnp_array, usm_ndarray, None}
Minimum and maximum value. If ``None``, clipping is not performed on the corresponding edge
Only one of `a_min` and `a_max` may be ``None``. Both are broadcast against a.
npolina4 marked this conversation as resolved.
Show resolved Hide resolved
out : dpnp_array, optional
npolina4 marked this conversation as resolved.
Show resolved Hide resolved
The results will be placed in this array. It may be the input array for in-place clipping.
`out` must be of the right shape to hold the output. Its type is preserved.

npolina4 marked this conversation as resolved.
Show resolved Hide resolved
Returns
-------
out : dpnp_array
An array with the elements of `a`, but where values < `a_min` are replaced with `a_min`,
and those > `a_max` with `a_max`.

Examples
--------
>>> import dpnp as np
>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.clip(a, 1, 8)
array([1, 1, 2, 3, 4, 5, 6, 7, 8, 8])
>>> np.clip(a, 8, 1)
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
>>> np.clip(a, 3, 6, out=a)
array([3, 3, 3, 3, 4, 5, 6, 6, 6, 6])
>>> a
array([3, 3, 3, 3, 4, 5, 6, 6, 6, 6])

>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> min = np.array([3, 4, 1, 1, 1, 4, 4, 4, 4, 4])
>>> np.clip(a, min, 8)
array([3, 4, 2, 3, 4, 5, 6, 7, 8, 8])

"""

usm_arr = dpnp.get_usm_ndarray(a)
usm_min = (
dpnp.get_usm_ndarray(a_min) if isinstance(a_min, dpnp_array) else a_min
)
usm_max = (
dpnp.get_usm_ndarray(a_max) if isinstance(a_max, dpnp_array) else a_max
)
npolina4 marked this conversation as resolved.
Show resolved Hide resolved

if out is not None:
usm_out = dpnp.get_usm_ndarray(out)
npolina4 marked this conversation as resolved.
Show resolved Hide resolved
dpt.clip(usm_arr, usm_min, usm_max, out=usm_out, order=order)
npolina4 marked this conversation as resolved.
Show resolved Hide resolved
npolina4 marked this conversation as resolved.
Show resolved Hide resolved
return out

return dpnp_array._create_from_usm_ndarray(
dpt.clip(usm_arr, usm_min, usm_max, order=order)
)


def conjugate(
x,
/,
Expand Down
10 changes: 0 additions & 10 deletions tests/skipped_tests.tbl
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,6 @@ tests/third_party/cupy/math_tests/test_floating.py::TestFloating::test_ldexp
tests/third_party/cupy/math_tests/test_floating.py::TestFloating::test_nextafter_combination
tests/third_party/cupy/math_tests/test_floating.py::TestFloating::test_nextafter_float

tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_clip_min_max_none
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_external_clip4
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_fmax_nan
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_fmin_nan
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_nan_to_num
Expand All @@ -435,14 +433,6 @@ tests/third_party/cupy/math_tests/test_misc.py::TestConvolveInvalid_param_1_{mod
tests/third_party/cupy/math_tests/test_misc.py::TestConvolveInvalid_param_2_{mode='full'}::test_convolve_empty
tests/third_party/cupy/math_tests/test_misc.py::TestConvolveInvalid_param_2_{mode='full'}::test_convolve_ndim

tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_clip1
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_clip3
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_clip_min_none
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_clip_max_none
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_external_clip1
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_external_clip2
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_external_clip3
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_clip2
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_fabs
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_fabs_negative
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_nan_to_num_scalar_nan
Expand Down
11 changes: 1 addition & 10 deletions tests/skipped_tests_gpu.tbl
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,6 @@ tests/third_party/cupy/math_tests/test_floating.py::TestFloating::test_ldexp
tests/third_party/cupy/math_tests/test_floating.py::TestFloating::test_nextafter_combination
tests/third_party/cupy/math_tests/test_floating.py::TestFloating::test_nextafter_float

tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_clip_min_max_none
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_external_clip4
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_fmax_nan
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_fmin_nan
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_nan_to_num
Expand All @@ -532,14 +530,7 @@ tests/third_party/cupy/math_tests/test_misc.py::TestConvolveInvalid_param_1_{mod
tests/third_party/cupy/math_tests/test_misc.py::TestConvolveInvalid_param_2_{mode='full'}::test_convolve_empty
tests/third_party/cupy/math_tests/test_misc.py::TestConvolveInvalid_param_2_{mode='full'}::test_convolve_ndim

tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_clip1
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_clip3
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_clip_min_none
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_clip_max_none
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_external_clip1
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_external_clip2
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_external_clip3
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_clip2

tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_fabs
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_fabs_negative
tests/third_party/cupy/math_tests/test_misc.py::TestMisc::test_nan_to_num_scalar_nan
Expand Down
9 changes: 9 additions & 0 deletions tests/test_dparray.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,12 @@ def test_repeat():
numpy_array = numpy.arange(4).repeat(3)
dpnp_array = dpnp.arange(4).repeat(3)
assert_array_equal(numpy_array, dpnp_array)


def test_clip():
numpy_array = numpy.arange(10)
dpnp_array = dpnp.arange(10)
result = dpnp.clip(dpnp_array, 3, 7)
expected = numpy.clip(numpy_array, 3, 7)

assert_array_equal(expected, result)
28 changes: 28 additions & 0 deletions tests/test_mathematical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2147,3 +2147,31 @@ def test_inplace_remainder(dtype):
dp_a %= 4

assert_allclose(dp_a, np_a)


@pytest.mark.parametrize(
"dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True)
)
def test_clip(dtype):
dp_array = dpnp.asarray([1, 2, 8, 1, 6, 4, 1], dtype=dtype)
np_array = dpnp.asnumpy(dp_array)

result = dpnp.clip(dp_array, 2, 6)
expected = numpy.clip(np_array, 2, 6)
assert_allclose(expected, result)


@pytest.mark.parametrize(
"dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True)
)
def test_clip_arrays(dtype):
npolina4 marked this conversation as resolved.
Show resolved Hide resolved
dp_array = dpnp.asarray([1, 2, 8, 1, 6, 4, 1], dtype=dtype)
np_array = dpnp.asnumpy(dp_array)

min_v = dpnp.asarray(2, dtype=dtype)
max_v = dpnp.asarray(6, dtype=dtype)

result = dpnp.clip(dp_array, min_v, max_v)
expected = numpy.clip(np_array, min_v.asnumpy(), max_v.asnumpy())

assert_allclose(expected, result)
Loading