Skip to content

docs: fix HowToFit tutorials 1-3 data figure, math rendering and MLE init - #58

Merged
Jammy2211 merged 2 commits into
mainfrom
feature/howtofit-tutorials-1-3
Sep 14, 2026
Merged

Jammy2211 merged 2 commits into
mainfrom
feature/howtofit-tutorials-1-3

Conversation

@Jammy2211

@Jammy2211 Jammy2211 commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Closes #57.

Reader-reported fixes to chapter 1, tutorials 1-3.

Stacked on #56 — merge #56 first. #56 (feature/model-figure-prose-simplify) rewrites the same tutorial 1 model-figure paragraphs, so this branch is cut from it and this PR is based on it, showing only this task's diff. GitHub retargets it to main once #56 merges. Where the two disagree — the "identical composition" paragraph — the reader's wording here is the newer instruction and wins.

Scripts Changed

  • scripts/chapter_1_introduction/tutorial_1_models.py
  • scripts/chapter_1_introduction/tutorial_2_fitting_data.py
  • scripts/chapter_1_introduction/tutorial_3_non_linear_search.py
  • notebooks/chapter_1_introduction/*.ipynb — regenerated, never hand-edited

Tutorial 1

  • The data now comes before the equation. The Model Parameterization section opened on the Gaussian equation with no picture of what it describes. It now opens with the gaussian_x1 errorbar figure, then the equation, then the Where: list — the order the RTD python_api page uses. Loaded with the same auto-simulation subprocess guard tutorials 2 and 3 already use, so nothing has to be simulated by hand.
  • The reader's wording replaces the model-figure "identical composition" paragraph, verbatim.
  • Extensibility now shows the thing it claims. The section argued that Model and Collection make models easy to extend and then stopped. It now composes x2 Gaussian + x4 Exponential — 18 free parameters in six lines — and displays model.info and the figure, and nothing else.
  • Parameter names in the prose match the classes; af.Model, Model, Gaussian, paths and model_data_from are backticked where they are code; both equations said I where the text said N; ratw -> rate, the Exponential.model_data_from docstring said "Returns a 1D Gaussian", the sigma (Exponential) print label printed rate, and the "download a HowToFit clone" link pointed at autofit_workspace.

Tutorials 1-3: math that actually renders

Display math used \[ ... \] and inline math \( ... \). Neither is a default MathJax delimiter in Jupyter, JupyterLab or Colab, so the reader saw raw LaTeX — this is what they reported. All of it is now $$ ... $$ and $ ... $. The \text{} / \frac{} bodies and the \_ escapes are unchanged.

Tutorial 3: the MLE fit was broken, not badly initialised

The reader asked for a closer starting point because the MLE fit "went wrong". The starting point was not the cause.

af.InitializerParamStartPoints was keyed on the priors of one af.Model(Gaussian), and then a freshly constructed af.Model(Gaussian) was passed to search.fit. The keys matched nothing in the model being fitted, PyAutoFit logged Range for centre/normalization/sigma not set ... Using defaults and started from random draws. Measured under the old wiring with the old 55 / 20 / 8 values: centre=97.14, normalization=521060, sigma=14.27, logL=-9.9e11.

The model is now created before the initializer and that same object is fitted. The identical bug in the MCMC InitializerParamBounds block is fixed too (its deliberate sigma omission is preserved, and still logs the one expected warning).

Start points move to centre=52.0, normalization=23.0, sigma=9.0 against a truth of 50 / 25 / 10 — close enough to climb reliably, still visibly wrong, so the section's teaching point stands.

value
truth 50.0 / 25.0 / 10.0
recovered 49.889 / 25.147 / 9.847, log likelihood 176.83
repeatability identical over 6 runs
margin 45/20/7 and the old 55/20/8 also reach this optimum once the wiring is fixed

The first, deliberately uninitialised MLE fit still fails (50.16 / 5.81 / 12.31, logL -3416), which is what that part of the tutorial teaches.

Also: two MLE plots and one Emcee plot were titled "Dynesty model fit".

The reader's ModuleNotFoundError: No module named 'emcee'

Not a HowToFit bug and not fixed here — see PyAutoLabs/PyAutoNerves#164. emcee and dynesty are real autofit dependencies, but the Colab bootstrap installs the stack with --no-deps and does not name them, so every Colab run of this tutorial dies at the af.Emcee fit. Fixed in PyAutoNerves; it reaches users on the next autonerves release.

Testing

  • python .github/scripts/run_smoke.py — exit 0, 18 run, 18 PASS, 0 FAIL (23 found, 5 excluded by config/build/no_run.yaml).
  • Tutorial 3 also run in full outside test mode (exit 0).
  • black --check clean on all three scripts.
  • Notebooks + catalogue regenerated with generate.py howtofit (19 scripts). tutorial_8_scientific_workflow.ipynb still has 0 code cells, as its prose-only design requires.

Heart

Verdict YELLOW, no RED, acknowledged by the human at launch for this exact reason set: workspace validation not passing (3 failed, cloud#34824535982 — autofit overview_2_scientific_workflow .ipynb/.py, autolens imaging/slam.ipynb); profiling drift in matrix_free_delaunay_hpc_a100_fp64, matrix_free_delaunay_nn_hpc_a100_fp64, matrix_free_rectangular_hpc_a100_fp64, mge_likelihood_summary_hst, mge_mass_jax_likelihood_summary_hst, +1 more. STALE: release validation incomplete (no rehearsal for current source). None touch HowToFit.

Out of scope (follow-ups)

  • Tutorials 6, 7 and tutorial_optional_bayesian_formalism.py carry the same unrendered \[ \] / \( \) blocks; the reader scoped this to tutorials 1-3.
  • Tutorial 3's __Priors__ section is dead code: the af.UniformPrior overrides are set on a model every later fit discards, and the normalization override (0, 10) excludes the truth 25 — the fits only work because they fall back to the config LogUniformPrior. Needs its own prompt.
  • Tutorial 1's two equations still use \begin{equation*}, which Jupyter does render; converting them to $$ would make chapter 1 internally consistent.

🤖 Generated with Claude Code

https://claude.ai/code/session_0132v5TNVbbvBBESAefHwnwG

…init

Tutorial 1 opens the Model Parameterization section with the 1D Gaussian
dataset it describes — the errorbar figure, then the equation, then the
`Where:` list, matching the RTD `python_api` page — loaded with the same
auto-simulation guard tutorials 2 and 3 use. The model-figure "identical
composition" paragraph takes the reader's wording, and the Extensibility
section now closes with a worked 18-parameter model (x2 `Gaussian` +
x4 `Exponential`) showing `model.info` and the figure and nothing else.

Tutorials 1-3 move display and inline math off `\[ \]` / `\( \)`, which
Jupyter and Colab do not typeset, onto `$$ $$` / `$ $`, which they do.

Tutorial 3's MLE initialisation was wired to a model that was thrown away:
`af.InitializerParamStartPoints` was keyed on one `af.Model(Gaussian)` and
`search.fit` was then handed a freshly constructed one, so the keys matched
nothing, PyAutoFit fell back to random draws, and the fit the prose promises
would succeed did not. The model is now built before the initializer and the
same object is fitted; the same bug is fixed in the MCMC
`InitializerParamBounds` block. Start points move to 52 / 23 / 9 against a
truth of 50 / 25 / 10, recovering 49.889 / 25.147 / 9.847 at log likelihood
176.83, deterministic over six runs. The first, deliberately uninitialised
MLE fit still fails, as the tutorial teaches.

Also: parameter names in the prose now match the classes (`centre`,
`normalization`, `sigma`, `rate`), `af.Model` / `Model` / `Gaussian` / `paths`
are backticked where they are code, the MLE and Emcee plot titles no longer
say Dynesty, and ~20 typos are fixed.

Closes #57

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0132v5TNVbbvBBESAefHwnwG
6f335b2 fixed the __Paths__ clone URL to point at HowToFit instead of
autofit_workspace, in scripts/ and notebooks/. markdown/ was left behind:
tutorial_1_models.md:104 still read PyAutoLabs/autofit_workspace, so the
published mirror still sent a reader who wants HowToFit to the wrong repo.

The cause is not this branch's: markdown/ is built by generate_markdown.py
over config/build/markdown_examples.yaml, a different tool from the
generate.py that builds notebooks/. Following the documented regeneration
step rebuilds notebooks/ and silently misses markdown/. The same gap was hit
twice on 2026-09-14 in HowToGalaxy and HowToLens (HowToGalaxy#75).

Regenerating also brings this page up to date with script changes that were
never rendered, which is most of the diff.

Landed here rather than as its own PR because on main both the .py and the
.md are still stale: a branch off main would duplicate this one's fix and
conflict with it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014ujQLXo85GNH7tG5eaNJ65
@Jammy2211
Jammy2211 merged commit 12ee3e1 into main Sep 14, 2026
7 checks passed
@Jammy2211
Jammy2211 deleted the feature/howtofit-tutorials-1-3 branch September 14, 2026 22:49
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.

docs: tutorials 1-3 — data figure, math rendering, MLE start point

1 participant