From e8334c6054e5870f0030688818532d80f21c6c7b Mon Sep 17 00:00:00 2001 From: Chris Langfield Date: Fri, 28 Jan 2022 15:33:27 -0500 Subject: [PATCH 1/7] added --downsample option to aspire extract-particles --- src/aspire/commands/extract_particles.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/aspire/commands/extract_particles.py b/src/aspire/commands/extract_particles.py index 572691d849..ccd4a5e6ea 100644 --- a/src/aspire/commands/extract_particles.py +++ b/src/aspire/commands/extract_particles.py @@ -48,6 +48,12 @@ @click.option( "--overwrite", is_flag=True, help="Overwrite output if it already exists?" ) +@click.option( + "--downsample", + default=0, + type=int, + help="Optionally downsample the data to this resolution prior to saving to starfile/.mrcs stack.", +) def extract_particles( mrc_paths, coord_paths, @@ -57,6 +63,7 @@ def extract_particles( batch_size, save_mode, overwrite, + downsample, ): """ Given a dataset of full micrographs and corresponding coordinate files @@ -109,6 +116,9 @@ def extract_particles( particle_size=particle_size, ) + if downsample > 0: + src.downsample(downsample) + # saves to .mrcs and STAR file with column "_rlnImageName" src.save( starfile_out, batch_size=batch_size, save_mode=save_mode, overwrite=overwrite From ac4a37eee01e30873c5c53ef659bbeb0eddda512 Mon Sep 17 00:00:00 2001 From: Chris Langfield Date: Fri, 28 Jan 2022 15:33:35 -0500 Subject: [PATCH 2/7] added --downsample option to aspire extract-particles --- tests/test_coordinate_source.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_coordinate_source.py b/tests/test_coordinate_source.py index a1fc26d433..f159327205 100644 --- a/tests/test_coordinate_source.py +++ b/tests/test_coordinate_source.py @@ -343,7 +343,17 @@ def testCommand(self): "--particle_size=256", ], ) + result_downsample = runner.invoke( + extract_particles, + [ + f"--mrc_paths={self.data_folder}/*.mrc", + f"--coord_paths={self.data_folder}/sample*.box", + f"--starfile_out={self.data_folder}/saved_star_ds.star", + "--downsample=33", + ], + ) # check that all commands completed successfully self.assertTrue(result_box.exit_code == 0) self.assertTrue(result_coord.exit_code == 0) self.assertTrue(result_star.exit_code == 0) + self.assertTrue(result_downsample.exit_code == 0) From 32b8b98773d81fbfcd1aae0f136ed721cf00c4e6 Mon Sep 17 00:00:00 2001 From: Chris Langfield Date: Fri, 28 Jan 2022 15:57:24 -0500 Subject: [PATCH 3/7] added normalize_bg and invert_contrast flags --- src/aspire/commands/extract_particles.py | 31 ++++++++++++++++++------ tests/test_coordinate_source.py | 6 +++-- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/aspire/commands/extract_particles.py b/src/aspire/commands/extract_particles.py index ccd4a5e6ea..78a4219c20 100644 --- a/src/aspire/commands/extract_particles.py +++ b/src/aspire/commands/extract_particles.py @@ -37,6 +37,22 @@ is_flag=True, help="Set this flag if coordinate files contain (X,Y) particle centers", ) +@click.option( + "--downsample", + default=0, + type=int, + help="Downsample the data to this resolution prior to saving to starfile/.mrcs stack", +) +@click.option( + "--normalize_bg", + is_flag=True, + help="Normalize images to the background noise", +) +@click.option( + "--invert_contrast", + is_flag=True, + help="Invert the contrast of the images so molecules are shown in white", +) @click.option( "--batch_size", default=512, help="Batch size to load images from .mrc files" ) @@ -48,22 +64,18 @@ @click.option( "--overwrite", is_flag=True, help="Overwrite output if it already exists?" ) -@click.option( - "--downsample", - default=0, - type=int, - help="Optionally downsample the data to this resolution prior to saving to starfile/.mrcs stack.", -) def extract_particles( mrc_paths, coord_paths, starfile_out, particle_size, centers, + downsample, + normalize_bg, + invert_contrast, batch_size, save_mode, overwrite, - downsample, ): """ Given a dataset of full micrographs and corresponding coordinate files @@ -116,8 +128,13 @@ def extract_particles( particle_size=particle_size, ) + # optional preprocessing steps if downsample > 0: src.downsample(downsample) + if normalize_bg: + src.normalize_background() + if invert_contrast: + src.invert_contrast() # saves to .mrcs and STAR file with column "_rlnImageName" src.save( diff --git a/tests/test_coordinate_source.py b/tests/test_coordinate_source.py index f159327205..3d7e90c826 100644 --- a/tests/test_coordinate_source.py +++ b/tests/test_coordinate_source.py @@ -343,17 +343,19 @@ def testCommand(self): "--particle_size=256", ], ) - result_downsample = runner.invoke( + result_preprocess = runner.invoke( extract_particles, [ f"--mrc_paths={self.data_folder}/*.mrc", f"--coord_paths={self.data_folder}/sample*.box", f"--starfile_out={self.data_folder}/saved_star_ds.star", "--downsample=33", + "--normalize_bg", + "--invert_contrast", ], ) # check that all commands completed successfully self.assertTrue(result_box.exit_code == 0) self.assertTrue(result_coord.exit_code == 0) self.assertTrue(result_star.exit_code == 0) - self.assertTrue(result_downsample.exit_code == 0) + self.assertTrue(result_preprocess.exit_code == 0) From 843043f06ace6e6ea4b2e27fc23d03e6da481e13 Mon Sep 17 00:00:00 2001 From: Chris Langfield Date: Mon, 31 Jan 2022 09:00:13 -0500 Subject: [PATCH 4/7] add --whiten option --- src/aspire/commands/extract_particles.py | 10 ++++++++++ tests/test_coordinate_source.py | 1 + 2 files changed, 11 insertions(+) diff --git a/src/aspire/commands/extract_particles.py b/src/aspire/commands/extract_particles.py index 78a4219c20..e419c052c1 100644 --- a/src/aspire/commands/extract_particles.py +++ b/src/aspire/commands/extract_particles.py @@ -5,6 +5,7 @@ import click from click import UsageError +from aspire.noise import WhiteNoiseEstimator from aspire.source.coordinates import BoxesCoordinateSource, CentersCoordinateSource logger = logging.getLogger(__name__) @@ -48,6 +49,11 @@ is_flag=True, help="Normalize images to the background noise", ) +@click.option( + "--whiten", + is_flag=True, + help="Estimate the noise variance of the images and whiten", +) @click.option( "--invert_contrast", is_flag=True, @@ -72,6 +78,7 @@ def extract_particles( centers, downsample, normalize_bg, + whiten, invert_contrast, batch_size, save_mode, @@ -133,6 +140,9 @@ def extract_particles( src.downsample(downsample) if normalize_bg: src.normalize_background() + if whiten: + estimator = WhiteNoiseEstimator(src) + src.whiten(estimator.filter) if invert_contrast: src.invert_contrast() diff --git a/tests/test_coordinate_source.py b/tests/test_coordinate_source.py index 3d7e90c826..9050e65e46 100644 --- a/tests/test_coordinate_source.py +++ b/tests/test_coordinate_source.py @@ -351,6 +351,7 @@ def testCommand(self): f"--starfile_out={self.data_folder}/saved_star_ds.star", "--downsample=33", "--normalize_bg", + "--whiten", "--invert_contrast", ], ) From 050e020265eeb32b604255ecedc769825a41327a Mon Sep 17 00:00:00 2001 From: Chris Langfield Date: Mon, 31 Jan 2022 09:17:16 -0500 Subject: [PATCH 5/7] new black version formatting --- gallery/tutorials/basic_image_array.py | 4 +-- gallery/tutorials/class_averaging.py | 2 +- gallery/tutorials/lecture_feature_demo.py | 2 +- src/aspire/abinitio/commonline_base.py | 4 +-- src/aspire/abinitio/commonline_sync.py | 2 +- src/aspire/apple/apple.py | 2 +- src/aspire/apple/helper.py | 6 ++-- src/aspire/apple/picking.py | 2 +- src/aspire/basis/dirac.py | 2 +- src/aspire/basis/ffb_3d.py | 2 +- src/aspire/basis/steerable.py | 2 +- src/aspire/covariance/covar.py | 6 ++-- src/aspire/covariance/covar2d.py | 14 ++++---- src/aspire/ctf/ctf_estimator.py | 40 +++++++++++------------ src/aspire/image/image.py | 6 ++-- src/aspire/image/preprocess.py | 6 ++-- src/aspire/noise/noise.py | 8 ++--- src/aspire/operators/filters.py | 14 ++++---- src/aspire/optimization/conj_grad.py | 2 +- src/aspire/reconstruction/kernel.py | 4 +-- src/aspire/reconstruction/mean.py | 2 +- src/aspire/source/simulation.py | 6 ++-- src/aspire/storage/micrograph.py | 2 +- src/aspire/utils/matrix.py | 18 +++++----- src/aspire/utils/misc.py | 8 ++--- src/aspire/volume/__init__.py | 18 +++++----- tests/test_BlkDiagMatrix.py | 4 +-- tests/test_adaptive_support.py | 4 +-- tests/test_filters.py | 4 +-- tests/test_preprocess_pipeline.py | 2 +- tests/test_utils.py | 4 +-- tests/test_volume.py | 4 +-- tests/test_wbemd.py | 2 +- 33 files changed, 104 insertions(+), 104 deletions(-) diff --git a/gallery/tutorials/basic_image_array.py b/gallery/tutorials/basic_image_array.py index e44aa48763..023c97e092 100644 --- a/gallery/tutorials/basic_image_array.py +++ b/gallery/tutorials/basic_image_array.py @@ -92,7 +92,7 @@ # Lets say we want to add different kind of noise to the images. # We can create our own function. Here we want to apply in two dimensions. def noise_function(x, y): - return np.exp(-(x * x + y * y) / (2 * 0.3 ** 2)) + return np.exp(-(x * x + y * y) / (2 * 0.3**2)) # We can create a custom filter from that function. @@ -168,7 +168,7 @@ def noise_function(x, y): def radial_profile(data): y, x = np.indices((data.shape)) # Distance from origin to lower left corner - r = np.sqrt(x ** 2 + y ** 2).astype(int) + r = np.sqrt(x**2 + y**2).astype(int) binsum = np.bincount(r.ravel(), np.log(1 + data.ravel())) bincount = np.bincount(r.ravel()) # Return the mean per bin diff --git a/gallery/tutorials/class_averaging.py b/gallery/tutorials/class_averaging.py index c60ce22724..dcf1272b60 100644 --- a/gallery/tutorials/class_averaging.py +++ b/gallery/tutorials/class_averaging.py @@ -136,7 +136,7 @@ # Using the sample variance, we'll compute a target noise variance # Noise var = np.var(src.images(0, src.n).asnumpy()) -noise_var = var * 2 ** 4 +noise_var = var * 2**4 # We create a uniform noise to apply to the 2D images noise_filter = ScalarFilter(dim=2, value=noise_var) diff --git a/gallery/tutorials/lecture_feature_demo.py b/gallery/tutorials/lecture_feature_demo.py index d707431d2d..eb8f4a48d4 100644 --- a/gallery/tutorials/lecture_feature_demo.py +++ b/gallery/tutorials/lecture_feature_demo.py @@ -335,7 +335,7 @@ def noise_function(x, y): - return 1e-7 * np.exp(-(x * x + y * y) / (2 * 0.3 ** 2)) + return 1e-7 * np.exp(-(x * x + y * y) / (2 * 0.3**2)) # In python, functions are first class objects. diff --git a/src/aspire/abinitio/commonline_base.py b/src/aspire/abinitio/commonline_base.py index b1fcdf43b6..f46302c577 100644 --- a/src/aspire/abinitio/commonline_base.py +++ b/src/aspire/abinitio/commonline_base.py @@ -416,10 +416,10 @@ def _estimate_num_shift_equations(self, n_img, equations_factor=1, max_memory=40 memory_total = equations_factor * ( n_equations_total * 2 * n_img * self.dtype.itemsize ) - if memory_total < (max_memory * 10 ** 6): + if memory_total < (max_memory * 10**6): n_equations = int(np.ceil(equations_factor * n_equations_total)) else: - subsampling_factor = (max_memory * 10 ** 6) / memory_total + subsampling_factor = (max_memory * 10**6) / memory_total subsampling_factor = min(1.0, subsampling_factor) n_equations = int(np.ceil(n_equations_total * subsampling_factor)) diff --git a/src/aspire/abinitio/commonline_sync.py b/src/aspire/abinitio/commonline_sync.py index 3ca390d53b..d9d585fbfe 100644 --- a/src/aspire/abinitio/commonline_sync.py +++ b/src/aspire/abinitio/commonline_sync.py @@ -335,7 +335,7 @@ def _vote_ij(self, clmatrix, n_theta, i, j, k_list): angles_hist = np.sum( np.exp( (2 * np.multiply.outer(angles, angles_grid) - squared_values) - / (2 * sigma ** 2) + / (2 * sigma**2) ), 0, ) diff --git a/src/aspire/apple/apple.py b/src/aspire/apple/apple.py index ee48e060a7..398a4f8cec 100644 --- a/src/aspire/apple/apple.py +++ b/src/aspire/apple/apple.py @@ -86,7 +86,7 @@ def __init__( self.query_image_size = query_image_size # q_box is the query box (or "container") size - self.q_box = (4000 ** 2) / (self.query_image_size ** 2) * 4 + self.q_box = (4000**2) / (self.query_image_size**2) * 4 # tau1 and tau2 correspond to particle and noise windows respectively self.tau1 = tau1 or int(self.q_box * 0.03) self.tau2 = tau2 or int(self.q_box * 0.3) diff --git a/src/aspire/apple/helper.py b/src/aspire/apple/helper.py index d488f07adb..434b39d16d 100644 --- a/src/aspire/apple/helper.py +++ b/src/aspire/apple/helper.py @@ -18,7 +18,7 @@ def gaussian_filter(cls, size_filter, std): -(size_filter - 1) // 2 : (size_filter - 1) // 2 + 1, ] - response = xp.exp(-xp.square(x) - xp.square(y) / (2 * (std ** 2))) / ( + response = xp.exp(-xp.square(x) - xp.square(y) / (2 * (std**2))) / ( xp.sqrt(2 * xp.pi) * std ) response[response < xp.finfo("float").eps] = 0 @@ -44,7 +44,7 @@ def extract_windows(cls, img, block_size): # TODO: It might be nicer to apply any trimming to all micrograph sides img = xp.asarray(img[:xbnd, :ybnd]) - dim3_size = np.sqrt(np.prod(img.shape) // (block_size ** 2)).astype(int) + dim3_size = np.sqrt(np.prod(img.shape) // (block_size**2)).astype(int) img = xp.reshape(img, (block_size, dim3_size, block_size, dim3_size), "F") @@ -73,7 +73,7 @@ def extract_query(cls, img, block_size): img[: -(img.shape[0] % block_size), : -(img.shape[1] % block_size)] ) - dim3_size = np.sqrt(np.prod(blocks.shape) // (block_size ** 2)).astype(int) + dim3_size = np.sqrt(np.prod(blocks.shape) // (block_size**2)).astype(int) blocks = xp.reshape(blocks, (block_size, dim3_size, block_size, dim3_size), "F") blocks = xp.transpose(blocks, (0, 2, 1, 3)) diff --git a/src/aspire/apple/picking.py b/src/aspire/apple/picking.py index 2a91808c45..251f5f60cf 100644 --- a/src/aspire/apple/picking.py +++ b/src/aspire/apple/picking.py @@ -391,7 +391,7 @@ def extract_particles(self, segmentation): labeled_segments, _ = ndimage.label(segmentation, np.ones((3, 3))) values, repeats = np.unique(labeled_segments, return_counts=True) - values_to_remove = np.where(repeats > self.max_size ** 2) + values_to_remove = np.where(repeats > self.max_size**2) values = np.take(values, values_to_remove) values = np.reshape(values, (1, 1, np.prod(values.shape)), "F") diff --git a/src/aspire/basis/dirac.py b/src/aspire/basis/dirac.py index 27941d58e9..8c906c2e2c 100644 --- a/src/aspire/basis/dirac.py +++ b/src/aspire/basis/dirac.py @@ -33,7 +33,7 @@ def _build(self): """ logger.info("Expanding object in a Dirac basis.") self.count = np.sum(self._mask) - self._sz_prod = self.nres ** self.ndim + self._sz_prod = self.nres**self.ndim def evaluate(self, v): """ diff --git a/src/aspire/basis/ffb_3d.py b/src/aspire/basis/ffb_3d.py index 83b5b9ebd8..a67967288e 100644 --- a/src/aspire/basis/ffb_3d.py +++ b/src/aspire/basis/ffb_3d.py @@ -81,7 +81,7 @@ def _precomp(self): radial_ell[:, ik] = sph_bessel(ell, rmat[:, ik]) nrm = np.abs(sph_bessel(ell + 1, self.r0[0:k_max_ell, ell].T) / 4) radial_ell = radial_ell / nrm - radial_ell_wtd = r ** 2 * wt_r * radial_ell + radial_ell_wtd = r**2 * wt_r * radial_ell radial_wtd[:, 0:k_max_ell, ell] = radial_ell_wtd # evaluate basis function in the phi dimension diff --git a/src/aspire/basis/steerable.py b/src/aspire/basis/steerable.py index af546dd89f..85f0527289 100644 --- a/src/aspire/basis/steerable.py +++ b/src/aspire/basis/steerable.py @@ -75,7 +75,7 @@ def calculate_bispectrum( # Default array to fill_value, we can use a value # k should never achieve.. - fill_value = self.complex_count ** 2 + fill_value = self.complex_count**2 compressed_radial_map = ( np.ones(np.max(unique_radial_indices) + 1, dtype=int) * fill_value ) diff --git a/src/aspire/covariance/covar.py b/src/aspire/covariance/covar.py index f1f9dc2207..181b6840e6 100644 --- a/src/aspire/covariance/covar.py +++ b/src/aspire/covariance/covar.py @@ -61,8 +61,8 @@ def compute_kernel(self): weights[:, 0, :] = 0 # TODO: This is where this differs from MeanEstimator - pts_rot = np.moveaxis(pts_rot, -1, 0).reshape(-1, 3, L ** 2) - weights = weights.T.reshape((-1, L ** 2)) + pts_rot = np.moveaxis(pts_rot, -1, 0).reshape(-1, 3, L**2) + weights = weights.T.reshape((-1, L**2)) batch_n = weights.shape[0] factors = np.zeros((batch_n, _2L, _2L, _2L), dtype=self.dtype) @@ -71,7 +71,7 @@ def compute_kernel(self): factors[j] = anufft(weights[j], pts_rot[j], (_2L, _2L, _2L), real=True) factors = Volume(factors).to_vec() - kernel += vecmat_to_volmat(factors.T @ factors) / (n * L ** 8) + kernel += vecmat_to_volmat(factors.T @ factors) / (n * L**8) # Ensure symmetric kernel kernel[0, :, :, :, :, :] = 0 diff --git a/src/aspire/covariance/covar2d.py b/src/aspire/covariance/covar2d.py index 84270bcabc..95cb734150 100644 --- a/src/aspire/covariance/covar2d.py +++ b/src/aspire/covariance/covar2d.py @@ -64,7 +64,7 @@ def shrink_covar(covar, noise_var, gamma, shrinker="frobenius_norm"): - noise_var ) c = np.divide( - (1 - np.divide(noise_var ** 2 * gamma, lambdas ** 2)), + (1 - np.divide(noise_var**2 * gamma, lambdas**2)), (1 + np.divide(noise_var * gamma, lambdas)), ) lambdas = lambdas * c @@ -330,7 +330,7 @@ def precond_fun(S, x): ensure(np.size(x) == p * p, "The sizes of S and x are not consistent.") x = m_reshape(x, (p, p)) y = S @ x @ S - y = m_reshape(y, (p ** 2,)) + y = m_reshape(y, (p**2,)) return y def apply(A, x): @@ -339,7 +339,7 @@ def apply(A, x): y = np.zeros_like(x) for k in range(0, len(A)): y = y + A[k] @ x @ A[k].T - y = m_reshape(y, (p ** 2,)) + y = m_reshape(y, (p**2,)) return y for ell in range(0, len(b)): @@ -347,7 +347,7 @@ def apply(A, x): for k in range(0, len(A)): A_ell.append(A[k][ell]) p = np.size(A_ell[0], 0) - b_ell = m_reshape(b[ell], (p ** 2,)) + b_ell = m_reshape(b[ell], (p**2,)) S = inv(M[ell]) cg_opt["preconditioner"] = lambda x: precond_fun(S, x) covar_coeff_ell, _, _ = conj_grad(lambda x: apply(A_ell, x), b_ell, cg_opt) @@ -635,7 +635,7 @@ def precond_fun(S, x): ensure(np.size(x) == p * p, "The sizes of S and x are not consistent.") x = m_reshape(x, (p, p)) y = S @ x @ S - y = m_reshape(y, (p ** 2,)) + y = m_reshape(y, (p**2,)) return y def apply(A, x): @@ -644,7 +644,7 @@ def apply(A, x): y = np.zeros_like(x) for k in range(0, len(A)): y = y + A[k] @ x @ A[k].T - y = m_reshape(y, (p ** 2,)) + y = m_reshape(y, (p**2,)) return y cg_opt = covar_est_opt @@ -655,7 +655,7 @@ def apply(A, x): for k in range(0, len(A_covar)): A_ell.append(A_covar[k][ell]) p = np.size(A_ell[0], 0) - b_ell = m_reshape(b_covar[ell], (p ** 2,)) + b_ell = m_reshape(b_covar[ell], (p**2,)) S = inv(M[ell]) cg_opt["preconditioner"] = lambda x: precond_fun(S, x) covar_coeff_ell, _, _ = conj_grad(lambda x: apply(A_ell, x), b_ell, cg_opt) diff --git a/src/aspire/ctf/ctf_estimator.py b/src/aspire/ctf/ctf_estimator.py index 19c6ef38b0..46e2ff9682 100644 --- a/src/aspire/ctf/ctf_estimator.py +++ b/src/aspire/ctf/ctf_estimator.py @@ -122,12 +122,12 @@ def generate_ctf(self): ) defocus_factor = np.pi * self.lmbd * self.r_ctf * defocus / 2 amplitude_contrast_term = self.amplitude_contrast / np.sqrt( - 1 - self.amplitude_contrast ** 2 + 1 - self.amplitude_contrast**2 ) chi = ( defocus_factor - - np.pi * self.lmbd ** 3 * self.cs * 1e6 * self.r_ctf ** 2 / 2 + - np.pi * self.lmbd**3 * self.cs * 1e6 * self.r_ctf**2 / 2 + amplitude_contrast_term ) h = -np.sin(chi) @@ -178,7 +178,7 @@ def normalize_blocks(self, blocks): # Create a sum and reshape so it may be broadcast with `block`. blocks_sum = np.sum(blocks, axis=(-1, -2))[:, np.newaxis, np.newaxis] - blocks -= blocks_sum / (block_size ** 2) + blocks -= blocks_sum / (block_size**2) return blocks @@ -413,14 +413,14 @@ def opt1d( signal = np.sqrt(signal) signal = signal[: 3 * signal.shape[0] // 4] - r_ctf_sq = r_ctf ** 2 + r_ctf_sq = r_ctf**2 c = np.zeros((max_defocus - min_defocus, signal.shape[1]), dtype=self.dtype) for f in range(min_defocus, max_defocus): ctf_im = np.abs( np.sin( np.pi * lmbd * f * r_ctf_sq - - 0.5 * np.pi * (lmbd ** 3) * cs * 1e6 * r_ctf_sq ** 2 + - 0.5 * np.pi * (lmbd**3) * cs * 1e6 * r_ctf_sq**2 + w ) ) @@ -435,8 +435,8 @@ def opt1d( ctf_im[: m + 1, m] = np.zeros((m + 1)) signal[: m + 1, m] = np.zeros((m + 1)) - Sx = np.sqrt(np.sum(ctf_im ** 2, axis=0)) - Sy = np.sqrt(np.sum(signal ** 2, axis=0)) + Sx = np.sqrt(np.sum(ctf_im**2, axis=0)) + Sy = np.sqrt(np.sum(signal**2, axis=0)) c[f - min_defocus, :] = np.sum(ctf_im * signal, axis=0) / (Sx * Sy) avg_defocus, low_freq_cutoff = np.unravel_index(np.argmax(c), c.shape)[:2] @@ -507,13 +507,13 @@ def pca(self, signal, pixel_size, g_min, g_max): max_limit = r_ctf[center, (center + np.ceil(rad_sq_max)).astype(int)] signal = np.where(r_ctf > max_limit, 0, signal) - moment_02 = Y ** 2 * signal + moment_02 = Y**2 * signal moment_02 = np.sum(moment_02, axis=(0, 1)) moment_11 = Y * X * signal moment_11 = np.sum(moment_11, axis=(0, 1)) - moment_20 = X ** 2 * signal + moment_20 = X**2 * signal moment_20 = np.sum(moment_20, axis=(0, 1)) moment_mat = np.zeros((2, 2)) @@ -569,8 +569,8 @@ def gd( y = (df1 - df2) * np.cos(2 * angle_ast) z = (df1 - df2) * np.sin(2 * angle_ast) - a = np.pi * lmbd * r ** 2 / 2 - b = np.pi * lmbd ** 3 * cs * 1e6 * r ** 4 / 2 - np.full( + a = np.pi * lmbd * r**2 / 2 + b = np.pi * lmbd**3 * cs * 1e6 * r**4 / 2 - np.full( shape=r.shape, fill_value=amplitude_contrast, dtype=self.dtype ) @@ -591,7 +591,7 @@ def gd( r = r[mask] theta = theta[mask] - sum_A = np.sum(signal ** 2) + sum_A = np.sum(signal**2) dx = 1 dy = 1 @@ -611,7 +611,7 @@ def gd( sine_z_term = a * np.sin(2 * theta) c1 = np.sum(np.abs(outer_sine) * signal) - c2 = np.sqrt(sum_A * np.sum(outer_sine ** 2)) + c2 = np.sqrt(sum_A * np.sum(outer_sine**2)) # gradients of numerator dx_c1 = np.sum(np.sign(outer_sine) * outer_cosine * a * signal) @@ -623,7 +623,7 @@ def gd( np.sign(outer_sine) * outer_cosine * a * np.sin(2 * theta) * signal ) - derivative_sqrt = 1 / (2 * np.sqrt(sum_A * np.sum(outer_sine ** 2))) + derivative_sqrt = 1 / (2 * np.sqrt(sum_A * np.sum(outer_sine**2))) derivative_sine2 = 2 * outer_sine * outer_cosine @@ -633,9 +633,9 @@ def gd( dz_c2 = derivative_sqrt * sum_A * np.sum(derivative_sine2 * sine_z_term) # gradients - dx = (dx_c1 * c2 - dx_c2 * c1) / c2 ** 2 - dy = (dy_c1 * c2 - dy_c2 * c1) / c2 ** 2 - dz = (dz_c1 * c2 - dz_c2 * c1) / c2 ** 2 + dx = (dx_c1 * c2 - dx_c2 * c1) / c2**2 + dy = (dy_c1 * c2 - dy_c2 * c1) / c2**2 + dz = (dz_c1 * c2 - dz_c2 * c1) / c2**2 # update x = x + alpha1 * dx @@ -663,7 +663,7 @@ def gd( sine_z_term = a * np.sin(2 * theta) c1 = np.sum(np.abs(outer_sine) * signal) - c2 = np.sqrt(sum_A * np.sum(outer_sine ** 2)) + c2 = np.sqrt(sum_A * np.sum(outer_sine**2)) p = c1 / c2 @@ -724,7 +724,7 @@ def estimate_ctf( amp = amplitude_contrast amplitude_contrast = np.arctan( - amplitude_contrast / np.sqrt(1 - amplitude_contrast ** 2) + amplitude_contrast / np.sqrt(1 - amplitude_contrast**2) ) lmbd = voltage_to_wavelength(voltage) / 10 # (Angstrom) @@ -847,7 +847,7 @@ def estimate_ctf( cc_array[ml, 0] - cc_array[ml, 1] ) * np.cos(2 * theta - 2 * cc_array[ml, 2] * np.ones(theta.shape, theta.dtype)) ctf_im = -np.sin( - np.pi * lmbd * r_ctf ** 2 / 2 * (df - lmbd ** 2 * r_ctf ** 2 * cs * 1e6) + np.pi * lmbd * r_ctf**2 / 2 * (df - lmbd**2 * r_ctf**2 * cs * 1e6) + amplitude_contrast ) ctf_signal = np.zeros(ctf_im.shape, ctf_im.dtype) diff --git a/src/aspire/image/image.py b/src/aspire/image/image.py index b058fbb244..6f720d5849 100644 --- a/src/aspire/image/image.py +++ b/src/aspire/image/image.py @@ -105,9 +105,9 @@ def normalize_bg(imgs, bg_radius=1.0, do_ramp=True): imgs_masked = imgs * mask denominator = np.sum(mask) first_moment = np.sum(imgs_masked, axis=(1, 2)) / denominator - second_moment = np.sum(imgs_masked ** 2, axis=(1, 2)) / denominator + second_moment = np.sum(imgs_masked**2, axis=(1, 2)) / denominator mean = first_moment.reshape(-1, 1, 1) - variance = second_moment.reshape(-1, 1, 1) - mean ** 2 + variance = second_moment.reshape(-1, 1, 1) - mean**2 std = np.sqrt(variance) return (imgs - mean) / std @@ -339,7 +339,7 @@ def backproject(self, rot_matrices): pts_rot = aspire.volume.rotated_grids(L, rot_matrices) pts_rot = pts_rot.reshape((3, -1)) - im_f = xp.asnumpy(fft.centered_fft2(xp.asarray(self.data))) / (L ** 2) + im_f = xp.asnumpy(fft.centered_fft2(xp.asarray(self.data))) / (L**2) if L % 2 == 0: im_f[:, 0, :] = 0 im_f[:, :, 0] = 0 diff --git a/src/aspire/image/preprocess.py b/src/aspire/image/preprocess.py index bccf7803cc..5cd71b8f8d 100644 --- a/src/aspire/image/preprocess.py +++ b/src/aspire/image/preprocess.py @@ -162,7 +162,7 @@ def downsample(insamples, szout, mask=None): outsamples_shifted = fft.ifft2(fft.ifftshift(xp.asarray(insamples_fft))) outsamples[idata] = np.real( - xp.asnumpy(outsamples_shifted) * (L_out ** 2 / L_in ** 2) + xp.asnumpy(outsamples_shifted) * (L_out**2 / L_in**2) ) elif insamples.ndim == 4: @@ -177,7 +177,7 @@ def downsample(insamples, szout, mask=None): fft.ifftshift(xp.asarray(insamples_fft)), axes=(0, 1, 2) ) outsamples[idata] = np.real( - xp.asnumpy(outsamples_shifted) * (L_out ** 3 / L_in ** 3) + xp.asnumpy(outsamples_shifted) * (L_out**3 / L_in**3) ) else: @@ -204,7 +204,7 @@ def fuzzy_mask(L, r0, risetime, origin=None): grids = [np.arange(1 - org, ell - org + 1) for ell, org in zip(L, origin)] XYZ = np.meshgrid(*grids, indexing="ij") - XYZ_sq = [X ** 2 for X in XYZ] + XYZ_sq = [X**2 for X in XYZ] R = np.sqrt(np.sum(XYZ_sq, axis=0)) k = 1.782 / risetime m = 0.5 * (1 - erf(k * (R - r0))) diff --git a/src/aspire/noise/noise.py b/src/aspire/noise/noise.py index 7a53c102d5..b99a43f665 100644 --- a/src/aspire/noise/noise.py +++ b/src/aspire/noise/noise.py @@ -83,8 +83,8 @@ def _estimate_noise_variance(self): _denominator = self.n * np.sum(mask) first_moment += np.sum(images_masked) / _denominator - second_moment += np.sum(np.abs(images_masked ** 2)) / _denominator - return second_moment - first_moment ** 2 + second_moment += np.sum(np.abs(images_masked**2)) / _denominator + return second_moment - first_moment**2 class AnisotropicNoiseEstimator(NoiseEstimator): @@ -129,9 +129,9 @@ def estimate_noise_psd(self): _denominator = self.n * np.sum(mask) mean_est += np.sum(images_masked) / _denominator im_masked_f = xp.asnumpy(fft.centered_fft2(xp.asarray(images_masked))) - noise_psd_est += np.sum(np.abs(im_masked_f ** 2), axis=0) / _denominator + noise_psd_est += np.sum(np.abs(im_masked_f**2), axis=0) / _denominator mid = self.L // 2 - noise_psd_est[mid, mid] -= mean_est ** 2 + noise_psd_est[mid, mid] -= mean_est**2 return noise_psd_est diff --git a/src/aspire/operators/filters.py b/src/aspire/operators/filters.py index 90f6ac58a9..f9b0fc7e8f 100644 --- a/src/aspire/operators/filters.py +++ b/src/aspire/operators/filters.py @@ -18,7 +18,7 @@ def voltage_to_wavelength(voltage): :param voltage: float, The electron voltage in kV. :return: float, The electron wavelength in nm. """ - return 12.2643247 / math.sqrt(voltage * 1e3 + 0.978466 * voltage ** 2) + return 12.2643247 / math.sqrt(voltage * 1e3 + 0.978466 * voltage**2) def wavelength_to_voltage(wavelength): @@ -28,7 +28,7 @@ def wavelength_to_voltage(wavelength): :return: float, The electron voltage in kV. """ return ( - -1e3 + math.sqrt(1e6 + 4 * 12.2643247 ** 2 * 0.978466 / wavelength ** 2) + -1e3 + math.sqrt(1e6 + 4 * 12.2643247**2 * 0.978466 / wavelength**2) ) / (2 * 0.978466) @@ -88,7 +88,7 @@ def evaluate(self, omega): if self.radial: if omega.ndim > 1: - omega = np.sqrt(np.sum(omega ** 2, axis=0)) + omega = np.sqrt(np.sum(omega**2, axis=0)) omega, idx = np.unique(omega, return_inverse=True) omega = np.vstack((omega, np.zeros_like(omega))) @@ -424,12 +424,12 @@ def _evaluate(self, omega): defocus[ind_nz] = self.defocus_mean + self.defocus_diff * np.cos(2 * angles_nz) c2 = -np.pi * self.wavelength * defocus - c4 = 0.5 * np.pi * (self.Cs * 1e7) * self.wavelength ** 3 + c4 = 0.5 * np.pi * (self.Cs * 1e7) * self.wavelength**3 - r2 = om_x ** 2 + om_y ** 2 - r4 = r2 ** 2 + r2 = om_x**2 + om_y**2 + r4 = r2**2 gamma = c2 * r2 + c4 * r4 - h = np.sqrt(1 - self.alpha ** 2) * np.sin(gamma) - self.alpha * np.cos(gamma) + h = np.sqrt(1 - self.alpha**2) * np.sin(gamma) - self.alpha * np.cos(gamma) if self.B: h *= np.exp(-self.B * r2) diff --git a/src/aspire/optimization/conj_grad.py b/src/aspire/optimization/conj_grad.py index 9dc06bc901..65ee79a471 100644 --- a/src/aspire/optimization/conj_grad.py +++ b/src/aspire/optimization/conj_grad.py @@ -164,7 +164,7 @@ def identity(input_x): obj = np.real( np.sum(x.conj() * a_x, -1) - 2 * np.real(np.sum(np.conj(b * x), -1)) ) - res = np.sqrt(np.sum(r ** 2, -1)) + res = np.sqrt(np.sum(r**2, -1)) info["iter"].append(i) info["res"].append(res) info["obj"].append(obj) diff --git a/src/aspire/reconstruction/kernel.py b/src/aspire/reconstruction/kernel.py index 6dd89eac8f..9ecd02355b 100644 --- a/src/aspire/reconstruction/kernel.py +++ b/src/aspire/reconstruction/kernel.py @@ -159,8 +159,8 @@ def toeplitz(self, L=None): if L is None: L = int(self.M / 2) - A = np.eye(L ** 3, dtype=self.dtype) - for i in range(L ** 3): + A = np.eye(L**3, dtype=self.dtype) + for i in range(L**3): A[:, i] = np.real(vol_to_vec(self.convolve_volume(vec_to_vol(A[:, i])))) A = vecmat_to_volmat(A) diff --git a/src/aspire/reconstruction/mean.py b/src/aspire/reconstruction/mean.py index 15b501aefd..a11b927c2f 100644 --- a/src/aspire/reconstruction/mean.py +++ b/src/aspire/reconstruction/mean.py @@ -33,7 +33,7 @@ def compute_kernel(self): kernel += ( 1 - / (self.n * self.L ** 4) + / (self.n * self.L**4) * anufft(weights, pts_rot[::-1], (_2L, _2L, _2L), real=True) ) diff --git a/src/aspire/source/simulation.py b/src/aspire/source/simulation.py index 1c482e6882..24f3db7a4d 100644 --- a/src/aspire/source/simulation.py +++ b/src/aspire/source/simulation.py @@ -260,7 +260,7 @@ def eval_eigs(self, eigs_est, lambdas_est): norm_est = anorm(lambdas_est) inner = ainner(B @ lambdas_true, lambdas_est @ B) - err = np.sqrt(norm_true ** 2 + norm_est ** 2 - 2 * inner) + err = np.sqrt(norm_true**2 + norm_est**2 - 2 * inner) rel_err = err / norm_true corr = inner / (norm_true * norm_est) @@ -309,7 +309,7 @@ def eval_coords(self, mean_vol, eig_vols, coords_est): mean_vol_norm2 = anorm(mean_vol) ** 2 norm_true = np.sqrt( - coords_true ** 2 + coords_true**2 + mean_vol_norm2 + 2 * res_inners + 2 * mean_eigs_inners * coords_true @@ -324,7 +324,7 @@ def eval_coords(self, mean_vol, eig_vols, coords_est): + res_inners ) norm_est = np.sqrt( - coords_est ** 2 + mean_vol_norm2 + 2 * mean_eigs_inners * coords_est + coords_est**2 + mean_vol_norm2 + 2 * mean_eigs_inners * coords_est ) corr = inner / (norm_true * norm_est) diff --git a/src/aspire/storage/micrograph.py b/src/aspire/storage/micrograph.py index e3c1d8e16a..aca4bf0fdb 100644 --- a/src/aspire/storage/micrograph.py +++ b/src/aspire/storage/micrograph.py @@ -115,7 +115,7 @@ def gaussian_filter(cls, size_filter, std): -(size_filter - 1) // 2 : (size_filter - 1) // 2 + 1, ] - response = xp.exp(-xp.square(x) - xp.square(y) / (2 * (std ** 2))) / ( + response = xp.exp(-xp.square(x) - xp.square(y) / (2 * (std**2))) / ( xp.sqrt(2 * xp.pi) * std ) response[response < xp.finfo("float").eps] = 0 diff --git a/src/aspire/utils/matrix.py b/src/aspire/utils/matrix.py index 2752967769..5b3fb3ebe8 100644 --- a/src/aspire/utils/matrix.py +++ b/src/aspire/utils/matrix.py @@ -73,7 +73,7 @@ def vec_to_im(X): """ shape = X.shape N = round(shape[0] ** (1 / 2)) - ensure(N ** 2 == shape[0], "First dimension of X must be square") + ensure(N**2 == shape[0], "First dimension of X must be square") return m_reshape(X, (N, N) + (shape[1:])) @@ -86,7 +86,7 @@ def vec_to_vol(X): """ shape = X.shape N = round(shape[0] ** (1 / 3)) - ensure(N ** 3 == shape[0], "First dimension of X must be cubic") + ensure(N**3 == shape[0], "First dimension of X must be cubic") return m_reshape(X, (N, N, N) + (shape[1:])) @@ -104,8 +104,8 @@ def vecmat_to_volmat(X): L1 = round(shape[0] ** (1 / 3)) L2 = round(shape[1] ** (1 / 3)) - ensure(L1 ** 3 == shape[0], "First dimension of X must be cubic") - ensure(L2 ** 3 == shape[1], "Second dimension of X must be cubic") + ensure(L1**3 == shape[0], "First dimension of X must be cubic") + ensure(L2**3 == shape[1], "Second dimension of X must be cubic") return m_reshape(X, (L1, L1, L1, L2, L2, L2) + (shape[2:])) @@ -125,7 +125,7 @@ def volmat_to_vecmat(X): l1 = shape[0] l2 = shape[3] - return m_reshape(X, (l1 ** 3, l2 ** 3) + (shape[6:])) + return m_reshape(X, (l1**3, l2**3) + (shape[6:])) def mdim_mat_fun_conj(X, d1, d2, f): @@ -181,7 +181,7 @@ def symmat_to_vec_iso(mat): mat, sz_roll = unroll_dim(mat, 3) N = mat.shape[0] mat = mat_to_vec(mat) - mat[np.arange(0, N ** 2, N + 1)] *= SQRT2_R + mat[np.arange(0, N**2, N + 1)] *= SQRT2_R mat *= SQRT2 mat = vec_to_mat(mat) mat = roll_dim(mat, sz_roll) @@ -202,7 +202,7 @@ def vec_to_symmat_iso(vec): mat, sz_roll = unroll_dim(mat, 3) N = mat.shape[0] mat = mat_to_vec(mat) - mat[np.arange(0, N ** 2, N + 1)] *= SQRT2 + mat[np.arange(0, N**2, N + 1)] *= SQRT2 mat *= SQRT2_R mat = vec_to_mat(mat) mat = roll_dim(mat, sz_roll) @@ -278,7 +278,7 @@ def mat_to_vec(mat, is_symmat=False): sz = mat.shape N = sz[0] ensure(sz[1] == N, "Matrix must be square") - return m_reshape(mat, (N ** 2,) + sz[2:]) + return m_reshape(mat, (N**2,) + sz[2:]) else: return symmat_to_vec(mat) @@ -294,7 +294,7 @@ def vec_to_mat(vec, is_symmat=False): if not is_symmat: sz = vec.shape N = int(round(np.sqrt(sz[0]))) - ensure(sz[0] == N ** 2, "Vector must represent square matrix.") + ensure(sz[0] == N**2, "Vector must represent square matrix.") return m_reshape(vec, (N, N) + sz[1:]) else: return vec_to_symmat(vec) diff --git a/src/aspire/utils/misc.py b/src/aspire/utils/misc.py index 03caa89bf2..82581e82d3 100644 --- a/src/aspire/utils/misc.py +++ b/src/aspire/utils/misc.py @@ -19,7 +19,7 @@ def abs2(x): Compute complex modulus squared. """ - return x.real ** 2 + x.imag ** 2 + return x.real**2 + x.imag**2 def ensure(cond, error_message=None): @@ -132,7 +132,7 @@ def gaussian_1d(size, mu=0, sigma=1, peak=1, dtype=np.float64): # Construct centered mesh g = grid_1d(size, normalized=False, dtype=dtype) - p = (g["x"] - mu) ** 2 / (2 * sigma ** 2) + p = (g["x"] - mu) ** 2 / (2 * sigma**2) return (peak * np.exp(-p)).astype(dtype, copy=False) @@ -156,8 +156,8 @@ def gaussian_2d(size, x0=0, y0=0, sigma_x=1, sigma_y=1, peak=1, dtype=np.float64 # Construct centered mesh g = grid_2d(size, shifted=False, normalized=False, indexing="xy", dtype=dtype) - p = (g["x"] - x0) ** 2 / (2 * sigma_x ** 2) + (g["y"] - y0) ** 2 / ( - 2 * sigma_y ** 2 + p = (g["x"] - x0) ** 2 / (2 * sigma_x**2) + (g["y"] - y0) ** 2 / ( + 2 * sigma_y**2 ) return (peak * np.exp(-p)).astype(dtype, copy=False) diff --git a/src/aspire/volume/__init__.py b/src/aspire/volume/__init__.py index fc9cc8e032..c25c570239 100644 --- a/src/aspire/volume/__init__.py +++ b/src/aspire/volume/__init__.py @@ -34,7 +34,7 @@ def qr_vols_forward(sim, s, n, vols, k): ims = np.swapaxes(ims, 1, 3) ims = np.swapaxes(ims, 0, 2) - Q_vecs = np.zeros((sim.L ** 2, k, n), dtype=vols.dtype) + Q_vecs = np.zeros((sim.L**2, k, n), dtype=vols.dtype) Rs = np.zeros((k, k, n), dtype=vols.dtype) im_vecs = mat_to_vec(ims) @@ -163,7 +163,7 @@ def project(self, vol_idx, rot_matrices): pts_rot = rotated_grids(self.resolution, rot_matrices) # TODO: rotated_grids might as well give us correctly shaped array in the first place - pts_rot = pts_rot.reshape((3, n * self.resolution ** 2)) + pts_rot = pts_rot.reshape((3, n * self.resolution**2)) im_f = nufft(data, pts_rot) / self.resolution @@ -179,7 +179,7 @@ def project(self, vol_idx, rot_matrices): def to_vec(self): """Returns an N x resolution ** 3 array.""" - return self._data.reshape((self.n_vols, self.resolution ** 3)) + return self._data.reshape((self.n_vols, self.resolution**3)) @staticmethod def from_vec(vec): @@ -196,7 +196,7 @@ def from_vec(vec): n_vols = vec.shape[0] resolution = round(vec.shape[1] ** (1 / 3)) - assert resolution ** 3 == vec.shape[1] + assert resolution**3 == vec.shape[1] data = vec.reshape((n_vols, resolution, resolution, resolution)) @@ -298,9 +298,9 @@ def rotate(self, rot_matrices, zero_nyquist=True): # If K = n_vols, we apply the ith rotation to ith volume. else: rot_matrices = rot_matrices.reshape((K, 1, 3, 3)) - pts_rot = np.zeros((K, 3, self.resolution ** 3)) + pts_rot = np.zeros((K, 3, self.resolution**3)) vol_f = np.empty( - (self.n_vols, self.resolution ** 3), dtype=complex_type(self.dtype) + (self.n_vols, self.resolution**3), dtype=complex_type(self.dtype) ) for i in range(K): pts_rot[i] = rotated_grids_3d(self.resolution, rot_matrices[i]) @@ -534,7 +534,7 @@ def _gen_gaussians(K, alpha, dtype=np.float64): for k in range(K): V = randn(3, 3).astype(dtype) / np.sqrt(3) Q[k, :, :] = qr(V)[0] - D[k, :, :] = alpha ** 2 / 16 * np.diag(np.sum(abs(V) ** 2, axis=0)) + D[k, :, :] = alpha**2 / 16 * np.diag(np.sum(abs(V) ** 2, axis=0)) mu[k, :] = 0.5 * randn(3) / np.sqrt(3) return Q, D, mu @@ -553,7 +553,7 @@ def rotated_grids(L, rot_matrices): """ grid2d = grid_2d(L, indexing="xy", dtype=rot_matrices.dtype) - num_pts = L ** 2 + num_pts = L**2 num_rots = rot_matrices.shape[0] pts = np.pi * np.vstack( [ @@ -582,7 +582,7 @@ def rotated_grids_3d(L, rot_matrices): """ grid3d = grid_3d(L, indexing="xyz", dtype=rot_matrices.dtype) - num_pts = L ** 3 + num_pts = L**3 num_rots = rot_matrices.shape[0] pts = np.pi * np.vstack( [ diff --git a/tests/test_BlkDiagMatrix.py b/tests/test_BlkDiagMatrix.py index 2e979cef0b..b7307981ff 100644 --- a/tests/test_BlkDiagMatrix.py +++ b/tests/test_BlkDiagMatrix.py @@ -310,9 +310,9 @@ def testBlkDiagMatrixAbs(self): self.allallfunc(blk_c, result) def testBlkDiagMatrixPow(self): - result = [blk ** 2 for blk in self.blk_a] + result = [blk**2 for blk in self.blk_a] - blk_c = self.blk_a ** 2.0 + blk_c = self.blk_a**2.0 self.allallfunc(blk_c, result) # In place power diff --git a/tests/test_adaptive_support.py b/tests/test_adaptive_support.py index ec81f6687e..f1beaa8208 100644 --- a/tests/test_adaptive_support.py +++ b/tests/test_adaptive_support.py @@ -26,8 +26,8 @@ def setUp(self): # three standard deviations are the following. self.references = { 1: 1 - np.exp(-1 / 2), - 2: 1 - np.exp(-(2 ** 2) / 2), - 3: 1 - np.exp(-(3 ** 2) / 2), + 2: 1 - np.exp(-(2**2) / 2), + 3: 1 - np.exp(-(3**2) / 2), } def testAdaptiveSupportBadThreshold(self): diff --git a/tests/test_filters.py b/tests/test_filters.py index f5f101bcab..0a629f9b47 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -28,7 +28,7 @@ def tearDown(self): pass def testFunctionFilter(self): - filt = FunctionFilter(lambda x, y: np.exp(-(x ** 2 + y ** 2) / 2)) + filt = FunctionFilter(lambda x, y: np.exp(-(x**2 + y**2) / 2)) result = filt.evaluate(self.omega) self.assertEqual(result.shape, (256,)) self.assertTrue( @@ -63,7 +63,7 @@ def testScalarFilter(self): def testPowerFilter(self): filt = PowerFilter( - filter=FunctionFilter(lambda x, y: np.exp(-(x ** 2 + y ** 2) / 2)), + filter=FunctionFilter(lambda x, y: np.exp(-(x**2 + y**2) / 2)), power=0.5, ) result = filt.evaluate(self.omega) diff --git a/tests/test_preprocess_pipeline.py b/tests/test_preprocess_pipeline.py index 0a082547cc..c1a4c6d8fa 100644 --- a/tests/test_preprocess_pipeline.py +++ b/tests/test_preprocess_pipeline.py @@ -21,7 +21,7 @@ def setUp(self): self.L = 64 self.n = 128 self.dtype = np.float32 - self.noise_filter = FunctionFilter(lambda x, y: np.exp(-(x ** 2 + y ** 2) / 2)) + self.noise_filter = FunctionFilter(lambda x, y: np.exp(-(x**2 + y**2) / 2)) self.sim = Simulation( L=self.L, diff --git a/tests/test_utils.py b/tests/test_utils.py index 29f23caa9e..642e35bb21 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -62,8 +62,8 @@ def testGaussian2d(self): g_y = np.sum(g, axis=0) / np.sum(g) # Corresponding 1d gaussians - peak_x = 1 / np.sqrt(2 * np.pi * s_x ** 2) - peak_y = 1 / np.sqrt(2 * np.pi * s_y ** 2) + peak_x = 1 / np.sqrt(2 * np.pi * s_x**2) + peak_y = 1 / np.sqrt(2 * np.pi * s_y**2) g_1d_x = gaussian_1d(L, mu=mu_x, sigma=s_x, peak=peak_x) g_1d_y = gaussian_1d(L, mu=mu_y, sigma=s_y, peak=peak_y) diff --git a/tests/test_volume.py b/tests/test_volume.py index 2d19f69f14..c990c22fe7 100644 --- a/tests/test_volume.py +++ b/tests/test_volume.py @@ -23,7 +23,7 @@ class VolumeTestCase(TestCase): def setUp(self): self.dtype = np.float32 self.n = n = 3 - self.data_1 = np.arange(n * self.res ** 3, dtype=self.dtype).reshape( + self.data_1 = np.arange(n * self.res**3, dtype=self.dtype).reshape( n, self.res, self.res, self.res ) self.data_2 = 123 * self.data_1.copy() @@ -32,7 +32,7 @@ def setUp(self): self.random_data = np.random.randn(self.res, self.res, self.res).astype( self.dtype ) - self.vec = self.data_1.reshape(n, self.res ** 3) + self.vec = self.data_1.reshape(n, self.res**3) def tearDown(self): pass diff --git a/tests/test_wbemd.py b/tests/test_wbemd.py index 897180c160..9c256b8bd9 100644 --- a/tests/test_wbemd.py +++ b/tests/test_wbemd.py @@ -11,7 +11,7 @@ def _smoothed_disk_image(x, y, radius, width, height): (Y, X) = mgrid[:height, :width] - ratio = ((X - x) ** 2 + (Y - y) ** 2) / (radius ** 2) + ratio = ((X - x) ** 2 + (Y - y) ** 2) / (radius**2) return 2.0 - 2 / (1 + np.exp(-ratio)) # Scaled sigmoid funciton From f3e3d3883cdc39319770664bc751bf4f035d193c Mon Sep 17 00:00:00 2001 From: Chris Langfield Date: Tue, 15 Feb 2022 09:03:50 -0500 Subject: [PATCH 6/7] help messages and standardize preprocess and extract-particles --- src/aspire/commands/extract_particles.py | 6 ++-- src/aspire/commands/preprocess.py | 36 +++++++++++++----------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/aspire/commands/extract_particles.py b/src/aspire/commands/extract_particles.py index e419c052c1..cdb2496d25 100644 --- a/src/aspire/commands/extract_particles.py +++ b/src/aspire/commands/extract_particles.py @@ -47,7 +47,7 @@ @click.option( "--normalize_bg", is_flag=True, - help="Normalize images to the background noise", + help="Normalize the images to have mean zero and variance one in the corners", ) @click.option( "--whiten", @@ -57,7 +57,7 @@ @click.option( "--invert_contrast", is_flag=True, - help="Invert the contrast of the images so molecules are shown in white", + help="Invert the contrast of the images to ensure that clean particles have positive intensity", ) @click.option( "--batch_size", default=512, help="Batch size to load images from .mrc files" @@ -136,7 +136,7 @@ def extract_particles( ) # optional preprocessing steps - if downsample > 0: + if 0 < downsample < src.L: src.downsample(downsample) if normalize_bg: src.normalize_background() diff --git a/src/aspire/commands/preprocess.py b/src/aspire/commands/preprocess.py index b7ef198c1a..bd91ca9a8d 100644 --- a/src/aspire/commands/preprocess.py +++ b/src/aspire/commands/preprocess.py @@ -31,24 +31,28 @@ ) @click.option("--flip_phase", default=True, help="Perform phase flip or not") @click.option( - "--max_resolution", - default=16, + "--downsample", + default=0, type=int, - help="Resolution for downsampling images read from STAR file", + help="Downsample the data to this resolution prior to saving to starfile/.mrcs stack", ) @click.option( - "--normalize_background", + "--normalize_bg", default=True, - help="Whether to normalize images to background noise", + help="Normalize the images to have mean zero and variance one in the corners", +) +@click.option( + "--whiten", + default=True, + help="Estimate the noise variance of the images and whiten", ) -@click.option("--whiten_noise", default=True, help="Whiten background noise") @click.option( "--invert_contrast", default=True, - help="Invert the contrast of images so molecules are shown in white", + help="Invert the contrast of the images to ensure that clean particles have positive intensity", ) @click.option( - "--batch_size", default=512, help="Batch size to load images from MRC files." + "--batch_size", default=512, help="Batch size to load images from MRC files" ) @click.option( "--save_mode", @@ -67,9 +71,9 @@ def preprocess( pixel_size, max_rows, flip_phase, - max_resolution, - normalize_background, - whiten_noise, + downsample, + normalize_bg, + whiten, invert_contrast, batch_size, save_mode, @@ -88,15 +92,15 @@ def preprocess( logger.info("Perform phase flip to input images") source.phase_flip() - if max_resolution < source.L: - logger.info(f"Downsample resolution to {max_resolution} X {max_resolution}") - source.downsample(max_resolution) + if 0 < downsample < source.L: + logger.info(f"Downsample resolution to {downsample} X {downsample}") + source.downsample(downsample) - if normalize_background: + if normalize_bg: logger.info("Normalize images to noise background") source.normalize_background() - if whiten_noise: + if whiten: logger.info("Whiten noise of images") noise_estimator = WhiteNoiseEstimator(source) source.whiten(noise_estimator.filter) From 0edc006f229be479bce60614f26939a1647378ae Mon Sep 17 00:00:00 2001 From: Chris Langfield Date: Tue, 15 Feb 2022 09:46:27 -0500 Subject: [PATCH 7/7] downsample images not data --- src/aspire/commands/extract_particles.py | 2 +- src/aspire/commands/preprocess.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/aspire/commands/extract_particles.py b/src/aspire/commands/extract_particles.py index cdb2496d25..10e801121a 100644 --- a/src/aspire/commands/extract_particles.py +++ b/src/aspire/commands/extract_particles.py @@ -42,7 +42,7 @@ "--downsample", default=0, type=int, - help="Downsample the data to this resolution prior to saving to starfile/.mrcs stack", + help="Downsample the images to this resolution prior to saving to starfile/.mrcs stack", ) @click.option( "--normalize_bg", diff --git a/src/aspire/commands/preprocess.py b/src/aspire/commands/preprocess.py index bd91ca9a8d..e91208a6d7 100644 --- a/src/aspire/commands/preprocess.py +++ b/src/aspire/commands/preprocess.py @@ -34,7 +34,7 @@ "--downsample", default=0, type=int, - help="Downsample the data to this resolution prior to saving to starfile/.mrcs stack", + help="Downsample the images to this resolution prior to saving to starfile/.mrcs stack", ) @click.option( "--normalize_bg",