Skip to content

Commit b279d25

Browse files
committed
add offset tests
1 parent b78fc20 commit b279d25

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

tests/test_relion_source.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66

77
from aspire.source import RelionSource
8+
from aspire.utils import RelionStarFile
89
from aspire.volume import SymmetryGroup
910

1011
from .test_starfile_stack import StarFileTestCase
@@ -101,3 +102,70 @@ def test_pixel_size(caplog):
101102
src = RelionSource(starfile_im_pix_size, pixel_size=1.234) # 1.4 in metadata
102103
# Ensure we prefer user provided
103104
np.testing.assert_allclose(src.pixel_size, 1.234)
105+
106+
107+
def test_offsets_conversion():
108+
"""
109+
Check that offset convention gets converted to Relion >= 3.1 convention.
110+
"""
111+
starfile = os.path.join(DATA_DIR, "sample_particles_relion30.star")
112+
113+
# Extract pixel valued offsets from starfile prior to source instantiation.
114+
metadata = RelionStarFile(starfile).get_merged_data_block()
115+
pixel_offsets = np.column_stack((metadata["_rlnOriginX"], metadata["_rlnOriginY"]))
116+
117+
# Create Relion Source and extract offsets from metadata using updated field names.
118+
src = RelionSource(starfile)
119+
angst_offsets = src.get_metadata(["_rlnOriginXAngst", "_rlnOriginYAngst"])
120+
121+
# Check that old convention offset fields have been removed.
122+
assert "_rlnOriginX" not in src._metadata
123+
assert "_rlnOriginY" not in src._metadata
124+
125+
# Check that offsets in metadata match up to pixel/angstrom conversion.
126+
np.testing.assert_allclose(angst_offsets / src.pixel_size, pixel_offsets)
127+
128+
# src.offsets should still return pixel valued offsets.
129+
np.testing.assert_allclose(src.offsets, pixel_offsets)
130+
131+
132+
def test_offsets():
133+
"""
134+
Check that offsets are loaded properly with starfile field _rlnOriginX(Y)Angst.
135+
"""
136+
# This starfile has offsets stored with angstrom values as _rlnOriginX(Y)Angst.
137+
starfile = os.path.join(DATA_DIR, "sample_particles_relion31.star")
138+
139+
# Create a RelionSource
140+
src = RelionSource(starfile)
141+
142+
# Check offsets are angstrom valued in metadata and correspond to src.offsets.
143+
angst_offsets = src.get_metadata(["_rlnOriginXAngst", "_rlnOriginYAngst"])
144+
np.testing.assert_allclose(src.offsets * src.pixel_size, angst_offsets)
145+
146+
147+
def test_offsets_save(tmp_path):
148+
"""
149+
Test that saving a RelionSource that was loaded with pixel offsets
150+
saves with angstrom valued offsets.
151+
"""
152+
# Starfile with pixel offsets.
153+
starfile = os.path.join(DATA_DIR, "sample_particles_relion30.star")
154+
155+
# Extract pixel valued offsets from starfile prior to source instantiation.
156+
metadata = RelionStarFile(starfile).get_merged_data_block()
157+
pixel_offsets = np.column_stack((metadata["_rlnOriginX"], metadata["_rlnOriginY"]))
158+
159+
# Create and RelionSource and save to starfile.
160+
src = RelionSource(starfile)
161+
save_path = tmp_path / "test_file.star"
162+
src.save(save_path)
163+
164+
# Saved starfile should have angstrom valued offsets.
165+
metadata = RelionStarFile(save_path).get_merged_data_block()
166+
angst_offsets = np.column_stack(
167+
(metadata["_rlnOriginXAngst"], metadata["_rlnOriginYAngst"])
168+
)
169+
170+
# Check saved offsets match original up to pixel_size scaling.
171+
np.testing.assert_allclose(angst_offsets / src.pixel_size, pixel_offsets)

0 commit comments

Comments
 (0)