First, lets load an example Hubble Space Telescope image of a real strong lens as an `Array2D`.
"""
dataset_path = path.join("dataset", "slacs", "slacs1430+4105")
image_path = path.join(dataset_path, "image.fits")
image = al.Array2D.from_fits(file_path=image_path, hdu=0, pixel_scales=0.03)

"""
The appearance of a (y,x) `Grid2D` of coordinates is customized using `Scatter` objects. To illustrate this, we will 
customize the appearance of the (y,x) origin on a figure using an `OriginScatter` object.

To plot a (y,x) grids of coordinates (like an origin) these objects wrap the following matplotlib method:

 https://matplotlib.org/3.2.2/api/_as_gen/matplotlib.pyplot.scatter.html
"""
origin_scatter = aplt.OriginScatter(marker="o", s=50)

mat_plot_2d = aplt.MatPlot2D(origin_scatter=origin_scatter)

array_plotter = aplt.Array2DPlotter(
    array=image, include_2d=aplt.Include2D(origin=True), mat_plot_2d=mat_plot_2d
)
array_plotter.figure_2d()

"""
There are numerous (y,x) grids of coordinates that PyAutoLens plots. For example, in addition to the origin,
there are grids like the multiple images of a strong lens, a source-plane grid of traced coordinates, etc.

All of these grids are plotted using a `Scatter` object and they are described in more detail in the 
`plot/include_2d` example scripts. 
"""
gaussian_sigma = 0.1

image = np.where(mask, 0.0, image.native)
image = al.Array2D.manual_native(array=image, pixel_scales=pixel_scales)

if gaussian_sigma is not None:
    random_noise = np.random.normal(
        loc=background_level, scale=gaussian_sigma, size=image.shape_native
    )
    image = np.where(mask, random_noise, image.native)
    image = al.Array2D.manual_native(array=image, pixel_scales=pixel_scales)

"""
The new image is plotted for inspection.
"""
array_plotter = aplt.Array2DPlotter(array=image)
array_plotter.figure_2d()

"""
Now we`re happy with the image, lets output it to the dataset folder of the lens, so that we can load it from a .fits
file in our pipelines!
"""
image.output_to_fits(
    file_path=path.join(dataset_path, "image_scaled.fits"), overwrite=True
)

"""
Next, load the `Imaging` noise-map, which we will use the scale the noise-map.
"""
noise_map = al.Array2D.from_fits(
    file_path=path.join(dataset_path, "noise_map.fits"), pixel_scales=pixel_scales
We can customize the colorbar using the `Colorbar` matplotlib wrapper object which wraps the following method(s):

 https://matplotlib.org/3.3.2/api/_as_gen/matplotlib.pyplot.colorbar.html
"""
cb = aplt.Colorbar(
    fraction=0.047,
    shrink=5.0,
    aspect=1.0,
    pad=0.01,
    anchor=(0.0, 0.5),
    panchor=(1.0, 0.0),
)

mat_plot_2d = aplt.MatPlot2D(colorbar=cb)

array_plotter = aplt.Array2DPlotter(array=image, mat_plot_2d=mat_plot_2d)
array_plotter.figure_2d()

"""
The labels of the `Colorbar` can also be customized. 

This uses the `cb.ax.set_yticklabels` to manually override the tick locations and labels:
 
 https://matplotlib.org/3.3.3/api/_as_gen/matplotlib.axes.Axes.set_yticklabels.html
 
The input parameters of both the above methods can be passed into the `Colorbar` object.
"""
cb = aplt.Colorbar(manual_tick_labels=[1.0, 2.0], manual_tick_values=[0.0, 0.25])


mat_plot_2d = aplt.MatPlot2D(colorbar=cb)
import autolens as al
import autolens.plot as aplt
"""
__Dataset__

First, lets load an example image of of a strong lens as an `Array2D`.
"""
dataset_path = path.join("dataset", "slacs", "slacs1430+4105")
image_path = path.join(dataset_path, "image.fits")
image = al.Array2D.from_fits(file_path=image_path, hdu=0, pixel_scales=0.03)
"""
__Figures__

We now pass the array to an `Array2DPlotter` and call the `figure` method.
"""
array_plotter = aplt.Array2DPlotter(array=image)
array_plotter.figure_2d()
"""
__Include__

An `Array2D` contains the following attributes which can be plotted automatically via the `Include2D` object.

(By default, an `Array2D` does not contain a `Mask2D`, we therefore manually created an `Array2D` with a mask to illustrate
plotting its mask and border below).
"""
include_2d = aplt.Include2D(origin=True, mask=True, border=True)

mask = al.Mask2D.circular_annular(
    shape_native=image.shape_native,
    pixel_scales=image.pixel_scales,
    inner_radius=0.3,
    hyper_galaxy_image=hyper_image,
    hyper_model_image=
    hyper_image_source,  # <- The source get its own hyper-galaxy image.
)

fit = fit_imaging_with_lens_and_source_galaxy(
    imaging=imaging,
    lens_galaxy=lens_galaxy,
    source_galaxy=source_magnification)
fit_imaging_plotter = aplt.FitImagingPlotter(fit=fit, include_2d=include_2d)
fit_imaging_plotter.subplot_fit_imaging()

lens_contribution_map = lens_galaxy_hyper.hyper_galaxy.contribution_map_from_hyper_images(
    hyper_model_image=hyper_image, hyper_galaxy_image=hyper_image_lens)

array_plotter = aplt.Array2DPlotter(array=lens_contribution_map)
array_plotter.set_title("Lens Contribution Map")
array_plotter.figure_2d()

source_contribution_map = source_magnification_hyper.hyper_galaxy.contribution_map_from_hyper_images(
    hyper_model_image=hyper_image, hyper_galaxy_image=hyper_image_source)

array_plotter = aplt.Array2DPlotter(array=source_contribution_map)
array_plotter.set_title("Source Contribution Map")
array_plotter.figure_2d()
"""
The contribution maps decompose the image into its different components. Next, we use each contribution map to scale 
different regions of the noise-map. From the fit above it was clear that both the lens and source required the noise to 
be scaled, but their different chi-squared values ( > 150 and ~ 30) means they require different levels of noise-scaling. 

Lets see how much our fit improves and Bayesian evidence increases.
Exemple #6
0
Setup the path the datasets we'll use to illustrate preprocessing, which is the folder `dataset/imaging/preprocess`.
"""
dataset_path = path.join("dataset", "imaging", "preprocess")
"""
__Loading Data From Individual Fits Files__

First, lets load a noise-map as an Array2D. This noise-map represents a good data-reduction that conforms to the 
formatting standards I describe in this tutorial!
"""
imaging_path = path.join(dataset_path, "imaging")

noise_map = al.Array2D.from_fits(file_path=path.join(imaging_path,
                                                     "noise_map.fits"),
                                 pixel_scales=0.1)

array_plotter = aplt.Array2DPlotter(array=noise_map)
array_plotter.figure_2d()
"""
__1) Converting Noise-Map Like The Image__

If in the previous preprocessing script you did any of the following to the image:

1) Converted it from counts / ADUs / other units to electrons per second.
2) Trimmed / padded the image.
3) Recentered the image.

You must perform identical operations on your noise-map (assuming it is in the same units and has the dimensions as the
image. You can simply cut and paste the appropriate functions in below - I`ve commented out the appropriate functions
you might of used.

"""
Exemple #7
0
Lets look at a few contribution maps, generated using hyper-galaxy's with different contribution factors.
"""
source_contribution_factor_1 = al.Galaxy(
    redshift=1.0,
    hyper_galaxy=al.HyperGalaxy(contribution_factor=1.0),
    hyper_galaxy_image=hyper_image,
    hyper_model_image=hyper_image,
)

contribution_map = source_contribution_factor_1.hyper_galaxy.contribution_map_from_hyper_images(
    hyper_model_image=hyper_image, hyper_galaxy_image=hyper_image
)

mat_plot_2d = aplt.MatPlot2D(title=aplt.Title(label="Contribution Map"))

array_plotter = aplt.Array2DPlotter(array=contribution_map, mat_plot_2d=mat_plot_2d)
array_plotter.figure_2d()

source_contribution_factor_3 = al.Galaxy(
    redshift=1.0,
    hyper_galaxy=al.HyperGalaxy(contribution_factor=3.0),
    hyper_galaxy_image=hyper_image,
    hyper_model_image=hyper_image,
)

contribution_map = source_contribution_factor_3.hyper_galaxy.contribution_map_from_hyper_images(
    hyper_model_image=hyper_image, hyper_galaxy_image=hyper_image
)

array_plotter = aplt.Array2DPlotter(array=contribution_map, mat_plot_2d=mat_plot_2d)
array_plotter.figure_2d()
Exemple #8
0
                                                     "noise_map.fits"),
                                 pixel_scales=0.1)
