Example #1
0
    def setUp(self):
        # Do for 1 dimensional signal
        self.N = 100
        self.sig1d = np.random.random(self.N)
        self.S1d = Sparsify()

        # Do along 1 dimension of a 3 dimensional signal
        self.sig3d = np.random.random((self.N,)*3)
        self.S3d = Sparsify(axis=1)
Example #2
0
'''Compare monotonic and combinatoric ordering schemes.'''

import numpy as np
import matplotlib.pyplot as plt

from mr_utils.utils import Sparsify, gini, piecewise
from mr_utils.cs import ordinator1d

if __name__ == '__main__':

    S = Sparsify()
    l1 = lambda x0: np.linalg.norm(x0, ord=1)
    sh = (3, 3)

    # plt.subplot(*sh, 1)
    N = 10
    # k = 3 # number of sinusoids
    # n = np.linspace(0, 2*np.pi, N)
    # x = np.sum(
    #     np.sin(n[None, :]/np.random.random(k)[:, None]), axis=0)
    # plt.plot(x)
    # plt.title('x[n]')
    #
    # plt.subplot(*sh, 2)
    # k0 = k
    # idx = ordinator1d(
    #     x, k=k0, forward=S.forward_dct, inverse=S.inverse_dct,
    #     chunksize=10, pdf=None, pdf_metric=None,
    #     sparse_metric=lambda x0: 1/gini(x0), disp=False)
    # # idx = relaxed_ordinator(
    # #     x, lam=.3, k=k0, unsparsify=S.inverse_dct, norm=False,
Example #3
0
        path, ['stcr_recon'])[0][..., skip:-slash, sl0]
    sx, sy, st = imspace_true.shape[:]
    # view(imspace_true)

    # We want to mask out the areas we want, start with a simple
    # circle
    xx = np.linspace(0, sx, sx)
    yy = np.linspace(0, sy, sy)
    X, Y = np.meshgrid(xx, yy)
    radius = 9
    ctr = (128, 130)
    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
Example #4
0
class TestSparsify(unittest.TestCase):
    '''Sanity checks for sparsifying transforms.'''

    def setUp(self):
        # Do for 1 dimensional signal
        self.N = 100
        self.sig1d = np.random.random(self.N)
        self.S1d = Sparsify()

        # Do along 1 dimension of a 3 dimensional signal
        self.sig3d = np.random.random((self.N,)*3)
        self.S3d = Sparsify(axis=1)

    def test_1d_finite_differences(self):
        '''Make sure 1d signals work fine for finite differences.'''
        self.assertTrue(np.allclose(self.S1d.inverse_fd(
            self.S1d.forward_fd(self.sig1d)), self.sig1d))

    def test_1d_dct(self):
        '''Make sure 1d signals work fine for DCT.'''
        self.assertTrue(np.allclose(self.S1d.inverse_dct(
            self.S1d.forward_dct(self.sig1d)), self.sig1d))

    def test_1d_wavelet(self):
        '''Make sure 1d signals work fine for wavelet.'''
        self.assertTrue(np.allclose(self.S1d.inverse_wvlt(
            self.S1d.forward_wvlt(self.sig1d)), self.sig1d))

    def test_nD_1d_finite_differences(self):
        '''FD along 1 dimension of a multidimensional array.'''
        self.assertTrue(np.allclose(self.S3d.inverse_fd(
            self.S3d.forward_fd(self.sig3d)), self.sig3d))

    def test_nD_1d_dct(self):
        '''DCT along 1 dimension of a multidimensional array.'''
        self.assertTrue(np.allclose(self.S3d.inverse_dct(
            self.S3d.forward_dct(self.sig3d)), self.sig3d))

    def test_nD_1d_wavelet(self):
        '''Wavelet along 1 dimension of a multidimensional array.'''
        self.assertTrue(np.allclose(self.S3d.inverse_wvlt(
            self.S3d.forward_wvlt(self.sig3d)), self.sig3d))
Example #5
0
    #     val = np.zeros(x0.shape, dtype=x0.dtype)
    #     for ii in range(st):
    #         val[..., ii] = cdf97_2d_forward(
    #             x0[..., ii], level=lvl)[0]
    #     return val
    # def unsparsify(x0):
    #     '''2d wavelet inverse'''
    #     val = np.zeros(x0.shape, dtype=x0.dtype)
    #     for ii in range(st):
    #         val[..., ii] = cdf97_2d_inverse(x0[..., ii], locs)
    #     return val
    # view(sparsify(imspace_true[..., sl0]))
    # view(unsparsify(sparsify(imspace_true[..., sl0])))

    # Try to do proximal gradient descent using finite differences
    S = Sparsify(axis=-1)
    sparsify = S.forward_fd
    unsparsify = S.inverse_fd
    # sparsify = S.forward_dct
    # unsparsify = S.inverse_dct
    # sparsify = S.forward_wvlt
    # unsparsify = S.inverse_wvlt
    # alpha = 1
    maxiter = 400
    # selective = lambda x_hat, update, cur_iter: select_n(
    #     x_hat, update, cur_iter, tot_iter=maxiter)
    selective = None
    ignore_residual = True
    ignore_mse = False
    thresh_sep = True
    disp = True
