示例#1
0
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 = g.Galaxy(light=lp.EllipticalSersic(centre=(0., 0.),
                                                       axis_ratio=0.8,
                                                       phi=90.0,
                                                       intensity=0.2,
                                                       effective_radius=1.0,
                                                       sersic_index=1.5))

    tracer = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy],
        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,
                                exposure_time=300.0,
                                psf=psf,
                                background_sky_level=0.1,
                                add_noise=True)
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.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 = g.Galaxy(light=lp.SphericalExponential(
        centre=(0.0, 0.0), intensity=0.2, effective_radius=0.2))
    tracer = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy],
        source_galaxies=[source_galaxy],
        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
def make_fit(lens_data, galaxy_light, galaxy_mass):
    tracer = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[galaxy_mass],
        source_galaxies=[galaxy_light],
        image_plane_grid_stack=lens_data.grid_stack,
        cosmology=cosmo.Planck15)
    return lens_fit.fit_lens_data_with_tracer(lens_data=lens_data,
                                              tracer=tracer)
示例#4
0
    def test__tracer_and_tracer_sensitive_are_identical__added__likelihood_is_noise_term(
            self, lens_data_blur):

        g0 = g.Galaxy(mass_profile=mp.SphericalIsothermal(einstein_radius=1.0))
        g1 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=2.0))

        tracer = ray_tracing.TracerImageSourcePlanes(
            lens_galaxies=[g0],
            source_galaxies=[g1],
            image_plane_grid_stack=lens_data_blur.grid_stack)

        fit = sensitivity_fit.SensitivityProfileFit(lens_data=lens_data_blur,
                                                    tracer_normal=tracer,
                                                    tracer_sensitive=tracer)

        assert (fit.fit_normal.image == lens_data_blur.image).all()
        assert (fit.fit_normal.noise_map == lens_data_blur.noise_map).all()

        model_image_1d = util.blurred_image_1d_from_1d_unblurred_and_blurring_images(
            unblurred_image_1d=tracer.image_plane_image_1d,
            blurring_image_1d=tracer.image_plane_blurring_image_1d,
            convolver=lens_data_blur.convolver_image)

        model_image = lens_data_blur.map_to_scaled_array(
            array_1d=model_image_1d)

        assert (fit.fit_normal.model_image == model_image).all()

        residual_map = fit_util.residual_map_from_data_mask_and_model_data(
            data=lens_data_blur.image,
            mask=lens_data_blur.mask,
            model_data=model_image)
        assert (fit.fit_normal.residual_map == residual_map).all()

        chi_squared_map = fit_util.chi_squared_map_from_residual_map_noise_map_and_mask(
            residual_map=residual_map,
            mask=lens_data_blur.mask,
            noise_map=lens_data_blur.noise_map)

        assert (fit.fit_normal.chi_squared_map == chi_squared_map).all()

        assert (fit.fit_sensitive.image == lens_data_blur.image).all()
        assert (fit.fit_sensitive.noise_map == lens_data_blur.noise_map).all()
        assert (fit.fit_sensitive.model_image == model_image).all()
        assert (fit.fit_sensitive.residual_map == residual_map).all()
        assert (fit.fit_sensitive.chi_squared_map == chi_squared_map).all()

        chi_squared = fit_util.chi_squared_from_chi_squared_map_and_mask(
            chi_squared_map=chi_squared_map, mask=lens_data_blur.mask)
        noise_normalization = fit_util.noise_normalization_from_noise_map_and_mask(
            mask=lens_data_blur.mask, noise_map=lens_data_blur.noise_map)
        assert fit.fit_normal.likelihood == -0.5 * (chi_squared +
                                                    noise_normalization)
        assert fit.fit_sensitive.likelihood == -0.5 * (chi_squared +
                                                       noise_normalization)

        assert fit.figure_of_merit == 0.0
示例#5
0
def make_fit(lens_data):
    lens_galaxy = g.Galaxy(mass=mp.SphericalIsothermal(einstein_radius=1.0),
                           redshift=1.0)
    lens_subhalo = g.Galaxy(mass=mp.SphericalIsothermal(einstein_radius=0.1),
                            redshift=1.0)
    source_galaxy = g.Galaxy(light=lp.EllipticalSersic(intensity=1.0),
                             redshift=2.0)

    tracer_normal = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy],
        source_galaxies=[source_galaxy],
        image_plane_grid_stack=lens_data.grid_stack,
        cosmology=cosmo.Planck15)
    tracer_sensitivity = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy, lens_subhalo],
        source_galaxies=[source_galaxy],
        image_plane_grid_stack=lens_data.grid_stack,
        cosmology=cosmo.Planck15)
    return sensitivity_fit.SensitivityProfileFit(
        lens_data=lens_data,
        tracer_normal=tracer_normal,
        tracer_sensitive=tracer_sensitivity)
def perform_fit_with_lens_and_source_galaxy(lens_galaxy, source_galaxy):

    ccd_data = simulate()
    mask = msk.Mask.circular_annular(shape=ccd_data.shape,
                                     pixel_scale=ccd_data.pixel_scale,
                                     inner_radius_arcsec=0.5,
                                     outer_radius_arcsec=2.2)
    lens_data = ld.LensData(ccd_data=ccd_data, mask=mask)
    tracer = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy],
        source_galaxies=[source_galaxy],
        image_plane_grid_stack=lens_data.grid_stack,
        border=lens_data.border)
    return lens_fit.fit_lens_data_with_tracer(lens_data=lens_data,
                                              tracer=tracer)
def perform_fit_with_source_galaxy(source_galaxy):

    ccd_data = simulate()
    mask = msk.Mask.circular_annular(shape=ccd_data.shape,
                                     pixel_scale=ccd_data.pixel_scale,
                                     inner_radius_arcsec=0.5,
                                     outer_radius_arcsec=2.2)
    lens_data = ld.LensData(ccd_data=ccd_data, 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_grid_stack=lens_data.grid_stack,
        border=lens_data.border)
    return lens_fit.fit_lens_data_with_tracer(lens_data=lens_data,
                                              tracer=tracer)
示例#8
0
def perform_fit_with_source_galaxy_mask_and_border(source_galaxy, mask,
                                                   use_border):

    ccd_data = simulate()
    lens_data = ld.LensData(ccd_data=ccd_data, 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))

    if use_border:
        border = lens_data.border
    else:
        border = None

    tracer = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy],
        source_galaxies=[source_galaxy],
        image_plane_grid_stack=lens_data.grid_stack,
        border=border)
    return lens_fit.fit_lens_data_with_tracer(lens_data=lens_data,
                                              tracer=tracer)
