コード例 #1
0
def test_input_and_filter_sizes(nh, nx):
    dtype_data = dtype_filter = np.float32
    x = cp.arange(nx, dtype=dtype_data)
    x_cpu = x.get()
    h_cpu = np.arange(1, nh + 1, dtype=dtype_filter)
    h = cp.asarray(h_cpu)

    # up=1 kernel case
    cp.testing.assert_allclose(signal.upfirdn(h_cpu, x_cpu, up=1, down=2),
                               upfirdn(h, x, up=1, down=2))

    # down=1 kernel case
    cp.testing.assert_allclose(signal.upfirdn(h_cpu, x_cpu, up=2, down=1),
                               upfirdn(h, x, up=2, down=1))
コード例 #2
0
def test_dtype_combos(dtype_data, dtype_filter):
    shape = (64, 64)
    size = int(np.prod(shape))
    x = cp.arange(size, dtype=dtype_data).reshape(shape)
    x_cpu = x.get()
    h_cpu = np.arange(5, dtype=dtype_filter)
    h = cp.asarray(h_cpu)

    # up=1 kernel case
    cp.testing.assert_allclose(signal.upfirdn(h_cpu, x_cpu, up=1, down=2),
                               upfirdn(h, x, up=1, down=2))

    # down=1 kernel case
    cp.testing.assert_allclose(signal.upfirdn(h_cpu, x_cpu, up=2, down=1),
                               upfirdn(h, x, up=2, down=1))
コード例 #3
0
def test_general_up_and_down(up, down, nx, nh):
    dtype_data = dtype_filter = np.float32
    x = cp.arange(nx, dtype=dtype_data)
    x_cpu = x.get()
    h_cpu = np.arange(1, nh + 1, dtype=dtype_filter)
    h = cp.asarray(h_cpu)

    cp.testing.assert_allclose(
        signal.upfirdn(h_cpu, x_cpu, up=up, down=down),
        upfirdn(h, x, up=up, down=down),
    )
コード例 #4
0
ファイル: test_upfirdn.py プロジェクト: MannyKayy/cupyimg
    def test_modes(self, size, h_len, mode, dtype):
        random_state = cp.random.RandomState(5)
        x = random_state.randn(size).astype(dtype)
        if dtype in (cp.complex64, cp.complex128):
            x += 1j * random_state.randn(size)
        h = cp.arange(1, 1 + h_len, dtype=x.real.dtype)

        y = upfirdn(h, x, up=1, down=1, mode=mode)
        # expected result: pad the input, filter with zero padding, then crop
        npad = h_len - 1
        if mode in ["antisymmetric", "antireflect", "smooth", "line"]:
            # use _pad_test test function for modes not supported by cp.pad.
            xpad = _pad_test(x, npre=npad, npost=npad, mode=mode)
        else:
            xpad = cp.pad(x, npad, mode=mode)
        ypad = upfirdn(h, xpad, up=1, down=1, mode="constant")
        y_expected = ypad[npad:-npad]

        atol = rtol = cp.finfo(dtype).eps * 1e2
        assert_allclose(y, y_expected, atol=atol, rtol=rtol)
コード例 #5
0
def test_up(up):
    dtype_data = dtype_filter = np.float32
    nx = 16
    nh = 4
    x = cp.arange(nx, dtype=dtype_data)
    x_cpu = x.get()
    h_cpu = np.arange(1, nh + 1, dtype=dtype_filter)
    h = cp.asarray(h_cpu)

    # up=1 kernel case
    cp.testing.assert_allclose(
        signal.upfirdn(h_cpu, x_cpu, up=up, down=1),
        upfirdn(h, x, up=up, down=1),
    )
コード例 #6
0
def test_axis_and_order(shape, axis, order):
    dtype_data = dtype_filter = np.float32
    size = int(np.prod(shape))
    x_cpu = np.arange(size, dtype=dtype_data).reshape(shape, order=order)
    h_cpu = np.arange(3, dtype=dtype_filter)
    x = cp.asarray(x_cpu, order=order)
    h = cp.asarray(h_cpu)
    ndim = len(shape)
    if axis >= -ndim and axis < ndim:
        # up=1 case
        cp.testing.assert_allclose(
            signal.upfirdn(h_cpu, x_cpu, up=1, down=2, axis=axis),
            upfirdn(h, x, up=1, down=2, axis=axis),
        )

        # down=1 case
        cp.testing.assert_allclose(
            signal.upfirdn(h_cpu, x_cpu, up=2, down=1, axis=axis),
            upfirdn(h, x, up=2, down=1, axis=axis),
        )
    else:
        with pytest.raises(ValueError):
            upfirdn(h, x, up=2, down=1, axis=axis)
