def test(): # simulated image mat_contents = sio.loadmat('data/sim_3dmri.mat') x0 = mat_contents["sim_3dmri"] x = x0[:, :, 40:60] nx, ny, nz = x.shape mask = ut.mask3d(nx, ny, nz, [15, 15, 0]) FTm = opts.FFTnd_kmask(mask) #ut.plotim3(np.absolute(mask[:,:,1:10]))#plot the mask # undersampling in k-space b = FTm.forward(x) ut.plotim3(np.absolute(FTm.backward(b))) #undersampled imag scale = scaling(FTm, b) #b = b/scale #do cs mri recon Nite = 2 #number of iterations step = 0.5 #step size #th = 1 #threshold #xopt = solvers.IST_2(FTm.forward,FTm.backward,b, Nite, step,1) #soft thresholding xopt = solvers.ADMM_l2Afxnb_tvx(FTm.forward, FTm.backward, b, Nite, step, 10, 1) #xopt = solvers.ADMM_l2Afxnb_l1x_2( FTm.forward, FTm.backward, b, Nite, step, 100, 1 ) ut.plotim3(np.absolute(xopt))
def test(): # simulated image mat_contents = sio.loadmat('data/sim_2dmri.mat') im = mat_contents["sim_2dmri"] #im = spimg.zoom(xorig, 0.4) #plotim2(im) #dwt = opts.DWTnd( wavelet = 'haar', level = 2, axes = (0, 1)) dwt = opts.DWT2d(wavelet='haar', level=4) nx, ny = im.shape mask = ut.mask2d(nx, ny, center_r=15) FTm = opts.FFT2d_kmask(mask) ut.plotim1(np.absolute(mask)) #plot the mask # undersampling in k-space b = FTm.forward(im) scaling = ut.optscaling(FTm, b) b = b / scaling ut.plotim1(np.absolute(FTm.backward(b))) #undersampled imag #do soft thresholding Nite = 100 #number of iterations step = 1 #step size th = 1.5 # theshold level #xopt = solvers.IST_2(FTm.forward, FTm.backward, b, Nite, step,th) xopt = solvers.IST_3(FTm.forward, FTm.backward, dwt.backward, dwt.forward, b, Nite, step, th) ut.plotim1(np.absolute(xopt))
def test(): # simulated image mat_contents = sio.loadmat('data/sim_2dmri.mat') xorig = mat_contents["sim_2dmri"] im = spimg.zoom(xorig, 0.4) #plotim2(x) nx, ny = im.shape #create undersampling mask k = int(round(nx * ny * 0.5)) #undersampling ri = np.random.choice(nx * ny, k, replace=False) #index for undersampling ma = np.zeros(nx * ny) #initialize an all zero vector ma[ri] = 1 #set sampled data points to 1 maskdiag = np.diag(ma) #mask is a diagonal matrix np.delete(maskdiag, ri, 0) #remove the all-zero rows in mask matrix #2d dct = kron(1ddct,1ddct) aa = spfft.dct(np.identity(nx), norm='ortho', axis=0) bb = spfft.dct(np.identity(ny), norm='ortho', axis=0) A = np.kron(aa, bb) #2d dct #apply mask to FT operator, Ax = b A = maskdiag * A b = A.dot(x.flatten()) # define A and invA fuctions, i.e. A(x) = b, invA(b) = x #do soft thresholding Nite = 50 #number of iterations step = 0.1 #step size th = 0.1 # theshold level Xopt = solvers.IST_1(A, b, Nite, step, th) plotim1(np.absolute(Xopt.reshape((nx, ny))))
def test(): # simulated image mat_contents = sio.loadmat('data/brain_32ch.mat'); x = mat_contents["DATA"] #mask = mat_contents["mask_randm_x3"].astype(np.float) nx,ny,nc = x.shape #crop k-space xcrop = ut.crop2d( x, 16 ) if 0:#do espirit Vim, sim = espirit_2d(xcrop, x.shape,\ nsingularv = 150, hkwin_shape = (16,16,16), pad_before_espirit = 0, pad_fact = 2 ) #coil map ut.plotim3(np.absolute(Vim),[4,-1],bar = 1) ut.plotim1(np.absolute(sim),bar = 1) #create espirit operator esp = opts.espirit(Vim) esp.save('../save_data/espirit_data_2d.mat') #esp.save('/working/larson/UTE_GRE_shuffling_recon/python_test/save_data/espirit_data_2d.mat') else: esp = opts.espirit() esp.restore('../save_data/espirit_data_2d.mat') #esp.restore('/working/larson/UTE_GRE_shuffling_recon/python_test/save_data/espirit_data_2d.mat') #create mask mask = ut.mask2d( nx, ny, center_r = 15, undersampling = 0.25 ) #FTm = opts.FFT2d_kmask(mask) FTm = opts.FFTW2d_kmask(mask) #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.plotim1(np.absolute(Aopt.backward(b))) #undersampled imag #do cs mri recon Nite = 40 #number of iterations step = 0.5 #step size tv_r = 0.002 # regularization term for tv term rho = 1.0 #th = 1 #threshold #xopt = solvers.IST_2(FTm.forward,FTm.backward,b, Nite, step,th) #soft thresholding xopt = solvers.ADMM_l2Afxnb_tvx( Aopt.forward, Aopt.backward, b, Nite, step, tv_r, rho) #xopt = solvers.ADMM_l2Afxnb_l1x_2( FTm.forward, FTm.backward, b, Nite, step, 100, 1 ) ut.plotim3(np.absolute(xopt))
def test(): # simulated image mat_contents = sio.loadmat('data/sim_2dmri.mat') im = mat_contents["sim_2dmri"] #plotim2(im) nx, ny = im.shape #create undersampling mask k = int(round(nx * ny * 0.5)) #undersampling ri = np.random.choice(nx * ny, k, replace=False) #index for undersampling ma = np.zeros(nx * ny) #initialize an all zero vector ma[ri] = 1 #set sampled data points to 1 mask = ma.reshape((nx, ny)) cx = np.int(nx / 2) cy = np.int(ny / 2) cxr = np.arange(round(cx - 15), round(cx + 15 + 1)) cyr = np.arange(round(cy - 15), round(cy + 15 + 1)) mask[np.ix_(map(int, cxr), map(int, cyr))] = np.ones( (cxr.shape[0], cyr.shape[0])) #center k-space is fully sampled # define A and invA fuctions, i.e. A(x) = b, invA(b) = x def Afunc(image): ksp = np.fft.fft2(image) ksp = np.fft.fftshift(ksp, (0, 1)) return np.multiply(ksp, mask) def invAfunc(ksp): ksp = np.fft.ifftshift(ksp, (0, 1)) im = np.fft.ifft2(ksp) return im plotim1(np.absolute(mask)) b = Afunc(im) plotim1(np.absolute(b)) plotim1(np.absolute(invAfunc(b))) #do soft thresholding Nite = 80 #number of iterations step = 1 #step size th = 1000 # theshold level #xopt = solvers.IST_2(Afunc,invAfunc,x,b, Nite, step,th) #xopt = solvers.ADMM_l2Afxnb_l1x( Afunc, invAfunc, b, Nite, step, 100, 1 ) xopt = solvers.ADMM_l2Afxnb_l1x_2(Afunc, invAfunc, b, Nite, step, 100, 1) plotim1(np.absolute(xopt))
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[:, :, :]))
def test(): # simulated image mat_contents = sio.loadmat('data/brain_32ch.mat') x = mat_contents["DATA"] #mask = mat_contents["mask_randm_x3"].astype(np.float) nx, ny, nc = x.shape #crop k-space xcrop = ut.crop2d(x, 16) if 0: #do espirit Vim, sim = espirit_2d(xcrop, x.shape,\ nsingularv = 150, hkwin_shape = (16,16,16), pad_before_espirit = 0, pad_fact = 2 ) #coil map ut.plotim3(np.absolute(Vim), [4, -1], bar=1) ut.plotim1(np.absolute(sim), bar=1) #create espirit operator esp = opts.espirit(Vim) esp.save('../save_data/espirit_data_2d.mat') else: esp = opts.espirit() esp.restore('../save_data/espirit_data_2d.mat') #create mask mask = ut.mask2d(nx, ny, center_r=15, undersampling=0.25) FTm = opts.FFT2d_kmask(mask) 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.plotim1(np.absolute(Aopt.backward(b))) #undersampled imag #do soft thresholding Nite = 50 #number of iterations step = 1 #step size th = 0.1 # theshold level #xopt = solvers.IST_2(FTm.forward, FTm.backward, b, Nite, step,th) xopt = solvers.FIST_3(Aopt.forward, Aopt.backward, dwt.backward, dwt.forward, b, Nite, step, th) ut.plotim3(np.absolute(xopt))
def ReconstructADMM_2D(fullysampled_kdata, mask, iterations=10, step=0.05, tv_r=0.005, rho=1.0, is_show=True): fullysampled_kdata = fullysampled_kdata[..., np.newaxis] FTm = opts.FFTW2d_kmask(mask) esp = opts.espirit(sensitivity=np.ones_like(fullysampled_kdata)) Aopt = opts.joint2operators(esp, FTm) im = FTm.backward(fullysampled_kdata) dwt = opts.DWT2d(wavelet='haar', level=4) # undersampling in k-space b = FTm.forward(im) scaling = ut.optscaling(FTm, b) b = b / scaling # do cs mri recon Nite = iterations # number of iterations step = step # step size tv_r = tv_r # regularization term for tv term rho = rho # th = 1 # threshold # xopt = solvers.IST_2(FTm.forward,FTm.backward,b, Nite, step,th) #soft thresholding xopt = solvers.ADMM_l2Afxnb_tvx(Aopt.forward, Aopt.backward, b, Nite, step, tv_r, rho, is_show=is_show) # xopt = solvers.ADMM_l2Afxnb_l1x_2( FTm.forward, FTm.backward, b, Nite, step, 100, 1 ) # ut.plotim3(np.absolute(xopt)) return xopt
def test1(): #ft = opts.FFTnd() #mat_contents = sio.loadmat(pathdat + 'rawdata2.mat'); #x = mat_contents["dataall"][...,0,11:13].astype(np.complex64)#.squeeze(axis = 4) #Vim = mat_contents["calib"].astype(np.complex64) #mask = mat_contents["maskn"][...,0,11:13].astype(np.complex64)#.squeeze(axis = 4) mat_contents = h5py.File(pathdat + 'rawdata.mat') x = mat_contents['dataall'][:].transpose( [5, 4, 3, 2, 1, 0]).squeeze(axis=4).view(np.complex128).astype(np.complex64) Vim = mat_contents["calib"][:].transpose([3, 2, 1, 0]).view( np.complex128).astype(np.complex64) mask = mat_contents["maskn"][:].transpose([5, 4, 3, 2, 1, 0]).squeeze(axis=4) for dd in range(x.shape[-1]): esp = opts.espirit(Vim) #FTm = opts.FFTnd_kmask(mask...,dd]) #FTm = opts.FFTWnd_kmask(mask[...,dd], threads = 15) FTm = cuopts.FFTnd_cuda_kmask(mask[..., dd]) Aopt = opts.joint2operators(esp, FTm) #wavelet operator dwt = opts.DWTnd(wavelet='db2', level=4, axes=(0, 1, 2)) # undersampling in k-space b = x[..., dd] scaling = ut.optscaling(Aopt, b) b = b / scaling #do wavelet l1 soft thresholding xopt = scaling * solvers.FIST_3(Aopt.forward, Aopt.backward, dwt.backward, dwt.forward, b, Nite=25, step=0.5, th=0.1) sio.savemat(pathdat + 'mripy_recon_l1wavelet' + str(dd) + '.mat', {'xopt': xopt})
def test(): # simulated image mat_contents = sio.loadmat('data/sim_2dmri.mat') x = mat_contents["sim_2dmri"] nx, ny = x.shape mask = ut.mask2d(nx, ny, center_r=15) FTm = opts.FFT2d_kmask(mask) ut.plotim1(np.absolute(mask)) #plot the mask # undersampling in k-space b = FTm.forward(x) ut.plotim1(np.absolute(FTm.backward(b))) #undersampled imag #do cs mri recon Nite = 20 #number of iterations step = 0.5 #step size #th = 1 #threshold #xopt = solvers.IST_2(FTm.forward,FTm.backward,b, Nite, step,th) #soft thresholding xopt = solvers.ADMM_l2Afxnb_tvx(FTm.forward, FTm.backward, b, Nite, step, 10, 1) #xopt = solvers.ADMM_l2Afxnb_l1x_2( FTm.forward, FTm.backward, b, Nite, step, 100, 1 ) ut.plotim1(np.absolute(xopt))
def test(): #path = '/home/pcao/3d_recon/' #matfile = 'Phantom_res256_256_20.mat' #phantom data #path = '/working/larson/UTE_GRE_shuffling_recon/UTEcones_recon/20170301/scan_1_phantom/' #matfile = 'Phantom_utecone.mat' #lung data path = '/working/larson/UTE_GRE_shuffling_recon/UTEcones_recon/20170301/lung_exp4_no_prep/' matfile = 'lung_utecone.mat' mat_contents = sio.loadmat(path + matfile) ktraj = mat_contents["ktraj"] dcf = mat_contents["dcf"] kdata = mat_contents["kdata"].astype(np.complex64) ncoils = kdata.shape[3] #bart nufft assumes the im_shape is weighted on ktraj, so I can extract this info here im_shape = [ 2 * int(np.max(ktraj[0])), 2 * int(np.max(ktraj[1])), 2 * int(np.max(ktraj[2])) ] # remove the weighting of im_shape from ktraj ktraj[0, :] = ktraj[0, :] * (1.0 / im_shape[0]) ktraj[1, :] = ktraj[1, :] * (1.0 / im_shape[1]) ktraj[2, :] = ktraj[2, :] * (1.0 / im_shape[2]) #reshape the kdata, flatten the xyz dims kdata = kdata.reshape((np.prod(kdata.shape[0:3]), ncoils)).squeeze() #call nufft3d here nft = cuoptc.NUFFT3d_cuda(im_shape, dcf) #nft = optc.NUFFT3d(im_shape, dcf) nft.normalize_set_ktraj(ktraj) ft = opts.FFTnd() im = nft.backward(kdata) x = ft.forward(im) ut.plotim3(np.absolute(im[:, :, :, 1]), pause_close=5) #get shape #nx,ny,nz,nc = x.shape #crop k-space xcrop = ut.crop3d(x, 12) if 0: #do espirit Vim, sim = espirit_3d(xcrop, x.shape, 500, hkwin_shape = (12,12,12),\ pad_before_espirit = 0, pad_fact = 2, sigv_th = 0.001, nsigv_th = 0.2 ) #coil map #ut.plotim3(np.absolute(Vim[:,:,im.shape[2]//2,:]),bar = 1) #ut.plotim3(np.absolute(sim),bar = 1) #create espirit operator esp = opts.espirit(Vim) #esp.save('../save_data/espirit_data_3d.mat') esp.save(path + 'espirit_data_3d.mat') else: esp = opts.espirit() #esp.restore('../save_data/espirit_data_3d.mat') esp.restore(path + 'espirit_data_3d.mat') #ut.plotim1(np.absolute(mask))#plot the mask Aopt = opts.joint2operators(esp, nft) #wavelet operator dwt = opts.DWTnd(wavelet='haar', level=4) # scaling = ut.optscaling(Aopt, kdata) kdata = kdata / scaling #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, kdata, Nite, step, tv_r, rho ) #do wavelet l1 soft thresholding Nite = 40 #number of iterations step = 0.1 #step size th = 0.06 # theshold level #xopt = solvers.IST_2( Aopt.forward, Aopt.backward, kdata, Nite, step, th ) #xopt = solvers.IST_22( Aopt.forward_backward, Aopt.backward, kdata, Nite, step, th ) #xopt = solvers.FIST_3( Aopt.forward, Aopt.backward, dwt.backward, dwt.forward, kdata, Nite, step, th ) #xopt = solvers.FIST_32( Aopt.forward_backward, Aopt.backward, dwt.backward, dwt.forward, kdata, Nite, step, th ) xopt = solvers.FIST_wrap(Aopt, dwt, kdata, Nite, step, th) #xopt = solvers.IST_wrap( Aopt, dwt, kdata, Nite, step, th ) ut.plotim3(np.absolute(xopt[:, :, :]), pause_close=5) sio.savemat(path + 'test_im_th0p06.mat', {'xopt': xopt})
def test(): # simulated image mat_contents = sio.loadmat('data/sim_2dmri.mat'); x = mat_contents["sim_2dmri"] #x = spimg.zoom(xorig, 0.4) #plotim2(x) nx,ny = x.shape #create undersampling mask k = int(round(nx*ny*0.5)) #undersampling ri = np.random.choice(nx*ny,k,replace=False) #index for undersampling ma = np.zeros(nx*ny) #initialize an all zero vector ma[ri] = 1 #set sampled data points to 1 mask = ma.reshape((nx,ny)) # define A and invA fuctions, i.e. A(x) = b, invA(b) = x def Afunc(im): ksp = np.fft.fft2(im) ksp = np.fft.fftshift(ksp,(0,1)) return np.multiply(ksp,mask) def invAfunc(ksp): ksp = np.fft.ifftshift(ksp,(0,1)) im = np.fft.ifft2(ksp) return im # center k-space index range cx = np.int(nx/2) cy = np.int(ny/2) cxr = np.arange(round(cx-15),round(cx+15+1)) cyr = np.arange(round(cy-15),round(cy+15+1)) mask[np.ix_(map(int,cxr),map(int,cyr))] = np.ones((cxr.shape[0],cyr.shape[0])) #center k-space is fully sampled plotim1(np.absolute(mask))#plot the mask # undersampling in k-space b = Afunc(x) #plotim1(np.absolute(b)) plotim1(np.absolute(invAfunc(b))) #undersampled imag #tv_x = tv.grad(x) #plotim1(np.absolute(tv.grad(x)[:,:,0])) #plotim1(np.absolute(tv.grad(x)[:,:,1])) #test tv denoising #y = np.absolute(invAfunc(b))#input image for tv #lambda_tv = 10#||f-y||+lambda*TV #f = pf.prox_tv2d(y, lambda_tv ) #plotim1(np.absolute(f)) #do cs mri recon Nite = 20 #number of iterations step = 1 #step size #th = 1000 # theshold level #xopt = solvers.IST_2(Afunc,invAfunc,b, Nite, step,th) #soft thresholding xopt = solvers.ADMM_l2Afxnb_tvx( Afunc, invAfunc, b, Nite, step, 10, 1 ) #xopt = solvers.ADMM_l2Afxnb_l1x_2( Afunc, invAfunc, b, Nite, step, 100, 1 ) plotim1(np.absolute(xopt))
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 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 = idealc.x_add_dx() addx.set_x(xpar) #addx.set_w([1, 1, 0.0001]) dwt = opts.DWT2d(wavelet='haar', level=4) Adwt_addx = opts.joint2operators(dwt, addx) #do soft thresholding #Nite = 200 #number of iterations #step = 0.01 #step size #th = 0.02 # theshold level #do tv cs mri recon Nite = 10 #number of iterations step = 1 #step size l1_r = 0.001 tv_r = 0.0001 # regularization term for tv term rho = 1.0 ostep = 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, l1_r, rho, 200 ) # TV ADMM # dxpar = solvers.ADMM_l2Afxnb_tvx( Aideal_ftm.forward, Aideal_ftm.backward, residual\ # , Nite, step, tv_r, rho, 15 ) dxpar = solvers.ADMM_l2Afxnb_tvTfx( Aideal_ftm.forward, Aideal_ftm.backward, \ addx.backward, addx.forward, residual, Nite, step, l1_r, rho, 200 ) # 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 ) if i % 1 == 0: ut.plotim3(np.absolute(xpar + ostep * dxpar)[..., 0:2], bar=1) ut.plotim3(b0_gain * 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) 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 sio.savemat('data/kellman_data/xpar.mat', {'xpar': xpar}) ut.plotim3(np.absolute(xpar)[..., 0:2], bar=1)
def test(): ft = opts.FFTnd() mat_contents = sio.loadmat( '/working/larson/UTE_GRE_shuffling_recon/brain_mt_recon_20160919/brain_3dMRI_32ch.mat' ) x = mat_contents["DATA"] #ut.plotim3(np.absolute(x[:,:,:,0])) im = ft.backward(x) #ut.plotim3(np.absolute(im[:,:,im.shape[2]//2,:])) #get shape nx, ny, nz, nc = x.shape #crop k-space xcrop = ut.crop3d(x, 12) if 1: #do espirit Vim, sim = espirit_3d(xcrop, x.shape, 150, hkwin_shape = (12,12,12),\ pad_before_espirit = 0, pad_fact = 2) #coil map #ut.plotim3(np.absolute(Vim[:,:,im.shape[2]//2,:]),bar = 1) #ut.plotim3(np.absolute(sim),bar = 1) #create espirit operator esp = opts.espirit(Vim) #esp.save('../save_data/espirit_data_3d.mat') esp.save( '/working/larson/UTE_GRE_shuffling_recon/python_test/save_data/espirit_data_3d.mat' ) else: esp = opts.espirit() #esp.restore('../save_data/espirit_data_3d.mat') esp.restore( '/working/larson/UTE_GRE_shuffling_recon/python_test/save_data/espirit_data_3d.mat' ) #create mask mask = ut.mask3d(nx, ny, nz, [15, 15, 0]) #FTm = opts.FFTnd_kmask(mask) FTm = opts.FFTWnd_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.DWTnd(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 ) xopt = solvers.ADMM_l2Afxnb_l1Tfx(Aopt.forward, Aopt.backward, dwt.backward, dwt.forward, b, Nite, step, tv_r, rho) #do wavelet l1 soft thresholding #Nite = 50 #number of iterations #step = 1 #step size #th = 0.4 # theshold level #xopt = solvers.FIST_3( Aopt.forward, Aopt.backward, dwt.backward, dwt.forward, b, Nite, step, th ) ut.plotim3(np.absolute(xopt[:, :, :]))
def test(): # simulated image #mat_contents = sio.loadmat('/data/larson/brain_uT2/2016-09-13_3T-volunteer/ute_32echo_random-csreconallec_l2_r0p01.mat', struct_as_record=False, squeeze_me=True) #datpath = '/data/larson/brain_uT2/2016-12-19_7T-volunteer/' # datpath = '/data/larson/brain_uT2/2016-09-13_3T-volunteer/' f = h5py.File(datpath + 'ute_32echo_random-csreconallec_l2_r0p01.mat') #datpath = '/data/larson/brain_uT2/2017-11-17_3T-DTI-volunteer/' #f = h5py.File(datpath+'P31232_ir4echo.7-csreconallec_l2_r0p01.mat') #im3d = f['imallplus'][0:10].transpose([1,2,3,0]) #im = im3d[:,40,:,:].squeeze().view(np.complex128) Ndiv = 4 im3d = f['imallplus'][0:Ndiv].transpose([1, 3, 2, 0]) im = im3d[35, :, :, :].squeeze().view(np.complex128) b0_gain = 1000.0 TE = b0_gain * 1e-6 * f['TE'][0][0:Ndiv] #TE = TEi[0:Ndiv] field = 3.0 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]) print(1000 / b0_gain * TE) #ut.plotim3(np.absolute(im[:,:,-10:-1]),[4,-1]) nx, ny, nte = im.shape if 0: nte = nte - 1 im = 1j * np.zeros((nx, ny, Ndiv)) for nd in range(Ndiv): im[:, :, nd] = (imi[:, :, nd] - imi[:, :, nd + 1]) / (TEi[nd] - TEi[nd + 1]) print(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.scaling(im) im = im / scaling ut.plotim3(np.absolute(im[:, :, :]), [4, -1], bar=1, pause_close=2) #ut.plotim3(mask) #ut.plotim3(np.absolute(FTm.backward(b))) #undersampled imag #parameters xpar = np.zeros((nx, ny, 4), np.complex128) #xpar[:,:,0] = 10*np.ones((nx,ny)) #ut.plotim3(np.absolute(xpar),[3,-1]) # IDEAL and FFT jointly IDEAL = idealc.IDEAL_fatmyelin_opt2( TE, fat_freq_arr, fat_rel_amp) #fat_freq_arr , fat_rel_amp Aideal_ftm = IDEAL #opts.joint2operators(IDEAL, FTm)#(FTm,IDEAL)# IDEAL.set_x(xpar) #should update in each gauss newton iteration residual = IDEAL.residual(im) #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_dfwater = idealc.x_add_dx() addx_dffat = idealc.x_add_dx() addx = idealc.x_add_dx() addx.set_x(xpar) #addx.set_w([0.01, 0.01, 0.0001]) #addx_water.set_x (xpar[...,0]) #should update in each gauss newton iteration #addx_fat.set_x (xpar[...,1]) #addx_dfwater.set_x (xpar[...,2]) #addx_dffat.set_x (xpar[...,3]) dwt = opts.DWT2d(wavelet='haar', level=4) tvop = tvopc.TV2d_r() #Adwt_addx_w = opts.joint2operators(tvop, addx_water) #Adwt_addx_f = opts.joint2operators(tvop, addx_fat) #Adwt_addx_dwat = opts.joint2operators(tvop, addx_dfwater) #Adwt_addx_dfat = opts.joint2operators(tvop, addx_dffat) Adwt_addx = opts.joint2operators(dwt, addx) #CGD #Nite = 400 #l1_r1 = 0.01 #l1_r2 = 0.01 #l1_r3 = 0.01 #l1_r4 = 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_r2 * alg.obj_sparsity(Adwt_addx_f, xi[...,1])\ # + l1_r3 * alg.obj_sparsity(Adwt_addx_dwat, xi[...,2])\ # + l1_r4 * alg.obj_sparsity(Adwt_addx_dfat, xi[...,3]) #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_r2 * alg.grad_sparsity(Adwt_addx_f, xi[...,1]) # gradall[...,2] += l1_r3 * alg.grad_sparsity(Adwt_addx_dwat, xi[...,2]) # gradall[...,3] += l1_r4 * alg.grad_sparsity(Adwt_addx_dfat, xi[...,3]) # return gradall #do soft thresholding Nite = 40 #number of iterations step = 0.001 #step size th = 0.001 # theshold level #do tv cs mri recon #Nite = 20 #number of iterations #step = 1 #step size #tv_r = 0.001 # regularization term for tv term #rho = 1.0 ostep = 0.6 for i in range(40): #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,25 ) # TV ADMM # dxpar = solvers.ADMM_l2Afxnb_tvx( Aideal_ftm.forward, Aideal_ftm.backward, residual\ # , Nite, step, tv_r, rho ) # dxpar = solvers.ADMM_l2Afxnb_tvTfx( Aideal_ftm.forward, Aideal_ftm.backward, \ # addx.backward, addx.forward, residual, Nite, step, tv_r, rho,25) # 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 = pf.prox_l2_Afxnb_CGD2( IDEAL.forward, IDEAL.backward, residual, Nite ) # dxpar = alg.conjugate_gradient(f, df, Aideal_ftm.backward(residual), Nite ) # ostep,j = alg.BacktrackingLineSearch(f, df, xpar, dxpar) if i % 1 == 0: nxpar = xpar + ostep * dxpar nxpar[..., 1] = 10 * nxpar[..., 1] ut.plotim3(np.absolute(nxpar)[..., 0:2], colormap='viridis', bar=1, vmin=0, vmax=1, pause_close=2) ut.plotim3(b0_gain * np.real(nxpar)[..., 2], colormap='viridis', bar=1, pause_close=2) ut.plotim3(b0_gain * np.imag(nxpar)[..., 2], colormap='viridis', bar=1, pause_close=2) ut.plotim3(b0_gain * np.real(nxpar)[..., 3], colormap='viridis', bar=1, pause_close=2) ut.plotim3(b0_gain * np.imag(nxpar)[..., 3], colormap='viridis', bar=1, pause_close=2) #sio.savemat(datpath + 'cs_ideal_fitting/cs_IDEAL_ADMM_dyn8.mat', {'xpar': nxpar, 'residual': residual}) xpar = xpar + ostep * dxpar #.astype(np.float64) if i > 1: #fix the frequence offset to be equal for two components freq_ave = 0.5 * np.real(xpar[:, :, 2]) + 0.5 * np.real(xpar[:, :, 3]) xpar[:, :, 2] = freq_ave + 1j * (np.imag(xpar[:, :, 2])) xpar[:, :, 3] = freq_ave + 1j * (np.imag(xpar[:, :, 3])) IDEAL.set_x(xpar) #should update in each gauss newton iteration residual = IDEAL.residual(im) ut.plotim3(np.absolute(residual), [4, -1], bar=1, pause_close=2) sio.savemat('../save_data/myelin/ideal_result.mat', \ {'xpar':xpar}) 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_dfwater.set_x(xpar[...,2]) #addx_dffat.set_x (xpar[...,3]) 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) ut.plotim3(np.real(xpar + ostep * dxpar)[..., 3], bar=1) ut.plotim3(np.imag(xpar + ostep * dxpar)[..., 3], bar=1)