Beispiel #1
0
 def test_max_level(self):
     '''Make sure we clip the level at the high end.'''
     with self.assertWarns(Warning):
         high_level = np.random.randint(6, 10)
         forward, _locs = cdf97_2d_forward(self.im, level=high_level)
     forward_check, _locs = cdf97_2d_forward(self.im, level=5)
     self.assertTrue(np.allclose(forward, forward_check))
    # Get the encoding model
    uft = UFT(mask)

    # Load in the test data
    kspace = load_test_data('mr_utils/test_data/tests/recon/reordering',
                            ['coil1'])[0]
    kspace = np.fft.fftshift(kspace)
    imspace = uft.inverse(kspace)

    # Undersample data to get prior
    kspace_u = kspace * mask
    imspace_u = uft.inverse(kspace_u)

    # Sparsifying transforms
    level = 3
    wvlt, locations = cdf97_2d_forward(imspace, level)
    sparsify = lambda x: cdf97_2d_forward(x, level)[0]
    unsparsify = lambda x: cdf97_2d_inverse(x, locations)

    # Decide how we'll be selective in our updates
    percent_to_keep = .05
    num_to_keep = int(percent_to_keep * imspace.size)

    def select_n(x_hat, update):
        '''Return indices of n largest updates each iteration.'''
        return np.unravel_index(
            np.argpartition(np.abs(x_hat - update).flatten(),
                            -num_to_keep)[-num_to_keep:], x_hat.shape)

    # Shared recon params
    prior = imspace_u.copy()
    R = 4  # decimate so the matching will go more quickly
    x = decimate(decimate(camera().astype(float), R, axis=0), R, axis=1)

    # Let's try a few different transforms:
    Ts = []
    Tis = []

    # Discrete cosine transform
    norm = 'ortho'
    Ts.append(lambda x: dct(dct(x, axis=0, norm=norm), axis=1, norm=norm))
    Tis.append(lambda x: idct(idct(x, axis=0, norm=norm), axis=1, norm=norm))
    assert np.allclose(x, Tis[-1](Ts[-1](x)))

    # Wavelet transform
    level = 3
    _, locs = cdf97_2d_forward(x, level)
    Ts.append(lambda x: cdf97_2d_forward(x, level)[0])
    Tis.append(lambda x: cdf97_2d_inverse(x, locs))
    assert np.allclose(x, Tis[-1](Ts[-1](x)))

    # Tuning parameter for each transform and case, k -- percent of coeffs
    nc = int(.2 * x.size)  # number of coefficients to keep for comparison
    ks = [
        [.1, .9],
        [.1, .9],  # k for bulk_up, whittle_down for DCT
        [.1, .9]
    ]  # k for bulk_up, whittle_down for wavelet

    for T, Ti, k in zip(Ts, Tis, ks):
        # Try to "bulk up" the first k percent of coefficients
        idx1 = bulk_up(x, T, Ti, k[0])
Beispiel #4
0
    plt.show()

    # Radial sampling pattern for retrospective undersampling
    num_spokes = 16
    samp = radial(im.shape, num_spokes, skinny=True, extend=True)
    samp_percent = np.sum(samp.flatten()) / samp.size * 100
    uft = UFT(samp)
    kspace_u = np.fft.fft2(im) * samp
    imspace_u = np.fft.ifft2(kspace_u)
    # view(samp)
    # view(imspace_u)
    # view(kspace_u)

    # Use wavelet transform
    lvl = 3
    _coeffs, locs = cdf97_2d_forward(im, lvl)
    sparsify = lambda x0: cdf97_2d_forward(x0, lvl)[0]
    unsparsify = lambda x0: cdf97_2d_inverse(x0, locs)
    assert np.allclose(unsparsify(sparsify(im)), im)

    # Recon params
    ignore_mse = False
    ignore_ssim = True
    ignore_residual = True
    thresh_sep = False
    maxiter = 1000
    disp = True
    strikes = 10

    # Do recon no ordering
    recon = proximal_GD(
Beispiel #5
0
 def test_forward_inverse(self):
     '''Test forward transform invertibility.'''
     forward, locs = cdf97_2d_forward(self.im, level=5)
     inverse = cdf97_2d_inverse(forward, locs)
     self.assertTrue(np.allclose(inverse, self.im))
Beispiel #6
0
if __name__ == '__main__':

    # Get a phantom
    N = 64
    x = binary_smiley(N)

    # Do some sampling
    num_spokes = 12
    mask = radial(x.shape, num_spokes)
    uft = UFT(mask)
    y = uft.forward_ortho(x)

    # Sparsifying transforms
    level = 3
    wvlt, locations = cdf97_2d_forward(x, level)
    sparsify = lambda x0: cdf97_2d_forward(x0, level)[0]
    unsparsify = lambda x0: cdf97_2d_inverse(x0, locations)

    # Do the recon
    alpha = .15
    disp = True
    ignore = False
    maxiter = 200
    x_c = proximal_GD(y,
                      forward_fun=uft.forward_ortho,
                      inverse_fun=uft.inverse_ortho,
                      sparsify=sparsify,
                      unsparsify=unsparsify,
                      reorder_fun=None,
                      alpha=alpha,
def T(x0):
    '''Wavelet transform.'''
    return cdf97_2d_forward(x0, level=5)[0]