Skip to content

feat: Preloads API for reusing channel-invariant inversion quantities - #344

Merged
Jammy2211 merged 2 commits into
mainfrom
feature/datacube-shared-state
Jun 8, 2026
Merged

Jammy2211 merged 2 commits into
mainfrom
feature/datacube-shared-state

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

Adds an opt-in Preloads mechanism to the inversion stack: a channel-invariant inversion quantity (starting with the curvature matrix F = LᵀW̃L, the dominant inversion-setup cost) can be computed once and injected so repeated evaluations reuse it instead of rebuilding it. This is the PyAutoArray half of the datacube cross-Analysis shared-state feature (PyAutoLens#565 / the analysis_shared_state epic): when every spectral channel of a datacube shares one lens model, F is identical for all of them and is now built once and reused.

The primary, safe home for preloads is a shared / combined likelihood, where invariance is explicit and easy to verify. An earlier preload system was removed because it was bug-prone and hard to know when a quantity was safe to preload; the AbstractPreloads docstring records that history and makes the opt-in, caller-owns-invariance contract explicit, and preloads are deliberately not applied as standard.

API Changes

All additive and backward compatible — no removals or renames.

  • Added aa.AbstractPreloads and aa.PreloadsInterferometer(curvature_matrix=None) — containers for preloaded inversion quantities (new autoarray/preloads/ package).
  • AbstractInversion, the interferometer inversions, and the inversion_from / inversion_interferometer_from factory gain an optional preloads=None argument.
  • InversionInterferometerSparse.curvature_matrix now returns preloads.curvature_matrix directly when a preload is supplied (otherwise computes it as before).

See full details below.

Test Plan

  • python -m pytest test_autoarray/ — full suite passes (841 tests)
  • New test_autoarray/inversion/inversion/interferometer/test_interferometer.py::test__preloads_interferometer__curvature_matrix_returned_directly_and_skips_rebuild: a sentinel preload is returned verbatim (the F build is skipped), an empty PreloadsInterferometer() falls back to the standard build, and preloading the real F leaves the per-channel data_vector unchanged.
Full API Changes (for automation & release notes)

Added

  • autoarray.preloads.abstract.AbstractPreloads(curvature_matrix=None) (exported as aa.AbstractPreloads).
  • autoarray.preloads.interferometer.PreloadsInterferometer(curvature_matrix=None) (exported as aa.PreloadsInterferometer).

Changed Signature (backward compatible — new optional kwarg, default None)

  • AbstractInversion.__init__(... , preloads=None)
  • AbstractInversionInterferometer.__init__(... , preloads=None)
  • InversionInterferometerSparse.__init__(... , preloads=None)
  • inversion_from(... , preloads=None)
  • inversion_interferometer_from(... , preloads=None)

Changed Behaviour

  • InversionInterferometerSparse.curvature_matrix returns preloads.curvature_matrix when a preload with a non-None curvature_matrix is supplied; otherwise unchanged.

Migration

  • None required. To opt in, pass preloads=aa.PreloadsInterferometer(curvature_matrix=F) to the inversion / factory.

🤖 Generated with Claude Code

Add an opt-in `Preloads` mechanism: precompute an invariant inversion
quantity once and inject it so repeated evaluations reuse it instead of
rebuilding it.

- New `aa.AbstractPreloads` / `aa.PreloadsInterferometer` containers
  (autoarray/preloads/) with an optional `curvature_matrix` field.
- The inversion stack (`AbstractInversion`, the interferometer inversions,
  and the `inversion_from` / `inversion_interferometer_from` factory) gains
  an optional `preloads=None` argument, threaded as a single object.
- The sparse interferometer inversion returns `preloads.curvature_matrix`
  (`F = LᵀW̃L`) directly when provided, skipping the dominant rebuild.

Default behaviour is unchanged (`preloads=None`). The primary use is a
shared / combined likelihood (e.g. a datacube FactorGraphModel) where
invariance is explicit; an earlier preload system was removed for being
bug-prone, so the docstrings make the opt-in, caller-owns-invariance
contract explicit.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Extend AbstractPreloads / PreloadsInterferometer with an optional
mapper_galaxy_dict field, so a pre-built mapper (carrying the expensive
Delaunay triangulation) can be preloaded and reused alongside the
curvature matrix. Stored opaquely; the consumer populates/interprets it.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@Jammy2211

Copy link
Copy Markdown
Collaborator Author

Updated (56557cc7): added an optional mapper_galaxy_dict field to AbstractPreloads / PreloadsInterferometer so a pre-built mapper can be preloaded alongside the curvature matrix (mapper-sharing). Still additive/backward-compatible. test_autoarray 841 passing.

@Jammy2211
Jammy2211 merged commit 09a8fbb into main Jun 8, 2026
6 checks passed
@Jammy2211
Jammy2211 deleted the feature/datacube-shared-state branch June 8, 2026 14:46
@Jammy2211 Jammy2211 removed the pending-release PR queued for the next release build label Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant