def __init__(self, psfs, timestep): self.shape = psfs.shape if (len(self.shape) == 2): c_psfs = numpy.ascontiguousarray(recenterPSF.recenterPSF(psfs), dtype = numpy.float) self.c_fista = fista_fft.initialize2D(c_psfs, timestep, self.shape[0], self.shape[1]) else: c_psfs = numpy.zeros(self.shape) for i in range(self.shape[2]): c_psfs[:,:,i] = recenterPSF.recenterPSF(psfs[:,:,i]) c_psfs = numpy.ascontiguousarray(c_psfs, dtype = numpy.float) self.c_fista = fista_fft.initialize3D(c_psfs, timestep, self.shape[0], self.shape[1], self.shape[2])
def __init__(self, psfs, rho): super(ADMMLasso, self).__init__() self.shape = psfs.shape c_psf = numpy.ascontiguousarray(recenterPSF.recenterPSF(psfs[:,:,0]), dtype = numpy.float64) self.c_admm_lasso = admm_lasso.initialize(c_psf, rho, self.shape[0], self.shape[1])
def test_matched_filter5(): """ Verify that the filter results are correct. """ x_size = 80 y_size = 90 objects = numpy.zeros((1, 5)) # Make filter with unit sum. objects[0,:] = [x_size/2, y_size/2, 1.0, 1.0, 1.0] psf = dg.drawGaussians((x_size, y_size), objects) psf = psf/numpy.sum(psf) flt = matchedFilterC.MatchedFilter(psf) # Make test image. image = numpy.zeros((x_size, y_size)) image[int(x_size/2), int(y_size/2)] = float(100) mf_conv = flt.convolve(image) t1 = numpy.fft.fft2(recenterPSF.recenterPSF(psf)) t2 = numpy.fft.fft2(image) np_conv = numpy.real(numpy.fft.ifft2(t1*t2)) assert(numpy.allclose(mf_conv, np_conv)) flt.cleanup()
def __init__(self, psfs, rho, **kwds): super(ADMMLasso, self).__init__(**kwds) self.shape = psfs.shape # Initialize C library. self.c_admm_lasso = admm_lasso.initialize3D(rho, self.shape[0], self.shape[1], self.shape[2]) # # Do the ADMM math on the Python side. # # Calculate A matrix. nz = self.shape[2] A = admmMath.Cells(nz, 1) for i in range(nz): tmp = recenterPSF.recenterPSF(psfs[:, :, i]) A[i, 0] = numpy.fft.fft2(tmp) # Calculate A transpose. At = admmMath.transpose(A) # Calculate AtA + rhoI inverse. G = admmMath.multiplyMatMat(At, A) for i in range(nz): G[i, i] += admmMath.identityMatrix(G.getMatrixShape(), scale=rho) [L, D, U] = admmMath.lduG(G) L_inv = admmMath.invL(L) D_inv = admmMath.invD(D) U_inv = admmMath.invU(U) G_inv = admmMath.multiplyMatMat(U_inv, admmMath.multiplyMatMat(D_inv, L_inv)) # Initialize A and G_inv matrices in the C library. fft_size = int(self.shape[1] / 2 + 1) for i in range(nz): # Remove redundant frequencies that FFTW doesn't use. c_A = A[i, 0][:, :fft_size] c_A = numpy.ascontiguousarray(c_A, dtype=numpy.complex128) admm_lasso.initializeA(self.c_admm_lasso, c_A, i) for i in range(nz): for j in range(nz): # Remove redundant frequencies that FFTW doesn't use. # # We index (j,i) here because this is what gives us results that # match admm_3d (the pure Python version of 3D ADMM. # c_G_inv = G_inv[j, i][:, :fft_size] c_G_inv = numpy.ascontiguousarray(c_G_inv, dtype=numpy.complex128) admm_lasso.initializeGInv(self.c_admm_lasso, c_G_inv, i * nz + j)
def __init__(self, psf, estimate_fft_plan = False, memoize = False, max_diff = 0.1): """ If you are only going to use this object on a few images using estimate_fft_plan = True is a good idea as the initialization will go a lot faster, particularly for large images. If you think that you may being repeatedly asking it to convolve the same image or almost the same image then using memoization might be a good idea. """ self.memoize = memoize self.psf_shape = psf.shape rc_psf = recenterPSF.recenterPSF(psf) if self.memoize: self.mfilter = m_filter.initialize(rc_psf, max_diff, rc_psf.shape[0], rc_psf.shape[1], int(estimate_fft_plan)) else: self.mfilter = m_filter.initialize(rc_psf, 0.0, rc_psf.shape[0], rc_psf.shape[1], int(estimate_fft_plan))
def __init__(self, psfs, timestep): if (psfs.shape[0] != psfs.shape[1]): print("The PSF must be square (in X-Y)!") exit() self.shape = psfs.shape self.a_mats = psfs self.a_mats_fft = [] self.a_mats_transpose_fft = [] self.image = None self.image_fft = None self.l_term = None self.nz = self.shape[2] self.t_step = timestep self.tk = None self.x = None self.y = None # Compute FFTs of PSFs. for i in range(self.nz): psf = recenterPSF.recenterPSF(psfs[:,:,i]) psf_fft = numpy.fft.fft2(psf) self.a_mats_fft.append(psf_fft) self.a_mats_transpose_fft.append(numpy.conj(psf_fft))
def __init__(self, psfs, timestep): if (psfs.shape[0] != psfs.shape[1]): print("The PSF must be square (in X-Y)!") exit() self.shape = psfs.shape self.a_mats = psfs self.a_mats_fft = [] self.a_mats_transpose_fft = [] self.image = None self.image_fft = None self.l_term = None self.nz = self.shape[2] self.t_step = timestep self.tk = None self.x = None self.y = None # Compute FFTs of PSFs. for i in range(self.nz): psf = recenterPSF.recenterPSF(psfs[:, :, i]) psf_fft = numpy.fft.fft2(psf) self.a_mats_fft.append(psf_fft) self.a_mats_transpose_fft.append(numpy.conj(psf_fft))
def __init__(self, psfs, timestep, dwls = False, **kwds): """ psfs is an array of psfs for different image planes (nx, ny, nz). They must be the same size as the image that will get analyzed. """ super(FISTA, self).__init__(**kwds) self.shape = psfs.shape self.a_mats = psfs self.a_mats_fft = [] self.a_mats_transpose_fft = [] self.dwls = dwls self.image_fft = None self.l_term = None self.nz = self.shape[2] self.t_step = timestep self.tk = None self.weights = None self.y = None # Compute FFTs of PSFs. for i in range(self.nz): psf = recenterPSF.recenterPSF(psfs[:,:,i]) psf_fft = numpy.fft.fft2(psf) self.a_mats_fft.append(psf_fft) self.a_mats_transpose_fft.append(numpy.conj(psf_fft))
def __init__(self, psfs, timestep): """ psfs is an array of psfs for different image planes (nx, ny, nz). They must be the same size as the image that will get analyzed. """ if (psfs.shape[0] != psfs.shape[1]): print("The PSF must be square (in X-Y)!") exit() self.shape = psfs.shape self.a_mats = psfs self.a_mats_fft = [] self.a_mats_transpose_fft = [] self.image = None self.image_fft = None self.l_term = None self.nz = self.shape[2] self.t_step = timestep self.tk = None self.x = None self.y = None # Compute FFTs of PSFs. for i in range(self.nz): psf = recenterPSF.recenterPSF(psfs[:, :, i]) psf_fft = numpy.fft.fft2(psf) self.a_mats_fft.append(psf_fft) self.a_mats_transpose_fft.append(numpy.conj(psf_fft))
def __init__(self, psfs, rho): super(ADMMLasso, self).__init__() self.shape = psfs.shape c_psf = numpy.ascontiguousarray(recenterPSF.recenterPSF(psfs[:, :, 0]), dtype=numpy.float64) self.c_admm_lasso = admm_lasso.initialize(c_psf, rho, self.shape[0], self.shape[1])
def __init__(self, psfs, timestep): """ For 2D psfs is an array of size (nx, ny). For 3D psfs is an array of psfs for different image planes (nx, ny, nz). The PSFs must be the same size as the image that will get analyzed. """ self.shape = psfs.shape if (len(self.shape) == 2): c_psfs = numpy.ascontiguousarray(recenterPSF.recenterPSF(psfs), dtype = numpy.float) self.c_fista = fista_fft.initialize2D(c_psfs, timestep, self.shape[0], self.shape[1]) else: c_psfs = numpy.zeros(self.shape) for i in range(self.shape[2]): c_psfs[:,:,i] = recenterPSF.recenterPSF(psfs[:,:,i]) c_psfs = numpy.ascontiguousarray(c_psfs, dtype = numpy.float) self.c_fista = fista_fft.initialize3D(c_psfs, timestep, self.shape[0], self.shape[1], self.shape[2])
def __init__(self, psf, estimate_fft_plan = False): """ If you are only going to use this object on a few images using estimate_fft_plan = True is a good idea as the initialization will go a lot faster, particularly for large images. """ self.psf_shape = psf.shape rc_psf = recenterPSF.recenterPSF(psf) self.mfilter = m_filter.initialize(rc_psf, rc_psf.shape[0], rc_psf.shape[1], int(estimate_fft_plan))
def __init__(self, psfs, timestep, dwls=False, **kwds): """ Psfs is an array of psfs for different image planes (nx, ny, nz). The PSFs must be the same size as the image that will get analyzed. """ super(FISTA, self).__init__(**kwds) self.dwls = dwls self.shape = psfs.shape c_psfs = numpy.zeros(self.shape) for i in range(self.shape[2]): c_psfs[:, :, i] = recenterPSF.recenterPSF(psfs[:, :, i]) c_psfs = numpy.ascontiguousarray(c_psfs, dtype=numpy.float) self.c_fista = fista_fft.initialize3D(c_psfs, timestep, self.shape[0], self.shape[1], self.shape[2], self.dwls)
def __init__(self, psfs, rho, **kwds): """ psfs is an array of psfs for different image planes (nx, ny, nz). They must be the same size as the image that will get analyzed. """ super(ADMM, self).__init__(**kwds) self.A = None self.At = None self.Atb = None self.G_inv = None self.rho = rho self.shape = psfs.shape self.u = None self.z = None # Calculate A matrix. nz = self.shape[2] self.A = admmMath.Cells(nz, 1) for i in range(nz): tmp = recenterPSF.recenterPSF(psfs[:, :, i]) self.A[i, 0] = numpy.fft.fft2(tmp) # Calculate A transpose. self.At = admmMath.transpose(self.A) # Calculate AtA + rhoI inverse. G = admmMath.multiplyMatMat(self.At, self.A) for i in range(nz): G[i, i] += admmMath.identityMatrix(G.getMatrixShape(), scale=rho) [L, D, U] = admmMath.lduG(G) L_inv = admmMath.invL(L) D_inv = admmMath.invD(D) U_inv = admmMath.invU(U) self.G_inv = admmMath.multiplyMatMat( U_inv, admmMath.multiplyMatMat(D_inv, L_inv))