-
Notifications
You must be signed in to change notification settings - Fork 1
fix: stale SMW cache and in-place beam smoothing on a live PICSLike instance #48
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
ggalloni
merged 5 commits into
master
from
worktree-fix-picslike-smw-cache-invalidation
Jul 14, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4b5e08b
fix: stale SMW cache and in-place beam smoothing on a live instance
ggalloni c061bf3
fix: drop the stale SMW cache on the MPI worker ranks too
ggalloni 272c49e
docs: note the worker-rank case in the changelog entry
ggalloni 73a67c0
fix: truncate an over-long C_ell to the analysis lmax in set_cls
ggalloni 92027ed
Merge remote-tracking branch 'origin/master' into worktree-fix-picsli…
ggalloni 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
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 |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| """Reusing a live PICSLike instance must not corrupt chi-squared. | ||
|
|
||
| Two pieces of per-evaluation state used to survive longer than they should: | ||
|
|
||
| * the harmonic (SMW) cache of ``(projected1, projected2, term1)``, which is | ||
| derived from the basis's noise factorisation and so belongs to a single basis | ||
| lifetime — a rebuild must not leave the next chi-squared evaluation pairing a | ||
| stale projection with the new basis's kernel; | ||
| * the theory spectra themselves, which beam smoothing multiplied in place, | ||
| re-smoothing the parameter grid's arrays on every evaluation. | ||
|
|
||
| Both were silent: no exception, just wrong numbers. | ||
| """ | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from picslike import PICSLike | ||
|
|
||
| #: Noise rescaling between basis #1 and basis #2. Same shapes on both sides, so | ||
| #: a stale cache is silently wrong rather than loudly broken. | ||
| NOISE_SCALE = 3.0 | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def cfg(sandboxed_config): | ||
| return sandboxed_config("tests/data/nside4/TQU/fast_config.yaml") | ||
|
|
||
|
|
||
| def _prepared(config_path): | ||
| """A PICSLike instance set up to the point just before the basis is built.""" | ||
| like = PICSLike(config_path) | ||
| like.setup_parameter_grid() | ||
| like.setup_fields() | ||
| like.setup_geometry() | ||
| like.setup_covariance_matrices() | ||
| like.setup_cls(lmax=like.lmax_signal) | ||
| like.setup_beams(lmax=like.lmax_signal) | ||
| return like | ||
|
|
||
|
|
||
| def test_repeated_evaluation_is_stable(cfg): | ||
| """The same parameter point evaluates to the same chi-squared every time.""" | ||
| like = _prepared(cfg) | ||
| like.setup_computation_basis(method="harmonic") | ||
| like.setup_maps() | ||
|
|
||
| point = like.parameter_grid.grid_points[0] | ||
| first, _ = like._compute_likelihood_point(point) | ||
| second, _ = like._compute_likelihood_point(point) | ||
|
|
||
| np.testing.assert_allclose(second, first, rtol=1e-12) | ||
|
|
||
|
|
||
| def test_basis_rebuild_invalidates_smw_cache(cfg): | ||
| """A second ``setup_computation_basis`` re-derives the cached SMW data.""" | ||
| like = _prepared(cfg) | ||
| like.setup_computation_basis(method="harmonic") | ||
| like.setup_maps() | ||
|
|
||
| point = like.parameter_grid.grid_points[0] | ||
| like._compute_likelihood_point(point) # populates the cache from basis #1 | ||
|
|
||
| # Rebuild on the live instance against a different noise model. The basis | ||
| # consumed (and nulled) noise_cov1, so the noise has to be re-materialised. | ||
| like.setup_covariance_matrices() | ||
| like.noise_cov1 *= NOISE_SCALE | ||
| like.setup_computation_basis(method="harmonic") | ||
| chi2, log_like = like._compute_likelihood_point(point) | ||
|
|
||
| ref = _prepared(cfg) | ||
| ref.noise_cov1 *= NOISE_SCALE | ||
| ref.setup_computation_basis(method="harmonic") | ||
| ref.setup_maps() | ||
| chi2_ref, log_like_ref = ref._compute_likelihood_point(point) | ||
|
|
||
| np.testing.assert_allclose(chi2, chi2_ref, rtol=1e-10) | ||
| np.testing.assert_allclose(log_like, log_like_ref, rtol=1e-10) |
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
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.
Considered, but declining — happy to be overruled by a human reviewer.
Mirroring the base signature means duplicating 10 parameters to add one line of cache invalidation, and that mirror rots silently.
Core.setup_computation_basisis not a stable signature:git log -Lon those lines shows it churning repeatedly —compress/delta_madded,compressionrenamed tobasis,method="auto"added, per-field thresholds added. Every one of those would have needed a matching edit here, and a missed one means PICSLike rejects a kwarg thatCoreaccepts — aTypeErroron a call that should work.On the three costs you name:
Thin wrapper over Core.setup_computation_basis — see there for the parameters), and Sphinx builds with zero new warnings against master.*args, **kwargsbuys the forwarding correctness with. It is the standard idiom for a pass-through override whose only job is a side effect beforesuper().The alternative that gets both — pinning
__signature__fromCore— is more magic than this override deserves.If the preference is to mirror it anyway, say so and it is a five-minute change.