Skip to content

docs: document the fit in plot_pull and plot_ratio - #772

Open
lyn1dev wants to merge 1 commit into
scikit-hep:mainfrom
lyn1dev:docs/plot-pull-fit
Open

lyn1dev wants to merge 1 commit into
scikit-hep:mainfrom
lyn1dev:docs/plot-pull-fit

Conversation

@lyn1dev

@lyn1dev lyn1dev commented Nov 22, 2026

Copy link
Copy Markdown

What was wrong

BaseHist.plot_pull (and plot_ratio with a model) documented func and ax_dict only, so a reader could not tell what happens behind the fit that the pull subplot is built from: which fitter runs, where the initial parameter values come from, how fit results can be read, or which keyword arguments are accepted. This is what #207 asks for, and its example (``h.plot_pull(pdf, ...)` with no further explanation) is still the only one.

What this changes

Documentation only, in src/hist/basehist.py:

  • plot_pull now documents the **kwargs groups (eb_*, fp_*, ub_*, bar_*, pp_*, rp_*), the likelihood and fit_fmt options, the return value of the two artists tuples, and the TypeError/ValueError cases.
  • A Notes section covers:
    • the fitter: scipy.optimize.curve_fit weighted by sqrt(histogram.variances()) as absolute uncertainties, with bins of zero uncertainty left out, or a Poisson likelihood minimised with iminuit.Minuit (seeded from the least-squares fit, covariance from Minuit.hesse) for likelihood=True;
    • initial values: the keyword defaults of the model (lambda x, a=1.0, b=2.0: ... starts at a=1.0, b=2.0), 1 for the parameters of a string expression, and height/mean/width estimated from the histogram for the built-in "gauss"/"gaus"/"normal" models;
    • that the fitted parameters are not returned, with fit_fmt and a direct curve_fit given as the two ways to obtain them;
    • that each pull is (value - model) / uncertainty, with non-finite pulls drawn as zero.
  • A doctest example showing a model with keyword defaults and fit_fmt labelling the parameter values.
  • plot_ratio gets the matching **kwargs/Returns/Notes text, since it shares _plot_ratiolike and the same fit.

No code, tests, or behaviour change: git diff --stat is 1 file changed, 91 insertions(+).

Testing

Every claim in the new text was checked by running the code behind it:

  • The documented example runs as a doctest:

    python -c "import doctest, hist; doctest.run_docstring_examples(hist.BaseHist.plot_pull, {'hist': hist}, name='plot_pull')"
    

    no output, i.e. no failures.

  • Initial values confirmed by wrapping hist.plot._curve_fit_wrapper and printing the p0 it builds: [350.0, 0.0, 1.0] for def model(x, constant=350.0, mean=0.0, sigma=1.0), and [1, 1, 1] for "a * np.exp(-((x - b) ** 2) / (2 * c**2))".

  • likelihood=True runs through iminuit.Minuit; fit_fmt produces the parameter label on the legend (constant = 78.5 $\pm$ 3.16, ...); plot_ratio with a model also works, as does the rp_uncertainty_type="efficiency" / rp_ylim pair named in the text.

  • The documented errors were reproduced: 2D input raises TypeError, an unknown keyword raises ValueError: {'nope'}' not needed, and an ax_dict without pull_ax raises ValueError: All axes should be all given or none at all.

  • Test suites:

    pytest tests/test_plot.py -q            -> 31 passed
    pytest tests -q                        -> 369 passed, 8 skipped
    

    (the 8 skips are missing optional awkward, pandas, and graphed_histogram in this environment)

  • Lint and type checks on the changed file:

    ruff check src/hist/basehist.py        -> All checks passed!
    ruff format --check src/hist/basehist.py -> 1 file already formatted
    numpydoc.validate(...)                 -> no section-order or parse errors
    

    pylint hist reports only the pre-existing scipy.special.gammaln false positive in src/hist/plot.py, and mypy reports the same 4 errors with and without this change (missing pandas stubs, numpy 2.5 instead of the pinned <2.3), so neither is caused by this PR.

  • A full Sphinx build was not run: this machine has neither pandoc nor Graphviz dot, which docs/contributing.md lists as documentation requirements.

Closes #207

AI was used to write this code.

Describe how the model is fitted in `BaseHist.plot_pull` and
`BaseHist.plot_ratio`: `scipy.optimize.curve_fit` weighted by the
histogram's bin uncertainties, or an iminuit Poisson likelihood when
`likelihood=True`.

Also document where the initial values come from (the model's keyword
defaults, or estimated from the histogram for the built-in "gauss"
models), the `fit_fmt` label, that the fitted parameters are not
returned, the prefixed keyword arguments, and the errors raised.

Closes scikit-hep#207

This branch has not been deployed

No deployments
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] add plot_pull documentation

1 participant