Skip to content

feat: fits use the lazy sparse operator, aggregator reapplies setup.json (5C-3) #885

Done

Description

@Jammy2211

Overview

Likelihood-lifecycle epic, phase 5C-3 (PyAutoGalaxy, then PyAutoLens). 5B-3 (PyAutoArray#712) added dataset.sparse_operator_for(xp, needs_operator, settings), a lazy sparse operator that is off by default. This phase makes the Galaxy and Lens fits use it through one choke point, so a plain FitImaging(dataset, tracer) gets the optimal operator when the policy is on. It also makes aggregator reloads reapply the search's setup.json, and merges the duplicated setup-resolution code into one shared helper. It ships as two PRs under this issue: 5C-3a PyAutoGalaxy, then 5C-3b PyAutoLens.

Plan

  • One choke point: AbstractFitInversion.sparse_operator (autogalaxy abstract_fit.py:74-86, inherited by all four fits) returns self.dataset.sparse_operator_for(xp=self._xp, needs_operator=self.total_mappers > 0, settings=self.settings), falling back to dataset.sparse_operator when the dataset has no such method.
    • Every aa.DatasetInterface(sparse_operator=...) site uses it.
    • The interferometer profile-subtraction helpers (sparse_profile_terms_from, uses_precomputed_data_term_from, sparse_chi_squared_from) take the resolved operator, so the inversion and the dirty-image correction never disagree.
  • Aggregator: _imaging_from / _interferometer_from reapply fit.value(name="setup") after masking and over-sampling. A missing file leaves the dataset unchanged; array-free datasets are skipped; a failed reapply logs one warning and reloads unapplied.
  • One shared resolution helper: a Galaxy AnalysisSetupMixin (no __init__) on AnalysisDataset, not on Analysis. It holds _init_setup, resolve_setup with an apply hook, the "would choose" line and save_setup, and setup_environment() moves into setup_util. Lens AnalysisSetupNoOperator becomes a thin subclass.
  • 5C-3a, PyAutoGalaxy: the property, the Galaxy call sites, the interferometer helpers, the Galaxy aggregator and the mixin.
  • 5C-3b, PyAutoLens: the Lens call sites (imaging :163, interferometer :251), the Lens aggregator fits, point/weak on the mixin, and an MRO test.

Design calls (human-approved 2026-11-24):

  1. One choke point (the property) for every DatasetInterface site and the interferometer correction helpers.
  2. Fits with only linear light profiles keep an attached operator (sparse_operator_for returns it first) but never trigger a lazy build (needs_operator = total_mappers > 0).
  3. The aggregator reapply is best-effort: on failure (e.g. a JAX sparse setup with no JAX), warn once and reload unapplied.
  4. Tests with the policy on: the figure of merit equals the dense one, the operator is built once, MGE-only builds nothing, JAX xp returns None, and a pixelization-plus-light-profile interferometer case exercises the dirty-image correction.
  • Tier + merge mode: effective level: supervised; merge mode: human /prm (per PR)
Detailed implementation plan

Affected Repositories

  • PyAutoLens (primary, issue home)
  • PyAutoGalaxy (5C-3a ships first)

Branch Survey

Repository Current Branch Dirty?
./PyAutoGalaxy main clean
./PyAutoLens main clean

Suggested branches: feature/likelihood-lifecycle-p5c3a-galaxy, then feature/likelihood-lifecycle-p5c3b-lens, in worktree ~/Code/PyAutoLabs-wt/likelihood-lifecycle-p5c3.

Implementation Steps

  1. 5C-3a autogalaxy/abstract_fit.py: rewrite the AbstractFitInversion.sparse_operator property as above, using getattr(self.dataset, "sparse_operator_for", None) for the fallback.
  2. 5C-3a autogalaxy/imaging/fit_imaging.py:148 and autogalaxy/interferometer/fit_interferometer.py:501: pass sparse_operator=self.sparse_operator.
  3. 5C-3a autogalaxy/interferometer/fit_interferometer.py: sparse_profile_terms_from (:92/:100), uses_precomputed_data_term_from (:198-200) and sparse_chi_squared_from (:285-306) take the resolved operator (an operator= argument, or read fit.sparse_operator). Update their callers at G :488 and the Lens equivalent; the Lens call site is 5C-3b if its signature changes.
  4. 5C-3a aggregator:
    • in autogalaxy/aggregator/imaging/imaging.py (_imaging_from) and autogalaxy/aggregator/interferometer/interferometer.py (_interferometer_from), after apply_mask / apply_over_sampling, reapply fit.value(name="setup");
    • the value is a plain dict from setup_util.setup_dict_from; if fit.value doesn't return it as a dict, use SearchOutput.plain;
    • dataset.apply_setup(d["setup_id"]), skipping array-free datasets, with a missing file → no-op and failure → one warning.
  5. 5C-3a consolidation: add AnalysisSetupMixin (autogalaxy/analysis/analysis/setup.py or setup_util.py) and move setup_environment() into setup_util. AnalysisDataset uses the mixin, overriding the key method and an _apply_resolved_setup(setup) hook (apply + consistency check, run after _use_jax is set). save_attributes calls save_setup. Behaviour is unchanged (existing 5C tests still pass).
  6. 5C-3b PyAutoLens:
    • autolens/imaging/fit_imaging.py:163 and autolens/interferometer/fit_interferometer.py:251 use self.sparse_operator; update the interferometer correction call (~:238);
    • autolens/aggregator/fit_imaging.py / fit_interferometer.py rely on the Galaxy dataset helpers (verify);
    • AnalysisSetupNoOperator → a thin subclass of the Galaxy mixin, removing the duplicated setup_environment;
    • an MRO/no-duplicate-base test for AnalysisPoint / AnalysisWeak.
  7. Tests:
    • Fits: in G test_autogalaxy/{imaging/test_fit_imaging.py, interferometer/test_fit_interferometer.py} and L test_autolens/{imaging,interferometer}/test_fit_*.py, with Settings(lazy_sparse_operator="auto"): figure of merit equals dense, built once, MGE-only builds nothing, JAX xp → no build, the pixelization + light profile interferometer case, and linear-light-only with an attached operator unchanged.
    • Aggregator: fake-fit round trip in the SimpleNamespace pattern of test_autolens/aggregator/test_tracer.py.
    • Mixin: the existing 5C setup tests still pass.
    • numba, jax and finufft uses go behind pytest.importorskip.
  8. Full suites plus pyauto-heart smoke --affected-by <repo> at each ship.

Key Files

  • autogalaxy/abstract_fit.py, autogalaxy/imaging/fit_imaging.py, autogalaxy/interferometer/fit_interferometer.py
  • autogalaxy/aggregator/imaging/imaging.py, autogalaxy/aggregator/interferometer/interferometer.py
  • autogalaxy/analysis/setup_util.py, autogalaxy/analysis/analysis/dataset.py
  • autolens/imaging/fit_imaging.py, autolens/interferometer/fit_interferometer.py, autolens/analysis/analysis/setup.py, autolens/aggregator/fit_*.py
  • Upstream: PyAutoArray autoarray/dataset/lazy_operator.py and sparse_operator_for (main faa80349)

Original Prompt

Click to expand starting prompt

Likelihood lifecycle phase 5C-3: PyAutoGalaxy + PyAutoLens fit call sites for the lazy operator, aggregator reloads reapply setup.json, one shared setup-resolution path

Type: feature
Target: PyAutoLens
Repos:

  • PyAutoGalaxy
  • PyAutoLens
    Themes:
  • hpc-gpu
  • profiling
    Difficulty: large
    Autonomy: supervised
    Priority: medium
    Status: draft
    Consequence: judge
    Witness: The PyAutoGalaxy and PyAutoLens fits (AbstractFit.sparse_operator, fit_imaging.py / fit_interferometer.py, lens/to_inversion.py) pass self.dataset.sparse_operator_for(xp, has_pixelization) (5B-3) into aa.DatasetInterface, so a plain al.FitImaging(dataset, tracer) gets the lazy operator when the policy is on. Loading a fit from the aggregator (al.agg.FitImagingAgg / FitInterferometerAgg and their PyAutoGalaxy counterparts) for a search that wrote files/setup.json reapplies that setup to the reloaded dataset (dataset.apply_setup). So the reloaded fit takes the same operator route as the search, and its log likelihood matches the stored value to the search's tolerance. A search with no setup.json reloads exactly as before. The analysis's modify_before_fit resolution and the 5B-3 lazy factory path share one resolution helper rather than two copies. The full PyAutoGalaxy and PyAutoLens suites are green.
    Review-minutes: 20
    Unattended: no
    Lane: any
    Gate: 5B-3 MERGED (the lazy factory operator and policy switch in PyAutoArray). Phasing approved by the human 2026-11-24.
    Epic: likelihood-lifecycle
    Phase: 5
    Parent: draft/research/autolens_profiling/likelihood_lifecycle_epic.md
    Filed: 2026-11-24

Context

  • 5C writes files/setup.json (setup fields plus chosen_by) beside each search's output, but autolens/aggregator/fit_imaging.py:91 / fit_interferometer.py:92 rebuild fits from dataset.fits with no operator. A reload can therefore take a different route from the search (the routes differ by about 1e-4 relative) and run slowly with heavy memory on CPU.
  • Setup resolution now lives in PyAutoGalaxy autogalaxy/analysis/setup_util.py / AnalysisDataset.resolve_setup (5C-1) and in the PyAutoLens point/weak setup mixin (5C-2). 5B-3 adds the factory-side resolution. 5C-3 makes them share one helper.

Out of scope

  • The lazy factory operator itself (5B-3).
  • The JAX path.
  • The default flip (R6).
  • Workspace changes (5F).

Activity

  1. Jammy2211 commented on Nov 24, 2026

    @Jammy2211
    CollaboratorAuthor

    5C-3a shipped as PyAutoLabs/PyAutoGalaxy#753 (pending-release): Galaxy fits resolve their sparse operator through one choke point (AbstractFitInversion.sparse_operator → sparse_operator_for), including the interferometer dirty-image correction; aggregator reloads reapply setup.json; AnalysisSetupMixin. Gate: 1659 Galaxy tests, Lens compat 1086 + 1 xfailed, Heart dev GREEN, smoke 5/5. 5C-3b (PyAutoLens call sites, point/weak on the mixin) follows after it merges.

  2. Jammy2211 commented on Nov 24, 2026

    @Jammy2211
    CollaboratorAuthor

    5C-3b shipped as #886 (pending-release): Lens fits resolve their sparse operator through the shared choke point (incl. the interferometer dirty-image correction); point/weak setup resolution now inherits PyAutoGalaxy's AnalysisSetupMixin. Gate: 1101 passed + 1 xfailed, Heart dev GREEN, smoke 4/4. This issue closes when #886 merges (close-out carries pending-release for PyAutoGalaxy#753 and #886).

  3. Jammy2211 commented on Nov 24, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped

    Phase 5C-3 merged in two PRs:

    Both pending-release. With lazy_sparse_operator on, fits outside modeling now get the optimal operator; it flips on with the R6 default flip. Next: 5D (PyAutoFit).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions