示例#1
0
def test():
    # simulated image
    mat_contents = sio.loadmat('data/kellman_data/PKdata3.mat',
                               struct_as_record=False,
                               squeeze_me=True)
    xdata = mat_contents["data"]
    im = xdata.images
    TE = xdata.TE
    field = xdata.FieldStrength
    fat_freq_arr = 42.58 * field * np.array(
        [-3.80, -3.40, -2.60, -1.94, -0.39, 0.60])
    fat_rel_amp = np.array([0.087, 0.693, 0.128, 0.004, 0.039, 0.048])

    ut.plotim3(np.real(im[:, :, :]))
    nx, ny, nte = im.shape
    #undersampling
    #mask  = ut.mask3d( nx, ny, nte, [15,15,0], 0.8)
    #FTm   = opts.FFT2d_kmask(mask)
    FTm = opts.FFT2d()
    b = FTm.forward(im)
    scaling = ut.optscaling(FTm, b)
    b = b / scaling

    #ut.plotim3(mask)
    ut.plotim3(np.absolute(FTm.backward(b)))  #undersampled imag
    #parameters
    xpar = np.zeros((nx, ny, 3), np.complex128)

    # IDEAL and FFT jointly
    IDEAL = idealc.IDEAL_opt2(TE, 217.0, 1.0)  #fat_freq_arr , fat_rel_amp
    Aideal_ftm = opts.joint2operators(IDEAL, FTm)  #(FTm,IDEAL)#
    IDEAL.set_x(xpar)  #should update in each gauss newton iteration
    residual = IDEAL.residual(b, FTm)
    #ut.plotim3(np.absolute(FTm.backward(residual)))
    # wavelet and x+d_x
    #addx        = idealc.x_add_dx()
    #addx.set_x(xpar) #should update in each gauss newton iteration
    #dwt         = opts.DWT2d(wavelet = 'haar', level=4)
    #Adwt_addx   = opts.joint2operators(dwt, addx)

    #do L2 cs mri recon
    Nite = 20  #number of iterations
    ostep = 1.0
    for i in range(20):
        dxpar = pf.prox_l2_Afxnb_CGD2(Aideal_ftm.forward, Aideal_ftm.backward,
                                      residual, Nite)
        if i % 5 == 0:
            ut.plotim3(np.absolute(xpar + ostep * dxpar)[..., 0:2], bar=1)
            ut.plotim3(np.real(xpar + ostep * dxpar)[..., 2], bar=1)
            ut.plotim3(np.imag(xpar + ostep * dxpar)[..., 2], bar=1)
        xpar = xpar + ostep * dxpar  #.astype(np.float64)
        #xpar[:,:,2] = np.real(xpar[:,:,2])

        IDEAL.set_x(xpar)  #should update in each gauss newton iteration
        residual = IDEAL.residual(b, FTm)
        #addx.set_x(xpar) #should update in each gauss newton iteration

    ut.plotim3(np.absolute(xpar)[..., 0:2], bar=1)
