|
| 1 | +import os.path |
| 2 | +from unittest import TestCase |
| 3 | + |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +from aspire.estimation.noise import WhiteNoiseEstimator |
| 7 | +from aspire.source.simulation import Simulation |
| 8 | +from aspire.utils.coor_trans import grid_2d |
| 9 | +from aspire.utils.filters import RadialCTFFilter, ScalarFilter |
| 10 | +from aspire.utils.matrix import anorm |
| 11 | +from aspire.volume import Volume |
| 12 | + |
| 13 | +DATA_DIR = os.path.join(os.path.dirname(__file__), 'saved_test_data') |
| 14 | + |
| 15 | + |
| 16 | +class PreprocessPLTestCase(TestCase): |
| 17 | + def setUp(self): |
| 18 | + |
| 19 | + self.L = 64 |
| 20 | + self.n = 128 |
| 21 | + |
| 22 | + self.noise_variance = 0.004 |
| 23 | + self.noise_filter = ScalarFilter(dim=2, value=self.noise_variance) |
| 24 | + |
| 25 | + self.sim = Simulation( |
| 26 | + L=self.L, |
| 27 | + n=self.n, |
| 28 | + filters=[RadialCTFFilter(defocus=d) for d in np.linspace(1.5e4, 2.5e4, 7)], |
| 29 | + noise_filter=self.noise_filter |
| 30 | + ) |
| 31 | + self.imgs_org = self.sim.images(start=0, num=self.n).asnumpy() |
| 32 | + |
| 33 | + def testPhaseFlip(self): |
| 34 | + self.sim.phase_flip() |
| 35 | + imgs_pf = self.sim.images(start=0, num=self.n).asnumpy() |
| 36 | + |
| 37 | + # check energy conservation |
| 38 | + self.assertTrue(anorm(self.imgs_org), anorm(imgs_pf)) |
| 39 | + |
| 40 | + def testDownsample(self): |
| 41 | + max_resolution = 8 |
| 42 | + self.sim.downsample(max_resolution) |
| 43 | + imgs_ds = self.sim.images(start=0, num=self.n).asnumpy() |
| 44 | + |
| 45 | + # check resolution |
| 46 | + self.assertTrue(max_resolution, imgs_ds.shape[1]) |
| 47 | + # check energy conservation after downsample |
| 48 | + self.assertTrue(anorm(self.imgs_org), anorm(imgs_ds)) |
| 49 | + |
| 50 | + def testNormBackground(self): |
| 51 | + bg_radius = 1.0 |
| 52 | + grid = grid_2d(self.L) |
| 53 | + mask = (grid['r'] > bg_radius) |
| 54 | + self.sim.normalize_background() |
| 55 | + imgs_nb = self.sim.images(start=0, num=self.n).asnumpy() |
| 56 | + new_mean = np.mean(imgs_nb[:, mask]) |
| 57 | + new_variance = np.var(imgs_nb[:, mask]) |
| 58 | + |
| 59 | + # new mean of noise should close to zero and 1 for variance |
| 60 | + self.assertTrue(new_mean < 1e-7 and abs(new_variance-1) < 1e-7) |
| 61 | + |
| 62 | + def testWhiten(self): |
| 63 | + noise_estimator = WhiteNoiseEstimator(self.sim) |
| 64 | + self.sim.whiten(noise_estimator.filter) |
| 65 | + imgs_wt = self.sim.images(start=0, num=self.n).asnumpy() |
| 66 | + |
| 67 | + # calculate correlation between two neighboring pixels from background |
| 68 | + corr_coef = np.corrcoef(imgs_wt[:, self.L-1, self.L-1], |
| 69 | + imgs_wt[:, self.L-2, self.L-1]) |
| 70 | + print(corr_coef) |
| 71 | + # correlation should be low |
| 72 | + self.assertTrue(np.abs(corr_coef[0, 1]) < 1e-1) |
| 73 | + |
| 74 | + def testInvertContrast(self): |
| 75 | + vols = Volume(np.load(os.path.join(DATA_DIR, 'clean70SRibosome_vol.npy'))) |
| 76 | + vols1 = vols.downsample((8*np.ones(3, dtype=int))) |
| 77 | + vols2 = -1.0*vols1 |
| 78 | + noise_var = 1.3957e-4 |
| 79 | + noise_filter = ScalarFilter(dim=2, value=noise_var) |
| 80 | + sim1 = Simulation( |
| 81 | + L=8, |
| 82 | + n=128, |
| 83 | + vols=vols1, |
| 84 | + filters=[RadialCTFFilter(defocus=d) for d in np.linspace(1.5e4, 2.5e4, 7)], |
| 85 | + noise_filter=noise_filter |
| 86 | + ) |
| 87 | + sim2 = Simulation( |
| 88 | + L=8, |
| 89 | + n=128, |
| 90 | + vols=vols2, |
| 91 | + filters=[RadialCTFFilter(defocus=d) for d in np.linspace(1.5e4, 2.5e4, 7)], |
| 92 | + noise_filter=noise_filter |
| 93 | + ) |
| 94 | + |
| 95 | + imgs1 = sim1.images(start=0, num=128).asnumpy() |
| 96 | + # need to set the negative images to the second simulation object |
| 97 | + sim2._cached_im = -imgs1 |
| 98 | + |
| 99 | + sim1.invert_contrast() |
| 100 | + sim2.invert_contrast() |
| 101 | + imgs1_rc = sim1.images(start=0, num=128).asnumpy() |
| 102 | + imgs2_rc = sim2.images(start=0, num=128).asnumpy() |
| 103 | + |
| 104 | + # all images should be the same after inverting contract |
| 105 | + self.assertTrue(np.allclose(imgs1_rc, imgs2_rc)) |
| 106 | + |
0 commit comments