示例#9
0
def simulate_image_from_galaxies_and_output_to_fits(lens_name, pixel_scale, shape, sub_grid_size, lens_galaxies,
                                                    source_galaxies):

    # Simulate a simple Gaussian PSF for the image.
    psf = ccd.PSF.simulate_as_gaussian(shape=(51, 51), sigma=pixel_scale, 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. A high-res sub-grid is necessary to ensure we fully resolve the central regions of the
    # lens and source galaxy light.
    image_plane_grid_stack = grids.GridStack.grid_stack_for_simulation(shape=shape, pixel_scale=pixel_scale,
                                                                       psf_shape=psf.shape, sub_grid_size=sub_grid_size)

    # Use the input galaxies to setup a tracer, which will generate the image-plane image for the simulated CCD data.
    tracer = ray_tracing.TracerImageSourcePlanes(lens_galaxies=lens_galaxies, source_galaxies=source_galaxies,
                                                 image_plane_grid_stack=image_plane_grid_stack)

    # Simulate the CCD data, remembering that we use a special image-plane image which ensures edge-effects don't
    # degrade our modeling of the telescope optics (e.g. the PSF convolution).
    simulated_ccd = ccd.CCDData.simulate(array=tracer.image_plane_image_for_simulation, pixel_scale=pixel_scale,
                                         exposure_time=300.0, psf=psf, background_sky_level=0.1, add_noise=True)

    # Now, lets output this simulated ccd-data to the test/profiling/data folder.
    path = '{}/'.format(os.path.dirname(os.path.realpath(__file__)))

    # The image_type tag tells us whether we are outputting an image at LSST, Euclid, HST or AO resolution.
    image_type = tools.image_type_from_pixel_scale(pixel_scale=pixel_scale)

    # Check a folder of the lens name and within that of the pixel scale tag exist in the data folder for the images to
    # be output. If it doesn't make it.
    if not os.path.exists(path + '/data/' + lens_name):
        os.makedirs(path + '/data/' + lens_name)

    if not os.path.exists(path + '/data/' + lens_name + '/' + image_type ):
        os.makedirs(path + '/data/' + lens_name + '/' + image_type )

    ccd.output_ccd_data_to_fits(ccd_data=simulated_ccd,
                                image_path=path + '/data/' + lens_name + '/' + image_type + '/image.fits',
                                psf_path=path + '/data/' + lens_name + '/' + image_type + '/psf.fits',
                                noise_map_path=path + '/data/' + lens_name + '/' + image_type + '/noise_map.fits',
                                overwrite=True)
示例#10
0
def perform_fit_x2_lenses_with_source_galaxy_mask_and_border(
        source_galaxy, mask, use_border):
    simulate_image_x2_lenses()
    lens_data = ld.LensData(ccd_data=ccd_data, mask=mask)
    lens_galaxy_0 = g.Galaxy(mass=mp.EllipticalIsothermal(
        centre=(1.1, 0.51), axis_ratio=0.9, phi=110.0, einstein_radius=1.07))
    lens_galaxy_1 = g.Galaxy(mass=mp.EllipticalIsothermal(
        centre=(-0.20,
                -0.35), axis_ratio=0.56, phi=16.0, einstein_radius=0.71))

    if use_border:
        border = lens_data.border
    else:
        border = None

    tracer = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy_0, lens_galaxy_1],
        source_galaxies=[source_galaxy],
        image_plane_grid_stack=lens_data.grid_stack,
        border=border)

    return lens_fit.fit_lens_data_with_tracer(lens_data=lens_data,
                                              tracer=tracer)
示例#11
0
def simulate_image_with_offset_centre_psf():

    from autolens.data.array import grids
    from autolens.model.galaxy import galaxy as g
    from autolens.lens import ray_tracing

    psf = im.PSF.simulate_as_gaussian(shape=(21, 21),
                                      sigma=0.05,
                                      pixel_scale=0.1,
                                      centre=(0.1, 0.1))

    image_plane_grids = grids.GridStack.grid_stack_for_simulation(
        shape=(100, 100), pixel_scale=0.1, psf_shape=(21, 21))

    lens_galaxy = g.Galaxy(light=lp.SphericalSersic(centre=(0.0, 0.0),
                                                    intensity=0.3,
                                                    effective_radius=1.0,
                                                    sersic_index=2.0),
                           mass=mp.SphericalIsothermal(centre=(0.0, 0.0),
                                                       einstein_radius=1.2))

    source_galaxy = g.Galaxy(light=lp.SphericalSersic(centre=(0.0, 0.0),
                                                      intensity=0.2,
                                                      effective_radius=1.0,
                                                      sersic_index=1.5))

    tracer = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy],
        source_galaxies=[source_galaxy],
        image_plane_grid_stack=[image_plane_grids])

    return im.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)
