Exemplo n.º 1
0
customize the plots using the PyAutoLens mat_plot_2d.

Below, we create a new function to apply as a generator to do this. However, we use a convenience method available 
in the PyAutoLens aggregator package to set up the fit.
"""
fit_agg = al.agg.FitImagingAgg(aggregator=agg)
fit_imaging_gen = fit_agg.max_log_likelihood_gen()

for fit in fit_imaging_gen:

    mat_plot_2d = aplt.MatPlot2D(
        figure=aplt.Figure(figsize=(12, 12)),
        title=aplt.Title(label="Custom Image", fontsize=24),
        yticks=aplt.YTicks(fontsize=24),
        xticks=aplt.XTicks(fontsize=24),
        cmap=aplt.Cmap(norm="log", vmax=1.0, vmin=1.0),
        colorbar_tickparams=aplt.ColorbarTickParams(labelsize=20),
        units=aplt.Units(in_kpc=True),
    )

    fit_imaging_plotter = aplt.FitImagingPlotter(fit=fit,
                                                 mat_plot_2d=mat_plot_2d)
    fit_imaging_plotter.figures_2d(normalized_residual_map=True)
"""
Making this plot for a paper? You can output it to hard disk.
"""
fit_agg = al.agg.FitImagingAgg(aggregator=agg)
fit_imaging_gen = fit_agg.max_log_likelihood_gen()

for fit in fit_imaging_gen:
dataset_path = path.join("dataset", "imaging", "no_lens_light", dataset_name)

"""
If you use this tool for your own dataset, you *must* double check this pixel scale is correct!
"""
pixel_scales = 0.1

"""
First, load the `Imaging` dataset, so that the location of galaxies is clear when scaling the noise-map.
"""
image = al.Array2D.from_fits(
    file_path=path.join(dataset_path, "image.fits"), pixel_scales=pixel_scales
)

cmap = aplt.Cmap(
    norm="log", vmin=1.0e-4, vmax=0.4 * np.max(image), linthresh=0.05, linscale=0.1
)

scribbler = scribbler.Scribbler(image=image.native, cmap=cmap)
mask = scribbler.show_mask()
mask = al.Mask2D.manual(mask=mask, pixel_scales=pixel_scales)

"""
Here, we change the image flux values to zeros. If included, we add some random Gaussian noise to most close resemble
noise in the image.
"""
background_level = al.preprocess.background_noise_map_from_edges_of_image(
    image=image, no_edges=2
)[0]

# gaussian_sigma = None
Exemplo n.º 3
0
imaging = al.Imaging.from_fits(
    image_path=path.join(dataset_path, "image.fits"),
    psf_path=path.join(dataset_path, "psf.fits"),
    noise_map_path=path.join(dataset_path, "noise_map.fits"),
    pixel_scales=pixel_scales,
)
image_2d = imaging.image.native
"""
For lenses with bright lens light emission, it can be difficult to get the source light to show. The normalization
below uses a log-scale with a capped maximum, which better contrasts the lens and source emission.
"""
cmap = aplt.Cmap(
    norm="linear",
    vmin=1.0e-4,
    vmax=np.max(imaging.image),
    #   linthresh=0.05,
    #   linscale=0.1,
)

norm = cmap.norm_from_array(array=None)

positions = []
"""
This code is a bit messy, but sets the image up as a matplotlib figure which one can double click on to mark the
positions on an image.
"""


def onclick(event):
    if event.dblclick:
Exemplo n.º 4
0
__Dataset__

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 
    hdu=0,
    pixel_scales=pixel_scales,
)
image_2d = image.native
"""
When you click on a pixel to mark a position, the search box looks around this click and finds the pixel with
the highest flux to mark the position.

The `search_box_size` is the number of pixels around your click this search takes place.
"""
search_box_size = 5
"""
The `norm` object created below customizes the minimum and maximum values of the colormap that is plotted, which can be
useful for datasets with a high dynamic range.
"""
cmap = aplt.Cmap(norm="linear", vmin=0.0, vmax=0.05)

norm = cmap.norm_from_array(array=None)
"""
This code is a bit messy, but sets the image up as a matplotlib figure which one can double click on to mark the
positions on an image.
"""
light_centres = []


def onclick(event):
    if event.dblclick:

        y_arcsec = np.rint(event.ydata / pixel_scales) * pixel_scales
        x_arcsec = np.rint(event.xdata / pixel_scales) * pixel_scales
Exemplo n.º 6
0
)

positions = solver.solve(lensing_obj=mass_profile,
                         source_plane_coordinate=light_profile.centre)

lens_galaxy = al.Galaxy(redshift=0.5, mass=mass_profile)
source_galaxy = al.Galaxy(redshift=1.0, light=light_profile)
tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

visuals_2d = aplt.Visuals2D(positions=positions)

magnification = tracer.magnification_from_grid(grid=grid)
magnification = np.nan_to_num(magnification)
print(magnification)

tracer_plotter = aplt.TracerPlotter(tracer=tracer,
                                    grid=grid,
                                    visuals_2d=visuals_2d)
tracer_plotter.figures(image=True)

mat_plot_2d = aplt.MatPlot2D(
    cmap=aplt.Cmap(vmax=9.0, vmin=-9.0),
    positions_scatter=aplt.PositionsScatter(s=100, c="w"),
)

tracer_plotter = aplt.TracerPlotter(tracer=tracer,
                                    grid=grid,
                                    visuals_2d=visuals_2d,
                                    mat_plot_2d=mat_plot_2d)
tracer_plotter.figure_magnification()
import numpy as np
from os import path
import autolens as al
import autolens.plot as aplt

dataset_path = path.join("..", "sdssj1152p3312")

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

# visuals_2d = aplt.Visuals2D(positions=positions, light_profile_centres=lens_centre)
mat_plot_2d = aplt.MatPlot2D(
    #   axis=aplt.Axis(extent=[-1.5, 1.5, -2.5, 0.5]),
    cmap=aplt.Cmap(vmin=0.0, vmax=0.3))

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

# imaging = al.Imaging.from_fits(
#     image_path=path.join(dataset_path, "f160w_image.fits"),
#     psf_path=path.join(dataset_path, "psf.fits"),
#     noise_map_path=path.join(dataset_path, "noise_map.fits"),
#     pixel_scales=0.03,
# )
#
# imaging_plotter = aplt.ImagingPlotter(imaging=imaging)
# imaging_plotter.subplot_imaging()
Exemplo n.º 8
0
Load the image which we will use to mark the positions.
"""
image = al.Array2D.from_fits(file_path=path.join(dataset_path, "image.fits"),
                             pixel_scales=pixel_scales)
"""
When you click on a pixel to mark a position, the search box looks around this click and finds the pixel with
the highest flux to mark the position.

The `search_box_size` is the number of pixels around your click this search takes place.
"""
search_box_size = 5
"""
For lenses with bright lens light emission, it can be difficult to get the source light to show. The normalization
below uses a log-scale with a capped maximum, which better contrasts the lens and source emission.
"""
cmap = aplt.Cmap(norm="linear", vmin=1.0e-4, vmax=np.max(image))

norm = cmap.norm_from(array=None)

positions = []
"""
This code is a bit messy, but sets the image up as a matplotlib figure which one can double click on to mark the
positions on an image.
"""


def onclick(event):
    if event.dblclick:

        # y_arcsec = np.rint(event.ydata / pixel_scales) * pixel_scales
        # x_arcsec = np.rint(event.xdata / pixel_scales) * pixel_scales
Exemplo n.º 9
0
image = al.Array2D.from_fits(file_path=image_path, hdu=0, pixel_scales=0.03)

"""
We can customize the colormap using the `Cmap` matplotlib wrapper object which wraps the following method(s):

 colors.Linear: https://matplotlib.org/3.3.2/tutorials/colors/colormaps.html
 colors.LogNorm: https://matplotlib.org/3.3.2/tutorials/colors/colormapnorms.html
 colors.SymLogNorm: https://matplotlib.org/3.3.2/api/_as_gen/matplotlib.colors.SymLogNorm.html
        
The colormap is used in various functions that plot images with a `cmap`, most notably `plt.imshow`:

 https://matplotlib.org/3.3.2/api/_as_gen/matplotlib.pyplot.imshow.html
 
First, lets plot the image using a linear colormap which uses a `colors.Normalize` object.
"""
cmap = aplt.Cmap(cmap="jet", norm="linear", vmin=0.0, vmax=1.0)

mat_plot_2d = aplt.MatPlot2D(cmap=cmap)

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

"""
We can instead use logarithmic colormap (this wraps the `colors.LogNorm` matplotlib object).
"""
cmap = aplt.Cmap(cmap="hot", norm="log", vmin=0.0, vmax=2.0)

mat_plot_2d = aplt.MatPlot2D(cmap=cmap)

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

Furthermore, using the `MatPLot2D`, `Visuals2D` and `Include2D` objects visualize any aspect of a fit we're interested 
in and fully customize the figure. 

Before beginning chapter 2 of **HowToLens**, you should checkout the package `autolens_workspace/plot`. This provides a 
full API reference of every plotting option in **PyAutoLens**, allowing you to create your own fully customized 
figures of strong lenses with minimal effort!
"""
mat_plot_2d = aplt.MatPlot2D(
    title=aplt.Title(label="This is the title", color="r", fontsize=20),
    ylabel=aplt.YLabel(label="Label of Y", color="b", fontsize=5, position=(0.2, 0.5)),
    xlabel=aplt.XLabel(label="Label of X", color="g", fontsize=10),
    cmap=aplt.Cmap(cmap="cool", norm="linear"),
)

include_2d = aplt.Include2D(
    origin=True, mask=True, border=True, light_profile_centres=True
)

visuals_2d = aplt.Visuals2D(critical_curves=tracer.critical_curves_from(grid=fit.grid))

light_profile_plotter = aplt.LightProfilePlotter(
    light_profile=fit.tracer.source_plane.galaxies[0].bulge,
    grid=source_plane_grid,
    mat_plot_2d=mat_plot_2d,
    include_2d=include_2d,
    visuals_2d=visuals_2d,
)
us applying a 'smoothness prior' on the reconstructed source galaxy's light.

This smoothing adds a 'penalty term' to the log likelihood of an inversion which is the summed difference between the 
reconstructed fluxes of every source-pixel pair multiplied by the `coefficient`. By setting the regularization 
coefficient to zero, we set this penalty term to zero, meaning that regularization is completely omitted.

Why do we need to regularize our solution? We just saw why, if we do not apply this smoothness prior to the source, we 
`over-fit` the image and reconstruct a noisy source with lots of extraneous features. This is what the  large flux 
values located at the exterior regions of the source reconstruction above are. If the inversions's sole aim is to 
maximize the log likelihood, it can do this by fitting *everything* accurately, including the noise.

If we change the `vmax` and `vmin` variables of the `Plotter`'s `CMap` such that the color-map is restricted to a 
narrower range of values, we can see that even without regularization we are still reconstructing the actual source 
galaxy.
"""
mat_plot_2d = aplt.MatPlot2D(cmap=aplt.Cmap(vmax=0.5, vmin=-0.5))

inversion_plotter = aplt.InversionPlotter(
    inversion=no_regularization_fit.inversion, mat_plot_2d=mat_plot_2d
)
inversion_plotter.figures_2d_of_mapper(mapper_index=0, reconstruction=True)

"""
Over-fitting is why regularization is necessary. Solutions like this will completely ruin our attempts to model a 
strong lens. By smoothing our source reconstruction we ensure it does not over fit noise in the image. 

So, what happens if we apply a high value for the regularization coefficient?
"""
source_galaxy = al.Galaxy(
    redshift=1.0,
    pixelization=al.pix.Rectangular(shape=(40, 40)),
Exemplo n.º 12
0
import autolens as al
import autolens.plot as aplt
from test_autolens.simulators.imaging import instrument_util
import numpy as np

imaging = instrument_util.load_test_imaging(
    dataset_name="light_sersic__source_sersic", instrument="vro"
)

array = imaging.image

plotter = aplt.MatPlot2D(
    figure=aplt.Figure(figsize=(10, 10)),
    cmap=aplt.Cmap(
        cmap="gray", norm="symmetric_log", vmin=-0.13, vmax=20, linthresh=0.02
    ),
    grid_scatter=aplt.GridScatter(marker="+", colors="cyan", s=450),
)

grid = al.GridIrregular(grid=[[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]])

print(grid)

vector_field = al.VectorFieldIrregular(
    vectors=[(1.0, 2.0), (2.0, 1.0)], grid=[(-1.0, 0.0), (-2.0, 0.0)]
)

aplt.Array(
    array=array.in_2d,
    grid=grid,
    positions=al.GridIrregularGrouped([(0.0, 1.0), (0.0, 2.0)]),