Ejemplo n.º 1
0
 def test_putmask(self, xp, dtype):
     a = testing.shaped_random(self.shape, xp, dtype=dtype, seed=0)
     mask = testing.shaped_random(self.shape, xp, dtype=numpy.bool_, seed=1)
     values = testing.shaped_random(self.shape, xp, dtype=dtype, seed=2)
     ret = xp.putmask(a, mask, values)
     assert ret is None
     return a
Ejemplo n.º 2
0
 def test_operator_matmul(self, xp, dtype1):
     if ((dtype1, dtype1) in self.skip_dtypes
             or (dtype1, dtype1) in self.skip_dtypes):
         return xp.array([])
     x1 = testing.shaped_random(self.shape_pair[0], xp, dtype1)
     x2 = testing.shaped_random(self.shape_pair[1], xp, dtype1)
     return operator.matmul(x1, x2)
Ejemplo n.º 3
0
 def test_putmask_differnt_dtypes_mask(self, xp, dtype):
     shape = (2, 3)
     a = testing.shaped_random(shape, xp, dtype=numpy.int64)
     mask = testing.shaped_random(shape, xp, dtype=dtype)
     values = testing.shaped_random((3, ), xp, dtype=numpy.int64)
     xp.putmask(a, mask, values)
     return a
Ejemplo n.º 4
0
 def test_place_shape_unmatch_error(self, dtype):
     for xp in (numpy, cupy):
         a = testing.shaped_arange(self.shape, xp, dtype)
         mask = testing.shaped_random((3, 4), xp, numpy.bool_)
         vals = testing.shaped_random((1, ), xp, dtype)
         with pytest.raises(ValueError):
             xp.place(a, mask, vals)
Ejemplo n.º 5
0
 def test_percentile_keepdims(self, xp, dtype, interpolation):
     a = testing.shaped_random((7, 2, 9, 2), xp, dtype)
     q = testing.shaped_random((5, ), xp, dtype=dtype, scale=100)
     return xp.percentile(a,
                          q,
                          axis=None,
                          keepdims=True,
                          interpolation=interpolation)
Ejemplo n.º 6
0
 def test_cupy_matmul(self, xp, dtype1):
     if ((dtype1, dtype1) in self.skip_dtypes
             or (dtype1, dtype1) in self.skip_dtypes):
         return xp.array([])
     shape1, shape2 = self.shape_pair
     x1 = testing.shaped_random(shape1, xp, dtype1)
     x2 = testing.shaped_random(shape2, xp, dtype1)
     return xp.matmul(x1, x2)
Ejemplo n.º 7
0
 def test_where_two_arrays(self, xp, cond_type, x_type, y_type):
     m = testing.shaped_random(self.cond_shape, xp, xp.bool_)
     # Almost all values of a matrix `shaped_random` makes are not zero.
     # To make a sparse matrix, we need multiply `m`.
     cond = testing.shaped_random(self.cond_shape, xp, cond_type) * m
     x = testing.shaped_random(self.x_shape, xp, x_type, seed=0)
     y = testing.shaped_random(self.y_shape, xp, y_type, seed=1)
     return xp.where(cond, x, y)
Ejemplo n.º 8
0
 def test_percentile_out(self, xp, dtype, interpolation):
     a = testing.shaped_random((10, 2, 3, 2), xp, dtype)
     q = testing.shaped_random((5, ), xp, dtype=dtype, scale=100)
     out = testing.shaped_random((5, 10, 2, 3), xp, dtype)
     return xp.percentile(a,
                          q,
                          axis=-1,
                          interpolation=interpolation,
                          out=out)
Ejemplo n.º 9
0
 def test_place(self, xp, dtype):
     a = testing.shaped_arange(self.shape, xp, dtype)
     if self.n_vals == 0:
         mask = xp.zeros(self.shape, dtype=numpy.bool_)
     else:
         mask = testing.shaped_random(self.shape, xp, numpy.bool_)
     vals = testing.shaped_random((self.n_vals, ), xp, dtype)
     xp.place(a, mask, vals)
     return a
Ejemplo n.º 10
0
 def test_putmask_differnt_dtypes_raises(self, a_dtype, val_dtype):
     shape = (2, 3)
     for xp in (numpy, cupy):
         a = testing.shaped_random(shape, xp, dtype=a_dtype)
         mask = testing.shaped_random(shape, xp, dtype=numpy.bool_)
         values = testing.shaped_random((3, ), xp, dtype=val_dtype)
         if not numpy.can_cast(val_dtype, a_dtype):
             with pytest.raises(TypeError):
                 xp.putmask(a, mask, values)
Ejemplo n.º 11
0
 def test_bool_subtract(self):
     xp = self.xp
     if xp is numpy and not testing.numpy_satisfies('>=1.14.0'):
         raise unittest.SkipTest('NumPy<1.14.0')
     shape = self.shape
     x = testing.shaped_random(shape, xp, dtype=numpy.bool_)
     y = testing.shaped_random(shape, xp, dtype=numpy.bool_)
     with pytest.raises(TypeError):
         xp.subtract(x, y)
Ejemplo n.º 12
0
 def test_round(self, xp, dtype):
     if dtype == numpy.bool_:
         # avoid cast problem
         a = testing.shaped_random(self.shape, xp, scale=10, dtype=dtype)
         return a.round(0)
     if dtype == numpy.float16:
         # avoid accuracy problem
         a = testing.shaped_random(self.shape, xp, scale=10, dtype=dtype)
         return a.round(0)
     a = testing.shaped_random(self.shape, xp, scale=100, dtype=dtype)
     return a.round(self.decimals)
Ejemplo n.º 13
0
 def func(xp):
     m = testing.shaped_random((2, 3), xp, xp.bool_)
     a = testing.shaped_random((2, 3), xp, dtype) * m
     c = xp.count_nonzero(a)
     if xp is cupy:
         # CuPy returns zero-dimensional array instead of
         # returning a scalar value
         self.assertIsInstance(c, xp.ndarray)
         self.assertEqual(c.dtype, 'l')
         self.assertEqual(c.shape, ())
     return int(c)
Ejemplo n.º 14
0
    def test_nanstd_huge(self, xp, dtype):
        a = testing.shaped_random((1024, 512), xp, dtype)

        if a.dtype.kind not in 'biu':
            a[:512, :256] = xp.nan

        return xp.nanstd(a, axis=1)
Ejemplo n.º 15
0
 def test_put_index_scalar(self, xp):
     dtype = cupy.float32
     a = testing.shaped_arange(self.shape, xp, dtype)
     inds = 4
     vals = testing.shaped_random((4, ), xp, dtype)
     xp.put(a, inds, vals)
     return a
Ejemplo n.º 16
0
 def test_place_empty_value_error(self, dtype):
     for xp in (numpy, cupy):
         a = testing.shaped_arange(self.shape, xp, dtype)
         mask = testing.shaped_arange(self.shape, xp, numpy.int) % 2 == 0
         vals = testing.shaped_random((0, ), xp, dtype)
         with pytest.raises(ValueError):
             xp.place(a, mask, vals)
Ejemplo n.º 17
0
 def test_put_inds_overflow_error(self, dtype):
     for xp in (numpy, cupy):
         a = testing.shaped_arange(self.shape, xp, dtype)
         inds = xp.array([2, -1, 3, 7])
         vals = testing.shaped_random((4, ), xp, dtype)
         with pytest.raises(IndexError):
             xp.put(a, inds, vals, mode='raise')
Ejemplo n.º 18
0
 def test_put_mode_error(self, dtype):
     for xp in (numpy, cupy):
         a = testing.shaped_arange(self.shape, xp, dtype)
         inds = xp.array([2, -1, 3, 0])
         vals = testing.shaped_random((4, ), xp, dtype)
         with pytest.raises(ValueError):
             xp.put(a, inds, vals, mode='unknown')
Ejemplo n.º 19
0
    def test_nanmean_with_nan_float(self, xp, dtype):
        a = testing.shaped_random(self.shape, xp, dtype)

        if a.dtype.kind not in 'biu':
            a[1, :] = xp.nan
            a[:, 3] = xp.nan

        return xp.nanmean(a, axis=self.axis, keepdims=self.keepdims)
