How to use the sigpy.get_device 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 / mri / bloch.py View on Github external
def _exp(b1):
    device = sp.get_device(b1)
    xp = device.xp

    with device:
        alpha = xp.abs(b1)
        phi = xp.angle(b1)

        cos_alpha = xp.cos(alpha / 2)
        sin_alpha = xp.sin(alpha / 2)
        cos_phi = xp.cos(phi)
        sin_phi = xp.sin(phi)

        return xp.array(
            [[cos_alpha, -1j * sin_alpha * cos_phi - sin_alpha * sin_phi],
             [-1j * sin_alpha * cos_phi + sin_alpha * sin_phi, cos_alpha]])
github mikgroup / sigpy / sigpy / mri / bloch.py View on Github external
def to_bloch_vector(input):
    """Convert magnetization array to Bloch vector representation.

    Args:
        input (array): magnetization array.

    Returns:
        m (array): Bloch vector reprsentation of shape [..., 2, 2].

    """
    device = sp.get_device(input)
    xp = device.xp

    with device:
        if is_bloch_vector(input):
            m = input
        elif is_density_matrix(input):
            mx = 2 * input[..., 1, 0].real
            my = 2 * input[..., 1, 0].imag
            mz = input[..., 0, 0] - input[..., 1, 1]
            m = xp.stack([mx, my, mz], axis=-1)
        else:
            raise ValueError('Input is not in either Bloch vector or '
                             'density matrix representation.')

    return m
github mikgroup / sigpy / sigpy / mri / linop.py View on Github external
img_ker = img_ker.reshape((1, ) + img_ker.shape)
    R = sp.linop.Reshape((num_coils, 1) + tuple(mps_ker_shape[1:]),
                         mps_ker_shape)
    C = sp.linop.ConvolveFilter(R.oshape, img_ker,
                                mode='valid', multi_channel=True)
    A = C * R

    if coord is not None:
        num_coils = mps_ker_shape[0]
        grd_shape = [num_coils] + sp.estimate_shape(coord)
        iF = sp.linop.IFFT(grd_shape, axes=range(-ndim, 0))
        N = sp.linop.NUFFT(grd_shape, coord)
        A = N * iF * A

    if weights is not None:
        with sp.get_device(weights):
            P = sp.linop.Multiply(A.oshape, weights**0.5)
        A = P * A

    return A
github mikgroup / sigpy / sigpy / mri / bloch.py View on Github external
def to_density_matrix(input):
    """Convert magnetization array to density matrix.

    Args:
        input (array): magnetization array.

    Returns:
        p (array): density matrix reprsentation of shape [..., 2, 2].

    """
    device = sp.get_device(input)
    xp = device.xp

    with device:
        if is_bloch_vector(input):
            mx, my, mz = input[..., 0], input[..., 1], input[..., 2]
            p = xp.stack([xp.stack([1 + mz, mx - 1j * my], -1),
                          xp.stack([mx + 1j * my, 1 - mz], -1)], -2)
            p /= 2
        elif is_density_matrix(input):
            p = input
        else:
            raise ValueError('Input is not in either Bloch vector or '
                             'density matrix representation.')

    return p
github mikgroup / sigpy / sigpy / mri / espirit.py View on Github external
crop (int): cropping threshold.

    Returns:
        array: ESPIRiT maps of the same shape as ksp.

    References:
        Martin Uecker, Peng Lai, Mark J. Murphy, Patrick Virtue, Michael Elad,
        John M. Pauly, Shreyas S. Vasanawala, and Michael Lustig
        ESPIRIT - An Eigenvalue Approach to Autocalibrating Parallel MRI:
        Where SENSE meets GRAPPA.
        Magnetic Resonance in Medicine, 71:990-1001 (2014)

    """
    img_ndim = ksp.ndim - 1
    num_coils = len(ksp)
    with sp.get_device(ksp):
        # Get calibration region
        calib_shape = [num_coils] + [calib_width] * img_ndim
        calib = sp.resize(ksp, calib_shape)
        calib = sp.to_device(calib, device)

    device = sp.Device(device)
    xp = device.xp
    with device:
        # Get calibration matrix
        kernel_shape = [num_coils] + [kernel_width] * img_ndim
        kernel_strides = [1] * (img_ndim + 1)
        mat = sp.array_to_blocks(calib, kernel_shape, kernel_strides)
        mat = mat.reshape([-1, sp.prod(kernel_shape)])

        # Perform SVD on calibration matrix
        _, S, VH = xp.linalg.svd(mat, full_matrices=False)
github mikgroup / sigpy / sigpy / mri / bloch.py View on Github external
def hard_pulse_rotation(input, b1):
    """Apply hard pulse rotation to input magnetization.

    Args:
        input (array): magnetization array.
        b1 (complex float): complex B1 value in radian.

    Returns:
        array: magnetization array after hard pulse rotation,
            in representation consistent with input.

    """
    p = to_density_matrix(input)

    device = sp.get_device(p)
    with device:
        b1 = sp.to_device(b1, device)
        p = _exp(b1) @ p @ _exp(-b1)

    if is_bloch_vector(input):
        return to_bloch_vector(p)
    else:
        return p
github mikgroup / sigpy / sigpy / mri / app.py View on Github external
def g(x):
            device = sp.get_device(x)
            xp = device.xp
            with device:
                return lamda * xp.sum(xp.abs(x)).item()