Skip to content

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

Closed

Description

@Jammy2211

Overview

Reader-reported fixes to HowToFit chapter 1 tutorials 1-3: tutorial 1 opens on the
model equation with no picture of the data it describes, display/inline math in
tutorials 2 and 3 uses \[ \] / \( \) delimiters that Jupyter and Colab do not
typeset, and tutorial 3's MLE initialisation starts far enough from the truth that
LBFGS does not reliably converge. The reader also hit ModuleNotFoundError: No module named 'emcee' on Colab, whose root cause is the --no-deps Colab bootstrap in
PyAutoNerves, not HowToFit.

Plan

  • Tutorial 1 — show the 1D Gaussian dataset (errorbar figure, as the RTD
    python_api page does) before the Gaussian equation and its Where: list, using
    the same auto-simulation guard tutorials 2 and 3 already use.
  • Tutorial 1 — replace the model-figure "identical composition" paragraph with the
    reader's wording, and close the Extensibility section with a worked 18-parameter
    model (x2 Gaussian + x4 Exponential) showing only model.info and the figure.
  • Tutorial 1 — backtick the bare af.Model / Model / Gaussian code references
    and sweep the rest of chapter 1 for the same miss; fix the typos found in the audit.
  • Tutorials 1-3 — convert \[ ... \] to $$ ... $$ and \( ... \) to $ ... $ so
    the equations render in Jupyter, Colab and nbviewer.
  • Tutorial 3 — move the MLE InitializerParamStartPoints closer to the truth
    (50 / 25 / 10) so the fit converges reliably while still being a visibly imperfect
    starting guess.
  • PyAutoNerves — add emcee and dynesty to the Colab _SHARED_EXTRAS install
    list so --no-deps stops silently dropping two real autofit dependencies.
  • Regenerate notebooks + navigator catalogue and run the smoke suite.
Detailed implementation plan

Affected Repositories

  • HowToFit (primary)
  • PyAutoNerves

Branch Survey

Repository Current Branch Dirty?
./HowToFit main (behind origin) clean
./PyAutoNerves main clean

Suggested branch: feature/howtofit-tutorials-1-3

Branch-claim waiver. worktree_check_conflict exits 1 on HowToFit with two
claims: howtofit-mode (scope there is one uncommitted README.md, no commits
ahead — disjoint) and model-figure-prose-simplify (PR #56, open, all checks
green
, which rewrites the same tutorial 1 model-figure paragraphs). The overlap
with #56 is real, so this task stacks on #56 rather than racing it: the branch
is cut from origin/feature/model-figure-prose-simplify and the PR is opened
against that base, so the diff shows only this task's changes and GitHub retargets
it to main once #56 merges. Merge #56 first. Where the two disagree — the
"identical composition" paragraph — the reader's wording in this issue is the newer
instruction and wins.

