Skip to content

Pixel Size Pass-through #1290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/aspire/denoising/class_avg.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def __init__(
n=self.averager.src.n,
dtype=self.averager.src.dtype,
symmetry_group=self.src.symmetry_group,
pixel_size=self.src.pixel_size,
)

# Any further operations should not mutate this instance.
Expand Down
8 changes: 7 additions & 1 deletion src/aspire/denoising/denoised_src.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ def __init__(self, src, denoiser):
:param denoiser: A `Denoiser` object for specifying a method for denoising
"""

super().__init__(src.L, src.n, dtype=src.dtype, metadata=src._metadata.copy())
super().__init__(
src.L,
src.n,
dtype=src.dtype,
pixel_size=src.pixel_size,
metadata=src._metadata.copy(),
)
# TODO, we can probably setup a reasonable default here.
self.denoiser = denoiser
if not isinstance(denoiser, Denoiser):
Expand Down
1 change: 1 addition & 0 deletions src/aspire/reconstruction/estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def estimate(self, b_coef=None, x0=None, tol=1e-5, regularizer=0):
b_coef = self.src_backward()
est_coef = self.conj_grad(b_coef, x0=x0, tol=tol, regularizer=regularizer)
est = Coef(self.basis, est_coef).evaluate()
est.pixel_size = self.src.pixel_size

return est

Expand Down
9 changes: 8 additions & 1 deletion src/aspire/source/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,21 @@ def __init__(
)
symmetry_group = symmetry_group or self.vols.symmetry_group

if pixel_size and (pixel_size != self.vols.pixel_size):
logger.warning(
f"Overriding volume pixel size, {self.vols.pixel_size}, with "
f"user provided pixel size of {pixel_size} angstrom."
)
pixel_size = pixel_size or self.vols.pixel_size

# Infer the details from volume when possible.
super().__init__(
L=self.vols.resolution,
n=n,
dtype=self.vols.dtype,
memory=memory,
symmetry_group=symmetry_group,
pixel_size=self.vols.pixel_size,
pixel_size=pixel_size,
)

# If a user provides both `L` and `vols`, resolution should match.
Expand Down
4 changes: 4 additions & 0 deletions tests/test_class_src.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def class_sim_fixture(dtype, img_size):
C=1,
angles=true_rots.angles,
symmetry_group="C4", # For testing symmetry_group pass-through.
pixel_size=1, # For testing pixel_size pass-through
)
# Prefetch all the images
src = src.cache()
Expand Down Expand Up @@ -209,6 +210,9 @@ class averages.
# Check symmetry_group pass-through.
assert test_src.symmetry_group == class_sim_fixture.symmetry_group

# Check pixel_size pass-through.
assert test_src.pixel_size == class_sim_fixture.pixel_size


# Test the _HeapItem helper class
def test_heap_helper():
Expand Down
7 changes: 6 additions & 1 deletion tests/test_covar2d_denoiser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
num_imgs = 1024
noise_var = 0.1848
noise_adder = WhiteNoiseAdder(var=noise_var)
pixel_size = 5
filters = [
RadialCTFFilter(5, 200, defocus=d, Cs=2.0, alpha=0.1)
RadialCTFFilter(pixel_size, 200, defocus=d, Cs=2.0, alpha=0.1)
for d in np.linspace(1.5e4, 2.5e4, 7)
]

Expand Down Expand Up @@ -63,6 +64,7 @@ def sim():
amplitudes=1.0,
dtype=dtype,
noise_adder=noise_adder,
pixel_size=pixel_size,
)
sim = sim.cache()
return sim
Expand Down Expand Up @@ -110,6 +112,9 @@ def test_batched_rotcov2d_MSE(sim, basis):
imgs_denoised, src.images[:], rtol=1e-05, atol=utest_tolerance(src.dtype)
)

# Test pixel_size pass-through.
assert sim.pixel_size == src.pixel_size


def test_source_mismatch(sim, basis):
"""
Expand Down
4 changes: 4 additions & 0 deletions tests/test_mean_estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def sim(L, dtype):
],
dtype=dtype,
seed=SEED,
pixel_size=1,
)

sim = sim.cache() # precompute images
Expand Down Expand Up @@ -104,6 +105,9 @@ def test_estimate(sim, estimator, mask):
est / np.linalg.norm(est), vol / np.linalg.norm(vol), atol=0.1
)

# Check pixel_size pass-through
np.testing.assert_array_equal(sim.pixel_size, estimate.pixel_size)


def test_adjoint(sim, basis, estimator):
"""
Expand Down
36 changes: 36 additions & 0 deletions tests/test_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,42 @@ def check_metadata(sim_src, relion_src):
)


def test_pixel_size(caplog):
"""
Check pixel size is instantiated properly and warnings occur if pixel
size is overridden.
"""
vol_px_sz = 10.0
L = 8
data = np.ones(L**3).reshape(L, L, L)
vol = Volume(data, pixel_size=vol_px_sz)

# Ensure vol pixel size
assert vol.pixel_size == vol_px_sz

# Generate Simulation and check pixel_size is inhereted from vol
sim = Simulation(vols=vol)
assert sim.pixel_size == vol_px_sz

# Generate Simulation with provided pixel_size and check
# that vol.pixel_size is overridden.
caplog.clear()
caplog.set_level(logging.WARN)

sim_px_sz = 5.0
msg = (
f"Overriding volume pixel size, {vol_px_sz}, with "
f"user provided pixel size of {sim_px_sz} angstrom."
)

assert msg not in caplog.text

sim = Simulation(vols=vol, pixel_size=sim_px_sz)

assert msg in caplog.text
assert sim.pixel_size == sim_px_sz


def test_mismatched_pixel_size():
"""
Confirm raises error when explicit Simulation and CTFFilter pixel sizes mismatch.
Expand Down