def test2(): mat_contents = sio.loadmat(pathdat + 'im_pca_2fa_2freq.mat') #im.mat I = np.array(mat_contents["I"].astype(np.float32)) nx, ny, nz, ndiv = I.shape #print(I.shape) imall = I.reshape([nx * ny * nz, ndiv]) npar = 8 imall = imall / np.ndarray.max(imall.flatten()) ut.plotim3(imall.reshape(I.shape)[..., 0], [5, -1], pause_close=1) model = tf_wrap.tf_model_top([None, ndiv], [None, npar], tf_prediction_func, tf_optimize_func, tf_error_func, arg=1.0) model.restore(pathdat + 'test_model_save_b1_2fa_2freq') prey = model.prediction(imall, np.zeros([imall.shape[0], npar])) immatch = prey.reshape([nx, ny, nz, npar]) #ut.plotim3(immatch[...,0],[10, -1],bar = 1, pause_close = 5) #ut.plotim3(immatch[...,1],[10, -1],bar = 1, pause_close = 5) #ut.plotim3(immatch[...,2],[10, -1],bar = 1, pause_close = 5) #ut.plotim3(immatch[...,3],[10, -1],bar = 1, pause_close = 5) #ut.plotim3(immatch[...,4],[10, -1],bar = 1, pause_close = 5) sio.savemat(pathdat + 'MRF_cnn_matchtt_b1_2fa_2freq.mat', { 'immatch': immatch, 'imall': imall })
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 test1(): mnist = input_data.read_data_sets('./data/MNIST_data/', one_hot=True) batch_size = 200 model = tf_wrap.tf_model_top([None, 784], [None, 1],\ tf_prediction_func, tf_optimize_func, tf_error_func, \ arg = batch_size, dtype = np.float32) d_loss, g_loss, dopt, gopt = model.model_wrap.optimize G, D1, D2 = model.model_wrap.prediction for i in range(1200): x, y = mnist.train.next_batch(batch_size) z = np.random.uniform(0, 1, (batch_size, 1)).astype(np.float32) loss_d, _ = model.sess.run([d_loss, dopt], { model.data: x, model.target: z }) z = np.random.uniform(0, 1, (batch_size, 1)).astype(np.float32) loss_g, _ = model.sess.run([g_loss, gopt], { model.data: x, model.target: z }) print('{}: {}\t{}'.format(i, loss_d, loss_g)) if i % 1000 == 0: getG = model.sess.run(G, { model.data: x, model.target: z }).reshape((batch_size, 28, 28)) ut.plotim3(sum(getG, 0)) model.save('../save_data/test_model_save')
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 test2(): mat_contents = sio.loadmat(pathdat + 'im_pca.mat') #im.mat I = np.array(mat_contents["I"].astype(np.float32)) nx, ny, nz, ndiv = I.shape imall = I.reshape([nx * ny * nz, ndiv]) imall = imall / np.ndarray.max(imall.flatten()) #ut.plotim3(imall.reshape(I.shape)[...,0]) for i in range(imall.shape[0]): tc = imall[i, :] #- np.mean(imall[i,:]) normtc = np.linalg.norm(tc) if normtc > 5e-2: imall[i, :] = tc / normtc else: imall[i, :] = np.zeros([1, ndiv]) #imall = imall/np.ndarray.max(imall.flatten()) ut.plotim3(imall.reshape(I.shape)[..., 0]) model = tf_wrap.tf_model_top([None, 13], [None, 3], tf_prediction_func, tf_optimize_func, tf_error_func) model.restore('../save_data/test_model_save') prey = model.prediction(imall, np.zeros([imall.shape[0], 3])) immatch = prey.reshape([nx, ny, nz, 3]) ut.plotim3(immatch[..., 0], bar=1) ut.plotim3(immatch[..., 1], bar=1) ut.plotim3(immatch[..., 2], bar=1) sio.savemat(pathdat + 'MRF_cnn_matchtt.mat', { 'immatch': immatch, 'imall': imall })
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 nufft = cuoptc.NUFFT3d_cuda(im_shape, dcf) nufft.normalize_set_ktraj(ktraj) #nufft = optc.NUFFT3d(im_shape, dcf) #nufft.normalize_set_ktraj(ktraj) im_under = nufft.backward(kdata) im_under = nufft.forward_backward(im_under) #kdata = nufft.forward(im_under) #im_under = nufft.backward(kdata) #im_under = ncoil_nufft( ktraj, dcf, kdata, ncoils, im_shape ) #im_under = allcoil_nufft( ktraj, dcf, kdata, ncoils, im_shape ) sio.savemat(path + 'nufftrecon2.mat', {'im_under': im_under}) #im_under = ut.loadmat(path +'nufftrecon.mat','im_under') rss_im_under = rss(im_under) ut.plotim3(np.absolute(im_under[:, :, im_shape[2] // 2, :].squeeze()), [8, -1]) ut.plotim3(rss_im_under) sio.savemat(path + 'nufftrecon_rss2.mat', {'rss_im_under': rss_im_under})
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 test2(): mat_contents = sio.loadmat(pathdat + 'im_pca.mat') #im.mat I = np.array(mat_contents["I"])[:, :, 25:45, :] if len(I.shape) == 3: nx, ny, ndiv = I.shape nz = 1 elif len(I.shape) == 4: nx, ny, nz, ndiv = I.shape imall = I.reshape([nx * ny * nz, ndiv]) npar = 4 abs_flag = 0 #apply pca on absolute time course, else compute absolute value after pca if abs_flag is 0: imall = np.absolute(imall).astype(np.float32) else: imall = imall.astype(np.float32) #imall = imall/np.ndarray.max(imall.flatten()) #ut.plotim3(imall.reshape(I.shape)[...,0]) #for i in range(imall.shape[0]): # tc = imall[i,:] #- np.mean(imall[i,:]) # normtc = np.linalg.norm(tc) # if normtc > 1e-3: # imall[i,:] = tc/normtc # else: # imall[i,:] = np.zeros([1,ndiv]) imall = 1000.0 * imall / np.ndarray.max(imall.flatten()) #0.2 ut.plotim3(imall.reshape(I.shape)[..., 0], [10, 6], pause_close=1) model = tf_wrap.tf_model_top([None, ndiv], [None, npar], tf_prediction_func, tf_optimize_func, tf_error_func) model.restore(pathdat + 'test_model_savecnn') prey = model.prediction(imall, np.zeros([imall.shape[0], npar])) immatch = prey.reshape([nx, ny, nz, npar]) ut.plotim3(immatch[..., 0], [10, 6], bar=1, pause_close=5) ut.plotim3(immatch[..., 1], [10, 6], bar=1, pause_close=5) ut.plotim3(immatch[..., 2], [10, 6], bar=1, pause_close=5) ut.plotim3(immatch[..., 3], [10, 6], bar=1, pause_close=5) sio.savemat(pathdat + 'MRF_cnn_matchttt.mat', { 'immatch': immatch, 'imall': imall })
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 test2(): N = 20 k1, k2, k3 = nufftfreqs3d(N, N, N) k_c = np.cos(np.multiply(5, k1)) + 1j * np.sin(np.multiply(5, k2)) # #k_c = np.ones((N,N,N))# hwin = hamming3d(N, N, N) ut.plotim3(np.absolute(hwin), [4, -1]) # apply hamming window k_c = np.multiply(k_c, hwin) im = dft3d_warp(N, N, N, k_c) ut.plotim3(np.absolute(im), [4, -1], bar=True) #use std fft lib ft = opts.FFTnd() npim = ft.backward(k_c) ut.plotim3(np.absolute(npim), [4, -1], bar=True) ut.plotim3(np.absolute(im - npim), [4, -1], bar=True) #interpolatation im_int = dft3d_warp(2 * N, 2 * N, N, k_c) ut.plotim3(np.absolute(im_int), [4, -1], bar=True)
def test2(): mat_contents = sio.loadmat(pathdat + 'im_pca.mat') #im.mat I = np.array(mat_contents["I"].astype(np.float32)) nx, ny, nz, ndiv = I.shape #print(I.shape) imall = I.reshape([nx * ny * nz, ndiv]) npar = 4 imall = 0.3 * imall / np.ndarray.max(imall.flatten()) #ut.plotim3(imall.reshape(I.shape)[...,0]) #for i in range(imall.shape[0]): # tc = imall[i,:] #- np.mean(imall[i,:]) # normtc = np.linalg.norm(tc) # if normtc > 1e-3: # imall[i,:] = tc/normtc # else: # imall[i,:] = np.zeros([1,ndiv]) #imall =imall/np.ndarray.max(imall.flatten())#0.2 #for kk in range(imall.shape[1]): # imall [:,kk] = (imall[:,kk])/np.std(imall[:,kk])# - np.mean(imall[:,kk]) ut.plotim3(imall.reshape(I.shape)[..., 0], [5, -1], pause_close=1) model = tf_wrap.tf_model_top([None, ndiv], [None, npar], tf_prediction_func, tf_optimize_func, tf_error_func, arg=1.0) model.restore(pathdat + 'test_model_save') prey = model.prediction(imall, np.zeros([imall.shape[0], npar])) immatch = prey.reshape([nx, ny, nz, npar]) ut.plotim3(immatch[..., 0], [10, -1], bar=1, pause_close=5) ut.plotim3(immatch[..., 1], [10, -1], bar=1, pause_close=5) ut.plotim3(immatch[..., 2], [10, -1], bar=1, pause_close=5) ut.plotim3(immatch[..., 3], [10, -1], bar=1, pause_close=5) sio.savemat(pathdat + 'MRF_cnn_matchtt.mat', { 'immatch': immatch, 'imall': imall })
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(): 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[:,:,:]))
def test(): ft = opts.FFTnd() # simulated image 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, :])) #crop k-space xcrop = ut.crop3d(x, 12) #do espirit2d Vim, sim = espirit_3d(xcrop, x.shape, 150, hkwin_shape = (12,12,12),\ pad_before_espirit = 0, pad_fact = 2) esp = opts.espirit(Vim) esp_im = esp.backward(im) ut.plotim3(np.absolute(esp_im[:, :, :])) im_recon = esp.forward(esp_im) ut.plotim3(np.absolute(im_recon[:, :, im.shape[2] // 2, :]))
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(): #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(): 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(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/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-22_7T-volunteer/' # datpath = '/data/larson/brain_uT2/2016-09-13_3T-volunteer/' f = h5py.File(datpath + 'ute_32echo_random-csreconallec_l2_r0p01.mat') #im3d = f['imallplus'][0:10].transpose([1,2,3,0]) #im = im3d[:,40,:,:].squeeze().view(np.complex128) Ndiv = 8 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] 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 #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([1, 1, 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 = 100 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 = 200 #number of iterations #step = 0.1 #step size #th = 0.001 # theshold level #do tv cs mri recon #Nite = 20 #number of iterations #step = 1 #step size #tv_r = 0.01 # regularization term for tv term #rho = 1.0 #ostep = 0.3 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 ) # 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_CGD.mat', { 'xpar': xpar, '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_cg.mat', \ {'xpar':xpar, 'residual':residual}) #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)
def espirit_3d( xcrop, x_shape, nsingularv = 150, hkwin_shape = (16,16,16),\ pad_before_espirit = 0, pad_fact = 1, sigv_th = 0.01, nsigv_th = 0.2 ): #ft = op.FFTnd((0,1,2))#3d fft operator ft = op.FFTWnd((0, 1, 2)) #3d fft operator timing = utc.timing() #multidimention tensor as the block hankel matrix #first 2 are x, y dims with rolling window size of hkwin_shape #last 1 is coil dimension, with stride of 1 # output dims are : (3_hankel_dims + 1_coil_dim)_win_size + (3_hankel_dims + 1_coil_dim)_rolling_times timing.start() h = hk.hankelnd_r(xcrop, (hkwin_shape[0], hkwin_shape[1], hkwin_shape[2], 1)) timing.stop().display('Create Hankel ').start() dimh = h.shape #flatten the tensor to create a matrix= [flatten(fist4 dims), flatten(last4 dims)] #the second dim of hmtx contain coil information, i.e. dimh[3]=1, dimh[7]=N_coils hmtx = h.reshape(( dimh[0]* dimh[1]* dimh[2]* dimh[3], dimh[4], dimh[5], dimh[6], dimh[7])).\ reshape((dimh[0]* dimh[1]* dimh[2]* dimh[3], dimh[4]* dimh[5]* dimh[6]* dimh[7])) timing.stop().display('Reshape Hankel ').start() #svd, could try other approaches # V has the coil information since the second dim of hmtx has coil data U, s, V = np.linalg.svd(hmtx, full_matrices=False) #U, s, V = randomized_svd(hmtx, n_components=nsingularv,n_iter=5,random_state=None) #U, s, V = scipy.sparse.linalg.svds(hmtx, nsingularv ) timing.stop().display('SVD ').start() #S = np.diag(s) #ut.plotim1(np.absolute(V[:,0:150]).T)#plot V singular vectors ut.plot(s.T) #plot singular values for k in range(len(s)): if s[k] > s[0] * nsigv_th: nsingularv = k print('extract %g out of %g singular vectors' % (nsingularv, len(s))) #invh = np.zeros(x.shape,complex) #print h.shape #hk.invhankelnd(h,invh,(2,3,1)) #reshape vn to generate k-space vn tensor #first dim is singular vector, which is transposed to the second last dimension vn = V[0:nsingularv, :].reshape( (nsingularv, dimh[4], dimh[5], dimh[6], dimh[7])).transpose( (1, 2, 3, 0, 4)) #zero pad vn, vn matrix of reshaped singular vectors, #dims of vn: nx,ny,nsingularv,ncoil #do pading before espirit, reduce the memory requirement if pad_before_espirit is 0: nx = min(pad_fact * xcrop.shape[0], x_shape[0]) ny = min(pad_fact * xcrop.shape[1], x_shape[1]) nz = min(pad_fact * xcrop.shape[2], x_shape[2]) else: nx = x_shape[0] ny = x_shape[1] nz = x_shape[2] #coil dim nc = x_shape[3] #create hamming window hwin = hamming3d(vn.shape[0], vn.shape[1], vn.shape[2]) # apply hamming window vn = np.multiply(vn, hwin[:, :, :, np.newaxis, np.newaxis]) #zero pad vn = ut.pad3d(vn, nx, ny, nz) #plot first singular vecctor Vn[0] imvn = ft.backward(vn) #ut.plotim3(np.absolute(imvn[:,:,0,:].squeeze()))#spatial feature of V[:,1] singular vector sim = np.zeros((nx, ny, nz), dtype=vn.dtype) Vim = np.zeros((nx, ny, nz, nc), dtype=vn.dtype) #espirit loop, Vim eigen vector, Sim eigen value, this is a pixel wise PCA on vn for ix in range(nx): for iy in range(ny): for iz in range(nz): vpix = imvn[ix, iy, iz, :, :].squeeze() vpix = np.matrix(vpix).transpose() vvH = vpix.dot(vpix.getH()) U, s, V = np.linalg.svd(vvH, full_matrices=False) #s, V = numpy.linalg.eig(vvH) #U, s, V = randomized_svd(vvH, n_components=2,n_iter=5,random_state=None) sim[ix, iy, iz] = s[0] Vim[ix, iy, iz, :] = V[0, :].squeeze() Vim = np.conj(Vim) timing.stop().display('ESPIRIT ') #pad the image after espirit if pad_before_espirit is 0: Vim = ft.backward( ut.pad3d(ft.forward(Vim), x_shape[0], x_shape[1], x_shape[2])) sim = ft.backward( ut.pad3d(ft.forward(sim), x_shape[0], x_shape[1], x_shape[2])) #plot first eigen vector, which is coil sensitvity map, and eigen value ut.plotim3(np.absolute(Vim[Vim.shape[0] // 2, :, :, :].squeeze())) ut.plotim1(np.absolute(sim[Vim.shape[0] // 2, :, :].squeeze())) #Vim_dims_name = ['x', 'y', 'z', 'coil'] #sim_dims_name = ['x', 'y', 'z'] Vimnorm = np.linalg.norm(Vim, axis=3) Vim = np.divide(Vim, 1e-6 + Vimnorm[:, :, :, np.newaxis]) sim = sim / np.max(sim.flatten()) for ix in range(x_shape[0]): for iy in range(x_shape[1]): for iz in range(x_shape[2]): if sim[ix, iy, iz] < sigv_th: Vim[ix, iy, iz, :] = np.zeros(nc) return Vim, np.absolute(sim) #, Vim_dims_name, sim_dims_name
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))
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)