swh:1:snp:f4c1ad9395dff93a83299e32c295952039ac85ba
Raw File
Tip revision: ec276104a503c5aadec0212bdf25ce2d993cd7e8 authored by Frank Ong on 04 March 2019, 00:50:10 UTC
Bump version: 0.1.0 → 0.1.1
Tip revision: ec27610
test_interp.py
import unittest
import numpy as np
from sigpy import interp, config

if config.cupy_enabled:
    import cupy as cp

if __name__ == '__main__':
    unittest.main()


class TestInterp(unittest.TestCase):

    def test_interpolate(self):
        xps = [np]
        if config.cupy_enabled:
            xps.append(cp)

        batch = 2
        for xp in xps:
            for ndim in [1, 2, 3]:
                with self.subTest(ndim=ndim, xp=xp):
                    shape = [3] + [1] * (ndim - 1)
                    width = 2.0
                    kernel = xp.array([1.0, 0.5])
                    coord = xp.array([[0.1] + [0] * (ndim - 1),
                                      [1.1] + [0] * (ndim - 1),
                                      [2.1] + [0] * (ndim - 1)])

                    input = xp.array([[0, 1.0, 0]] * batch).reshape(
                        [batch] + shape)
                    output = interp.interpolate(input, width, kernel, coord)
                    output_expected = xp.array([[0.1, 0.9, 0]] * batch)
                    xp.testing.assert_allclose(output, output_expected,
                                               atol=1e-7)

    def test_gridding(self):
        xps = [np]
        if config.cupy_enabled:
            xps.append(cp)

        batch = 2
        for xp in xps:
            for ndim in [1, 2, 3]:
                with self.subTest(ndim=ndim, xp=xp):
                    shape = [3] + [1] * (ndim - 1)
                    width = 2.0
                    kernel = xp.array([1.0, 0.5])
                    coord = xp.array([[0.1] + [0] * (ndim - 1),
                                      [1.1] + [0] * (ndim - 1),
                                      [2.1] + [0] * (ndim - 1)])

                    input = xp.array([[0, 1.0, 0]] * batch)
                    output = interp.gridding(
                        input, [batch] + shape, width, kernel, coord)
                    output_expected = xp.array(
                        [[0, 0.9, 0.1]] * batch).reshape([batch] + shape)
                    xp.testing.assert_allclose(output, output_expected,
                                               atol=1e-7)
back to top