コード例 #7
0
ファイル: test_upfirdn.py プロジェクト: mritools/cupyimg
 def scrub(self, x, axis=-1):
     yr = np.apply_along_axis(upfirdn_naive, axis, x.get(), self.h.get(),
                              self.up, self.down)
     yr = cp.asarray(yr)
     y = upfirdn(self.h, x, self.up, self.down, axis=axis)
     dtypes = (self.h.dtype, x.dtype)
     if all(d == cp.complex64 for d in dtypes):
         assert_equal(y.dtype, cp.complex64)
     elif cp.complex64 in dtypes and cp.float32 in dtypes:
         assert_equal(y.dtype, cp.complex64)
     elif all(d == cp.float32 for d in dtypes):
         assert_equal(y.dtype, cp.float32)
     elif cp.complex128 in dtypes or cp.complex64 in dtypes:
         assert_equal(y.dtype, cp.complex128)
     else:
         assert_equal(y.dtype, cp.float64)
     assert_allclose(yr, y)
コード例 #8
0
ファイル: test_upfirdn.py プロジェクト: MannyKayy/cupyimg
    def test_vs_lfilter(self):
        # Check that up=1.0 gives same answer as lfilter + slicing
        random_state = cp.random.RandomState(17)
        try_types = (int, cp.float32, cp.complex64, float, complex)
        size = 10000
        down_factors = [2, 11, 79]

        for dtype in try_types:
            x = random_state.randn(size).astype(dtype)
            if dtype in (cp.complex64, cp.complex128):
                x += 1j * random_state.randn(size)

            tol = cp.finfo(cp.float32).eps * 100

            for down in down_factors:
                h = firwin(31, 1.0 / down, window="hamming")
                yl = cp.asarray(lfilter(h, 1.0, x.get())[::down])
                h = cp.asarray(h)
                y = upfirdn(h, x, up=1, down=down)
                assert_allclose(yl, y[: yl.size], atol=tol, rtol=tol)
コード例 #9
0
def test_convolve1d(dtype_x, dtype_h, len_x, mode):
    x_cpu = np.arange(1, 1 + len_x, dtype=dtype_x)
    for len_h in range(1, len_x):
        h_cpu = np.arange(1, 1 + len_h, dtype=dtype_h)
        min_origin = -(len_h // 2)
        max_origin = (len_h - 1) // 2
        for origin in range(min_origin, max_origin + 1):
            y = ndi.convolve1d(x_cpu, h_cpu, mode=mode, cval=0, origin=origin)

            # test via convolve1d
            y3 = convolve1d(
                cp.asarray(x_cpu),
                cp.asarray(h_cpu),
                mode=mode,
                cval=0,
                origin=origin,
            )
            cp.testing.assert_allclose(y, y3)

            # test using upfirdn directly
            offset = len(h_cpu) // 2 + origin
            mode_kwargs = _get_ndimage_mode_kwargs(mode, cval=0)
            y2 = upfirdn(
                cp.asarray(h_cpu),
                cp.asarray(x_cpu),
                offset=offset,
                **mode_kwargs,
            )[:len_x]
            cp.testing.assert_allclose(y, y2)

        for origin in [min_origin - 1, max_origin + 1]:
            with pytest.raises(ValueError):
                convolve1d(
                    cp.asarray(x_cpu),
                    cp.asarray(h_cpu),
                    mode=mode,
                    cval=0,
                    origin=origin,
                )
コード例 #10
0
ファイル: test_upfirdn.py プロジェクト: MannyKayy/cupyimg
def _pad_test(x, npre, npost, mode):
    # test array extension by convolving with an impulse padded with zeros
    h = cp.zeros((npre + npost + 1))
    h[npre] = 1
    return upfirdn(h, x, up=1, down=1, mode=mode)