From ff4259abf6ea70cd252545872adcb4e81db281d2 Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Thu, 3 Sep 2026 14:50:32 -0400 Subject: [PATCH] docs: tutorial_2_mappers draws polygons in both planes; dead index sections fixed (image-source-mappings P3, autolens_workspace#525) tutorial_2_mappers.py: drop the "SLIGHTLY BUGGY" warning; replace the four dead `slim_indexes_for_pix_indexes` blocks with `mapper.mappings_from` and draw the resulting `Mapping` objects via `subplot_image_and_mapper(regions=)`, so each source-plane cell and the image-plane regions it maps to share a colour. The point-level `indexes=` block now actually plots, in both planes. Hard-coded mesh indexes are clamped with `min(index, mapper.pixels - 1)`. tutorial_3_inversions.py: the same dead block now draws the five random mappings it always described. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016o9DxkjAvvpRAApQhRQBEM --- .../tutorial_2_mappers.ipynb | 224 +++++++++++------- .../tutorial_3_inversions.ipynb | 24 +- .../tutorial_2_mappers.py | 192 +++++++++------ .../tutorial_3_inversions.py | 18 +- 4 files changed, 291 insertions(+), 167 deletions(-) diff --git a/notebooks/chapter_3_pixelizations/tutorial_2_mappers.ipynb b/notebooks/chapter_3_pixelizations/tutorial_2_mappers.ipynb index 205bf2f..2e185f8 100644 --- a/notebooks/chapter_3_pixelizations/tutorial_2_mappers.ipynb +++ b/notebooks/chapter_3_pixelizations/tutorial_2_mappers.ipynb @@ -12,8 +12,6 @@ "\n", "Therefore, in this tutorial, we'll cover mappers in more detail.\n", "\n", - "WARNING: THIS TUTORIAL'S VISUALS ARE SLIGHTLY BUGGY CURRENTLY AND WILL BE FIXED IN THE FUTURE.\n", - "\n", "__Contents__\n", "\n", "- **Initial Setup:** we'll use new strong lensing data, where.\n", @@ -71,7 +69,8 @@ "\n", "from pathlib import Path\n", "import autolens as al\n", - "import autolens.plot as aplt" + "import autolens.plot as aplt\n", + "import autoarray.plot as aaplt" ], "outputs": [], "execution_count": null @@ -213,9 +212,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We first plot the mapper's pixelization, which is a grid of rectangular pixels in the source-plane.\n", - "\n", - "We plot this next to the image using the `subplot_image_and_mapper` method." + "Lets look at the two things a mapper holds. In the image-plane it has the (y,x) coordinate of every image-pixel it\n", + "uses, and in the source-plane it has the rectangular mesh those coordinates land on after being ray-traced by the\n", + "tracer." ] }, { @@ -234,28 +233,43 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Using the `positions=` overlay input we are also going to highlight specific grid coordinates in certain colors, such\n", - "that we can see how they map from the image-plane to the source-plane and vice versa.\n", + "The mapper's job is to pair those two things together. Before we look at how it pairs whole source pixels, it helps\n", + "to see the simpler, point-level version: take a handful of image-pixel coordinates and follow the *same* coordinates\n", + "into the source-plane.\n", "\n", - "We do this by specifying their integer indexes, corresponding to the index of each data point in the image and source\n", - "plane grids. These indexes are used to highlight the grid coordinates in the image and source-plane grids that map\n", - "directly to one another." + "We do this by giving the `indexes=` input a list of index groups, where each group is drawn in its own colour. The\n", + "same colour therefore marks the same coordinates in both planes." ] }, { "cell_type": "code", "metadata": {}, "source": [ + "total_points = len(mapper.image_plane_data_grid)\n", + "\n", + "side = int(round(total_points**0.5))\n", + "\n", + "centre = side * (side // 2) + side // 2\n", + "\n", "indexes = [\n", - " [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],\n", - " [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000],\n", + " [index for index in range(0, 10) if index < total_points],\n", + " [\n", + " centre + step * side\n", + " for step in range(-5, 5)\n", + " if 0 <= centre + step * side < total_points\n", + " ],\n", "]\n", "\n", - "\n", - "aplt.plot_array(\n", - " array=dataset.data, title=\"Image\", positions=mapper.image_plane_data_grid\n", + "aaplt.plot_grid(\n", + " grid=mapper.image_plane_data_grid,\n", + " indexes=indexes,\n", + " title=\"Image-Plane Data Grid\",\n", ")\n", - "aplt.plot_grid(grid=mapper.source_plane_mesh_grid, title=\"Source-Plane Mesh Grid\")" + "aaplt.plot_grid(\n", + " grid=mapper.source_plane_data_grid,\n", + " indexes=indexes,\n", + " title=\"Source-Plane Data Grid (Traced)\",\n", + ")" ], "outputs": [], "execution_count": null @@ -264,57 +278,44 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "It can help to plot the image-plane grid and source-plane grid over each image, in order to see how the\n", - "mapped pixels relate to the image and source-plane grids overall.\n", + "The red points, the first ten image-pixels at the very corner of the image, barely move: they are far from the lens\n", + "galaxy, so they are hardly deflected. The green points run vertically through the centre of the image, straight\n", + "through the lens galaxy, and are dragged a long way -- in the source-plane they no longer form a straight line, and\n", + "they pile up near the centre.\n", "\n", - "This requires us to plot each image individually (e.g. not using `subplot_image_and_mapper`), so we can plot each\n", - "grid via the visuals object and have them displayed clearly." - ] - }, - { - "cell_type": "code", - "metadata": {}, - "source": [ + "This is only half the story though, because it tells us where individual coordinates go, not which *source pixel*\n", + "they end up inside. That is what a mapper actually stores, and it is what we look at next.\n", "\n", + "We can now make the mapping appear the other way round. That is, we can input a source-pixel index (of our\n", + "rectangular mesh) and ask the mapper which image-pixels land inside it.\n", "\n", - "aplt.plot_array(array=dataset.data, title=\"Image\")\n", + "`mapper.mappings_from` answers exactly this. It takes a list of source-pixel index *groups* and returns one `Mapping`\n", + "per group. Each `Mapping` carries:\n", "\n", + " - `source_contours`: the outline of the source-plane cell(s) in the group.\n", + " - `image_contours`: the outlines of the connected regions of image-pixels which map into that group.\n", "\n", - "aplt.plot_grid(grid=mapper.source_plane_mesh_grid, title=\"Source-Plane Mesh Grid\")" - ], - "outputs": [], - "execution_count": null - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We can now make these mappings appear the other way round. That is, we can input a source-pixel index (of our\n", - "rectangular grid) and highlight how all of the image-pixels that it contains map to the image-plane.\n", - "\n", - "To make the indexes appear in the image-plane, we have to convert them from their source-plane pixel indexes\n", - "to image plane image-pixel indexes using the mapper.\n", - "\n", - "Lets map source pixel 312, the central source-pixel, to the image sub-grid and then plot it via the visuals object.\n", + "Both are polygons in arc-seconds, so we can draw them straight onto the image and onto the source-plane, in the same\n", + "colour. That colour is the whole point: it says \"this source region produces these image regions\".\n", "\n", - "The pixels, plotted in red, extended beyond the central square pixel of the source-plane grid. This is because\n", - "the pairing of data pixels to source pixels is not one-to-one, as an interpolation scheme is used to map pixels\n", - "which land near the edges of the source-pixel, but outside them, to that source pixel with a weight." + "Lets map source pixel 312, the central source-pixel, and draw it in both planes. We clamp the index with\n", + "`min(index, mapper.pixels - 1)` so the tutorial still runs if you shrink the mesh." ] }, { "cell_type": "code", "metadata": {}, "source": [ - "pix_indexes = [[312]]\n", + "pix_indexes = [[min(312, mapper.pixels - 1)]]\n", "\n", - "indexes = mapper.slim_indexes_for_pix_indexes(pix_indexes=pix_indexes)\n", + "mappings = mapper.mappings_from(pix_indexes=pix_indexes)\n", "\n", - "\n", - "aplt.plot_array(\n", - " array=dataset.data, title=\"Image\", positions=mapper.image_plane_data_grid\n", - ")\n", - "aplt.plot_grid(grid=mapper.source_plane_mesh_grid, title=\"Source-Plane Mesh Grid\")" + "aaplt.subplot_image_and_mapper(\n", + " mapper=mapper,\n", + " image=dataset.data,\n", + " regions=mappings,\n", + " mesh_grid=mapper.source_plane_mesh_grid,\n", + ")" ], "outputs": [], "execution_count": null @@ -323,25 +324,37 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "There we have it, multiple imaging in all its glory. \n", + "There we have it, multiple imaging in all its glory.\n", + "\n", + "On the right, a single small red rectangle: one cell of the source-plane mesh. On the left, that same red appears in\n", + "two or more *separate* regions of the image, sitting on the lensed source's ring. The lens galaxy's mass has taken\n", + "one patch of source and produced several images of it, and the mapper knows which image-pixels belong to which.\n", "\n", - "Try changing the source-pixel indexes of the line below. This will give you a feel for how different regions of the \n", - "source-plane map to the image." + "Notice also that the red regions in the image-plane are wider than a strict \"four image pixels per source pixel\"\n", + "footprint would be. This is because the pairing of image-pixels to source-pixels is not one-to-one: a bilinear\n", + "interpolation scheme is used, so an image-pixel which lands near the edge of a source-pixel, but outside it, is still\n", + "paired with that source-pixel with a weight.\n", + "\n", + "Try changing the source-pixel indexes below. This will give you a feel for how different regions of the source-plane\n", + "map to the image." ] }, { "cell_type": "code", "metadata": {}, "source": [ - "pix_indexes = [[312, 318], [412]]\n", - "\n", - "indexes = mapper.slim_indexes_for_pix_indexes(pix_indexes=pix_indexes)\n", + "pix_indexes = [\n", + " [min(index, mapper.pixels - 1) for index in group] for group in [[312, 313], [412]]\n", + "]\n", "\n", + "mappings = mapper.mappings_from(pix_indexes=pix_indexes)\n", "\n", - "aplt.plot_array(\n", - " array=dataset.data, title=\"Image\", positions=mapper.image_plane_data_grid\n", - ")\n", - "aplt.plot_grid(grid=mapper.source_plane_mesh_grid, title=\"Source-Plane Mesh Grid\")" + "aaplt.subplot_image_and_mapper(\n", + " mapper=mapper,\n", + " image=dataset.data,\n", + " regions=mappings,\n", + " mesh_grid=mapper.source_plane_mesh_grid,\n", + ")" ], "outputs": [], "execution_count": null @@ -350,32 +363,43 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Okay, so I think we can agree, mapper's map things! More specifically, they map source-plane pixels to multiple pixels \n", + "Two groups, two colours. The first group is two neighbouring source pixels, so red covers twice the area of the\n", + "source-plane and correspondingly wider arcs in the image. The second group is a single pixel further from the centre\n", + "of the source-plane, drawn in green, and its images land somewhere else entirely on the ring.\n", + "\n", + "This is the key insight of the whole chapter: *every* source pixel has a set of image regions like this, and the\n", + "collection of all of them is the \"mapping matrix\" that an inversion solves. Tutorial 3 puts that matrix to work.\n", + "\n", + "Okay, so I think we can agree, mapper's map things! More specifically, they map source-plane pixels to multiple pixels\n", "in the observed image of a strong lens.\n", "\n", "__Mask__\n", "\n", - "Finally, lets repeat the steps that we performed above, but now using a masked image. By applying a `Mask2D`, the \n", - "mapper only maps image-pixels that are not removed by the mask. This removes the (many) image pixels at the edge of the \n", - "image, where the source is not present. These pixels also pad-out the source-plane, thus by removing them our \n", + "Finally, lets repeat the steps that we performed above, but now using a masked image. By applying a `Mask2D`, the\n", + "mapper only maps image-pixels that are not removed by the mask. This removes the (many) image pixels at the edge of the\n", + "image, where the source is not present. These pixels also pad-out the source-plane, thus by removing them our\n", "source-plane reduces in size.\n", "\n", - "Lets just have a quick look at these edges pixels:" + "Lets first see why they are worth removing, by drawing source pixels near the edge of the mesh:" ] }, { "cell_type": "code", "metadata": {}, "source": [ - "pix_indexes = [[0, 1, 2, 3, 4, 5, 6, 7], [620, 621, 622, 623, 624]]\n", - "\n", - "indexes = mapper.slim_indexes_for_pix_indexes(pix_indexes=pix_indexes)\n", + "pix_indexes = [\n", + " [min(index, mapper.pixels - 1) for index in group]\n", + " for group in [[600, 601, 602, 603, 604], [620, 621, 622, 623, 624]]\n", + "]\n", "\n", + "mappings = mapper.mappings_from(pix_indexes=pix_indexes)\n", "\n", - "aplt.plot_array(\n", - " array=dataset.data, title=\"Image\", positions=mapper.image_plane_data_grid\n", - ")\n", - "aplt.plot_grid(grid=mapper.source_plane_mesh_grid, title=\"Source-Plane Mesh Grid\")" + "aaplt.subplot_image_and_mapper(\n", + " mapper=mapper,\n", + " image=dataset.data,\n", + " regions=mappings,\n", + " mesh_grid=mapper.source_plane_mesh_grid,\n", + ")" ], "outputs": [], "execution_count": null @@ -384,6 +408,10 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "Both groups sit along the bottom edge of the source-plane mesh, and both map to a single blob in a corner of the\n", + "image, far from the lensed source, where there is no signal to reconstruct. They are wasted source pixels: the\n", + "inversion has to solve for them, but the data has nothing to say about them.\n", + "\n", "Lets use an annular `Mask2D`, which will capture the ring-like shape of the lensed source galaxy." ] }, @@ -474,23 +502,51 @@ "We can more clearly see the diamond of points in the centre of the source-plane (for those who have been reading up, \n", "this diamond is called the `caustic`).\n", "\n", - "Now lets plot some indexes mapping from the source-plane to the image-plane, which will highlight how the\n", - "source-pixels map to the image pixels." + "The source-plane is now only ~1\" across instead of ~7\", so every source pixel covers a much larger fraction of the\n", + "figure and the polygons we drew above are far easier to read. Lets draw four source pixels at once, each in its own\n", + "colour." ] }, { "cell_type": "code", "metadata": {}, "source": [ - "pix_indexes = [[312], [314], [316], [318]]\n", + "pix_indexes = [\n", + " [min(index, mapper.pixels - 1)] for index in [312, 314, 316, 318]\n", + "]\n", "\n", - "indexes = mapper.slim_indexes_for_pix_indexes(pix_indexes=pix_indexes)\n", + "mappings = mapper.mappings_from(pix_indexes=pix_indexes)\n", "\n", + "aaplt.subplot_image_and_mapper(\n", + " mapper=mapper,\n", + " image=dataset.data,\n", + " regions=mappings,\n", + " mesh_grid=mapper.source_plane_mesh_grid,\n", + ")" + ], + "outputs": [], + "execution_count": null + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Four source pixels, four colours, and in the image-plane each colour picks out its own short segments of the ring,\n", + "on opposite sides of it. Source pixels which are neighbours in the source-plane produce image regions which are\n", + "neighbours along each arc -- the lensing is smooth, and the mapper preserves that.\n", "\n", - "aplt.plot_array(\n", - " array=dataset.data, title=\"Image\", positions=mapper.image_plane_data_grid\n", - ")\n", - "aplt.plot_grid(grid=mapper.source_plane_mesh_grid, title=\"Source-Plane Mesh Grid\")" + "The number of image-plane regions each source pixel maps to is the number of multiple images it produces (the mask\n", + "can split one arc into more than one region, so treat these counts as a lower bound on the physical multiplicity):" + ] + }, + { + "cell_type": "code", + "metadata": {}, + "source": [ + "for mapping in mappings:\n", + " print(\n", + " f\"Source pixel {mapping.pix_indexes} maps to {len(mapping.image_regions)} image-plane regions.\"\n", + " )" ], "outputs": [], "execution_count": null diff --git a/notebooks/chapter_3_pixelizations/tutorial_3_inversions.ipynb b/notebooks/chapter_3_pixelizations/tutorial_3_inversions.ipynb index 3061c81..17ac577 100644 --- a/notebooks/chapter_3_pixelizations/tutorial_3_inversions.ipynb +++ b/notebooks/chapter_3_pixelizations/tutorial_3_inversions.ipynb @@ -73,7 +73,8 @@ "\n", "from pathlib import Path\n", "import autolens as al\n", - "import autolens.plot as aplt" + "import autolens.plot as aplt\n", + "import autoarray.plot as aaplt" ], "outputs": [], "execution_count": null @@ -416,22 +417,27 @@ "overly-simplified. To be good at lens modeling you do not need to understand the details of how an inversion works, you \n", "simply need to be able to use an inversion to model a strong lens. \n", "\n", - "To begin, lets consider some random mappings between our mapper`s source-pixels and the image." + "To begin, lets consider some random mappings between our mapper`s source-pixels and the image.\n", + "\n", + "`mapper.mappings_from` returns one `Mapping` per group of source-pixels, which we pass straight to\n", + "`subplot_image_and_mapper` as `regions=`. Each mapping is drawn in its own colour: the source-plane cell on the right,\n", + "and the image-plane regions it maps to on the left. Tutorial 2 covers this in detail." ] }, { "cell_type": "code", "metadata": {}, "source": [ - "pix_indexes = [[445], [285], [313], [132], [11]]\n", - "\n", - "indexes = mapper.slim_indexes_for_pix_indexes(pix_indexes=pix_indexes)\n", + "pix_indexes = [[min(index, mapper.pixels - 1)] for index in [445, 285, 313, 132, 211]]\n", "\n", + "mappings = mapper.mappings_from(pix_indexes=pix_indexes)\n", "\n", - "aplt.plot_array(\n", - " array=dataset.data, title=\"Image\", positions=mapper.image_plane_data_grid\n", - ")\n", - "aplt.plot_grid(grid=mapper.source_plane_mesh_grid, title=\"Source-Plane Mesh Grid\")" + "aaplt.subplot_image_and_mapper(\n", + " mapper=mapper,\n", + " image=dataset.data,\n", + " regions=mappings,\n", + " mesh_grid=mapper.source_plane_mesh_grid,\n", + ")" ], "outputs": [], "execution_count": null diff --git a/scripts/chapter_3_pixelizations/tutorial_2_mappers.py b/scripts/chapter_3_pixelizations/tutorial_2_mappers.py index ff270f3..146937c 100644 --- a/scripts/chapter_3_pixelizations/tutorial_2_mappers.py +++ b/scripts/chapter_3_pixelizations/tutorial_2_mappers.py @@ -7,8 +7,6 @@ Therefore, in this tutorial, we'll cover mappers in more detail. -WARNING: THIS TUTORIAL'S VISUALS ARE SLIGHTLY BUGGY CURRENTLY AND WILL BE FIXED IN THE FUTURE. - __Contents__ - **Initial Setup:** we'll use new strong lensing data, where. @@ -25,6 +23,7 @@ from pathlib import Path import autolens as al import autolens.plot as aplt +import autoarray.plot as aaplt """ __Initial Setup__ @@ -106,9 +105,9 @@ """ -We first plot the mapper's pixelization, which is a grid of rectangular pixels in the source-plane. - -We plot this next to the image using the `subplot_image_and_mapper` method. +Lets look at the two things a mapper holds. In the image-plane it has the (y,x) coordinate of every image-pixel it +uses, and in the source-plane it has the rectangular mesh those coordinates land on after being ray-traced by the +tracer. """ aplt.plot_array( array=dataset.data, title="Image", positions=mapper.image_plane_data_grid @@ -116,101 +115,141 @@ aplt.plot_grid(grid=mapper.source_plane_mesh_grid, title="Source-Plane Mesh Grid") """ -Using the `positions=` overlay input we are also going to highlight specific grid coordinates in certain colors, such -that we can see how they map from the image-plane to the source-plane and vice versa. +The mapper's job is to pair those two things together. Before we look at how it pairs whole source pixels, it helps +to see the simpler, point-level version: take a handful of image-pixel coordinates and follow the *same* coordinates +into the source-plane. -We do this by specifying their integer indexes, corresponding to the index of each data point in the image and source -plane grids. These indexes are used to highlight the grid coordinates in the image and source-plane grids that map -directly to one another. +We do this by giving the `indexes=` input a list of index groups, where each group is drawn in its own colour. The +same colour therefore marks the same coordinates in both planes. """ +total_points = len(mapper.image_plane_data_grid) + +side = int(round(total_points**0.5)) + +centre = side * (side // 2) + side // 2 + indexes = [ - [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], - [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000], + [index for index in range(0, 10) if index < total_points], + [ + centre + step * side + for step in range(-5, 5) + if 0 <= centre + step * side < total_points + ], ] - -aplt.plot_array( - array=dataset.data, title="Image", positions=mapper.image_plane_data_grid +aaplt.plot_grid( + grid=mapper.image_plane_data_grid, + indexes=indexes, + title="Image-Plane Data Grid", +) +aaplt.plot_grid( + grid=mapper.source_plane_data_grid, + indexes=indexes, + title="Source-Plane Data Grid (Traced)", ) -aplt.plot_grid(grid=mapper.source_plane_mesh_grid, title="Source-Plane Mesh Grid") """ -It can help to plot the image-plane grid and source-plane grid over each image, in order to see how the -mapped pixels relate to the image and source-plane grids overall. +The red points, the first ten image-pixels at the very corner of the image, barely move: they are far from the lens +galaxy, so they are hardly deflected. The green points run vertically through the centre of the image, straight +through the lens galaxy, and are dragged a long way -- in the source-plane they no longer form a straight line, and +they pile up near the centre. -This requires us to plot each image individually (e.g. not using `subplot_image_and_mapper`), so we can plot each -grid via the visuals object and have them displayed clearly. -""" +This is only half the story though, because it tells us where individual coordinates go, not which *source pixel* +they end up inside. That is what a mapper actually stores, and it is what we look at next. +We can now make the mapping appear the other way round. That is, we can input a source-pixel index (of our +rectangular mesh) and ask the mapper which image-pixels land inside it. -aplt.plot_array(array=dataset.data, title="Image") +`mapper.mappings_from` answers exactly this. It takes a list of source-pixel index *groups* and returns one `Mapping` +per group. Each `Mapping` carries: + - `source_contours`: the outline of the source-plane cell(s) in the group. + - `image_contours`: the outlines of the connected regions of image-pixels which map into that group. -aplt.plot_grid(grid=mapper.source_plane_mesh_grid, title="Source-Plane Mesh Grid") +Both are polygons in arc-seconds, so we can draw them straight onto the image and onto the source-plane, in the same +colour. That colour is the whole point: it says "this source region produces these image regions". +Lets map source pixel 312, the central source-pixel, and draw it in both planes. We clamp the index with +`min(index, mapper.pixels - 1)` so the tutorial still runs if you shrink the mesh. """ -We can now make these mappings appear the other way round. That is, we can input a source-pixel index (of our -rectangular grid) and highlight how all of the image-pixels that it contains map to the image-plane. +pix_indexes = [[min(312, mapper.pixels - 1)]] -To make the indexes appear in the image-plane, we have to convert them from their source-plane pixel indexes -to image plane image-pixel indexes using the mapper. +mappings = mapper.mappings_from(pix_indexes=pix_indexes) -Lets map source pixel 312, the central source-pixel, to the image sub-grid and then plot it via the visuals object. +aaplt.subplot_image_and_mapper( + mapper=mapper, + image=dataset.data, + regions=mappings, + mesh_grid=mapper.source_plane_mesh_grid, +) -The pixels, plotted in red, extended beyond the central square pixel of the source-plane grid. This is because -the pairing of data pixels to source pixels is not one-to-one, as an interpolation scheme is used to map pixels -which land near the edges of the source-pixel, but outside them, to that source pixel with a weight. """ -pix_indexes = [[312]] +There we have it, multiple imaging in all its glory. -indexes = mapper.slim_indexes_for_pix_indexes(pix_indexes=pix_indexes) +On the right, a single small red rectangle: one cell of the source-plane mesh. On the left, that same red appears in +two or more *separate* regions of the image, sitting on the lensed source's ring. The lens galaxy's mass has taken +one patch of source and produced several images of it, and the mapper knows which image-pixels belong to which. +Notice also that the red regions in the image-plane are wider than a strict "four image pixels per source pixel" +footprint would be. This is because the pairing of image-pixels to source-pixels is not one-to-one: a bilinear +interpolation scheme is used, so an image-pixel which lands near the edge of a source-pixel, but outside it, is still +paired with that source-pixel with a weight. -aplt.plot_array( - array=dataset.data, title="Image", positions=mapper.image_plane_data_grid -) -aplt.plot_grid(grid=mapper.source_plane_mesh_grid, title="Source-Plane Mesh Grid") - +Try changing the source-pixel indexes below. This will give you a feel for how different regions of the source-plane +map to the image. """ -There we have it, multiple imaging in all its glory. - -Try changing the source-pixel indexes of the line below. This will give you a feel for how different regions of the -source-plane map to the image. -""" -pix_indexes = [[312, 318], [412]] - -indexes = mapper.slim_indexes_for_pix_indexes(pix_indexes=pix_indexes) +pix_indexes = [ + [min(index, mapper.pixels - 1) for index in group] for group in [[312, 313], [412]] +] +mappings = mapper.mappings_from(pix_indexes=pix_indexes) -aplt.plot_array( - array=dataset.data, title="Image", positions=mapper.image_plane_data_grid +aaplt.subplot_image_and_mapper( + mapper=mapper, + image=dataset.data, + regions=mappings, + mesh_grid=mapper.source_plane_mesh_grid, ) -aplt.plot_grid(grid=mapper.source_plane_mesh_grid, title="Source-Plane Mesh Grid") """ -Okay, so I think we can agree, mapper's map things! More specifically, they map source-plane pixels to multiple pixels +Two groups, two colours. The first group is two neighbouring source pixels, so red covers twice the area of the +source-plane and correspondingly wider arcs in the image. The second group is a single pixel further from the centre +of the source-plane, drawn in green, and its images land somewhere else entirely on the ring. + +This is the key insight of the whole chapter: *every* source pixel has a set of image regions like this, and the +collection of all of them is the "mapping matrix" that an inversion solves. Tutorial 3 puts that matrix to work. + +Okay, so I think we can agree, mapper's map things! More specifically, they map source-plane pixels to multiple pixels in the observed image of a strong lens. __Mask__ -Finally, lets repeat the steps that we performed above, but now using a masked image. By applying a `Mask2D`, the -mapper only maps image-pixels that are not removed by the mask. This removes the (many) image pixels at the edge of the -image, where the source is not present. These pixels also pad-out the source-plane, thus by removing them our +Finally, lets repeat the steps that we performed above, but now using a masked image. By applying a `Mask2D`, the +mapper only maps image-pixels that are not removed by the mask. This removes the (many) image pixels at the edge of the +image, where the source is not present. These pixels also pad-out the source-plane, thus by removing them our source-plane reduces in size. -Lets just have a quick look at these edges pixels: +Lets first see why they are worth removing, by drawing source pixels near the edge of the mesh: """ -pix_indexes = [[0, 1, 2, 3, 4, 5, 6, 7], [620, 621, 622, 623, 624]] - -indexes = mapper.slim_indexes_for_pix_indexes(pix_indexes=pix_indexes) +pix_indexes = [ + [min(index, mapper.pixels - 1) for index in group] + for group in [[600, 601, 602, 603, 604], [620, 621, 622, 623, 624]] +] +mappings = mapper.mappings_from(pix_indexes=pix_indexes) -aplt.plot_array( - array=dataset.data, title="Image", positions=mapper.image_plane_data_grid +aaplt.subplot_image_and_mapper( + mapper=mapper, + image=dataset.data, + regions=mappings, + mesh_grid=mapper.source_plane_mesh_grid, ) -aplt.plot_grid(grid=mapper.source_plane_mesh_grid, title="Source-Plane Mesh Grid") """ +Both groups sit along the bottom edge of the source-plane mesh, and both map to a single blob in a corner of the +image, far from the lensed source, where there is no signal to reconstruct. They are wasted source pixels: the +inversion has to solve for them, but the data has nothing to say about them. + Lets use an annular `Mask2D`, which will capture the ring-like shape of the lensed source galaxy. """ mask = al.Mask2D.circular_annular( @@ -257,18 +296,35 @@ We can more clearly see the diamond of points in the centre of the source-plane (for those who have been reading up, this diamond is called the `caustic`). -Now lets plot some indexes mapping from the source-plane to the image-plane, which will highlight how the -source-pixels map to the image pixels. +The source-plane is now only ~1" across instead of ~7", so every source pixel covers a much larger fraction of the +figure and the polygons we drew above are far easier to read. Lets draw four source pixels at once, each in its own +colour. """ -pix_indexes = [[312], [314], [316], [318]] - -indexes = mapper.slim_indexes_for_pix_indexes(pix_indexes=pix_indexes) +pix_indexes = [ + [min(index, mapper.pixels - 1)] for index in [312, 314, 316, 318] +] +mappings = mapper.mappings_from(pix_indexes=pix_indexes) -aplt.plot_array( - array=dataset.data, title="Image", positions=mapper.image_plane_data_grid +aaplt.subplot_image_and_mapper( + mapper=mapper, + image=dataset.data, + regions=mappings, + mesh_grid=mapper.source_plane_mesh_grid, ) -aplt.plot_grid(grid=mapper.source_plane_mesh_grid, title="Source-Plane Mesh Grid") + +""" +Four source pixels, four colours, and in the image-plane each colour picks out its own short segments of the ring, +on opposite sides of it. Source pixels which are neighbours in the source-plane produce image regions which are +neighbours along each arc -- the lensing is smooth, and the mapper preserves that. + +The number of image-plane regions each source pixel maps to is the number of multiple images it produces (the mask +can split one arc into more than one region, so treat these counts as a lower bound on the physical multiplicity): +""" +for mapping in mappings: + print( + f"Source pixel {mapping.pix_indexes} maps to {len(mapping.image_regions)} image-plane regions." + ) """ __Wrap Up__ diff --git a/scripts/chapter_3_pixelizations/tutorial_3_inversions.py b/scripts/chapter_3_pixelizations/tutorial_3_inversions.py index 13d011e..ddee2cf 100644 --- a/scripts/chapter_3_pixelizations/tutorial_3_inversions.py +++ b/scripts/chapter_3_pixelizations/tutorial_3_inversions.py @@ -27,6 +27,7 @@ from pathlib import Path import autolens as al import autolens.plot as aplt +import autoarray.plot as aaplt """ __Initial Setup__ @@ -232,16 +233,21 @@ simply need to be able to use an inversion to model a strong lens. To begin, lets consider some random mappings between our mapper`s source-pixels and the image. -""" -pix_indexes = [[445], [285], [313], [132], [11]] -indexes = mapper.slim_indexes_for_pix_indexes(pix_indexes=pix_indexes) +`mapper.mappings_from` returns one `Mapping` per group of source-pixels, which we pass straight to +`subplot_image_and_mapper` as `regions=`. Each mapping is drawn in its own colour: the source-plane cell on the right, +and the image-plane regions it maps to on the left. Tutorial 2 covers this in detail. +""" +pix_indexes = [[min(index, mapper.pixels - 1)] for index in [445, 285, 313, 132, 211]] +mappings = mapper.mappings_from(pix_indexes=pix_indexes) -aplt.plot_array( - array=dataset.data, title="Image", positions=mapper.image_plane_data_grid +aaplt.subplot_image_and_mapper( + mapper=mapper, + image=dataset.data, + regions=mappings, + mesh_grid=mapper.source_plane_mesh_grid, ) -aplt.plot_grid(grid=mapper.source_plane_mesh_grid, title="Source-Plane Mesh Grid") """ These mappings are known before the inversion reconstructs the source galaxy, which means before this inversion is