Ejemplo n.º 1
0
__Visualization Customization__

The benefit of inspecting fits using the aggregator, rather than the files outputs to the hard-disk, is that we can 
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()
"""
source_plane_grid = image_plane.traced_grid_from(grid=image_plane_grid)

print("Traced source-plane coordinates of `Grid2D` pixel 0:")
print(source_plane_grid.native[0, 0, :])
print("Traced source-plane coordinates of `Grid2D` pixel 1:")
print(source_plane_grid.native[0, 1, :])
"""
We now have grid of coordinates in the source-plane, so we can set up the source-plane as a `Plane` object.
"""
source_plane = al.Plane(galaxies=[source_galaxy])
"""
If its not yet clear what is going on, the figure below should explain below. Lets again plot our `image_plane_grid`,
which we know is a boring uniform grid of $(y,x)$ coordinates, but also plot the source-plane grid.
"""
mat_plot_2d = aplt.MatPlot2D(title=aplt.Title(label="Image-plane Grid"))

plane_plotter = aplt.PlanePlotter(plane=image_plane,
                                  grid=image_plane_grid,
                                  mat_plot_2d=mat_plot_2d)
plane_plotter.figures_2d(plane_grid=True)

mat_plot_2d = aplt.MatPlot2D(title=aplt.Title(label="Source-plane Grid"))

plane_plotter = aplt.PlanePlotter(plane=source_plane,
                                  grid=source_plane_grid,
                                  mat_plot_2d=mat_plot_2d)
plane_plotter.figures_2d(plane_grid=True)
"""
The source-plane looks very interesting! We can see it is not regular, not uniform, and has an aestetically pleasing
visual appearance. Remember that every coordinate on this source-plane grid (e.g. every black dot) corresponds to a 
imaging_plotter.subplot_imaging()
"""
Output the simulated dataset to the dataset path as .fits files.
"""
imaging.output_to_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"),
    overwrite=True,
)
"""
Output a subplot of the simulated dataset, the image and a subplot of the `Tracer`'s quantities to the dataset path 
as .png files.
"""
mat_plot_2d = aplt.MatPlot2D(
    title=aplt.Title(label="Hubble Space Telescope Image"),
    output=aplt.Output(path=dataset_path, format="png"),
)

imaging_plotter = aplt.ImagingPlotter(imaging=imaging, mat_plot_2d=mat_plot_2d)
imaging_plotter.subplot_imaging()
imaging_plotter.figures(image=True)

tracer_plotter = aplt.TracerPlotter(tracer=tracer,
                                    grid=grid,
                                    mat_plot_2d=mat_plot_2d)
tracer_plotter.subplot_tracer()
"""
Pickle the `Tracer` in the dataset folder, ensuring the true `Tracer` is safely stored and available if we need to 
check how the dataset was simulated in the future. 
Ejemplo n.º 4
0
Output the simulated dataset to the dataset path as .fits files.
"""
imaging.output_to_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"),
    overwrite=True,
)
"""
__Visualize__

Output a subplot of the simulated dataset, the image and the tracer's quantities to the dataset path as .png files.
"""
mat_plot_2d = aplt.MatPlot2D(
    title=aplt.Title(label="Vero Rubin Observator Image"),
    output=aplt.Output(path=dataset_path, format="png"),
)

imaging_plotter = aplt.ImagingPlotter(imaging=imaging, mat_plot_2d=mat_plot_2d)
imaging_plotter.subplot_imaging()
imaging_plotter.figures_2d(image=True)

tracer_plotter = aplt.TracerPlotter(tracer=tracer,
                                    grid=grid,
                                    mat_plot_2d=mat_plot_2d)