示例#12
0
    def test__tracers_are_different__likelihood_is_non_zero(
            self, lens_data_blur):

        pixelization = pix.Rectangular(shape=(3, 3))
        regularization = reg.Constant(coefficients=(1.0, ))

        g0 = g.Galaxy(mass_profile=mp.SphericalIsothermal(einstein_radius=1.0))
        g0_subhalo = g.Galaxy(subhalo=mp.SphericalIsothermal(
            einstein_radius=0.1))
        g1 = g.Galaxy(pixelization=pixelization, regularization=regularization)

        tracer_normal = ray_tracing.TracerImageSourcePlanes(
            lens_galaxies=[g0],
            source_galaxies=[g1],
            image_plane_grid_stack=lens_data_blur.grid_stack)

        tracer_sensitive = ray_tracing.TracerImageSourcePlanes(
            lens_galaxies=[g0, g0_subhalo],
            source_galaxies=[g1],
            image_plane_grid_stack=lens_data_blur.grid_stack)

        fit = sensitivity_fit.SensitivityInversionFit(
            lens_data=lens_data_blur,
            tracer_normal=tracer_normal,
            tracer_sensitive=tracer_sensitive)

        assert (fit.fit_normal.image == lens_data_blur.image).all()
        assert (fit.fit_normal.noise_map == lens_data_blur.noise_map).all()

        mapper = pixelization.mapper_from_grid_stack_and_border(
            grid_stack=tracer_normal.source_plane.grid_stack, border=None)
        inversion = inv.inversion_from_image_mapper_and_regularization(
            mapper=mapper,
            regularization=regularization,
            image_1d=lens_data_blur.image_1d,
            noise_map_1d=lens_data_blur.noise_map_1d,
            convolver=lens_data_blur.convolver_mapping_matrix)

        assert fit.fit_normal.model_image == pytest.approx(
            inversion.reconstructed_data, 1.0e-4)

        residual_map = fit_util.residual_map_from_data_mask_and_model_data(
            data=lens_data_blur.image,
            mask=lens_data_blur.mask,
            model_data=inversion.reconstructed_data)

        assert fit.fit_normal.residual_map == pytest.approx(
            residual_map, 1.0e-4)

        chi_squared_map = fit_util.chi_squared_map_from_residual_map_noise_map_and_mask(
            residual_map=residual_map,
            mask=lens_data_blur.mask,
            noise_map=lens_data_blur.noise_map)

        assert fit.fit_normal.chi_squared_map == pytest.approx(
            chi_squared_map, 1.0e-4)

        assert (fit.fit_sensitive.image == lens_data_blur.image).all()
        assert (fit.fit_sensitive.noise_map == lens_data_blur.noise_map).all()

        mapper = pixelization.mapper_from_grid_stack_and_border(
            grid_stack=tracer_sensitive.source_plane.grid_stack, border=None)
        inversion = inv.inversion_from_image_mapper_and_regularization(
            mapper=mapper,
            regularization=regularization,
            image_1d=lens_data_blur.image_1d,
            noise_map_1d=lens_data_blur.noise_map_1d,
            convolver=lens_data_blur.convolver_mapping_matrix)

        assert fit.fit_sensitive.model_image == pytest.approx(
            inversion.reconstructed_data, 1.0e-4)

        residual_map = fit_util.residual_map_from_data_mask_and_model_data(
            data=lens_data_blur.image,
            mask=lens_data_blur.mask,
            model_data=inversion.reconstructed_data)

        assert fit.fit_sensitive.residual_map == pytest.approx(
            residual_map, 1.0e-4)

        chi_squared_map = fit_util.chi_squared_map_from_residual_map_noise_map_and_mask(
            residual_map=residual_map,
            mask=lens_data_blur.mask,
            noise_map=lens_data_blur.noise_map)

        assert fit.fit_sensitive.chi_squared_map == pytest.approx(
            chi_squared_map, 1.0e-4)

        chi_squared_normal = fit_util.chi_squared_from_chi_squared_map_and_mask(
            chi_squared_map=fit.fit_normal.chi_squared_map,
            mask=lens_data_blur.mask)
        chi_squared_sensitive = fit_util.chi_squared_from_chi_squared_map_and_mask(
            chi_squared_map=fit.fit_sensitive.chi_squared_map,
            mask=lens_data_blur.mask)
        noise_normalization = fit_util.noise_normalization_from_noise_map_and_mask(
            mask=lens_data_blur.mask, noise_map=lens_data_blur.noise_map)
        assert fit.fit_normal.likelihood == -0.5 * (chi_squared_normal +
                                                    noise_normalization)
        assert fit.fit_sensitive.likelihood == -0.5 * (chi_squared_sensitive +
                                                       noise_normalization)

        assert fit.figure_of_merit == fit.fit_sensitive.likelihood - fit.fit_normal.likelihood

        fit_from_factory = sensitivity_fit.fit_lens_data_with_sensitivity_tracers(
            lens_data=lens_data_blur,
            tracer_normal=tracer_normal,
            tracer_sensitive=tracer_sensitive)

        assert fit.figure_of_merit == fit_from_factory.figure_of_merit
示例#13
0
print(lens_galaxy)

# And for our source galaxy, the same Sersic light profile
sersic_light_profile = light_profiles.SphericalSersic(centre=(0.0, 0.0), intensity=1.0, effective_radius=1.0,
                                                      sersic_index=1.0)
