diff --git a/config/output.yaml b/config/output.yaml index 88f7d01..58e9223 100755 --- a/config/output.yaml +++ b/config/output.yaml @@ -95,4 +95,8 @@ latent_results: true # Whether to output the `latent.results` file. # Other Files: -search_log: true # `search.log`: logging produced whilst running the fit method \ No newline at end of file +search_log: true # `search.log`: logging produced whilst running the fit method + +# `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 \ No newline at end of file diff --git a/notebooks/chapter_2_lens_modeling/tutorial_10_prior_passing.ipynb b/notebooks/chapter_2_lens_modeling/tutorial_10_prior_passing.ipynb index 7ac69d5..d0ef931 100644 --- a/notebooks/chapter_2_lens_modeling/tutorial_10_prior_passing.ipynb +++ b/notebooks/chapter_2_lens_modeling/tutorial_10_prior_passing.ipynb @@ -208,6 +208,28 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is the same model as the first search of the previous tutorial, so it is the same map: greyed `centre` and\n", + "`sersic_index` pills for the parameters fixed to values, the blue `shared \u00d72` and `\u2197 bulge.ell_comps` pair\n", + "joining the mass's ellipticity to the bulge's, dashed `intensity \u00b7 solved` pills on both linear light profiles,\n", + "and a footer of `11 unique sampled scalars`.\n", + "\n", + "Hold this map in mind as you read the second model below. This tutorial is about what changes between the two \u2014\n", + "and, more importantly, about what does not." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model_1).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, @@ -376,6 +398,33 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is the lesson of this tutorial, drawn. Prior passing moves the **legend** and leaves the **map** alone:\n", + "almost every number in the `info` above has been replaced by a narrow prior centred on search 1's result, and\n", + "not one of those replacements shows up in the figure, because a prior is a number and numbers are not structure.\n", + "\n", + "What the map *does* show are the two changes we made deliberately, and separately from the passing itself: the\n", + "`centre` and `sersic_index` pills are white rather than grey (we chose not to pass those, so they are free\n", + "again, and the blue bracket between bulge and mass is gone with them), and the source's card is now\n", + "`bulge \u00b7 Sersic` rather than `bulge \u00b7 ExponentialCore` \u2014 the upgrade `take_attributes` exists to make possible.\n", + "The footer moves from `11 unique sampled scalars` to `19`.\n", + "\n", + "This is a good habit when chaining searches: draw both models. Every difference you see on the map is a\n", + "structural decision you made on purpose; everything else the chain did for you is in the legend." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model_2).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, diff --git a/notebooks/chapter_2_lens_modeling/tutorial_1_non_linear_search.ipynb b/notebooks/chapter_2_lens_modeling/tutorial_1_non_linear_search.ipynb index d4bc315..d50729c 100644 --- a/notebooks/chapter_2_lens_modeling/tutorial_1_non_linear_search.ipynb +++ b/notebooks/chapter_2_lens_modeling/tutorial_1_non_linear_search.ipynb @@ -432,6 +432,44 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The same model can also be drawn as a figure, via `af.ModelPlotter`.\n", + "\n", + "The figure and the `info` are two views of the same model, and it is worth being precise about the division of\n", + "labour between them. The figure is the **map**: it shows the structure of the model, meaning which galaxy owns\n", + "which profile, which profile owns which parameter, and what state every parameter is in \u2014 free, fixed to a\n", + "value, shared with another parameter, related to one by an expression, or solved for during the fit rather than\n", + "sampled. The `info` printed above is the **legend**: it lists the priors and values themselves, the numbers the\n", + "map has no room for.\n", + "\n", + "Read this first map from the outside in. The outer frame is titled `galaxies`, after the `Collection` we just\n", + "composed. Inside it sit two cards, `lens \u00b7 Galaxy` and `source \u00b7 Galaxy`, each labelled underneath with its\n", + "redshift, `redshift = 0.5` and `redshift = 1.0` \u2014 which is how **PyAutoLens** knows which galaxy does the\n", + "lensing and which one is lensed. Inside the lens card is a single profile card, `mass \u00b7 IsothermalSph`, holding\n", + "the pills `centre` (carrying a `2D` badge, because a centre is a tuple of two coordinates) and `einstein_radius`.\n", + "Inside the source card is `bulge \u00b7 ExponentialCoreSph`, whose greyed-out `radius_break`, `gamma` and `alpha`\n", + "pills mark parameters fixed to default values, and whose dashed `intensity \u00b7 solved` pill marks the one\n", + "parameter that is not sampled at all: because this is a linear light profile, its `intensity` is solved for by\n", + "linear algebra at every likelihood evaluation. The footer totals the model up as `6 unique sampled scalars`,\n", + "`5 fixed leaf slots` and `1 parameter solved during fitting`.\n", + "\n", + "Draw the map every time you compose a model. It fits on one screen now and may look like a redundant restatement\n", + "of the `info`, but the two views scale very differently: by chapter 4 our models hold fifteen galaxies and their\n", + "`info` runs to hundreds of lines, whilst the map still fits in a glance." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, @@ -508,6 +546,27 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Drawing the model again makes the point sharply: the map is identical to the one above. Priors live in the\n", + "legend, not on the map, so customizing them changes many numbers in the `info` and nothing at all in the figure.\n", + "\n", + "That is exactly what you want from the two views. When you tune priors you are changing where the search looks,\n", + "not what it is looking for, and an unchanged map is the visual confirmation that you have not accidentally\n", + "changed the model itself whilst editing it." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, diff --git a/notebooks/chapter_2_lens_modeling/tutorial_2_practicalities.ipynb b/notebooks/chapter_2_lens_modeling/tutorial_2_practicalities.ipynb index c889a7e..30567b3 100644 --- a/notebooks/chapter_2_lens_modeling/tutorial_2_practicalities.ipynb +++ b/notebooks/chapter_2_lens_modeling/tutorial_2_practicalities.ipynb @@ -280,6 +280,31 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As in the previous tutorial we also draw the model, remembering the division of labour between the two views:\n", + "the figure is the **map**, showing the structure of the model \u2014 which galaxy owns which profile and parameter,\n", + "and whether each parameter is free, fixed or solved \u2014 and the `info` is the **legend**, holding the priors and\n", + "values themselves.\n", + "\n", + "Because this is the same model as tutorial 1, it is the same map: the `galaxies` frame holding a\n", + "`lens \u00b7 Galaxy` card at `redshift = 0.5` with its `mass \u00b7 IsothermalSph` profile, a `source \u00b7 Galaxy` card at\n", + "`redshift = 1.0` with its `bulge \u00b7 ExponentialCoreSph` and its dashed `intensity \u00b7 solved` pill, and a footer\n", + "reading `6 unique sampled scalars`. Nothing in this tutorial changes the model \u2014 every setting we discuss\n", + "below belongs to the search, and the search is not drawn on this map." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, diff --git a/notebooks/chapter_2_lens_modeling/tutorial_4_dealing_with_failure.ipynb b/notebooks/chapter_2_lens_modeling/tutorial_4_dealing_with_failure.ipynb index c94aac4..4e82a8f 100644 --- a/notebooks/chapter_2_lens_modeling/tutorial_4_dealing_with_failure.ipynb +++ b/notebooks/chapter_2_lens_modeling/tutorial_4_dealing_with_failure.ipynb @@ -427,6 +427,34 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Drawing the model makes plain why this fit is so much harder than the one in tutorial 1. The figure is the\n", + "**map** of the model, showing its structure and the state of every parameter, whilst the `info` above is the\n", + "**legend**, holding the priors we just customized.\n", + "\n", + "The `lens \u00b7 Galaxy` card now holds three profile cards rather than one: `bulge \u00b7 Sersic`, `mass \u00b7 Isothermal`\n", + "and `shear \u00b7 ExternalShear`. Every pill is white, meaning every parameter is free, and the footer counts\n", + "`20 unique sampled scalars` against the six of tutorial 1. Note also that these are *ordinary* light profiles\n", + "rather than the linear ones we have been using: `intensity` appears as a plain free pill on both `bulge` cards\n", + "instead of the dashed `intensity \u00b7 solved` we saw before, so the search has to sample it. Tutorial 5 will take\n", + "those two dimensions back.\n", + "\n", + "The priors we tightened above are nowhere on this map, because a prior is a number and numbers belong to the\n", + "legend. The map's job here is to show you the *size* of the problem the priors are helping with." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, @@ -612,6 +640,30 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now the map earns its keep. On the `mass \u00b7 Isothermal` card the `centre` and `ell_comps` pills are no longer\n", + "free parameters of their own: each carries a blue reference badge reading `\u2197 bulge.centre` and\n", + "`\u2197 bulge.ell_comps`, and a blue bracket runs down the side of the figure joining them to the matching\n", + "`shared \u00d72` badges on the `bulge \u00b7 Sersic` card. That bracket *is* the light-traces-mass assumption, drawn.\n", + "The footer confirms the saving: `16 unique sampled scalars` and `4 shared priors`, down from the twenty of the\n", + "previous model.\n", + "\n", + "Pairing parameters is easy to get subtly wrong and hard to spot in a long `info` listing, but trivial to check\n", + "on the map. Whenever you pair parameters, draw the model and confirm the brackets land where you meant them to." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, diff --git a/notebooks/chapter_2_lens_modeling/tutorial_5_linear_profiles.ipynb b/notebooks/chapter_2_lens_modeling/tutorial_5_linear_profiles.ipynb index 9e897b7..ff865a7 100644 --- a/notebooks/chapter_2_lens_modeling/tutorial_5_linear_profiles.ipynb +++ b/notebooks/chapter_2_lens_modeling/tutorial_5_linear_profiles.ipynb @@ -265,6 +265,31 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The figure shows the linear light profiles in a way the `info` cannot. On both `bulge` cards \u2014 the lens's\n", + "`Sersic` and the source's `ExponentialCore` \u2014 the final pill is drawn with a dashed outline and reads\n", + "`intensity \u00b7 solved`, and the figure's legend line spells the dash out as *solved during fitting*. The footer\n", + "then separates the two kinds of quantity for you: `18 unique sampled scalars`, the dimensions the search\n", + "actually explores, and `2 parameters solved during fitting`, the intensities the inversion computes exactly at\n", + "every likelihood evaluation.\n", + "\n", + "This is precisely the distinction the section above is making. In the `info` the `intensity` is simply absent,\n", + "and an absence is a hard thing to notice; on the map it is present but visibly marked as belonging to a\n", + "different machine." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, @@ -690,7 +715,7 @@ "metadata": {}, "source": [ "The `info` attribute shows the model, which is a lot longer than we have seen previously, given that is \n", - "composed of 20 Gaussians in total!" + "composed of 45 Gaussians in total (30 for the lens, 15 for the source)!" ] }, { @@ -702,6 +727,33 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The `info` for this model is enormous \u2014 forty-five Gaussians, each with its own block of parameters. The map is\n", + "not, because it collapses repetition into **plates**. Each `bulge \u00b7 Basis` card holds a `profile_list` frame\n", + "containing one dashed plate: a single `Gaussian` card badged `30 components` on the lens and `15 components` on\n", + "the source, with the index range (`0 - 29`, `0 - 14`) printed beneath the title. A plate says \"this one card\n", + "stands for all of these components\", and its pills describe what every member of the plate has in common:\n", + "`centre` and `ell_comps` carry blue `shared across group` badges, `sigma` reads `fixed, varies by member`, and\n", + "`intensity` is the familiar dashed `intensity \u00b7 solved`. The caption under the plate repeats it in words,\n", + "`Gaussian with priors centre \u21c4 shared, ell_comps \u21c4 shared, sigma fixed (varies by member), intensity solved`.\n", + "\n", + "That is the whole MGE trick in one line of the figure, and the footer proves the arithmetic: `13 unique sampled\n", + "scalars` alongside `47 fixed leaf slots`, `45 parameters solved during fitting` and `2 plates standing for 45\n", + "components`. Forty-five Gaussians, thirteen dimensions \u2014 the N=13 quoted above." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, diff --git a/notebooks/chapter_2_lens_modeling/tutorial_9_search_chaining.ipynb b/notebooks/chapter_2_lens_modeling/tutorial_9_search_chaining.ipynb index 1d4987a..125f0dd 100644 --- a/notebooks/chapter_2_lens_modeling/tutorial_9_search_chaining.ipynb +++ b/notebooks/chapter_2_lens_modeling/tutorial_9_search_chaining.ipynb @@ -305,6 +305,30 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The map of `model_1` shows the three kinds of simplification we just made, each drawn differently. The `centre`\n", + "pills on `bulge \u00b7 Sersic` and `mass \u00b7 Isothermal` are greyed out, as is `sersic_index`: grey means fixed to a\n", + "value and removed from the search. `ell_comps` carries a blue `shared \u00d72` badge on the bulge and an\n", + "`\u2197 bulge.ell_comps` reference on the mass, with a bracket joining them \u2014 the pairing. And `intensity \u00b7 solved`\n", + "sits dashed on both linear light profiles. The footer adds it all up: `11 unique sampled scalars`, `10 fixed\n", + "leaf slots`, `2 shared priors` and `2 parameters solved during fitting`.\n", + "\n", + "Eleven dimensions is a deliberately crude model, and that is exactly what the first search in a chain wants: it\n", + "needs only to be good enough to locate the lens roughly, and to do so quickly." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model_1).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, @@ -560,6 +584,28 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The second model's map is the comparison worth making. Every grey pill has turned white \u2014 the centres and the\n", + "`sersic_index` are free again \u2014 the blue bracket between `bulge` and `mass` has gone with them, and the source's\n", + "card is now a `Sersic` carrying a `sersic_index` pill of its own. The footer reads `19 unique sampled scalars`\n", + "against the eleven of `model_1`.\n", + "\n", + "That is search chaining drawn as a picture: search 1 fits the small map, and what it learns buys us the priors\n", + "that make the large map affordable." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model_2).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, diff --git a/notebooks/chapter_4_scaling_up_lensing/tutorial_1_extra_galaxies.ipynb b/notebooks/chapter_4_scaling_up_lensing/tutorial_1_extra_galaxies.ipynb index 79ba77e..7e76259 100644 --- a/notebooks/chapter_4_scaling_up_lensing/tutorial_1_extra_galaxies.ipynb +++ b/notebooks/chapter_4_scaling_up_lensing/tutorial_1_extra_galaxies.ipynb @@ -593,6 +593,37 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The model can also be drawn, and this is the first model in the series with a genuinely new *shape* rather than\n", + "simply new profiles. The figure is the **map** \u2014 the structure of the model, which galaxy owns which profile and\n", + "parameter, and whether each parameter is free, fixed or solved for during the fit \u2014 and the `info` above is the\n", + "**legend**, holding the priors and fixed values themselves.\n", + "\n", + "The new shape is immediately visible: there are now two top-level frames side by side rather than one. The\n", + "familiar `galaxies` frame holds `lens \u00b7 Galaxy` and `source \u00b7 Galaxy`, and beside it sits a second frame titled\n", + "`extra_galaxies`, holding the card `0 \u00b7 Galaxy` at `redshift = 0.5` with its own `light \u00b7 ExponentialSph` and\n", + "`mass \u00b7 IsothermalSph` profiles. That second frame is the `extra_galaxies` input to the `Collection` drawn as a\n", + "picture: the interloper is part of the model and part of the ray-tracing, but it is visibly neither the lens nor\n", + "the source.\n", + "\n", + "Then look at what the extra galaxy costs. Both of its `centre` pills are greyed out \u2014 fixed to the centre we\n", + "measured from the data \u2014 and its `light` card carries the dashed `intensity \u00b7 solved` pill, so only\n", + "`effective_radius` and `einstein_radius` are left white. The footer reads `21 unique sampled scalars`, just two\n", + "more than the same lens-and-source model would score without it: an entire extra galaxy for two dimensions." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, diff --git a/notebooks/chapter_4_scaling_up_lensing/tutorial_2_multi_galaxy.ipynb b/notebooks/chapter_4_scaling_up_lensing/tutorial_2_multi_galaxy.ipynb index 685708e..ad94b06 100644 --- a/notebooks/chapter_4_scaling_up_lensing/tutorial_2_multi_galaxy.ipynb +++ b/notebooks/chapter_4_scaling_up_lensing/tutorial_2_multi_galaxy.ipynb @@ -316,7 +316,36 @@ "cell_type": "code", "metadata": {}, "source": [ - "print(model.info)\n", + "print(model.info)" + ], + "outputs": [], + "execution_count": null + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Drawing the model shows the multi-galaxy regime at a glance. The figure is the **map** of the model's structure\n", + "and the `info` is the **legend** of its priors; here it is the map that carries the news.\n", + "\n", + "Inside the `galaxies` frame there are now three cards rather than two: `lens_0 \u00b7 Galaxy` and `lens_1 \u00b7 Galaxy`,\n", + "both at `redshift = 0.5`, above the single `source \u00b7 Galaxy` at `redshift = 1.0`. Each deflector card holds its\n", + "own `mass \u00b7 Isothermal` with three white pills \u2014 `centre`, `ell_comps`, `einstein_radius` \u2014 two independent,\n", + "fully free mass models. That is the defining feature of this regime, and the thing every earlier chapter lacked.\n", + "\n", + "Each galaxy's light is drawn as a `bulge \u00b7 Basis` card wrapping a dashed **plate**: one `Gaussian` card badged\n", + "`20 components`, standing for all twenty Gaussians of the MGE, with `centre` and `ell_comps` carrying blue\n", + "`shared across group` badges, `sigma` reading `fixed, varies by member` and `intensity` dashed as\n", + "`intensity \u00b7 solved`. The footer does the accounting: `22 unique sampled scalars` \u2014 the same number the\n", + "`prior_count` prints below \u2014 alongside `63 fixed leaf slots`, `60 parameters solved during fitting` and\n", + "`3 plates standing for 60 components`." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model).figure()\n", "\n", "print(f\"Free parameters (free mass centres): {model.prior_count}\")" ], @@ -406,7 +435,27 @@ "cell_type": "code", "metadata": {}, "source": [ - "print(model.info)\n", + "print(model.info)" + ], + "outputs": [], + "execution_count": null + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The map is the cleanest way to see what just changed. The two `mass \u00b7 Isothermal` cards' `centre` pills have\n", + "turned grey \u2014 fixed to the measured light centres, no longer sampled \u2014 and nothing else in the figure has moved.\n", + "The footer falls from `22 unique sampled scalars` to `18`, with `67 fixed leaf slots` where there were `63`.\n", + "\n", + "Four grey pills, four dimensions gone. That is the standard trick of the multi-galaxy regime, drawn." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model).figure()\n", "\n", "print(f\"Free parameters (fixed mass centres): {model.prior_count}\")" ], @@ -636,7 +685,28 @@ "\n", "model = af.Collection(galaxies=af.Collection(**lens_dict, source=source))\n", "\n", - "print(model.info)\n", + "print(model.info)" + ], + "outputs": [], + "execution_count": null + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "With a third deflector the map simply grows a third card: `lens_0`, `lens_1` and `lens_2 \u00b7 Galaxy`, each an\n", + "identical copy of the structure above, each with its grey fixed mass `centre`. The footer reads `25 unique\n", + "sampled scalars` and `4 plates standing for 80 components`.\n", + "\n", + "Nothing about the *shape* of the model changed when we added a galaxy, which is exactly the point the list-based\n", + "API is making. The map grows by one card per deflector, and the parameter count by seven." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model).figure()\n", "\n", "print(f\"Free parameters (three deflectors, fixed mass centres): {model.prior_count}\")" ], diff --git a/notebooks/chapter_4_scaling_up_lensing/tutorial_3_scaling_relation.ipynb b/notebooks/chapter_4_scaling_up_lensing/tutorial_3_scaling_relation.ipynb index 399334e..0a00a28 100644 --- a/notebooks/chapter_4_scaling_up_lensing/tutorial_3_scaling_relation.ipynb +++ b/notebooks/chapter_4_scaling_up_lensing/tutorial_3_scaling_relation.ipynb @@ -507,6 +507,38 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is the model whose map is worth the most, because the scaling relation is a piece of structure the `info`\n", + "can only describe in words. The figure is the **map** \u2014 what the model is made of and how its parts are tied\n", + "together \u2014 and the `info` is the **legend** of priors and values.\n", + "\n", + "Two top-level frames again, but not the pair from tutorial 1: `galaxies` holds `lens \u00b7 Galaxy` (the anchor) and\n", + "`source \u00b7 Galaxy`, and below it a frame titled `scaling_galaxies` holds the tied member as `0 \u00b7 Galaxy`. All\n", + "three galaxies wear a `bulge \u00b7 Basis` plate, a `Gaussian` card badged `10 components`; on the two lens galaxies\n", + "its caption reads `Gaussian with priors centre fixed, ell_comps \u21c4 shared, sigma fixed (varies by member),\n", + "intensity solved`, which is the MGE costing two free parameters apiece, exactly as claimed above.\n", + "\n", + "Now look at the tied galaxy's `mass \u00b7 IsothermalSph` card. Where the anchor has a plain white `einstein_radius`\n", + "pill, the tied galaxy has an amber **expression** pill spelling the relation out in full \u2014\n", + "`einstein_radius = galaxies.lens.mass.einstein_radius * ` \u2014 with an amber dashed line running\n", + "across the figure from the anchor's `einstein_radius` pill to it. The figure's legend names this state\n", + "`relation (expression shown)`, and it is a state no amount of reading priors can reveal: the tied radius has no\n", + "prior, because it is not a parameter. The footer's `11 unique sampled scalars` counts the anchor's radius once\n", + "and the tied galaxy's not at all." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, diff --git a/notebooks/chapter_4_scaling_up_lensing/tutorial_4_group_scale.ipynb b/notebooks/chapter_4_scaling_up_lensing/tutorial_4_group_scale.ipynb index c7e3a23..40c905a 100644 --- a/notebooks/chapter_4_scaling_up_lensing/tutorial_4_group_scale.ipynb +++ b/notebooks/chapter_4_scaling_up_lensing/tutorial_4_group_scale.ipynb @@ -609,7 +609,36 @@ "cell_type": "code", "metadata": {}, "source": [ - "print(model.info)\n", + "print(model.info)" + ], + "outputs": [], + "execution_count": null + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The figure is the **map** of this model and the `info` is its **legend**; at group scale the map begins doing\n", + "real work, because the model now contains a population rather than a handful of named galaxies.\n", + "\n", + "The `galaxies` frame holds the two individually-modelled cards, `lens \u00b7 Galaxy` (the BGG, with its\n", + "`bulge \u00b7 SersicSph` and `mass \u00b7 IsothermalSph`) and `source \u00b7 Galaxy`. Beside them the `extra_galaxies` frame\n", + "holds not two cards but one dashed **plate**: a `Galaxy` card badged `2 components` and subtitled\n", + "`0 - 1 \u00b7 redshift = 0.5`, standing for both members at once. The plate's pills say what the members have in\n", + "common and how they differ \u2014 `centre \u00b7 fixed, varies by member` (every member's centre is fixed, each to its own\n", + "value), and `effective_radius`, `sersic_index` and `sigma` each badged `independent`, which the figure's legend\n", + "glosses as *one prior per member*. The `r_core`, `r_cut`, `redshift_object`, `redshift_source`, `H0` and `Om0`\n", + "pills are grey: the pinned dPIE constants.\n", + "\n", + "The footer reads `19 unique sampled scalars` \u2014 the N=19 the line below prints \u2014 with `1 plate standing for 2\n", + "components`. Hold on to that number and that plate: the next model changes both." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model).figure()\n", "\n", "print(f\"Total free parameters: {model.prior_count}\")" ], @@ -798,7 +827,36 @@ "cell_type": "code", "metadata": {}, "source": [ - "print(model.info)\n", + "print(model.info)" + ], + "outputs": [], + "execution_count": null + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The map records the change precisely. The tied members have moved out of the `extra_galaxies` frame into one\n", + "titled `scaling_galaxies`, and they are now drawn as two separate cards, `0 \u00b7 Galaxy` and `1 \u00b7 Galaxy`, rather\n", + "than as a single plate \u2014 a plate stands for components that share one description, and these two no longer do.\n", + "On each card's `mass \u00b7 dPIEMassSph` the `sigma` pill is no longer an `independent` prior but an amber\n", + "**expression** pill, and both expressions name the same prior:\n", + "`sigma = scaling_galaxies.1.mass.sigma.sigma_ref * `, with a different ratio on each card. One\n", + "free prior, two galaxies \u2014 the scaling relation, drawn.\n", + "\n", + "The `r_cut` pills stay grey, as they were before. `r_cut_ref * luminosity_ratio ** 0.7` is arithmetic on plain\n", + "numbers rather than on a prior, so it yields a fixed value, and a fixed value is a legend entry, not a relation.\n", + "\n", + "The footer falls from `19 unique sampled scalars` to `18`. One parameter is an unremarkable saving for two\n", + "members, but the map shows why the saving keeps growing: however many cards you add to the `scaling_galaxies`\n", + "frame, every one of their `sigma` pills points back at that single `sigma_ref`." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model).figure()\n", "\n", "print(f\"Total free parameters: {model.prior_count}\")" ], diff --git a/notebooks/chapter_4_scaling_up_lensing/tutorial_5_cluster_scale.ipynb b/notebooks/chapter_4_scaling_up_lensing/tutorial_5_cluster_scale.ipynb index 15a96ea..b26f4e7 100644 --- a/notebooks/chapter_4_scaling_up_lensing/tutorial_5_cluster_scale.ipynb +++ b/notebooks/chapter_4_scaling_up_lensing/tutorial_5_cluster_scale.ipynb @@ -620,6 +620,40 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This figure is the punchline of the chapter, and it repays reading slowly. The map shows the model's structure;\n", + "the `info` above is the legend of its priors.\n", + "\n", + "Each of the four tiers described above is drawn as its own object. The `galaxies` frame opens with a dashed\n", + "**plate** \u2014 `Galaxy` badged `2 components`, subtitled `lens_0 \u00b7 lens_1 \u00b7 redshift = 0.5` \u2014 standing for both\n", + "main galaxies at once: on its `mass \u00b7 dPIEMassSph` card, `centre \u00b7 fixed, varies by member` is grey, `sigma` and\n", + "`r_cut` are badged `independent` (one free prior per member), and `r_core`, the two redshifts and `H0` / `Om0`\n", + "are grey pinned constants. Below it the host halo appears as an ordinary card, `host_halo \u00b7 Galaxy`, holding\n", + "`dark \u00b7 NFWMCRLudlowSph` with exactly one white pill, `mass_at_200`. Below that a second plate, `Galaxy` badged\n", + "`2 components` and subtitled `source_0 \u00b7 source_1`, holds `point_0 \u00b7 PointSolved` whose only pill is the dashed\n", + "`centre \u00b7 solved` \u2014 the source centres solved for rather than sampled.\n", + "\n", + "The second top-level frame, `scaling_galaxies`, holds the member tier: a plate badged `9 components` and one\n", + "card, `9 \u00b7 Galaxy`, standing apart from it, and on both the `sigma` pill is an amber expression reading\n", + "`sigma = scaling_galaxies.9.mass.sigma.scaling_sigma_ref * `. Ten members, one free prior.\n", + "\n", + "Now read the footer: `6 unique sampled scalars`, `113 fixed leaf slots`, `2 parameters solved during fitting`\n", + "and `3 plates standing for 13 components`. Thirteen galaxies and two sources, six dimensions. The `info` needs\n", + "a hundred lines to say that; the map says it in one picture, and the six white pills are countable at a glance." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, diff --git a/notebooks/chapter_4_scaling_up_lensing/tutorial_6_weak_lensing.ipynb b/notebooks/chapter_4_scaling_up_lensing/tutorial_6_weak_lensing.ipynb index 6bbf611..b46a2a7 100644 --- a/notebooks/chapter_4_scaling_up_lensing/tutorial_6_weak_lensing.ipynb +++ b/notebooks/chapter_4_scaling_up_lensing/tutorial_6_weak_lensing.ipynb @@ -494,6 +494,34 @@ "outputs": [], "execution_count": null }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The map of a weak-lensing model is the smallest in the series, and that is the point of drawing it. The figure\n", + "shows the model's structure; the `info` above lists its priors.\n", + "\n", + "Inside the `galaxies` frame, `lens \u00b7 Galaxy` at `redshift = 0.5` holds a single `mass \u00b7 Isothermal` card with\n", + "three white pills \u2014 `centre`, `ell_comps` and `einstein_radius` \u2014 and `source \u00b7 Galaxy` at `redshift = 1.0`\n", + "holds nothing at all: its card carries the line `no parameters` where every other source in this series has had\n", + "a light profile. In weak lensing the source galaxies are the *data*, a catalogue of measured shapes, so there is\n", + "no source light to fit. The footer reads `5 unique sampled scalars`, against the twenty-plus of the imaging\n", + "tutorials.\n", + "\n", + "Set this map beside the cluster model's from the previous tutorial and you have the two extremes of the chapter\n", + "in one comparison: fifteen galaxies in six dimensions there, two galaxies in five here \u2014 and in both cases the\n", + "figure tells you the dimensionality far faster than scrolling through `info` ever could." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "af.ModelPlotter(model).figure()" + ], + "outputs": [], + "execution_count": null + }, { "cell_type": "markdown", "metadata": {}, diff --git a/scripts/chapter_2_lens_modeling/tutorial_10_prior_passing.py b/scripts/chapter_2_lens_modeling/tutorial_10_prior_passing.py index 1090356..a33ae13 100644 --- a/scripts/chapter_2_lens_modeling/tutorial_10_prior_passing.py +++ b/scripts/chapter_2_lens_modeling/tutorial_10_prior_passing.py @@ -114,6 +114,17 @@ """ print(model_1.info) +""" +This is the same model as the first search of the previous tutorial, so it is the same map: greyed `centre` and +`sersic_index` pills for the parameters fixed to values, the blue `shared ×2` and `↗ bulge.ell_comps` pair +joining the mass's ellipticity to the bulge's, dashed `intensity · solved` pills on both linear light profiles, +and a footer of `11 unique sampled scalars`. + +Hold this map in mind as you read the second model below. This tutorial is about what changes between the two — +and, more importantly, about what does not. +""" +af.ModelPlotter(model_1).figure() + """ __Search__ @@ -205,6 +216,22 @@ """ print(model_2.info) +""" +Here is the lesson of this tutorial, drawn. Prior passing moves the **legend** and leaves the **map** alone: +almost every number in the `info` above has been replaced by a narrow prior centred on search 1's result, and +not one of those replacements shows up in the figure, because a prior is a number and numbers are not structure. + +What the map *does* show are the two changes we made deliberately, and separately from the passing itself: the +`centre` and `sersic_index` pills are white rather than grey (we chose not to pass those, so they are free +again, and the blue bracket between bulge and mass is gone with them), and the source's card is now +`bulge · Sersic` rather than `bulge · ExponentialCore` — the upgrade `take_attributes` exists to make possible. +The footer moves from `11 unique sampled scalars` to `19`. + +This is a good habit when chaining searches: draw both models. Every difference you see on the map is a +structural decision you made on purpose; everything else the chain did for you is in the legend. +""" +af.ModelPlotter(model_2).figure() + """ __Search__ diff --git a/scripts/chapter_2_lens_modeling/tutorial_1_non_linear_search.py b/scripts/chapter_2_lens_modeling/tutorial_1_non_linear_search.py index 171dc06..ecead8a 100644 --- a/scripts/chapter_2_lens_modeling/tutorial_1_non_linear_search.py +++ b/scripts/chapter_2_lens_modeling/tutorial_1_non_linear_search.py @@ -293,6 +293,33 @@ """ print(model.info) +""" +The same model can also be drawn as a figure, via `af.ModelPlotter`. + +The figure and the `info` are two views of the same model, and it is worth being precise about the division of +labour between them. The figure is the **map**: it shows the structure of the model, meaning which galaxy owns +which profile, which profile owns which parameter, and what state every parameter is in — free, fixed to a +value, shared with another parameter, related to one by an expression, or solved for during the fit rather than +sampled. The `info` printed above is the **legend**: it lists the priors and values themselves, the numbers the +map has no room for. + +Read this first map from the outside in. The outer frame is titled `galaxies`, after the `Collection` we just +composed. Inside it sit two cards, `lens · Galaxy` and `source · Galaxy`, each labelled underneath with its +redshift, `redshift = 0.5` and `redshift = 1.0` — which is how **PyAutoLens** knows which galaxy does the +lensing and which one is lensed. Inside the lens card is a single profile card, `mass · IsothermalSph`, holding +the pills `centre` (carrying a `2D` badge, because a centre is a tuple of two coordinates) and `einstein_radius`. +Inside the source card is `bulge · ExponentialCoreSph`, whose greyed-out `radius_break`, `gamma` and `alpha` +pills mark parameters fixed to default values, and whose dashed `intensity · solved` pill marks the one +parameter that is not sampled at all: because this is a linear light profile, its `intensity` is solved for by +linear algebra at every likelihood evaluation. The footer totals the model up as `6 unique sampled scalars`, +`5 fixed leaf slots` and `1 parameter solved during fitting`. + +Draw the map every time you compose a model. It fits on one screen now and may look like a redundant restatement +of the `info`, but the two views scale very differently: by chapter 4 our models hold fifteen galaxies and their +`info` runs to hundreds of lines, whilst the map still fits in a glance. +""" +af.ModelPlotter(model).figure() + """ __Priors__ @@ -347,6 +374,16 @@ """ print(model.info) +""" +Drawing the model again makes the point sharply: the map is identical to the one above. Priors live in the +legend, not on the map, so customizing them changes many numbers in the `info` and nothing at all in the figure. + +That is exactly what you want from the two views. When you tune priors you are changing where the search looks, +not what it is looking for, and an unchanged map is the visual confirmation that you have not accidentally +changed the model itself whilst editing it. +""" +af.ModelPlotter(model).figure() + """ __Analysis__ diff --git a/scripts/chapter_2_lens_modeling/tutorial_2_practicalities.py b/scripts/chapter_2_lens_modeling/tutorial_2_practicalities.py index 84d53e4..14af3e0 100755 --- a/scripts/chapter_2_lens_modeling/tutorial_2_practicalities.py +++ b/scripts/chapter_2_lens_modeling/tutorial_2_practicalities.py @@ -174,6 +174,20 @@ print(model.info) +""" +As in the previous tutorial we also draw the model, remembering the division of labour between the two views: +the figure is the **map**, showing the structure of the model — which galaxy owns which profile and parameter, +and whether each parameter is free, fixed or solved — and the `info` is the **legend**, holding the priors and +values themselves. + +Because this is the same model as tutorial 1, it is the same map: the `galaxies` frame holding a +`lens · Galaxy` card at `redshift = 0.5` with its `mass · IsothermalSph` profile, a `source · Galaxy` card at +`redshift = 1.0` with its `bulge · ExponentialCoreSph` and its dashed `intensity · solved` pill, and a footer +reading `6 unique sampled scalars`. Nothing in this tutorial changes the model — every setting we discuss +below belongs to the search, and the search is not drawn on this map. +""" +af.ModelPlotter(model).figure() + """ __Search__ diff --git a/scripts/chapter_2_lens_modeling/tutorial_4_dealing_with_failure.py b/scripts/chapter_2_lens_modeling/tutorial_4_dealing_with_failure.py index 8ad6d0f..ed9282f 100644 --- a/scripts/chapter_2_lens_modeling/tutorial_4_dealing_with_failure.py +++ b/scripts/chapter_2_lens_modeling/tutorial_4_dealing_with_failure.py @@ -223,6 +223,23 @@ """ print(model.info) +""" +Drawing the model makes plain why this fit is so much harder than the one in tutorial 1. The figure is the +**map** of the model, showing its structure and the state of every parameter, whilst the `info` above is the +**legend**, holding the priors we just customized. + +The `lens · Galaxy` card now holds three profile cards rather than one: `bulge · Sersic`, `mass · Isothermal` +and `shear · ExternalShear`. Every pill is white, meaning every parameter is free, and the footer counts +`20 unique sampled scalars` against the six of tutorial 1. Note also that these are *ordinary* light profiles +rather than the linear ones we have been using: `intensity` appears as a plain free pill on both `bulge` cards +instead of the dashed `intensity · solved` we saw before, so the search has to sample it. Tutorial 5 will take +those two dimensions back. + +The priors we tightened above are nowhere on this map, because a prior is a number and numbers belong to the +legend. The map's job here is to show you the *size* of the problem the priors are helping with. +""" +af.ModelPlotter(model).figure() + """ We can now create this custom search and run it. Our non-linear search will now start by sampling higher likelihood regions of parameter space, given our improved and more informed priors. @@ -331,6 +348,19 @@ """ print(model.info) +""" +Now the map earns its keep. On the `mass · Isothermal` card the `centre` and `ell_comps` pills are no longer +free parameters of their own: each carries a blue reference badge reading `↗ bulge.centre` and +`↗ bulge.ell_comps`, and a blue bracket runs down the side of the figure joining them to the matching +`shared ×2` badges on the `bulge · Sersic` card. That bracket *is* the light-traces-mass assumption, drawn. +The footer confirms the saving: `16 unique sampled scalars` and `4 shared priors`, down from the twenty of the +previous model. + +Pairing parameters is easy to get subtly wrong and hard to spot in a long `info` listing, but trivial to check +on the map. Whenever you pair parameters, draw the model and confirm the brackets land where you meant them to. +""" +af.ModelPlotter(model).figure() + """ We now create this search and run it. """ diff --git a/scripts/chapter_2_lens_modeling/tutorial_5_linear_profiles.py b/scripts/chapter_2_lens_modeling/tutorial_5_linear_profiles.py index 8d74dd6..da1b8de 100644 --- a/scripts/chapter_2_lens_modeling/tutorial_5_linear_profiles.py +++ b/scripts/chapter_2_lens_modeling/tutorial_5_linear_profiles.py @@ -149,6 +149,20 @@ """ print(model.info) +""" +The figure shows the linear light profiles in a way the `info` cannot. On both `bulge` cards — the lens's +`Sersic` and the source's `ExponentialCore` — the final pill is drawn with a dashed outline and reads +`intensity · solved`, and the figure's legend line spells the dash out as *solved during fitting*. The footer +then separates the two kinds of quantity for you: `18 unique sampled scalars`, the dimensions the search +actually explores, and `2 parameters solved during fitting`, the intensities the inversion computes exactly at +every likelihood evaluation. + +This is precisely the distinction the section above is making. In the `info` the `intensity` is simply absent, +and an absence is a hard thing to notice; on the map it is present but visibly marked as belonging to a +different machine. +""" +af.ModelPlotter(model).figure() + """ We now create this search and run it. @@ -429,10 +443,26 @@ """ The `info` attribute shows the model, which is a lot longer than we have seen previously, given that is -composed of 20 Gaussians in total! +composed of 45 Gaussians in total (30 for the lens, 15 for the source)! """ print(model.info) +""" +The `info` for this model is enormous — forty-five Gaussians, each with its own block of parameters. The map is +not, because it collapses repetition into **plates**. Each `bulge · Basis` card holds a `profile_list` frame +containing one dashed plate: a single `Gaussian` card badged `30 components` on the lens and `15 components` on +the source, with the index range (`0 - 29`, `0 - 14`) printed beneath the title. A plate says "this one card +stands for all of these components", and its pills describe what every member of the plate has in common: +`centre` and `ell_comps` carry blue `shared across group` badges, `sigma` reads `fixed, varies by member`, and +`intensity` is the familiar dashed `intensity · solved`. The caption under the plate repeats it in words, +`Gaussian with priors centre ⇄ shared, ell_comps ⇄ shared, sigma fixed (varies by member), intensity solved`. + +That is the whole MGE trick in one line of the figure, and the footer proves the arithmetic: `13 unique sampled +scalars` alongside `47 fixed leaf slots`, `45 parameters solved during fitting` and `2 plates standing for 45 +components`. Forty-five Gaussians, thirteen dimensions — the N=13 quoted above. +""" +af.ModelPlotter(model).figure() + """ We now fit the model. """ diff --git a/scripts/chapter_2_lens_modeling/tutorial_9_search_chaining.py b/scripts/chapter_2_lens_modeling/tutorial_9_search_chaining.py index afe17cf..cf4ee35 100644 --- a/scripts/chapter_2_lens_modeling/tutorial_9_search_chaining.py +++ b/scripts/chapter_2_lens_modeling/tutorial_9_search_chaining.py @@ -167,6 +167,19 @@ """ print(model_1.info) +""" +The map of `model_1` shows the three kinds of simplification we just made, each drawn differently. The `centre` +pills on `bulge · Sersic` and `mass · Isothermal` are greyed out, as is `sersic_index`: grey means fixed to a +value and removed from the search. `ell_comps` carries a blue `shared ×2` badge on the bulge and an +`↗ bulge.ell_comps` reference on the mass, with a bracket joining them — the pairing. And `intensity · solved` +sits dashed on both linear light profiles. The footer adds it all up: `11 unique sampled scalars`, `10 fixed +leaf slots`, `2 shared priors` and `2 parameters solved during fitting`. + +Eleven dimensions is a deliberately crude model, and that is exactly what the first search in a chain wants: it +needs only to be good enough to locate the lens roughly, and to do so quickly. +""" +af.ModelPlotter(model_1).figure() + """ __Search + Analysis__ @@ -312,6 +325,17 @@ """ print(model_2.info) +""" +The second model's map is the comparison worth making. Every grey pill has turned white — the centres and the +`sersic_index` are free again — the blue bracket between `bulge` and `mass` has gone with them, and the source's +card is now a `Sersic` carrying a `sersic_index` pill of its own. The footer reads `19 unique sampled scalars` +against the eleven of `model_1`. + +That is search chaining drawn as a picture: search 1 fits the small map, and what it learns buys us the priors +that make the large map affordable. +""" +af.ModelPlotter(model_2).figure() + """ Lets setup and run the search. As expected, it gives us the correct lens model. However, it does so significantly faster than we are used to! diff --git a/scripts/chapter_4_scaling_up_lensing/tutorial_1_extra_galaxies.py b/scripts/chapter_4_scaling_up_lensing/tutorial_1_extra_galaxies.py index fc4ce81..d11df6e 100644 --- a/scripts/chapter_4_scaling_up_lensing/tutorial_1_extra_galaxies.py +++ b/scripts/chapter_4_scaling_up_lensing/tutorial_1_extra_galaxies.py @@ -388,6 +388,26 @@ """ print(model.info) +""" +The model can also be drawn, and this is the first model in the series with a genuinely new *shape* rather than +simply new profiles. The figure is the **map** — the structure of the model, which galaxy owns which profile and +parameter, and whether each parameter is free, fixed or solved for during the fit — and the `info` above is the +**legend**, holding the priors and fixed values themselves. + +The new shape is immediately visible: there are now two top-level frames side by side rather than one. The +familiar `galaxies` frame holds `lens · Galaxy` and `source · Galaxy`, and beside it sits a second frame titled +`extra_galaxies`, holding the card `0 · Galaxy` at `redshift = 0.5` with its own `light · ExponentialSph` and +`mass · IsothermalSph` profiles. That second frame is the `extra_galaxies` input to the `Collection` drawn as a +picture: the interloper is part of the model and part of the ray-tracing, but it is visibly neither the lens nor +the source. + +Then look at what the extra galaxy costs. Both of its `centre` pills are greyed out — fixed to the centre we +measured from the data — and its `light` card carries the dashed `intensity · solved` pill, so only +`effective_radius` and `einstein_radius` are left white. The footer reads `21 unique sampled scalars`, just two +more than the same lens-and-source model would score without it: an entire extra galaxy for two dimensions. +""" +af.ModelPlotter(model).figure() + """ __Extra Galaxies Fit__ diff --git a/scripts/chapter_4_scaling_up_lensing/tutorial_2_multi_galaxy.py b/scripts/chapter_4_scaling_up_lensing/tutorial_2_multi_galaxy.py index 84c05e3..d94acea 100644 --- a/scripts/chapter_4_scaling_up_lensing/tutorial_2_multi_galaxy.py +++ b/scripts/chapter_4_scaling_up_lensing/tutorial_2_multi_galaxy.py @@ -204,6 +204,24 @@ """ print(model.info) +""" +Drawing the model shows the multi-galaxy regime at a glance. The figure is the **map** of the model's structure +and the `info` is the **legend** of its priors; here it is the map that carries the news. + +Inside the `galaxies` frame there are now three cards rather than two: `lens_0 · Galaxy` and `lens_1 · Galaxy`, +both at `redshift = 0.5`, above the single `source · Galaxy` at `redshift = 1.0`. Each deflector card holds its +own `mass · Isothermal` with three white pills — `centre`, `ell_comps`, `einstein_radius` — two independent, +fully free mass models. That is the defining feature of this regime, and the thing every earlier chapter lacked. + +Each galaxy's light is drawn as a `bulge · Basis` card wrapping a dashed **plate**: one `Gaussian` card badged +`20 components`, standing for all twenty Gaussians of the MGE, with `centre` and `ell_comps` carrying blue +`shared across group` badges, `sigma` reading `fixed, varies by member` and `intensity` dashed as +`intensity · solved`. The footer does the accounting: `22 unique sampled scalars` — the same number the +`prior_count` prints below — alongside `63 fixed leaf slots`, `60 parameters solved during fitting` and +`3 plates standing for 60 components`. +""" +af.ModelPlotter(model).figure() + print(f"Free parameters (free mass centres): {model.prior_count}") """ @@ -272,6 +290,15 @@ """ print(model.info) +""" +The map is the cleanest way to see what just changed. The two `mass · Isothermal` cards' `centre` pills have +turned grey — fixed to the measured light centres, no longer sampled — and nothing else in the figure has moved. +The footer falls from `22 unique sampled scalars` to `18`, with `67 fixed leaf slots` where there were `63`. + +Four grey pills, four dimensions gone. That is the standard trick of the multi-galaxy regime, drawn. +""" +af.ModelPlotter(model).figure() + print(f"Free parameters (fixed mass centres): {model.prior_count}") """ @@ -436,6 +463,16 @@ print(model.info) +""" +With a third deflector the map simply grows a third card: `lens_0`, `lens_1` and `lens_2 · Galaxy`, each an +identical copy of the structure above, each with its grey fixed mass `centre`. The footer reads `25 unique +sampled scalars` and `4 plates standing for 80 components`. + +Nothing about the *shape* of the model changed when we added a galaxy, which is exactly the point the list-based +API is making. The map grows by one card per deflector, and the parameter count by seven. +""" +af.ModelPlotter(model).figure() + print(f"Free parameters (three deflectors, fixed mass centres): {model.prior_count}") """ diff --git a/scripts/chapter_4_scaling_up_lensing/tutorial_3_scaling_relation.py b/scripts/chapter_4_scaling_up_lensing/tutorial_3_scaling_relation.py index 0b4059f..435c8a8 100644 --- a/scripts/chapter_4_scaling_up_lensing/tutorial_3_scaling_relation.py +++ b/scripts/chapter_4_scaling_up_lensing/tutorial_3_scaling_relation.py @@ -346,6 +346,27 @@ print(model.info) +""" +This is the model whose map is worth the most, because the scaling relation is a piece of structure the `info` +can only describe in words. The figure is the **map** — what the model is made of and how its parts are tied +together — and the `info` is the **legend** of priors and values. + +Two top-level frames again, but not the pair from tutorial 1: `galaxies` holds `lens · Galaxy` (the anchor) and +`source · Galaxy`, and below it a frame titled `scaling_galaxies` holds the tied member as `0 · Galaxy`. All +three galaxies wear a `bulge · Basis` plate, a `Gaussian` card badged `10 components`; on the two lens galaxies +its caption reads `Gaussian with priors centre fixed, ell_comps ⇄ shared, sigma fixed (varies by member), +intensity solved`, which is the MGE costing two free parameters apiece, exactly as claimed above. + +Now look at the tied galaxy's `mass · IsothermalSph` card. Where the anchor has a plain white `einstein_radius` +pill, the tied galaxy has an amber **expression** pill spelling the relation out in full — +`einstein_radius = galaxies.lens.mass.einstein_radius * ` — with an amber dashed line running +across the figure from the anchor's `einstein_radius` pill to it. The figure's legend names this state +`relation (expression shown)`, and it is a state no amount of reading priors can reveal: the tied radius has no +prior, because it is not a parameter. The footer's `11 unique sampled scalars` counts the anchor's radius once +and the tied galaxy's not at all. +""" +af.ModelPlotter(model).figure() + """ __Parameter Counts__ diff --git a/scripts/chapter_4_scaling_up_lensing/tutorial_4_group_scale.py b/scripts/chapter_4_scaling_up_lensing/tutorial_4_group_scale.py index a522a6c..fa94eb4 100644 --- a/scripts/chapter_4_scaling_up_lensing/tutorial_4_group_scale.py +++ b/scripts/chapter_4_scaling_up_lensing/tutorial_4_group_scale.py @@ -420,6 +420,24 @@ """ print(model.info) +""" +The figure is the **map** of this model and the `info` is its **legend**; at group scale the map begins doing +real work, because the model now contains a population rather than a handful of named galaxies. + +The `galaxies` frame holds the two individually-modelled cards, `lens · Galaxy` (the BGG, with its +`bulge · SersicSph` and `mass · IsothermalSph`) and `source · Galaxy`. Beside them the `extra_galaxies` frame +holds not two cards but one dashed **plate**: a `Galaxy` card badged `2 components` and subtitled +`0 - 1 · redshift = 0.5`, standing for both members at once. The plate's pills say what the members have in +common and how they differ — `centre · fixed, varies by member` (every member's centre is fixed, each to its own +value), and `effective_radius`, `sersic_index` and `sigma` each badged `independent`, which the figure's legend +glosses as *one prior per member*. The `r_core`, `r_cut`, `redshift_object`, `redshift_source`, `H0` and `Om0` +pills are grey: the pinned dPIE constants. + +The footer reads `19 unique sampled scalars` — the N=19 the line below prints — with `1 plate standing for 2 +components`. Hold on to that number and that plate: the next model changes both. +""" +af.ModelPlotter(model).figure() + print(f"Total free parameters: {model.prior_count}") """ @@ -554,6 +572,24 @@ """ print(model.info) +""" +The map records the change precisely. The tied members have moved out of the `extra_galaxies` frame into one +titled `scaling_galaxies`, and they are now drawn as two separate cards, `0 · Galaxy` and `1 · Galaxy`, rather +than as a single plate — a plate stands for components that share one description, and these two no longer do. +On each card's `mass · dPIEMassSph` the `sigma` pill is no longer an `independent` prior but an amber +**expression** pill, and both expressions name the same prior: +`sigma = scaling_galaxies.1.mass.sigma.sigma_ref * `, with a different ratio on each card. One +free prior, two galaxies — the scaling relation, drawn. + +The `r_cut` pills stay grey, as they were before. `r_cut_ref * luminosity_ratio ** 0.7` is arithmetic on plain +numbers rather than on a prior, so it yields a fixed value, and a fixed value is a legend entry, not a relation. + +The footer falls from `19 unique sampled scalars` to `18`. One parameter is an unremarkable saving for two +members, but the map shows why the saving keeps growing: however many cards you add to the `scaling_galaxies` +frame, every one of their `sigma` pills points back at that single `sigma_ref`. +""" +af.ModelPlotter(model).figure() + print(f"Total free parameters: {model.prior_count}") """ diff --git a/scripts/chapter_4_scaling_up_lensing/tutorial_5_cluster_scale.py b/scripts/chapter_4_scaling_up_lensing/tutorial_5_cluster_scale.py index 973fbdf..c936e4e 100644 --- a/scripts/chapter_4_scaling_up_lensing/tutorial_5_cluster_scale.py +++ b/scripts/chapter_4_scaling_up_lensing/tutorial_5_cluster_scale.py @@ -415,6 +415,29 @@ print(model.info) +""" +This figure is the punchline of the chapter, and it repays reading slowly. The map shows the model's structure; +the `info` above is the legend of its priors. + +Each of the four tiers described above is drawn as its own object. The `galaxies` frame opens with a dashed +**plate** — `Galaxy` badged `2 components`, subtitled `lens_0 · lens_1 · redshift = 0.5` — standing for both +main galaxies at once: on its `mass · dPIEMassSph` card, `centre · fixed, varies by member` is grey, `sigma` and +`r_cut` are badged `independent` (one free prior per member), and `r_core`, the two redshifts and `H0` / `Om0` +are grey pinned constants. Below it the host halo appears as an ordinary card, `host_halo · Galaxy`, holding +`dark · NFWMCRLudlowSph` with exactly one white pill, `mass_at_200`. Below that a second plate, `Galaxy` badged +`2 components` and subtitled `source_0 · source_1`, holds `point_0 · PointSolved` whose only pill is the dashed +`centre · solved` — the source centres solved for rather than sampled. + +The second top-level frame, `scaling_galaxies`, holds the member tier: a plate badged `9 components` and one +card, `9 · Galaxy`, standing apart from it, and on both the `sigma` pill is an amber expression reading +`sigma = scaling_galaxies.9.mass.sigma.scaling_sigma_ref * `. Ten members, one free prior. + +Now read the footer: `6 unique sampled scalars`, `113 fixed leaf slots`, `2 parameters solved during fitting` +and `3 plates standing for 13 components`. Thirteen galaxies and two sources, six dimensions. The `info` needs +a hundred lines to say that; the map says it in one picture, and the six white pills are countable at a glance. +""" +af.ModelPlotter(model).figure() + """ __Analysis + Factor Graph__ diff --git a/scripts/chapter_4_scaling_up_lensing/tutorial_6_weak_lensing.py b/scripts/chapter_4_scaling_up_lensing/tutorial_6_weak_lensing.py index 755cfd6..2841a02 100644 --- a/scripts/chapter_4_scaling_up_lensing/tutorial_6_weak_lensing.py +++ b/scripts/chapter_4_scaling_up_lensing/tutorial_6_weak_lensing.py @@ -333,6 +333,23 @@ print(model.info) +""" +The map of a weak-lensing model is the smallest in the series, and that is the point of drawing it. The figure +shows the model's structure; the `info` above lists its priors. + +Inside the `galaxies` frame, `lens · Galaxy` at `redshift = 0.5` holds a single `mass · Isothermal` card with +three white pills — `centre`, `ell_comps` and `einstein_radius` — and `source · Galaxy` at `redshift = 1.0` +holds nothing at all: its card carries the line `no parameters` where every other source in this series has had +a light profile. In weak lensing the source galaxies are the *data*, a catalogue of measured shapes, so there is +no source light to fit. The footer reads `5 unique sampled scalars`, against the twenty-plus of the imaging +tutorials. + +Set this map beside the cluster model's from the previous tutorial and you have the two extremes of the chapter +in one comparison: fifteen galaxies in six dimensions there, two galaxies in five here — and in both cases the +figure tells you the dimensionality far faster than scrolling through `info` ever could. +""" +af.ModelPlotter(model).figure() + """ We fit the model with the nested sampling algorithm Nautilus, as throughout the series. A weak-lensing likelihood evaluation takes milliseconds (one shear-field evaluation at 1500 positions plus a chi-squared sum), so this