Skip to content

Commit 2c8ecdf

Browse files
committed
_populate_pixel_size. Handle mismatches. tests
1 parent af591f8 commit 2c8ecdf

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

src/aspire/source/image.py

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import functools
33
import logging
44
import os.path
5+
import warnings
56
from abc import ABC, abstractmethod
67
from collections import OrderedDict
78
from collections.abc import Iterable
@@ -204,30 +205,48 @@ def __init__(
204205
)
205206
)
206207

207-
# Populate pixel_size with metadata if possible.
208-
if pixel_size is None:
209-
if self.has_metadata(["_rlnImagePixelSize"]):
210-
pixel_size = self.get_metadata(["_rlnImagePixelSize"])[0]
211-
elif self.has_metadata(["_rlnDetectorPixelSize", "_rlnMagnification"]):
212-
detector_pixel_size = self.get_metadata(["_rlnDetectorPixelSize"])[0]
213-
magnification = self.get_metadata(["_rlnMagnification"])[0]
214-
pixel_size = 10000 * detector_pixel_size / magnification
215-
else:
216-
raise ValueError(
217-
"No pixel size found in metadata. Pixel size must be provided."
218-
)
219-
else:
220-
self.set_metadata("_rlnImagePixelSize", pixel_size)
221-
222-
self.pixel_size = float(pixel_size)
223-
208+
self._populate_pixel_size(pixel_size)
224209
self._populate_symmetry_group(symmetry_group)
225210

226211
self.unique_filters = []
227212
self.generation_pipeline = Pipeline(xforms=None, memory=memory)
228213

229214
logger.info(f"Creating {self.__class__.__name__} with {len(self)} images.")
230215

216+
def _populate_pixel_size(self, pixel_size):
217+
# Populate pixel_size from metadata if possible.
218+
_pixel_size = None
219+
if self.has_metadata(["_rlnImagePixelSize"]):
220+
_pixel_size = self.get_metadata(["_rlnImagePixelSize"])[0]
221+
elif self.has_metadata(["_rlnDetectorPixelSize", "_rlnMagnification"]):
222+
detector_pixel_size = self.get_metadata(["_rlnDetectorPixelSize"])[0]
223+
magnification = self.get_metadata(["_rlnMagnification"])[0]
224+
_pixel_size = 10000 * detector_pixel_size / magnification
225+
226+
# Resolve any pixel_size conflicts.
227+
if _pixel_size is None and pixel_size is None:
228+
raise ValueError(
229+
"No pixel size found in metadata. Please provide `pixel_size` argument."
230+
)
231+
232+
if pixel_size is not None:
233+
# If both provided prefer user, warn on mismatch.
234+
if _pixel_size is not None and not np.allclose(_pixel_size, pixel_size):
235+
warnings.warn(
236+
f"User provided pixel_size: {pixel_size} angstrom, does not match"
237+
f" pixel_size found in metadata: {_pixel_size} angstrom. Setting"
238+
f" pixel_size to {pixel_size} angstrom.",
239+
UserWarning,
240+
stacklevel=2,
241+
)
242+
chosen = float(pixel_size)
243+
else:
244+
chosen = float(_pixel_size)
245+
246+
# Set pixel_size attribute and metadata.
247+
self.set_metadata("_rlnImagePixelSize", chosen)
248+
self.pixel_size = chosen
249+
231250
@property
232251
def symmetry_group(self):
233252
"""

tests/test_relion_source.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def test_pixel_size(caplog):
6767
- "_rlnDetectorPixelSize" and "_rlnMagnification"
6868
- User provided pixel size
6969
- No pixel size provided
70+
- Both user provided and metadata pixel size
7071
and check src.pixel_size is correct.
7172
"""
7273
starfile_im_pix_size = os.path.join(DATA_DIR, "sample_particles_relion31.star")
@@ -92,5 +93,11 @@ def test_pixel_size(caplog):
9293
np.testing.assert_equal(src.pixel_size, pix_size)
9394

9495
# Check we raise if pixel_size not provided and not found in metadata.
95-
with pytest.raises(ValueError, match=r".*No pixel size found in metadata.*"):
96+
with pytest.raises(ValueError, match="`pixel_size` not found in metadata"):
9697
src = RelionSource(starfile_no_pix_size)
98+
99+
# Check we warn if both provided and mismatched.
100+
with pytest.warns(UserWarning, match="does not match pixel_size"):
101+
src = RelionSource(starfile_im_pix_size, pixel_size=1.234) # 1.4 in metadata
102+
# Ensure we prefer user provided
103+
np.testing.assert_allclose(src.pixel_size, 1.234)

0 commit comments

Comments
 (0)