Skip to content

Commit 81b8f42

Browse files
committed
address comments
1 parent 9b548c2 commit 81b8f42

File tree

1 file changed

+36
-76
lines changed

1 file changed

+36
-76
lines changed

dpnp/tests/test_mathematical.py

Lines changed: 36 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
generate_random_numpy_array,
2424
get_abs_array,
2525
get_all_dtypes,
26+
get_array,
2627
get_complex_dtypes,
2728
get_float_complex_dtypes,
2829
get_float_dtypes,
@@ -648,12 +649,8 @@ def test_basic(self, array, dtype):
648649
)
649650
def test_to_begin(self, to_begin):
650651
a = numpy.array([1, 2, 4, 7, 0])
651-
ia = dpnp.array([1, 2, 4, 7, 0])
652-
653-
if isinstance(to_begin, dpnp.ndarray):
654-
np_to_begin = dpnp.asnumpy(to_begin)
655-
else:
656-
np_to_begin = to_begin
652+
ia = dpnp.array(a)
653+
np_to_begin = get_array(numpy, to_begin)
657654

658655
result = dpnp.ediff1d(ia, to_begin=to_begin)
659656
expected = numpy.ediff1d(a, to_begin=np_to_begin)
@@ -672,12 +669,8 @@ def test_to_begin(self, to_begin):
672669
)
673670
def test_to_end(self, to_end):
674671
a = numpy.array([1, 2, 4, 7, 0])
675-
ia = dpnp.array([1, 2, 4, 7, 0])
676-
677-
if isinstance(to_end, dpnp.ndarray):
678-
np_to_end = dpnp.asnumpy(to_end)
679-
else:
680-
np_to_end = to_end
672+
ia = dpnp.array(a)
673+
np_to_end = get_array(numpy, to_end)
681674

682675
result = dpnp.ediff1d(ia, to_end=to_end)
683676
expected = numpy.ediff1d(a, to_end=np_to_end)
@@ -696,17 +689,10 @@ def test_to_end(self, to_end):
696689
)
697690
def test_to_begin_to_end(self, to_begin, to_end):
698691
a = numpy.array([1, 2, 4, 7, 0])
699-
ia = dpnp.array([1, 2, 4, 7, 0])
700-
701-
if isinstance(to_begin, dpnp.ndarray):
702-
np_to_begin = dpnp.asnumpy(to_begin)
703-
else:
704-
np_to_begin = to_begin
692+
ia = dpnp.array(a)
705693

706-
if isinstance(to_end, dpnp.ndarray):
707-
np_to_end = dpnp.asnumpy(to_end)
708-
else:
709-
np_to_end = to_end
694+
np_to_begin = get_array(numpy, to_begin)
695+
np_to_end = get_array(numpy, to_end)
710696

711697
result = dpnp.ediff1d(ia, to_end=to_end, to_begin=to_begin)
712698
expected = numpy.ediff1d(a, to_end=np_to_end, to_begin=np_to_begin)
@@ -1790,7 +1776,7 @@ def get_numpy_func(self):
17901776
return numpy.trapz
17911777
return numpy.trapezoid
17921778

1793-
@pytest.mark.parametrize("dt", get_all_dtypes(no_bool=True))
1779+
@pytest.mark.parametrize("dt", get_all_dtypes(no_none=True, no_bool=True))
17941780
@pytest.mark.parametrize(
17951781
"data",
17961782
[[1, 2, 3], [[1, 2, 3], [4, 5, 6]], [1, 4, 6, 9, 10, 12], [], [1]],
@@ -1816,8 +1802,8 @@ def test_simple(self, dt):
18161802
)
18171803
assert_allclose(result, expected, rtol=1e-6)
18181804

1819-
@pytest.mark.parametrize("y_dt", get_all_dtypes(no_bool=True))
1820-
@pytest.mark.parametrize("x_dt", get_all_dtypes(no_bool=True))
1805+
@pytest.mark.parametrize("y_dt", get_all_dtypes(no_none=True, no_bool=True))
1806+
@pytest.mark.parametrize("x_dt", get_all_dtypes(no_none=True, no_bool=True))
18211807
@pytest.mark.parametrize("y_arr", [[1, 2, 4, 5], [1.0, 2.5, 6.0, 7.0]])
18221808
@pytest.mark.parametrize("x_arr", [[2, 5, 6, 9]])
18231809
def test_x_samples(self, y_arr, x_arr, y_dt, x_dt):
@@ -1979,7 +1965,7 @@ def test_discont(self, dt):
19791965

