Skip to content

Commit 2b8ab4b

Browse files
committed
Add tests for bias keyword and usm_ndarray input
1 parent 6b55bc0 commit 2b8ab4b

File tree

2 files changed

+45
-22
lines changed

2 files changed

+45
-22
lines changed

dpnp/dpnp_utils/dpnp_utils_statistics.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -128,34 +128,18 @@ def dpnp_cov(
128128
129129
"""
130130

131-
def _get_2dmin_array(x):
132-
"""
133-
Transform an input array to a form required for building a covariance matrix.
134-
135-
If applicable, it reshapes the input array to have 2 dimensions or greater.
136-
If applicable, it transposes the input array when 'rowvar' is False.
137-
It casts to another dtype, if the input array differs from requested one.
138-
139-
"""
140-
if x.ndim == 0:
141-
x = dpnp.reshape(x, (1, 1))
142-
elif x.ndim == 1:
143-
x = x[dpnp.newaxis, :]
144-
elif not rowvar:
145-
x = x.T
146-
147-
if x.dtype != dtype:
148-
x = dpnp.astype(x, dtype)
149-
return x
150-
151-
x = _get_2dmin_array(m)
131+
# need to create a copy of input, since it will be modified in-place
132+
x = dpnp.array(m, ndmin=2, dtype=dtype)
133+
if not rowvar and m.ndim != 1:
134+
x = x.T
135+
152136
if x.shape[0] == 0:
153137
return dpnp.empty_like(
154138
x, shape=(0, 0), dtype=dpnp.default_float_type(m.sycl_queue)
155139
)
156140

157141
if y is not None:
158-
y = _get_2dmin_array(y)
142+
y = dpnp.array(y, copy=None, ndmin=2, dtype=dtype)
159143
x = dpnp.concatenate((x, y), axis=0)
160144

161145
# get the product of frequencies and weights

dpnp/tests/test_statistics.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,45 @@ def test_dtype(self, dt):
532532
assert_allclose(result, expected)
533533
assert result.dtype == dt
534534

535+
@pytest.mark.parametrize("dt", get_float_complex_dtypes())
536+
@pytest.mark.parametrize("bias", [True, False])
537+
def test_bias(self, dt, bias):
538+
a = generate_random_numpy_array((3, 4), dtype=dt)
539+
ia = dpnp.array(a)
540+
541+
expected = numpy.cov(a, bias=bias)
542+
result = dpnp.cov(ia, bias=bias)
543+
assert_dtype_allclose(result, expected)
544+
545+
# with rowvar
546+
expected = numpy.cov(a, rowvar=False, bias=bias)
547+
result = dpnp.cov(ia, rowvar=False, bias=bias)
548+
assert_dtype_allclose(result, expected)
549+
550+
freq = numpy.array([1, 4, 1, 7])
551+
ifreq = dpnp.array(freq)
552+
553+
# with frequency
554+
expected = numpy.cov(a, bias=bias, fweights=freq)
555+
result = dpnp.cov(ia, bias=bias, fweights=ifreq)
556+
assert_dtype_allclose(result, expected)
557+
558+
weights = numpy.array([1.2, 3.7, 5.0, 1.1])
559+
iweights = dpnp.array(weights)
560+
561+
# with weights
562+
expected = numpy.cov(a, bias=bias, aweights=weights)
563+
result = dpnp.cov(ia, bias=bias, aweights=iweights)
564+
assert_dtype_allclose(result, expected)
565+
566+
def test_usm_ndarray(self):
567+
a = numpy.array([[0, 2], [1, 1], [2, 0]])
568+
ia = dpt.asarray(a)
569+
570+
expected = numpy.cov(a.T)
571+
result = dpnp.cov(ia.T)
572+
assert_allclose(result, expected)
573+
535574
# numpy 2.2 properly transposes 2d array when rowvar=False
536575
@with_requires("numpy>=2.2")
537576
@pytest.mark.filterwarnings("ignore::RuntimeWarning")

0 commit comments

Comments
 (0)