"""
Returns the 2D Gaussian that the image is blurred with. This blurring smooths over noise in the image, which will 
otherwise lead unmasked values with in individual pixels if not smoothed over correctly.
"""
blurring_gaussian = al.Kernel2D.from_gaussian(
    shape_native=(31, 31),
    pixel_scales=image.pixel_scales,
    sigma=blurring_gaussian_sigma,
)
"""
Blur the image with this Gaussian smoothing kernel and plot the resulting image.
"""
blurred_image = blurring_gaussian.convolved_array_from_array(array=image)
aplt.Array2DPlotter(array=blurred_image)
"""
Now compute the absolute signal-to-noise map of this blurred image, given the noise-map of the observed dataset.
"""
blurred_signal_to_noise_map = blurred_image / noise_map
aplt.Array2DPlotter(array=blurred_signal_to_noise_map)
"""
Now create the mask in 2ll pixels where the signal to noise is above some threshold value.
"""
mask = np.where(blurred_signal_to_noise_map.native > signal_to_noise_threshold,
                False, True)
mask = al.Mask2D.manual(mask=mask, pixel_scales=image.pixel_scales)

visuals_2d = aplt.Visuals2D(mask=mask)
array_plotter = aplt.Array2DPlotter(array=image, visuals_2d=visuals_2d)
array_plotter.figure_2d()
Exemple #9
0
First, lets load and plot the image of our example strong lens cluster, cluster. 

We will use this to verify that our strong lens galaxy centres are aligned with the data.
"""
dataset_name = "cluster"
dataset_path = path.join("dataset", "cluster", dataset_name)

image = al.Array2D.from_fits(file_path=path.join(dataset_path,
                                                 "f160w_image.fits"),
                             hdu=0,
                             pixel_scales=0.03)

mat_plot_2d = aplt.MatPlot2D(cmap=aplt.Cmap(vmin=0.0, vmax=0.1))

array_plotter = aplt.Array2DPlotter(array=image.native,
                                    mat_plot_2d=mat_plot_2d)
array_plotter.figure_2d()
"""
__Redshift__

The redshift of the galaxy cluster, and therefore all of its member lenses, is used by the lens model and therefore 
must be set. 
"""
redshift_lens = 0.5
"""
__Brightest Cluster Galaxy (BCG)__

We next create the model of the brightest cluster galaxy (BCG), which will be fitted for individually in the lens 
model using an `EllIsothermal` model. This can easily be extended to include multiple lenses in the cluster.

The centre of the BCG will be the origin of our coordinate system, so we do not need to update the priors on its 
`mass_sie__source_parametric.py` simulator). 

Lets load the input deflection angle map from a .fits files (which is created in the code mentioned above).
"""
deflections_y = al.Array2D.from_fits(
    file_path=path.join("dataset", "misc", "deflections_y.fits"),
    pixel_scales=imaging.pixel_scales,
)
deflections_x = al.Array2D.from_fits(
    file_path=path.join("dataset", "misc", "deflections_x.fits"),
    pixel_scales=imaging.pixel_scales,
)
"""
Lets plot the deflection angles to make sure they look like what we expect!
"""
aplt.Array2DPlotter(array=deflections_y)
aplt.Array2DPlotter(array=deflections_x)
"""
Lets next load and plot the image-plane grid.
"""
grid = al.Grid2D.from_fits(
    file_path=path.join("dataset", "misc", "grid.fits"),
    pixel_scales=imaging.pixel_scales,
)
grid_plotter = aplt.Grid2DPlotter(grid=grid)
grid_plotter.figure_2d()
"""
We now create our `InputDeflections` `MassProfile`, which represents our input deflection angle map as a 
`MassProfile` in PyAutoLens so that it can be used with objects like `Galaxy`'s and `Tracer`.

This takes as input both the input deflection angles and their corresponding image-plane grid, with the latter used to
Exemple #11
0
image = al.Array2D.from_fits(file_path=image_path, hdu=0, pixel_scales=0.03)
"""
We will also need the mask we will plot on the figure, which we associate with the image.
"""
mask = al.Mask2D.circular_annular(
    shape_native=image.shape_native,
    pixel_scales=image.pixel_scales,
    inner_radius=0.3,
    outer_radius=3.0,
)
masked_image = al.Array2D.manual_mask(array=image.native, mask=mask)
"""
The `Array2D` includes its mask as an internal property, meaning we can plot it via an `Include2D` object.
"""
include_2d = aplt.Include2D(mask=True)
array_plotter = aplt.Array2DPlotter(array=masked_image, include_2d=include_2d)
array_plotter.figure_2d()
"""
The appearance of the mask is customized using a `Scatter` object.

To plot the mask this object wraps the following matplotlib method:

 https://matplotlib.org/3.2.2/api/_as_gen/matplotlib.pyplot.scatter.html
"""
mask_scatter = aplt.MaskScatter(marker="o", c="r", s=50)

