From 3eadda376e3633e8f1f830a2544b9ab0102bd3e2 Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Fri, 11 Sep 2026 09:42:21 -0400 Subject: [PATCH] Add model figures to the model cookbooks (PyAutoFit#1613) Draw the model figure (af.ModelPlotter, PyAutoFit#1614) beside model.info in the model, multi_level_model and multiple_datasets cookbooks, regenerate their notebooks, and add the new output.yaml key `model_figure: false` (opt-in model.png per search; absent key means off). Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_0178yLU9v19GtACcRPFggjGa --- config/output.yaml | 6 +- notebooks/cookbooks/model.ipynb | 80 ++++++++++++++++++++- notebooks/cookbooks/multi_level_model.ipynb | 42 ++++++++++- notebooks/cookbooks/multiple_datasets.ipynb | 58 +++++++++++++++ scripts/cookbooks/model.py | 34 +++++++++ scripts/cookbooks/multi_level_model.py | 18 +++++ scripts/cookbooks/multiple_datasets.py | 25 +++++++ 7 files changed, 260 insertions(+), 3 deletions(-) diff --git a/config/output.yaml b/config/output.yaml index 52c88595..84987ae4 100644 --- a/config/output.yaml +++ b/config/output.yaml @@ -101,4 +101,8 @@ noise_map: true # `noise_map.json`: The value of every RMS noise map value. search_log: true # `search.log`: logging produced whilst running the fit method -model_graph: false # `model.graph`: graphical-model factor graph summary. Only meaningful for graphical models (autofit.graphical); empty for ordinary PriorModel/Collection fits, so disabled by default. \ No newline at end of file +model_graph: false # `model.graph`: graphical-model factor graph summary. Only meaningful for graphical models (autofit.graphical); empty for ordinary PriorModel/Collection fits, so disabled by default. + +# `model.png`: the model figure drawn beside `model.info` (af.ModelPlotter). Opt-in until the +# model-figures epic's phase-3 acceptance renders pass. Unlike other keys an absent key means off. +model_figure: false diff --git a/notebooks/cookbooks/model.ipynb b/notebooks/cookbooks/model.ipynb index 2e951247..e61a8caf 100644 --- a/notebooks/cookbooks/model.ipynb +++ b/notebooks/cookbooks/model.ipynb @@ -447,6 +447,28 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can also draw the model, via `af.ModelPlotter`. The figure is the **map** of a model and the `info` above is its \n", + "**legend**: the map shows the structure, meaning which component owns which parameter and which of those parameters \n", + "are fixed, shared, related to one another or constrained, whereas the `info` lists the priors and values themselves.\n", + "\n", + "Customization is where the map earns its place, because linking a parameter to another or adding an assertion changes \n", + "a model's structure and not just its numbers. The `centre` pill states the expression `centre = normalization + sigma` \n", + "that defines it and the assertions are drawn as constraints, which the `info` above does not print at all." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, @@ -555,6 +577,23 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The model figure tags `centre` as `2D`, which is why counting the pills on the map does not reproduce the four free \n", + "parameters its footer counts." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, @@ -950,6 +989,23 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A `Collection` is drawn on the model figure as one card per component, with the fixed values greyed out and any \n", + "assertion attached to the component it constrains." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, @@ -1042,6 +1098,24 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Repeated components collapse on the model figure into a single dashed frame badged with how many components it stands \n", + "for, so this five component model is drawn as two frames whose parameters are marked `independent` (one prior per \n", + "member) rather than as five near identical cards." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, @@ -1466,7 +1540,11 @@ "and `af.Collection()` object.\n", "\n", "Advanced model composition uses multi-level models, which compose models from hierarchies of Python classes. This is\n", - "described in the multi-level model cookbook. " + "described in the multi-level model cookbook. \n", + "\n", + "The model figures drawn above can also be output for every fit: setting `model_figure: true` in the \n", + "workspace's `config/output.yaml` writes a `model.png` beside the `model.info` file of every search, which is off by \n", + "default." ] } ], diff --git a/notebooks/cookbooks/multi_level_model.ipynb b/notebooks/cookbooks/multi_level_model.ipynb index fcda3892..1d88f9f6 100644 --- a/notebooks/cookbooks/multi_level_model.ipynb +++ b/notebooks/cookbooks/multi_level_model.ipynb @@ -319,6 +319,29 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The model figure draws this hierarchy as containment. The figure is the **map** of a model and the `info` above is its \n", + "**legend**: the map shows the structure, meaning which component owns which parameter and which of those parameters \n", + "are fixed, shared, related to one another or constrained, whereas the `info` lists the priors and values themselves.\n", + "\n", + "Each group's `higher_level_centre` sits in the outer `MultiLevelGaussians` card whereas its `Gaussian`s sit inside \n", + "the `gaussian_list` frame nested within it, which is exactly the reason to use a multi-level model, drawn rather than \n", + "indented. The repeated groups and their repeated `Gaussian`s each collapse into a dashed frame badged with how many \n", + "components it stands for." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, @@ -448,7 +471,24 @@ "cell_type": "code", "metadata": {}, "source": [ - "print(group_level.info)\n" + "print(group_level.info)" + ], + "outputs": [], + "execution_count": null + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "On the model figure the fixed `higher_level_centre` is greyed out and the linked `normalization` carries a blue shared \n", + "badge, whereas `centre` and `sigma` stay marked `independent`, meaning one prior per member." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(group_level).figure()\n" ], "outputs": [], "execution_count": null diff --git a/notebooks/cookbooks/multiple_datasets.ipynb b/notebooks/cookbooks/multiple_datasets.ipynb index 1a5cc96d..72158ed2 100644 --- a/notebooks/cookbooks/multiple_datasets.ipynb +++ b/notebooks/cookbooks/multiple_datasets.ipynb @@ -327,6 +327,28 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We can also draw this global model, via `af.ModelPlotter`. The figure is the **map** of a model and the `info` above \n", + "is its **legend**: the map shows the structure, meaning which dataset gets which component and which parameters are \n", + "shared between them, whereas the `info` lists the priors and values themselves.\n", + "\n", + "Because every dataset is fitted by the same `Gaussian`, the three components collapse into one frame and each of \n", + "`centre`, `normalization` and `sigma` carries a shared badge, which is the picture of a three parameter fit to three \n", + "datasets." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(factor_graph.global_prior_model).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, @@ -562,6 +584,23 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Only `centre` now carries the shared badge on the model figure, whereas `normalization` and `sigma` are marked \n", + "`independent`, meaning one prior per dataset, and the footer counts the seven unique sampled scalars this makes." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(factor_graph.global_prior_model).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, @@ -678,6 +717,25 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The relational model is the case where the model figure is worth the most, because the relation is structure whereas \n", + "the `info` above can only express it as nesting. Each dataset's `sigma` pill states the expression that defines it and \n", + "`centre` and `normalization` are badged as shared, so the map says directly that adding a fourth dataset would add a \n", + "fourth card and no new parameters." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(factor_graph.global_prior_model).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, diff --git a/scripts/cookbooks/model.py b/scripts/cookbooks/model.py index 0ecf7e66..bc47ba55 100644 --- a/scripts/cookbooks/model.py +++ b/scripts/cookbooks/model.py @@ -232,6 +232,17 @@ def __init__( """ print(model.info) +""" +We can also draw the model, via `af.ModelPlotter`. The figure is the **map** of a model and the `info` above is its +**legend**: the map shows the structure, meaning which component owns which parameter and which of those parameters +are fixed, shared, related to one another or constrained, whereas the `info` lists the priors and values themselves. + +Customization is where the map earns its place, because linking a parameter to another or adding an assertion changes +a model's structure and not just its numbers. The `centre` pill states the expression `centre = normalization + sigma` +that defines it and the assertions are drawn as constraints, which the `info` above does not print at all. +""" +af.ModelPlotter(model).figure() + """ The overwriting of priors shown above can be achieved via the following alternative API: """ @@ -287,6 +298,12 @@ def __init__( print("\nInfo:") print(model.info) +""" +The model figure tags `centre` as `2D`, which is why counting the pills on the map does not reproduce the four free +parameters its footer counts. +""" +af.ModelPlotter(model).figure() + """ Here are examples of how model customization can be applied to a model with tuple parameters: """ @@ -496,6 +513,12 @@ def __init__( print(model.info) +""" +A `Collection` is drawn on the model figure as one card per component, with the fixed values greyed out and any +assertion attached to the component it constrains. +""" +af.ModelPlotter(model).figure() + """ __JSon Outputs (Collection)__ @@ -545,6 +568,13 @@ def __init__( print(model.info) +""" +Repeated components collapse on the model figure into a single dashed frame badged with how many components it stands +for, so this five component model is drawn as two frames whose parameters are marked `independent` (one prior per +member) rather than as five near identical cards. +""" +af.ModelPlotter(model).figure() + """ A model can be created via `af.Collection()` where a dictionary of `af.Model()` objects are passed to it. @@ -780,4 +810,8 @@ def __init__( Advanced model composition uses multi-level models, which compose models from hierarchies of Python classes. This is described in the multi-level model cookbook. + +The model figures drawn above can also be output for every fit: setting `model_figure: true` in the +workspace's `config/output.yaml` writes a `model.png` beside the `model.info` file of every search, which is off by +default. """ diff --git a/scripts/cookbooks/multi_level_model.py b/scripts/cookbooks/multi_level_model.py index 65c379cb..5d7c6107 100644 --- a/scripts/cookbooks/multi_level_model.py +++ b/scripts/cookbooks/multi_level_model.py @@ -171,6 +171,18 @@ def __init__( print(model.info) +""" +The model figure draws this hierarchy as containment. The figure is the **map** of a model and the `info` above is its +**legend**: the map shows the structure, meaning which component owns which parameter and which of those parameters +are fixed, shared, related to one another or constrained, whereas the `info` lists the priors and values themselves. + +Each group's `higher_level_centre` sits in the outer `MultiLevelGaussians` card whereas its `Gaussian`s sit inside +the `gaussian_list` frame nested within it, which is exactly the reason to use a multi-level model, drawn rather than +indented. The repeated groups and their repeated `Gaussian`s each collapse into a dashed frame badged with how many +components it stands for. +""" +af.ModelPlotter(model).figure() + """ Composing the same model without the multi-level model is less concise, less readable and prone to error: """ @@ -250,6 +262,12 @@ def __init__( """ print(group_level.info) +""" +On the model figure the fixed `higher_level_centre` is greyed out and the linked `normalization` carries a blue shared +badge, whereas `centre` and `sigma` stay marked `independent`, meaning one prior per member. +""" +af.ModelPlotter(group_level).figure() + """ __Alternative API__ diff --git a/scripts/cookbooks/multiple_datasets.py b/scripts/cookbooks/multiple_datasets.py index ec71e018..958b47e5 100644 --- a/scripts/cookbooks/multiple_datasets.py +++ b/scripts/cookbooks/multiple_datasets.py @@ -188,6 +188,17 @@ """ print(factor_graph.global_prior_model.info) +""" +We can also draw this global model, via `af.ModelPlotter`. The figure is the **map** of a model and the `info` above +is its **legend**: the map shows the structure, meaning which dataset gets which component and which parameters are +shared between them, whereas the `info` lists the priors and values themselves. + +Because every dataset is fitted by the same `Gaussian`, the three components collapse into one frame and each of +`centre`, `normalization` and `sigma` carries a shared badge, which is the picture of a three parameter fit to three +datasets. +""" +af.ModelPlotter(factor_graph.global_prior_model).figure() + """ To fit multiple datasets, we pass the `FactorGraphModel` to a non-linear search. @@ -335,6 +346,12 @@ print(factor_graph.global_prior_model.info) +""" +Only `centre` now carries the shared badge on the model figure, whereas `normalization` and `sigma` are marked +`independent`, meaning one prior per dataset, and the footer counts the seven unique sampled scalars this makes. +""" +af.ModelPlotter(factor_graph.global_prior_model).figure() + """ Fit this model to the data using dynesty. """ @@ -408,6 +425,14 @@ print(factor_graph.global_prior_model.info) +""" +The relational model is the case where the model figure is worth the most, because the relation is structure whereas +the `info` above can only express it as nesting. Each dataset's `sigma` pill states the expression that defines it and +`centre` and `normalization` are badged as shared, so the map says directly that adding a fourth dataset would add a +fourth card and no new parameters. +""" +af.ModelPlotter(factor_graph.global_prior_model).figure() + """ We can fit the model as per usual. """