Skip to content

Commit 8b38fd6

Browse files
committed
improve coverage dpnp_iface_manipulation.py
1 parent 264c6d8 commit 8b38fd6

File tree

3 files changed

+187
-188
lines changed

3 files changed

+187
-188
lines changed

dpnp/dpnp_iface_manipulation.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3894,18 +3894,13 @@ def transpose(a, axes=None):
38943894
38953895
"""
38963896

3897-
if isinstance(a, dpnp_array):
3898-
array = a
3899-
elif isinstance(a, dpt.usm_ndarray):
3900-
array = dpnp_array._create_from_usm_ndarray(a)
3901-
else:
3902-
raise TypeError(
3903-
f"An array must be any of supported type, but got {type(a)}"
3904-
)
3897+
dpnp.check_supported_arrays_type(a)
3898+
if isinstance(a, dpt.usm_ndarray):
3899+
a = dpnp_array._create_from_usm_ndarray(a)
39053900

39063901
if axes is None:
3907-
return array.transpose()
3908-
return array.transpose(*axes)
3902+
return a.transpose()
3903+
return a.transpose(*axes)
39093904

39103905

39113906
permute_dims = transpose # permute_dims is an alias for transpose

dpnp/tests/test_arraymanipulation.py

Lines changed: 171 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,35 @@
1515
from .third_party.cupy import testing
1616

1717

18+
class TestAsfarray:
19+
@testing.with_requires("numpy<2.0")
20+
@pytest.mark.parametrize("dtype", get_all_dtypes())
21+
@pytest.mark.parametrize(
22+
"data", [[1, 2, 3], [1.0, 2.0, 3.0]], ids=["[1, 2, 3]", "[1., 2., 3.]"]
23+
)
24+
def test_asfarray1(self, dtype, data):
25+
expected = numpy.asfarray(data, dtype)
26+
result = dpnp.asfarray(data, dtype)
27+
assert_array_equal(result, expected)
28+
29+
@testing.with_requires("numpy<2.0")
30+
@pytest.mark.usefixtures("suppress_complex_warning")
31+
@pytest.mark.parametrize("dtype", get_all_dtypes())
32+
@pytest.mark.parametrize("data", [[1.0, 2.0, 3.0]], ids=["[1., 2., 3.]"])
33+
@pytest.mark.parametrize("data_dtype", get_all_dtypes(no_none=True))
34+
def test_asfarray2(self, dtype, data, data_dtype):
35+
expected = numpy.asfarray(numpy.array(data, dtype=data_dtype), dtype)
36+
result = dpnp.asfarray(dpnp.array(data, dtype=data_dtype), dtype)
37+
assert_array_equal(result, expected)
38+
39+
# This is only for coverage with NumPy 2.0 and above
40+
def test_asfarray_coverage(self):
41+
expected = [1.0, 2.0, 3.0]
42+
result = dpnp.asfarray([1, 2, 3])
43+
44+
assert_array_equal(result, expected)
45+
46+
1847
class TestAtleast1d:
1948
def test_0D_array(self):
2049
a = dpnp.array(1)
@@ -132,6 +161,148 @@ def test_dpnp_dpt_array(self):
132161
assert_array_equal(res, desired)
133162

134163

164+
class TestBroadcastArray:
165+
def assert_broadcast_correct(self, input_shapes):
166+
np_arrays = [numpy.zeros(s, dtype="i1") for s in input_shapes]
167+
out_np_arrays = numpy.broadcast_arrays(*np_arrays)
168+
dpnp_arrays = [dpnp.asarray(Xnp) for Xnp in np_arrays]
169+
out_dpnp_arrays = dpnp.broadcast_arrays(*dpnp_arrays)
170+
for Xnp, X in zip(out_np_arrays, out_dpnp_arrays):
171+
assert_array_equal(
172+
Xnp, dpnp.asnumpy(X), err_msg=f"Failed for {input_shapes})"
173+
)
174+
175+
def assert_broadcast_arrays_raise(self, input_shapes):
176+
dpnp_arrays = [dpnp.asarray(numpy.zeros(s)) for s in input_shapes]
177+
pytest.raises(ValueError, dpnp.broadcast_arrays, *dpnp_arrays)
178+
179+
def test_broadcast_arrays_same(self):
180+
Xnp = numpy.arange(10)
181+
Ynp = numpy.arange(10)
182+
res_Xnp, res_Ynp = numpy.broadcast_arrays(Xnp, Ynp)
183+
X = dpnp.asarray(Xnp)
184+
Y = dpnp.asarray(Ynp)
185+
res_X, res_Y = dpnp.broadcast_arrays(X, Y)
186+
assert_array_equal(res_Xnp, dpnp.asnumpy(res_X))
187+
assert_array_equal(res_Ynp, dpnp.asnumpy(res_Y))
188+
189+
def test_broadcast_arrays_one_off(self):
190+
Xnp = numpy.array([[1, 2, 3]])
191+
Ynp = numpy.array([[1], [2], [3]])
192+
res_Xnp, res_Ynp = numpy.broadcast_arrays(Xnp, Ynp)
193+
X = dpnp.asarray(Xnp)
194+
Y = dpnp.asarray(Ynp)
195+
res_X, res_Y = dpnp.broadcast_arrays(X, Y)
196+
assert_array_equal(res_Xnp, dpnp.asnumpy(res_X))
197+
assert_array_equal(res_Ynp, dpnp.asnumpy(res_Y))
198+
199+
@pytest.mark.parametrize(
200+
"shapes",
201+
[
202+
(),
203+
(1,),
204+
(3,),
205+
(0, 1),
206+
(0, 3),
207+
(1, 0),
208+
(3, 0),
209+
(1, 3),
210+
(3, 1),
211+
(3, 3),
212+
],
213+
)
214+
def test_broadcast_arrays_same_shapes(self, shapes):
215+
for shape in shapes:
216+
single_input_shapes = [shape]
217+
self.assert_broadcast_correct(single_input_shapes)
218+
double_input_shapes = [shape, shape]
219+
self.assert_broadcast_correct(double_input_shapes)
220+
triple_input_shapes = [shape, shape, shape]
221+
self.assert_broadcast_correct(triple_input_shapes)
222+
223+
@pytest.mark.parametrize(
224+
"shapes",
225+
[
226+
[[(1,), (3,)]],
227+
[[(1, 3), (3, 3)]],
228+
[[(3, 1), (3, 3)]],
229+
[[(1, 3), (3, 1)]],
230+
[[(1, 1), (3, 3)]],
231+
[[(1, 1), (1, 3)]],
232+
[[(1, 1), (3, 1)]],
233+
[[(1, 0), (0, 0)]],
234+
[[(0, 1), (0, 0)]],
235+
[[(1, 0), (0, 1)]],
236+
[[(1, 1), (0, 0)]],
237+
[[(1, 1), (1, 0)]],
238+
[[(1, 1), (0, 1)]],
239+
],
240+
)
241+
def test_broadcast_arrays_same_len_shapes(self, shapes):
242+
# Check that two different input shapes of the same length, but some have
243+
# ones, broadcast to the correct shape.
244+
for input_shapes in shapes:
245+
self.assert_broadcast_correct(input_shapes)
246+
self.assert_broadcast_correct(input_shapes[::-1])
247+
248+
@pytest.mark.parametrize(
249+
"shapes",
250+
[
251+
[[(), (3,)]],
252+
[[(3,), (3, 3)]],
253+
[[(3,), (3, 1)]],
254+
[[(1,), (3, 3)]],
255+
[[(), (3, 3)]],
256+
[[(1, 1), (3,)]],
257+
[[(1,), (3, 1)]],
258+
[[(1,), (1, 3)]],
259+
[[(), (1, 3)]],
260+
[[(), (3, 1)]],
261+
[[(), (0,)]],
262+
[[(0,), (0, 0)]],
263+
[[(0,), (0, 1)]],
264+
[[(1,), (0, 0)]],
265+
[[(), (0, 0)]],
266+
[[(1, 1), (0,)]],
267+
[[(1,), (0, 1)]],
268+
[[(1,), (1, 0)]],
269+
[[(), (1, 0)]],
270+
[[(), (0, 1)]],
271+
],
272+
)
273+
def test_broadcast_arrays_different_len_shapes(self, shapes):
274+
# Check that two different input shapes (of different lengths) broadcast
275+
# to the correct shape.
276+
for input_shapes in shapes:
277+
self.assert_broadcast_correct(input_shapes)
278+
self.assert_broadcast_correct(input_shapes[::-1])
279+
280+
@pytest.mark.parametrize(
281+
"shapes",
282+
[
283+
[[(3,), (4,)]],
284+
[[(2, 3), (2,)]],
285+
[[(3,), (3,), (4,)]],
286+
[[(1, 3, 4), (2, 3, 3)]],
287+
],
288+
)
289+
def test_incompatible_shapes_raise_valueerror(self, shapes):
290+
for input_shapes in shapes:
291+
self.assert_broadcast_arrays_raise(input_shapes)
292+
self.assert_broadcast_arrays_raise(input_shapes[::-1])
293+
294+
def test_broadcast_arrays_empty_input(self):
295+
assert dpnp.broadcast_arrays() == []
296+
297+
def test_subok_error(self):
298+
x = dpnp.ones((4))
299+
with pytest.raises(NotImplementedError):
300+
dpnp.broadcast_arrays(x, subok=True)
301+
302+
with pytest.raises(NotImplementedError):
303+
dpnp.broadcast_to(x, (4, 4), subok=True)
304+
305+
135306
class TestColumnStack:
136307
def test_non_iterable(self):
137308
with pytest.raises(TypeError):
@@ -987,180 +1158,6 @@ def test_generator(self):
9871158
dpnp.vstack(map(lambda x: x, dpnp.ones((3, 2))))
9881159

9891160

990-
@testing.with_requires("numpy<2.0")
991-
@pytest.mark.parametrize("dtype", get_all_dtypes())
992-
@pytest.mark.parametrize(
993-
"data", [[1, 2, 3], [1.0, 2.0, 3.0]], ids=["[1, 2, 3]", "[1., 2., 3.]"]
994-
)
995-
def test_asfarray(dtype, data):
996-
expected = numpy.asfarray(data, dtype)
997-
result = dpnp.asfarray(data, dtype)
998-
999-
assert_array_equal(result, expected)
1000-
1001-
1002-
@testing.with_requires("numpy<2.0")
1003-
@pytest.mark.usefixtures("suppress_complex_warning")
1004-
@pytest.mark.parametrize("dtype", get_all_dtypes())
1005-
@pytest.mark.parametrize("data", [[1.0, 2.0, 3.0]], ids=["[1., 2., 3.]"])
1006-
@pytest.mark.parametrize("data_dtype", get_all_dtypes(no_none=True))
1007-
def test_asfarray2(dtype, data, data_dtype):
1008-
expected = numpy.asfarray(numpy.array(data, dtype=data_dtype), dtype)
1009-
result = dpnp.asfarray(dpnp.array(data, dtype=data_dtype), dtype)
1010-
1011-
assert_array_equal(result, expected)
1012-
1013-
1014-
def assert_broadcast_correct(input_shapes):
1015-
np_arrays = [numpy.zeros(s, dtype="i1") for s in input_shapes]
1016-
out_np_arrays = numpy.broadcast_arrays(*np_arrays)
1017-
dpnp_arrays = [dpnp.asarray(Xnp) for Xnp in np_arrays]
1018-
out_dpnp_arrays = dpnp.broadcast_arrays(*dpnp_arrays)
1019-
for Xnp, X in zip(out_np_arrays, out_dpnp_arrays):
1020-
assert_array_equal(
1021-
Xnp, dpnp.asnumpy(X), err_msg=f"Failed for {input_shapes})"
1022-
)
1023-
1024-
1025-
def assert_broadcast_arrays_raise(input_shapes):
1026-
dpnp_arrays = [dpnp.asarray(numpy.zeros(s)) for s in input_shapes]
1027-
pytest.raises(ValueError, dpnp.broadcast_arrays, *dpnp_arrays)
1028-
1029-
1030-
def test_broadcast_arrays_same():
1031-
Xnp = numpy.arange(10)
1032-
Ynp = numpy.arange(10)
1033-
res_Xnp, res_Ynp = numpy.broadcast_arrays(Xnp, Ynp)
1034-
X = dpnp.asarray(Xnp)
1035-
Y = dpnp.asarray(Ynp)
1036-
res_X, res_Y = dpnp.broadcast_arrays(X, Y)
1037-
assert_array_equal(res_Xnp, dpnp.asnumpy(res_X))
1038-
assert_array_equal(res_Ynp, dpnp.asnumpy(res_Y))
1039-
1040-
1041-
def test_broadcast_arrays_one_off():
1042-
Xnp = numpy.array([[1, 2, 3]])
1043-
Ynp = numpy.array([[1], [2], [3]])
1044-
res_Xnp, res_Ynp = numpy.broadcast_arrays(Xnp, Ynp)
1045-
X = dpnp.asarray(Xnp)
1046-
Y = dpnp.asarray(Ynp)
1047-
res_X, res_Y = dpnp.broadcast_arrays(X, Y)
1048-
assert_array_equal(res_Xnp, dpnp.asnumpy(res_X))
1049-
assert_array_equal(res_Ynp, dpnp.asnumpy(res_Y))
1050-
1051-
1052-
@pytest.mark.parametrize(
1053-
"shapes",
1054-
[
1055-
(),
1056-
(1,),
1057-
(3,),
1058-
(0, 1),
1059-
(0, 3),
1060-
(1, 0),
1061-
(3, 0),
1062-
(1, 3),
1063-
(3, 1),
1064-
(3, 3),
1065-
],
1066-
)
1067-
def test_broadcast_arrays_same_shapes(shapes):
1068-
for shape in shapes:
1069-
single_input_shapes = [shape]
1070-
assert_broadcast_correct(single_input_shapes)
1071-
double_input_shapes = [shape, shape]
1072-
assert_broadcast_correct(double_input_shapes)
1073-
triple_input_shapes = [shape, shape, shape]
1074-
assert_broadcast_correct(triple_input_shapes)
1075-
1076-
1077-
@pytest.mark.parametrize(
1078-
"shapes",
1079-
[
1080-
[[(1,), (3,)]],
1081-
[[(1, 3), (3, 3)]],
1082-
[[(3, 1), (3, 3)]],
1083-
[[(1, 3), (3, 1)]],
1084-
[[(1, 1), (3, 3)]],
1085-
[[(1, 1), (1, 3)]],
1086-
[[(1, 1), (3, 1)]],
1087-
[[(1, 0), (0, 0)]],
1088-
[[(0, 1), (0, 0)]],
1089-
[[(1, 0), (0, 1)]],
1090-
[[(1, 1), (0, 0)]],
1091-
[[(1, 1), (1, 0)]],
1092-
[[(1, 1), (0, 1)]],
1093-
],
1094-
)
1095-
def test_broadcast_arrays_same_len_shapes(shapes):
1096-
# Check that two different input shapes of the same length, but some have
1097-
# ones, broadcast to the correct shape.
1098-
1099-
for input_shapes in shapes:
1100-
assert_broadcast_correct(input_shapes)
1101-
assert_broadcast_correct(input_shapes[::-1])
1102-
1103-
1104-
@pytest.mark.parametrize(
1105-
"shapes",
1106-
[
1107-
[[(), (3,)]],
1108-
[[(3,), (3, 3)]],
1109-
[[(3,), (3, 1)]],
1110-
[[(1,), (3, 3)]],
1111-
[[(), (3, 3)]],
1112-
[[(1, 1), (3,)]],
1113-
[[(1,), (3, 1)]],
1114-
[[(1,), (1, 3)]],
1115-
[[(), (1, 3)]],
1116-
[[(), (3, 1)]],
1117-
[[(), (0,)]],
1118-
[[(0,), (0, 0)]],
1119-
[[(0,), (0, 1)]],
1120-
[[(1,), (0, 0)]],
1121-
[[(), (0, 0)]],
1122-
[[(1, 1), (0,)]],
1123-
[[(1,), (0, 1)]],
1124-
[[(1,), (1, 0)]],
1125-
[[(), (1, 0)]],
1126-
[[(), (0, 1)]],
1127-
],
1128-
)
1129-
def test_broadcast_arrays_different_len_shapes(shapes):
1130-
# Check that two different input shapes (of different lengths) broadcast
1131-
# to the correct shape.
1132-
1133-
for input_shapes in shapes:
1134-
assert_broadcast_correct(input_shapes)
1135-
assert_broadcast_correct(input_shapes[::-1])
1136-
1137-
1138-
@pytest.mark.parametrize(
1139-
"shapes",
1140-
[
1141-
[[(3,), (4,)]],
1142-
[[(2, 3), (2,)]],
1143-
[[(3,), (3,), (4,)]],
1144-
[[(1, 3, 4), (2, 3, 3)]],
1145-
],
1146-
)
1147-
def test_incompatible_shapes_raise_valueerror(shapes):
1148-
for input_shapes in shapes:
1149-
assert_broadcast_arrays_raise(input_shapes)
1150-
assert_broadcast_arrays_raise(input_shapes[::-1])
1151-
1152-
1153-
def test_broadcast_arrays_empty_input():
1154-
assert dpnp.broadcast_arrays() == []
1155-
1156-
1157-
def test_subok_error():
1158-
x = dpnp.ones((4))
1159-
with pytest.raises(NotImplementedError):
1160-
dpnp.broadcast_arrays(x, subok=True)
1161-
dpnp.broadcast_to(x, (4, 4), subok=True)
1162-
1163-
11641161
def test_can_cast():
11651162
X = dpnp.ones((2, 2), dtype=dpnp.int64)
11661163
pytest.raises(TypeError, dpnp.can_cast, X, 1)

0 commit comments

Comments
 (0)