-
Notifications
You must be signed in to change notification settings - Fork 26
ImageSource.save Optics Block #1337
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
j-c-c
wants to merge
25
commits into
develop
Choose a base branch
from
save_optics_group
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
f709929
initial ImageSource.save with optics block
j-c-c 133a3d7
add _rlnImageSize column to metadata when saving.
j-c-c bc01f4c
Add _rlnImageSize to coordinate source test.
j-c-c 0b1204f
use rlnImageName to get n_rows
j-c-c e4e24b0
test for sim save with optics block
j-c-c 7c88e0a
add _rlnImageDimensionality
j-c-c 3e78f9a
test save/load roundtrip w/ phase_flip.
j-c-c 816923c
update coord source test
j-c-c 85b9fd4
cleanup
j-c-c 10056b1
remove unused var
j-c-c 6a84644
missing optics fields warning. warning test. code comments.
j-c-c 35b0532
removed unused import.
j-c-c 4da7ee8
deprecate _rlnAmplitude in favor of private __amplitude field.
j-c-c 1ef064c
use _aspireAmplitude to retain ampitudes for saved files
j-c-c 020e38c
ensure pixel size is added to mrc header
j-c-c 65fbfcd
test mrc pixel_size in header
j-c-c 5c28890
Use defaultdict
j-c-c 8ab9ee7
Remove old file compatibility
j-c-c 60942fa
Remove _aspireAmplitude from relion_metadata_fields dict.
j-c-c 7091078
Test save on source slices
j-c-c 195f44c
Always write an optics block
j-c-c a3d6381
Gallery example simulation -> relion
j-c-c 7324533
clean up gallery
j-c-c 665e038
_aspireMetadata 'no_ctf' tag for missing optics fields. Detect ASPIRE…
j-c-c 5cb9557
test cleanup
j-c-c File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| """ | ||
| Simulated Stack → RELION Reconstruction | ||
| ======================================= | ||
|
|
||
| This experiment shows how to: | ||
|
|
||
| 1. build a synthetic dataset with ASPIRE, | ||
| 2. write the stack via ``ImageSource.save`` so RELION can consume it, and | ||
| 3. call :code:`relion_reconstruct` on the saved STAR file. | ||
| """ | ||
|
|
||
| # %% | ||
| # Imports | ||
| # ------- | ||
|
|
||
| import logging | ||
| from pathlib import Path | ||
|
|
||
| import numpy as np | ||
|
|
||
| from aspire.downloader import emdb_2660 | ||
| from aspire.noise import WhiteNoiseAdder | ||
| from aspire.operators import RadialCTFFilter | ||
| from aspire.source import Simulation | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| # %% | ||
| # Configuration | ||
| # ------------- | ||
| # We set a few parameters to initialize the Simulation. | ||
| # You can safely alter ``n_particles`` (or change the voltages, etc.) when | ||
| # trying this interactively; the defaults here are chosen for demonstrative purposes. | ||
|
|
||
| output_dir = Path("relion_save_demo") | ||
| output_dir.mkdir(exist_ok=True) | ||
|
|
||
| n_particles = 512 | ||
| snr = 0.25 | ||
| voltages = np.linspace(200, 300, 3) # kV settings for the radial CTF filters | ||
| star_path = output_dir / f"sim_n{n_particles}.star" | ||
|
|
||
|
|
||
| # %% | ||
| # Volume and Filters | ||
| # ------------------ | ||
| # Start from the EMDB-2660 ribosome map and build a small set of radial CTF filters | ||
| # that RELION will recover as optics groups. | ||
|
|
||
| vol = emdb_2660() | ||
| ctf_filters = [RadialCTFFilter(voltage=kv) for kv in voltages] | ||
|
|
||
|
|
||
| # %% | ||
| # Simulate, Add Noise, Save | ||
| # ------------------------- | ||
| # Initialize the Simulation: | ||
| # mix the CTFs across the stack, add white noise at a target SNR, | ||
| # and write the particles and metadata to a RELION-compatible STAR/MRC stack. | ||
|
|
||
| sim = Simulation( | ||
| n=n_particles, | ||
| vols=vol, | ||
| unique_filters=ctf_filters, | ||
| noise_adder=WhiteNoiseAdder.from_snr(snr), | ||
| ) | ||
| sim.save(star_path, overwrite=True) | ||
|
|
||
|
|
||
| # %% | ||
| # Running ``relion_reconstruct`` | ||
| # ------------------------------ | ||
| # ``relion_reconstruct`` is an external RELION command, so we just show the call. | ||
| # Run this in a RELION-enabled shell after generating the STAR file above. | ||
|
|
||
| relion_cmd = [ | ||
| "relion_reconstruct", | ||
| "--i", | ||
| str(star_path), | ||
| "--o", | ||
| str(output_dir / "relion_recon.mrc"), | ||
| "--ctf", | ||
| ] | ||
|
|
||
| logger.info(" ".join(relion_cmd)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -323,10 +323,10 @@ def test_dtype_passthrough(dtype): | |
| # Check dtypes | ||
| np.testing.assert_equal(src.dtype, dtype) | ||
| np.testing.assert_equal(src.images[:].dtype, dtype) | ||
| np.testing.assert_equal(src.amplitudes.dtype, dtype) | ||
|
|
||
| # offsets are always stored as doubles | ||
| # offsets and amplitudes are always stored as doubles | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good idea, thanks
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep! |
||
| np.testing.assert_equal(src.offsets.dtype, np.float64) | ||
| np.testing.assert_equal(src.amplitudes.dtype, np.float64) | ||
|
|
||
|
|
||
| def test_stack_1d_only(): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ | |
| import numpy as np | ||
| import pytest | ||
|
|
||
| from aspire.source import RelionSource, Simulation | ||
| from aspire.source import ImageSource, RelionSource, Simulation | ||
| from aspire.utils import RelionStarFile | ||
| from aspire.volume import SymmetryGroup | ||
|
|
||
|
|
@@ -61,6 +61,41 @@ def test_symmetry_group(caplog): | |
| assert str(src_override_sym.symmetry_group) == "C6" | ||
|
|
||
|
|
||
| def test_prepare_relion_optics_blocks_warns(caplog): | ||
| """ | ||
| Test we warn when optics group metadata is missing. | ||
| """ | ||
| # metadata dict with no CTF values | ||
| metadata = { | ||
| "_rlnImagePixelSize": np.array([1.234]), | ||
| "_rlnImageSize": np.array([32]), | ||
| "_rlnImageDimensionality": np.array([2]), | ||
| "_rlnImageName": np.array(["[email protected]"]), | ||
| } | ||
|
|
||
| caplog.clear() | ||
| with caplog.at_level(logging.WARNING): | ||
| optics_block, particle_block = ImageSource._prepare_relion_optics_blocks( | ||
| metadata.copy() | ||
| ) | ||
|
|
||
| # We should get and optics block | ||
| assert optics_block is not None | ||
|
|
||
| # Verify defaults were injected. | ||
| np.testing.assert_allclose(optics_block["_rlnImagePixelSize"], [1.234]) | ||
| np.testing.assert_array_equal(optics_block["_rlnImageSize"], [32]) | ||
| np.testing.assert_array_equal(optics_block["_rlnImageDimensionality"], [2]) | ||
| np.testing.assert_allclose(optics_block["_rlnVoltage"], [0]) | ||
| np.testing.assert_allclose(optics_block["_rlnSphericalAberration"], [0]) | ||
| np.testing.assert_allclose(optics_block["_rlnAmplitudeContrast"], [0]) | ||
|
|
||
| # Caplog should contain the warnings about the three missing fields. | ||
| assert "Optics field _rlnSphericalAberration not found" in caplog.text | ||
| assert "Optics field _rlnVoltage not found" in caplog.text | ||
| assert "Optics field _rlnAmplitudeContrast not found" in caplog.text | ||
|
|
||
|
|
||
| def test_pixel_size(caplog): | ||
| """ | ||
| Instantiate RelionSource from starfiles containing the following pixel size | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what in the world
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😅