mat_plot_2d = aplt.MatPlot2D(mask_scatter=mask_scatter)

array_plotter = aplt.Array2DPlotter(array=masked_image,
                                    mat_plot_2d=mat_plot_2d,
                                    include_2d=include_2d)
Setup the path the datasets we'll use to illustrate preprocessing, which is the folder `dataset/imaging/preprocess`.
"""
dataset_path = path.join("dataset", "imaging", "preprocess")
"""
__Loading Data From Individual Fits Files__

First, lets load a PSF as a Kernel2D. This psf represents a good data-reduction that conforms to the formatting 
standards I describe in this tutorial!
"""
imaging_path = path.join(dataset_path, "imaging")

psf = al.Kernel2D.from_fits(file_path=path.join(imaging_path, "psf.fits"),
                            hdu=0,
                            pixel_scales=0.1)

array_plotter = aplt.Array2DPlotter(array=psf)
array_plotter.figure_2d()
"""
__1) PSF Size__

The majority of PSF blurring occurs at its central core, which is the most important region for strong lens modeling. 
By default, the size of the PSF kernel in the .fits is used to perform convolution. The larger this stamp, the longer 
this convolution will take to run. Large PSFs (e.g. > 51 x 51) could have significantly slow down on run-time. In general, 
we would recommend the PSF size is 21 x 21. 

Lets look at an image where a large PSF kernel is loaded.
"""
imaging_path = path.join(dataset_path, "imaging_with_large_psf")

large_psf = al.Kernel2D.from_fits(file_path=path.join(imaging_path,
                                                      "psf.fits"),
Lets load the input deflection angle map from a .fits files (which is created in the code mentioned above).
"""
deflections_y = al.Array2D.from_fits(
    file_path=path.join("dataset", "misc", "deflections_y.fits"),
    pixel_scales=imaging.pixel_scales,
)
deflections_x = al.Array2D.from_fits(
    file_path=path.join("dataset", "misc", "deflections_x.fits"),
    pixel_scales=imaging.pixel_scales,
)

"""
Lets plot the deflection angles to make sure they look like what we expect!
"""
aplt.Array2DPlotter(array=deflections_y)
aplt.Array2DPlotter(array=deflections_x)

"""
Lets next load and plot the image-plane grid
"""
grid = al.Grid2D.from_fits(
    file_path=path.join("dataset", "misc", "grid.fits"),
    pixel_scales=imaging.pixel_scales,
)
grid_plotter = aplt.Grid2DPlotter(grid=grid)
grid_plotter.figure_2d()

"""
The `Mask2D` our model-fit using the `InputDeflections` will use. This is set up the same way as the previous script, but
not this `Mask2D` now defines the image-plane region we will fit the data (and therefore where our residuals, chi-squared,
Exemple #14
0

n_y, n_x = imaging.image.shape_native
hw = int(n_x / 2) * pixel_scales
ext = [-hw, hw, -hw, hw]
fig = plt.figure(figsize=(14, 14))
plt.imshow(imaging.image.native, cmap="jet", extent=ext)
plt.colorbar()
cid = fig.canvas.mpl_connect("button_press_event", onclick)
plt.show()
fig.canvas.mpl_disconnect(cid)
plt.close(fig)

light_centres = al.Grid2DIrregular(grid=light_centres)
"""
Now lets plot the image and lens light centre, so we can check that the centre overlaps the brightest pixel in the
lens light.
"""
visuals_2d = aplt.Visuals2D(light_profile_centres=light_centres)
aplt.Array2DPlotter(array=imaging.image, visuals_2d=visuals_2d)
"""
Now we`re happy with the lens light centre(s), lets output them to the dataset folder of the lens, so that we can 
load them from a.json file in our pipelines!
"""
try:
    light_centres.output_to_json(file_path=path.join(dataset_path,
                                                     "light_centres.json"),
                                 overwrite=True)
except AttributeError:
    pass
Exemple #15
0
        print("clicked on:", y_pixels, x_pixels)
        print("Max flux pixel:", y_pixels_max, x_pixels_max)
        print("Arc-sec Coordinate", y_arcsec, x_arcsec)

        positions.append((y_arcsec, x_arcsec))


n_y, n_x = imaging.image.shape_native
hw = int(n_x / 2) * pixel_scales
ext = [-hw, hw, -hw, hw]
fig = plt.figure(figsize=(14, 14))
plt.imshow(imaging.image.native, cmap="jet", extent=ext, norm=norm)
plt.colorbar()
cid = fig.canvas.mpl_connect("button_press_event", onclick)
plt.show()
fig.canvas.mpl_disconnect(cid)
plt.close(fig)

positions = al.Grid2DIrregular(grid=positions)
"""
Now lets plot the image and positions, so we can check that the positions overlap different regions of the source.
"""
array_plotter = aplt.Array2DPlotter(array=imaging.image)
array_plotter.figure_2d()
"""
Now we`re happy with the positions, lets output them to the dataset folder of the lens, so that we can load them from a
.json file in our pipelines!
"""
positions.output_to_json(file_path=path.join(dataset_path, "positions.json"),
                         overwrite=True)
Exemple #16
0
"""
imaging_path = path.join(dataset_path, "imaging")