示例#2
0
def test():
    ft = opts.FFT2d()
    mat_contents = sio.loadmat(
        '/working/larson/UTE_GRE_shuffling_recon/20170718_voluteer_ir_fulksp/exp2_ir_fulksp/rawdata.mat'
    )
    x = mat_contents["da"].squeeze(axis=0).squeeze(axis=3)
    mask = mat_contents["mask"].squeeze(axis=0).squeeze(axis=3)
    Vim = mat_contents["calib"][40, ...]
    #ut.plotim3(np.absolute(x[:,:,:,0]))
    im = ft.backward(x)
    ut.plotim3(np.absolute(im[:, :, im.shape[2] // 2, :]))
    #get shape
    nx, ny, nc, nd = x.shape
    #create espirit operator
    esp = opts.espirit(Vim)

    #FTm  = opts.FFTnd_kmask(mask)
    FTm = opts.FFTW2d_kmask(mask, threads=5)
    #ut.plotim1(np.absolute(mask))#plot the mask
    Aopt = opts.joint2operators(esp, FTm)
    #create image
    im = FTm.backward(x)
    #ut.plotim3(np.absolute(im[:,:,:]))
    #wavelet operator
    dwt = opts.DWT2d(wavelet='haar', level=4)
    # undersampling in k-space
    b = FTm.forward(im)
    scaling = ut.optscaling(FTm, b)
    b = b / scaling
    #ut.plotim3(np.absolute(Aopt.backward(b))) #undersampled imag

    #do tv cs mri recon
    #Nite = 20 #number of iterations
    #step = 0.5 #step size
    #tv_r = 0.002 # regularization term for tv term
    #rho  = 1.0
    #xopt = solvers.ADMM_l2Afxnb_tvx( Aopt.forward, Aopt.backward, b, Nite, step, tv_r, rho )

    #do wavelet l1 soft thresholding
    Nite = 50  #number of iterations
    step = 1  #step size
    th = 0.1  # theshold level
    xopt = solvers.FIST_3(Aopt.forward, Aopt.backward, dwt.backward,
                          dwt.forward, b, Nite, step, th)

    ut.plotim3(np.absolute(xopt[:, :, :]))
示例#3
0
def test1():
    N = 20
    k1, k2 = nufftfreqs2d(N, N)
    k_c = np.cos(np.multiply(5, k1)) + 1j * np.sin(np.multiply(5, k2))  #
    #k_c       = np.ones((N,N))#
    hwin = hamming2d(N, N)
    #ut.plotgray(np.absolute(hwin))
    # apply hamming window
    k_c = np.multiply(k_c, hwin)
    im = dft2d_warp(N, N, k_c)
    ut.plotim1(np.absolute(im), bar=True)
    #use std fft lib
    ft = opts.FFT2d()
    npim = ft.backward(k_c)
    ut.plotim1(np.absolute(npim), bar=True)
    ut.plotim1(np.absolute(im - npim), bar=True)
    #interpolatation
    im_int = dft2d_warp(5 * N, 5 * N, k_c)
    ut.plotim1(np.absolute(im_int), bar=True)
示例#4
0
def test():
    ft = opts.FFT2d()
    # simulated image
    mat_contents = sio.loadmat('data/brain_32ch.mat');
    x = mat_contents["DATA"]    
    #ut.plotim1(np.absolute(x[:,:,0]))
    im = ft.backward(x[:,:,:])
    ut.plotim3(np.absolute(im[:,:,:]))
    #crop k-space
    xcrop = ut.crop2d( x, 16 )  
    #do espirit2d  
    Vim, sim = espirit_2d(xcrop, x.shape,\
     nsingularv = 150, hkwin_shape = (16,16,16), pad_before_espirit = 0, pad_fact = 2 )
    ut.plotim3(np.absolute(Vim))
    esp = opts.espirit(Vim)
    esp_im = esp.backward(im)
    ut.plotim1(np.absolute(esp_im))
    im_recon = esp.forward(esp_im)
    ut.plotim3(np.absolute(im_recon[:,:,:]))
示例#5
0
def test():
    # simulated image
    mat_contents = sio.loadmat(pathdat, struct_as_record=False, squeeze_me=True)
    xdata        = mat_contents["data"] 
    im           = xdata.images
    field        = xdata.FieldStrength
    b0_gain      = 100.0
    TE           = b0_gain * xdata.TE
    fat_freq_arr = (1.0/b0_gain) * 42.58 * field * np.array([-3.80, -3.40, -2.60, -1.94, -0.39, 0.60])
    fat_rel_amp  = np.array([0.087, 0.693, 0.128, 0.004, 0.039, 0.048])
 
    ut.plotim3(np.real(im[:,:,:]))
    nx,ny,nte  = im.shape
    #undersampling
    mask       = ut.mask3d( nx, ny, nte, [15,15,0], 0.8)
    #FTm   = opts.FFT2d_kmask(mask)
    #FTm        = opts.FFTW2d_kmask(mask)
    FTm   = opts.FFT2d()
    b          = FTm.forward(im)
    scaling    = ut.optscaling(FTm,b)
    b          = b/scaling

    #ut.plotim3(mask)
    ut.plotim3(np.absolute(FTm.backward(b))) #undersampled imag
    #parameters
    xpar        = np.zeros((nx,ny,3), np.complex128)
    #xpar[:,:,0]  = 10*np.ones((nx,ny))
    #ut.plotim3(np.absolute(xpar),[3,-1])
    # IDEAL and FFT jointly
    IDEAL       = idealc.IDEAL_opt2(TE, fat_freq_arr , fat_rel_amp )#fat_freq_arr , fat_rel_amp
    Aideal_ftm  = opts.joint2operators(IDEAL, FTm)#(FTm,IDEAL)#
    IDEAL.set_x(xpar) #should update in each gauss newton iteration
    residual    = IDEAL.residual(b, FTm)
    #ut.plotim3(np.absolute(FTm.backward(residual)))
    # wavelet and x+d_x
    addx_water  = idealc.x_add_dx()
    addx_fat    = idealc.x_add_dx()
    addx_df     = idealc.x_add_dx()
    #addx        = idealc.x_add_dx()
    #addx.set_w([1, 1, 0.0001])
    addx_water.set_x(xpar[...,0]) #should update in each gauss newton iteration
    addx_fat.set_x  (xpar[...,1])
    addx_df.set_x   (xpar[...,2])
    dwt         = opts.DWT2d(wavelet = 'haar', level=4)
    tvop        = tvopc.TV2d()
    Adwt_addx_w = opts.joint2operators(dwt, addx_water)   
    Adwt_addx_f = opts.joint2operators(dwt, addx_fat)
    Adwt_addx_d = opts.joint2operators(tvop, addx_df)   
    #Adwt_addx   = opts.joint2operators(dwt, addx)

    #CGD
    Nite  = 80
    l1_r1 = 0.01
    l1_r2 = 0.01
    def f(xi):
        #return np.linalg.norm(Aideal_ftm.forward(xi)-residual)
        return alg.obj_fidelity(Aideal_ftm, xi, residual) \
        + l1_r1 * alg.obj_sparsity(Adwt_addx_w, xi[...,0])\
        + l1_r1 * alg.obj_sparsity(Adwt_addx_f, xi[...,1])\
        + l1_r2 * alg.obj_sparsity(Adwt_addx_d, xi[...,2])

    def df(xi):
        #return 2*Aideal_ftm.backward(Aideal_ftm.forward(xi)-residual)
        gradall = alg.grad_fidelity(Aideal_ftm, xi, residual)
        gradall[...,0] += l1_r1 * alg.grad_sparsity(Adwt_addx_w, xi[...,0])
        gradall[...,1] += l1_r1 * alg.grad_sparsity(Adwt_addx_f, xi[...,1])
        gradall[...,2] += l1_r2 * alg.grad_sparsity(Adwt_addx_d, xi[...,2])  
        return gradall      

    #do soft thresholding
    #Nite = 20 #number of iterations
    #step = 0.1 #step size
    #th   = 1 # theshold level
    #do tv cs mri recon
    #Nite = 40 #number of iterations
    #step = 1 #step size
    #tv_r = 0.01 # regularization term for tv term
    #rho  = 1.0  
    ostep = 1.0#0.3       
    for i in range(20):
        #wavelet L1 IST
    #    dxpar = solvers.IST_3( Aideal_ftm.forward, Aideal_ftm.backward,\
    #                Adwt_addx.backward, Adwt_addx.forward, residual, Nite, step, th )
        #wavelet L1 ADMM
    #    dxpar = solvers.ADMM_l2Afxnb_l1Tfx( Aideal_ftm.forward, Aideal_ftm.backward, \
    #               Adwt_addx.backward, Adwt_addx.forward, residual, Nite, step, tv_r, rho,15 )
        # TV ADMM
    #    dxpar = solvers.ADMM_l2Afxnb_tvx( Aideal_ftm.forward, Aideal_ftm.backward, residual\
    #    	, Nite, step, tv_r, rho ) 
        # L2 CGD
    #    dxpar = pf.prox_l2_Afxnb_CGD2( Aideal_ftm.forward, Aideal_ftm.backward, residual, rho, Nite )
    #    dxpar = pf.prox_l2_Afxnb_CGD2( Aideal_ftm.forward, Aideal_ftm.backward, residual, Nite )
        # L1 CGD
        dxpar   = alg.conjugate_gradient(f, df, Aideal_ftm.backward(residual), Nite )
        ostep,j = alg.BacktrackingLineSearch(f, df, xpar, dxpar)
        if i%1 == 0:
            ut.plotim3(np.absolute(xpar + ostep*dxpar)[...,0:2],bar=1)
            ut.plotim3(np.real(xpar + ostep*dxpar)[...,2],bar=1)
            ut.plotim3(np.imag(xpar + ostep*dxpar)[...,2],bar=1)
        xpar = xpar + ostep*dxpar#.astype(np.float64)   

        #if i > 1: #unwrapping on frequence
        #    xpar[:,:,2] = np.real(unwrap_freq(np.real(xpar[:,:,2])))\
        #    +1j*(np.imag(xpar[:,:,2]))

        IDEAL.set_x(xpar) #should update in each gauss newton iteration
        residual = IDEAL.residual(b, FTm)
    #    addx.set_x(xpar) #should update in each gauss newton iteration
        addx_water.set_x(xpar[...,0]) #should update in each gauss newton iteration
        addx_fat.set_x  (xpar[...,1])
        addx_df.set_x   (xpar[...,2])
    ut.plotim3(np.absolute(xpar)[...,0:2],bar=1)
    ut.plotim3(np.real(xpar + ostep*dxpar)[...,2],bar=1)
    ut.plotim3(np.imag(xpar + ostep*dxpar)[...,2],bar=1)
    sio.savemat(pathdat + 'IDEAL_CGD_result.mat', {'xpar':xpar, 'residual':residual})
示例#6
0
def test():
    ft = opt.FFT2d()

    # simulated image
    mat_contents = sio.loadmat('data/brain_8ch.mat')
    x = mat_contents["DATA"]

    #ut.plotim1(np.absolute(x[:,:,0]))

    im = ft.backward(x[:, :, :])
    ut.plotim3(np.absolute(im[:, :, :]))

    #shape of x
    nx, ny, nc = x.shape

    xcrop = ut.crop2d(x, 30)

    #ksp = ft.forward(im)
    #ut.plotim1(np.absolute(ksp[:,:]))

    #multidimention tensor as the block hankel matrix
    h = hk.hankelnd_r(xcrop, (16, 16, 1))
    dimh = h.shape
    #flatten the tensor to create a matrix
    hmtx = h.reshape(
        (dimh[0] * dimh[1] * dimh[2], dimh[3], dimh[4], dimh[5])).reshape(
            (dimh[0] * dimh[1] * dimh[2], dimh[3] * dimh[4] * dimh[5]))

    #svd, could try other approaches
    U, s, V = np.linalg.svd(hmtx, full_matrices=False)
    #S = np.diag(s)
    #ut.plotim1(np.absolute(V[:,0:150]).T)#plot V singular vectors
    ut.plot(s)  #plot sigular values
    #invh = np.zeros(x.shape,complex)
    #print h.shape
    #hk.invhankelnd(h,invh,(2,3,1))

    #reshape vn to generate k-space vn tensor
    nsingular = 150  #number of truncated sigular vectors
    vn = V[0:nsingular, :].reshape(
        (nsingular, dimh[3], dimh[4], dimh[5])).transpose((1, 2, 0, 3))

    #zero pad vn, vn matrix of reshaped singular vectors,
    #dims of vn: nx,ny,nsingular,ncoil
    vn = ut.pad2d(vn, nx, ny)
    #plot first singular vecctor Vn[0]
    imvn = ft.forward(vn)
    #ut.plotim3(np.absolute(imvn[:,:,0,:].squeeze()))#spatial feature of V[:,1] singular vector
    sim = 1j * np.zeros((nx, ny))
    Vim = 1j * np.zeros((nx, ny, nc))
    #Uim = 1j*np.zeros((nx,ny,nc))

    for ix in range(nx):
        for iy in range(ny):
            vpix = imvn[ix, iy, :, :].squeeze()
            vpix = np.matrix(vpix).transpose()
            vvH = vpix.dot(vpix.getH())
            U, s, V = np.linalg.svd(vvH, full_matrices=False)
            sim[ix, iy] = s[0]
            Vim[ix, iy, :] = V[0, :].squeeze()
            #Uim[ix,iy,:] = U[:,0].squeeze()

    #plot first eigen vector, eigen value
    ut.plotim3(np.absolute(Vim))
    #ut.plotim3(np.absolute(Uim))
    ut.plotim1(np.absolute(sim))