From 74c069be593bb37803866452fbd3a29156d33a30 Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Thu, 26 Nov 2026 15:04:55 +0000 Subject: [PATCH] Tutorial 4: introduce the HilbertSNR default Adds a section on why peak-normalised Hilbert weights struggle and how HilbertSNR's absolute-S/N point budget works (autolens_workspace#693). Co-Authored-By: Claude Opus 5.5 --- llms-full.txt | 2 +- .../tutorial_4_adaptive_reconstruction.ipynb | 63 +++++++++++++++++++ .../tutorial_4_adaptive_reconstruction.py | 52 +++++++++++++++ workspace_index.json | 1 + 4 files changed, 117 insertions(+), 1 deletion(-) diff --git a/llms-full.txt b/llms-full.txt index 8b6b69f..ef494d4 100644 --- a/llms-full.txt +++ b/llms-full.txt @@ -47,7 +47,7 @@ AUTO-GENERATED by PyAutoHands — do not edit by hand; regenerate with generate. - [Tutorial 3: Fit Problems](scripts/chapter_3_pixelizations/tutorial_3_fit_problems.py): Investigate why compact galaxies can challenge mesh resolution and constant regularization even with a known PSF. - Contents: When the best coefficient is not enough, Three progressively compact galaxies, Simulating the images, Reconstructing the galaxies, Finding the preferred regularization, Starting with the extended galaxy, Making the galaxy more compact, A very compact galaxy, Why the mesh becomes a limitation, Why one regularization coefficient becomes a compromise, Why maximizing the evidence does not remove the compromise, Wrap Up - [Tutorial 4: Adaptive Reconstruction](scripts/chapter_3_pixelizations/tutorial_4_adaptive_reconstruction.py): Combine Delaunay meshes, Hilbert brightness adaptation and adaptive Split regularization for directly observed galaxies. - - Contents: Adaptive galaxy reconstruction, Returning to the compact-galaxy problem, From image coordinates to a reconstruction mesh, Constructing the Delaunay galaxy mesh, Interpolating within a triangle, Regularization on an irregular mesh, Building an adapt image, Placing points with a Hilbert image mesh, Controlling the brightness weighting, Keeping resolution in the faint regions, Reconstructing with the Hilbert mesh, Comparing overlay and Hilbert reconstructions, Smoothing on an irregular mesh, Reconstructing with ConstantSplit, Allowing different smoothing strengths, Controlling the transition with `signal_scale`, Exploring the adaptive coefficients, Does adaptive regularization address the fit problem?, Wrap Up + - Contents: Adaptive galaxy reconstruction, Returning to the compact-galaxy problem, From image coordinates to a reconstruction mesh, Constructing the Delaunay galaxy mesh, Interpolating within a triangle, Regularization on an irregular mesh, Building an adapt image, Placing points with a Hilbert image mesh, Controlling the brightness weighting, Keeping resolution in the faint regions, The default image mesh: `HilbertSNR`, Reconstructing with the Hilbert mesh, Comparing overlay and Hilbert reconstructions, Smoothing on an irregular mesh, Reconstructing with ConstantSplit, Allowing different smoothing strengths, Controlling the transition with `signal_scale`, Exploring the adaptive coefficients, Does adaptive regularization address the fit problem?, Wrap Up - [Tutorial 5: Model Fit](scripts/chapter_3_pixelizations/tutorial_5_model_fit.py): Tutorials 3 and 4 explored mesh resolution and adaptive smoothing using a galaxy reconstructed entirely on a mesh. We now return to the complementary question of how to combine a smooth analytic component with a pixelization. The rectangular mesh here keeps that decomposition experiment simple; inspect its residuals and revisit the adaptive choices from Tutorial 4 when the pixelized component needs more spatially varying resolution. - Contents: Search 1: Fit the MGE, Search 2: Add the pixelization, Search 3: Refine both components, Wrap Up diff --git a/notebooks/chapter_3_pixelizations/tutorial_4_adaptive_reconstruction.ipynb b/notebooks/chapter_3_pixelizations/tutorial_4_adaptive_reconstruction.ipynb index 8499c9a..f0f5195 100644 --- a/notebooks/chapter_3_pixelizations/tutorial_4_adaptive_reconstruction.ipynb +++ b/notebooks/chapter_3_pixelizations/tutorial_4_adaptive_reconstruction.ipynb @@ -21,6 +21,7 @@ "- Placing points with a Hilbert image mesh\n", "- Controlling the brightness weighting\n", "- Keeping resolution in the faint regions\n", + "- The default image mesh: `HilbertSNR`\n", "- Reconstructing with the Hilbert mesh\n", "- Comparing overlay and Hilbert reconstructions\n", "- Smoothing on an irregular mesh\n", @@ -690,6 +691,68 @@ "\n", "Because the background can cover much more area than the galaxy light, even a modest floor can affect the allocation substantially. We should inspect the resulting mesh rather than interpret the floor as a fixed fraction of points assigned to faint regions.\n", "\n", + "Both `weight_power` and `weight_floor` act on weights divided by the **brightest** value in the adapt image. How the rest of the galaxy is sampled therefore depends on how bright that single peak is.\n", + "\n", + "---\n", + "\n", + "__The default image mesh: `HilbertSNR`__\n", + "\n", + "That dependence on the peak is a problem for real data. Suppose the galaxy contains a compact clump or bulge with a signal-to-noise (S/N) of 250, next to extended emission at S/N 30. With `weight_power=3.5` and `weight_floor=0.01`, any pixel below S/N ~67 receives the floor weight, so the well-detected extended emission is weighted like empty sky and receives few points. Lowering the power or raising the floor spreads points more evenly, but then the mesh stops concentrating on the galaxy at all.\n", + "\n", + "The default image mesh, `HilbertSNR`, keeps the Hilbert path but shares out the points differently:\n", + "\n", + "- 25% are spread evenly over the mask, so the background is always covered.\n", + "- 70% follow the galaxy's **absolute** S/N through a soft curve: zero at or below S/N 2, which is noise, rising through S/N 5 and flattening out for bright pixels. Faint detected emission always receives points, and the brightest regions cannot take them all.\n", + "- 5% go to the region near the brightest S/N, giving a compact bright feature extra resolution.\n", + "- No image pixel receives more than 8 points, so a faint galaxy does not have its points piled onto a few pixels.\n", + "\n", + "Because the weighting uses absolute S/N, it expects an S/N map rather than an image in flux units. S/N is the natural input: it is unit-free and comparable between datasets, whereas an image's values depend on its brightness units and exposure. Here the galaxy is the only emission, so its S/N map is the data divided by the noise map. In a model fit with several galaxies, the S/N map of each galaxy is the data with every other galaxy's model subtracted, divided by the noise map; `ag.AdaptImages.from_result` computes these maps from a previous fit and builds the mesh and the adaptive regularization input from them in one call." + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "\n", + "snr_map = ag.Array2D(\n", + " values=np.asarray(dataset.data.slim) / np.asarray(dataset.noise_map.slim),\n", + " mask=dataset.mask,\n", + ")\n", + "\n", + "snr_image_mesh = ag.image_mesh.HilbertSNR(pixels=hilbert_pixels)\n", + "\n", + "snr_image_plane_mesh_grid = snr_image_mesh.image_plane_mesh_grid_from(\n", + " mask=dataset.mask,\n", + " adapt_data=snr_map,\n", + ")\n", + "\n", + "aplt.plot_array(array=snr_map, title=\"Signal-to-Noise Map\")\n", + "\n", + "for label, mesh_grid in [\n", + " (\"Hilbert: weight_power=1.0, weight_floor=0.05\", hilbert_image_plane_mesh_grid),\n", + " (\"HilbertSNR (default)\", snr_image_plane_mesh_grid),\n", + "]:\n", + " aplt.plot_array(\n", + " array=dataset.data,\n", + " grid=mesh_grid,\n", + " title=label,\n", + " )" + ], + "outputs": [], + "execution_count": null + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Compare where the two meshes place their points across the galaxy's bright centre, its fainter emission and the background.\n", + "\n", + "`HilbertSNR` has no `weight_power` or `weight_floor`. Its defaults were chosen on fits of HST and JWST lens datasets and checked on sources from a peak S/N of 5 to 1000. They can be changed (for example `background`, the evenly spread share), but in most fits they should not need to be.\n", + "\n", + "Two limits are worth knowing. A very faint galaxy, with only a few pixels above S/N 2, receives most of its points on those pixels. A very bright galaxy, whose detected emission is almost all past the flattening of the soft curve, receives an even spread of points across it.\n", + "\n", + "For the fixed experiments in the rest of this tutorial we keep the `weight_power` mesh, so the effect of each setting stays easy to see; the workspace's modelling examples use `HilbertSNR`.\n", + "\n", "For our first reconstruction, we return to the mesh already constructed with `weight_power=1.0` and `weight_floor=0.05`. These are starting choices to investigate, rather than settings we have established as optimal.\n", "\n", "---\n", diff --git a/scripts/chapter_3_pixelizations/tutorial_4_adaptive_reconstruction.py b/scripts/chapter_3_pixelizations/tutorial_4_adaptive_reconstruction.py index fa864c8..2110cd5 100644 --- a/scripts/chapter_3_pixelizations/tutorial_4_adaptive_reconstruction.py +++ b/scripts/chapter_3_pixelizations/tutorial_4_adaptive_reconstruction.py @@ -16,6 +16,7 @@ - Placing points with a Hilbert image mesh - Controlling the brightness weighting - Keeping resolution in the faint regions +- The default image mesh: `HilbertSNR` - Reconstructing with the Hilbert mesh - Comparing overlay and Hilbert reconstructions - Smoothing on an irregular mesh @@ -549,6 +550,57 @@ def fit_delaunay_overlay(coefficient): Because the background can cover much more area than the galaxy light, even a modest floor can affect the allocation substantially. We should inspect the resulting mesh rather than interpret the floor as a fixed fraction of points assigned to faint regions. +Both `weight_power` and `weight_floor` act on weights divided by the **brightest** value in the adapt image. How the rest of the galaxy is sampled therefore depends on how bright that single peak is. + +--- + +__The default image mesh: `HilbertSNR`__ + +That dependence on the peak is a problem for real data. Suppose the galaxy contains a compact clump or bulge with a signal-to-noise (S/N) of 250, next to extended emission at S/N 30. With `weight_power=3.5` and `weight_floor=0.01`, any pixel below S/N ~67 receives the floor weight, so the well-detected extended emission is weighted like empty sky and receives few points. Lowering the power or raising the floor spreads points more evenly, but then the mesh stops concentrating on the galaxy at all. + +The default image mesh, `HilbertSNR`, keeps the Hilbert path but shares out the points differently: + +- 25% are spread evenly over the mask, so the background is always covered. +- 70% follow the galaxy's **absolute** S/N through a soft curve: zero at or below S/N 2, which is noise, rising through S/N 5 and flattening out for bright pixels. Faint detected emission always receives points, and the brightest regions cannot take them all. +- 5% go to the region near the brightest S/N, giving a compact bright feature extra resolution. +- No image pixel receives more than 8 points, so a faint galaxy does not have its points piled onto a few pixels. + +Because the weighting uses absolute S/N, it expects an S/N map rather than an image in flux units. S/N is the natural input: it is unit-free and comparable between datasets, whereas an image's values depend on its brightness units and exposure. Here the galaxy is the only emission, so its S/N map is the data divided by the noise map. In a model fit with several galaxies, the S/N map of each galaxy is the data with every other galaxy's model subtracted, divided by the noise map; `ag.AdaptImages.from_result` computes these maps from a previous fit and builds the mesh and the adaptive regularization input from them in one call. +""" + +snr_map = ag.Array2D( + values=np.asarray(dataset.data.slim) / np.asarray(dataset.noise_map.slim), + mask=dataset.mask, +) + +snr_image_mesh = ag.image_mesh.HilbertSNR(pixels=hilbert_pixels) + +snr_image_plane_mesh_grid = snr_image_mesh.image_plane_mesh_grid_from( + mask=dataset.mask, + adapt_data=snr_map, +) + +aplt.plot_array(array=snr_map, title="Signal-to-Noise Map") + +for label, mesh_grid in [ + ("Hilbert: weight_power=1.0, weight_floor=0.05", hilbert_image_plane_mesh_grid), + ("HilbertSNR (default)", snr_image_plane_mesh_grid), +]: + aplt.plot_array( + array=dataset.data, + grid=mesh_grid, + title=label, + ) + +r""" +Compare where the two meshes place their points across the galaxy's bright centre, its fainter emission and the background. + +`HilbertSNR` has no `weight_power` or `weight_floor`. Its defaults were chosen on fits of HST and JWST lens datasets and checked on sources from a peak S/N of 5 to 1000. They can be changed (for example `background`, the evenly spread share), but in most fits they should not need to be. + +Two limits are worth knowing. A very faint galaxy, with only a few pixels above S/N 2, receives most of its points on those pixels. A very bright galaxy, whose detected emission is almost all past the flattening of the soft curve, receives an even spread of points across it. + +For the fixed experiments in the rest of this tutorial we keep the `weight_power` mesh, so the effect of each setting stays easy to see; the workspace's modelling examples use `HilbertSNR`. + For our first reconstruction, we return to the mesh already constructed with `weight_power=1.0` and `weight_floor=0.05`. These are starting choices to investigate, rather than settings we have established as optimal. --- diff --git a/workspace_index.json b/workspace_index.json index 6bfd455..847d30b 100644 --- a/workspace_index.json +++ b/workspace_index.json @@ -316,6 +316,7 @@ "Placing points with a Hilbert image mesh", "Controlling the brightness weighting", "Keeping resolution in the faint regions", + "The default image mesh: `HilbertSNR`", "Reconstructing with the Hilbert mesh", "Comparing overlay and Hilbert reconstructions", "Smoothing on an irregular mesh",