{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Example: Input Source Image\n", "===========================\n", "\n", "This example illustrates how to create a lensed image of a source, from a image of a source (E.g. the image is\n", "discrete pixel intensity values on a square or rectangular grid).\n", "\n", "Typically the source image will be a high resolution unlensed galaxy image, in order to simulate strong lenses\n", "with realistic source emission.\n", "\n", "However, it could be an image of anything, so you could make a lensed image of your dog if you really wanted!" ] }, { "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", "from scipy.interpolate import griddata\n", "import autolens as al\n", "import autolens.plot as aplt" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Galaxy Image__\n", "\n", "We first load the image of the galaxy (from a .fits file) which will be lensed.\n", "\n", "This image is typically a real galaxy image that is not gravitationally lensed. " ] }, { "cell_type": "code", "metadata": {}, "source": [ "data_path = path.join(\"scripts\", \"misc\", \"galaxy_image.fits\")\n", "\n", "galaxy_image = al.Array2D.from_fits(file_path=data_path, pixel_scales=0.02)" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "To create the lensed image, we will ray-trace image pixels to the source-plane and interpolate them onto the \n", "source galaxy image" ] }, { "cell_type": "code", "metadata": {}, "source": [ "source_plane_grid = al.Grid2D.uniform(\n", " shape_native=galaxy_image.shape_native,\n", " pixel_scales=galaxy_image.pixel_scales,\n", " origin=(0.0, 0.0),\n", ")" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Image-PLane Grid__\n", "\n", "The 2D grid of (y,x) coordinates which we will ray-trace to the source-plane (via a lens model) and compare to the\n", "source-galaxy image pixel fluxes to create our lensed image." ] }, { "cell_type": "code", "metadata": {}, "source": [ "image_plane_grid = al.Grid2D.uniform(shape_native=(100, 100), pixel_scales=0.05)" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Tracer__\n", "\n", "Define the mass model which is used for the ray-tracing, from which the lensed source image will be created.\n", "\n", "An input `source` galaxy is required below, so that the `Tracer` has a source-plane (at redshift 1.0) which the\n", "image-plane grid's coordinates are ray-traced too." ] }, { "cell_type": "code", "metadata": {}, "source": [ "lens = al.Galaxy(\n", " redshift=0.5, mass=al.mp.IsothermalSph(centre=(0.0, 0.0), einstein_radius=1.0)\n", ")\n", "source = al.Galaxy(redshift=1.0)\n", "\n", "tracer = al.Tracer.from_galaxies(galaxies=[lens, source])" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ray-trace the image-plane grid to the source-plane.\n", "\n", "This is the grid we will overlay the source image, in order to created the lensed source image." ] }, { "cell_type": "code", "metadata": {}, "source": [ "traced_image_plane_grid = tracer.traced_grid_2d_list_from(grid=image_plane_grid)[-1]" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Interpolation__\n", "\n", "We now use the scipy interpolation function `griddata`, where:\n", "\n", " - `points`: the 2D grid of (y,x) coordinates representing the location of every pixel of the galaxy image from\n", " which we are creating the lensed source image.\n", " \n", " - `values`: the intensity values of the galaxy image which is being used to create the lensed source image.\n", " \n", " - `xi`: the image-plane grid ray traced to the source-plane, defining the image on which the lensed source is created.\n", " \n", "The interpolation works by pairing every ray-traced (y,x) coordinate in the `traced_image_plane_grid` to its\n", "closest 4 coordinates in `source_plane_grid`. \n", "\n", "It then uses Delaunay interpolation to compute the intensity from these 4 coordinates." ] }, { "cell_type": "code", "metadata": {}, "source": [ "lensed_image = griddata(\n", " points=source_plane_grid, values=galaxy_image, xi=traced_image_plane_grid\n", ")" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Lensed Source Image__\n", "\n", "We can plot the lensed source image to make sure it looks sensible." ] }, { "cell_type": "code", "metadata": {}, "source": [ "lensed_image = al.Array2D.no_mask(\n", " values=lensed_image,\n", " shape_native=image_plane_grid.shape_native,\n", " pixel_scales=image_plane_grid.pixel_scales,\n", ")\n", "\n", "array_2d_plotter = aplt.Array2DPlotter(array=lensed_image)\n", "array_2d_plotter.figure_2d()\n" ], "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 }