Example #6
0
from mr_utils.load_data import load_mat
from mr_utils.utils import Sparsify
from mr_utils import view
from mr_utils.cs import relaxed_ordinator

if __name__ == '__main__':

    filename = 'meas_MID42_CV_Radial7Off_triple_2.9ml_FID242_GROG.mat'
    data = load_mat(filename, key='Image')
    # view(data)

    pt = (133, 130)
    px = data[pt[0], pt[1], :, 0]
    pxr = px.real/np.max(np.abs(px.real))
    pxi = px.imag/np.max(np.abs(px.imag))
    Sr = Sparsify(pxr)
    Si = Sparsify(pxi)

    # plt.plot(px.real)
    # plt.plot(px.imag)
    # plt.title('Real/Imag time curve')
    # plt.show()

    # # Try Finite Differences
    # plt.plot(Sr.forward_fd(px.real))
    # plt.plot(Si.forward_fd(px.imag))
    # plt.show()
    #
    pi_sortr = np.argsort(pxr)[::-1]
    pi_sorti = np.argsort(pxi)[::-1]
Example #7
0
from mr_utils.load_data import load_mat
from mr_utils.cs import relaxed_ordinator
from mr_utils.utils import Sparsify

if __name__ == '__main__':

    # Load data
    x = load_mat('/home/nicholas/Downloads/Temporal_reordering/prior.mat',
                 key='prior')
    print(x.shape)

    rpi = np.zeros(x.shape, dtype=int)
    ipi = np.zeros(x.shape, dtype=int)
    for idx in tqdm(np.ndindex(x.shape[:2]), leave=False):
        ii, jj = idx[0], idx[1]

        xr = x[ii, jj, :].real/np.max(np.abs(x[ii, jj, :].real))
        xi = x[ii, jj, :].imag/np.max(np.abs(x[ii, jj, :].imag))
        Sr = Sparsify(xr)
        Si = Sparsify(xi)

        rpi[ii, jj, :] = relaxed_ordinator(
            xr, lam=.08, k=10, unsparsify=Sr.inverse_fd,
            transform_shape=(xr.size-1,))
        ipi[ii, jj, :] = relaxed_ordinator(
            xi, lam=0.1, k=13, unsparsify=Si.inverse_fd,
            transform_shape=(xi.size-1,))

    np.save('rpi.npy', rpi)
    np.save('ipy.npy', ipi)
    for kk in range(int(N)):
        cost[kk] = pobj(
            sparsify(x[relaxed_ordinator(x,
                                         lam=lam,
                                         k=kk,
                                         unsparsify=unsparsify)]))
    return (np.argmin(cost), np.min(cost))


if __name__ == '__main__':

    # Let's make a signal, any start with a random signal
    N = 70
    np.random.seed(2)
    x = np.random.normal(1, 1, N)
    S = Sparsify(x)

    do_fd = False
    do_dct = True

    # Finite differences
    if do_fd:
        pi_sort = np.argsort(x)[::-1]
        pi_ls = relaxed_ordinator(x,
                                  lam=4,
                                  k=10,
                                  unsparsify=S.inverse_fd,
                                  transform_shape=(x.size - 1, ))

        # Let's look at the results
        plt.plot(-np.sort(-np.abs(S.forward_fd(x))), label='x')
Example #9
0
def fix_axis():
    '''Remove ticks from axis.'''
    frame1 = plt.gca()
    frame1.axes.xaxis.set_visible(False)
    frame1.axes.yaxis.set_visible(False)
    plt.box(False)

if __name__ == '__main__':

    # Create a signal, any signal, for us to work with
    N = 100
    k = 5 # number of sinusoids
    n = np.linspace(0, 2*np.pi, N)
    x = np.sum(
        np.sin(n[None, :]/np.random.random(k)[:, None]), axis=0)
    S = Sparsify()
    l1 = lambda x0: np.linalg.norm(x0, ord=1)
    sh = (3, 3)
    text_loc = (0, 0)

    # Do DCT first
    plt.subplot(*sh, 1)
    plt.plot(x)
    plt.title('x[n]')

    plt.subplot(*sh, 2)
    dx = S.forward_dct(x)
    dxs = S.forward_dct(np.sort(x)[::-1])
    plt.plot(dx, label='DCT(x[n])')
    plt.plot(dxs, label='DCT(sort(x[n]))')
    plt.title('DCT')