def setUp(self): L = 8 n = 32 C = 1 SNR = 1 pixel_size = 5 voltage = 200 defocus_min = 1.5e4 defocus_max = 2.5e4 defocus_ct = 7 Cs = 2.0 alpha = 0.1 filters = [ RadialCTFFilter(pixel_size, voltage, defocus=d, Cs=2.0, alpha=0.1) for d in np.linspace(defocus_min, defocus_max, defocus_ct) ] # Since FFBBasis2D doesn't yet implement dtype, we'll set this to double to match its built in types. sim = Simulation(n=n, C=C, filters=filters, dtype='double') vols = np.load(os.path.join(DATA_DIR, 'clean70SRibosome_vol.npy')) vols = vols[..., np.newaxis] vols = downsample(vols, (L * np.ones(3, dtype=int))) sim.vols = vols self.basis = FFBBasis2D((L, L)) # use new methods to generate random rotations and clean images sim.rots = qrand_rots(n, seed=0) self.imgs_clean = vol2img(vols[..., 0], sim.rots) self.h_idx = np.array([filters.index(f) for f in sim.filters]) self.filters = filters self.h_ctf_fb = [filt.fb_mat(self.basis) for filt in self.filters] self.imgs_ctf_clean = sim.eval_filters(self.imgs_clean) sim.cache(self.imgs_ctf_clean) power_clean = anorm(self.imgs_ctf_clean)**2 / np.size( self.imgs_ctf_clean) self.noise_var = power_clean / SNR self.imgs_ctf_noise = self.imgs_ctf_clean + np.sqrt( self.noise_var) * randn(L, L, n, seed=0) 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)
logger.info( 'Generate random distributed rotation angles and obtain corresponding 2D clean images.' ) rots = qrand_rots(num_imgs, seed=0) imgs_clean = vol2img(sim.vols[..., 0], rots) # Assign the CTF information and index for each image h_idx = np.array([filters.index(f) for f in sim.filters]) # Evaluate CTF in the 8X8 FB basis h_ctf_fb = [filt.fb_mat(ffbbasis) for filt in filters] # Apply the CTF to the clean images. logger.info('Apply CTF filters to clean images.') imgs_ctf_clean = Image(sim.eval_filters(imgs_clean)) sim.cache(imgs_ctf_clean) # imgs_ctf_clean is an Image object. Convert to numpy array for subsequent statements imgs_ctf_clean = imgs_ctf_clean.asnumpy() # Apply the noise at the desired singal-noise ratio to the filtered clean images logger.info('Apply noise filters to clean images.') power_clean = anorm(imgs_ctf_clean)**2 / np.size(imgs_ctf_clean) noise_var = power_clean / sn_ratio imgs_noise = imgs_ctf_clean + np.sqrt(noise_var) * randn( img_size, img_size, num_imgs, seed=0) # Expand the images, both clean and noisy, in the Fourier-Bessel basis. This # can be done exactly (that is, up to numerical precision) using the # `basis.expand` function, but for our purposes, an approximation will do. # Since the basis is close to orthonormal, we may approximate the exact