|
| 1 | +import unittest |
| 2 | + |
| 3 | +import numpy |
| 4 | +import pytest |
| 5 | + |
| 6 | +import dpnp as cupy |
| 7 | +from tests.third_party.cupy import testing |
| 8 | + |
| 9 | + |
| 10 | +@testing.gpu |
| 11 | +class TestCorrcoef(unittest.TestCase): |
| 12 | + |
| 13 | + @testing.for_all_dtypes() |
| 14 | + @testing.numpy_cupy_allclose() |
| 15 | + def test_corrcoef(self, xp, dtype): |
| 16 | + a = testing.shaped_arange((2, 3), xp, dtype) |
| 17 | + return xp.corrcoef(a) |
| 18 | + |
| 19 | + @testing.for_all_dtypes() |
| 20 | + @testing.numpy_cupy_allclose() |
| 21 | + def test_corrcoef_diag_exception(self, xp, dtype): |
| 22 | + a = testing.shaped_arange((1, 3), xp, dtype) |
| 23 | + return xp.corrcoef(a) |
| 24 | + |
| 25 | + @testing.for_all_dtypes() |
| 26 | + @testing.numpy_cupy_allclose() |
| 27 | + def test_corrcoef_y(self, xp, dtype): |
| 28 | + a = testing.shaped_arange((2, 3), xp, dtype) |
| 29 | + y = testing.shaped_arange((2, 3), xp, dtype) |
| 30 | + return xp.corrcoef(a, y=y) |
| 31 | + |
| 32 | + @testing.for_all_dtypes() |
| 33 | + @testing.numpy_cupy_allclose() |
| 34 | + def test_corrcoef_rowvar(self, xp, dtype): |
| 35 | + a = testing.shaped_arange((2, 3), xp, dtype) |
| 36 | + y = testing.shaped_arange((2, 3), xp, dtype) |
| 37 | + return xp.corrcoef(a, y=y, rowvar=False) |
| 38 | + |
| 39 | + |
| 40 | +@testing.gpu |
| 41 | +class TestCov(unittest.TestCase): |
| 42 | + |
| 43 | + def generate_input(self, a_shape, y_shape, xp, dtype): |
| 44 | + a = testing.shaped_arange(a_shape, xp, dtype) |
| 45 | + y = None |
| 46 | + if y_shape is not None: |
| 47 | + y = testing.shaped_arange(y_shape, xp, dtype) |
| 48 | + return a, y |
| 49 | + |
| 50 | + @testing.for_all_dtypes() |
| 51 | + @testing.numpy_cupy_allclose() |
| 52 | + def check(self, a_shape, y_shape=None, rowvar=True, bias=False, |
| 53 | + ddof=None, xp=None, dtype=None): |
| 54 | + a, y = self.generate_input(a_shape, y_shape, xp, dtype) |
| 55 | + return xp.cov(a, y, rowvar, bias, ddof) |
| 56 | + |
| 57 | + @testing.for_all_dtypes() |
| 58 | + @testing.numpy_cupy_allclose() |
| 59 | + def check_warns(self, a_shape, y_shape=None, rowvar=True, bias=False, |
| 60 | + ddof=None, xp=None, dtype=None): |
| 61 | + with testing.assert_warns(RuntimeWarning): |
| 62 | + a, y = self.generate_input(a_shape, y_shape, xp, dtype) |
| 63 | + return xp.cov(a, y, rowvar, bias, ddof) |
| 64 | + |
| 65 | + @testing.for_all_dtypes() |
| 66 | + def check_raises(self, a_shape, y_shape=None, rowvar=True, bias=False, |
| 67 | + ddof=None, dtype=None): |
| 68 | + for xp in (numpy, cupy): |
| 69 | + a, y = self.generate_input(a_shape, y_shape, xp, dtype) |
| 70 | + with pytest.raises(ValueError): |
| 71 | + xp.cov(a, y, rowvar, bias, ddof) |
| 72 | + |
| 73 | + def test_cov(self): |
| 74 | + self.check((2, 3)) |
| 75 | + self.check((2,), (2,)) |
| 76 | + self.check((1, 3), (1, 3), rowvar=False) |
| 77 | + self.check((2, 3), (2, 3), rowvar=False) |
| 78 | + self.check((2, 3), bias=True) |
| 79 | + self.check((2, 3), ddof=2) |
| 80 | + |
| 81 | + def test_cov_warns(self): |
| 82 | + self.check_warns((2, 3), ddof=3) |
| 83 | + self.check_warns((2, 3), ddof=4) |
| 84 | + |
| 85 | + def test_cov_raises(self): |
| 86 | + self.check_raises((2, 3), ddof=1.2) |
| 87 | + self.check_raises((3, 4, 2)) |
| 88 | + self.check_raises((2, 3), (3, 4, 2)) |
| 89 | + |
| 90 | + def test_cov_empty(self): |
| 91 | + self.check((0, 1)) |
| 92 | + |
| 93 | + |
| 94 | +@testing.gpu |
| 95 | +@testing.parameterize(*testing.product({ |
| 96 | + 'mode': ['valid', 'same', 'full'], |
| 97 | + 'shape1': [(5,), (6,), (20,), (21,)], |
| 98 | + 'shape2': [(5,), (6,), (20,), (21,)], |
| 99 | +})) |
| 100 | +class TestCorrelateShapeCombination(unittest.TestCase): |
| 101 | + |
| 102 | + @testing.for_all_dtypes(no_float16=True) |
| 103 | + @testing.numpy_cupy_allclose(rtol=1e-4) |
| 104 | + def test_correlate(self, xp, dtype): |
| 105 | + a = testing.shaped_arange(self.shape1, xp, dtype) |
| 106 | + b = testing.shaped_arange(self.shape2, xp, dtype) |
| 107 | + return xp.correlate(a, b, mode=self.mode) |
| 108 | + |
| 109 | + |
| 110 | +@testing.gpu |
| 111 | +@testing.parameterize(*testing.product({ |
| 112 | + 'mode': ['valid', 'full', 'same'] |
| 113 | +})) |
| 114 | +class TestCorrelate(unittest.TestCase): |
| 115 | + |
| 116 | + @testing.for_all_dtypes() |
| 117 | + @testing.numpy_cupy_allclose(rtol=1e-5) |
| 118 | + def test_correlate_non_contiguous(self, xp, dtype): |
| 119 | + a = testing.shaped_arange((300,), xp, dtype) |
| 120 | + b = testing.shaped_arange((100,), xp, dtype) |
| 121 | + return xp.correlate(a[::200], b[10::70], mode=self.mode) |
| 122 | + |
| 123 | + @testing.for_all_dtypes(no_float16=True) |
| 124 | + @testing.numpy_cupy_allclose(rtol=1e-4) |
| 125 | + def test_correlate_large_non_contiguous(self, xp, dtype): |
| 126 | + a = testing.shaped_arange((10000,), xp, dtype) |
| 127 | + b = testing.shaped_arange((1000,), xp, dtype) |
| 128 | + return xp.correlate(a[200::], b[10::700], mode=self.mode) |
| 129 | + |
| 130 | + @testing.for_all_dtypes_combination(names=['dtype1', 'dtype2']) |
| 131 | + @testing.numpy_cupy_allclose(rtol=1e-2) |
| 132 | + def test_correlate_diff_types(self, xp, dtype1, dtype2): |
| 133 | + a = testing.shaped_random((200,), xp, dtype1) |
| 134 | + b = testing.shaped_random((100,), xp, dtype2) |
| 135 | + return xp.correlate(a, b, mode=self.mode) |
| 136 | + |
| 137 | + |
| 138 | +@testing.gpu |
| 139 | +@testing.parameterize(*testing.product({ |
| 140 | + 'mode': ['valid', 'same', 'full'] |
| 141 | +})) |
| 142 | +class TestCorrelateInvalid(unittest.TestCase): |
| 143 | + |
| 144 | + @testing.with_requires('numpy>=1.18') |
| 145 | + @testing.for_all_dtypes() |
| 146 | + def test_correlate_empty(self, dtype): |
| 147 | + for xp in (numpy, cupy): |
| 148 | + a = xp.zeros((0,), dtype) |
| 149 | + with pytest.raises(ValueError): |
| 150 | + xp.correlate(a, a, mode=self.mode) |
| 151 | + |
| 152 | + @testing.for_all_dtypes() |
| 153 | + def test_correlate_ndim(self, dtype): |
| 154 | + for xp in (numpy, cupy): |
| 155 | + a = testing.shaped_arange((5, 10, 2), xp, dtype) |
| 156 | + b = testing.shaped_arange((3, 4, 4), xp, dtype) |
| 157 | + with pytest.raises(ValueError): |
| 158 | + xp.correlate(a, b, mode=self.mode) |
| 159 | + |
| 160 | + @testing.for_all_dtypes() |
| 161 | + def test_correlate_zero_dim(self, dtype): |
| 162 | + for xp in (numpy, cupy): |
| 163 | + a = testing.shaped_arange((), xp, dtype) |
| 164 | + b = testing.shaped_arange((1,), xp, dtype) |
| 165 | + with pytest.raises(ValueError): |
| 166 | + xp.correlate(a, b, mode=self.mode) |
0 commit comments