tracer_plotter.subplot_tracer()
tracer_plotter.subplot_plane_images()
"""
__Tracer Output__
Ejemplo n.º 5
0
 3) Divide the image created in step 2) by its maximum value, such that all pixels range between 0.0 and 1.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
)
Ejemplo n.º 6
0
imaging_plotter.subplot_imaging()
"""
Output the simulated dataset to the dataset path as .fits files.
"""
imaging.output_to_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"),
    overwrite=True,
)
"""
Output a subplot of the simulated dataset, the image and a subplot of the `Tracer`'s quantities to the dataset path 
as .png files.
"""
mat_plot_2d = aplt.MatPlot2D(
    title=aplt.Title(label="Keck Adaptive Optics Image"),
    output=aplt.Output(path=dataset_path, format="png"),
)

imaging_plotter = aplt.ImagingPlotter(imaging=imaging, mat_plot_2d=mat_plot_2d)
imaging_plotter.subplot_imaging()
imaging_plotter.figures(image=True)

tracer_plotter = aplt.TracerPlotter(tracer=tracer,
                                    grid=grid,
                                    mat_plot_2d=mat_plot_2d)
tracer_plotter.subplot_tracer()
"""
Pickle the `Tracer` in the dataset folder, ensuring the true `Tracer` is safely stored and available if we need to 
check how the dataset was simulated in the future. 
imaging_plotter.subplot_imaging()
"""
Output the simulated dataset to the dataset path as .fits files.
"""
imaging.output_to_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"),
    overwrite=True,
)
"""
Output a subplot of the simulated dataset, the image and a subplot of the `Tracer`'s quantities to the dataset path 
as .png files.
"""
mat_plot_2d = aplt.MatPlot2D(
    title=aplt.Title(label="Euclid Image"),
    output=aplt.Output(path=dataset_path, format="png"),
)

imaging_plotter = aplt.ImagingPlotter(imaging=imaging, mat_plot_2d=mat_plot_2d)
imaging_plotter.subplot_imaging()
imaging_plotter.figures(image=True)

tracer_plotter = aplt.TracerPlotter(tracer=tracer,
                                    grid=grid,
                                    mat_plot_2d=mat_plot_2d)
tracer_plotter.subplot_tracer()
"""
Pickle the `Tracer` in the dataset folder, ensuring the true `Tracer` is safely stored and available if we need to 
check how the dataset was simulated in the future. 
"""
Lets also create a small satellite galaxy nearby the lens galaxy and at the same redshift.
"""
lens_satellite = al.Galaxy(
    redshift=0.5,
    bulge=al.lp.SphDevVaucouleurs(centre=(1.0, 0.0),
                                  intensity=2.0,
                                  effective_radius=0.2),
    mass=al.mp.SphIsothermal(centre=(1.0, 0.0), einstein_radius=0.4),
)

print(lens_satellite)
"""
Lets have a quick look at the appearance of our lens galaxy and its satellite.
"""
mat_plot_2d = aplt.MatPlot2D(title=aplt.Title(label="Lens Galaxy"))

galaxy_plotter = aplt.GalaxyPlotter(galaxy=lens_galaxy,
                                    grid=grid,
                                    mat_plot_2d=mat_plot_2d)
galaxy_plotter.figures_2d(image=True)

mat_plot_2d = aplt.MatPlot2D(title=aplt.Title(label="Lens Satellite"))

galaxy_plotter = aplt.GalaxyPlotter(galaxy=lens_satellite,
                                    grid=grid,
                                    mat_plot_2d=mat_plot_2d)
galaxy_plotter.figures_2d(image=True)
"""
And their deflection angles, noting that the satellite does not contribute as much to the deflections.
"""
)
light_profile_plotter.set_title("Disk Image")
light_profile_plotter.figures_2d(image=True)

"""
__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,
"""
__Plot Customization__

Does the figure display correctly on your computer screen? 

If not, you can customize a number of matplotlib setup options using a `MatPlot2D` object in **PyAutoLens**, which 
wraps the `matplotlib` methods used to display the image.

(For example, the `Figure` class wraps the `matplotlib` method `plt.figure(), whereas the `Yticks` class wraps
`plt.yticks`).
"""
mat_plot_2d = aplt.MatPlot2D(
    figure=aplt.Figure(figsize=(7, 7)),
    yticks=aplt.YTicks(fontsize=8),
    xticks=aplt.XTicks(fontsize=8),
    title=aplt.Title(fontsize=12),
    ylabel=aplt.YLabel(fontsize=6),
    xlabel=aplt.XLabel(fontsize=6),
)

imaging_plotter = aplt.ImagingPlotter(imaging=imaging, mat_plot_2d=mat_plot_2d)
imaging_plotter.figures_2d(image=True)
"""
Many matplotlib options can be customized, but for now we're only concerned with making sure figures display clear in 
your Jupyter Notebooks. Nevertheless, a comprehensive API reference guide of all `matplotlib` wrappers and methods can 
be found in the `autolens_workspace/plot` package. You should check this out once you are more familiar with 
**PyAutoLens**.

Ideally, we would not specify a new `MatPlot2D` object every time we plot an image, especially as you would be 
changing the same option to the same values every time (e.g. the figsize) to make the figure display correctly over 
and over again. Fortunately, the default values used by **PyAutoLens** can be fully customized via the config files.
Ejemplo n.º 11
0
    noise_map_path=path.join(dataset_path, "noise_map.fits"),
    overwrite=True,
)

"""
Lets plot the simulated `Imaging` dataset before we output it to fits.
"""
imaging_plotter = aplt.ImagingPlotter(imaging=imaging)
imaging_plotter.subplot_imaging()

"""
Output a subplot of the simulated dataset, the image and a subplot of the `Tracer`'s quantities to the dataset path 
as .png files.
"""
mat_plot_2d = aplt.MatPlot2D(
    title=aplt.Title(label="Hubble Space Telescope Upscaled Image"),
    output=aplt.Output(path=dataset_path, format="png"),
)

imaging_plotter = aplt.ImagingPlotter(imaging=imaging, mat_plot_2d=mat_plot_2d)
imaging_plotter.subplot_imaging()
imaging_plotter.figures_2d(image=True)

tracer_plotter = aplt.TracerPlotter(tracer=tracer, grid=grid, mat_plot_2d=mat_plot_2d)
tracer_plotter.subplot_tracer()

"""
Pickle the `Tracer` in the dataset folder, ensuring the true `Tracer` is safely stored and available if we need to 
check how the dataset was simulated in the future. 

This will also be accessible via the `Aggregator` if a model-fit is performed using the dataset.
Ejemplo n.º 12
0
import autolens.plot as aplt
"""
__Grids__

In **PyAutoLens**, a `Grid2D` is a set of two-dimensional $(y,x)$ coordinates (in arc-seconds) that are deflected and 
traced by a strong lensing system.

The $(y,x)$ coordinates on the `Grid2D` are aligned with the image we analyze, such that each coordinate maps to the 
centre of each image-pixel. Lets make a `Grid2D` on a grid of 100 x 100 pixels, with a pixel scale (arcsecond-to-pixel 
conversion factor) of 0.05", giving us a 5" x 5" grid.
"""
grid = al.Grid2D.uniform(shape_native=(100, 100), pixel_scales=0.05)
"""
First, lets plot this `Grid2D`, which shows that it is a fairly boring uniform `Grid2D` of dots.
"""
mat_plot_2d = aplt.MatPlot2D(title=aplt.Title(
    label="Fairly Boring Uniform Grid2D Of Dots"))

grid_plotter = aplt.Grid2DPlotter(grid=grid, mat_plot_2d=mat_plot_2d)
grid_plotter.figure_2d()
"""
We can print each coordinate of this `Grid2D`, revealing that it consists of a set of arc-second coordinates (where the 
spacing between each coordinate corresponds to the `pixel_scales` of 0.05" defined above).
"""
print("(y,x) pixel 0:")
print(grid.native[0, 0])
print("(y,x) pixel 1:")
print(grid.native[0, 1])
print("(y,x) pixel 2:")
print(grid.native[0, 2])
print("(y,x) pixel 100:")
print(grid.native[1, 0])
This coordinate `Grid` is aligned with the image we analyze, such that each coordinate on a  `Grid` maps to the centre 
of each image-pixel. Lets make a `Grid` using 100 x 100 pixels, with a pixel scale (arcsecond-to-pixel conversion 
factor) of 0.05", giving us a 5" x 5" grid.
"""

# %%
grid = al.Grid.uniform(shape_2d=(100, 100), pixel_scales=0.05)

# %%
"""
First, lets plot this `Grid`, which shows that it is a fairly bland uniform `Grid` of dots.
"""

# %%
mat_plot_2d = aplt.MatPlot2D(title=aplt.Title(
    label="Fairly Bland Uniform Grid Of Dots"))

grid_plotter = aplt.GridPlotter(grid=grid, mat_plot_2d=mat_plot_2d)
grid_plotter.figure()

# %%
"""
We can print each coordinate of this `Grid`, revealing that it consists of a set of arc-second coordinates (where the 
spacing between each coordinate corresponds to the `pixel_scales` of 0.05" defined above)
"""

# %%
print("(y,x) pixel 0:")
print(grid.in_2d[0, 0])
print("(y,x) pixel 1:")
print(grid.in_2d[0, 1])