19801966
@pytest.mark.usefixtures("suppress_divide_invalid_numpy_warnings")
19811967
@pytest.mark.parametrize("val_type", [bool, int, float])
1982-
@pytest.mark.parametrize("data_type", get_all_dtypes())
1968+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
19831969
@pytest.mark.parametrize(
19841970
"func", ["add", "divide", "multiply", "power", "subtract"]
19851971
)
@@ -1996,30 +1982,24 @@ def test_discont(self, dt):
19961982
[[[1, 3], [3, 1]], [[0, 1], [1, 3]]],
19971983
],
19981984
],
1999-
ids=[
2000-
"[[0, 0], [0, 0]]",
2001-
"[[1, 2], [1, 2]]",
2002-
"[[1, 2], [3, 4]]",
2003-
"[[[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]]]",
2004-
"[[[[1, 2], [3, 4]], [[1, 2], [2, 1]]], [[[1, 3], [3, 1]], [[0, 1], [1, 3]]]]",
2005-
],
1985+
ids=["2D-zeros", "2D-repetitive", "2D", "3D", "4D"],
20061986
)
2007-
def test_op_with_scalar(array, val, func, data_type, val_type):
2008-
a = numpy.array(array, dtype=data_type)
2009-
ia = dpnp.array(array, dtype=data_type)
1987+
def test_op_with_scalar(array, val, func, dtype, val_type):
1988+
a = numpy.array(array, dtype=dtype)
1989+
ia = dpnp.array(a)
20101990
val_ = val_type(val)
20111991

20121992
if func == "power":
20131993
if (
20141994
val_ == 0
2015-
and numpy.issubdtype(data_type, numpy.complexfloating)
1995+
and numpy.issubdtype(dtype, numpy.complexfloating)
20161996
and not dpnp.all(ia)
20171997
):
20181998
pytest.skip(
20191999
"(0j ** 0) is different: (NaN + NaNj) in dpnp and (1 + 0j) in numpy"
20202000
)
20212001

2022-
if func == "subtract" and val_type == bool and data_type == dpnp.bool:
2002+
if func == "subtract" and val_type == bool and dtype == dpnp.bool:
20232003
with pytest.raises(TypeError):
20242004
result = getattr(dpnp, func)(ia, val_)
20252005
expected = getattr(numpy, func)(a, val_)
@@ -2037,43 +2017,43 @@ def test_op_with_scalar(array, val, func, data_type, val_type):
20372017

20382018

20392019
@pytest.mark.parametrize("shape", [(), (3, 2)], ids=["0D", "2D"])
2040-
@pytest.mark.parametrize("dtype", get_all_dtypes())
2020+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
20412021
def test_multiply_scalar(shape, dtype):
20422022
a = numpy.ones(shape, dtype=dtype)
2043-
ia = dpnp.ones(shape, dtype=dtype)
2023+
ia = dpnp.array(a)
20442024

20452025
result = 0.5 * ia * 1.7
20462026
expected = 0.5 * a * 1.7
20472027
assert_allclose(result, expected)
20482028

20492029

20502030
@pytest.mark.parametrize("shape", [(), (3, 2)], ids=["0D", "2D"])
2051-
@pytest.mark.parametrize("dtype", get_all_dtypes())
2031+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
20522032
def test_add_scalar(shape, dtype):
20532033
a = numpy.ones(shape, dtype=dtype)
2054-
ia = dpnp.ones(shape, dtype=dtype)
2034+
ia = dpnp.array(a)
20552035

20562036
result = 0.5 + ia + 1.7
20572037
expected = 0.5 + a + 1.7
20582038
assert_allclose(result, expected)
20592039

20602040

20612041
@pytest.mark.parametrize("shape", [(), (3, 2)], ids=["0D", "2D"])
2062-
@pytest.mark.parametrize("dtype", get_all_dtypes())
2042+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
20632043
def test_subtract_scalar(shape, dtype):
20642044
a = numpy.ones(shape, dtype=dtype)
2065-
ia = dpnp.ones(shape, dtype=dtype)
2045+
ia = dpnp.array(a)
20662046

20672047
result = 0.5 - ia - 1.7
20682048
expected = 0.5 - a - 1.7
20692049
assert_allclose(result, expected)
20702050

20712051

20722052
@pytest.mark.parametrize("shape", [(), (3, 2)], ids=["0D", "2D"])
2073-
@pytest.mark.parametrize("dtype", get_all_dtypes())
2053+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
20742054
def test_divide_scalar(shape, dtype):
20752055
a = numpy.ones(shape, dtype=dtype)
2076-
ia = dpnp.ones(shape, dtype=dtype)
2056+
ia = dpnp.array(a)
20772057

