{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Tutorial 5: Borders\n", "===================\n", "\n", "In the previous tutorials, the source-plane pixel grid perfectly mapped over the traced image-pixel $(y,x)$ coordinates\n", "in the source plane. If these pixels mapped to a larger area in the source plane, its pixel-grid would automatically\n", "increase its size so as to cover every source-plane coordinate.\n", "\n", "In this tutorial, we will consider how the size of the pixelization grid is chosen and introduce the concept of a\n", "border." ] }, { "cell_type": "code", "metadata": {}, "source": [ "%matplotlib inline\n", "from pyprojroot import here\n", "workspace_path = str(here())\n", "%cd $workspace_path\n", "print(f\"Working Directory has been set to `{workspace_path}`\")\n", "\n", "from os import path\n", "import autolens as al\n", "import autolens.plot as aplt" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Initial Setup__\n", "\n", "we'll use the same strong lensing data as the previous tutorial, where:\n", "\n", " - The lens galaxy's light is omitted.\n", " - The lens galaxy's total mass distribution is an `Isothermal` and `ExternalShear`.\n", " - The source galaxy's light is an `Sersic`." ] }, { "cell_type": "code", "metadata": {}, "source": [ "dataset_name = \"simple__no_lens_light\"\n", "dataset_path = path.join(\"dataset\", \"imaging\", dataset_name)\n", "\n", "dataset = al.Imaging.from_fits(\n", " data_path=path.join(dataset_path, \"data.fits\"),\n", " noise_map_path=path.join(dataset_path, \"noise_map.fits\"),\n", " psf_path=path.join(dataset_path, \"psf.fits\"),\n", " pixel_scales=0.1,\n", ")\n", "\n", "dataset_plotter = aplt.ImagingPlotter(dataset=dataset)\n", "dataset_plotter.subplot_dataset()" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Borders__\n", "\n", "So, what is a border? In the image-plane, a border is the set of exterior pixels in a mask that are at its border.\n", "\n", "Lets plot the image with a circular circular and tell our `ImagingPlotter` to plot the border." ] }, { "cell_type": "code", "metadata": {}, "source": [ "mask_circular = al.Mask2D.circular(\n", " shape_native=dataset.shape_native,\n", " pixel_scales=dataset.pixel_scales,\n", " sub_size=2,\n", " radius=2.5,\n", ")\n", "dataset = dataset.apply_mask(mask=mask_circular)\n", "\n", "include = aplt.Include2D(border=True)\n", "\n", "dataset_plotter = aplt.ImagingPlotter(dataset=dataset, include_2d=include)\n", "dataset_plotter.subplot_dataset()" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, for a circular mask the border *is* the edge of the mask (the ring of black dots we're used to \n", "seeing whenever we plot a mask. \n", "\n", "For an annular mask, pixels on its inner edge are not a part of the border, whereas those on its outer edge are." ] }, { "cell_type": "code", "metadata": {}, "source": [ "mask_annular = al.Mask2D.circular_annular(\n", " shape_native=dataset.shape_native,\n", " pixel_scales=dataset.pixel_scales,\n", " sub_size=2,\n", " inner_radius=0.8,\n", " outer_radius=2.5,\n", ")\n", "\n", "dataset = dataset.apply_mask(mask=mask_circular)\n", "\n", "dataset_plotter = aplt.ImagingPlotter(dataset=dataset, include_2d=include)\n", "dataset_plotter.subplot_dataset()" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "A border is therefore *only* the pixels at the exterior edge of a mask\n", "\n", "So, what does a border actually do? To show you, we'll need to fit this image with a lens model and inversion. We'll \n", "use the same convenience function we used in the previous tutorial (to perform a quick source galaxy fit) with the \n", "options to input a mask and use a border." ] }, { "cell_type": "code", "metadata": {}, "source": [ "\n", "\n", "def perform_fit_with_source_galaxy_mask_and_border(\n", " dataset, source_galaxy, mask, settings_inversion\n", "):\n", " dataset = dataset.apply_mask(mask=mask)\n", "\n", " lens_galaxy = al.Galaxy(\n", " redshift=0.5,\n", " mass=al.mp.Isothermal(\n", " centre=(0.0, 0.0),\n", " einstein_radius=1.6,\n", " ell_comps=al.convert.ell_comps_from(axis_ratio=0.9, angle=45.0),\n", " ),\n", " shear=al.mp.ExternalShear(gamma_1=0.05, gamma_2=0.05),\n", " )\n", "\n", " tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])\n", "\n", " return al.FitImaging(\n", " dataset=dataset, tracer=tracer, settings_inversion=settings_inversion\n", " )\n" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "Okay, so lets first look at the mapper without using a border and using annular mask.\n", "\n", "First, note how we set up the border, using a `al.SettingsInversion` object. This behaves analogously to the \n", "`SettingsImaging` and `SettingsLens` objects we have used in previous tutorials." ] }, { "cell_type": "code", "metadata": {}, "source": [ "pixelization = al.Pixelization(\n", " mesh=al.mesh.Rectangular(shape=(40, 40)),\n", " regularization=al.reg.Constant(coefficient=1.0),\n", ")\n", "\n", "source_galaxy = al.Galaxy(redshift=1.0, pixelization=pixelization)\n", "\n", "fit = perform_fit_with_source_galaxy_mask_and_border(\n", " dataset=dataset,\n", " source_galaxy=source_galaxy,\n", " mask=mask_annular,\n", " settings_inversion=al.SettingsInversion(relocate_pix_border=False),\n", ")\n", "\n", "include = aplt.Include2D(mapper_source_plane_data_grid=True)\n", "\n", "fit_plotter = aplt.FitImagingPlotter(fit=fit, include_2d=include)\n", "fit_plotter.figures_2d_of_planes(plane_index=1, plane_image=True)" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "Everything looks fine, we get a reconstructed source on a visually appeasing source-plane grid. So, why are we so \n", "worried about borders? Lets see what happens if we use a circular mask instead." ] }, { "cell_type": "code", "metadata": {}, "source": [ "fit = perform_fit_with_source_galaxy_mask_and_border(\n", " dataset=dataset,\n", " source_galaxy=source_galaxy,\n", " mask=mask_circular,\n", " settings_inversion=al.SettingsInversion(relocate_pix_border=False),\n", ")\n", "\n", "inversion_plotter = aplt.InversionPlotter(inversion=fit.inversion, include_2d=include)\n", "inversion_plotter.figures_2d_of_pixelization(pixelization_index=0, reconstruction=True)" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "Woah, whats happened? There are lots of additional $(y,x)$ coordinates in the source-plane grid, some of which trace \n", "to extremely large radii far away from the central regions of the source-plane! These points are the traced image-pixels \n", "that correspond to the central image-pixels that the annular mask removed (e.g. they were at radii with 0.8\" of the \n", "centre).\n", "\n", "Lets quickly check this by plotting the indexes of these image-pixels." ] }, { "cell_type": "code", "metadata": {}, "source": [ "visuals = aplt.Visuals2D(indexes=[986, 987, 988, 989, 990, 991])\n", "include = aplt.Include2D(mapper_source_plane_data_grid=True)\n", "\n", "mapper_plotter = aplt.MapperPlotter(\n", " mapper=fit.inversion.linear_obj_list[0],\n", " visuals_2d=visuals,\n", " include_2d=include,\n", ")\n", "\n", "mapper_plotter.subplot_image_and_mapper(image=fit.dataset.data)" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "So, why is this happening? What is the mass profile physically doing to create these source plane coordinates at \n", "extremely large radial values? \n", "\n", "Towards the centre of th elliptical isothermal mass profile, the density begins to rise very sharply, it becomes \n", "extremely steep or 'cuspy'. This cuspy behaviour towards its centre can cause extremely large deflection angles to be \n", "calculated:" ] }, { "cell_type": "code", "metadata": {}, "source": [ "tracer_plotter = aplt.TracerPlotter(tracer=fit.tracer, grid=fit.grid)\n", "tracer_plotter.figures_2d(deflections_y=True, deflections_x=True)" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "Central image pixel can therefore be subjected to 'demagnification', whereby they trace to extremely large values in \n", "the source plane! \n", "\n", "Physically, this is not a problem, and it is the reason we do not see a 'central image' in most strong lenses, as the \n", "light-rays which take this path through the centre of the lens are demagnified. However, if the lens galaxy had a less\n", "steep inner mass distribution (termed a 'core') we would see the central image.\n", "\n", "Demagnification is a problem for the pixelization and inversion though, which reconstruct the flux of these \n", "demagnified pixels just like the other pixels in the image-pixel. There are two negative consequences:\n", "\n", " 1) The rectangular pixel-grid that we 'lay over' the source-plane is very larger because it expands to include the \n", " demagnified image-pixels. As a result, larger source-pixels are used to reconstruct the central regions of the \n", " source-plane (where the source galaxy is actually located), meaning we reconstruct the source-galaxy at a lower \n", " effective resolution.\n", " \n", " 2) The inversion reconstructs the flux of the demanigified image pixels using source-pixels which contain *only* \n", " demagnified image pixels (these are the source pixels at the edge of the source plane). These source-pixels *should* \n", " have had other image-pixels traced within them via image-pixels at even larger radii from the centre of the lens \n", " galaxy. However, these image-pixels are at radii above 3.0\", meaning the circular mask removed them from the inversion.\n", "\n", "Lets quickly use a large circular mask to confirm that these pixels exist when we don't mask them." ] }, { "cell_type": "code", "metadata": {}, "source": [ "mask_circular_large = al.Mask2D.circular(\n", " shape_native=dataset.shape_native, pixel_scales=dataset.pixel_scales, radius=4.0\n", ")\n", "\n", "fit = perform_fit_with_source_galaxy_mask_and_border(\n", " dataset=dataset,\n", " source_galaxy=source_galaxy,\n", " mask=mask_circular,\n", " settings_inversion=al.SettingsInversion(relocate_pix_border=False),\n", ")\n", "\n", "fit_plotter = aplt.FitImagingPlotter(fit=fit, include_2d=include)\n", "fit_plotter.figures_2d_of_planes(plane_index=1, plane_image=True)" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "This second point is a *huge* problem and it can introduce extremely dangerous systematics into our source \n", "reconstruction and lens models. \n", "\n", "Borders are the solution to this problem. We simply take the mask's` border in the image-plane that we showed above, \n", "trace it to the source-plane and relocate all traced image-pixels pixels outside this source-plane border to its edge. \n", "Lets take a look:" ] }, { "cell_type": "code", "metadata": {}, "source": [ "fit = perform_fit_with_source_galaxy_mask_and_border(\n", " dataset=dataset,\n", " source_galaxy=source_galaxy,\n", " mask=mask_circular,\n", " settings_inversion=al.SettingsInversion(relocate_pix_border=True),\n", ")\n", "\n", "fit_plotter = aplt.FitImagingPlotter(fit=fit, include_2d=include)\n", "fit_plotter.figures_2d_of_planes(plane_index=1, plane_image=True)\n", "\n", "visuals = aplt.Visuals2D(indexes=[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])\n", "\n", "mapper_plotter = aplt.MapperPlotter(\n", " mapper=fit.inversion.linear_obj_list[0],\n", " visuals_2d=visuals,\n", " include_2d=include,\n", ")\n", "mapper_plotter.subplot_image_and_mapper(image=fit.dataset.data)" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "This successfully addresses both of the issues above! However, you might be thinking, isn't that a bit of a hack? Its \n", "not really a physical treatment of the ray-tracing, is it?\n", "\n", "Well, you are right. However, the *only* physical way to do this would be to use a mask so large that all demangified \n", "central pixels are surrounded by traced image-pixels. This would require a mask so large the **PyAutoLens** fit would\n", "become extremely slow. This is not a feasible solution, thus borders provide us with a workaround, one that is \n", "extensively tested and does not introduce systematic biases into the lens modeling procedure.\n", "\n", "To end, lets illustrate how important borders are when modeling multiple lens galaxies. Their complex mass distribution \n", "and lensing configurations produce nasty edge effects where image pixels not just in the centre of mask, but anywhere \n", "in the mask, trace beyond the source-plane border.\n", "\n", "we'll use new strong lensing data as the previous tutorial, where:\n", "\n", " - The lens galaxy's light is omitted.\n", " - There are two lens galaxies whose `MassProfile`'s are `Isothermal`.\n", " - The source galaxy's light is an `Sersic`." ] }, { "cell_type": "code", "metadata": {}, "source": [ "dataset_name = \"x2_lens_galaxies\"\n", "dataset_path = path.join(\"dataset\", \"imaging\", dataset_name)\n", "\n", "dataset = al.Imaging.from_fits(\n", " data_path=path.join(dataset_path, \"data.fits\"),\n", " noise_map_path=path.join(dataset_path, \"noise_map.fits\"),\n", " psf_path=path.join(dataset_path, \"psf.fits\"),\n", " pixel_scales=0.05,\n", ")" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "We again must define a mask around this image, lets start with a 2.8\" mask. we'll use larger masks to illustrate the\n", "effects of the border in a moment." ] }, { "cell_type": "code", "metadata": {}, "source": [ "mask_circular = al.Mask2D.circular(\n", " shape_native=dataset.shape_native,\n", " pixel_scales=dataset.pixel_scales,\n", " sub_size=2,\n", " radius=2.8,\n", ")\n", "\n", "dataset = dataset.apply_mask(mask=mask_circular)\n", "\n", "include = aplt.Include2D(border=True)\n", "\n", "dataset_plotter = aplt.ImagingPlotter(dataset=dataset, include_2d=include)\n", "dataset_plotter.subplot_dataset()" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "We need to redefine our perform fit function, to use the x2 lens galaxy model." ] }, { "cell_type": "code", "metadata": {}, "source": [ "\n", "\n", "def perform_fit_x2_lenses_with_source_galaxy_mask_and_border(\n", " dataset, source_galaxy, mask, settings_inversion\n", "):\n", " dataset = dataset.apply_mask(mask=mask)\n", "\n", " lens_galaxy_0 = al.Galaxy(\n", " redshift=0.5,\n", " bulge=al.lp.Sersic(\n", " centre=(0.0, -1.0),\n", " ell_comps=(0.25, 0.1),\n", " intensity=0.1,\n", " effective_radius=0.8,\n", " sersic_index=2.5,\n", " ),\n", " mass=al.mp.Isothermal(\n", " centre=(1.1, 0.51), ell_comps=(0.0, 0.15), einstein_radius=1.07\n", " ),\n", " )\n", "\n", " lens_galaxy_1 = al.Galaxy(\n", " redshift=0.5,\n", " bulge=al.lp.Sersic(\n", " centre=(0.0, 1.0),\n", " ell_comps=(0.0, 0.1),\n", " intensity=0.1,\n", " effective_radius=0.6,\n", " sersic_index=3.0,\n", " ),\n", " mass=al.mp.Isothermal(\n", " centre=(-0.20, -0.35), ell_comps=(0.06, 0.1053), einstein_radius=0.71\n", " ),\n", " )\n", "\n", " tracer = al.Tracer.from_galaxies(\n", " galaxies=[lens_galaxy_0, lens_galaxy_1, source_galaxy]\n", " )\n", "\n", " return al.FitImaging(\n", " dataset=dataset, tracer=tracer, settings_inversion=settings_inversion\n", " )\n" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, lets fit this image using the input model and perform the source reconstruction without a border. As you can see, \n", "we get many demagnified image pixels which trace well beyond our source-plane border if we don't relocate them!" ] }, { "cell_type": "code", "metadata": {}, "source": [ "fit = perform_fit_x2_lenses_with_source_galaxy_mask_and_border(\n", " dataset=dataset,\n", " source_galaxy=source_galaxy,\n", " mask=mask_circular,\n", " settings_inversion=al.SettingsInversion(relocate_pix_border=False),\n", ")\n", "\n", "include = aplt.Include2D(mapper_source_plane_data_grid=True, border=True)\n", "\n", "fit_plotter = aplt.FitImagingPlotter(fit=fit, include_2d=include)\n", "fit_plotter.figures_2d_of_planes(plane_index=1, plane_image=True)" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, when we relocate them, we get a good-looking source-plane with a well defined border and edge, thus ensuring \n", "our analysis will be free of systematic biases." ] }, { "cell_type": "code", "metadata": {}, "source": [ "fit = perform_fit_x2_lenses_with_source_galaxy_mask_and_border(\n", " dataset=dataset,\n", " source_galaxy=source_galaxy,\n", " mask=mask_circular,\n", " settings_inversion=al.SettingsInversion(relocate_pix_border=True),\n", ")\n", "\n", "fit_plotter = aplt.FitImagingPlotter(fit=fit, include_2d=include)\n", "fit_plotter.figures_2d_of_planes(plane_index=1, plane_image=True)" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "Multi-galaxy modeling is rife for border effects and if you have multiple lens galaxies I heartily recommend you pay \n", "a close eye to your source-plane borders!\n", "\n", "Care must also be taken when choosing the size of your mask. If you don't choose a big enough mask, the border won't \n", "be able to relocate all of the demanigified image pixels to the border edge.\n", "\n", "(The figures below look pretty horrible, because every ray-traced image coordinate is being plotted in the \n", "source plane. Therefore, there are many black dots which overwhelm the figure. The point to focus on are the\n", "edges of the grid, where one can see the relocations of these coordinates.\n", "\n", "By setting `aplt.Include2D(mapper_source_plane_data_grid=False)` a cleaner figure without this swarm of points\n", "is provided)." ] }, { "cell_type": "code", "metadata": {}, "source": [ "mask_circular = al.Mask2D.circular(\n", " shape_native=dataset.shape_native,\n", " pixel_scales=dataset.pixel_scales,\n", " sub_size=2,\n", " radius=2.5,\n", ")\n", "fit = perform_fit_x2_lenses_with_source_galaxy_mask_and_border(\n", " dataset=dataset,\n", " source_galaxy=source_galaxy,\n", " mask=mask_circular,\n", " settings_inversion=al.SettingsInversion(relocate_pix_border=True),\n", ")\n", "\n", "fit_plotter = aplt.FitImagingPlotter(fit=fit, include_2d=include)\n", "fit_plotter.figures_2d_of_planes(plane_index=1, plane_image=True)\n", "\n", "mask_circular = al.Mask2D.circular(\n", " shape_native=dataset.shape_native,\n", " pixel_scales=dataset.pixel_scales,\n", " sub_size=2,\n", " radius=2.7,\n", ")\n", "fit = perform_fit_x2_lenses_with_source_galaxy_mask_and_border(\n", " dataset=dataset,\n", " source_galaxy=source_galaxy,\n", " mask=mask_circular,\n", " settings_inversion=al.SettingsInversion(relocate_pix_border=True),\n", ")\n", "\n", "fit_plotter = aplt.FitImagingPlotter(fit=fit, include_2d=include)\n", "fit_plotter.figures_2d_of_planes(plane_index=1, plane_image=True)\n", "\n", "\n", "mask_circular = al.Mask2D.circular(\n", " shape_native=dataset.shape_native,\n", " pixel_scales=dataset.pixel_scales,\n", " sub_size=2,\n", " radius=2.9,\n", ")\n", "fit = perform_fit_x2_lenses_with_source_galaxy_mask_and_border(\n", " dataset=dataset,\n", " source_galaxy=source_galaxy,\n", " mask=mask_circular,\n", " settings_inversion=al.SettingsInversion(relocate_pix_border=True),\n", ")\n", "\n", "fit_plotter = aplt.FitImagingPlotter(fit=fit, include_2d=include)\n", "fit_plotter.figures_2d_of_planes(plane_index=1, plane_image=True)\n", "\n", "\n", "mask_circular = al.Mask2D.circular(\n", " shape_native=dataset.shape_native,\n", " pixel_scales=dataset.pixel_scales,\n", " sub_size=2,\n", " radius=3.1,\n", ")\n", "fit = perform_fit_x2_lenses_with_source_galaxy_mask_and_border(\n", " dataset=dataset,\n", " source_galaxy=source_galaxy,\n", " mask=mask_circular,\n", " settings_inversion=al.SettingsInversion(relocate_pix_border=True),\n", ")\n", "\n", "fit_plotter = aplt.FitImagingPlotter(fit=fit, include_2d=include)\n", "fit_plotter.figures_2d_of_planes(plane_index=1, plane_image=True)" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Wrap Up__\n", "\n", "When using **PyAutoLens** to perform inversions, you probably won't think about borders all that often. Borders should \n", "pretty much take care of themselves.\n", "\n", "However, as I showed above, if you don't choose a large enough mask things can go wrong and its important you know what \n", "borders are, so you can diagnose this potential source of systematics!" ] }, { "cell_type": "code", "metadata": {}, "source": [], "outputs": [], "execution_count": null } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 4 }