Ejemplo n.º 1
0
def test__image_and_mapper_subplot_is_output_for_all_mappers(
        imaging_7x7, rectangular_mapper_7x7_3x3, voronoi_mapper_9_3x3,
        plot_path, plot_patch):

    critical_curves = [(0.0, 0.0), (0.1, 0.1)]
    caustics = [(0.0, 0.0), (0.1, 0.1)]

    aplt.mapper.subplot_image_and_mapper(
        image=imaging_7x7.image,
        mapper=rectangular_mapper_7x7_3x3,
        critical_curves=critical_curves,
        caustics=caustics,
        include=aplt.Include(inversion_pixelization_grid=True,
                             inversion_grid=True,
                             inversion_border=True),
        image_pixel_indexes=[[0, 1, 2], [3]],
        source_pixel_indexes=[[1, 2], [0]],
        sub_plotter=aplt.SubPlotter(
            output=aplt.Output(path=plot_path, format="png")),
    )
    assert plot_path + "subplot_image_and_mapper.png" in plot_patch.paths

    aplt.mapper.subplot_image_and_mapper(
        image=imaging_7x7.image,
        mapper=voronoi_mapper_9_3x3,
        critical_curves=critical_curves,
        caustics=caustics,
        include=aplt.Include(inversion_pixelization_grid=True,
                             inversion_grid=True,
                             inversion_border=True),
        image_pixel_indexes=[[0, 1, 2], [3]],
        source_pixel_indexes=[[1, 2], [0]],
        sub_plotter=aplt.SubPlotter(
            output=aplt.Output(path=plot_path, format="png")),
    )
    assert plot_path + "subplot_image_and_mapper.png" in plot_patch.paths

# And now, we'll use the same magnification based source to fit this data.

source_magnification = al.Galaxy(
    redshift=1.0,
    pixelization=al.pix.VoronoiMagnification(shape=(30, 30)),
    regularization=al.reg.Constant(coefficient=3.3),
)

fit = fit_masked_imaging_with_source_galaxy(masked_imaging=masked_imaging,
                                            source_galaxy=source_magnification)

aplt.fit_imaging.subplot_fit_imaging(
    fit=fit,
    include=aplt.Include(inversion_image_pixelization_grid=True, mask=True))

aplt.inversion.reconstruction(
    inversion=fit.inversion,
    include=aplt.Include(inversion_pixelization_grid=True))

# The fit isn't great. The main structure of the lensed source is reconstructed, but there are residuals. These
# residuals are worse than we saw in the previous tutorials (when source's compact central structure was the problem).
# So, the obvious question is can our adaptive pixelization and regularization schemes address the problem?

# Lets find out, using this solution as our hyper-galaxy-image. In this case, our hyper-galaxy-image isn't a perfect
# fit to the dataset. This shouldn't be too problematic, as the solution still captures the source's overall structure.
# The pixelization / regularization hyper-galaxy-parameters have enough flexibility in how they use this image to adapt
# themselves, so the hyper-galaxy-image doesn't *need* to be perfect.

hyper_image = fit.model_image.in_1d_binned
Ejemplo n.º 3
0
Given the perfect symmetry of the system, every path the source's light takes around the lens galaxy is radially 
identical. Thus, nothing else but a ring of light can form!

This is called an 'Einstein Ring' and its radius is called the 'Einstein Radius', which are both named after the man 
who famously used gravitational lensing to prove his theory of general relativity.

Finally, because we know our source-`Galaxy`'s `LightProfile`, we can also plot its `plane-image`. This image is how the 
source intrinsically appears in the source-plane (e.g. without lensing). This is a useful thing to know, because the 
source-s light is highly magnified, meaning astronomers can study it in a lot more detail than would otherwise be 
possible!
"""

# %%
aplt.Plane.plane_image(
    plane=source_plane, grid=source_plane_grid, include=aplt.Include(grid=True)
)

# %%
"""
Plotting the `Grid` over the plane image obscures its appearance, which isn't ideal. We can of course tell **PyAutoLens** 
not to plot the grid.
"""

# %%
aplt.Plane.plane_image(
    plane=source_plane, grid=source_plane_grid, include=aplt.Include(grid=False)
)

# %%
"""
Ejemplo n.º 4
0
                                    einstein_radius=1.6),
)