20782058
result = 0.5 / ia / 1.7
20792059
expected = 0.5 / a / 1.7
@@ -2084,7 +2064,7 @@ def test_divide_scalar(shape, dtype):
20842064
"data", [[[1.0, -1.0], [0.1, -0.1]], [-2, -1, 0, 1, 2]], ids=["2D", "1D"]
20852065
)
20862066
@pytest.mark.parametrize(
2087-
"dtype", get_all_dtypes(no_bool=True, no_unsigned=True)
2067+
"dtype", get_all_dtypes(no_none=True, no_bool=True, no_unsigned=True)
20882068
)
20892069
def test_negative(data, dtype):
20902070
a = numpy.array(data, dtype=dtype)
@@ -2116,7 +2096,7 @@ def test_negative_boolean():
21162096
@pytest.mark.parametrize(
21172097
"data", [[[1.0, -1.0], [0.1, -0.1]], [-2, -1, 0, 1, 2]], ids=["2D", "1D"]
21182098
)
2119-
@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True))
2099+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True, no_bool=True))
21202100
def test_positive(data, dtype):
21212101
a = get_abs_array(data, dtype=dtype)
21222102
ia = dpnp.array(a)
@@ -2215,11 +2195,11 @@ def test_sign(dtype):
22152195
ids=["[2, 0, -2]", "[1.1, -1.1]"],
22162196
)
22172197
@pytest.mark.parametrize(
2218-
"dtype", get_all_dtypes(no_complex=True, no_unsigned=True)
2198+
"dtype", get_all_dtypes(no_none=True, no_complex=True, no_unsigned=True)
22192199
)
22202200
def test_signbit(data, dtype):
22212201
a = numpy.array(data, dtype=dtype)
2222-
ia = dpnp.array(data, dtype=dtype)
2202+
ia = dpnp.array(a)
22232203

22242204
result = dpnp.signbit(ia)
22252205
expected = numpy.signbit(a)
@@ -2233,7 +2213,7 @@ def test_signbit(data, dtype):
22332213

22342214

22352215
class TestRealImag:
2236-
@pytest.mark.parametrize("dtype", get_all_dtypes())
2216+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
22372217
def test_real_imag(self, dtype):
22382218
a = generate_random_numpy_array(20, dtype)
22392219
ia = dpnp.array(a)
@@ -2284,7 +2264,7 @@ def test_projection_infinity(self, dtype):
22842264
assert iout is result
22852265
assert dpnp.allclose(result, expected)
22862266

2287-
@pytest.mark.parametrize("dtype", get_all_dtypes())
2267+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
22882268
def test_projection(self, dtype):
22892269
result = dpnp.proj(dpnp.array(1, dtype=dtype))
22902270
expected = dpnp.array(complex(1, 0))
@@ -2303,7 +2283,9 @@ class TestRoundingFuncs:
23032283
def func_params(self, request):
23042284
return request.param
23052285

2306-
@pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True))
2286+
@pytest.mark.parametrize(
2287+
"dtype", get_all_dtypes(no_none=True, no_complex=True)
2288+
)
23072289
def test_out(self, func_params, dtype):
23082290
func_name = func_params["func_name"]
23092291
input_values = func_params["input_values"]
@@ -2349,7 +2331,7 @@ def test_invalid_shape(self, func_params, shape):
23492331

23502332
class TestHypot:
23512333
@pytest.mark.parametrize(
2352-
"dtype", get_all_dtypes(no_bool=True, no_complex=True)
2334+
"dtype", get_all_dtypes(no_none=True, no_bool=True, no_complex=True)
23532335
)
23542336
def test_hypot(self, dtype):
23552337
a, b, expected = _get_numpy_arrays_2in_1out("hypot", dtype, [0, 10, 10])
@@ -2413,28 +2395,6 @@ def test_basic(self, dtype, axis, keepdims):
24132395

24142396
assert_dtype_allclose(res, exp)
24152397

2416-
@pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True))
2417-
@pytest.mark.parametrize("axis", [None, 2, -1, (0, 1)])
2418-
@pytest.mark.parametrize("keepdims", [True, False])
2419-
def test_logsumexp_out(self, dtype, axis, keepdims):
2420-
a = dpnp.ones((3, 4, 5, 6, 7), dtype=dtype)
2421-
exp_dt = None
2422-
dtype_list = [dpnp.bool, dpnp.int8, dpnp.uint8, dpnp.int16, dpnp.uint16]
2423-
if dtype in dtype_list:
2424-
exp_dt = dpnp.default_float_type(a.device)
2425-
exp = numpy.logaddexp.reduce(
2426-
dpnp.asnumpy(a), axis=axis, keepdims=keepdims, dtype=exp_dt
2427-
)
2428-
2429-
exp_dt = exp.dtype
2430-
if exp_dt == numpy.float64 and not has_support_aspect64():
2431-
exp_dt = numpy.float32
2432-
iout = dpnp.empty_like(a, shape=exp.shape, dtype=exp_dt)
2433-
res = dpnp.logsumexp(a, axis=axis, out=iout, keepdims=keepdims)
2434-
2435-
assert res is iout
2436-
assert_dtype_allclose(res, exp)
2437-
24382398
@pytest.mark.parametrize(
24392399
"in_dt", get_all_dtypes(no_none=True, no_bool=True, no_complex=True)
24402400
)

0 commit comments

Comments
 (0)