Skip to content

Commit 7c4b39a

Browse files
npolina4antonwolfy
andauthored
Implement sparse and copy arguments for dpnp.mesgrid function (#1675)
* Implement sparse and copy arguments for dpnp.mesgrid function * address comments * Removed limitation block from th description * added tests --------- Co-authored-by: Anton Volkov <[email protected]> Co-authored-by: Anton <[email protected]>
1 parent 38a7ca8 commit 7c4b39a

File tree

5 files changed

+56
-77
lines changed

5 files changed

+56
-77
lines changed

dpnp/dpnp_container.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -271,18 +271,6 @@ def linspace(
271271
return dpnp_array(array_obj.shape, buffer=array_obj)
272272

273273

274-
def meshgrid(*xi, indexing="xy"):
275-
"""Creates list of `dpnp_array` coordinate matrices from vectors."""
276-
if len(xi) == 0:
277-
return []
278-
arrays = tuple(dpnp.get_usm_ndarray(x) for x in xi)
279-
arrays_obj = dpt.meshgrid(*arrays, indexing=indexing)
280-
return [
281-
dpnp_array._create_from_usm_ndarray(array_obj)
282-
for array_obj in arrays_obj
283-
]
284-
285-
286274
def ones(
287275
shape,
288276
*,

dpnp/dpnp_iface_arraycreation.py

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,12 +1394,28 @@ def meshgrid(*xi, copy=True, sparse=False, indexing="xy"):
13941394
13951395
For full documentation refer to :obj:`numpy.meshgrid`.
13961396
1397-
Limitations
1398-
-----------
1399-
Each array instance from `xi` is supported as either :class:`dpnp.dpnp.ndarray` or :class:`dpctl.tensor.usm_ndarray`.
1400-
Parameter `copy` is supported only with default value ``True``.
1401-
Parameter `sparse` is supported only with default value ``False``.
1402-
Otherwise the function will be executed sequentially on CPU.
1397+
Parameters
1398+
----------
1399+
x1, x2,..., xn : {dpnp.ndarray, usm_ndarray}
1400+
1-D arrays representing the coordinates of a grid.
1401+
indexing : {'xy', 'ij'}, optional
1402+
Cartesian ('xy', default) or matrix ('ij') indexing of output.
1403+
sparse : bool, optional
1404+
If True the shape of the returned coordinate array for dimension `i`
1405+
is reduced from ``(N1, ..., Ni, ... Nn)`` to
1406+
``(1, ..., 1, Ni, 1, ..., 1)``. Default is False.
1407+
copy : bool, optional
1408+
If False, a view into the original arrays are returned in order to
1409+
conserve memory. Default is True.
1410+
1411+
Returns
1412+
-------
1413+
X1, X2,..., XN : tuple of dpnp.ndarrays
1414+
For vectors `x1`, `x2`,..., `xn` with lengths ``Ni=len(xi)``,
1415+
returns ``(N1, N2, N3,..., Nn)`` shaped arrays if indexing='ij'
1416+
or ``(N2, N1, N3,..., Nn)`` shaped arrays if indexing='xy'
1417+
with the elements of `xi` repeated to fill the matrix along
1418+
the first dimension for `x1`, the second for `x2` and so on.
14031419
14041420
Examples
14051421
--------
@@ -1433,18 +1449,32 @@ def meshgrid(*xi, copy=True, sparse=False, indexing="xy"):
14331449
14341450
"""
14351451

1436-
if not all((isinstance(x, (dpnp.ndarray, dpt.usm_ndarray)) for x in xi)):
1437-
pass
1438-
elif indexing not in ["ij", "xy"]:
1439-
pass
1440-
elif copy is not True:
1441-
pass
1442-
elif sparse is not False:
1443-
pass
1444-
else:
1445-
return dpnp_container.meshgrid(*xi, indexing=indexing)
1452+
if not dpnp.check_supported_arrays_type(*xi):
1453+
raise TypeError("Each input array must be any of supported type")
1454+
1455+
ndim = len(xi)
1456+
1457+
if indexing not in ["xy", "ij"]:
1458+
raise ValueError(
1459+
"Unrecognized indexing keyword value, expecting 'xy' or 'ij'."
1460+
)
1461+
1462+
s0 = (1,) * ndim
1463+
output = [
1464+
dpnp.reshape(x, s0[:i] + (-1,) + s0[i + 1 :]) for i, x in enumerate(xi)
1465+
]
1466+
1467+
if indexing == "xy" and ndim > 1:
1468+
output[0] = output[0].reshape((1, -1) + s0[2:])
1469+
output[1] = output[1].reshape((-1, 1) + s0[2:])
1470+
1471+
if not sparse:
1472+
output = dpnp.broadcast_arrays(*output)
1473+
1474+
if copy:
1475+
output = [x.copy() for x in output]
14461476

1447-
return call_origin(numpy.meshgrid, xi, copy, sparse, indexing)
1477+
return output
14481478

14491479

14501480
class MGridClass:

tests/skipped_tests.tbl

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -151,30 +151,6 @@ tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_ones_like_s
151151
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_zeros_like_subok
152152
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_zeros_strides
153153

154-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_0_{copy=False, indexing='xy', sparse=False}::test_meshgrid0
155-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_0_{copy=False, indexing='xy', sparse=False}::test_meshgrid1
156-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_0_{copy=False, indexing='xy', sparse=False}::test_meshgrid2
157-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_0_{copy=False, indexing='xy', sparse=False}::test_meshgrid3
158-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_1_{copy=False, indexing='xy', sparse=True}::test_meshgrid0
159-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_1_{copy=False, indexing='xy', sparse=True}::test_meshgrid1
160-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_1_{copy=False, indexing='xy', sparse=True}::test_meshgrid2
161-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_1_{copy=False, indexing='xy', sparse=True}::test_meshgrid3
162-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_2_{copy=False, indexing='ij', sparse=False}::test_meshgrid0
163-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_2_{copy=False, indexing='ij', sparse=False}::test_meshgrid1
164-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_2_{copy=False, indexing='ij', sparse=False}::test_meshgrid2
165-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_2_{copy=False, indexing='ij', sparse=False}::test_meshgrid3
166-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_3_{copy=False, indexing='ij', sparse=True}::test_meshgrid0
167-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_3_{copy=False, indexing='ij', sparse=True}::test_meshgrid1
168-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_3_{copy=False, indexing='ij', sparse=True}::test_meshgrid2
169-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_3_{copy=False, indexing='ij', sparse=True}::test_meshgrid3
170-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_5_{copy=True, indexing='xy', sparse=True}::test_meshgrid0
171-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_5_{copy=True, indexing='xy', sparse=True}::test_meshgrid1
172-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_5_{copy=True, indexing='xy', sparse=True}::test_meshgrid2
173-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_5_{copy=True, indexing='xy', sparse=True}::test_meshgrid3
174-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid0
175-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid1
176-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid2
177-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid3
178154
tests/third_party/cupy/indexing_tests/test_generate.py::TestAxisConcatenator::test_AxisConcatenator_init1
179155
tests/third_party/cupy/indexing_tests/test_generate.py::TestAxisConcatenator::test_len
180156
tests/third_party/cupy/indexing_tests/test_generate.py::TestC_::test_c_1

tests/skipped_tests_gpu.tbl

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -230,30 +230,6 @@ tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_ones_like_s
230230
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_zeros_like_subok
231231
tests/third_party/cupy/creation_tests/test_basic.py::TestBasic::test_zeros_strides
232232

233-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_0_{copy=False, indexing='xy', sparse=False}::test_meshgrid0
234-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_0_{copy=False, indexing='xy', sparse=False}::test_meshgrid1
235-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_0_{copy=False, indexing='xy', sparse=False}::test_meshgrid2
236-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_0_{copy=False, indexing='xy', sparse=False}::test_meshgrid3
237-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_1_{copy=False, indexing='xy', sparse=True}::test_meshgrid0
238-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_1_{copy=False, indexing='xy', sparse=True}::test_meshgrid1
239-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_1_{copy=False, indexing='xy', sparse=True}::test_meshgrid2
240-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_1_{copy=False, indexing='xy', sparse=True}::test_meshgrid3
241-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_2_{copy=False, indexing='ij', sparse=False}::test_meshgrid0
242-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_2_{copy=False, indexing='ij', sparse=False}::test_meshgrid1
243-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_2_{copy=False, indexing='ij', sparse=False}::test_meshgrid2
244-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_2_{copy=False, indexing='ij', sparse=False}::test_meshgrid3
245-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_3_{copy=False, indexing='ij', sparse=True}::test_meshgrid0
246-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_3_{copy=False, indexing='ij', sparse=True}::test_meshgrid1
247-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_3_{copy=False, indexing='ij', sparse=True}::test_meshgrid2
248-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_3_{copy=False, indexing='ij', sparse=True}::test_meshgrid3
249-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_5_{copy=True, indexing='xy', sparse=True}::test_meshgrid0
250-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_5_{copy=True, indexing='xy', sparse=True}::test_meshgrid1
251-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_5_{copy=True, indexing='xy', sparse=True}::test_meshgrid2
252-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_5_{copy=True, indexing='xy', sparse=True}::test_meshgrid3
253-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid0
254-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid1
255-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid2
256-
tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid3
257233
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange_negative_size
258234
tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange_no_dtype_int
259235

tests/test_arraycreation.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,3 +878,12 @@ def test_logspace_axis(axis):
878878
[2, 3], [20, 15], num=2, base=[[1, 3], [5, 7]], axis=axis
879879
)
880880
assert_dtype_allclose(func(dpnp), func(numpy))
881+
882+
883+
def test_meshgrid_raise_error():
884+
a = numpy.array([1, 2, 3, 4])
885+
with pytest.raises(TypeError):
886+
dpnp.meshgrid(a)
887+
b = dpnp.array([1, 2, 3, 4])
888+
with pytest.raises(ValueError):
889+
dpnp.meshgrid(b, indexing="ab")

0 commit comments

Comments
 (0)