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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
224 changes: 140 additions & 84 deletions notebooks/chapter_3_pixelizations/tutorial_2_mappers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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."
]
},
{
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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."
]
},
Expand Down Expand Up @@ -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
Expand Down
24 changes: 15 additions & 9 deletions notebooks/chapter_3_pixelizations/tutorial_3_inversions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading