Skip to content

feat(model_figure): roll the model figure across autofit_workspace + HowToFit, EP state figure (phase 6a) #1620

Closed

Description

@Jammy2211

Overview

Phase 6a of the model-figures epic: the PyAutoFit surfaces cut of the phase 6 rollout map. Every script and tutorial in autofit_workspace and HowToFit that prints model.info should show af.ModelPlotter(model).figure() beside it, and both expectation-propagation scripts should draw the phase-5 EP state figure. Drawing that state figure after the high-level FactorGraphModel.optimise(...) is impossible today without a private API, so the task is library-first: one small additive change to EPResult, then two workspace PRs.

Plan

  • Library (PyAutoFit): expose the swept graph as EPResult.factor_graph (additive, default None), passed through from FactorGraphModel.optimise, with a test and a docs example.
  • autofit_workspace: a figure beside every bare model.info — 28 bare sites across 9 scripts.
  • HowToFit: the same across 8 tutorials — 14 bare sites, tutorial register.
  • EP: state figure + visualise_interval=1 in both EP scripts, plus the missing model_figure key in HowToFit/config/output.yaml.
  • Notebooks + catalogue regenerated per each repo's own convention.
  • Ship library-first: ship_library (PyAutoFit PR) → ship_workspace (two workspace PRs behind the library-first gate).
Detailed implementation plan

Affected Repositories

  • PyAutoFit (primary)
  • autofit_workspace
  • HowToFit

Work Classification: Both (library first)

Worktree root: ~/Code/PyAutoLabs-wt/model-figures-rollout-autofit/

Branch Survey

Repository Current Branch Dirty?
./PyAutoFit main clean
./autofit_workspace main clean
./HowToFit main clean

Suggested branch: feature/model-figures-rollout-autofit (same name in all three so workspace smoke CI checks out the same-named library branch)

1. Library: expose the swept graph on EPResult (PyAutoFit)

  • autofit/graphical/declarative/result.py: EPResult.__init__(..., factor_graph=None) storing self.factor_graph — docstring: "the factor graph the optimiser actually swept; pass it to af.EPPlotter (never FactorGraphModel.graph, which rebuilds)". Additive, default None.
  • autofit/graphical/declarative/abstract.py:210-214: pass factor_graph=opt.factor_graph.
  • Test in test_autofit/graphical/ (next to existing EP result/optimise tests; fixtures test_factor_failure_recovery.make_two_variable_approx or the declarative doubles): after optimise(...), result.factor_graph is the optimiser's graph and af.EPPlotter(result.factor_graph, ep_history=result.ep_history).figure(kind="state", path=tmp, format="png") writes a file with no factor reading absent.
  • docs/features/graphical.md "Seeing the EP run": add the high-level form af.EPPlotter(result.factor_graph, ep_history=result.ep_history).figure(kind="state") beside the existing optimiser form; Sphinx warnings must stay at baseline 30.
  • Ship: ship_library → PR (pending-release). Black on touched files only (black 25.1 reformats 263 files on main). Full test_autofit must pass (2843 at phase 5).

2. Workspace: figure beside every bare model.info (autofit_workspace, HowToFit)

Pattern (verbatim from phases 2/4, e.g. autofit_workspace/scripts/cookbooks/model.py:235-244, HowToFit/.../tutorial_2_graphical_model.py:217-235): print(x.info) → blank line → """ prose """ → af.ModelPlotter(x).figure(). The prose opens with the fixed map/legend sentence the first time it appears in a script, then one short paragraph on what this figure shows that the info cannot (which parameters are fixed/shared/related/asserted, plates, what changed since the previous figure). Later sites in the same script use 1-3 lines of prose. Default figure() settings (format "show" → inline in notebooks, commits no asset). No try/except in scripts; a guard belongs in the library.

