def find_k(lam, x, sparsify, unsparsify, N):
    '''Find best k given lambda.'''

    pobj = partial(obj, x=x, lam=lam, unsparsify=unsparsify, norm=False)
    cost = np.zeros(N)
    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))
Exemple #2
0
    recon_sort = np.zeros_like(imspace_u)
    roi_cnt = 0
    for idx in tqdm(np.ndindex((sx, sy)), total=sx*sy, leave=False):
        ii, jj = idx[:]

        # TODO:
        # Mask out small ROIs to do run ordinator in
        if roi[ii, jj]:
            # sord = ordinator1d(
            #     np.abs(imspace_true[ii, jj, :]), k=10,
            #     forward=S.forward_wvlt, inverse=S.inverse_wvlt,
            #     chunksize=10, pdf=None, pdf_metric=None,
            #     sparse_metric=None, disp=False)
            sord = relaxed_ordinator(
                np.abs(imspace_true[ii, jj, :]), lam=.05, k=10,
                unsparsify=S.inverse_wvlt, norm=False, warm=False,
                transform_shape=None, disp=False)
            roi_cnt += 1
            tqdm.write('ROI counter: %d' % roi_cnt)
        else:
            # Sorting doesn't suppress motion!
            sord = np.argsort(np.abs(imspace_true[ii, jj, :]))

        recon_sort[ii, jj, sord] = ptv.tv1_1d( #pylint: disable=E1137
            np.abs(imspace_u[ii, jj, sord]), w)

    plt.plot(np.abs(imspace_true[ctr[0], ctr[1], :]))
    plt.plot(np.abs(recon_sort[ctr[0], ctr[1], :]))
    plt.plot(np.abs(recon_l1[ctr[0], ctr[1], :]))
    plt.show()
Exemple #3
0
    # plt.plot(Si.forward_fd(px.imag))
    # plt.show()
    #
    pi_sortr = np.argsort(pxr)[::-1]
    pi_sorti = np.argsort(pxi)[::-1]

    #
    # plt.plot(-np.sort(-np.abs(Sr.forward_fd(px.real[pi_sortr]))))
    # plt.plot(-np.sort(-np.abs(Si.forward_fd(px.imag[pi_sorti]))))
    # plt.show()

    # Try finding a better one?



    pi_lsr = relaxed_ordinator(
        pxr, lam=.08, k=10, unsparsify=Sr.inverse_dct)
    pi_lsi = relaxed_ordinator(
        pxi, lam=.1, k=13, unsparsify=Si.inverse_dct)


    # LOOK AT IT
    plt.plot(-np.sort(-np.abs(Sr.forward_dct(pxr[pi_sortr]))),
             label='DCT, real')
    plt.plot(-np.sort(-np.abs(Si.forward_dct(pxi[pi_sorti]))),
             label='DCT, imag')

    plt.plot(-np.sort(-np.abs(Sr.forward_dct(pxr[pi_lsr]))), '--',
             label='Lagrangian DCT, real')
    plt.plot(-np.sort(-np.abs(Si.forward_dct(pxi[pi_lsi]))), '--',
             label='Lagrangian DCT, imag')
    plt.legend()
