Skip to content

docs: ship stored fit results so HowToLens tutorials skip sampling #130

Closed

Description

@Jammy2211

Overview

HowToLens tutorials make readers wait minutes for each non-linear search. This task ships small, git-tracked stored results so every search after the first loads instantly through PyAutoFit's existing "Fit Already Completed" path, and the tutorials say so and show the real run time. This is phase 1 (HowToLens chapter 2 plus the design gate); later phases cover chapter 3/4/optional and HowToFit/HowToGalaxy.

Plan

  • Confirm the design: PyAutoFit already skips the search when an output folder holds .completed and needs only files/samples_summary.json (samples.csv optional). Check that the stored folders' identifiers stay stable across a release; if not, stop and file the PyAutoFit "bare result from disk" phase instead.
  • Add a regeneration script that runs every chapter 2 search for real, records its wall time, and writes a minimal tracked folder per search (full samples only where a tutorial reads result.samples).
  • Keep tutorial 1's full output folder as the illustration of a real output directory.
  • Update each chapter 2 search cell's prose: the result is loaded on purpose, here is what the real fit cost, and how to rerun it yourself.
  • Add a check that runs outside test mode and fails if any chapter 2 search actually samples.
  • Regenerate notebooks; smoke and CI green.
  • Tier: judge — merge mode: human /prm.
Detailed implementation plan

Affected Repositories

  • HowToLens (primary). PyAutoFit only if the design gate fails (separate phase).

Branch Survey

Repository Current Branch Dirty?
./lens/HowToLens main (8aa7fa1) clean

Suggested branch: feature/howtolens-stored-results

