plt.subplot(1, 3, 2) plt.hist(im.imag.flatten(), density=True) plt.title('Distribution of Imag') plt.xlabel('Variance: %g' % laplace_sample_var(im.imag.flatten())) plt.subplot(1, 3, 3) plt.hist(np.abs(im.flatten()), density=True) plt.title('Distribution of Mag') plt.xlabel('Variance: %g' % exp_sample_var(np.abs(im).flatten())) 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)
roi = (X - ctr[0])**2 + (Y - ctr[1])**2 < radius**2 roi0 = np.tile(roi, (st, 1, 1)).transpose((1, 2, 0)) print('%d curves in roi' % int(np.sum(roi.flatten()))) # view(roi) S = Sparsify() # view(S.forward_dct(imspace_true[ctr[0], ctr[1], :])) # view(imspace_true*roi0) # Undersampling pattern samp0 = np.zeros((sx, sy, st)) desc = 'Making sampling mask' num_spokes = 16 offsets = np.random.randint(0, high=st, size=st) for ii in trange(st, leave=False, desc=desc): samp0[..., ii] = radial( (sx, sy), num_spokes, offset=offsets[ii], extend=True, skinny=False) # view(samp0) # Set up the recon x = imspace_true.copy() ax = (0, 1) uft = UFT(samp0, axes=ax, scale=True) forward = uft.forward_ortho inverse = uft.inverse_ortho y = forward(x) # view(y, log=True) imspace_u = inverse(y) # from mr_utils.cs import SpatioTemporalTVSB # recon, err = SpatioTemporalTVSB(
from mr_utils import view from mr_utils.test_data.phantom import binary_smiley from mr_utils.sim.traj import radial from mr_utils.cs.models import UFT from mr_utils.cs import proximal_GD from mr_utils.utils.wavelet import cdf97_2d_forward, cdf97_2d_inverse from mr_utils.utils.sort2d import sort2d if __name__ == '__main__': # Phantom N = 2**9 x = binary_smiley(N) # Sampling mask and encoding model mask = radial(x.shape, 16, extend=True) uft = UFT(mask) # Sample y = uft.forward_ortho(x) # Decide how we'll be selective in our updates percent_to_keep = .04 num_to_keep = int(percent_to_keep * y.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)
from mr_utils.test_data.phantom import binary_smiley from mr_utils.utils.wavelet import cdf97_2d_forward, cdf97_2d_inverse from mr_utils.sim.traj import radial from mr_utils.cs.models import UFT from mr_utils.cs import proximal_GD from mr_utils import view 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,
N = 64 # x is NxN num_spokes = 16 run = ['monosort', 'lagrangian'] # none is always run to get prior # Need a reasonable numerical phantom x = np.rot90(modified_shepp_logan((N, N, N))[:, :, int(N / 2)]) # view(x) # Sparsifying transform level = 3 wvlt, locations = cdf97_2d_forward(x, level) sparsify = lambda x: cdf97_2d_forward(x, level)[0] unsparsify = lambda x: cdf97_2d_inverse(x, locations) # Do radial golden-angle sampling mask = radial(x.shape, num_spokes, skinny=True, extend=False) uft = UFT(mask) kspace_u = uft.forward_ortho(x) view(kspace_u, fft=True) # # We need to find the best alpha for the no ordering recon # pGD = partial( # proximal_GD, y=kspace_u, forward_fun=uft.forward_ortho, # inverse_fun=uft.inverse_ortho, sparsify=sparsify, # unsparsify=unsparsify, mode='soft', thresh_sep=True, # selective=None, x=x, ignore_residual=False, disp=False, # maxiter=500) # obj = lambda alpha0: compare_mse( # np.abs(x), np.abs(pGD(alpha=alpha0))) # alpha0 = 0.05 # res = minimize(obj, alpha0)
# We want a 2d signal, so let's make a binary smiley face N = 40 smiley = binary_smiley(N) # Make sure it looks alright plt.imshow(smiley) plt.title('Phantom') plt.show() # True signal m = smiley.flatten() # Create undersampling pattern, try golden angle num_spokes = 64 samp = radial(smiley.shape, num_spokes, skinny=True, extend=False) plt.imshow(samp) plt.title('Sampling pattern, %d rays' % num_spokes) plt.show() # Construct undersampled fourier transform matrix print('Started computation of matrix E...') t0 = time() A = np.fft.fftshift(np.fft.fft(np.eye(N, N))) E = np.diag(samp.flatten()).dot(np.kron(A, A)) E /= np.sqrt(np.sum(np.abs(E)**2, axis=0)) s = E.dot(m.conj()) print('Finished computation of matrix E! Took %g seconds' % (time() - t0)) # Look at how incoherent the sampling matrix is (we want it to be eye) plt.imshow(np.abs(E.conj().T.dot(E)))
k = np.sum(np.abs(smiley_fd) > 0) print('delta = %d/%d = %%%g' % (k,smiley_fd.size,k/smiley_fd.size*100)) # plt.plot(smiley_fd) # plt.show() A = np.fft.fftshift( np.fft.fft( np.eye(N,N) ) ) print(A.shape) A = np.kron(A,A) # Create undersampling pattern, try golden angle num_spokes = 16 samp = radial(smiley.shape,num_spokes,skinny=True) # samp = np.ones(samp.shape) # plt.imshow(samp) # plt.show() # Put smiley in kspace kspace = np.fft.fftshift(np.fft.fft2(smiley)) # plt.imshow(np.log(np.abs(kspace))) # plt.show() # plt.plot(np.abs(A.dot(smiley.flatten()))) # plt.plot(np.abs(kspace.flatten())) # plt.show() # assert np.allclose(A.dot(smiley.flatten()),kspace.flatten()) # Sample kspace