Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion config/output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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
80 changes: 79 additions & 1 deletion notebooks/cookbooks/model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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": {},
Expand Down Expand Up @@ -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": {},
Expand Down Expand Up @@ -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": {},
Expand Down Expand Up @@ -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": {},
Expand Down Expand Up @@ -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."
]
}
],
Expand Down
42 changes: 41 additions & 1 deletion notebooks/cookbooks/multi_level_model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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": {},
Expand Down Expand Up @@ -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
Expand Down
58 changes: 58 additions & 0 deletions notebooks/cookbooks/multiple_datasets.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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": {},
Expand Down Expand Up @@ -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": {},
Expand Down Expand Up @@ -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": {},
Expand Down
34 changes: 34 additions & 0 deletions scripts/cookbooks/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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)__

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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.
"""
18 changes: 18 additions & 0 deletions scripts/cookbooks/multi_level_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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__
Expand Down
Loading
Loading