Skip to content

Commit 7b96b9b

Browse files
npolina4antonwolfy
andauthored
Updated dpnp.vstack function (#1595)
* Update dpnp.vstack function. * Returned skip for broadcast_arrays tests * address comments --------- Co-authored-by: Anton <[email protected]>
1 parent 4850e53 commit 7b96b9b

File tree

5 files changed

+58
-37
lines changed

5 files changed

+58
-37
lines changed

dpnp/dpnp_iface_manipulation.py

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,20 +1548,61 @@ def unique(ar, **kwargs):
15481548
return call_origin(numpy.unique, ar, **kwargs)
15491549

15501550

1551-
def vstack(tup):
1551+
def vstack(tup, *, dtype=None, casting="same_kind"):
15521552
"""
15531553
Stack arrays in sequence vertically (row wise).
15541554
15551555
For full documentation refer to :obj:`numpy.vstack`.
15561556
1557-
"""
1557+
Returns
1558+
-------
1559+
out : dpnp.ndarray
1560+
The array formed by stacking the given arrays, will be at least 2-D.
1561+
1562+
Limitations
1563+
-----------
1564+
Each array in `tup` is supported as either :class:`dpnp.ndarray`
1565+
or :class:`dpctl.tensor.usm_ndarray`. Otherwise ``TypeError`` exception
1566+
will be raised.
1567+
Parameters `dtype` and `casting` are supported with default value.
1568+
Otherwise the function will be executed sequentially on CPU.
15581569
1559-
# TODO:
1560-
# `call_origin` cannot convert sequence of array to sequence of
1561-
# nparray
1562-
tup_new = []
1563-
for tp in tup:
1564-
tpx = dpnp.asnumpy(tp) if not isinstance(tp, numpy.ndarray) else tp
1565-
tup_new.append(tpx)
1570+
See Also
1571+
--------
1572+
:obj:`dpnp.concatenate` : Join a sequence of arrays along an existing axis.
1573+
:obj:`dpnp.stack` : Join a sequence of arrays along a new axis.
1574+
:obj:`dpnp.hstack` : Stack arrays in sequence horizontally (column wise).
1575+
:obj:`dpnp.dstack` : Stack arrays in sequence depth wise (along third axis).
1576+
:obj:`dpnp.column_stack` : Stack 1-D arrays as columns into a 2-D array.
1577+
:obj:`dpnp.block` : Assemble an nd-array from nested lists of blocks.
1578+
:obj:`dpnp.split` : Split array into a list of multiple sub-arrays of equal size.
15661579
1567-
return call_origin(numpy.vstack, tup_new)
1580+
Examples
1581+
--------
1582+
>>> import dpnp as np
1583+
>>> a = np.array([1, 2, 3])
1584+
>>> b = np.array([4, 5, 6])
1585+
>>> np.vstack((a, b))
1586+
array([[1, 2, 3],
1587+
[4, 5, 6]])
1588+
1589+
>>> a = np.array([[1], [2], [3]])
1590+
>>> b = np.array([[4], [5], [6]])
1591+
>>> np.vstack((a, b))
1592+
array([[1],
1593+
[2],
1594+
[3],
1595+
[4],
1596+
[5],
1597+
[6]])
1598+
1599+
"""
1600+
1601+
if not hasattr(tup, "__getitem__"):
1602+
raise TypeError(
1603+
"Arrays to stack must be passed as a sequence type such as list or tuple."
1604+
)
1605+
arrs = dpnp.atleast_2d(*tup)
1606+
if not isinstance(arrs, list):
1607+
arrs = [arrs]
1608+
return dpnp.concatenate(arrs, axis=0, dtype=dtype, casting=casting)

tests/skipped_tests.tbl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ tests/third_party/cupy/fft_tests/test_fft.py::TestFftn_param_23_{axes=None, norm
3434

3535
tests/third_party/intel/test_zero_copy_test1.py::test_dpnp_interaction_with_dpctl_memory
3636

37-
tests/test_arraymanipulation.py::TestVstack::test_generator
38-
3937
tests/test_linalg.py::test_cond[-1-[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]
4038
tests/test_linalg.py::test_cond[1-[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]
4139
tests/test_linalg.py::test_cond[-2-[[1, 0, -1], [0, 1, 0], [1, 0, 1]]]
@@ -450,7 +448,6 @@ tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_8_{s
450448
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_8_{shapes=[(2, 0, 1, 1, 3), (2, 1, 0, 0, 3)]}::test_broadcast_arrays
451449
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_9_{shapes=[(0, 1, 1, 3), (2, 1, 0, 0, 3)]}::test_broadcast
452450
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_9_{shapes=[(0, 1, 1, 3), (2, 1, 0, 0, 3)]}::test_broadcast_arrays
453-
454451
tests/third_party/cupy/manipulation_tests/test_dims.py::TestInvalidBroadcast_param_0_{shapes=[(3,), (2,)]}::test_invalid_broadcast
455452
tests/third_party/cupy/manipulation_tests/test_dims.py::TestInvalidBroadcast_param_0_{shapes=[(3,), (2,)]}::test_invalid_broadcast_arrays
456453
tests/third_party/cupy/manipulation_tests/test_dims.py::TestInvalidBroadcast_param_1_{shapes=[(3, 2), (2, 3)]}::test_invalid_broadcast
@@ -459,6 +456,7 @@ tests/third_party/cupy/manipulation_tests/test_dims.py::TestInvalidBroadcast_par
459456
tests/third_party/cupy/manipulation_tests/test_dims.py::TestInvalidBroadcast_param_2_{shapes=[(3, 2), (3, 4)]}::test_invalid_broadcast_arrays
460457
tests/third_party/cupy/manipulation_tests/test_dims.py::TestInvalidBroadcast_param_3_{shapes=[(0,), (2,)]}::test_invalid_broadcast
461458
tests/third_party/cupy/manipulation_tests/test_dims.py::TestInvalidBroadcast_param_3_{shapes=[(0,), (2,)]}::test_invalid_broadcast_arrays
459+
462460
tests/third_party/cupy/manipulation_tests/test_shape.py::TestRavel::test_ravel2
463461
tests/third_party/cupy/manipulation_tests/test_shape.py::TestRavel::test_ravel3
464462
tests/third_party/cupy/manipulation_tests/test_shape.py::TestRavel::test_external_ravel

tests/skipped_tests_gpu.tbl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsMult
210210
tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsMultivariateNormal_param_3_{d=4, shape=(3, 2)}::test_normal
211211

212212
tests/third_party/intel/test_zero_copy_test1.py::test_dpnp_interaction_with_dpctl_memory
213-
tests/test_arraymanipulation.py::TestVstack::test_generator
214213

215214
tests/test_linalg.py::test_cond[-1-[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]
216215
tests/test_linalg.py::test_cond[1-[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]

tests/test_arraymanipulation.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -583,35 +583,30 @@ class TestVstack:
583583
def test_non_iterable(self):
584584
assert_raises(TypeError, dpnp.vstack, 1)
585585

586-
@pytest.mark.usefixtures("allow_fall_back_on_numpy")
587586
def test_empty_input(self):
588-
assert_raises(ValueError, dpnp.vstack, ())
587+
assert_raises(TypeError, dpnp.vstack, ())
589588

590-
@pytest.mark.usefixtures("allow_fall_back_on_numpy")
591589
def test_0D_array(self):
592590
a = dpnp.array(1)
593591
b = dpnp.array(2)
594592
res = dpnp.vstack([a, b])
595593
desired = dpnp.array([[1], [2]])
596594
assert_array_equal(res, desired)
597595

598-
@pytest.mark.usefixtures("allow_fall_back_on_numpy")
599596
def test_1D_array(self):
600597
a = dpnp.array([1])
601598
b = dpnp.array([2])
602599
res = dpnp.vstack([a, b])
603600
desired = dpnp.array([[1], [2]])
604601
assert_array_equal(res, desired)
605602

606-
@pytest.mark.usefixtures("allow_fall_back_on_numpy")
607603
def test_2D_array(self):
608604
a = dpnp.array([[1], [2]])
609605
b = dpnp.array([[1], [2]])
610606
res = dpnp.vstack([a, b])
611607
desired = dpnp.array([[1], [2], [1], [2]])
612608
assert_array_equal(res, desired)
613609

614-
@pytest.mark.usefixtures("allow_fall_back_on_numpy")
615610
def test_2D_array2(self):
616611
a = dpnp.array([1, 2])
617612
b = dpnp.array([1, 2])
@@ -620,8 +615,8 @@ def test_2D_array2(self):
620615
assert_array_equal(res, desired)
621616

622617
def test_generator(self):
623-
with assert_warns(FutureWarning):
624-
dpnp.vstack((numpy.arange(3) for _ in range(2)))
618+
with pytest.raises(TypeError):
619+
dpnp.vstack((dpnp.arange(3) for _ in range(2)))
625620

626621

627622
class TestAtleast1d:

tests/third_party/cupy/manipulation_tests/test_join.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -312,27 +312,24 @@ def test_hstack_casting(self, xp, dtype1, dtype2, casting):
312312
# may raise TypeError or ComplexWarning
313313
return xp.hstack((a, b), dtype=dtype2, casting=casting)
314314

315-
@pytest.mark.skip("dpnp.vstack() is not implemented yet")
316315
@testing.numpy_cupy_array_equal()
317316
def test_vstack_vectors(self, xp):
318317
a = xp.arange(3)
319318
b = xp.arange(2, -1, -1)
320319
return xp.vstack((a, b))
321320

322-
@pytest.mark.skip("dpnp.vstack() is not implemented yet")
323321
@testing.numpy_cupy_array_equal()
324322
def test_vstack_single_element(self, xp):
325323
a = xp.arange(3)
326324
return xp.vstack((a,))
327325

328-
@pytest.mark.skip("dpnp.vstack() is not implemented yet")
329326
def test_vstack_wrong_ndim(self):
330327
a = cupy.empty((3,))
331328
b = cupy.empty((3, 1))
332329
with pytest.raises(ValueError):
333330
cupy.vstack((a, b))
334331

335-
@pytest.mark.skip("dpnp.vstack() is not implemented yet")
332+
@pytest.mark.usefixtures("allow_fall_back_on_numpy")
336333
@testing.with_requires("numpy>=1.24.0")
337334
@testing.for_all_dtypes_combination(names=["dtype1", "dtype2"])
338335
@testing.numpy_cupy_array_equal(accept_error=TypeError)
@@ -341,18 +338,9 @@ def test_vstack_dtype(self, xp, dtype1, dtype2):
341338
b = testing.shaped_arange((3, 4), xp, dtype1)
342339
return xp.vstack((a, b), dtype=dtype2)
343340

344-
@pytest.mark.skip("dpnp.vstack() is not implemented yet")
341+
@pytest.mark.usefixtures("allow_fall_back_on_numpy")
345342
@testing.with_requires("numpy>=1.24.0")
346-
@pytest.mark.parametrize(
347-
"casting",
348-
[
349-
"no",
350-
"equiv",
351-
"safe",
352-
"same_kind",
353-
"unsafe",
354-
],
355-
)
343+
@testing.for_castings()
356344
@testing.for_all_dtypes_combination(names=["dtype1", "dtype2"])
357345
@testing.numpy_cupy_array_equal(
358346
accept_error=(TypeError, numpy.ComplexWarning)

0 commit comments

Comments
 (0)