source_galaxy = galaxy.Galaxy(light=sersic_light_profile)
print(source_galaxy)

# Now, lets use the lens and source galaxies to ray-trace our grid-stack, using a 'tracer' from the ray-tracing
# module. When we pass our galaxies and grid-stack into the Tracer below, the following happens:

# 1) Using the lens-galaxy's mass-profile, the deflection angle of every image-plane grid coordinate is computed.
# 2) These deflection angles are used to trace every image-plane coordinate to a source-plane coordinate.
# 3) This creates the source-plane grid-stack of lensed coordinates.

tracer = ray_tracing.TracerImageSourcePlanes(lens_galaxies=[lens_galaxy], source_galaxies=[source_galaxy],
                                             image_plane_grid_stack=image_plane_grid_stack)

# The tracer is composed of an image-plane and source-plane, just like in the previous example!
print('Regular-grid image-plane coordinate 1')
print(tracer.image_plane.grid_stack.regular[0])
print('Regular-grid image-plane coordinate 2')
print(tracer.image_plane.grid_stack.regular[1])
print('Regular-grid image-plane coordinate 3')
print(tracer.image_plane.grid_stack.regular[2])

# And the source-plane's grid has been deflected.
print('Regular-grid source-plane coordinate 1')
print(tracer.source_plane.grid_stack.regular[0])
print('Regular-grid source-plane coordinate 2')
print(tracer.source_plane.grid_stack.regular[1])
print('Regular-grid source-plane coordinate 3')
示例#14
0
def make_tracer(galaxy_light, galaxy_mass, grid_stack):
    return ray_tracing.TracerImageSourcePlanes(lens_galaxies=[galaxy_mass, galaxy_light],
                                               source_galaxies=[galaxy_light],
                                               image_plane_grid_stack=grid_stack)
示例#15
0
#    results = pd.concat(list_)

image_plane_grid_stack = grids.GridStack.from_shape_pixel_scale_and_sub_size(
    shape_2d=ccd_data.shape, pixel_scales=ccd_data.pixel_scales, sub_size=2)

#print(data)
#print(data.iloc[0,16])

lens_galaxy = al.Galaxy(mass=al.mp.EllipticalIsothermal(
    centre=(data.iloc[0, 5], data.iloc[0, 6]),
    axis_ratio=data.iloc[0, 7],
    phi=data.iloc[0, 8],
    einstein_radius=data.iloc[0, 9]),
                        redshift=0.285)
source_galaxy = al.Galaxy(
    pixelization=pix.AdaptiveMagnification(shape_2d=(data.iloc[0, 14],
                                                     data.iloc[0, 15])),
    regularization=reg.Constant(data.iloc[0, 16]),
    redshift=0.575)

#image_plane_grid_stack = grids.GridStack.from_shape_pixel_scale_and_sub_size(shape_2d=(301, 301), pixel_scales=0.03,
#                                                                      sub_size=2)

tracer = ray_tracing.TracerImageSourcePlanes(
    lens_galaxies=[lens_galaxy],
    source_galaxies=[source_galaxy],
    image_plane_grid_stack=lens_data.grid_stack,
    cosmology=cosmology.Planck15)