Exemple #4
0
    no_ord_chan1 = sparsify(data[:, 0])
    no_ord_chan1 = no_ord_chan1[np.abs(no_ord_chan1) >= tol]
    no_ord_chan2 = sparsify(data[:, 1])
    no_ord_chan2 = no_ord_chan2[np.abs(no_ord_chan2) >= tol]
    comp_bit_required = (no_ord_chan1.size + no_ord_chan2.size)*16

    # Loop over the chunks, choose threshold of coefficients to throw
    # away
    for data0 in tqdm(np.array_split(
            data, int(data.shape[0]/chunk_size), axis=0),
                      leave=False):

        # Sort the data, both channels and get inverse permutation
        if use_ordinator:
            lam, k = .5, round(chunk_size/3)
            idx1 = relaxed_ordinator(data0[:, 0], lam, k, unsparsify)
            idx2 = relaxed_ordinator(data0[:, 1], lam, k, unsparsify)
        else:
            idx1, idx2 = np.argsort(data0, axis=0).T[:]

        idx1i = inverse_permutation(idx1)
        idx2i = inverse_permutation(idx2)

        # Now we need to do some heavy lifting -- find the ranks!
        ranks1.append(pi2rank(idx1i, method='rank1', iterative=False))
        ranks2.append(pi2rank(idx2i, method='rank1', iterative=False))

        # Do sparsifying transform
        chan1 = sparsify(data0[idx1, 0])
        chan2 = sparsify(data0[idx2, 1])
        # unsparsify(chan1)
Exemple #5
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)
    # 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')
        plt.plot(-np.sort(-np.abs(S.forward_fd(x[pi_sort]))), label='sort(x)')
        plt.plot(-np.sort(-np.abs(S.forward_fd(x[pi_ls]))), label='Lagrangian')
        plt.legend()
        plt.title('Finite Differences')
        plt.show()

    # DCT
    if do_dct:
        pi_sort = np.argsort(x)[::-1]
        # pi_ord = ordinator1d(
Exemple #7
0
    # Now jumble up to get our real signal
    pi = np.random.permutation(np.arange(N))
    x = xpi[pi]

    # Make it noisy if we want that
    if add_noise:
        x += np.random.normal(0, 1, x.shape)

    # # Show the messy x signal
    # plt.plot(x)
    # plt.stem(dct(x, norm='ortho'))
    # plt.show()

    # Find the ordering!
    pi = relaxed_ordinator(x, lam, k, lambda c0: idct(c0, norm='ortho'))

    sh = (1, 3)
    plt.subplot(*sh, 1)
    plt.plot(x)
    plt.title('x[n]')

    plt.subplot(*sh, 2)
    plt.plot(dct(x, norm='ortho'), label='DCT(x[n])')
    plt.plot(dct(x[pi], norm='ortho'), label='DCT(x[pi])')
    plt.plot(dct(np.sort(x), norm='ortho'), label='DCT(sort(x[n]))')
    plt.legend()

    plt.subplot(*sh, 3)
    plt.plot(-np.sort(-np.abs(dct(x, norm='ortho'))), label='DCT(x[n])')
    plt.plot(-np.sort(-np.abs(dct(x[pi], norm='ortho'))), label='DCT(x[pi])')
Exemple #8
0
        # This is actually quite hard to do, since it takes a while
        # to run and there are 3 parameters to tune: lam, k for
        # relaxed_ordinator and alpha for step size...
        # Choose k to be 80% of k_recon_sort2d.
        k = int(.8 * k_recon_sort2d)
        print('k is: %d' % k)
        path = '/examples/cs/reordering/paper/'
        path = ROOT_DIR + path

        if isfile(path + 'pi_real.npy'):
            print('found it!')
            pi_real = np.load(path + 'pi_real.npy')
        else:
            print('Have to compute pi_real!')
            pi_real = relaxed_ordinator(recon_none.real,
                                        lam=.5,
                                        k=k,
                                        unsparsify=unsparsify)
            np.save(path + 'pi_real.npy', pi_real)
            print('done with pi_real')
        # pi_imag = relaxed_ordinator(recon_none.imag, lam=.5, k=k,
        #                             unsparsify=unsparsify)
        # np.save('pi_imag.npy', pi_imag)
        # print('done with pi_imag')
        idx_ls = pi_real + 1j * np.arange(pi_real.size).astype(int)
        lagrangesort = lambda x: idx_ls
        # alpha0 = .05
        recon_ls = proximal_GD(kspace_u,
                               uft.forward_ortho,
                               uft.inverse_ortho,
                               sparsify,
                               unsparsify,