Skip to content

Commit 039255a

Browse files
authored
add place (#480)
* add place
1 parent b23a36e commit 039255a

File tree

5 files changed

+112
-24
lines changed

5 files changed

+112
-24
lines changed

dpnp/dpnp_algo/dpnp_algo_indexing.pyx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ __all__ += [
4343
"dpnp_diagonal",
4444
"dpnp_fill_diagonal",
4545
"dpnp_nonzero",
46+
"dpnp_place",
4647
"dpnp_put",
4748
"dpnp_take",
4849
"dpnp_tril_indices",
@@ -163,6 +164,15 @@ cpdef tuple dpnp_nonzero(dparray in_array1):
163164
return result
164165

165166

167+
cpdef dpnp_place(dparray arr, dparray mask, vals):
168+
cpdef int counter = 0
169+
cpdef int vals_len = len(vals)
170+
for i in range(arr.size):
171+
if mask[i]:
172+
arr[i] = vals[counter % vals_len]
173+
counter += 1
174+
175+
166176
cpdef dpnp_put(input, ind, v):
167177
ind_is_list = isinstance(ind, list)
168178
for i in range(input.size):

dpnp/dpnp_iface_indexing.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
"""
4141

4242

43+
import collections
44+
4345
import numpy
4446

4547
from dpnp.dpnp_algo import *
@@ -54,6 +56,7 @@
5456
"diagonal",
5557
"fill_diagonal",
5658
"nonzero",
59+
"place",
5760
"put",
5861
"take",
5962
"tril_indices",
@@ -267,6 +270,30 @@ def nonzero(a):
267270
return call_origin(numpy.nonzero, a)
268271

269272

273+
def place(arr, mask, vals):
274+
"""
275+
Change elements of an array based on conditional and input values.
276+
For full documentation refer to :obj:`numpy.place`.
277+
278+
Limitations
279+
-----------
280+
Input arrays ``arr`` and ``mask`` are supported as :obj:`dpnp.ndarray`.
281+
Parameter ``vals`` is supported as 1-D sequence.
282+
"""
283+
284+
if not use_origin_backend(arr):
285+
if not isinstance(arr, dparray):
286+
pass
287+
elif not isinstance(mask, dparray):
288+
pass
289+
elif not isinstance(vals, collections.Sequence):
290+
pass
291+
else:
292+
return dpnp_place(arr, mask, vals)
293+
294+
return call_origin(numpy.place, arr, mask, vals)
295+
296+
270297
def put(input, ind, v, mode='raise'):
271298
"""
272299
Replaces specified elements of an array with given values.

tests/skipped_tests.tbl

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -609,18 +609,6 @@ tests/third_party/cupy/indexing_tests/test_insert.py::TestPutmask::test_putmask_
609609
tests/third_party/cupy/indexing_tests/test_insert.py::TestPutmask::test_putmask_scalar_values
610610
tests/third_party/cupy/indexing_tests/test_insert.py::TestPutmaskDifferentDtypes::test_putmask_differnt_dtypes_mask
611611
tests/third_party/cupy/indexing_tests/test_insert.py::TestPutmaskDifferentDtypes::test_putmask_differnt_dtypes_raises
612-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_0_{n_vals=0, shape=(7,)}::test_place
613-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_1_{n_vals=0, shape=(2, 3)}::test_place
614-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_2_{n_vals=0, shape=(4, 3, 2)}::test_place
615-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_3_{n_vals=1, shape=(7,)}::test_place
616-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_4_{n_vals=1, shape=(2, 3)}::test_place
617-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_5_{n_vals=1, shape=(4, 3, 2)}::test_place
618-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_6_{n_vals=3, shape=(7,)}::test_place
619-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_7_{n_vals=3, shape=(2, 3)}::test_place
620-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_8_{n_vals=3, shape=(4, 3, 2)}::test_place
621-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_9_{n_vals=15, shape=(7,)}::test_place
622-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_10_{n_vals=15, shape=(2, 3)}::test_place
623-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_11_{n_vals=15, shape=(4, 3, 2)}::test_place
624612
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlaceRaises_param_0_{shape=(7,)}::test_place_empty_value_error
625613
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlaceRaises_param_0_{shape=(7,)}::test_place_shape_unmatch_error
626614
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlaceRaises_param_1_{shape=(2, 3)}::test_place_empty_value_error

tests/skipped_tests_gpu.tbl

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -750,18 +750,6 @@ tests/third_party/cupy/indexing_tests/test_insert.py::TestPutmask::test_putmask_
750750
tests/third_party/cupy/indexing_tests/test_insert.py::TestPutmask::test_putmask_scalar_values
751751
tests/third_party/cupy/indexing_tests/test_insert.py::TestPutmaskDifferentDtypes::test_putmask_differnt_dtypes_mask
752752
tests/third_party/cupy/indexing_tests/test_insert.py::TestPutmaskDifferentDtypes::test_putmask_differnt_dtypes_raises
753-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_0_{n_vals=0, shape=(7,)}::test_place
754-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_1_{n_vals=0, shape=(2, 3)}::test_place
755-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_2_{n_vals=0, shape=(4, 3, 2)}::test_place
756-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_3_{n_vals=1, shape=(7,)}::test_place
757-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_4_{n_vals=1, shape=(2, 3)}::test_place
758-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_5_{n_vals=1, shape=(4, 3, 2)}::test_place
759-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_6_{n_vals=3, shape=(7,)}::test_place
760-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_7_{n_vals=3, shape=(2, 3)}::test_place
761-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_8_{n_vals=3, shape=(4, 3, 2)}::test_place
762-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_9_{n_vals=15, shape=(7,)}::test_place
763-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_10_{n_vals=15, shape=(2, 3)}::test_place
764-
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlace_param_11_{n_vals=15, shape=(4, 3, 2)}::test_place
765753
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlaceRaises_param_0_{shape=(7,)}::test_place_empty_value_error
766754
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlaceRaises_param_0_{shape=(7,)}::test_place_shape_unmatch_error
767755
tests/third_party/cupy/indexing_tests/test_insert.py::TestPlaceRaises_param_1_{shape=(2, 3)}::test_place_empty_value_error

tests/test_indexing.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,81 @@ def test_fill_diagonal(array, val):
6060
numpy.testing.assert_array_equal(expected, result)
6161

6262

63+
@pytest.mark.parametrize("vals",
64+
[[100, 200],
65+
(100, 200)],
66+
ids=['[100, 200]',
67+
'(100, 200)'])
68+
@pytest.mark.parametrize("mask",
69+
[[[True, False], [False, True]],
70+
[[False, True], [True, False]],
71+
[[False, False], [True, True]]],
72+
ids=['[[True, False], [False, True]]',
73+
'[[False, True], [True, False]]',
74+
'[[False, False], [True, True]]'])
75+
@pytest.mark.parametrize("arr",
76+
[[[0, 0], [0, 0]],
77+
[[1, 2], [1, 2]],
78+
[[1, 2], [3, 4]]],
79+
ids=['[[0, 0], [0, 0]]',
80+
'[[1, 2], [1, 2]]',
81+
'[[1, 2], [3, 4]]'])
82+
def test_place1(arr, mask, vals):
83+
a = numpy.array(arr)
84+
ia = dpnp.array(a)
85+
m = numpy.array(mask)
86+
im = dpnp.array(m)
87+
numpy.place(a, m, vals)
88+
dpnp.place(ia, im, vals)
89+
numpy.testing.assert_array_equal(a, ia)
90+
91+
92+
@pytest.mark.parametrize("vals",
93+
[[100, 200],
94+
[100, 200, 300, 400, 500, 600],
95+
[100, 200, 300, 400, 500, 600, 800, 900]],
96+
ids=['[100, 200]',
97+
'[100, 200, 300, 400, 500, 600]',
98+
'[100, 200, 300, 400, 500, 600, 800, 900]'])
99+
@pytest.mark.parametrize("mask",
100+
[[[[True, False], [False, True]], [[False, True], [True, False]], [[False, False], [True, True]]]],
101+
ids=['[[[True, False], [False, True]], [[False, True], [True, False]], [[False, False], [True, True]]]'])
102+
@pytest.mark.parametrize("arr",
103+
[[[[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]]]],
104+
ids=['[[[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]]]'])
105+
def test_place2(arr, mask, vals):
106+
a = numpy.array(arr)
107+
ia = dpnp.array(a)
108+
m = numpy.array(mask)
109+
im = dpnp.array(m)
110+
numpy.place(a, m, vals)
111+
dpnp.place(ia, im, vals)
112+
numpy.testing.assert_array_equal(a, ia)
113+
114+
115+
@pytest.mark.parametrize("vals",
116+
[[100, 200],
117+
[100, 200, 300, 400, 500, 600],
118+
[100, 200, 300, 400, 500, 600, 800, 900]],
119+
ids=['[100, 200]',
120+
'[100, 200, 300, 400, 500, 600]',
121+
'[100, 200, 300, 400, 500, 600, 800, 900]'])
122+
@pytest.mark.parametrize("mask",
123+
[[[[[False, False], [True, True]], [[True, True], [True, True]]], [[[False, False], [True, True]], [[False, False], [False, False]]]]],
124+
ids=['[[[[False, False], [True, True]], [[True, True], [True, True]]], [[[False, False], [True, True]], [[False, False], [False, False]]]]'])
125+
@pytest.mark.parametrize("arr",
126+
[[[[[1, 2], [3, 4]], [[1, 2], [2, 1]]], [[[1, 3], [3, 1]], [[0, 1], [1, 3]]]]],
127+
ids=['[[[[1, 2], [3, 4]], [[1, 2], [2, 1]]], [[[1, 3], [3, 1]], [[0, 1], [1, 3]]]]'])
128+
def test_place3(arr, mask, vals):
129+
a = numpy.array(arr)
130+
ia = dpnp.array(a)
131+
m = numpy.array(mask)
132+
im = dpnp.array(m)
133+
numpy.place(a, m, vals)
134+
dpnp.place(ia, im, vals)
135+
numpy.testing.assert_array_equal(a, ia)
136+
137+
63138
@pytest.mark.parametrize("v",
64139
[0, 1, 2, 3, 4],
65140
ids=['0', '1', '2', '3', '4'])

0 commit comments

Comments
 (0)