print(np.log10(tracer.einstein_masses_of_planes[0]))
def test__simulate_lensed_source_and_fit__include_psf_blurring__chi_squared_is_0__noise_normalization_correct(
):

    psf = ccd.PSF.simulate_as_gaussian(shape=(3, 3),
                                       pixel_scale=0.2,
                                       sigma=0.75)

    grid_stack = grids.GridStack.grid_stack_for_simulation(shape=(11, 11),
                                                           pixel_scale=0.2,
                                                           psf_shape=psf.shape,
                                                           sub_grid_size=1)

    lens_galaxy = g.Galaxy(light=lp.EllipticalSersic(centre=(0.1, 0.1),
                                                     intensity=0.1),
                           mass=mp.EllipticalIsothermal(centre=(0.1, 0.1),
                                                        einstein_radius=1.8))
    source_galaxy = g.Galaxy(
        light=lp.EllipticalExponential(centre=(0.1, 0.1), intensity=0.5))
    tracer = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy],
        source_galaxies=[source_galaxy],
        image_plane_grid_stack=grid_stack)

    ccd_simulated = ccd.CCDData.simulate(
        array=tracer.image_plane_image_for_simulation,
        pixel_scale=0.2,
        exposure_time=300.0,
        psf=psf,
        background_sky_level=None,
        add_noise=False)
    ccd_simulated.noise_map = np.ones(ccd_simulated.image.shape)

    path = "{}/data/simulate_and_fit".format(
        os.path.dirname(os.path.realpath(
            __file__)))  # Setup path so we can output the simulated image.

    try:
        shutil.rmtree(path)
    except FileNotFoundError:
        pass

    if os.path.exists(path) == False:
        os.makedirs(path)

    array_util.numpy_array_to_fits(array=ccd_simulated.image,
                                   file_path=path + '/image.fits')
    array_util.numpy_array_to_fits(array=ccd_simulated.noise_map,
                                   file_path=path + '/noise_map.fits')
    array_util.numpy_array_to_fits(array=psf, file_path=path + '/psf.fits')

    ccd_data = ccd.load_ccd_data_from_fits(image_path=path + '/image.fits',
                                           noise_map_path=path +
                                           '/noise_map.fits',
                                           psf_path=path + '/psf.fits',
                                           pixel_scale=0.2)

    mask = msk.Mask.circular(shape=ccd_data.image.shape,
                             pixel_scale=0.2,
                             radius_arcsec=0.8)

    lens_data = ld.LensData(ccd_data=ccd_data, mask=mask, sub_grid_size=1)

    tracer = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy],
        source_galaxies=[source_galaxy],
        image_plane_grid_stack=lens_data.grid_stack)

    fitter = lens_fit.LensProfileFit(lens_data=lens_data, tracer=tracer)

    assert fitter.chi_squared == pytest.approx(0.0, 1e-4)
source_galaxy_2 = g.Galaxy(light=lp.EllipticalSersic(centre=(0.45, -0.35),
                                                     axis_ratio=0.6,
                                                     phi=90.0,
                                                     intensity=0.03,
                                                     effective_radius=0.3,
                                                     sersic_index=3.5))
source_galaxy_3 = g.Galaxy(light=lp.EllipticalSersic(centre=(-0.05, -0.0),
                                                     axis_ratio=0.9,
                                                     phi=140.0,
                                                     intensity=0.03,
                                                     effective_radius=0.1,
                                                     sersic_index=4.0))

tracer = ray_tracing.TracerImageSourcePlanes(
    lens_galaxies=[lens_galaxy],
    source_galaxies=[
        source_galaxy_0, source_galaxy_1, source_galaxy_2, source_galaxy_3
    ],
    image_plane_grid_stack=lens_data.grid_stack)

true_fit = lens_fit.fit_lens_data_with_tracer(lens_data=lens_data,
                                              tracer=tracer)

lens_fit_plotters.plot_fit_subplot(fit=true_fit)

# And indeed, we see far improved residuals, chi-squareds, etc.

# The morale of this story is that, if the source morphology is complex, there is no way we can build a pipeline to
# fit it. For this tutorial, this was true even though our source model could actually fit the data perfectly. For
# real lenses, the source will be *even more complex* and there is even less hope of getting a good fit :(

