Skip to content

Commit 5ff59cc

Browse files
chris-langfieldgarrettwrong
authored andcommitted
Add preprocessing flags to aspire extract-particles (#551)
* added --downsample option to aspire extract-particles * added --downsample option to aspire extract-particles * added normalize_bg and invert_contrast flags * add --whiten option * new black version formatting * help messages and standardize preprocess and extract-particles * downsample images not data
1 parent 18e83d8 commit 5ff59cc

File tree

3 files changed

+70
-16
lines changed

3 files changed

+70
-16
lines changed

src/aspire/commands/extract_particles.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import click
66
from click import UsageError
77

8+
from aspire.noise import WhiteNoiseEstimator
89
from aspire.source.coordinates import BoxesCoordinateSource, CentersCoordinateSource
910

1011
logger = logging.getLogger(__name__)
@@ -37,6 +38,27 @@
3738
is_flag=True,
3839
help="Set this flag if coordinate files contain (X,Y) particle centers",
3940
)
41+
@click.option(
42+
"--downsample",
43+
default=0,
44+
type=int,
45+
help="Downsample the images to this resolution prior to saving to starfile/.mrcs stack",
46+
)
47+
@click.option(
48+
"--normalize_bg",
49+
is_flag=True,
50+
help="Normalize the images to have mean zero and variance one in the corners",
51+
)
52+
@click.option(
53+
"--whiten",
54+
is_flag=True,
55+
help="Estimate the noise variance of the images and whiten",
56+
)
57+
@click.option(
58+
"--invert_contrast",
59+
is_flag=True,
60+
help="Invert the contrast of the images to ensure that clean particles have positive intensity",
61+
)
4062
@click.option(
4163
"--batch_size", default=512, help="Batch size to load images from .mrc files"
4264
)
@@ -54,6 +76,10 @@ def extract_particles(
5476
starfile_out,
5577
particle_size,
5678
centers,
79+
downsample,
80+
normalize_bg,
81+
whiten,
82+
invert_contrast,
5783
batch_size,
5884
save_mode,
5985
overwrite,
@@ -109,6 +135,17 @@ def extract_particles(
109135
particle_size=particle_size,
110136
)
111137

138+
# optional preprocessing steps
139+
if 0 < downsample < src.L:
140+
src.downsample(downsample)
141+
if normalize_bg:
142+
src.normalize_background()
143+
if whiten:
144+
estimator = WhiteNoiseEstimator(src)
145+
src.whiten(estimator.filter)
146+
if invert_contrast:
147+
src.invert_contrast()
148+
112149
# saves to .mrcs and STAR file with column "_rlnImageName"
113150
src.save(
114151
starfile_out, batch_size=batch_size, save_mode=save_mode, overwrite=overwrite

src/aspire/commands/preprocess.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,28 @@
3131
)
3232
@click.option("--flip_phase", default=True, help="Perform phase flip or not")
3333
@click.option(
34-
"--max_resolution",
35-
default=16,
34+
"--downsample",
35+
default=0,
3636
type=int,
37-
help="Resolution for downsampling images read from STAR file",
37+
help="Downsample the images to this resolution prior to saving to starfile/.mrcs stack",
3838
)
3939
@click.option(
40-
"--normalize_background",
40+
"--normalize_bg",
4141
default=True,
42-
help="Whether to normalize images to background noise",
42+
help="Normalize the images to have mean zero and variance one in the corners",
43+
)
44+
@click.option(
45+
"--whiten",
46+
default=True,
47+
help="Estimate the noise variance of the images and whiten",
4348
)
44-
@click.option("--whiten_noise", default=True, help="Whiten background noise")
4549
@click.option(
4650
"--invert_contrast",
4751
default=True,
48-
help="Invert the contrast of images so molecules are shown in white",
52+
help="Invert the contrast of the images to ensure that clean particles have positive intensity",
4953
)
5054
@click.option(
51-
"--batch_size", default=512, help="Batch size to load images from MRC files."
55+
"--batch_size", default=512, help="Batch size to load images from MRC files"
5256
)
5357
@click.option(
5458
"--save_mode",
@@ -67,9 +71,9 @@ def preprocess(
6771
pixel_size,
6872
max_rows,
6973
flip_phase,
70-
max_resolution,
71-
normalize_background,
72-
whiten_noise,
74+
downsample,
75+
normalize_bg,
76+
whiten,
7377
invert_contrast,
7478
batch_size,
7579
save_mode,
@@ -88,15 +92,15 @@ def preprocess(
8892
logger.info("Perform phase flip to input images")
8993
source.phase_flip()
9094

91-
if max_resolution < source.L:
92-
logger.info(f"Downsample resolution to {max_resolution} X {max_resolution}")
93-
source.downsample(max_resolution)
95+
if 0 < downsample < source.L:
96+
logger.info(f"Downsample resolution to {downsample} X {downsample}")
97+
source.downsample(downsample)
9498

95-
if normalize_background:
99+
if normalize_bg:
96100
logger.info("Normalize images to noise background")
97101
source.normalize_background()
98102

99-
if whiten_noise:
103+
if whiten:
100104
logger.info("Whiten noise of images")
101105
noise_estimator = WhiteNoiseEstimator(source)
102106
source.whiten(noise_estimator.filter)

tests/test_coordinate_source.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,20 @@ def testCommand(self):
343343
"--particle_size=256",
344344
],
345345
)
346+
result_preprocess = runner.invoke(
347+
extract_particles,
348+
[
349+
f"--mrc_paths={self.data_folder}/*.mrc",
350+
f"--coord_paths={self.data_folder}/sample*.box",
351+
f"--starfile_out={self.data_folder}/saved_star_ds.star",
352+
"--downsample=33",
353+
"--normalize_bg",
354+
"--whiten",
355+
"--invert_contrast",
356+
],
357+
)
346358
# check that all commands completed successfully
347359
self.assertTrue(result_box.exit_code == 0)
348360
self.assertTrue(result_coord.exit_code == 0)
349361
self.assertTrue(result_star.exit_code == 0)
362+
self.assertTrue(result_preprocess.exit_code == 0)

0 commit comments

Comments
 (0)