image = al.Array2D.from_fits(
    file_path=path.join(imaging_path, "image.fits"), pixel_scales=0.1
)

"""
There are numerous reasons why the image below is a good data-set for lens modeling. I strongly recommend 
you adapt your data reduction pipelines to conform to the formats discussed below - it`ll make your time using 
PyAutoLens a lot simpler.

However, you may not have access to the data-reduction tools that made the data, so we've included in-built functions 
in PyAutoLens to convert the data to a suitable format.
"""
array_plotter = aplt.Array2DPlotter(array=image)
array_plotter.figure_2d()

"""
__1) Converting Data To Electrons Per Second__

1) Brightness units - the image`s flux values should be in units of electrons per second (as opposed to electrons, 
counts, ADU`s etc.). Although PyAutoLens can technically perform an analysis using other units, the default setup 
assumes electrons per second (e.g. the priors on `LightProfile` intensity and `Regularization` parameters). Thus, images 
not in electrons per second should be converted!

Lets look at an image that is in units of counts - its easy to tell because the peak values are in the 1000`s or 10000`s.
"""
imaging_path = f"{dataset_path}/imaging_in_counts"

image_in_counts = al.Array2D.from_fits(
Exemple #17
0
# mask = al.Mask2D.elliptical_annular(
#     shape_native=image.shape_native,
#     pixel_scales=image.pixel_scales,
#     inner_major_axis_radius=0.5,
#     inner_axis_ratio=0.7,
#     inner_phi=45.0,
#     outer_major_axis_radius=0.5,
#     outer_axis_ratio=0.7,
#     outer_phi=45.0,
#     centre=(0.0, 0.0),
# )
"""
Now lets plot the image and mask, so we can check that the mask includes the regions of the image we want.
"""
visuals_2d = aplt.Visuals2D(mask=mask)

array_plotter = aplt.Array2DPlotter(array=image, visuals_2d=visuals_2d)
array_plotter.figure_2d()
"""
Now we`re happy with the mask, lets output it to the dataset folder of the lens, so that we can load it from a .fits
file in our pipelines!
"""
mask.output_to_fits(file_path=path.join(dataset_path, "mask.fits"),
                    overwrite=True)
"""
The workspace also includes a GUI for drawing a mask, which can be found at 
`autolens_workspace/notebooks/preprocess/imaging/gui/mask.py`. This tools allows you to draw the mask via a `spray paint` mouse
icon, such that you can draw irregular masks more tailored to the source's light.
"""
Exemple #18
0
setting the `weight_floor` to 0.0 such that it does not change the cluster weight map.
"""
source_weight_power_0 = al.Galaxy(
    redshift=1.0,
    pixelization=al.pix.DelaunayBrightnessImage(pixels=500,
                                                weight_floor=0.0,
                                                weight_power=0.0),
    regularization=al.reg.Constant(coefficient=1.0),
    hyper_model_image=hyper_image,
    hyper_galaxy_image=hyper_image,
)

cluster_weight_power_0 = source_weight_power_0.pixelization.weight_map_from(
    hyper_image=source_weight_power_0.hyper_galaxy_image)

array_plotter = aplt.Array2DPlotter(array=cluster_weight_power_0,
                                    visuals_2d=aplt.Visuals2D(mask=mask))
array_plotter.figure_2d()

source_weight_power_5 = al.Galaxy(
    redshift=1.0,
    pixelization=al.pix.DelaunayBrightnessImage(pixels=500,
                                                weight_floor=0.0,
                                                weight_power=5.0),
    regularization=al.reg.Constant(coefficient=1.0),
    hyper_model_image=hyper_image,
    hyper_galaxy_image=hyper_image,
)

cluster_weight_power_5 = source_weight_power_5.pixelization.weight_map_from(
    hyper_image=source_weight_power_5.hyper_galaxy_image)