How to use the sigpy.util function in sigpy

To help you get started, we’ve selected a few sigpy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mikgroup / sigpy / sigpy / nufft.py View on Github external
os_shape[a] = os_i
            idx = xp.arange(i, dtype=input.dtype)

            # Calculate apodization
            apod = (beta**2 - (np.pi * width * (idx - i // 2) / os_i)**2)**0.5
            apod /= xp.sinh(apod)

            # Swap axes
            output = output.swapaxes(a, -1)
            os_shape[a], os_shape[-1] = os_shape[-1], os_shape[a]

            # Apodize
            output *= apod

            # Oversampled FFT
            output = util.resize(output, os_shape)
            output = fft.fft(output, axes=[-1], norm=None)
            output /= i**0.5

            # Swap back
            output = output.swapaxes(a, -1)
            os_shape[a], os_shape[-1] = os_shape[-1], os_shape[a]

        coord = _scale_coord(backend.to_device(coord, device), input.shape, oversamp)
        table = backend.to_device(
            _kb(np.arange(n, dtype=coord.dtype) / n, width, beta, dtype=coord.dtype), device)

        output = interp.interp(output, width, table, coord)

        return output
github mikgroup / sigpy / tests / test_app.py View on Github external
def test_MaxEig(self):
        n = 5
        mat = util.randn([n, n])
        A = linop.MatMul([n, 1], mat)
        s = np.linalg.svd(mat, compute_uv=False)

        npt.assert_allclose(app.MaxEig(
            A.H * A, max_iter=1000, show_pbar=False).run(), s[0]**2, atol=1e-3)
github mikgroup / sigpy / tests / test_conv.py View on Github external
def test_convolve_valid(self):
        mode = 'valid'
        devices = [backend.cpu_device]
        if config.cupy_enabled:
            devices.append(backend.Device(0))

        for device in devices:
            xp = device.xp
            with device:
                for dtype in [np.float32, np.float64,
                              np.complex64, np.complex128]:
                    x = util.dirac([1, 3], device=device, dtype=dtype)
                    W = xp.ones([1, 3], dtype=dtype)
                    y = backend.to_device(conv.convolve(
                        x, W, mode=mode), backend.cpu_device)
                    npt.assert_allclose(y, [[1]], atol=1e-5)

                    x = util.dirac([1, 3], device=device, dtype=dtype)
                    W = xp.ones([1, 2], dtype=dtype)
                    y = backend.to_device(conv.convolve(
                        x, W, mode=mode), backend.cpu_device)
                    npt.assert_allclose(y, [[1, 1]], atol=1e-5)

                    x = util.dirac([1, 3], device=device, dtype=dtype)
                    W = xp.ones([2, 1, 3], dtype=dtype)
                    y = backend.to_device(
                        conv.convolve(
                            x,
github mikgroup / sigpy / tests / test_util.py View on Github external
def test_dirac(self):
        output = util.dirac([5])
        truth = [0, 0, 1, 0, 0]
        npt.assert_allclose(output, truth)

        output = util.dirac([4])
        truth = [0, 0, 1, 0]
        npt.assert_allclose(output, truth)
github mikgroup / sigpy / sigpy / linop.py View on Github external
def _hstack_params(shapes, axis):
    if axis is None:
        return _hstack_params([[util.prod(shape)] for shape in shapes], 0)

    ishape = list(shapes[0])
    ndim = len(ishape)
    idx = shapes[0][axis]
    indices = []

    for shape in shapes[1:]:
        if len(shape) != ndim:
            raise Exception(
                'Shapes must have the same lengths to concatenate.')

        for i in range(ndim):
            if i == axis:
                ishape[i] += shape[i]
                indices.append(idx)
                idx += shape[i]
github mikgroup / sigpy / sigpy / comm.py View on Github external
def allreduce(self, input):
        if self.size == 1:
            return

        if config.mpi4py_enabled:
            mpi_buffer = util.to_device(input, util.cpu_device)
            self.mpi_comm.Allreduce(MPI.IN_PLACE, mpi_buffer)
            util.move_to(input, mpi_buffer)
github mikgroup / sigpy / sigpy / fft.py View on Github external
def _fftc(input, oshape=None, axes=None, norm='ortho'):

    ndim = input.ndim
    axes = util._normalize_axes(axes, ndim)
    device = backend.get_device(input)
    xp = device.xp

    if oshape is None:
        oshape = input.shape

    with device:
        tmp = input
        tshape = list(input.shape)
        for a in axes:
            i = oshape[a]
            tshape[a] = i

            tmp = tmp.swapaxes(a, -1)
            tshape[a], tshape[-1] = tshape[-1], tshape[a]
github mikgroup / sigpy / sigpy / thresh.py View on Github external
def l2_proj(eps, input, axes=None):
    """Projection onto L2 ball.

    Args:
        eps (float, or array): L2 ball scaling.
        input (array)

    Returns:
        array: Result.

    """
    axes = util._normalize_axes(axes, input.ndim)

    xp = backend.get_array_module(input)
    norm = xp.sum(xp.abs(input)**2, axis=axes, keepdims=True)**0.5
    mask = norm < eps
    output = mask * input + (1 - mask) * (eps * input / (norm + mask))

    return output