masked_imaging = al.masked.imaging(imaging=imaging, mask=mask)

source_galaxy = al.Galaxy(
    redshift=1.0,
    pixelization=al.pix.Rectangular(shape=(40, 40)),
    regularization=al.reg.Constant(coefficient=0.5),
)

tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

fit = al.fit(masked_dataset=masked_imaging, tracer=tracer)

aplt.fit_imaging.subplot_fit_imaging(fit=fit, include=aplt.Include(mask=True))

# Okay, so lets think about the rectangular pixelization. Is this the optimal way to reconstruct our source? Are there
# features in the source-plane that arn't ideal? How do you think we could do a better job?

# Well, given we're doing a whole tutorial on using a different pixelization to the rectangular grid, you've probably
# guessed that it isn't optimal. Infact, its pretty rubbish, and not a pixelization we should actually want to model
# many lenses with!

# So what is wrong with the grid? Well, lets think about the source reconstruction.
aplt.inversion.reconstruction(
    inversion=fit.inversion,
    include=aplt.Include(inversion_pixelization_grid=True))

# There is one clear problem, we are using a small number of the total source pixels to reconstruct the source. The
# majority of source pixels are located away from the source. By my estimate, we're using just 16 pixels (the
Ejemplo n.º 5
0
looking solution. If we reduce this regularization_coefficient to zero, our source reconstruction goes weird.
"""

# %%
source_galaxy = al.Galaxy(
    redshift=1.0,
    pixelization=al.pix.Rectangular(shape=(40, 40)),
    regularization=al.reg.Constant(coefficient=0.0),
)

no_regularization_fit = perform_fit_with_source_galaxy(
    imaging=imaging, source_galaxy=source_galaxy
)

aplt.FitImaging.subplot_fit_imaging(
    fit=no_regularization_fit, include=aplt.Include(mask=True)
)

# %%
"""
So, what`s happening here? Why does reducing the `Regularization` do this to our source reconstruction?

When our `Inversion` reconstructs a source, it doesn`t *just* compute the set of fluxes that best-fit the image. It 
also `regularizes` this solution, going to every pixel on our rectangular `Grid` and comparing its reconstructed flux 
with its 4 neighboring pixels. If the difference in flux is large the solution is penalized, reducing its log 
likelihood. You can think of this as us applying a prior that our source galaxy solution is `smooth`.

This 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 regularization_coefficient. By setting the 
regularization coefficient to zero, we set this penalty term to zero, meaning that `Regularization`.s omitted.
Ejemplo n.º 6
0
noise_map_path = dataset_path + "/noise_map.fits"

imaging = al.imaging.from_fits(
    image_path=image_path,
    psf_path=psf_path,
    noise_map_path=noise_map_path,
    pixel_scales=0.03,
)

pipeline = lens_sersic_sie__source_sersic.make_pipeline(
    phase_folders=[dataset_label, dataset_name], with_shear=True
)

aplt.array(
    array=model_image, critical_curves=galaxy.critical_curves, caustics=galaxy.caustics
)

aplt.fit_imaging.subplot_fit_imaging(
    fit=fit, include=aplt.Include(critical_curves=True, grid=False)
)

# Now we run the pipeline on the dataset to get the result. If a mask was supplied to the pipeline when it was run, it is
# important the same mask is supplied in this run statement.

# The skip_optimizer boolean flag means that the non-linear searches will not run, and visualization will be skipped.
# This ensures the running of the pipeline is fast.

result = pipeline.run(dataset=imaging, skip_optimizer=True)

