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/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) #this set the size of data dwt = opts.DWT2d(wavelet='haar', level=4) #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 xpar = ADMM_l2Agaussnewton_l1Tfx(IDEAL, FTm, dwt, b, Nite, step, l1_r, rho) #xpar = ADMM_l2Aguassnewton_tvx(IDEAL, FTm, b, Nite, step, tv_r, rho ) #xpar = ADMM_l2Agaussnewton_l1Tfx_tvx( IDEAL, FTm, dwt, b, Nite, step, l1_r, tv_r, rho) ut.plotim3(np.absolute(xpar)[..., 0:2], bar=1) ut.plotim3(b0_gain * np.real(xpar)[..., 2], bar=1) ut.plotim3(b0_gain * 2.0 * np.pi * np.imag(xpar)[..., 2], bar=1)
def test(): # read rf and tr arrays from mat file mat_contents = sio.loadmat(pathdat + 'mrf_t1t2b0pd_mrf_randphasecyc_traintest.mat') far = np.array(mat_contents["rf"].astype(np.complex128).squeeze()) trr = np.array(mat_contents["trr"].astype(np.float64).squeeze()) # input MRF time courses mat_contents2 = sio.loadmat(pathdat + 'datax1.mat') data_x = np.array(mat_contents2["datax1"]).astype(np.float64) # prepare for sequence simulation, y->x_hat Nk = far.shape[0] Nexample = data_x.shape[0] ti = 10 #ms M0 = np.array([0.0, 0.0, 1.0]).astype(np.float64) #image size nx = 217 ny = 181 # mask in ksp mask = ut.mask3d(nx, ny, Nk, [15, 15, 0], 0.4) #FTm = opts.FFT2d_kmask(mask) FTm = cuopts.FFT2d_cuda_kmask(mask) #intial timing timing = utc.timing() # x real and imag parts should be combined data_x_c = np.zeros((Nexample, Nk), np.complex128) data_x_c = ssmrf.seqdata_realimag_2complex(data_x) #could do mask M here, data_x_c = M*data_x_c data_x_c = mask_ksp3d(nx, ny, Nk, FTm, data_x_c) #data_x0_c = data_x_c data_x = ssmrf.seqdata_complex_2realimag(data_x_c) data_x0 = data_x # intial tmp data test_outy0 = np.zeros((Nexample, 4), dtype=np.float64) test_outy = np.zeros((Nexample, 4), dtype=np.float64) tmptest_outy = np.zeros((Nexample, 4), dtype=np.float64) acctest_outy = np.zeros((Nexample, 4), dtype=np.float64) # f'(y), cnn model timing.start() ssmrf.batch_apply_tf_cuda(Nexample, ny, sess.run, x, data_x, y_conv, test_outy0, keep_prob) timing.stop().display('CNN model first estimation ') test_outy0 = constraints(test_outy0) acctest_outy = test_outy0 # for loop start here for _ in range(40): #cuda simulate bloch for each pixel T1r, T2r, dfr, PDr = ssmrf.set_par((test_outy)) timing.start() data_x_c = ssmrf.bloch_sim_batch_cuda(Nexample, 7 * ny, Nk, PDr, T1r, T2r, dfr, M0, trr, far, ti) timing.stop().display('Bloch sim in loop ') #0.5* d||M*f(p)-b||_2^2/dp = f'*(M*f(p)-b) = f'(M*f(p)) - f'(b) # could do mask, M, here, e.g. data_x_c=M*data_x_c data_x_c = mask_ksp3d(nx, ny, Nk, FTm, data_x_c) #seperate real/imag parts or abs/angle parts data_x = ssmrf.seqdata_complex_2realimag(data_x_c) # apply CNN model, f'(i) ssmrf.batch_apply_tf_cuda(Nexample, ny, sess.run, x, data_x, y_conv, tmptest_outy, keep_prob) tmptest_outy = constraints(tmptest_outy) # gradient = f'(M * f(p)) - f'(b) tmptest_outy = tmptest_outy - test_outy0 #+ 0.2*test_outy # gradient descent for test_outy acctest_outy = acctest_outy - tmptest_outy print('gradient 0.5* d||f(p)-b||_2^2/dp: %g' % np.linalg.norm(tmptest_outy)) #print('test_outy: %g' % np.linalg.norm(test_outy)) acctest_outy = constraints(acctest_outy) #could do soft thresholding on test_outy here, i.e. test_outy = threshold(test_outy) #test_outy = l0_par(nx, ny, acctest_outy) # test_outy = tv_par(nx, ny, acctest_outy) sio.savemat(pathdat + 'cnn_cs_testouty.mat', {'test_outy': test_outy})
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})
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(): # read rf and tr arrays from mat file mat_contents = sio.loadmat(pathdat + 'mrf_t1t2b0pd_mrf_randphasecyc_traintest.mat') far = np.array(mat_contents["rf"].astype(np.complex128).squeeze()) trr = np.array(mat_contents["trr"].astype(np.float64).squeeze()) # input MRF time courses mat_contents2 = sio.loadmat(pathdat + 'datax1.mat') data_x = np.array(mat_contents2["datax1"]).astype(np.float64) # prepare for sequence simulation, y->x_hat Nk = far.shape[0] Nexample = data_x.shape[0] ti = 10 #ms M0 = np.array([0.0, 0.0, 1.0]).astype(np.float64) #image size nx = 217 ny = 181 # mask in ksp mask = ut.mask3d(nx, ny, Nk, [15, 15, 0], 0.4) #FTm = opts.FFT2d_kmask(mask) FTm = cuopts.FFT2d_cuda_kmask(mask) #intial timing timing = utc.timing() # x real and imag parts should be combined data_x_c = np.zeros((Nexample, Nk), np.complex128) data_x_c = ssmrf.seqdata_realimag_2complex(data_x) #could do mask M here, data_x_c = M*data_x_c data_x_c = mask_ksp3d(nx, ny, Nk, FTm, data_x_c) data_x0_c = data_x_c data_x_c_pre = data_x_c data_x_acc = ssmrf.seqdata_complex_2realimag(data_x_c) # intial tmp data test_outy0 = np.zeros((Nexample, 4), dtype=np.float64) test_outy = np.zeros((Nexample, 4), dtype=np.float64) step = 1.0 # for loop start here for _ in range(40): for _ in range(10): ssmrf.batch_apply_tf_cuda(Nexample, ny, sess.run, x, data_x_acc, y_conv, test_outy, keep_prob) #cuda bloch simulation for each pixel, parameters-->x T1r, T2r, dfr, PDr = ssmrf.set_par(test_outy) #timing.start() data_x_c = ssmrf.bloch_sim_batch_cuda(Nexample, 7 * ny, Nk, PDr, T1r, T2r, dfr, M0, trr, far, ti) print( np.linalg.norm(data_x_c - ssmrf.seqdata_realimag_2complex(data_x_acc))) print(np.linalg.norm(data_x_c)) data_x_acc = ssmrf.seqdata_complex_2realimag(data_x_c) #timing.stop().display('Bloch sim in loop ') #0.5* d||M*FT(x)-b||_2^2/dx = 2 * FT^-1(M*(FT(x) - b)) # lamda * d(x^2)/dx = 2*lambda*x data_x_c = mask_ksp3d(nx, ny, Nk, FTm, data_x_c - data_x0_c) #data_x_c-data_x0_c# # gradient descent/update data_x_acc = data_x_acc - step * ssmrf.seqdata_complex_2realimag( data_x_c) #data_x_c = data_x_c - mask_ksp3d(nx, ny, Nk, FTm, data_x_c) + data_x0_c #data_x_acc = ssmrf.seqdata_complex_2realimag(data_x_c) #data_x_c = (wavel1_data(nx, ny, ssmrf.seqdata_realimag_2complex(data_x_acc))) # apply CNN model, x-->parameters print('gradient 0.5* d||f(p)-b||_2^2/dp: %g' % np.linalg.norm(data_x0_c - data_x_c)) #could do soft thresholding on test_outy here, i.e. test_outy = threshold(test_outy) #test_outy = wavel1_par(nx, ny, test_outy) # #test_outy = constraints( test_outy ) #test_outy = tv_par(nx, ny, test_outy) #test_outy = constraints( test_outy ) sio.savemat(pathdat + 'cnn_cs_testouty.mat', {'test_outy': test_outy})