-
Notifications
You must be signed in to change notification settings - Fork 26
Fix IndexSource filter mapping #1230
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d21b18f
add filter/preprocess specific test of indexed source
garrettwrong b7e2fae
fix the indexed source filter mapping
garrettwrong 9c9dfcf
fix inds dtype
garrettwrong 82929fa
tmp unmark test, test CI
garrettwrong 7e3947a
revert
garrettwrong 5fb55c8
rescale internal pixel size after downsample
garrettwrong c35818a
add more source saving tests
garrettwrong 3a39658
pass through pixel_size=None
garrettwrong be500a4
exhaustive index-save-load testing
garrettwrong a89939c
cleanup indexed source test, prep for review
garrettwrong 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
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 |
|---|---|---|
| @@ -1,9 +1,14 @@ | ||
| import logging | ||
| import os | ||
| import tempfile | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from aspire.source import Simulation | ||
| from aspire.downloader import emdb_8012 | ||
| from aspire.operators import CTFFilter | ||
| from aspire.source import RelionSource, Simulation | ||
| from aspire.utils import Rotation | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
@@ -49,3 +54,153 @@ def test_repr(sim_fixture): | |
|
|
||
| # Check index counts are mentioned in the repr | ||
| assert f"{sim2.n} of {sim.n}" in repr(sim2) | ||
|
|
||
|
|
||
| @pytest.mark.expensive | ||
| def test_filter_mapping(): | ||
| """ | ||
| This test is designed to ensure that `unique_filters` and `filter_indices` | ||
| are being remapped correctly upon slicing. | ||
|
|
||
| Additionally it tests that a realistic preprocessing pipeline is equivalent | ||
| and can be saved then reloaded. | ||
| """ | ||
|
|
||
| # Generate N projection images, | ||
| # using N//2 rotations and | ||
| # N//2 ctf filters such that images[0::2] == images[1::2]. | ||
| N = 100 | ||
| SEED = 1234 | ||
| DT = np.float64 | ||
| DS = 129 | ||
|
|
||
| v = emdb_8012().astype(DT) | ||
|
|
||
| # Generate N//2 rotations | ||
| rots = Rotation.generate_random_rotations(N // 2, dtype=DT, seed=SEED) | ||
| angles = Rotation(np.repeat(rots, 2, axis=0)).angles | ||
|
|
||
| # Generate N//2 rotations and repeat indices | ||
| defoci = np.linspace(1000, 25000, N // 2) | ||
| ctf_filters = [ | ||
| CTFFilter( | ||
| v.pixel_size, | ||
| 200, | ||
| defocus_u=defoci[d], | ||
| defocus_v=defoci[-d], | ||
| defocus_ang=np.pi / (N // 2) * d, | ||
| Cs=2.0, | ||
| alpha=0.1, | ||
| ) | ||
| for d in range(N // 2) | ||
| ] | ||
| ctf_indices = np.repeat(np.arange(N // 2), 2) | ||
|
|
||
| # Construct the source | ||
| src = Simulation( | ||
| vols=v, | ||
| n=N, | ||
| dtype=DT, | ||
| seed=SEED, | ||
| unique_filters=ctf_filters, | ||
| filter_indices=ctf_indices, | ||
| angles=angles, | ||
| offsets=0, | ||
| amplitudes=1, | ||
| ).cache() | ||
|
|
||
| srcA = src[0::2] | ||
| srcB = src[1::2] | ||
|
|
||
| # Sanity check the images before proceeding | ||
| np.testing.assert_allclose(srcA.images[:], src.images[0::2]) | ||
| np.testing.assert_allclose(srcB.images[:], src.images[1::2]) | ||
| # Confirm the intention of the test | ||
| np.testing.assert_allclose(srcB.images[:], srcA.images[:]) | ||
|
|
||
| # Preprocess the `src` stack | ||
| pp = ( | ||
| src.phase_flip() | ||
| .downsample(DS) | ||
| .normalize_background() | ||
| .legacy_whiten() | ||
| .invert_contrast() | ||
| .cache() | ||
| ) | ||
|
|
||
| # Preprocess the indexed sources | ||
| ppA = ( | ||
| srcA.phase_flip() | ||
| .downsample(DS) | ||
| .normalize_background() | ||
| .legacy_whiten() | ||
| .invert_contrast() | ||
| .cache() | ||
| ) | ||
| ppB = ( | ||
| srcB.phase_flip() | ||
| .downsample(DS) | ||
| .normalize_background() | ||
| .legacy_whiten() | ||
| .invert_contrast() | ||
| .cache() | ||
| ) | ||
|
|
||
| # Confirm we match the original images | ||
| np.testing.assert_allclose(ppA.images[:], pp.images[0::2], atol=1e-6) | ||
| np.testing.assert_allclose(ppB.images[:], pp.images[1::2], atol=1e-6) | ||
| # Confirm A and B are equivalent | ||
| np.testing.assert_allclose(ppB.images[:], ppA.images[:], atol=1e-6) | ||
|
|
||
| # Create a tmp dir for this test output | ||
| with tempfile.TemporaryDirectory() as tmpdir_name: | ||
| # Save the initial images | ||
| src.save(os.path.join(tmpdir_name, "src.star")) | ||
| srcA.save(os.path.join(tmpdir_name, "srcA.star")) | ||
| srcB.save(os.path.join(tmpdir_name, "srcB.star")) | ||
|
|
||
| # Save the preprocessed images. | ||
| pp.save(os.path.join(tmpdir_name, "pp.star")) | ||
| ppA.save(os.path.join(tmpdir_name, "ppA.star")) | ||
| ppB.save(os.path.join(tmpdir_name, "ppB.star")) | ||
|
|
||
| # Reload, assigning `pixel_size`. | ||
| _src = RelionSource( | ||
| os.path.join(tmpdir_name, "src.star"), pixel_size=src.pixel_size | ||
| ) | ||
| _srcA = RelionSource( | ||
| os.path.join(tmpdir_name, "srcA.star"), pixel_size=srcA.pixel_size | ||
| ) | ||
| _srcB = RelionSource( | ||
| os.path.join(tmpdir_name, "srcB.star"), pixel_size=srcB.pixel_size | ||
| ) | ||
| _pp = RelionSource( | ||
| os.path.join(tmpdir_name, "pp.star"), pixel_size=pp.pixel_size | ||
| ) | ||
| _ppA = RelionSource( | ||
| os.path.join(tmpdir_name, "ppA.star"), pixel_size=ppA.pixel_size | ||
| ) | ||
| _ppB = RelionSource( | ||
| os.path.join(tmpdir_name, "ppB.star"), pixel_size=ppB.pixel_size | ||
| ) | ||
|
|
||
| # Confirm reloaded sources match the source it was saved from. | ||
| # This implies the equalities in the next section translate | ||
| # to the original source as well | ||
| # Ideally, _if everything is working_, many of these are redundant. | ||
| # If something fails to work, they may help pinpoint the fault. | ||
| np.testing.assert_allclose(_src.images[:], src.images[:]) | ||
| np.testing.assert_allclose(_srcA.images[:], srcA.images[:]) | ||
| np.testing.assert_allclose(_srcB.images[:], srcB.images[:]) | ||
| np.testing.assert_allclose(_pp.images[:], pp.images[:], atol=1e-6) | ||
| np.testing.assert_allclose(_ppA.images[:], ppA.images[:], atol=1e-6) | ||
| np.testing.assert_allclose(_ppB.images[:], ppB.images[:], atol=1e-6) | ||
|
|
||
| # Confirm reloading slices matches the reloading saved stack of images. | ||
| np.testing.assert_allclose(_srcA.images[:], _src.images[0::2]) | ||
| np.testing.assert_allclose(_srcB.images[:], _src.images[1::2]) | ||
| np.testing.assert_allclose(_ppA.images[:], _pp.images[0::2], atol=1e-5) | ||
| np.testing.assert_allclose(_ppB.images[:], _pp.images[1::2], atol=1e-5) | ||
| # Confirm A and B are still equivalent | ||
| np.testing.assert_allclose(_srcB.images[:], _srcA.images[:]) | ||
| np.testing.assert_allclose(_ppB.images[:], _ppA.images[:]) | ||
|
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. See #1236 for additional tests that might fit here. |
||
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.
Noting that see don't necessarily need to be copied, but it should be less risky in case they are mutated.