print(result)
Ejemplo n.º 7
0
    redshift=0.5,
    mass=al.lmp.EllipticalSersic(centre=(0.0, 0.0),
                                 intensity=0.1,
                                 mass_to_light_ratio=1.0),
)

source_galaxy = al.Galaxy(
    redshift=1.0,
    light=al.lp.EllipticalSersic(
        centre=(0.0, 0.0),
        elliptical_comps=(-0.055555, 0.096225),
        intensity=0.1,
        effective_radius=0.5,
        sersic_index=3.0,
    ),
)

masked_imaging = al.MaskedImaging(
    imaging=imaging,
    mask=mask,
    settings=al.SettingsMaskedImaging(grid_class=al.GridInterpolate,
                                      pixel_scales_interp=0.1,
                                      sub_size=1),
)

tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy])
fit = al.FitImaging(masked_imaging=masked_imaging, tracer=tracer)
aplt.FitImaging.subplot_fit_imaging(fit=fit,
                                    include=aplt.Include(critical_curves=False,
                                                         caustics=False))
Ejemplo n.º 8
0
    positions = solver.solve(
        lensing_obj=tracer,
        source_plane_coordinate=tracer.source_plane.galaxies[0].light.centre,
    )

    positions_true = al.GridCoordinates.load(file_path=pickle_path,
                                             filename=f"positions_{str(i)}")

    minimum_separations = util.minimum_separations_from(
        positions_true=positions, positions=positions_true)
    in_positions_true = util.check_if_positions_in_positions_true(
        positions_true=positions_true, positions=positions, threshold=0.1)

    print()
    print(positions_true.in_list)
    print(positions.in_list)
    print(minimum_separations)
    print(in_positions_true)

    positions_plot = al.GridCoordinates(
        coordinates=[positions.in_list[0], positions_true.in_list[0]])

    aplt.Tracer.image(
        tracer=tracer,
        grid=grid,
        positions=positions_plot,
        include=aplt.Include(origin=False,
                             critical_curves=True,
                             caustics=False),
    )
Ejemplo n.º 9
0
# %%
"""
So, what is a border? In the image-plane, a border is the set of exterior pixels in a mask that are at, well, its 
border. Lets plot the image with a circular _Mask_, and tell our _Imaging_ _Plotter_ to plot the border as well.
"""

# %%
mask_circular = al.Mask.circular(shape_2d=imaging.shape_2d,
                                 pixel_scales=imaging.pixel_scales,
                                 sub_size=2,
                                 radius=2.5)

aplt.Imaging.subplot_imaging(imaging=imaging,
                             mask=mask_circular,
                             include=aplt.Include(border=True))

# %%
"""
As you can see, for a circular _Mask_, the border *is* the edge of our _Mask_ (the ring of black dots we're used to 
seeing whenever we plot a _Mask_). For an annular _Mask_, not every pixel on the edge of the mask is necessarily a part 
of its border!
"""

# %%
mask_annular = al.Mask.circular_annular(
    shape_2d=imaging.shape_2d,
    pixel_scales=imaging.pixel_scales,
    sub_size=2,
    inner_radius=0.8,
    outer_radius=2.5,
    labels=aplt.Labels.sub(ysize=6, xsize=6, titlesize=12),
)

aplt.Imaging.subplot_imaging(imaging=imaging, sub_plotter=sub_plotter)

# %%
"""
Again, you can customize the default appearance of subplots by editing the config file 
autolens_workspace/config/visualize/subplots.ini`.

The other thing we can do with figures is choose what we include in the plot. For example, we can choose whether to 
include the origin of the coordinate system on our plot of the image:
"""

# %%
aplt.Imaging.image(imaging=imaging, plotter=plotter, include=aplt.Include(origin=True))

aplt.Imaging.image(imaging=imaging, plotter=plotter, include=aplt.Include(origin=False))