Coverage rule (the prompt's acceptance is a grep: a figure call beside every model.info occurrence): add one at every bare site, with these explicit exceptions recorded in the completion record:

  • autofit_workspace/scripts/cookbooks/result.py:406 — indented print(model.info) inside the aggregator loop: one figure after the loop for the last loaded model, prose says the three are identical in structure.
  • result*.info / samples.info lines are not model prints (not counted).
  • Where a print re-shows an unchanged structure after a prior/value-only edit (e.g. cookbooks/model.py:109 vs the priors block), the figure still goes in, prose one line: "the map is unchanged; only the legend moved" — that contrast is the teaching point.

autofit_workspace sites (census, file: line numbers of bare prints): cookbooks/model.py 109, 352, 394, 486, 501, 584, 588, 612, 660, 680, 738, 803 (12 bare of 16); cookbooks/multi_level_model.py 99, 354; features/model_comparison.py 124, 206, 289; features/search_chaining.py 157, 232, 305, 497, 533 (prior-passing is the best figure case: show the passed priors as the same map with a changed legend); features/search_grid_search.py 129; searches/start_point.py 125, 148; overview/overview_1_the_basics.py 222, 235, 549 (global_prior_model), 718; cookbooks/result.py 406 (loop exception). cookbooks/model.py:814-816 already carries the once-per-script wrap-up note about model_figure: true → keep it single.

HowToFit sites: chapter_1_introduction/tutorial_1_models.py 234, 381, 394, 486 (highest priority — the composition tutorial; teach the map/legend idea here in full tutorial register); tutorial_3_non_linear_search.py 265; tutorial_4_why_modeling_is_hard.py 309, 551, 661, 757 (x5 Gaussian: plate 5 components); tutorial_7_the_details.py 271; tutorial_optional_bayesian_formalism.py 271; chapter_3_graphical_models/tutorial_3_graphical_benefits.py 352 (global_prior_model); tutorial_5_expectation_propagation.py 221 (global_prior_model, plus EP below).

EP rollout (the phase-5 flag)

  • autofit_workspace/scripts/features/expectation_propagation.py: at the factor_graph composition (before the low-level dissection) add print(factor_graph.global_prior_model.info) + af.ModelPlotter(factor_graph.global_prior_model).figure() (the structure map; the factor-graph view itself needs the swept graph, so it comes after the fit). In the factor_graph.optimise(...) call (:303-309) add visualise_interval=1 (flows through **kwargs to EPOptimiser.run, default 100, so with max_steps=5 the state PNG otherwise refreshes once). After the call add prose + af.EPPlotter(factor_graph_result.factor_graph, ep_history=factor_graph_result.ep_history).figure(kind="state"), reading the figure: factor status, update age, confirmed reverted updates; then extend the existing __Output Folder__ prose with graph_model.png / graph_state.png (written per tick when config/output.yaml model_figure: true).
  • HowToFit/scripts/chapter_3_graphical_models/tutorial_5_expectation_propagation.py: figure beside :221, visualise_interval=1 on :263-264, EPPlotter state figure + tutorial-register prose after the fit, output-folder bullets for the two PNGs. Leave tutorial_optional_hierarchical_ep.py alone (its optimise is commented out by design).
  • HowToFit/config/output.yaml: add model_figure: false with the same two-line comment as autofit_workspace/config/output.yaml:106-108 — today the key is absent there, so the prose pointer would be false.
  • Under CI the LaplaceOptimiser EP loop runs real sweeps even in PYAUTO_TEST_MODE (not a NonLinearSearch), and EPState.from_history renders an empty history as absent rather than raising — so the inline state figure is safe in smoke; verify by the headless runs below.

Notebooks + catalogue

Per repo convention (both AGENTS.md): from the repo root, PYTHONPATH=../PyAutoHands/autohands python3 ../PyAutoHands/autohands/generate.py autofit / ... howtofit (the worktree's activate.sh sets cwd/paths). Commit only the touched scripts' .ipynb twins plus llms-full.txt / workspace_index.json if the flow rewrites them; never sweep in other scripts' pre-existing drift. HowToFit: confirm tutorial_8_scientific_workflow.ipynb still has zero code cells.

Verification

  • PyAutoFit: new test + full pytest test_autofit/ in the worktree; black --check on touched files; sphinx build warning count == 30.
  • Every touched script runs headless from its repo root under the smoke env profile (PYAUTO_TEST_MODE=1 PYAUTO_SKIP_FIT_OUTPUT=1 PYAUTO_SKIP_VISUALIZATION=1 PYAUTO_SKIP_CHECKS=1, MPLBACKEND=Agg, MPLCONFIGDIR=/tmp/matplotlib, NUMBA_CACHE_DIR=/tmp/numba_cache, PYTHONPATH pointing at the worktree PyAutoFit so the new EPResult.factor_graph resolves; PYAUTO_SKIP_API_GATE=1 for the branch-only symbol) — exit 0, no traceback. HowToFit smoke runs every tutorial; autofit_workspace smoke allowlist covers overview_1, cookbooks/{model,result,samples,latent_variables} and searches/* — run the EP and features scripts locally anyway.
  • pyauto-heart smoke grades against the canonical checkout, so the EP scripts must be run headless against the worktree PyAutoFit explicitly (phase-2 trap).
  • Notebooks regenerated; navigator catalogue current; git status shows only intended twins.
  • Ship order: ship_library (PyAutoFit PR) → ship_workspace (autofit_workspace PR, HowToFit PR; both bodies link the library PR; library-first gate) → /prm over the three, then Mind record + follow-up: the rollout map's next cut is (b) PyAutoLens surfaces.

Original Prompt

Click to expand starting prompt
# Model figures phase 6a — PyAutoFit surfaces (autofit_workspace + HowToFit, incl. the EP state figure)

Type: feature
Target: workspaces
Repos:
- PyAutoFit
- autofit_workspace
- HowToFit
Themes:
- visualization
- notebooks
Difficulty: medium
Autonomy: supervised
Priority: normal
Status: formalised
Consequence: judge
Review-minutes: 30
Unattended: ready
Epic: model-figures
Phase: 6a
Filed: 2026-09-13

This is sub-task **(a)** cut from the rollout map
`draft/feature/workspaces/model_figures_6_rollout.md`, which orders one sub-task at a
time and never bundles two repos' rollouts into one PR. The map's bullet, verbatim:
"**(a) PyAutoFit surfaces** — `autofit_workspace` cookbooks and `features/`, the
`howtofit` scripts and the HowToFit chapters. The graphical-models chapter waits for
phase 4." Phase 4 has shipped, so that chapter is in scope here.

## Context

The `model-figures` epic (phases 1-5 shipped, phase 5 merged as PyAutoFit#1619) gives
every model a figure: `af.ModelPlotter(model).figure()` for the structure, and for
expectation propagation `af.EPPlotter(...)` plus per-run `graph_model.png` /
`graph_state.png`. Census (read-only, 2026-09-13): `autofit_workspace/scripts/` has
**28 bare** `print(<model>.info)` sites across 9 scripts; `HowToFit/scripts/` has **14
bare** sites across 8 tutorials. Neither EP script draws a figure.

**API gap found:** `FactorGraphModel.optimise(...)`
(`PyAutoFit/autofit/graphical/declarative/abstract.py:172-214`) builds the swept graph
inside `_make_ep_optimiser` and returns an `EPResult` (`declarative/result.py:57`)
carrying only `ep_history`, `declarative_factor`, `updated_ep_mean_field`.
`EPPlotter(kind="state")` must receive **the optimiser's own factor graph** (history is
keyed by its factor objects; `FactorGraphModel.graph` rebuilds and renames on every
access — `docs/features/graphical.md:288-292` says so). So a workspace script cannot
draw the state figure after the high-level `optimise` without a private API. The fix
belongs in the library, once: `EPResult.factor_graph` (additive, default `None`), passed
through from `abstract.py:210-214`, with a test and a `docs/features/graphical.md`
"Seeing the EP run" high-level form.

## Coverage rule

Acceptance is a grep: a figure call beside every `model.info` occurrence. Add one at
**every** bare site, with these exceptions recorded in the completion record:

- `autofit_workspace/scripts/cookbooks/result.py:406` — indented `print(model.info)`
  inside the aggregator loop: one figure **after** the loop for the last loaded model,
  prose says the three are identical in structure.
- `result*.info` / `samples.info` lines are not model prints (not counted).
- Where a print re-shows an **unchanged structure** after a prior/value-only edit (e.g.
  `cookbooks/model.py:109`), the figure still goes in, prose one line: "the map is
  unchanged; only the legend moved" — that contrast is the teaching point.

Pattern (verbatim from phases 2/4): `print(x.info)` → blank line → `"""` prose `"""` →
`af.ModelPlotter(x).figure()`. The prose opens with the fixed map/legend sentence the
first time it appears in a script, then one short paragraph on what the figure shows
that the info cannot. Default `figure()` settings. No `try/except` in scripts; a guard
belongs in the library.

## EP rollout (the phase-5 flag)

- `autofit_workspace/scripts/features/expectation_propagation.py`: at the `factor_graph`
  composition add `print(factor_graph.global_prior_model.info)` +
  `af.ModelPlotter(factor_graph.global_prior_model).figure()`. Add `visualise_interval=1`
  to the `factor_graph.optimise(...)` call (`:303-309`; default 100, so with
  `max_steps=5` the state PNG otherwise refreshes once). After the call add prose +
  `af.EPPlotter(factor_graph_result.factor_graph,
  ep_history=factor_graph_result.ep_history).figure(kind="state")`, reading the figure
  (factor status, update age, confirmed reverted updates); extend the `__Output Folder__`
  prose with `graph_model.png` / `graph_state.png`.
- `HowToFit/scripts/chapter_3_graphical_models/tutorial_5_expectation_propagation.py`:
  figure beside `:221`, `visualise_interval=1` on `:263-264`, EPPlotter state figure +
  tutorial-register prose after the fit, output-folder bullets for the two PNGs. Leave
  `tutorial_optional_hierarchical_ep.py` alone (its optimise is commented out by design).
- `HowToFit/config/output.yaml`: add `model_figure: false` with the same two-line comment
  as `autofit_workspace/config/output.yaml:106-108` — today the key is absent there, so
  the prose pointer would be false.
- Under CI the `LaplaceOptimiser` EP loop runs real sweeps even in `PYAUTO_TEST_MODE`, and
  `EPState.from_history` renders an empty history as `absent` rather than raising — so the
  inline state figure is safe in smoke.

## Notebooks + catalogue

Per repo convention (both AGENTS.md): from the repo root,
`PYTHONPATH=../PyAutoHands/autohands python3 ../PyAutoHands/autohands/generate.py autofit`
/ `... howtofit`. Commit **only** the touched scripts' `.ipynb` twins plus
`llms-full.txt` / `workspace_index.json` if the flow rewrites them; never sweep in other
scripts' pre-existing drift. HowToFit: confirm `tutorial_8_scientific_workflow.ipynb`
still has zero code cells.

## Verification

- PyAutoFit: new test + full `pytest test_autofit/`; `black --check` on touched files;
  sphinx build warning count == 30.
- Every touched script runs headless from its repo root under the smoke env profile
  (`PYAUTO_TEST_MODE=1 PYAUTO_SKIP_FIT_OUTPUT=1 PYAUTO_SKIP_VISUALIZATION=1
  PYAUTO_SKIP_CHECKS=1`, `MPLBACKEND=Agg`, `MPLCONFIGDIR=/tmp/matplotlib`,
  `NUMBA_CACHE_DIR=/tmp/numba_cache`, `PYTHONPATH` pointing at the worktree PyAutoFit so
  the new `EPResult.factor_graph` resolves; `PYAUTO_SKIP_API_GATE=1` for the branch-only
  symbol) — exit 0, no traceback. HowToFit smoke runs *every* tutorial;
  autofit_workspace's allowlist is narrower, so run the EP and `features/` scripts
  locally anyway. `pyauto-heart smoke` grades against the canonical checkout, so run the
  EP scripts against the worktree PyAutoFit explicitly (phase-2 trap).
- Notebooks regenerated; navigator catalogue current; `git status` shows only intended
  twins.
- Ship order: `ship_library` (PyAutoFit PR) → `ship_workspace` (autofit_workspace PR,
  HowToFit PR; both bodies link the library PR; library-first gate) → `/prm` over the
  three, then Mind record + follow-up: the map's next cut is **(b) PyAutoLens surfaces**.

(PyAutoMind/draft/feature/workspaces/model_figures_6a_autofit_surfaces.md, moved to active/ on issue.)

Activity

  1. Jammy2211 commented on Sep 13, 2026

    @Jammy2211
    CollaboratorAuthor

    Library PR Created

    #1621

    API changes (additive only):

    • Added EPResult.factor_graph — the DeclarativeFactorGraph the EP optimiser actually swept, so af.EPPlotter(result.factor_graph, ep_history=result.ep_history).figure(kind="state") works after a high-level factor_graph.optimise(...) run.
    • EPResult.__init__ gains a trailing keyword factor_graph=None; AbstractDeclarativeFactor.optimise() populates it from opt.factor_graph.
    • Nothing removed, renamed or made required — no migration needed; docs/features/graphical.md ("Seeing the EP run") documents the new form.

    Opened under Heart RED release validation FAILED (stage integrate) with human authorisation 2026-09-13; merge stays human via /prm.

    Next: /start_workspace (autofit_workspace + HowToFit on the same-named branch, in flight).

  2. Jammy2211 commented on Sep 13, 2026

    @Jammy2211
    CollaboratorAuthor

    Workspace PRs Created

    Smoke, run locally from each repo root under the smoke env profile: autofit_workspace 8 scripts + 2 notebooks PASS (exit 0); HowToFit 18 scripts PASS (exit 0).

    All three PRs were opened under Heart RED release validation FAILED (stage integrate) (unrelated autolens integrate scripts) with explicit human authorisation on 2026-09-13. Merge stays human.

    Next: /prm over PyAutoFit#1621 → autofit_workspace#154 → HowToFit#52 once CI is green.

    Pre-existing defect found while writing tutorial 5's prose, out of scope here and to be filed as its own prompt: scripts/chapter_3_graphical_models/tutorial_5_expectation_propagation.py creates centre_shared_prior but never assigns it to any model, so despite the prose no centre is actually shared across the datasets. The new figure prose describes only what the map actually shows.

  3. added a commit that references this issue on Sep 13, 2026
  4. Jammy2211 commented on Sep 13, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped

    Phase 6a of the model-figures epic — figures beside every model.info across the AutoFit surfaces.

    PRs merged

    What shipped

    • Library (PyAutoFit) — EPResult.factor_graph, so an EP result can hand its factor graph straight to the plotter (the surface the EP state figure needs).
    • autofit_workspace — 31 ModelPlotter call sites + 1 EPPlotter across 9 scripts; notebooks regenerated.
    • HowToFit — 13 ModelPlotter + 1 EPPlotter across 7 tutorials, plus the model_figure config key; notebooks regenerated.

    CI — PyAutoFit: Docs (docs-build) + Tests (nojax, 3.12, 3.13) green. autofit_workspace: Navigator Check (3 jobs) + Smoke Tests (changes, 3.12, 3.13) green. HowToFit: Navigator Check (3) + Tutorials Complete + Smoke Tests (3) green.

    Pending release — PyAutoFit#1621 is merged but unpublished; the EPResult.factor_graph surface reaches users at the next PyAutoFit release.

    Follow-ups

    • Bug prompt filed: draft/bug/howtofit/tutorial_5_ep_shared_centre_never_assigned.md.
    • Rollout map next cut is (b) PyAutoLens surfaces (draft/feature/workspaces/model_figures_6_rollout.md).
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