Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using rowvar keyword in dpnp.cov #1371

Merged
merged 1 commit into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions dpnp/dpnp_iface_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

from dpnp.dpnp_algo import *
from dpnp.dpnp_utils import *
from dpnp.dpnp_array import dpnp_array
import dpnp


Expand Down Expand Up @@ -237,7 +238,8 @@ def correlate(x1, x2, mode='valid'):


def cov(x1, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None):
"""
"""cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None):

Estimate a covariance matrix, given data and weights.

For full documentation refer to :obj:`numpy.cov`.
Expand All @@ -248,7 +250,6 @@ def cov(x1, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=
Dimension of input array ``m`` is limited by ``m.ndim > 2``.
Size and shape of input arrays are supported to be equal.
Prameters ``y`` is supported only with default value ``None``.
Prameters ``rowvar`` is supported only with default value ``True``.
Prameters ``bias`` is supported only with default value ``False``.
Prameters ``ddof`` is supported only with default value ``None``.
Prameters ``fweights`` is supported only with default value ``None``.
Expand Down Expand Up @@ -280,8 +281,6 @@ def cov(x1, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=
pass
elif y is not None:
pass
elif not rowvar:
pass
elif bias:
pass
elif ddof is not None:
Expand All @@ -291,8 +290,14 @@ def cov(x1, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=
elif aweights is not None:
pass
else:
if x1_desc.dtype != dpnp.float64:
x1_desc = dpnp.get_dpnp_descriptor(dpnp.astype(x1, dpnp.float64), copy_when_nondefault_queue=False)
if not rowvar and x1.shape[0] != 1:
x1 = x1.get_array() if isinstance(x1, dpnp_array) else x1
x1 = dpnp_array._create_from_usm_ndarray(x1.mT)
x1 = dpnp.astype(x1, dpnp.float64) if x1_desc.dtype != dpnp.float64 else x1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test that this works on Iris Xe, or other devices that do not support float64 data type.

Copy link
Collaborator Author

@vtavana vtavana Apr 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will not work. @antonwolfy mentioned it is written like this because Numpy by default returns float64 for any input data type. The suggestion was to fix it in a separate ticket since it needs to correct dpnp c++ backend code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've filed #1379 to add support of dpnp.cov on Iris Xe

x1_desc = dpnp.get_dpnp_descriptor(x1, copy_when_nondefault_queue=False)
elif x1_desc.dtype != dpnp.float64:
x1 = dpnp.astype(x1, dpnp.float64)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here

x1_desc = dpnp.get_dpnp_descriptor(x1, copy_when_nondefault_queue=False)

return dpnp_cov(x1_desc).get_pyobj()

Expand Down
17 changes: 16 additions & 1 deletion tests/test_statistics.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest

from .helper import get_all_dtypes
import dpnp

import numpy
Expand Down Expand Up @@ -114,3 +114,18 @@ def test_bincount_weights(self, array, weights):
expected = numpy.bincount(np_a, weights=weights)
result = dpnp.bincount(dpnp_a, weights=weights)
numpy.testing.assert_array_equal(expected, result)

@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True))
def test_cov_rowvar1(dtype):
a = dpnp.array([[0, 2], [1, 1], [2, 0]], dtype=dtype)
b = numpy.array([[0, 2], [1, 1], [2, 0]], dtype=dtype)
numpy.testing.assert_array_equal(dpnp.cov(a.T), dpnp.cov(a,rowvar=False))
numpy.testing.assert_array_equal(numpy.cov(b,rowvar=False), dpnp.cov(a,rowvar=False))

@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True))
def test_cov_rowvar2(dtype):
a = dpnp.array([[0, 1, 2]], dtype=dtype)
b = numpy.array([[0, 1, 2]], dtype=dtype)
numpy.testing.assert_array_equal(numpy.cov(b,rowvar=False), dpnp.cov(a,rowvar=False))