Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
image_plane_grids=[li.grids])
fit = lensing_fitting.LensingProfileFit(lensing_images=[li], tracer=tracer)
assert (fit.model_images_of_planes[0][0] == g0_model_image).all()
assert (fit.model_images_of_planes[0][1] == g0_model_image).all()
tracer = ray_tracing.TracerImageSourcePlanes(lens_galaxies=[g0], source_galaxies=[g.Galaxy()],
image_plane_grids=[li.grids])
fit = lensing_fitting.LensingProfileFit(lensing_images=[li], tracer=tracer)
assert (fit.model_images_of_planes[0][0] == g0_model_image).all()
assert fit.model_images_of_planes[0][1] == None
tracer = ray_tracing.TracerImageSourcePlanes(lens_galaxies=[g.Galaxy()], source_galaxies=[g0],
image_plane_grids=[li.grids])
fit = lensing_fitting.LensingProfileFit(lensing_images=[li], tracer=tracer)
assert fit.model_images_of_planes[0][0] == None
assert (fit.model_images_of_planes[0][1] == g0_model_image).all()
tracer = ray_tracing.TracerImageSourcePlanes(lens_galaxies=[g.Galaxy()], source_galaxies=[g.Galaxy()],
image_plane_grids=[li.grids])
fit = lensing_fitting.LensingProfileFit(lensing_images=[li], tracer=tracer)
assert fit.model_images_of_planes[0][0] == None
assert fit.model_images_of_planes[0][1] == None
def make_galaxy_mass():
return g.Galaxy(mass=mp.SphericalIsothermal(einstein_radius=1.0), redshift=1.0)
def test___x2_galaxies__3x3_padded_image__asymetric_psf_blurring(self):
psf = im.PSF(array=(np.array([[0.0, 3.0, 0.0],
[0.0, 1.0, 2.0],
[0.0, 0.0, 0.0]])), pixel_scale=1.0)
mask = msk.Mask(array=np.array([[True, True, True],
[True, False, True],
[True, True, True]]), pixel_scale=1.0)
padded_grid_stack = grids.GridStack.padded_grid_stack_from_mask_sub_grid_size_and_psf_shape(mask=mask,
sub_grid_size=1,
psf_shape=(3, 3))
g0 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=0.1))
g1 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=0.2))
tracer = ray_tracing.TracerImagePlane(lens_galaxies=[g0, g1], image_plane_grid_stack=padded_grid_stack)
manual_blurred_image_0 = tracer.image_plane.image_plane_image_1d
manual_blurred_image_0 = padded_grid_stack.regular.map_to_2d_keep_padded(padded_array_1d=manual_blurred_image_0)
manual_blurred_image_0 = psf.convolve(array=manual_blurred_image_0)
unmasked_blurred_image_of_planes = \
util.unmasked_blurred_image_of_planes_from_padded_grid_stack_and_psf(planes=tracer.planes,
padded_grid_stack=padded_grid_stack, psf=psf)
assert unmasked_blurred_image_of_planes[0] == \
pytest.approx(manual_blurred_image_0[1:4, 1:4], 1.0e-4)
def test_make_pixelization_variable(self):
instance = af.ModelInstance()
mapper = af.ModelMapper()
mapper.lens_galaxy = gm.GalaxyModel(
redshift=g.Redshift, pixelization=px.Rectangular, regularization=rg.Constant
)
mapper.source_galaxy = gm.GalaxyModel(
redshift=g.Redshift, light=lp.EllipticalLightProfile
)
assert mapper.prior_count == 9
instance.lens_galaxy = g.Galaxy(
pixelization=px.Rectangular(), regularization=rg.Constant(), redshift=1.0
)
instance.source_galaxy = g.Galaxy(
redshift=1.0, light=lp.EllipticalLightProfile()
)
# noinspection PyTypeChecker
phase = phase_extensions.VariableFixingHyperPhase(
# The psf will be output as '/workspace/data/example1/psf.fits'.
# (these files are already in the workspace and are remade running this script)
lens_name = 'multi_plane'
pixel_scale = 0.05
# Simulate a simple Gaussian PSF for the image.
psf = ccd.PSF.simulate_as_gaussian(shape=(11, 11), sigma=0.05, pixel_scale=pixel_scale)
# Setup the image-plane grid stack of the CCD array which will be used for generating the image-plane image of the
# simulated strong lens.
image_plane_grid_stack = grids.GridStack.grid_stack_for_simulation(shape=(400, 400), pixel_scale=pixel_scale,
psf_shape=psf.shape, sub_grid_size=1)
# Setup the lens galaxy's light mass (SIE) and source galaxy light (elliptical Sersic) for this simulated lens.
lens_galaxy = g.Galaxy(mass=mp.EllipticalIsothermal(centre=(0.0, 0.0), einstein_radius=1.6, axis_ratio=0.7, phi=45.0),
shear=mp.ExternalShear(magnitude=0.05, phi=90.0),
redshift=0.5)
source_galaxy = g.Galaxy(light=lp.EllipticalSersic(centre=(0.1, 0.1), axis_ratio=0.8, phi=60.0,
intensity=1.0, effective_radius=1.0, sersic_index=2.5),
redshift=1.0)
# Setup our line-of-sight (los) galaxies using Spherical Sersic profiles for their light and Singular
# Isothermal Sphere (SIS) profiles. We'll use 3 galaxies, but you can add more if desired.
los_0 = g.Galaxy(light=lp.SphericalSersic(centre=(4.0, 4.0), intensity=0.30, effective_radius=0.3, sersic_index=2.0),
mass=mp.SphericalIsothermal(centre=(4.0, 4.0), einstein_radius=0.02),
redshift=0.25)
los_1 = g.Galaxy(light=lp.SphericalSersic(centre=(3.6, -5.3), intensity=0.20, effective_radius=0.6, sersic_index=1.5),
mass=mp.SphericalIsothermal(centre=(3.6, -5.3), einstein_radius=0.04),
redshift=0.75)
los_2 = g.Galaxy(light=lp.SphericalSersic(centre=(-3.1, -2.4), intensity=0.35, effective_radius=0.4, sersic_index=2.5),
def simulate():
from autolens.data.array import grids
from autolens.model.galaxy import galaxy as g
from autolens.lens import ray_tracing
psf = ccd.PSF.simulate_as_gaussian(shape=(11, 11), sigma=0.05, pixel_scale=0.05)
image_plane_grid_stack = grids.GridStack.grid_stack_for_simulation(shape=(180, 180), pixel_scale=0.05, psf_shape=(11, 11))
lens_galaxy = g.Galaxy(mass=mp.EllipticalIsothermal(centre=(0.0, 0.0), axis_ratio=0.8, phi=135.0,
einstein_radius=1.6))
source_galaxy_0 = g.Galaxy(light=lp.EllipticalSersic(centre=(0.1, 0.1), axis_ratio=0.8, phi=90.0, intensity=0.2,
effective_radius=0.3, sersic_index=1.0))
tracer = ray_tracing.TracerImageSourcePlanes(lens_galaxies=[lens_galaxy],
source_galaxies=[source_galaxy_0],
image_plane_grid_stack=image_plane_grid_stack)
return ccd.CCDData.simulate(array=tracer.image_plane_image_for_simulation, pixel_scale=0.05,
exposure_time=300.0, psf=psf, background_sky_level=0.1, add_noise=True)
print(galaxy_intensities[0])
print('intensity of regular-grid pixel 2:')
print(galaxy_intensities[1])
print('intensity of regular-grid pixel 3:')
print(galaxy_intensities[2])
print('etc.')
# A galaxy plotter allows us to the plot the image, just like the profile plotters did for a light
# profile (again, mapping the 1D image to 2D).
galaxy_plotters.plot_intensities(galaxy=galaxy_with_light_profile, grid=grid_stack.regular)
# We can pass galaxies as many profiles as we like. Lets create a galaxy with three light profiles.
light_profile_1 = light_profiles.SphericalSersic(centre=(0.0, 0.0), intensity=1.0, effective_radius=1.0, sersic_index=2.5)
light_profile_2 = light_profiles.SphericalSersic(centre=(1.0, 1.0), intensity=1.0, effective_radius=2.0, sersic_index=3.0)
light_profile_3 = light_profiles.SphericalSersic(centre=(1.0, -1.0), intensity=1.0, effective_radius=2.0, sersic_index=2.0)
galaxy_with_3_light_profiles = galaxy.Galaxy(light_1=light_profile_1, light_2=light_profile_2, light_3=light_profile_3)
# We can print the galaxy to confirm it possesses the Sersic light-profiles above.
print(galaxy_with_3_light_profiles)
# If we plot the galaxy, we see 3 blobs of light!
galaxy_plotters.plot_intensities(galaxy=galaxy_with_3_light_profiles, grid=grid_stack.regular)
# We can also plot each individual light profile using the 'subplot' galaxy plotter.
galaxy_plotters.plot_intensities_subplot(galaxy=galaxy_with_3_light_profiles, grid=grid_stack.regular)
# Mass profiles interact with Galaxy objects in the exact same way as light profiles.
# Lets create a galaxy with three SIS mass profiles.
mass_profile_1 = mass_profiles.SphericalIsothermal(centre=(0.0, 0.0), einstein_radius=1.0)
mass_profile_2 = mass_profiles.SphericalIsothermal(centre=(1.0, 1.0), einstein_radius=1.0)
mass_profile_3 = mass_profiles.SphericalIsothermal(centre=(1.0, -1.0), einstein_radius=1.0)
galaxy_with_3_mass_profiles = galaxy.Galaxy(mass_1=mass_profile_1, mass_2=mass_profile_2, mass_3=mass_profile_3)
def simulate():
from autolens.data.array import grids
from autolens.model.galaxy import galaxy as g
from autolens.lens import ray_tracing
psf = ccd.PSF.simulate_as_gaussian(shape=(11, 11), sigma=0.05, pixel_scale=0.05)
image_plane_grid_stack = grids.GridStack.grid_stack_for_simulation(shape=(180, 180), pixel_scale=0.05, psf_shape=(11, 11))
lens_galaxy_0 = g.Galaxy(light=lp.EllipticalSersic(centre=(0.0, -1.0), axis_ratio=0.8, phi=55.0, intensity=0.1,
effective_radius=0.8, sersic_index=2.5),
mass=mp.EllipticalIsothermal(centre=(1.0, 0.0), axis_ratio=0.7, phi=45.0,
einstein_radius=1.0))
lens_galaxy_1 = g.Galaxy(light=lp.EllipticalSersic(centre=(0.0, 1.0), axis_ratio=0.8, phi=100.0, intensity=0.1,
effective_radius=0.6, sersic_index=3.0),
mass=mp.EllipticalIsothermal(centre=(-1.0, 0.0), axis_ratio=0.8, phi=90.0,
einstein_radius=0.8))
source_galaxy = g.Galaxy(light=lp.SphericalExponential(centre=(0.05, 0.15), intensity=0.2, effective_radius=0.5))
tracer = ray_tracing.TracerImageSourcePlanes(lens_galaxies=[lens_galaxy_0, lens_galaxy_1],
source_galaxies=[source_galaxy],
image_plane_grid_stack=image_plane_grid_stack)
return ccd.CCDData.simulate(array=tracer.image_plane_image_for_simulation, pixel_scale=0.05,
def simulate_two_source_galaxies():
from autolens.data.array import grids
from autolens.model.galaxy import galaxy as g
from autolens.lens import ray_tracing
psf = ccd.PSF.simulate_as_gaussian(shape=(11, 11), sigma=0.1, pixel_scale=0.1)
image_plane_grid_stack = grids.GridStack.grid_stack_for_simulation(shape=(130, 130), pixel_scale=0.1, psf_shape=(11, 11))
lens_galaxy = g.Galaxy(mass=mp.SphericalIsothermal(centre=(0.0, 0.0), einstein_radius=1.6))
source_galaxy_0 = g.Galaxy(light=lp.SphericalExponential(centre=(1.0, 0.0), intensity=0.2, effective_radius=0.2))
source_galaxy_1 = g.Galaxy(light=lp.SphericalExponential(centre=(-1.0, 0.0), intensity=0.2, effective_radius=0.2))
tracer = ray_tracing.TracerImageSourcePlanes(lens_galaxies=[lens_galaxy], source_galaxies=[source_galaxy_0,
source_galaxy_1],
image_plane_grid_stack=image_plane_grid_stack)
ccd_simulated = ccd.CCDData.simulate(array=tracer.image_plane_image_for_simulation, pixel_scale=0.1,
exposure_time=300.0, psf=psf, background_sky_level=0.1, add_noise=True)
return ccd_simulated
# (You may notice we include a border in the tracer, instead of setting it to None - just ignore this for now, as we'll
# be covering borders in the next tutorial)
def perform_fit_with_source_galaxy(source_galaxy):
image = simulate()
mask = ma.Mask.circular_annular(shape=image.shape, pixel_scale=image.pixel_scale, inner_radius_arcsec=0.5,
outer_radius_arcsec=2.2)
lensing_image = li.LensingImage(data=image, mask=mask)
lens_galaxy = g.Galaxy(
mass=mp.EllipticalIsothermal(centre=(0.0, 0.0), axis_ratio=0.8, phi=135.0, einstein_radius=1.6))
tracer = ray_tracing.TracerImageSourcePlanes(lens_galaxies=[lens_galaxy], source_galaxies=[source_galaxy],
image_plane_grids=[lensing_image.grids], border=lensing_image.border)
return lensing_fitting.fit_lensing_image_with_tracer(lensing_image=lensing_image, tracer=tracer)
# Okay, so lets look at our fit_normal from the previous tutorial in more detail. We'll use a higher resolution 40 x 40 grid.
source_galaxy = g.Galaxy(pixelization=pix.Rectangular(shape=(40, 40)), regularization=reg.Constant(coefficients=(1.0,)))
fit = perform_fit_with_source_galaxy(source_galaxy=source_galaxy)
lensing_fitting_plotters.plot_fitting_subplot(fit=fit)
# It still looks pretty good! However, this is because I sneakily chose a regularization coefficient that gives a
# good looking solution, without telling you. If we reduce this regularization coefficient to zero, our source
# reconstruction goes extremely weird.
source_galaxy = g.Galaxy(pixelization=pix.Rectangular(shape=(40, 40)), regularization=reg.Constant(coefficients=(0.0,)))
no_regularization_fit = perform_fit_with_source_galaxy(source_galaxy=source_galaxy)
lensing_fitting_plotters.plot_fitting_subplot(fit=no_regularization_fit)
# So, whats happening here, and why does removing 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_normal the regular. It is also
# 'regularized', whereby we go to every pixel on our rectangular grid and compare its reconstructed flux with its
# neighboring pixels. If the difference in flux is large, we penalize this solution, reducing its likelihood. You can
# think of this as us smoothing' our solution.