def setUp(self): n = 32 L = 8 filters = [ RadialCTFFilter(5, 200, defocus=d, Cs=2.0, alpha=0.1) for d in np.linspace(1.5e4, 2.5e4, 7) ] self.dtype = np.float32 self.noise_var = 0.1848 # Initial noise filter to generate noise images. # Noise variance is set to a value far away that is used to calculate # covariance matrix and CWF coefficients in order to check the function # for rebuilding positive definite covariance matrix. noise_filter = ScalarFilter(dim=2, value=self.noise_var * 0.001) self.src = Simulation(L, n, unique_filters=filters, dtype=self.dtype, noise_filter=noise_filter) self.basis = FFBBasis2D((L, L), dtype=self.dtype) self.coeff = self.basis.evaluate_t(self.src.images(0, self.src.n)) self.ctf_idx = self.src.filter_indices self.ctf_fb = [f.fb_mat(self.basis) for f in self.src.unique_filters] self.cov2d = RotCov2D(self.basis) self.bcov2d = BatchedRotCov2D(self.src, self.basis, batch_size=7)
def setUp(self): self.dtype = np.float32 L = 8 n = 32 pixel_size = 5.0 * 65 / L voltage = 200 defocus_min = 1.5e4 defocus_max = 2.5e4 defocus_ct = 7 self.noise_var = 1.3957e-4 noise_filter = ScalarFilter(dim=2, value=self.noise_var) unique_filters = [ RadialCTFFilter(pixel_size, voltage, defocus=d, Cs=2.0, alpha=0.1) for d in np.linspace(defocus_min, defocus_max, defocus_ct) ] vols = Volume( np.load(os.path.join(DATA_DIR, "clean70SRibosome_vol.npy")).astype( self.dtype ) ) # RCOPT vols = vols.downsample((L * np.ones(3, dtype=int))) * 1.0e3 # Since FFBBasis2D doesn't yet implement dtype, we'll set this to double to match its built in types. sim = Simulation( n=n, L=L, vols=vols, unique_filters=unique_filters, offsets=0.0, amplitudes=1.0, dtype=self.dtype, noise_filter=noise_filter, ) self.basis = FFBBasis2D((L, L), dtype=self.dtype) self.h_idx = sim.filter_indices self.h_ctf_fb = [filt.fb_mat(self.basis) for filt in unique_filters] self.imgs_clean = sim.projections() self.imgs_ctf_clean = sim.clean_images() self.imgs_ctf_noise = sim.images(start=0, num=n) self.cov2d = RotCov2D(self.basis) self.coeff_clean = self.basis.evaluate_t(self.imgs_clean) self.coeff = self.basis.evaluate_t(self.imgs_ctf_noise)
def build(self): """ Computes the FSPCA basis. This may take some time for large image stacks. """ coef = self.basis.evaluate_t(self.src.images(0, self.src.n)) if self.noise_var is None: from aspire.noise import WhiteNoiseEstimator logger.info("Estimating the noise of images.") self.noise_var = WhiteNoiseEstimator(self.src).estimate() logger.info(f"Setting noise_var={self.noise_var}") cov2d = RotCov2D(self.basis) covar_opt = { "shrinker": "frobenius_norm", "verbose": 0, "max_iter": 250, "iter_callback": [], "store_iterates": False, "rel_tolerance": 1e-12, "precision": "float64", "preconditioner": "identity", } self.mean_coef_est = cov2d.get_mean(coef) self.covar_coef_est = cov2d.get_covar( coef, mean_coeff=self.mean_coef_est, noise_var=self.noise_var, covar_est_opt=covar_opt, ) # Create the arrays to be packed by _compute_spca self.eigvals = np.zeros(self.basis.count, dtype=self.dtype) self.eigvecs = BlkDiagMatrix.empty(2 * self.basis.ell_max + 1, dtype=self.dtype) self.spca_coef = np.zeros((self.src.n, self.basis.count), dtype=self.dtype) self._compute_spca(coef) self._compress(self.components)
coeff_noise = ffbbasis.evaluate_t(imgs_noise) # Create the Cov2D object and calculate mean and covariance for clean images without CTF. # Given the clean Fourier-Bessel coefficients, we can estimate the symmetric # mean and covariance. Note that these are not the same as the sample mean and # covariance, since these functions use the rotational and reflectional # symmetries of the distribution to constrain to further constrain the # estimate. Note that the covariance matrix estimate is not a full matrix, # but is block diagonal. This form is a consequence of the symmetry # constraints, so to reduce space, only the diagonal blocks are stored. The # mean and covariance estimates will allow us to evaluate the mean and # covariance estimates from the filtered, noisy data, later. logger.info( "Get 2D covariance matrices of clean and noisy images using FB coefficients." ) cov2d = RotCov2D(ffbbasis) mean_coeff = cov2d.get_mean(coeff_clean) covar_coeff = cov2d.get_covar(coeff_clean, mean_coeff, noise_var=0) # Estimate mean and covariance for noise images with CTF and shrink method. # We now estimate the mean and covariance from the Fourier-Bessel # coefficients of the noisy, filtered images. These functions take into # account the filters applied to each image to undo their effect on the # estimates. For the covariance estimation, the additional information of # the estimated mean and the variance of the noise are needed. Again, the # covariance matrix estimate is provided in block diagonal form. covar_opt = { "shrinker": "frobenius_norm", "verbose": 0, "max_iter": 250, "iter_callback": [],