Skip to content

Commit fdeec0f

Browse files
committed
cast pixel_size as float correctly. Add test to check pixel_size is coherent between source and metadata.
1 parent a9d2d41 commit fdeec0f

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/aspire/source/image.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,9 @@ def pixel_size(self, value):
228228
"""
229229
Set the pixel_size attribute and update value in metadata.
230230
"""
231+
value = float(value)
232+
self._pixel_size = value
231233
self.set_metadata("_rlnImagePixelSize", value)
232-
self._pixel_size = float(value)
233234

234235
def _populate_pixel_size(self, pixel_size):
235236
"""

tests/test_array_image_source.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,35 @@ def test_pixel_size(caplog):
280280
np.testing.assert_allclose(src.pixel_size, user_px_sz)
281281

282282

283+
def test_pixel_size_type(tmp_path):
284+
"""
285+
Test that pixel_size dtype is stored in doubles as an attribute
286+
and in metadata.
287+
"""
288+
# Test for diffetent types of sources.
289+
sim = Simulation(n=2, pixel_size=1)
290+
arr_src = ArrayImageSource(sim.images[:].asnumpy(), pixel_size=1)
291+
292+
starfile = tmp_path / "source.star"
293+
sim.save(starfile)
294+
rln_src = RelionSource(starfile, pixel_size=1)
295+
296+
# Check attribute type
297+
assert isinstance(sim.pixel_size, float)
298+
assert isinstance(arr_src.pixel_size, float)
299+
assert isinstance(rln_src.pixel_size, float)
300+
301+
# Check type in _metadata dict
302+
assert isinstance(sim._metadata["_rlnImagePixelSize"][0], float)
303+
assert isinstance(arr_src._metadata["_rlnImagePixelSize"][0], float)
304+
assert isinstance(rln_src._metadata["_rlnImagePixelSize"][0], float)
305+
306+
# Check get_metadata type
307+
assert isinstance(sim.get_metadata("_rlnImagePixelSize")[0], float)
308+
assert isinstance(arr_src.get_metadata("_rlnImagePixelSize")[0], float)
309+
assert isinstance(rln_src.get_metadata("_rlnImagePixelSize")[0], float)
310+
311+
283312
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
284313
def test_dtype_passthrough(dtype):
285314
"""

0 commit comments

Comments
 (0)