# %%
"""
Throughout the **HowToLens** lecture series you'll see lots more objects that can include on figures.

Just like the matplotlib setup, you can customize what does and does not appear on figures by default using the 
config file `autolens_workspace/config/visualize/general.ini`

Great! Hopefully, visualization in **PyAutoLens** is displaying nicely for us to get on with the **HowToLens** lecture series.
"""

# %%
Ejemplo n.º 11
0
    creates the fit`s `model_image`.

    2) Computes the difference between this model_image and the observed image-data, creating the fit`s `residual_map`.

    3) Divides the residual-map by the noise-map, creating the fit`s `normalized_residual_map`.

    4) Squares every value in the normalized residual-map, creating the fit`s `chi_squared_map`.

    5) Sums up these chi-squared values and converts them to a `log_likelihood`, which quantifies how good the tracer`s 
       fit to the data was (higher log_likelihood = better fit).
"""

# %%
fit = al.FitImaging(masked_imaging=masked_imaging, tracer=tracer)

aplt.FitImaging.subplot_fit_imaging(fit=fit, include=aplt.Include(mask=True))

# %%
"""
We can print the fit`s attributes. As usual, we can choose whether to return the fits in 2d or 1d, and in 2d if we 
don't specify where we'll get all zeros, as the edges were masked:
"""

# %%
print("Model-Image:")
print(fit.model_image.in_2d)
print(fit.model_image.in_1d)
print()
print("Residual Maps:")
print(fit.residual_map.in_2d)
print(fit.residual_map.in_1d)
Ejemplo n.º 12
0
    grid=masked_imaging.grid)[1]

# %%
"""
We'll use another rectangular _Pixelization_ and _Mapper_ to perform the reconstruction.
"""

# %%
rectangular = al.pix.Rectangular(shape=(25, 25))

mapper = rectangular.mapper_from_grid_and_sparse_grid(grid=source_plane_grid)

aplt.Mapper.subplot_image_and_mapper(
    image=imaging.image,
    mapper=mapper,
    include=aplt.Include(mask=True, inversion_grid=True),
)

# %%
"""
And now, finally, we're going to use our _Mapper_ to invert the image using an _Inversion_. I'll explain how this 
works in a second - but lets just go ahead and use the _Inversion_ first. (Ignore the 'regularization' input below for 
now, we'll cover this in the next tutorial).
"""

# %%
inversion = al.Inversion(
    masked_dataset=masked_imaging,
    mapper=mapper,
    regularization=al.reg.Constant(coefficient=1.0),
)
Ejemplo n.º 13
0
Given the perfect symmetry of the system, every path the source's light takes around the lens galaxy is radially 
identical. Thus, nothing else but a ring of light can form!

This is called an 'Einstein Ring' and its radius is called the 'Einstein Radius', which are both named after the man 
who famously used gravitational lensing to prove his theory of general relativity.

Finally, because we know our source-galaxy's _LightProfile_, we can also plot its 'plane-image'. This image is how the 
source intrinsically appears in the source-plane (e.g. without lensing). This is a useful thing to know, because the 
source-s light is highly magnified, meaning astronomers can study it in a lot more detail than would otherwise be 
possible!
"""

# %%
aplt.Plane.plane_image(plane=source_plane,
                       grid=source_plane_grid,
                       include=aplt.Include(grid=True))

# %%
"""
Plotting the _Grid_ over the plane image obscures its appearance, which isn't ideal. We can of course tell __PyAutoLens__ 
not to plot the grid.
"""

# %%
aplt.Plane.plane_image(plane=source_plane,
                       grid=source_plane_grid,
                       include=aplt.Include(grid=False))

# %%
"""
For _MassProfile_'s, you can also plot their 'critical curve' and 'caustics', which for those unfamiliar with lensing 
tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

aplt.tracer.profile_image(tracer=tracer, grid=masked_imaging.grid)

# To fit the image, we pass the lens dataset and tracer to a LensDataFit object. This performs the following steps:

# 1) Blurs the tracer image with the lens dataset's PSF, ensuring that the telescope optics are accounted for by the fit.
#    This creates the fit's 'model_image'.
# 2) Computes the difference between this model_image and the observed image, creating the fit's 'residual_map'.
# 3) Divides the residuals by the noise-map and squares each value, creating the fit's 'chi_squared_map'.
# 4) Sums up these chi-squared values and converts them to a 'likelihood', which quantifies how good the tracer's
#    fit to the dataset was (higher likelihood = better fit).

fit = al.fit(masked_dataset=masked_imaging, tracer=tracer)

aplt.fit_imaging.subplot_fit_imaging(fit=fit, include=aplt.Include(mask=True))

# We can create a sub-plotters of the fit to an individual place in the tracer, showing:

# 1) The observed image (again).
# 2) The part of the observed image that that plane's galaxies are fitting.
# 3) The model image of that plane's galaxies
# 4) The model galaxy in the (unlensed) source plane.

aplt.fit_imaging.subplots_of_all_planes(fit=fit, include=aplt.Include(mask=True))

# A fit also provides a likelihood, which is a single-figure estimate of how good the model image fitted the
# simulated image (in unmasked pixels only!).

print("Likelihood:")
print(fit.likelihood)
Ejemplo n.º 15
0
import autolens as al
import autolens.plot as aplt

grid = al.Grid.uniform(shape_2d=(100, 100), pixel_scales=0.05, sub_size=4)

sis_mass_profile = al.mp.EllipticalIsothermal(centre=(1.0, 1.0),
                                              einstein_radius=1.6,
                                              axis_ratio=0.7)

aplt.MassProfile.convergence(
    mass_profile=sis_mass_profile,
    grid=grid,
    include=aplt.Include(critical_curves=True, caustics=True),
)

# galaxy = al.Galaxy(mass=sis_mass_profile, redshift=1)
#
# aplt.Galaxy.convergence(
#     galaxy=galaxy,
#     grid=grid,
#     include_critical_curves=False,
#     include_caustics=True,
# )
        centre=(0.0, 0.0), axis_ratio=0.8, phi=45.0, einstein_radius=1.6
    ),
)

tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, al.Galaxy(redshift=1.0)])

source_plane_grid = tracer.traced_grids_of_planes_from_grid(grid=grid)[1]

# Next, we setup our pixelization and mapper using the tracer's source-plane grid.
rectangular = al.pix.Rectangular(shape=(25, 25))

mapper = rectangular.mapper_from_grid_and_sparse_grid(grid=source_plane_grid)

# We're going to plot our mapper alongside the image we used to generate the source-plane grid.
aplt.mapper.subplot_image_and_mapper(
    image=imaging.image, mapper=mapper, include=aplt.Include(inversion_grid=True)
)

# The pixels in the image map to the pixels in the source-plane, and visa-versa. Lets highlight a set of
# image-pixels in both the image and source-plane.
aplt.mapper.subplot_image_and_mapper(
    image=imaging.image,
    mapper=mapper,
    include=aplt.Include(inversion_grid=True),
    image_pixel_indexes=[
        [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
        [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000],
    ],
)

# That's nice, and we can see the mappings, but it isn't really what we want to know, is it? We really want to go the
source_plane_grid = tracer.traced_grids_of_planes_from_grid(grid=masked_imaging.grid)[1]

# %%
"""
we'll use another rectangular `Pixelization` and `Mapper` to perform the reconstruction.
"""

# %%
rectangular = al.pix.Rectangular(shape=(25, 25))

mapper = rectangular.mapper_from_grid_and_sparse_grid(grid=source_plane_grid)

aplt.Mapper.subplot_image_and_mapper(
    image=imaging.image,
    mapper=mapper,
    include=aplt.Include(mask=True, inversion_grid=True),
)

# %%
"""
And now, finally, we're going to use our `Mapper` to invert the image using an `Inversion`. I'll explain how this 
works in a second - but lets just go ahead and use the `Inversion` first. (Ignore the `regularization` input below for 
now, we'll cover this in the next tutorial).
"""

# %%
inversion = al.Inversion(
    masked_dataset=masked_imaging,
    mapper=mapper,
    regularization=al.reg.Constant(coefficient=1.0),
)
Ejemplo n.º 18
0
    redshift=1.0,
    light=al.lp.EllipticalSersic(
        centre=(0.1, 0.1),
        axis_ratio=0.7,
        phi=65.0,
        intensity=1.0,
        effective_radius=0.4,
        sersic_index=3.5,
    ),
)

tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

fit = al.fit(masked_dataset=masked_imaging, tracer=tracer)

aplt.fit_imaging.subplot_fit_imaging(fit=fit, include=aplt.Include(mask=True))

# Clearly, the model provides a terrible fit and this tracer is not a plausible representation of
# the image-simulator (of course, we already knew that, given that we simulated it!)

# The likelihood drops dramatically, as expected.
print("Previous Likelihoods:")
print(11697.24)
print(10319.44)
print("New Likelihood:")
print(fit.likelihood)

# Congratulations, you've fitted your first strong lens with PyAutoLens! Perform the following exercises:

# 1) In this example, we 'knew' the correct solution, because we simulated the lens ourselves. In the real Universe,
#    we have no idea what the correct solution is. How would you go about finding the correct solution?
Ejemplo n.º 19
0
"""

# %%
print(type(mapper))

# %%
"""
By plotting our mapper, we now see our _Pixelization_. Its a fairly boring _Grid_ of rectangular pixels.

(we'll cover what the 'inversion' means in a later tutorial).
"""

# %%
aplt.MapperObj(
    mapper=mapper,
    include=aplt.Include(inversion_grid=False,
                         inversion_pixelization_grid=False),
    plotter=aplt.Plotter(labels=aplt.Labels(
        title="Fairly Boring Grid of Rectangular Pixels")),
)

# %%
"""
However, the _Mapper_ does contain lots of interesting information about our _Pixelization_, for example its 
pixelization_grid tells us where the pixel centers are located.
"""

# %%
print("Rectangular Grid Pixel Centre 1:")
print(mapper.pixelization_grid[0])
print("Rectangular Grid Pixel Centre 2:")
print(mapper.pixelization_grid[1])
Ejemplo n.º 20
0
    mass=al.mp.EllipticalIsothermal(
        centre=(0.0, 0.0), elliptical_comps=(0.1, 0.0), einstein_radius=0.8
    ),
)

source_galaxy = al.Galaxy(
    redshift=1.0,
    pixelization=al.pix.Rectangular(shape=(40, 40)),
    regularization=al.reg.Constant(coefficient=1.0),
)

fit = perform_fit_with_lens__source_galaxy(
    imaging=imaging, lens_galaxy=lens_galaxy, source_galaxy=source_galaxy
)

aplt.FitImaging.subplot_fit_imaging(fit=fit, include=aplt.Include(mask=True))
aplt.FitImaging.subplot_of_plane(
    fit=fit, plane_index=1, include=aplt.Include(mask=True)
)

# %%
"""
What happened!? This incorrect mass-model provides a really good_fit to the image! The residuals and chi-squared-map 
are as good as the ones we saw in the last tutorial.

How can an incorrect lens model provide such a fit? Well, as I'm sure you noticed, the source has been reconstructed 
as a demagnified version of the image. Clearly, this isn't a physical solution or a solution that we want our 
non-linear search to find, but for _Inversion_'s these solutions are real; they exist.

This isn't necessarily problematic for lens modeling. Afterall, the source reconstruction above is extremely complex, 
in that it requires a lot of pixels to fit the image accurately. Indeed, its Bayesian log evidence is much lower than