def derivativeFunction(x, N, N_im, dims, dimOpt, dimLenOpt, lam1, lam2, data, k, strtag, ph, kern, dirWeight=0.1, dirs=None, dirInfo=[None,None,None,None], nmins=0, wavelet="db4", mode="per", a=10.): ''' This is the function that we're going to be optimizing via the scipy optimization pack. This is the function that represents Compressed Sensing ''' #import pdb; pdb.set_trace() disp = 0 gTV = 0 gXFM = 0 x.shape = N if len(N) > 2: x0 = np.zeros(N_im) for i in xrange(N[0]): x0[i,:,:] = tf.iwt(x[i,:,:],wavelet,mode,dims,dimOpt,dimLenOpt) else: x0 = tf.iwt(x,wavelet,mode,dims,dimOpt,dimLenOpt) gDataCons = tf.wt(grads.gDataCons(x0,N_im,ph,data,k),wavelet,mode,dims,dimOpt,dimLenOpt)[0] if lam1 > 1e-6: gTV = tf.wt(grads.gTV(x0,N_im,strtag,kern,dirWeight,dirs,nmins,dirInfo=dirInfo,a=a),wavelet,mode,dims,dimOpt,dimLenOpt)[0] # Calculate the TV gradient if lam2 > 1e-6: gXFM = grads.gXFM(x,a=a) x.shape = (x.size,) return (gDataCons + lam1*gTV + lam2*gXFM).flatten() # Export the flattened array
def derivativeFunction(x, N, N_im, dims, dimOpt, dimLenOpt, lam1, lam2, data, k, strtag, ph, kern, dirWeight=0.1, dirs=None, dirInfo=[None, None, None, None], nmins=0, wavelet="db4", mode="per", a=10.): ''' This is the function that we're going to be optimizing via the scipy optimization pack. This is the function that represents Compressed Sensing ''' #import pdb; pdb.set_trace() disp = 0 gTV = 0 gXFM = 0 x.shape = N if len(N) > 2: x0 = np.zeros(N_im) for i in xrange(N[0]): x0[i, :, :] = tf.iwt(x[i, :, :], wavelet, mode, dims, dimOpt, dimLenOpt) else: x0 = tf.iwt(x, wavelet, mode, dims, dimOpt, dimLenOpt) gDataCons = tf.wt(grads.gDataCons(x0, N_im, ph, data, k), wavelet, mode, dims, dimOpt, dimLenOpt)[0] if lam1 > 1e-6: gTV = tf.wt( grads.gTV(x0, N_im, strtag, kern, dirWeight, dirs, nmins, dirInfo=dirInfo, a=a), wavelet, mode, dims, dimOpt, dimLenOpt)[0] # Calculate the TV gradient if lam2 > 1e-6: gXFM = grads.gXFM(x, a=a) x.shape = (x.size, ) return (gDataCons + lam1 * gTV + lam2 * gXFM).flatten() # Export the flattened array
def df(x, N, N_im, dims, dimOpt, dimLenOpt, lam1, lam2, data, k, strtag, ph, kern, dirWeight=0.1, dirs=None, dirInfo=[None,None,None,None], nmins=0, wavelet="db4", mode="per", level=3, a=10., mask=None, kmask=None): ''' This is the function that we're going to be optimizing via the scipy optimization pack. This is the function that represents Compressed Sensing ''' disp = 0 gTV = 0 gXFM = 0 x.shape = N if len(N) > 2: x0 = np.zeros(N_im) for i in xrange(N[0]): x0[i,:,:] = tf.iwt(x[i,:,:],wavelet,mode,dims,dimOpt,dimLenOpt) else: x0 = tf.iwt(x,wavelet,mode,dims,dimOpt,dimLenOpt) x0 = x0*mask[np.newaxis,:,:] gdc = grads.gDataCons(x0,N_im,ph,data,k,kmask=kmask) #import pdb; pdb.set_trace() if lam1 > 1e-6: gtv = grads.gTV(x0,N_im,strtag,kern,dirWeight,dirs,nmins,dirInfo=dirInfo,a=a) gDataCons = np.zeros(N) gTV = np.zeros(N) gXFM = np.zeros(N) for i in xrange(N[0]): gDataCons[i,:,:] = tf.wt(gdc[i,:,:],wavelet,mode,level,dims,dimOpt,dimLenOpt,mask)[0] if lam1 > 1e-6: gTV[i,:,:] = tf.wt(gtv[i,:,:],wavelet,mode,level,dims,dimOpt,dimLenOpt,mask)[0] # Calculate the TV gradient if lam2 > 1e-6: gXFM[i,:,:] = grads.gXFM(x[i,:,:],a=a) #import pdb; pdb.set_trace() x.shape = (x.size,) return (gDataCons + lam1*gTV + lam2*gXFM).flatten() # Export the flattened array
elif len(N) == 3: k = k.reshape(np.hstack([1,N[-2:]])).repeat(N[0],0) im_scan = abs(im).reshape(N) im_dc = np.load('/home/asalerno/Documents/pyDirectionCompSense/brainData/P14/data/im_dc.npy') ph_ones = np.ones(N[-2:], complex) ph_scan = np.exp(1.j*np.angle(im)) data = np.zeros(N,complex) for i in range(N[0]): k[i,:,:] = np.fft.fftshift(k[i,:,:]) data[i,:,:] = k[i,:,:]*tf.fft2c(im[i,:,:], ph=ph_ones) N_im = N hld, dims, dimOpt, dimLenOpt = tf.wt(im_scan[0].real,wavelet,mode) N = np.hstack([N_im[0], hld.shape]) w_scan = np.zeros(N) w_full = np.zeros(N) w_dc = np.zeros(N) for i in xrange(N[0]): w_scan[i,:,:] = tf.wt(im_scan.real[i,:,:],wavelet,mode,dims,dimOpt,dimLenOpt)[0] w_full[i,:,:] = tf.wt(abs(im[i,:,:]),wavelet,mode,dims,dimOpt,dimLenOpt)[0] w_dc[i,:,:] = tf.wt(im_dc[i,:,:],wavelet,mode,dims,dimOpt,dimLenOpt)[0] w_dc = w_dc.flatten() im_sp = im_dc.copy().reshape(N_im) minval = np.min(abs(im)) maxval = np.max(abs(im))
def runCSAlgorithm(fromfid=False, filename='/home/asalerno/Documents/pyDirectionCompSense/brainData/P14/data/fullySampledBrain.npy', sliceChoice=150, strtag = ['','spatial', 'spatial'], xtol = [1e-2, 1e-3, 5e-4, 5e-4], TV = [0.01, 0.005, 0.002, 0.001], XFM = [0.01,.005, 0.002, 0.001], dirWeight=0, pctg=0.25, radius=0.2, P=2, pft=False, ext=0.5, wavelet='db4', mode='per', method='CG', ItnLim=30, lineSearchItnLim=30, alpha_0=0.6, c=0.6, a=10.0, kern = np.array([[[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]], [[ 0., 0., 0.], [ 0., -1., 0.], [ 0., 1., 0.]], [[ 0., 0., 0.], [ 0., -1., 1.], [ 0., 0., 0.]]]), dirFile = None, nmins = None, dirs = None, M = None, dirInfo = [None]*4, saveNpy=False, saveNpyFile=None, saveImsPng=False, saveImsPngFile=None, saveImDiffPng=False, saveImDiffPngFile=None, disp=False): ##import pdb; pdb.set_trace() if fromfid==True: inputdirectory=filename[0] petable=filename[1] fullImData = rff.getDataFromFID(petable,inputdirectory,2)[0,:,:,:] fullImData = fullImData/np.max(abs(fullImData)) im = fullImData[:,:,sliceChoice] else: im = np.load(filename)[sliceChoice,:,:] N = np.array(im.shape) # image Size pdf = samp.genPDF(N[-2:], P, pctg, radius=radius, cyl=np.hstack([1, N[-2:]]), style='mult', pft=pft, ext=ext) if pft: print('Partial Fourier sampling method used') k = samp.genSampling(pdf, 50, 2)[0].astype(int) if len(N) == 2: N = np.hstack([1, N]) k = k.reshape(N) im = im.reshape(N) elif (len(N) == 3) and ('dir' not in strtag): k = k.reshape(np.hstack([1,N[-2:]])).repeat(N[0],0) ph_ones = np.ones(N[-2:], complex) ph_scan = np.zeros(N, complex) data = np.zeros(N,complex) im_scan = np.zeros(N,complex) for i in range(N[0]): k[i,:,:] = np.fft.fftshift(k[i,:,:]) data[i,:,:] = k[i,:,:]*tf.fft2c(im[i,:,:], ph=ph_ones) # IMAGE from the "scanner data" im_scan_wph = tf.ifft2c(data[i,:,:], ph=ph_ones) ph_scan[i,:,:] = tf.matlab_style_gauss2D(im_scan_wph,shape=(5,5)) ph_scan[i,:,:] = np.exp(1j*ph_scan[i,:,:]) im_scan[i,:,:] = tf.ifft2c(data[i,:,:], ph=ph_scan[i,:,:]) #im_lr = samp.loRes(im,pctg) # ------------------------------------------------------------------ # # A quick way to look at the PSF of the sampling pattern that we use # delta = np.zeros(N[-2:]) delta[int(N[-2]/2),int(N[-1]/2)] = 1 psf = tf.ifft2c(tf.fft2c(delta,ph_ones)*k,ph_ones) # ------------------------------------------------------------------ # ## ------------------------------------------------------------------ # ## -- Currently broken - Need to figure out what's happening here. -- # ## ------------------------------------------------------------------ # #if pft: #for i in xrange(N[0]): #dataHold = np.fft.fftshift(data[i,:,:]) #kHold = np.fft.fftshift(k[i,:,:]) #loc = 98 #for ix in xrange(N[-2]): #for iy in xrange(loc,N[-1]): #dataHold[-ix,-iy] = dataHold[ix,iy].conj() #kHold[-ix,-iy] = kHold[ix,iy] ## ------------------------------------------------------------------ # pdfDiv = pdf.copy() pdfZeros = np.where(pdf==0) pdfDiv[pdfZeros] = 1 #im_scan_imag = im_scan.imag #im_scan = im_scan.real N_im = N.copy() hld, dims, dimOpt, dimLenOpt = tf.wt(im_scan[0].real,wavelet,mode) N = np.hstack([N_im[0], hld.shape]) w_scan = np.zeros(N) w_full = np.zeros(N) im_dc = np.zeros(N_im) w_dc = np.zeros(N) for i in xrange(N[0]): w_scan[i,:,:] = tf.wt(im_scan.real[i,:,:],wavelet,mode,dims,dimOpt,dimLenOpt)[0] w_full[i,:,:] = tf.wt(abs(im[i,:,:]),wavelet,mode,dims,dimOpt,dimLenOpt)[0] im_dc[i,:,:] = tf.ifft2c(data[i,:,:] / np.fft.ifftshift(pdfDiv), ph=ph_scan[i,:,:]).real.copy() w_dc[i,:,:] = tf.wt(im_dc,wavelet,mode,dims,dimOpt,dimLenOpt)[0] w_dc = w_dc.flatten() im_sp = im_dc.copy().reshape(N_im) minval = np.min(abs(im)) maxval = np.max(abs(im)) data = np.ascontiguousarray(data) imdcs = [im_dc,np.zeros(N_im),np.ones(N_im),np.random.randn(np.prod(N_im)).reshape(N_im)] imdcs[-1] = imdcs[-1] - np.min(imdcs[-1]) imdcs[-1] = imdcs[-1]/np.max(abs(imdcs[-1])) mets = ['Density Corrected','Zeros','1/2''s','Gaussian Random Shift (0,1)'] wdcs = [] for i in range(len(imdcs)): wdcs.append(tf.wt(imdcs[i][0],wavelet,mode,dims,dimOpt,dimLenOpt)[0].reshape(N)) ims = [] #print('Starting the CS Algorithm') for kk in range(len(wdcs)): w_dc = wdcs[kk] print(mets[kk]) for i in range(len(TV)): args = (N, N_im, dims, dimOpt, dimLenOpt, TV[i], XFM[i], data, k, strtag, ph_scan, kern, dirWeight, dirs, dirInfo, nmins, wavelet, mode, a) w_result = opt.minimize(f, w_dc, args=args, method=method, jac=df, options={'maxiter': ItnLim, 'lineSearchItnLim': lineSearchItnLim, 'gtol': 0.01, 'disp': 1, 'alpha_0': alpha_0, 'c': c, 'xtol': xtol[i], 'TVWeight': TV[i], 'XFMWeight': XFM[i], 'N': N}) if np.any(np.isnan(w_result['x'])): print('Some nan''s found. Dropping TV and XFM values') elif w_result['status'] != 0: print('TV and XFM values too high -- no solution found. Dropping...') else: w_dc = w_result['x'] w_res = w_dc.reshape(N) im_res = np.zeros(N_im) for i in xrange(N[0]): im_res[i,:,:] = tf.iwt(w_res[i,:,:],wavelet,mode,dims,dimOpt,dimLenOpt) ims.append(im_res) if saveNpy: if saveNpyFile is None: np.save('./holdSave_im_res_' + str(int(pctg*100)) + 'p_all_SP',ims) else: np.save(saveNpyFile,ims) if saveImsPng: vis.figSubplots(ims,titles=mets,clims=(minval,maxval),colorbar=True) if not disp: if saveImsPngFile is None: saveFig.save('./holdSave_ims_' + str(int(pctg*100)) + 'p_all_SP') else: saveFig.save(saveImsPngFile) if saveImDiffPng: imdiffs, clims = vis.imDiff(ims) diffMets = ['DC-Zeros','DC-Ones','DC-Random','Zeros-Ones','Zeros-Random','Ones-Random'] vis.figSubplots(imdiffs,titles=diffMets,clims=clims,colorbar=True) if not disp: if saveImDiffPngFile is None: saveFig.save('./holdSave_im_diffs_' + str(int(pctg*100)) + 'p_all_SP') else: saveFig.save(saveImDiffPngFile) if disp: plt.show()
dataFull = np.zeros(N,complex) im_scan = np.zeros(N, complex) for i in range(N[0]): data[i,:,:] = np.fft.fftshift(k[i,:,:])*tf.fft2c(im[i,:,:], ph=ph_ones) dataFull[i,:,:] = np.fft.fftshift(tf.fft2c(im[i,:,:], ph=ph_ones)) im_scan_wph = tf.ifft2c(data[i,:,:], ph=ph_ones) #ph_scan[i,:,:] = tf.matlab_style_gauss2D(im_scan_wph,shape=(5,5)) ph_scan[i,:,:] = np.angle(gaussian_filter(im_scan_wph.real,2) + 1.j*gaussian_filter(im_scan_wph.imag,2)) ph_scan[i,:,:] = np.exp(1j*ph_scan[i,:,:]) im_scan[i,:,:] = tf.ifft2c(data[i,:,:], ph=ph_scan[i,:,:]) N_im = N hld, dims, dimOpt, dimLenOpt = tf.wt(im_scan[0].real,wavelet,mode) N = np.hstack([N_im[0], hld.shape]) w_scan = np.zeros(N) w_full = np.zeros(N) im_dc = np.zeros(N_im) w_dc = np.zeros(N) for i in xrange(N[0]): w_scan[i,:,:] = tf.wt(im_scan.real[i,:,:],wavelet,mode,dims,dimOpt,dimLenOpt)[0] w_full[i,:,:] = tf.wt(abs(im[i,:,:]),wavelet,mode,dims,dimOpt,dimLenOpt)[0] k[i,:,:] = np.fft.fftshift(k[i,:,:]) im_dc = np.zeros(N_im) w_dc = np.zeros(N)
np.hstack([1, NSub[-2:]])) kMasked = kHld #kMasks.append(kMasked) # Now we need to construct the starting point if locSteps[j] == 0: pdfDiv = pdf.copy() else: pdfDiv = pdf[locSteps[j]:-locSteps[j], locSteps[j]:-locSteps[j]].copy() pdfZeros = np.where(pdfDiv < 1e-4) pdfDiv[pdfZeros] = 1 pdfDiv = pdfDiv.reshape(np.hstack([1, NSub[-2:]])).repeat(NSub[0], 0) N_imSub = NSub hldSub, dimsSub, dimOptSub, dimLenOptSub = tf.wt( im_scanSub[0].real, wavelet, mode) NSub = np.hstack([N_imSub[0], hldSub.shape]) w_scanSub = np.zeros(NSub) im_dcSub = np.zeros(N_imSub) w_dcSub = np.zeros(NSub) if j == 0: data_dc = np.zeros(N_imSub, complex) else: data_dc_hld = np.zeros(N_imSub, complex) for i in range(N_imSub[0]): data_dc_hld[i] = tf.zpad( np.fft.fftshift(data_dc[i], axes=(-2, -1)), N_imSub[-2:]) * (1 - kSub[i]) data_dc = np.fft.fftshift(data_dc_hld, axes=(-2, -1))
im_scan = np.zeros(N, complex) for i in range(N[0]): data[i, :, :] = np.fft.fftshift(k[i, :, :]) * tf.fft2c(im[i, :, :], ph=ph_ones) dataFull[i, :, :] = np.fft.fftshift(tf.fft2c(im[i, :, :], ph=ph_ones)) im_scan_wph = tf.ifft2c(data[i, :, :], ph=ph_ones) #ph_scan[i,:,:] = tf.matlab_style_gauss2D(im_scan_wph,shape=(5,5)) ph_scan[i, :, :] = np.angle( gaussian_filter(im_scan_wph.real, 2) + 1.j * gaussian_filter(im_scan_wph.imag, 2)) ph_scan[i, :, :] = np.exp(1j * ph_scan[i, :, :]) im_scan[i, :, :] = tf.ifft2c(data[i, :, :], ph=ph_scan[i, :, :]) N_im = N hld, dims, dimOpt, dimLenOpt = tf.wt(im_scan[0].real, wavelet, mode) N = np.hstack([N_im[0], hld.shape]) w_scan = np.zeros(N) w_full = np.zeros(N) im_dc = np.zeros(N_im) w_dc = np.zeros(N) for i in xrange(N[0]): w_scan[i, :, :] = tf.wt(im_scan.real[i, :, :], wavelet, mode, dims, dimOpt, dimLenOpt)[0] w_full[i, :, :] = tf.wt(abs(im[i, :, :]), wavelet, mode, dims, dimOpt, dimLenOpt)[0] k[i, :, :] = np.fft.fftshift(k[i, :, :]) im_dc = np.zeros(N_im)
kHold = np.fft.fftshift(k[i, :, :]) loc = 98 for ix in xrange(N[-2]): for iy in xrange(loc, N[-1]): dataHold[-ix, -iy] = dataHold[ix, iy].conj() kHold[-ix, -iy] = kHold[ix, iy] # ------------------------------------------------------------------ # pdfDiv = pdf.copy() pdfZeros = np.where(pdf == 0) pdfDiv[pdfZeros] = 1 #im_scan_imag = im_scan.imag #im_scan = im_scan N_im = N hld, dims, dimOpt, dimLenOpt = tf.wt(im_scan[0].real, wavelet, mode) N = np.hstack([N_im[0], hld.shape]) w_scan = np.zeros(N) w_full = np.zeros(N) im_dc = np.zeros(N_im) w_dc = np.zeros(N) for i in xrange(N[0]): w_scan[i, :, :] = tf.wt(im_scan.real[i, :, :], wavelet, mode, dims, dimOpt, dimLenOpt)[0] w_full[i, :, :] = tf.wt(abs(im[i, :, :]), wavelet, mode, dims, dimOpt, dimLenOpt)[0] im_dc[i, :, :] = tf.ifft2c(data[i, :, :] / np.fft.ifftshift(pdfDiv), ph=ph_scan[i, :, :]).real.copy()
kMasked = kSub[0].copy() else: padMask = tf.zpad(np.ones(kMasked.shape),NSub[-2:]) kMasked = (1-padMask)*kSub[0] + padMask*tf.zpad(kMasked,NSub[-2:]) # Now we need to construct the starting point if locSteps[j]==0: pdfDiv = pdf.copy() else: pdfDiv = pdf[locSteps[j]:-locSteps[j],locSteps[j]:-locSteps[j]].copy() pdfZeros = np.where(pdfDiv < 1e-4) pdfDiv[pdfZeros] = 1 N_imSub = NSub hldSub, dimsSub, dimOptSub, dimLenOptSub = tf.wt(im_scanSub[0].real,wavelet,mode) NSub = np.hstack([N_imSub[0], hldSub.shape]) w_scanSub = np.zeros(NSub) im_dcSub = np.zeros(N_imSub) w_dcSub = np.zeros(NSub) for i in xrange(N[0]): w_scanSub[i,:,:] = tf.wt(im_scanSub.real[i,:,:],wavelet,mode,dimsSub,dimOptSub,dimLenOptSub)[0] im_dcSub[i,:,:] = tf.ifft2c(dataSub[i,:,:] / np.fft.ifftshift(pdfDiv), ph=ph_scanSub[i,:,:], sz=szFull).real.copy() w_dcSub[i,:,:] = tf.wt(im_dcSub,wavelet,mode,dimsSub,dimOptSub,dimLenOptSub)[0] kSub[i,:,:] = np.fft.fftshift(kSub[i,:,:]) w_dcSub = w_dcSub.flatten() im_spSub = im_dcSub.copy().reshape(N_imSub)
def runCSAlgorithm( fromfid=False, filename='/home/asalerno/Documents/pyDirectionCompSense/brainData/P14/data/fullySampledBrain.npy', sliceChoice=150, strtag=['', 'spatial', 'spatial'], xtol=[1e-2, 1e-3, 5e-4, 5e-4], TV=[0.01, 0.005, 0.002, 0.001], XFM=[0.01, .005, 0.002, 0.001], dirWeight=0, pctg=0.25, radius=0.2, P=2, pft=False, ext=0.5, wavelet='db4', mode='per', method='CG', ItnLim=30, lineSearchItnLim=30, alpha_0=0.6, c=0.6, a=10.0, kern=np.array([[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]], [[0., 0., 0.], [0., -1., 0.], [0., 1., 0.]], [[0., 0., 0.], [0., -1., 1.], [0., 0., 0.]]]), dirFile=None, nmins=None, dirs=None, M=None, dirInfo=[None] * 4, saveNpy=False, saveNpyFile=None, saveImsPng=False, saveImsPngFile=None, saveImDiffPng=False, saveImDiffPngFile=None, disp=False): ##import pdb; pdb.set_trace() if fromfid == True: inputdirectory = filename[0] petable = filename[1] fullImData = rff.getDataFromFID(petable, inputdirectory, 2)[0, :, :, :] fullImData = fullImData / np.max(abs(fullImData)) im = fullImData[:, :, sliceChoice] else: im = np.load(filename)[sliceChoice, :, :] N = np.array(im.shape) # image Size pdf = samp.genPDF(N[-2:], P, pctg, radius=radius, cyl=np.hstack([1, N[-2:]]), style='mult', pft=pft, ext=ext) if pft: print('Partial Fourier sampling method used') k = samp.genSampling(pdf, 50, 2)[0].astype(int) if len(N) == 2: N = np.hstack([1, N]) k = k.reshape(N) im = im.reshape(N) elif (len(N) == 3) and ('dir' not in strtag): k = k.reshape(np.hstack([1, N[-2:]])).repeat(N[0], 0) ph_ones = np.ones(N[-2:], complex) ph_scan = np.zeros(N, complex) data = np.zeros(N, complex) im_scan = np.zeros(N, complex) for i in range(N[0]): k[i, :, :] = np.fft.fftshift(k[i, :, :]) data[i, :, :] = k[i, :, :] * tf.fft2c(im[i, :, :], ph=ph_ones) # IMAGE from the "scanner data" im_scan_wph = tf.ifft2c(data[i, :, :], ph=ph_ones) ph_scan[i, :, :] = tf.matlab_style_gauss2D(im_scan_wph, shape=(5, 5)) ph_scan[i, :, :] = np.exp(1j * ph_scan[i, :, :]) im_scan[i, :, :] = tf.ifft2c(data[i, :, :], ph=ph_scan[i, :, :]) #im_lr = samp.loRes(im,pctg) # ------------------------------------------------------------------ # # A quick way to look at the PSF of the sampling pattern that we use # delta = np.zeros(N[-2:]) delta[int(N[-2] / 2), int(N[-1] / 2)] = 1 psf = tf.ifft2c(tf.fft2c(delta, ph_ones) * k, ph_ones) # ------------------------------------------------------------------ # ## ------------------------------------------------------------------ # ## -- Currently broken - Need to figure out what's happening here. -- # ## ------------------------------------------------------------------ # #if pft: #for i in xrange(N[0]): #dataHold = np.fft.fftshift(data[i,:,:]) #kHold = np.fft.fftshift(k[i,:,:]) #loc = 98 #for ix in xrange(N[-2]): #for iy in xrange(loc,N[-1]): #dataHold[-ix,-iy] = dataHold[ix,iy].conj() #kHold[-ix,-iy] = kHold[ix,iy] ## ------------------------------------------------------------------ # pdfDiv = pdf.copy() pdfZeros = np.where(pdf == 0) pdfDiv[pdfZeros] = 1 #im_scan_imag = im_scan.imag #im_scan = im_scan.real N_im = N.copy() hld, dims, dimOpt, dimLenOpt = tf.wt(im_scan[0].real, wavelet, mode) N = np.hstack([N_im[0], hld.shape]) w_scan = np.zeros(N) w_full = np.zeros(N) im_dc = np.zeros(N_im) w_dc = np.zeros(N) for i in xrange(N[0]): w_scan[i, :, :] = tf.wt(im_scan.real[i, :, :], wavelet, mode, dims, dimOpt, dimLenOpt)[0] w_full[i, :, :] = tf.wt(abs(im[i, :, :]), wavelet, mode, dims, dimOpt, dimLenOpt)[0] im_dc[i, :, :] = tf.ifft2c(data[i, :, :] / np.fft.ifftshift(pdfDiv), ph=ph_scan[i, :, :]).real.copy() w_dc[i, :, :] = tf.wt(im_dc, wavelet, mode, dims, dimOpt, dimLenOpt)[0] w_dc = w_dc.flatten() im_sp = im_dc.copy().reshape(N_im) minval = np.min(abs(im)) maxval = np.max(abs(im)) data = np.ascontiguousarray(data) imdcs = [ im_dc, np.zeros(N_im), np.ones(N_im), np.random.randn(np.prod(N_im)).reshape(N_im) ] imdcs[-1] = imdcs[-1] - np.min(imdcs[-1]) imdcs[-1] = imdcs[-1] / np.max(abs(imdcs[-1])) mets = [ 'Density Corrected', 'Zeros', '1/2' 's', 'Gaussian Random Shift (0,1)' ] wdcs = [] for i in range(len(imdcs)): wdcs.append( tf.wt(imdcs[i][0], wavelet, mode, dims, dimOpt, dimLenOpt)[0].reshape(N)) ims = [] #print('Starting the CS Algorithm') for kk in range(len(wdcs)): w_dc = wdcs[kk] print(mets[kk]) for i in range(len(TV)): args = (N, N_im, dims, dimOpt, dimLenOpt, TV[i], XFM[i], data, k, strtag, ph_scan, kern, dirWeight, dirs, dirInfo, nmins, wavelet, mode, a) w_result = opt.minimize(f, w_dc, args=args, method=method, jac=df, options={ 'maxiter': ItnLim, 'lineSearchItnLim': lineSearchItnLim, 'gtol': 0.01, 'disp': 1, 'alpha_0': alpha_0, 'c': c, 'xtol': xtol[i], 'TVWeight': TV[i], 'XFMWeight': XFM[i], 'N': N }) if np.any(np.isnan(w_result['x'])): print('Some nan' 's found. Dropping TV and XFM values') elif w_result['status'] != 0: print( 'TV and XFM values too high -- no solution found. Dropping...' ) else: w_dc = w_result['x'] w_res = w_dc.reshape(N) im_res = np.zeros(N_im) for i in xrange(N[0]): im_res[i, :, :] = tf.iwt(w_res[i, :, :], wavelet, mode, dims, dimOpt, dimLenOpt) ims.append(im_res) if saveNpy: if saveNpyFile is None: np.save('./holdSave_im_res_' + str(int(pctg * 100)) + 'p_all_SP', ims) else: np.save(saveNpyFile, ims) if saveImsPng: vis.figSubplots(ims, titles=mets, clims=(minval, maxval), colorbar=True) if not disp: if saveImsPngFile is None: saveFig.save('./holdSave_ims_' + str(int(pctg * 100)) + 'p_all_SP') else: saveFig.save(saveImsPngFile) if saveImDiffPng: imdiffs, clims = vis.imDiff(ims) diffMets = [ 'DC-Zeros', 'DC-Ones', 'DC-Random', 'Zeros-Ones', 'Zeros-Random', 'Ones-Random' ] vis.figSubplots(imdiffs, titles=diffMets, clims=clims, colorbar=True) if not disp: if saveImDiffPngFile is None: saveFig.save('./holdSave_im_diffs_' + str(int(pctg * 100)) + 'p_all_SP') else: saveFig.save(saveImDiffPngFile) if disp: plt.show()
padMask = tf.zpad(np.ones(kMasked[msk].shape),NSub[-2:]) kHld[msk] = ((1-padMask)*kSub[msk] + padMask*tf.zpad(kMasked[msk],NSub[-2:])).reshape(np.hstack([1,NSub[-2:]])) kMasked = kHld #kMasks.append(kMasked) # Now we need to construct the starting point if locSteps[j]==0: pdfDiv = pdf.copy() else: pdfDiv = pdf[locSteps[j]:-locSteps[j],locSteps[j]:-locSteps[j]].copy() pdfZeros = np.where(pdfDiv < 1e-4) pdfDiv[pdfZeros] = 1 pdfDiv = pdfDiv.reshape(np.hstack([1,NSub[-2:]])).repeat(NSub[0],0) N_imSub = NSub hldSub, dimsSub, dimOptSub, dimLenOptSub = tf.wt(im_scanSub[0].real,wavelet,mode) NSub = np.hstack([N_imSub[0], hldSub.shape]) w_scanSub = np.zeros(NSub) im_dcSub = np.zeros(N_imSub) w_dcSub = np.zeros(NSub) if j == 0: data_dc = np.zeros(N_imSub,complex) else: data_dc_hld = np.zeros(N_imSub,complex) for i in range(N_imSub[0]): data_dc_hld[i] = tf.zpad(np.fft.fftshift(data_dc[i],axes=(-2,-1)),N_imSub[-2:])*(1-kSub) data_dc = np.fft.fftshift(data_dc_hld,axes=(-2,-1)) dataSub += data_dc