# But fear not, PyAutoLens has you covered. In chapter 4, we'll introduce a completely new way to model the source
    source_galaxy = al.Galaxy(light=al.lp.EllipticalSersic(
        centre=(results.loc[lens[i]]['param']['source_light_centre_0'],
                results.loc[lens[i]]['param']['source_light_centre_1']),
        axis_ratio=results.loc[lens[i]]['param']['source_light_axis_ratio'],
        phi=results.loc[lens[i]]['param']['source_light_phi'],
        intensity=results.loc[lens[i]]['param']['source_light_intensity'],
        effective_radius=results.loc[
            lens[i]]['param']['source_light_effective_radius'],
        sersic_index=results.loc[
            lens[i]]['param']['source_light_sersic_index']),
                              redshift=slacs['z_source'][i])

    tracer = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy],
        source_galaxies=[source_galaxy],
        image_plane_grid_stack=image_plane_grid_stack,
        cosmology=cosmology.Planck15)
    tracer_error_hi = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy_hi],
        source_galaxies=[source_galaxy],
        image_plane_grid_stack=image_plane_grid_stack,
        cosmology=cosmology.Planck15)
    tracer_error_low = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy_low],
        source_galaxies=[source_galaxy],
        image_plane_grid_stack=image_plane_grid_stack,
        cosmology=cosmology.Planck15)

    einstein_mass = tracer.einstein_masses_in_solar_masses_of_planes[0]
    einstein_mass_error_hi = tracer_error_hi.einstein_masses_in_solar_masses_of_planes[
# Lets look at our source galaxies (before lensing)
galaxy_plotters.plot_intensities(galaxy=source_galaxy_0, grid=image_plane_grid_stack.regular, title='Source Galaxy 0')
galaxy_plotters.plot_intensities(galaxy=source_galaxy_1, grid=image_plane_grid_stack.regular, title='Source Galaxy 1')

# Now lets pass these our 4 galaxies to the ray_tracing module, which means the following will occur:

# 1) Using every mass-profile in each lens galaxy, the deflection angles are computed.
# 2) These deflection angles are summed, such that the deflection of light due to every mass-profile and both
#    the lens galaxy and its satellite is computed.
# 3) These deflection angles are used to trace every image-grid and sub-grid coordinate to a source-plane coordinate.
# 4) The light of all source galaxies is ray-traced back forward to form our image-plane image.

# Note that we've also supplied the tracer below with a Planck15 cosmology.
tracer = ray_tracing.TracerImageSourcePlanes(lens_galaxies=[lens_galaxy, lens_satellite],
                                             source_galaxies=[source_galaxy_0, source_galaxy_1],
                                             image_plane_grid_stack=image_plane_grid_stack,
                                             cosmology=cosmology.Planck15)

# As we did previously, we can inspect each grid.
plane_plotters.plot_plane_grid(plane=tracer.image_plane, title='Image-plane Grid')
plane_plotters.plot_plane_grid(plane=tracer.source_plane, title='Source-plane Grid')

# We can zoom in on the 'centre' of the source-plane.
plane_plotters.plot_plane_grid(plane=tracer.source_plane, axis_limits=[-0.2, 0.2, -0.2, 0.2], title='Source-plane Grid')

# Lets plot the lensing quantities again. Note that, because we supplied our galaxies with redshifts and
# our tracer with a cosmology, our units have been converted to kiloparsecs!
# (This line can take a bit of time to run)
ray_tracing_plotters.plot_ray_tracing_subplot(tracer=tracer)

# In the previous example, we saw that the tracer had attributes we plotted (e.g. surface density, potential, etc.)
示例#20
0
# Now, lets simulate the source, mask it, and use a plot to check the masking is appropriate.
ccd_data = simulate()
mask = msk.Mask.circular_annular(shape=ccd_data.shape,
                                 pixel_scale=ccd_data.pixel_scale,
                                 inner_radius_arcsec=1.0,
                                 outer_radius_arcsec=2.2)
ccd_plotters.plot_image(ccd_data=ccd_data, mask=mask)

# Next, lets set this image up as lens data, and setup a tracer using the input lens galaxy model (we don't need
# to provide the source's light profile, as we're using a mapper to reconstruct it).
lens_data = ld.LensData(ccd_data=ccd_data, mask=mask, sub_grid_size=1)
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=[g.Galaxy()],
    image_plane_grid_stack=lens_data.grid_stack)

# We'll use another rectangular pixelization and mapper to perform the reconstruction
rectangular = pix.Rectangular(shape=(25, 25))
mapper = rectangular.mapper_from_grid_stack_and_border(
    grid_stack=tracer.source_plane.grid_stack, border=None)
mapper_plotters.plot_image_and_mapper(ccd_data=ccd_data,
                                      mask=mask,
                                      mapper=mapper,
                                      should_plot_grid=True)

# And now, finally, we're going to use our mapper to invert the image using the 'inversions' module, which is imported
# as 'inv'. I'll explain how this works in a second - but lets just go ahead and perform the inversion first.
# (Ignore the 'regularization' input below for now, we'll cover this in the next tutorial).
inversion = inv.Inversion(image_1d=lens_data.image_1d,