Ejemplo n.º 20
0
    def test_ihfft(self, xp, dtype):
        a = testing.shaped_random(self.shape, xp, dtype)
        out = xp.fft.ihfft(a, n=self.n, norm=self.norm)

        if xp is np and dtype in [np.float16, np.float32, np.complex64]:
            out = out.astype(np.complex64)

        return out
Ejemplo n.º 21
0
    def test_ifftn(self, xp, dtype):
        a = testing.shaped_random(self.shape, xp, dtype)
        out = xp.fft.ifftn(a, s=self.s, axes=self.axes, norm=self.norm)

        if self.axes is not None and not self.axes:
            assert out is a
            return out

        return out
Ejemplo n.º 22
0
 def test_put(self, xp, dtype):
     a = testing.shaped_arange(self.shape, xp, dtype)
     # Take care so that actual indices don't overlap.
     if self.mode == 'raise':
         inds = xp.array([2, -1, 3, 0])
     else:
         inds = xp.array([2, -8, 3, 7])
     vals = testing.shaped_random((self.n_vals, ), xp, dtype)
     xp.put(a, inds, vals, self.mode)
     return a
Ejemplo n.º 23
0
    def test_nanstd_out(self, xp, dtype):
        a = testing.shaped_random((10, 20, 30), xp, dtype)
        z = xp.zeros((20, 30))

        if a.dtype.kind not in 'biu':
            a[1, :] = xp.nan
            a[:, 3] = xp.nan

        xp.nanstd(a, axis=0, out=z)
        return z
Ejemplo n.º 24
0
 def test_argminmax_dtype(self, in_dtype, result_dtype):
     a = testing.shaped_random(self.shape, cupy, in_dtype)
     if self.is_module:
         func = getattr(cupy, self.func)
         y = func(a, dtype=result_dtype)
     else:
         func = getattr(a, self.func)
         y = func(dtype=result_dtype)
     assert y.shape == ()
     assert y.dtype == result_dtype
Ejemplo n.º 25
0
    def test_median_invalid_axis(self):
        for xp in [numpy, cupy]:
            a = testing.shaped_random((3, 4, 5), xp)
            with pytest.raises(numpy.AxisError):
                return xp.median(a, -a.ndim - 1, keepdims=False)

            with pytest.raises(numpy.AxisError):
                return xp.median(a, a.ndim, keepdims=False)

            with pytest.raises(numpy.AxisError):
                return xp.median(a, (-a.ndim - 1, 1), keepdims=False)

            with pytest.raises(numpy.AxisError):
                return xp.median(a, (0, a.ndim,), keepdims=False)
Ejemplo n.º 26
0
    def test_cub_max(self, xp, dtype, axis):
        a = testing.shaped_random(self.shape, xp, dtype)
        if self.order in ('c', 'C'):
            a = xp.ascontiguousarray(a)
        elif self.order in ('f', 'F'):
            a = xp.asfortranarray(a)

        if xp is numpy:
            return a.max(axis=axis)

        # xp is cupy, first ensure we really use CUB
        ret = cupy.empty(())  # Cython checks return type, need to fool it
        if len(axis) == len(self.shape):
            func = 'cupy.core._routines_statistics.cub.device_reduce'
        else:
            func = 'cupy.core._routines_statistics.cub.device_segmented_reduce'
        with testing.AssertFunctionIsCalled(func, return_value=ret):
            a.max(axis=axis)
        # ...then perform the actual computation
        return a.max(axis=axis)
Ejemplo n.º 27
0
 def test_min_axis0(self, xp, dtype):
     a = testing.shaped_random((2, 3, 4), xp, dtype)
     return a.min(axis=0)
Ejemplo n.º 28
0
 def test_min_axis_large(self, xp, dtype):
     a = testing.shaped_random((3, 1000), xp, dtype)
     return a.min(axis=0)
Ejemplo n.º 29
0
 def test_min_all_keepdims(self, xp, dtype):
     a = testing.shaped_random((2, 3), xp, dtype)
     return a.min(keepdims=True)
Ejemplo n.º 30
0
 def test_min_all(self, xp, dtype):
     a = testing.shaped_random((2, 3), xp, dtype)
     return a.min()