def grad_k(ws, fdensity, alpha, sig, psf_k): print('grad_k begin') mo = np.exp(-4.) ws = real_to_complex(ws) ws = ws.reshape((n_grid, n_grid)) #wk = ws; ws = np.real(fft.ifft2(ws)) l1 = -1 * fft.ifft2((fft.ifft2(fft.fft2(ws) * fft.fft2(psf)) - data) / sig_noise**2) * psf_k ''' l1_og = fft.ifft2((Psi(ws) - data)/sig_noise**2)*psf_k; l1_other = fft.ifft2((fft.fft2(fft.ifft2(ws)*fft.ifft2(psf)) - data)/sig_noise**2)*psf_k; print('diff is:'); print(l1 - l1_other); print(l1-l1_og); ''' #print(l1-l1_other) l1 = l1.flatten() xsi = (1. - fdensity) * gaussian(np.log(ws), loc=np.log( mo), scale=sig) / ws + fdensity * (ws**alpha / w_norm) l2 = -1 * gaussian(np.log(ws), loc=np.log(mo), scale=sig) * ( 1. - fdensity) / ws**2 - (1. - fdensity) * np.log(ws / mo) * np.exp( -np.log(ws / mo)**2 / 2 / sig**2) / np.sqrt( 2 * np.pi) / ws**2 / sig**3 + fdensity * alpha * ws**( alpha - 1) / w_norm l2 = l2 / np.absolute(xsi) l2 = fft.ifft2(l2).flatten() l_tot = l1 + l2 #return l1,l2; l_tot = complex_to_real(l_tot) #print('grad is'); #print(l_tot); return l_tot
def run(self): plt.imshow(self.data); plt.show(); plt.imshow(self.psf); plt.show(); #set up initial guesses #create initial parameters tt0 = np.zeros((self.n_grid,self.n_grid)) + self.wlim[1]; #begin with high uniform mass in each pixel tt0 = self.xi*np.log(np.exp(tt0/self.xi)-1); #print(tt0); tt0_k = fft.fft2(tt0); #take fft t_ini = self.complex_to_real(tt0_k.flatten()) #flatten to 1d for scipy and embed in 2R alpha_ini = 3.; f_ini = self.f_true; #m_1,mf = self.optimize_m(t_ini,self.xi,f_ini,alpha_ini); m_1 = np.loadtxt('mock00024.out'); m_1 = self.xi*np.log(np.exp(m_1/self.xi)-1); m_1 = fft.fft2(m_1); m_1 = self.complex_to_real(m_1.flatten()); a_1 = self.optimize_alpha(m_1,self.xi,f_ini,alpha_ini); print(a_1); mf,m_2 = self.optimize_m(tt0,self.xi,f_ini,a_1); print('new alpha'); print(a_1); return m_2;
def fft_convolve2d(x, y): """ 2D convolution, using FFT""" fr = fft.fft2(x) fr2 = fft.fft2(np.flipud(np.fliplr(y))) m, n = fr.shape cc = np.real(fft.ifft2(fr * fr2)) cc = np.roll(cc, -int(m / 2) + 1, axis=0) cc = np.roll(cc, -int(n / 2) + 1, axis=1) return cc
def lnlike_k(ws): ''' log likelihood ''' #ws = ws.reshape((n_grid,n_grid)); #ws = np.absolute(fft.ifft2(ws)); like = -0.5 * np.sum( (fft.ifft2(fft.fft2(ws) * fft.fft2(psf)) - data)**2 / sig_noise**2) like = np.real(like) #print('like is:'); #print(like); return like
def lnlike_k(ws): ''' log likelihood w/ periodic boundary conditions (need for solving w/ respect to fourier coefficients) ''' like = -0.5 * np.sum( (np.real(fft.ifft2(fft.fft2(ws) * fft.fft2(psf))) - data)**2 / sig_noise**2) #like = -0.5 * np.sum((np.real(fft.fft2(fft.fft2(ws)))- data)**2/sig_noise**2); #like = np.real(like); #print('like is:'); #print(like); return like
def loss_fn(self,wsp_k,xi,f,alpha): wsp_k = self.real_to_complex(wsp_k); #2*reals -> complex wsp_k = wsp_k.reshape((self.n_grid,self.n_grid)); #reshape to 2d wsp = np.real(fft.ifft2(wsp_k)); ws = xi*np.log(np.exp(wsp/xi)+1) #reparametrize from m_prime back to m ws_k = fft.fft2(ws); return self.loss_like(ws_k) - self.loss_prior(ws,f,alpha);
def loss_fn_real(self, wsp_k, f, alpha): wsp_k = wsp_k.reshape((self.n_grid, self.n_grid)) #reshape to 2d wsp = np.real(fft.ifft2(wsp_k)) ws = wsp #reparametrize from m_prime back to m ws_k = fft.fft2(ws) return self.loss_like(ws_k) - self.loss_prior(ws, f, alpha)
def makeMock(grnd): grnd_x = np.around(grnd['xnano'] / 100).astype(int) grnd_y = np.around(grnd['ynano'] / 100).astype(int) grnd_arr = np.zeros((64, 64)) grnd_arr[grnd_y, grnd_x] = grnd['intensity'] grnd_arr = grnd_arr / np.max(grnd_arr) grnd_conv = np.real(fft.ifft2(fft.fft2(grnd_arr) * psf_k)) return grnd_conv
def grad_like(self,wsp,ws,ws_k,xi): #print('start grad_like') conv = np.real(fft.ifft2(ws_k*self.psf_k)); #convolution of ws with psf term1 = (conv - self.data)/self.n_grid**2 /self.sig_noise**2 #term thats squared in like (with N in denom) grad = np.zeros((self.n_grid,self.n_grid),dtype='complex') for i in range(0,self.n_grid): for j in range(0,self.n_grid): #try to modulate by hand ft1 = fft.fft2(1/(1+np.exp(-1*wsp/xi))); ftp = np.roll(ft1,(i,j),axis=(0,1)); term2 = fft.ifft2(ftp*self.psf_k); grad[i,j] = np.sum(term1*term2); grad_real = self.complex_to_real(np.conj(grad.flatten())); #embed to 2R #print('end grad_like'); return grad_real; #return 1d array
def run(self): #plt.imshow(self.data); #plt.show(); #plt.imshow(self.psf); #plt.show(); #set up initial guesses #create initial parameters tt0 = np.zeros((self.n_grid, self.n_grid)) + 2 * self.wlim[1] #begin with high uniform mass in each pixel tt0 = self.xi * np.log(np.exp(tt0 / self.xi) - 1) tt0_k = fft.fft2(tt0) #take fft t_ini = self.complex_to_real( tt0_k.flatten()) #flatten to 1d for scipy and embed in 2R alpha_ini = -1.25 f_ini = self.f_true return self.optimize_m(t_ini, self.xi, f_ini, alpha_ini)
theta_grid = np.linspace(0., 1., n_grid) # gridding of theta (same as pixels) #create true values - assign to grid x_true = np.abs(np.random.rand(Ndata)) # location of sources y_true = np.abs(np.random.rand(Ndata)) #w_true = np.abs(np.random.rand(Ndata))+1; #true grid needs to be set up with noise w_true_grid = np.zeros((n_grid, n_grid)) for x, y, w in zip(x_true, y_true, w_true): w_true_grid[np.argmin(np.abs(theta_grid - x)), np.argmin(np.abs(theta_grid - y))] = w data = np.real(fft.ifft2( fft.fft2(w_true_grid) * fft.fft2(psf))) #+ np.absolute(sig_noise* np.random.randn(n_grid,n_grid)); data3 = signal.convolve(w_true_grid, psf) diff = int((len(data3[:, 0]) - n_grid) / 2) data3 = data3[diff:n_grid + diff, diff:n_grid + diff] #data = data3; ''' fig, ax = plt.subplots(1,2) ax[0].imshow(w_true_grid); ax[0].set_title('True Positions') #ax[1].imshow(data3[:-4,:-4]); ax[1].imshow(data4); ax[1].set_title('Observed Data') plt.show(); ''' #create fft of psf
#pix pix_1d = np.linspace(0., 1., n_grid) # pixel gridding sig_psf = 0.03 #gaussian sigma in pix print('sig_psf') print(sig_psf) sig_sq = sig_psf**2 #so we don't have to compute #create our psf mid = int(n_grid / 2) x, y = np.meshgrid(pix_1d, pix_1d) psf = np.exp(-((y - pix_1d[mid])**2 + (x - pix_1d[mid])**2) / 2 / sig_psf**2) #keep in mind difference between x and y position and indices! Here, you are given indices, but meshgrid is in x-y coords #fourier transform of psf psf_k = fft.fft2(psf) start = 0 def readImages(imageDir, nImages): imA = np.zeros((n_grid, n_grid, nImages)) for i in range(0, nImages): imageId = str(i) impath = imageDir + str(imageId) + '.dat' imA[:, :, i] = np.loadtxt(impath) return imA def runSB(imageDir, saveDir, psf, psf_k, imageArray):
n_grid = 5 pix_1d = np.linspace(0., 1., n_grid) # pixel gridding fdensity_true = float(Ndata) / float(n_grid**2) #number density of obj in 1d sig_psf = 0.01 # psf width sig_noise = 0.01 # noise level #create coordinate grid theta_grid = np.linspace(0., 1., n_grid) # gridding of theta (same as pixels) mid = int(n_grid / 2) x, y = np.meshgrid(theta_grid, theta_grid) psf = np.exp(-((y - theta_grid[mid])**2 + (x - theta_grid[mid])**2) / 2. / sig_psf**2) #create fft of psf psf_k = fft.fft2(psf) #create true values - assign to grid #x_true = np.abs(np.random.rand(Ndata)) # location of sources #y_true = np.abs(np.random.rand(Ndata)) x_true = [0.5] y_true = [0.5] w_true = np.ones(Ndata) * 5 #w_true = np.abs(np.random.rand(Ndata))+1 #true grid needs to be set up with noise w_true_grid = np.zeros((n_grid, n_grid)) for x, y, w in zip(x_true, y_true, w_true): w_true_grid[np.argmin(np.abs(theta_grid - x)), np.argmin(np.abs(theta_grid - y))] = w
def loss_fn(wsp_k,xi,f,alpha): wsp = np.real(fft.ifft2(wsp_k)); ws = xi*np.log(np.exp(wsp/xi)+1) #reparametrize from m_prime back to m ws_k = fft.fft2(ws); return loss_like(ws_k) - loss_prior(ws,f,alpha);
#create mock data to run on ######################################################################## #create coordinate grid theta_grid = np.linspace(0., 1., n_grid) # gridding of theta (same as pixels) #create true values - assign to grid x_true = np.abs(np.random.rand(Ndata)) # location of sources y_true = np.abs(np.random.rand(Ndata)); #w_true = np.abs(np.random.rand(Ndata))+1; #true grid needs to be set up with noise w_true_grid = np.zeros((n_grid,n_grid)) for x,y, w in zip(x_true,y_true, w_true): w_true_grid[np.argmin(np.abs(theta_grid - x)),np.argmin(np.abs(theta_grid - y))] = w data =np.real(fft.ifft2(fft.fft2(w_true_grid)*fft.fft2(psf)))+ sig_noise * np.random.randn(n_grid,n_grid); ######################################################################## #now begin the actual execution ######################################################################## #now we begin the optimization tt0 = np.zeros(n_grid**2) +3; #begin with high uniform M #begin with the simple method of just minimizing f_curr = fdensity_true; a_curr = 2; sig_delta = 0.75; posterior = lnpost(tt0,f_curr,a_curr,sig_delta); print(posterior)
theta_grid = np.linspace(0., 1., n_grid) # gridding of theta (same as pixels) #create true values - assign to grid x_true = np.abs(np.random.rand(Ndata)) # location of sources y_true = np.abs(np.random.rand(Ndata)) #w_true = np.abs(np.random.rand(Ndata))+1; #true grid needs to be set up with noise w_true_grid = np.zeros((n_grid, n_grid)) for x, y, w in zip(x_true, y_true, w_true): w_true_grid[np.argmin(np.abs(theta_grid - x)), np.argmin(np.abs(theta_grid - y))] = w data = np.real( fft.ifft2(fft.fft2(w_true_grid) * fft.fft2(psf))) + sig_noise * np.random.randn(n_grid, n_grid) ''' fig, ax = plt.subplots(1,2) ax[0].imshow(w_true_grid); ax[0].set_title('True Positions') #ax[1].imshow(data3[:-4,:-4]); ax[1].imshow(data4); ax[1].set_title('Observed Data') plt.show(); ''' #create fft of psf psf_k = fft.fft2(psf) ######################################################################## #now begin the actual execution
#create coordinate grid theta_grid = np.linspace(0., 1., n_grid) # gridding of theta (same as pixels) #create true values - assign to grid x_true = np.abs(np.random.rand(Ndata)) # location of sources y_true = np.abs(np.random.rand(Ndata)) #w_true = np.abs(np.random.rand(Ndata))+1; #true grid needs to be set up with noise w_true_grid = np.zeros((n_grid, n_grid)) for x, y, w in zip(x_true, y_true, w_true): w_true_grid[np.argmin(np.abs(theta_grid - x)), np.argmin(np.abs(theta_grid - y))] = w data = np.real(fft.ifft2(fft.fft2(w_true_grid) * fft.fft2(psf))) + np.absolute( sig_noise * np.random.randn(n_grid, n_grid)) data3 = signal.convolve(w_true_grid, psf) diff = int((len(data3[:, 0]) - n_grid) / 2) data3 = data3[diff:n_grid + diff, diff:n_grid + diff] #data = data3; ''' fig, ax = plt.subplots(1,2) ax[0].imshow(w_true_grid); ax[0].set_title('True Positions') #ax[1].imshow(data3[:-4,:-4]); ax[1].imshow(data4); ax[1].set_title('Observed Data') plt.show(); ''' #create fft of psf
x, y = np.meshgrid(grid1d, grid1d) psf = np.exp(-((y - grid1d[mid])**2 + (x - grid1d[mid])**2) / 2. / sig_psf**2) xx = np.linspace(-0.5, 0.5, n_grid) bump = np.exp(-0.5 * xx**2 / sig_psf**2) #bump /= np.trapz(bump) # normalize the integral to 1 _psf = bump[:, np.newaxis] * bump[np.newaxis, :] fig, ax = plt.subplots(1, 2) ax[0].imshow(psf) ax[0].set_title('psf') ax[1].imshow(_psf) ax[1].set_title('psf') plt.savefig('test0.png') data = np.real(fft.ifft2(fft.fft2(w_true_grid) * fft.fft2(_psf))) data_p = Signal.fftconvolve(w_true_grid, _psf) #, mode='same') #psf_k = fft.fft2(psf); psf_k = fft.fft2(_psf) def lnlike_k(ws): ''' log likelihood w/ periodic boundary conditions (need for solving w/ respect to fourier coefficients) ''' return 0.5 * np.sum( (aSignal.convolve(ws, _psf) - data_p)**2) / sig_noise**2 #return 0.5 * np.sum(((fft.ifft2(ws*psf_k))- data_p)**2)/sig_noise**2 #return 0.5 * np.sum((np.real(fft.ifft2(ws*psf_k))- data)**2)/sig_noise**2
back_std = 22.0; #given by challenge, may want to est from image w/ 3sig clip lam = 723.0; #wavelength NA = 1.4 #numerical aperture; FWHM = lam/(2*NA); #fwhm in nm of gaussian psf sig_nm = FWHM/(2*np.log(2.0)); sig_psf = sig_nm/100/64;#gaussian sigma in pix sig_sq = sig_psf**2 #so we don't have to compute sig_noise = back_std; #create our psf mid = int(n_grid/2); x,y = np.meshgrid(pix_1d,pix_1d); psf = np.exp(-((y-pix_1d[mid])**2 + (x - pix_1d[mid])**2)/2/sig_psf**2); #keep in mind difference between x and y position and indices! Here, you are given indices, but meshgrid is in x-y coords #fourier transform of psf psf_k = fft.fft2(psf); img = plt.imread('/home/moss/SMLM/data/sequence/00002.tif'); img = img-np.average(img[img<np.average(img)+3*np.std(img)]); data = img/np.max(img); xi = data + 0.5; f = 7/(64**2); sig_noise = np.std(data[data<np.average(data)+3*np.std(data)]); print('noise is'); print(sig_noise); norm_sig = 0.75; norm_mean = -5; wlim = (0.01,5); def roll_fft(f): r,c = np.shape(f); f2 = np.roll(f,(c//2)); f3 = np.roll(f2,(r//2),axis=-2);
def lnlike(ws): ''' log likelihood ''' return -0.5 * np.sum((np.real(fft.ifft2(fft.fft2(ws)*fft.fft2(psf))) - data)**2/sig_noise**2)
y_true = np.abs(np.random.rand(Ndata)) #w_true = np.abs(np.random.rand(Ndata))+1; #true grid needs to be set up with noise w_true_grid = np.zeros((n_grid, n_grid)) for x, y, w in zip(x_true, y_true, w_true): w_true_grid[np.argmin(np.abs(theta_grid - x)), np.argmin(np.abs(theta_grid - y))] = w data4 = convolvesame_fft(w_true_grid, psf) + sig_noise # * np.random.randn(n_grid,n_grid); data2 = Psi(w_true_grid) + sig_noise # * np.random.randn(n_grid,n_grid); data3 = np.real( fft.ifft2( fft.fft2(np.pad(w_true_grid, ((5, 0), (5, 0)), 'constant')) * fft.fft2(np.pad(psf, ((5, 0), (5, 0)), 'constant'))) ) + sig_noise # * np.random.randn(n_grid,n_grid); data = np.real( fft.ifft2(fft.fft2(w_true_grid) * fft.fft2(psf))) + sig_noise * np.random.randn(n_grid, n_grid) #print(data-data2); ''' fig, ax = plt.subplots(1,2) ax[0].imshow(w_true_grid); ax[0].set_title('True Positions') #ax[1].imshow(data3[:-4,:-4]); ax[1].imshow(data4); ax[1].set_title('Observed Data') plt.show(); '''