Facts from the survey

  • NonLinearSearch.fit → paths.is_complete (.completed or a zip containing one) → result_via_completed_fit (autofit/non_linear/search/abstract_search.py), which loads samples_summary.json and tries samples.csv (FileNotFoundError → samples=None).
  • pre_fit_output still runs on a completed fit (visualize_before_fit), so measure its cost.
  • Test mode writes to output/test_mode/..., so smoke runs never touch shipped results: the check must run with test mode off.
  • .gitignore ignores output/ wholesale.
  • Chapter 2 searches (post-docs: implement approved Chapter 2 lessons 1–4 #113): t1 (1), t2 practicalities (1, reads samples), t3 why_modeling_is_hard (several; one reuses t2's tutorial_2_practicalities folder; reads samples), t4 linear_profiles + mge (2), t5 data_and_likelihoods (2), t6 search_chaining (2, reads samples).

Implementation Steps

  1. Design gate: run chapter 2 with test mode off in a scratch output dir; record per-search identifier, folder size, needed files and wall time; confirm the identifier is stable against a re-import and the current PyPI release. Report on the issue.
  2. scripts/stored_results/generate.py (or similar): runs each search, keeps .completed, .identifier, files/{samples_summary,model,search,samples_info}.json, + samples.csv where needed, writes a run_times.json manifest (search name → wall time, machine, library version).
  3. .gitignore: un-ignore the stored-result folders for chapter 2 only.
  4. Tutorial prose: a short note at each search cell (from the manifest's times) and a one-paragraph explanation in tutorial 1/README.
  5. Check script (scripts/stored_results/check.py): runs with test mode off and asserts every chapter 2 search logs "Fit Already Completed"; wire into CI or the smoke harness.
  6. Regenerate notebooks + llms files; run smoke.

Key Files

  • scripts/chapter_2_lens_modeling/tutorial_*.py
  • .gitignore, output/howtolens/chapter_2/...
  • PyAutoFit autofit/non_linear/search/abstract_search.py, autofit/non_linear/paths/directory.py (read only)

Witness

Running every chapter 2 tutorial from a clean checkout with test mode off logs "Fit Already Completed: skipping non-linear search" for each search (zero searches sample); the tracked stored-results footprint is reported in MB.

Original Prompt

Click to expand starting prompt

HowTo series: ship stored fit results so notebooks run without sampling waits

Type: docs
Target: HowToLens
Repos:

  • PyAutoFit
  • HowToFit
  • HowToGalaxy
  • HowToLens
    Difficulty: too-large
    Autonomy: supervised
    Priority: normal
    Memory: wiki/lensing/sources/dark-matter-substructure.md; wiki/galaxies/sources/massive-ellipticals.md; wiki/galaxies/sources/cosmos-survey.md
    Status: formalised
    Consequence: glance
    Witness: running every HowToLens tutorial that calls search.fit with a clean checkout logs "Fit Already Completed: skipping non-linear search" for each search (zero searches sample), and the tracked stored-results footprint is reported in MB.
    Review-minutes: 3
    Unattended: needs-slicing

Human idea (2026-11-01). Make HowToLens (then HowToFit, HowToGalaxy, other HowTo series) ship stored, lightweight fit results that PyAutoFit loads instantly, so readers complete notebooks uninterrupted.

Intent

  • Every tutorial that runs a non-linear search (HowToLens: 14 scripts — chapter_2_lens_modeling tutorials 1–7, chapter_4_scaling_up_lensing tutorials 1–6, chapter_optional/tutorial_searches) loads a pre-computed result when the reader runs the cell; no sampling wait.
  • Tutorials say clearly that this is intentional, and show the real run time that produced each stored result (keep search.summary / timing so readers see what the fit actually cost).
  • The FIRST fit (ch2 tutorial 1) may ship its full output folder, for illustration of a real output directory; later fits use the lightweight store.
  • Only a few cells and comments per tutorial change.

Survey already done (2026-11-01, read-only)

  • PyAutoFit already skips the search when a fit is complete: NonLinearSearch.fit -> paths.is_complete (.completed file or a zip holding one) -> result_via_completed_fit (fit/PyAutoFit/autofit/non_linear/search/abstract_search.py ~L330, ~L513), which needs only files/samples_summary.json (via paths.load_samples_summary); full samples.csv is OPTIONAL (FileNotFoundError -> samples=None).
  • So option (b) — PyAutoFit as-is, ship minimal <output>/<path_prefix>/<name>/<identifier>/ folders with .completed + files/samples_summary.json (+ model.json/search.json) — looks viable without a library change.
  • HowToLens .gitignore ignores output/ wholesale; shipped results need a dedicated tracked location or un-ignore rule.

Open questions / risks for the plan

  1. Identifier fragility: the output folder is keyed by a hash of model + search settings (unique_tag); any prior/default-config/library change moves the hash and the stored result silently stops matching (fit reruns). Need a regeneration script + a CI/Heart staleness check (fail if a tutorial would actually sample).
  2. Which tutorials use result.samples (corner plots, PDFs, errors from full samples — e.g. ch2 tutorial 7 results)? Those need samples.csv shipped, or rewriting to use samples_summary only. Measure on-disk size.
  3. pre_fit_output still runs (visualize_before_fit etc.) on a completed fit — check cost; consider whether output mode should skip it.
  4. Option (a) fallback: a PyAutoFit "bare-bones result from disk" API (small json -> Result) only if (b) is too fragile or too big. Prefer (b).
  5. Where timing lives: samples_summary/search.summary already record time? Confirm and surface it in tutorial text.
  6. Colab/notebook path: stored results must be present in the Colab bootstrap clone too.

Suggested phasing

  • P1 research: per-tutorial survey — files needed, sizes, samples.csv dependence, identifier stability across a release; decide (a) vs (b).
  • P2 (only if (a)): PyAutoFit library change.
  • P3 HowToLens chapter 2 rollout (generator script, stored results, tutorial prose + run-time display, staleness check).
  • P4 HowToLens chapter 4 + optional.
  • P5 other HowTo series (HowToFit, HowToGalaxy).

🤖 Generated with Claude Code

Activity

  1. Jammy2211 commented on Nov 6, 2026

    @Jammy2211
    CollaboratorAuthor

    Design-gate findings + decision (2026-11-06)

    Load path confirmed. Ch2 tutorial 2 run for real (test mode off): search Time To Run = 0:33:34 (of which Visualization Time = 1120 s from quick-update figures). Re-running the script then logged Fit Already Completed: skipping non-linear search and printed the result ~54 s in (setup + JAX compile only). Tutorial 1 whole script: 250 s.

    Identifier. The output hash covers the model (class paths + priors) and the Nautilus __identifier_fields__ (n_live, n_update, enlarge_per_dim, …, seed), not n_batch or update frequencies. Stored results survive prose/cosmetic edits; they move if a profile class changes module, a default prior changes, or a Nautilus default changes → the staleness check is required.

    Size. Tutorial 2's completed folder is 22 MB; files/samples.csv alone is 12 MB (14,534 rows); samples_summary.json 28 KB, model.json 12 KB.

    Decision (human): stored results must be lightweight; the goal is getting readers through the tutorials quickly.

    • Default: ship no samples.csv. Results load from samples_summary.json (+ model/search json, .completed).
    • Only where a tutorial reads result.samples (ch2 t2, t3, t6): ship an extremely thin samples.csv (order 10 equal-weight samples), enough for the cells to run. Check what corner plots/PDF cells do with ~10 samples and reword those cells if needed.
    • Tutorial 1 still keeps its full output folder as the illustration.

    Separate bug found: aplt.corner_anesthetic(samples=result.samples) at tutorial_2_practicalities.py:453 ran 44+ minutes on the 20-parameter model (not hung: 400 axes, 190 2D-KDE panels, pandas shared-axis tick pass). Filed in Mind as draft/bug/autofit/corner_anesthetic_takes_40_minutes_on_a.md; thin samples make it fast here, but the library default still needs fixing.

  2. Jammy2211 commented on Nov 6, 2026

    @Jammy2211
    CollaboratorAuthor

    Correction to the comment above: thin samples do NOT fix the corner-plot slowness. With samples.csv cut to 10 equal-weight rows (5 KB, loads fine: Fit Already Completed), corner_anesthetic at tutorial_2_practicalities.py:453 still ran >14 min. The time goes into building 400 shared matplotlib axes and pandas' per-panel handle_shared_axes tick-label pass, independent of sample count. Bug prompt updated. Consequence for this task: until that bug is fixed, ch2 tutorial 2 (and any 20-parameter corner plot) needs a tutorial-side workaround, e.g. plotting a parameter subset.

  3. Jammy2211 commented on Nov 10, 2026

    @Jammy2211
    CollaboratorAuthor

    Progress (2026-11-10)

    Corner-plot fix PyAutoFit#1761 shipped in 2026.11.9.1. With it, tutorial 2 runs end to end in 36–48 s from its stored result (was >90 min).

    Pushed 5051c33 on feature/howtolens-stored-results:

    • .github/scripts/stored_results.py — harvest <full-output-root> copies the minimal set (.completed, .identifier, search.summary, files/{model,search,samples_info,samples_summary}.json, thinned samples.csv; ~72 KB per fit, verified byte-identical after a load so readers never see modified files) and the illustrated tutorial 2 folder (full minus image/search corner PNGs, ~3.6 MB). check runs tutorials 2–6 with test mode off and start_resume_fit patched to fail (68 s; verified OK for t2 and FAILED-with-folder for unstored t3–t6).
    • Simulators lens_sersic, no_lens_light__mass_sis, lens_extra_galaxy: noise_seed=1 (was random, -1), so every reader simulates the exact data the stored fits describe. Verified byte-identical data.fits across runs.
    • Prose: tutorial 1 = the one live search; tutorial 2 __Stored Results__ section + prints the recorded run time (result.samples.time); one-line pointers in tutorials 3–6. Tutorial 3's existing run-time table already reads recorded times.

    Decisions flagged for review:

    1. Tutorial 1 is not stored: its search has no name/path_prefix, so output is disabled by design (~4 min live). Tutorial 2 carries the illustrated full folder, since it is the tutorial that walks the output tree.
    2. Samples: 10 equal-weight draws by default, but 1,000 for tutorial_2_practicalities and tutorial_6_search_2, whose corner plots the prose asks readers to interpret (10 points would break that lesson). ~0.5 MB each.
    3. Optimiser fits (tutorial 6 search 1, MultiStartProdigy) keep their highest-likelihood rows, since they carry no posterior weights.

    Next: regenerate tutorials 2–6 on RAL (ral partition, 5 jobs, t3 after t2, private checkout at tag 2026.11.9.1), harvest, git add -f, run check, regenerate notebooks, ship. Blocked on an interactive SSH login to RAL.

  4. Jammy2211 commented on Nov 10, 2026

    @Jammy2211
    CollaboratorAuthor

    RAL regeneration running (private checkout at 2026.11.9.1, jobs t2 414811, t3 414812 after t2, t4 414813, t5 414814, t6 414818). Tutorial 6 hit the jaxlib 0.11.2 XLA:CPU deadlock at its first MultiStartProdigy jit (0% CPU, no progress): resubmitted with XLA_FLAGS=--xla_cpu_multi_thread_eigen=false, now optimising normally. Reader risk to note in the PR: anyone who deletes the stored folder and reruns tutorial 6 on CPU with jaxlib 0.11.2 may hang the same way; the stored result avoids it by default.

  5. Jammy2211 commented on Nov 10, 2026

    @Jammy2211
    CollaboratorAuthor

    State at end of 2026-11-10 (resume here)

    Branch feature/howtolens-stored-results at c8fc19a. stored_results.py check passes for tutorials 2-6: every chapter 2 search loads a stored result. Stored total about 5.5 MB (tutorial 2's illustrated folder 3.9 MB).

    Tutorial changes made on this branch, for review:

    • Tutorial 3 less-thorough n_live 21 → 50. 21 crashed Nautilus every time (LinAlgError in its first bounds).
    • seed=1 on all tutorial 2/3 Nautilus searches. Unseeded, tutorial 2 (n_live=200) reached the right solution on the laptop but a poor one on RAL (log Z −42,262 vs +3,322).
    • Prior tuning 75 → 150 (75 stalled; 100 reached a poor fit).
    • Reduced complexity 75 → 300 (75 and 150 reached poor fits; 200 stalled). With 300 it now matches the thorough search.
    • Seed 1 and seed 2 gave the same outcomes for every search (good fits ≈ +3,322 to +3,340; less thorough fails at −59,734, which is its lesson).
    • Simulators noise_seed=1.

    Found along the way:

    • XLA:CPU deadlock on jaxlib 0.11.2 hit tutorials 4 and 6 on RAL. Research filed in Mind: draft/research/autonerves/jax_version_policy_and_the_recurring_xla.md.
    • Library bug, not yet filed: the search scores elliptical components with magnitude ≥ 1. 99% of the failed less-thorough fit's weight sits on invalid source shapes, and PyAutoFit then can't rebuild its max-likelihood sample. harvest works around it by keeping valid rows only.
    • Loading a stored fit writes a .zip and status files next to its folder. They're gitignored, so readers' git status stays clean.

    Next: read tutorial 3's printed output once for fallback warnings; regenerate notebooks and llms files; /ship_workspace; PR for human /prm; file the ell_comps bug via /intake.

  6. Jammy2211 commented on Nov 11, 2026

    @Jammy2211
    CollaboratorAuthor

    Heart RED override: development only (2026-11-11)

    Authorization (live human, this session, verbatim): "yes override, open the PR". This authorizes commit, push and PR-open for feature/howtolens-stored-results only. Merge stays separate: human /prm with every required GitHub check green. No release.

    Heart verdict at ship time: RED (pyauto-heart readiness). Exact reasons:

    • autocti_workspace: Smoke Tests failure on main
    • manifest drift: organism-map blocks (generated) — 7 mismatch(es) vs PyAutoMind/repos.yaml
    • PyAutoArray: open PR 7d old
    • PyAutoMind: open PR 7d old
    • release validation incomplete: no rehearsal for current source

    None involve HowToLens, and this branch does not claim to fix any of them.

    Branch gates passed (head a7aa184):

    • stored_results.py check: OK for Tutorials 2-6 (no search samples).
    • Tutorial 3 run as a reader: all five searches load; comparison tables correct.
    • Smoke (test mode, profile_smoke.yaml): scripts/simulator all PASS; scripts/chapter_2_lens_modeling Tutorials 2 and 4–7 PASS.
    • Tutorials 1 and 3 timed out at the 300 s cap during the batch smoke, with the laptop at load average 8.5–9.3 on 8 cores. Rerun individually under the same smoke environment, both PASS: Tutorial 1 in 24 s, Tutorial 3 in 45 s, no tracebacks. CI on main runs them in 6.1 s and 7.3 s. The PR's exact-head CI smoke is the authoritative leg and must be green before merge.
    • Notebooks regenerated from scripts via PyAutoHands; catalogue unchanged.
  7. Jammy2211 commented on Nov 11, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped

    Merged in #134 (merge 653c48320, 2026-11-11). Phase 1 of the HowTo stored-results epic is complete.

    • Chapter 2 Tutorials 2-6 load stored results from output/howtolens/chapter_2/ (10 fits, ~5.5 MB); Tutorial 1 stays the reader's one live search.
    • .github/scripts/stored_results.py (harvest, check) and the stored_results.yml CI job (PRs, main, daily), which passed against PyPI 2026.11.9.1 + jax 0.11.2.
    • Tutorial 3 live points fixed (50 / 150 / 300); seed=1 on Tutorial 2/3 searches; simulators noise_seed=1.
    • Colab gets the stored results from the next release's HowToLens tag. Today's tag predates this merge.
    • Shipped under the Heart RED development override (recorded above). Merged on a separate explicit human /prm with all 9 checks green.

    Follow-ups filed in Mind:

    • Phase 2, Chapters 3/4/optional: draft/docs/howtolens/howtolens_stored_results_phase_2_chapters_3.md.
    • Heart release check: draft/feature/pyautoheart/release_validation_check_howto_stored_fit_result.md.
    • ell_comps bug: draft/bug/autogalaxy/searches_score_elliptical_components_with_magnit.md.
    • JAX version policy: PyAutoNerves#213.
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