Implementation Steps

  1. scripts/chapter_1_introduction/tutorial_1_models.py — in Model
    Parameterization
    , add the dataset figure ahead of the equation: copy the
    dataset_path = path.join("dataset", "example_1d", "gaussian_x1") +
    auto-simulation subprocess guard + af.util.numpy_array_from_json loads +
    plt.errorbar block from tutorial_3_non_linear_search.py (lines 157-197), with
    a short lead-in sentence. Requires from os import path at the tutorial head.
    Keep the existing equation and Where: list immediately after it.
  2. Same file — fix the equation typos: g(x, I, \sigma) -> g(x, N, \sigma) and
    g(x, I, \lambda) -> g(x, N, \lambda); the "three parameters" line naming
    (x, N, \sigma) -> (centre, N, \sigma); ratw -> rate and "has fast" ->
    "how fast" in the Exponential docstring; Exponential.model_data_from's
    docstring "Returns a 1D Gaussian" -> "1D Exponential"; the
    print("sigma (Exponential) = ", instance.exponential.rate) label -> rate; the
    "If you don't have a HowToFit clone" URL -> the HowToFit repo; the stray double
    blank line in the Where: list.
  3. Same file — backtick bare code references: af.Model (Model Mapping intro),
    Model object (Model Creation), Gaussian class. Sweep the other chapter 1
    scripts for the same miss (log_likelihood_function in
    tutorial_4_why_modeling_is_hard.py, Samples / Result in
    tutorial_5_results_and_samples.py) and fix only unambiguous code references —
    ordinary English uses of "Gaussian" stay plain.
  4. Same file — replace the model-figure "identical composition" paragraph with the
    reader's two paragraphs, verbatim.
  5. Same file — after "These objects serve numerous other essential purposes...",
    add the worked extensibility example: an af.Collection of x2 Gaussian +
    x4 Exponential (18 free parameters), then print(model.total_free_parameters)
    is not wanted — only print(model.info) and af.ModelPlotter(model).figure().
  6. tutorial_2_fitting_data.py — convert the display math at the normalized-residual,
    chi-squared (x2), noise-normalization and log-likelihood blocks from \[ \] to
    $$ $$. Check the surrounding docstrings are raw (r""") where backslashes need
    to survive.
  7. tutorial_3_non_linear_search.py — same conversion in the Parameter Space
    section, including the inline \( x \) forms -> $x$; also fix the parameter
    naming (x, I, \sigma) there.
  8. tutorial_3_non_linear_search.py — move InitializerParamStartPoints from
    centre=55.0, normalization=20.0, sigma=8.0 to a closer but still visibly wrong
    guess against the truth 50.0 / 25.0 / 10.0; verify by running the tutorial that
    the fit converges (max log likelihood instance within a few percent of truth).
  9. PyAutoNerves/autonerves/setup_colab.py — add "emcee>=3.1.6" and
    "dynesty==2.1.5" to _SHARED_EXTRAS (matching PyAutoFit/pyproject.toml),
    with a comment saying why a --no-deps install has to name them. Check
    PyAutoNerves tests for any assertion over that list.
  10. Regenerate notebooks + catalogue from the HowToFit repo root:
    PYTHONPATH=../PyAutoHands/autohands python3 ../PyAutoHands/autohands/generate.py howtofit,
    then run python .github/scripts/run_smoke.py until green.

Key Files

  • HowToFit/scripts/chapter_1_introduction/tutorial_1_models.py — data figure, worked 18-parameter example, prose replacement, typos
  • HowToFit/scripts/chapter_1_introduction/tutorial_2_fitting_data.py — math delimiters
  • HowToFit/scripts/chapter_1_introduction/tutorial_3_non_linear_search.py — math delimiters, MLE start point
  • HowToFit/notebooks/chapter_1_introduction/*.ipynb — regenerated, never hand-edited
  • PyAutoNerves/autonerves/setup_colab.py — _SHARED_EXTRAS Colab install list

Out of scope (follow-up)

Tutorials 6, 7 and tutorial_optional_bayesian_formalism.py carry the same
unrendered \[ \] / \( \) blocks; the reader scoped this task to tutorials 1-3.

Original Prompt

Click to expand starting prompt
# HowToFit tutorials 1-3: data figure, math rendering, MLE start point

Type: docs
Target: HowToFit
Repos:
- HowToFit
- PyAutoNerves
Themes:
- howtofit
- notebooks
- rendering
- colab
Difficulty: medium
Autonomy: safe
Priority: normal
Status: formalised
Consequence: judge
Review-minutes: 20
Unattended: ready
Filed: 2026-09-14

## User request (verbatim)

Fixes to HowToFit tutorials 1 through 3, auto all the way to PR without asking me HowToFit:

Tutorial 1

show image of 1D Gaussian data (from the RTD example) with brief explanatory text after:
"Where:

x: The x-axis coordinate where the Gaussian is evaluated.

N: The overall normalization of the Gaussian.

\sigma: The size of the Gaussian (Full Width Half Maximum,  FWHM , is  22ln2−−−−√σ )."

An af.Model did not have `` around it to look like code, fix that and make sure its ok through HowTo.

Replace:

same issue here albeit it is less technical:
The figure is identical to the previous one too, and that is the point worth taking from this example. The two ways of writing the composition look different on the page, but they build the same structure, and structure is all the map draws. Whenever you change how a model is written, draw it: if the map does not move then nothing about the model moved either, and any difference between the two fits has to be hiding in the legend rather than in the model.

With:

The figure is identical to the previous example, because both ways of writing the model produce the same model structure.

This is a useful check when composing models in different ways: if the figure looks the same, the underlying structure of the model is the same too. Any differences between the fits must therefore come from details such as the parameter priors or fixed values, rather than how the model is composed.

Can you end tutorial 1 here:

Extensibility

It should now be clear why we use Model and Collection objects to construct our model.

These objects facilitate the straightforward extension of our models to include multiple components and parameters. For instance, we can add more Gaussian and Exponential components to the Collection, or define new Python classes to represent entirely new model components with additional parameters.

These objects serve numerous other essential purposes that we will explore in subsequent tutorials.

With an actual example which uses the API, Gaussian and Exponential to compose an 18 parameter model of x2 Gaussian
and x4 Expoentnial, displaying its model.info and image but nothing else.

Also look for typos

Tutorial 2:

Make sure stuff like this displays correctly in jupyter:

[ \text{normalized_residual} = \frac{\text{residual_map}}{\text{noise_map}} = \frac{\text{data} - \text{model_data}}{\text{noise_map}} ]

Also look for typos

Tutorial 3:

Again certain equations especially in the parameter space bit are not math formatted.

The MLE fit went wrong for me can you make the starting point closer so it always work, but not so close its
unfair.

    result = search.fit(model=model, analysis=analysis)

    ModuleNotFoundError: No module named 'emcee'
    (traceback through autofit/non_linear/search/mcmc/emcee/search.py line 135, Colab,
     /usr/local/lib/python3.13/dist-packages/autofit)

## Audit (session findings, 2026-09-14)

**Tutorial 1** (`scripts/chapter_1_introduction/tutorial_1_models.py`)

- No data figure exists. The RTD original is
  `PyAutoFit/docs/overview/python_api.md` lines 48-68: the `data.png` errorbar
  figure, then the `g(x, I, \sigma)` equation, then the `Where:` list. The
  tutorial has the equation + `Where:` list (lines 101-112) but no figure.
  The dataset is `dataset/example_1d/gaussian_x1` (centre 50.0,
  normalization 25.0, sigma 10.0); tutorials 2 and 3 already load it with an
  auto-simulation `subprocess` guard that tutorial 1 can copy verbatim.
- Line 261 `af.Model` is bare prose; line 212 `a Model object` and line 204
  `the Gaussian class` are the same class of miss. A sweep of every
  module-level docstring in `scripts/` finds only two `af.`-prefixed misses
  (t1:261 and `log_likelihood_function` at
  `tutorial_4_why_modeling_is_hard.py:169`), plus a handful of bare class
  names in chapter 1.
- Typos: the Gaussian equation writes `g(x, I, \sigma)` but the parameter is
  named `N` below it (same for the Exponential's `g(x, I, \lambda)`);
  line 118 calls the parameters `(x, N, \sigma)` when the class names them
  `centre, normalization, sigma`; line 367 `ratw` -> `rate`; line 368 `has
  fast` -> `how fast`; line 376 the `Exponential.model_data_from` docstring
  says "Returns a 1D Gaussian"; line 461 prints `sigma (Exponential)` for
  `instance.exponential.rate`; line 93 points a HowToFit clone at
  `autofit_workspace`; line 110/111 carry a stray double blank line.

**Tutorial 2** (`tutorial_2_fitting_data.py`) — display math at lines 297,
323, 344, 365-367 and 387 uses `\[ ... \]`, which Jupyter/Colab's markdown
renderer does not typeset (only `$...$` / `$$...$$` are default delimiters).
Confirmed unrendered in `notebooks/.../tutorial_2_fitting_data.ipynb` cell 22.

**Tutorial 3** (`tutorial_3_non_linear_search.py`) — the Parameter Space
section (lines 52-88) uses both `\[ ... \]` display math and `\( ... \)`
inline math; neither renders. The MLE `InitializerParamStartPoints` at lines
508-514 starts at `centre=55.0, normalization=20.0, sigma=8.0` against a truth
of `50.0 / 25.0 / 10.0` — far enough that LBFGS does not reliably converge.

**Root cause of the `emcee` traceback** — not a HowToFit bug. `emcee>=3.1.6`
and `dynesty==2.1.5` are real `dependencies` of autofit
(`PyAutoFit/pyproject.toml` lines 42 and 44), but the Colab bootstrap
`PyAutoNerves/autonerves/setup_colab.py` installs the stack with `--no-deps`
(line ~270) and neither package is listed in `_SHARED_EXTRAS`. Every Colab
run of tutorial 3 therefore dies at the `af.Emcee` fit, and would die again at
`af.DynestyStatic`. Fix belongs in `_SHARED_EXTRAS`; it reaches users on the
next `autonerves` release.

## Scope

Math-delimiter conversion is scoped to tutorials 1-3 as requested. Tutorials 6,
7 and `tutorial_optional_bayesian_formalism.py` carry the same unrendered
`\[ \]` / `\( \)` blocks and need a follow-up sweep.

Activity

  1. Jammy2211 commented on Sep 14, 2026

    @Jammy2211
    ContributorAuthor

    Shipped

    Merged in #58 (tutorials 1-3) with the Colab half in PyAutoLabs/PyAutoNerves#164.

    • Tutorial 1 — the 1D Gaussian dataset figure now opens Model Parameterization (figure → equation → Where: list, as the RTD python_api page has it); the reader's replacement wording for the model-figure paragraph, verbatim; Extensibility closes with a worked 18-parameter model (x2 Gaussian + x4 Exponential) showing model.info and the figure only; code references backticked; ~10 typos.
    • Tutorials 1-3 — display and inline math moved off \[ \] / \( \) onto $$ $$ / $ $, which Jupyter and Colab actually typeset.
    • Tutorial 3 — the MLE fit was broken, not badly initialised: InitializerParamStartPoints was keyed on one af.Model(Gaussian) while a freshly built one was passed to search.fit, so no key matched and PyAutoFit fell back to random draws. Fixed in both the LBFGS and Emcee blocks; start points 52/23/9 recover 49.889 / 25.147 / 9.847.
    • The reported ModuleNotFoundError: No module named 'emcee' was not a HowToFit bug — the Colab bootstrap installs with --no-deps and never named emcee/dynesty, both real autofit dependencies. Fixed in PyAutoNerves#164; it reaches Colab on the next autonerves release.

    Smoke 18/18, CI green on all 7 checks.

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