def test__centre_mass_profile_on_grid_coordinate__peak_density_is_correct_index( ): image_grid = grids.RegularGrid.from_shape_and_pixel_scale(shape=(5, 5), pixel_scale=1.0) sis = mp.SphericalIsothermal(centre=(2.0, -2.0)) density_1d = sis.surface_density_from_grid(grid=image_grid) density_2d = image_grid.array_2d_from_array_1d(array_1d=density_1d) assert density_1d.argmax() == 0 assert np.unravel_index(density_2d.argmax(), density_2d.shape) == (0, 0) sis = mp.SphericalIsothermal(centre=(2.0, 2.0)) density_1d = sis.surface_density_from_grid(grid=image_grid) density_2d = image_grid.array_2d_from_array_1d(array_1d=density_1d) assert density_1d.argmax() == 4 assert np.unravel_index(density_2d.argmax(), density_2d.shape) == (0, 4) sis = mp.SphericalIsothermal(centre=(-2.0, -2.0)) density_1d = sis.surface_density_from_grid(grid=image_grid) density_2d = image_grid.array_2d_from_array_1d(array_1d=density_1d) assert density_1d.argmax() == 20 assert np.unravel_index(density_2d.argmax(), density_2d.shape) == (4, 0) sis = mp.SphericalIsothermal(centre=(-2.0, 2.0)) density_1d = sis.surface_density_from_grid(grid=image_grid) density_2d = image_grid.array_2d_from_array_1d(array_1d=density_1d) assert density_1d.argmax() == 24 assert np.unravel_index(density_2d.argmax(), density_2d.shape) == (4, 4)
def pipeline(): lens_mass = mp.SphericalIsothermal(centre=(0.0, 0.0), einstein_radius=1.6) lens_subhalo = mp.SphericalIsothermal(centre=(1.0, 1.0), einstein_radius=0.0) source_light = lp.SphericalSersic(centre=(0.0, 0.0), intensity=1.0, effective_radius=0.5, sersic_index=1.0) lens_galaxy = galaxy.Galaxy(mass=lens_mass, subhalo=lens_subhalo) source_galaxy = galaxy.Galaxy(light=source_light) tools.reset_paths(test_name=test_name, output_path=output_path) tools.simulate_integration_image(test_name=test_name, pixel_scale=0.1, lens_galaxies=[lens_galaxy], source_galaxies=[source_galaxy], target_signal_to_noise=30.0) ccd_data = ccd.load_ccd_data_from_fits( image_path=path + '/data/' + test_name + '/image.fits', psf_path=path + '/data/' + test_name + '/psf.fits', noise_map_path=path + '/data/' + test_name + '/noise_map.fits', pixel_scale=0.1) pipeline = make_pipeline(test_name=test_name) result = pipeline.run(data=ccd_data) print(dir(result))
def phase(): pixel_scale = 0.1 image_shape = (150, 150) tools.reset_paths(test_name=test_name, output_path=output_path) grid_stack = grids.GridStack.from_shape_pixel_scale_and_sub_grid_size(shape=image_shape, pixel_scale=pixel_scale, sub_grid_size=4) galaxy = g.Galaxy(mass=mp.SphericalIsothermal(centre=(0.0, 0.0), einstein_radius=1.0)) deflections = galaxy_util.deflections_of_galaxies_from_grid(galaxies=[galaxy], grid=grid_stack.sub) deflections_y = grid_stack.regular.scaled_array_from_array_1d(array_1d=deflections[:,0]) deflections_x = grid_stack.regular.scaled_array_from_array_1d(array_1d=deflections[:,1]) noise_map = scaled_array.ScaledSquarePixelArray(array=np.ones(deflections_y.shape), pixel_scale=pixel_scale) data_y = gd.GalaxyData(image=deflections_y, noise_map=noise_map, pixel_scale=pixel_scale) data_x = gd.GalaxyData(image=deflections_x, noise_map=noise_map, pixel_scale=pixel_scale) phase = ph.GalaxyFitPhase(galaxies=dict(gal=gm.GalaxyModel(light=mp.SphericalIsothermal)), use_deflections=True, sub_grid_size=4, optimizer_class=nl.MultiNest, phase_name=test_name+'/') phase.run(galaxy_data=[data_y, data_x])
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 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
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 test__galaxy_data_deflections_x(self, image, mask): galaxy_data = gd.GalaxyData(image=image, noise_map=2.0 * np.ones((4, 4)), pixel_scale=3.0) galaxy_fit_data = gd.GalaxyFitData(galaxy_data=galaxy_data, mask=mask, sub_grid_size=2, use_deflections_x=True) assert galaxy_fit_data.pixel_scale == 3.0 assert (galaxy_fit_data.image == np.ones((4, 4))).all() assert (galaxy_fit_data.noise_map == 2.0 * np.ones((4, 4))).all() assert (galaxy_fit_data.mask == np.array([[True, True, True, True], [True, False, False, True], [True, False, False, True], [True, True, True, True]])).all() assert (galaxy_fit_data.image_1d == np.ones(4)).all() assert (galaxy_fit_data.noise_map_1d == 2.0 * np.ones(4)).all() assert (galaxy_fit_data.mask_1d == np.array( [False, False, False, False])).all() galaxy = MockGalaxy(value=1, shape=4) deflections_x = galaxy_fit_data.profile_quantity_from_galaxy_and_sub_grid( galaxies=[galaxy], sub_grid=galaxy_fit_data.grid_stack.sub) assert (deflections_x == np.array([1.0, 1.0, 1.0, 1.0])).all() galaxy = g.Galaxy(mass=mp.SphericalIsothermal(einstein_radius=1.0)) deflections_gal = galaxy.deflections_from_grid( grid=galaxy_fit_data.grid_stack.sub) deflections_gal = np.asarray([ galaxy_fit_data.grid_stack.sub.sub_data_to_regular_data( deflections_gal[:, 0]), galaxy_fit_data.grid_stack.sub.sub_data_to_regular_data( deflections_gal[:, 1]) ]).T deflections_gd = galaxy_fit_data.profile_quantity_from_galaxy_and_sub_grid( galaxies=[galaxy], sub_grid=galaxy_fit_data.grid_stack.sub) assert (deflections_gal[:, 1] == deflections_gd).all()
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)
def test___same_as_above__image_and_source_plane(self): 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)) psf_0 = im.PSF(array=(np.array([[0.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 0.0]])), pixel_scale=1.0) psf_1 = 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) g0 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=0.1)) g1 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=0.2)) g2 = g.Galaxy(mass_profile=mp.SphericalIsothermal(einstein_radius=1.0)) tracer = ray_tracing_stack.TracerImageSourcePlanesStack( lens_galaxies=[g0, g1, g2], source_galaxies=[g0, g1], image_plane_grid_stacks=[padded_grid_stack, padded_grid_stack]) manual_blurred_image_i00 = tracer.image_plane.image_plane_images_1d_of_galaxies[ 0][0] manual_blurred_image_i00 = padded_grid_stack.regular.map_to_2d_keep_padded( padded_array_1d=manual_blurred_image_i00) manual_blurred_image_i00 = psf_0.convolve( array=manual_blurred_image_i00) manual_blurred_image_i01 = tracer.image_plane.image_plane_images_1d_of_galaxies[ 0][1] manual_blurred_image_i01 = padded_grid_stack.regular.map_to_2d_keep_padded( padded_array_1d=manual_blurred_image_i01) manual_blurred_image_i01 = psf_0.convolve( array=manual_blurred_image_i01) manual_blurred_image_i10 = tracer.image_plane.image_plane_images_1d_of_galaxies[ 1][0] manual_blurred_image_i10 = padded_grid_stack.regular.map_to_2d_keep_padded( padded_array_1d=manual_blurred_image_i10) manual_blurred_image_i10 = psf_1.convolve( array=manual_blurred_image_i10) manual_blurred_image_i11 = tracer.image_plane.image_plane_images_1d_of_galaxies[ 1][1] manual_blurred_image_i11 = padded_grid_stack.regular.map_to_2d_keep_padded( padded_array_1d=manual_blurred_image_i11) manual_blurred_image_i11 = psf_1.convolve( array=manual_blurred_image_i11) manual_blurred_image_s00 = tracer.source_plane.image_plane_images_1d_of_galaxies[ 0][0] manual_blurred_image_s00 = padded_grid_stack.regular.map_to_2d_keep_padded( padded_array_1d=manual_blurred_image_s00) manual_blurred_image_s00 = psf_0.convolve( array=manual_blurred_image_s00) manual_blurred_image_s01 = tracer.source_plane.image_plane_images_1d_of_galaxies[ 0][1] manual_blurred_image_s01 = padded_grid_stack.regular.map_to_2d_keep_padded( padded_array_1d=manual_blurred_image_s01) manual_blurred_image_s01 = psf_0.convolve( array=manual_blurred_image_s01) manual_blurred_image_s10 = tracer.source_plane.image_plane_images_1d_of_galaxies[ 1][0] manual_blurred_image_s10 = padded_grid_stack.regular.map_to_2d_keep_padded( padded_array_1d=manual_blurred_image_s10) manual_blurred_image_s10 = psf_1.convolve( array=manual_blurred_image_s10) manual_blurred_image_s11 = tracer.source_plane.image_plane_images_1d_of_galaxies[ 1][1] manual_blurred_image_s11 = padded_grid_stack.regular.map_to_2d_keep_padded( padded_array_1d=manual_blurred_image_s11) manual_blurred_image_s11 = psf_1.convolve( array=manual_blurred_image_s11) unmasked_blurred_image_of_datas_planes_and_galaxies = \ stack_util.unmasked_blurred_image_of_datas_planes_and_galaxies_from_padded_grid_stacks_and_psf( planes=tracer.planes, padded_grid_stacks=[padded_grid_stack, padded_grid_stack], psfs=[psf_0, psf_1]) assert (unmasked_blurred_image_of_datas_planes_and_galaxies[0][0][0] == manual_blurred_image_i00[1:4, 1:4]).all() assert (unmasked_blurred_image_of_datas_planes_and_galaxies[0][0][1] == manual_blurred_image_i01[1:4, 1:4]).all() assert (unmasked_blurred_image_of_datas_planes_and_galaxies[1][0][0] == manual_blurred_image_i10[1:4, 1:4]).all() assert (unmasked_blurred_image_of_datas_planes_and_galaxies[1][0][1] == manual_blurred_image_i11[1:4, 1:4]).all() assert (unmasked_blurred_image_of_datas_planes_and_galaxies[0][1][0] == manual_blurred_image_s00[1:4, 1:4]).all() assert (unmasked_blurred_image_of_datas_planes_and_galaxies[0][1][1] == manual_blurred_image_s01[1:4, 1:4]).all() assert (unmasked_blurred_image_of_datas_planes_and_galaxies[1][1][0] == manual_blurred_image_s10[1:4, 1:4]).all() assert (unmasked_blurred_image_of_datas_planes_and_galaxies[1][1][1] == manual_blurred_image_s11[1:4, 1:4]).all()
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), mass=mp.SphericalIsothermal(centre=(-3.1, -2.4), einstein_radius=0.03), redshift=1.25)
def test_2d_symmetry(self): isothermal_1 = mp.SphericalIsothermal(einstein_radius=1.0) isothermal_2 = mp.SphericalIsothermal(centre=(100, 0), einstein_radius=1.0) isothermal_3 = mp.SphericalIsothermal(centre=(0, 100), einstein_radius=1.0) isothermal_4 = mp.SphericalIsothermal(centre=(100, 100), einstein_radius=1.0) gal_isothermal = g.Galaxy(redshift=0.5, mass_profile_1=isothermal_1, mass_profile_2=isothermal_2, mass_profile_3=isothermal_3, mass_profile_4=isothermal_4) assert gal_isothermal.surface_density_from_grid( np.array([[49.0, 0.0]])) == pytest.approx( gal_isothermal.surface_density_from_grid( np.array([[51.0, 0.0]])), 1e-5) assert gal_isothermal.surface_density_from_grid( np.array([[0.0, 49.0]])) == pytest.approx( gal_isothermal.surface_density_from_grid( np.array([[0.0, 51.0]])), 1e-5) assert gal_isothermal.surface_density_from_grid( np.array([[100.0, 49.0]])) == pytest.approx( gal_isothermal.surface_density_from_grid( np.array([[100.0, 51.0]])), 1e-5) assert gal_isothermal.surface_density_from_grid( np.array([[49.0, 49.0]])) == pytest.approx( gal_isothermal.surface_density_from_grid( np.array([[51.0, 51.0]])), 1e-5) assert gal_isothermal.potential_from_grid(np.array( [[49.0, 0.0]])) == pytest.approx( gal_isothermal.potential_from_grid(np.array([[51.0, 0.0]])), 1e-5) assert gal_isothermal.potential_from_grid(np.array( [[0.0, 49.0]])) == pytest.approx( gal_isothermal.potential_from_grid(np.array([[0.0, 51.0]])), 1e-5) assert gal_isothermal.potential_from_grid(np.array([[ 100.0, 49.0 ]])) == pytest.approx( gal_isothermal.potential_from_grid(np.array([[100.0, 51.0]])), 1e-5) assert gal_isothermal.potential_from_grid(np.array( [[49.0, 49.0]])) == pytest.approx( gal_isothermal.potential_from_grid(np.array([[51.0, 51.0]])), 1e-5) assert -1.0 * gal_isothermal.deflections_from_grid( np.array([[49.0, 0.0]]))[0, 0] == pytest.approx( gal_isothermal.deflections_from_grid( np.array([[51.0, 0.0]]))[0, 0], 1e-5) assert 1.0 * gal_isothermal.deflections_from_grid( np.array([[0.0, 49.0]]))[0, 0] == pytest.approx( gal_isothermal.deflections_from_grid( np.array([[0.0, 51.0]]))[0, 0], 1e-5) assert 1.0 * gal_isothermal.deflections_from_grid( np.array([[100.0, 49.0]]))[0, 0] == pytest.approx( gal_isothermal.deflections_from_grid( np.array([[100.0, 51.0]]))[0, 0], 1e-5) assert -1.0 * gal_isothermal.deflections_from_grid( np.array([[49.0, 49.0]]))[0, 0] == pytest.approx( gal_isothermal.deflections_from_grid( np.array([[51.0, 51.0]]))[0, 0], 1e-5) assert 1.0 * gal_isothermal.deflections_from_grid( np.array([[49.0, 0.0]]))[0, 1] == pytest.approx( gal_isothermal.deflections_from_grid( np.array([[51.0, 0.0]]))[0, 1], 1e-5) assert -1.0 * gal_isothermal.deflections_from_grid( np.array([[0.0, 49.0]]))[0, 1] == pytest.approx( gal_isothermal.deflections_from_grid( np.array([[0.0, 51.0]]))[0, 1], 1e-5) assert -1.0 * gal_isothermal.deflections_from_grid( np.array([[100.0, 49.0]]))[0, 1] == pytest.approx( gal_isothermal.deflections_from_grid( np.array([[100.0, 51.0]]))[0, 1], 1e-5) assert -1.0 * gal_isothermal.deflections_from_grid( np.array([[49.0, 49.0]]))[0, 1] == pytest.approx( gal_isothermal.deflections_from_grid( np.array([[51.0, 51.0]]))[0, 1], 1e-5)
def make_galaxy_mass_x2(): return g.Galaxy(sis_0=mp.SphericalIsothermal(einstein_radius=1.0), sis_1=mp.SphericalIsothermal(einstein_radius=1.0))
def make_galaxy(): return g.Galaxy(light=lp.SphericalSersic(intensity=1.0), mass=mp.SphericalIsothermal(einstein_radius=1.0))
def test___manual_image_and_psf(self, lens_data_stack_manual): g0 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=1.0)) g1 = g.Galaxy(mass_profile=mp.SphericalIsothermal( einstein_radius=1.0)) tracer = ray_tracing_stack.TracerImageSourcePlanesStack( lens_galaxies=[g0, g1], source_galaxies=[g0], image_plane_grid_stacks=lens_data_stack_manual.grid_stacks) padded_tracer = ray_tracing_stack.TracerImageSourcePlanesStack( lens_galaxies=[g0, g1], source_galaxies=[g0], image_plane_grid_stacks=lens_data_stack_manual. padded_grid_stacks) fit = lens_fit_stack.fit_lens_image_stack_with_tracer( lens_data_stack=lens_data_stack_manual, tracer=tracer, padded_tracer=padded_tracer) assert lens_data_stack_manual.noise_maps[0] == pytest.approx( fit.noise_maps[0], 1e-4) assert lens_data_stack_manual.noise_maps[1] == pytest.approx( fit.noise_maps[1], 1e-4) model_image_1d_0 = lens_fit_util.blurred_image_1d_from_1d_unblurred_and_blurring_images( unblurred_image_1d=tracer.image_plane_images_1d[0], blurring_image_1d=tracer.image_plane_blurring_images_1d[0], convolver=lens_data_stack_manual.convolvers_image[0]) model_image_1d_1 = lens_fit_util.blurred_image_1d_from_1d_unblurred_and_blurring_images( unblurred_image_1d=tracer.image_plane_images_1d[1], blurring_image_1d=tracer.image_plane_blurring_images_1d[1], convolver=lens_data_stack_manual.convolvers_image[1]) model_image_0 = lens_data_stack_manual.map_to_scaled_arrays[0]( array_1d=model_image_1d_0) model_image_1 = lens_data_stack_manual.map_to_scaled_arrays[1]( array_1d=model_image_1d_1) assert model_image_0 == pytest.approx(fit.model_images[0], 1e-4) assert model_image_1 == pytest.approx(fit.model_images[1], 1e-4) residual_map_0 = fit_util.residual_map_from_data_mask_and_model_data( data=lens_data_stack_manual.images[0], mask=lens_data_stack_manual.masks[0], model_data=model_image_0) residual_map_1 = fit_util.residual_map_from_data_mask_and_model_data( data=lens_data_stack_manual.images[1], mask=lens_data_stack_manual.masks[1], model_data=model_image_1) assert residual_map_0 == pytest.approx(fit.residual_maps[0], 1e-4) assert residual_map_1 == pytest.approx(fit.residual_maps[1], 1e-4) chi_squared_map_0 = fit_util.chi_squared_map_from_residual_map_noise_map_and_mask( residual_map=residual_map_0, mask=lens_data_stack_manual.masks[0], noise_map=lens_data_stack_manual.noise_maps[0]) chi_squared_map_1 = fit_util.chi_squared_map_from_residual_map_noise_map_and_mask( residual_map=residual_map_1, mask=lens_data_stack_manual.masks[1], noise_map=lens_data_stack_manual.noise_maps[1]) assert chi_squared_map_0 == pytest.approx(fit.chi_squared_maps[0], 1e-4) assert chi_squared_map_1 == pytest.approx(fit.chi_squared_maps[1], 1e-4) chi_squared_0 = fit_util.chi_squared_from_chi_squared_map_and_mask( chi_squared_map=chi_squared_map_0, mask=lens_data_stack_manual.masks[0]) noise_normalization_0 = fit_util.noise_normalization_from_noise_map_and_mask( noise_map=lens_data_stack_manual.noise_maps[0], mask=lens_data_stack_manual.masks[0]) likelihood_0 = fit_util.likelihood_from_chi_squared_and_noise_normalization( chi_squared=chi_squared_0, noise_normalization=noise_normalization_0) chi_squared_1 = fit_util.chi_squared_from_chi_squared_map_and_mask( chi_squared_map=chi_squared_map_1, mask=lens_data_stack_manual.masks[1]) noise_normalization_1 = fit_util.noise_normalization_from_noise_map_and_mask( noise_map=lens_data_stack_manual.noise_maps[1], mask=lens_data_stack_manual.masks[1]) likelihood_1 = fit_util.likelihood_from_chi_squared_and_noise_normalization( chi_squared=chi_squared_1, noise_normalization=noise_normalization_1) assert fit.chi_squareds == [chi_squared_0, chi_squared_1] assert fit.noise_normalizations == [ noise_normalization_0, noise_normalization_1 ] assert fit.likelihoods == [likelihood_0, likelihood_1] assert likelihood_0 + likelihood_1 == pytest.approx( fit.likelihood, 1e-4) assert likelihood_0 + likelihood_1 == fit.figure_of_merit blurred_image_of_planes_0 = lens_fit_util.blurred_image_of_planes_from_1d_images_and_convolver( total_planes=tracer.total_planes, image_plane_image_1d_of_planes=tracer. image_plane_images_1d_of_planes[0], image_plane_blurring_image_1d_of_planes=tracer. image_plane_blurring_images_1d_of_planes[0], convolver=lens_data_stack_manual.convolvers_image[0], map_to_scaled_array=lens_data_stack_manual. map_to_scaled_arrays[0]) blurred_image_of_planes_1 = lens_fit_util.blurred_image_of_planes_from_1d_images_and_convolver( total_planes=tracer.total_planes, image_plane_image_1d_of_planes=tracer. image_plane_images_1d_of_planes[1], image_plane_blurring_image_1d_of_planes=tracer. image_plane_blurring_images_1d_of_planes[1], convolver=lens_data_stack_manual.convolvers_image[1], map_to_scaled_array=lens_data_stack_manual. map_to_scaled_arrays[1]) assert (blurred_image_of_planes_0[0] == fit.model_images_of_planes[0][0]).all() assert (blurred_image_of_planes_0[1] == fit.model_images_of_planes[0][1]).all() assert (blurred_image_of_planes_1[0] == fit.model_images_of_planes[1][0]).all() assert (blurred_image_of_planes_1[1] == fit.model_images_of_planes[1][1]).all() unmasked_blurred_image_0 = \ lens_fit_util.unmasked_blurred_image_from_padded_grid_stack_psf_and_unmasked_image( padded_grid_stack=lens_data_stack_manual.padded_grid_stacks[0], psf=lens_data_stack_manual.psfs[0], unmasked_image_1d=padded_tracer.image_plane_images_1d[0]) unmasked_blurred_image_1 = \ lens_fit_util.unmasked_blurred_image_from_padded_grid_stack_psf_and_unmasked_image( padded_grid_stack=lens_data_stack_manual.padded_grid_stacks[1], psf=lens_data_stack_manual.psfs[1], unmasked_image_1d=padded_tracer.image_plane_images_1d[1]) assert (unmasked_blurred_image_0 == fit.unmasked_model_images[0] ).all() assert (unmasked_blurred_image_1 == fit.unmasked_model_images[1] ).all() unmasked_blurred_image_of_galaxies_i0 = \ lens_fit_util.unmasked_blurred_image_of_galaxies_from_psf_and_unmasked_1d_galaxy_images( galaxies=padded_tracer.image_plane.galaxies, image_plane_image_1d_of_galaxies=padded_tracer.image_plane.image_plane_images_1d_of_galaxies[0], padded_grid_stack=lens_data_stack_manual.padded_grid_stacks[0], psf=lens_data_stack_manual.psfs[0]) unmasked_blurred_image_of_galaxies_s0 = \ lens_fit_util.unmasked_blurred_image_of_galaxies_from_psf_and_unmasked_1d_galaxy_images( galaxies=padded_tracer.source_plane.galaxies, image_plane_image_1d_of_galaxies=padded_tracer.source_plane.image_plane_images_1d_of_galaxies[0], padded_grid_stack=lens_data_stack_manual.padded_grid_stacks[0], psf=lens_data_stack_manual.psfs[0]) unmasked_blurred_image_of_galaxies_i1 = \ lens_fit_util.unmasked_blurred_image_of_galaxies_from_psf_and_unmasked_1d_galaxy_images( galaxies=padded_tracer.image_plane.galaxies, image_plane_image_1d_of_galaxies=padded_tracer.image_plane.image_plane_images_1d_of_galaxies[0], padded_grid_stack=lens_data_stack_manual.padded_grid_stacks[1], psf=lens_data_stack_manual.psfs[1]) unmasked_blurred_image_of_galaxies_s1 = \ lens_fit_util.unmasked_blurred_image_of_galaxies_from_psf_and_unmasked_1d_galaxy_images( galaxies=padded_tracer.source_plane.galaxies, image_plane_image_1d_of_galaxies=padded_tracer.source_plane.image_plane_images_1d_of_galaxies[0], padded_grid_stack=lens_data_stack_manual.padded_grid_stacks[1], psf=lens_data_stack_manual.psfs[1]) assert ( unmasked_blurred_image_of_galaxies_i0[0] == fit. unmasked_model_images_of_planes_and_galaxies[0][0][0]).all() assert ( unmasked_blurred_image_of_galaxies_s0[0] == fit. unmasked_model_images_of_planes_and_galaxies[0][1][0]).all() assert ( unmasked_blurred_image_of_galaxies_i1[0] == fit. unmasked_model_images_of_planes_and_galaxies[1][0][0]).all() assert ( unmasked_blurred_image_of_galaxies_s1[0] == fit. unmasked_model_images_of_planes_and_galaxies[1][1][0]).all()
# Get the relative path to the config files and output folder in our workspace. path = '{}/../../'.format(os.path.dirname(os.path.realpath(__file__))) # Use this path to explicitly set the config path and output path. conf.instance = conf.Config(config_path=path + 'config', output_path=path + 'output') # First, we'll setup the grid stack we use to simulate a deflection profile. pixel_scale = 0.05 image_shape = (250, 250) grid_stack = grids.GridStack.from_shape_pixel_scale_and_sub_grid_size( shape=image_shape, pixel_scale=pixel_scale, sub_grid_size=4) # Now lets create two galaxies, using singular isothermal spheres. We'll put the two galaxies at different redshifts, # and the second galaxy will be much lower mass as if it is a 'perturber' of the main lens galaxy. lens_galaxy = g.Galaxy(mass=mp.SphericalIsothermal(centre=(0.0, 0.0), einstein_radius=1.0), redshift=0.5) perturber = g.Galaxy(mass=mp.SphericalIsothermal(centre=(0.5, 0.5), einstein_radius=0.1), redshift=0.2) # We only need the source galaxy to have a redshift - given we're not fitting an image it doens't need a light profile. source_galaxy = g.Galaxy(redshift=1.0) # We'll use a tracer to compute our multi-plane deflection angles. tracer = ray_tracing.TracerMultiPlanes( galaxies=[lens_galaxy, perturber, source_galaxy], image_plane_grid_stack=grid_stack) # We'll now extract the deflection angles from the tracer - we will extract the two deflection angle maps (y and x) # separately.
def test__deflection_angles(): image_grid = grids.RegularGrid.from_shape_and_pixel_scale(shape=(5, 5), pixel_scale=1.0) sis = mp.SphericalIsothermal(centre=(0.0, 0.0), einstein_radius=1.0) deflections_1d = sis.deflections_from_grid(grid=image_grid) deflections_x_2d = image_grid.array_2d_from_array_1d( array_1d=deflections_1d[:, 1]) assert deflections_x_2d[0, 0] == pytest.approx(-1.0 * deflections_x_2d[0, 4], 1e-2) assert deflections_x_2d[1, 1] == pytest.approx(-1.0 * deflections_x_2d[1, 3], 1e-2) assert deflections_x_2d[3, 1] == pytest.approx(-1.0 * deflections_x_2d[3, 3], 1e-2) assert deflections_x_2d[4, 0] == pytest.approx(-1.0 * deflections_x_2d[4, 4], 1e-2) assert deflections_x_2d[0, 2] == pytest.approx(deflections_x_2d[4, 2], 1e-2) sis = mp.SphericalIsothermal(centre=(0.0, 0.0), einstein_radius=1.0) deflections_1d = sis.deflections_from_grid(grid=image_grid) deflections_y_2d = image_grid.array_2d_from_array_1d( array_1d=deflections_1d[:, 0]) assert deflections_y_2d[0, 0] == pytest.approx(-1.0 * deflections_y_2d[4, 0], 1e-2) assert deflections_y_2d[1, 1] == pytest.approx(-1.0 * deflections_y_2d[3, 1], 1e-2) assert deflections_y_2d[1, 3] == pytest.approx(-1.0 * deflections_y_2d[3, 3], 1e-2) assert deflections_y_2d[0, 4] == pytest.approx(-1.0 * deflections_y_2d[4, 4], 1e-2) assert deflections_y_2d[2, 0] == pytest.approx(deflections_y_2d[2, 4], 1e-2) # def test__move_source_galaxy_around_source_plane__peak_follows_source_direction(): # # image_grid = masks.RegularGrid.from_shape_and_pixel_scale(shape=(5, 5), pixel_scales=1.0) # sis = mp.SphericalIsothermal(origin=(0.0, 0.0), einstein_radius=1.0) # sersic = lp.SphericalSersic(origin=(1.0, 0.0)) # # deflections = sis.deflections_from_grid(grid=image_grid) # source_grid = np.subtract(image_grid, deflections) # print(image_grid[22]) # print(deflections[22]) # print(source_grid[22]) # stop # source_image = sersic.intensities_from_grid(grid=source_grid) # print(source_image.argmax()) # # data_grids = masks.GridStack.from_shape_and_pixel_scale(shape=(5, 5), pixel_scales=1.0) # lens_galaxy = g.Galaxy(mass=sis) # source_galaxy = g.Galaxy(light=sersic) # tracer = ray_tracing.TracerImageSourcePlanes(lens_galaxies=[lens_galaxy], source_galaxies=[source_galaxy], # image_plane_grids=data_grids) # # print(source_grid) # print(tracer.source_plane.grid_stacks.regular) # print(np.subtract(source_grid, tracer.source_plane.grid_stacks.regular)) # print(tracer.regular_plane_image)
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) # We can print a galaxy to confirm it possesses the sis mass-profiles above. print(galaxy_with_3_mass_profiles) # We can use a galaxy plotter to plot these deflection angles. # (Deflection angles of mass-profiles add together just like the light-profile image's above) galaxy_plotters.plot_deflections_y(galaxy=galaxy_with_3_mass_profiles, grid=grid_stack.regular) galaxy_plotters.plot_deflections_x(galaxy=galaxy_with_3_mass_profiles, grid=grid_stack.regular) # I wonder what 3 summed surface density maps or potential's look like ;) galaxy_plotters.plot_surface_density(galaxy=galaxy_with_3_mass_profiles, grid=grid_stack.regular)
def make_galaxy(): return g.Galaxy(redshift=3, sersic=light_profiles.EllipticalSersic(), exponential=light_profiles.EllipticalExponential(), spherical=mass_profiles.SphericalIsothermal())
# that arise from a lens model fit (e.g due to the source reconstruction or quality of data). # Get the relative path to the config files and output folder in our workspace. path = '{}/../../'.format(os.path.dirname(os.path.realpath(__file__))) # Use this path to explicitly set the config path and output path. conf.instance = conf.Config(config_path=path+'config', output_path=path+'output') # First, we'll setup the grid stack we use to simulate a surface density profile. pixel_scale = 0.05 image_shape = (250, 250) grid_stack = grids.GridStack.from_shape_pixel_scale_and_sub_grid_size(shape=image_shape, pixel_scale=pixel_scale, sub_grid_size=4) # Now lets create a galaxy, using a simple singular isothermal sphere. galaxy = g.Galaxy(mass=mp.SphericalIsothermal(centre=(0.0, 0.0), einstein_radius=1.0)) # Next, we'll generate its surface density profile. Note that, because we're using the galaxy_util surface density # function, the sub-grid of the grid-stack that is passed to this function is used to over-sample the surface density. # The surface density averages over this sub-grid, such that it is the shape of the image (250, 250). surface_density = galaxy_util.surface_density_of_galaxies_from_grid(galaxies=[galaxy], grid=grid_stack.sub) surface_density = grid_stack.regular.scaled_array_from_array_1d(array_1d=surface_density) # Now, we'll set this surface density up as our 'galaxy-data', meaning that it is what we'll fit via a non-linear # search phase. To perform a fit we need a noise-map to help define our chi-squared. Given we are fitting a direct # lensing quantity the actual values of this noise-map arn't particularly important, so we'll just use a noise-map of # all 0.1's noise_map = scaled_array.ScaledSquarePixelArray(array=0.1*np.ones(surface_density.shape), pixel_scale=pixel_scale) data = gd.GalaxyData(image=surface_density, noise_map=noise_map, pixel_scale=pixel_scale) # The fit will use a mask, which we setup like any other fit. Lets use a circular mask of 2.0"
def test___if_galaxy_has_pixelization__unmasked_image_is_none(self): 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)) psf_0 = im.PSF(array=(np.array([[0.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 0.0]])), pixel_scale=1.0) psf_1 = 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) g0 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=0.1), pixelization=pix.Rectangular(), regularization=reg.Constant()) g1 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=0.2)) g2 = g.Galaxy(mass_profile=mp.SphericalIsothermal(einstein_radius=1.0)) tracer = ray_tracing_stack.TracerImageSourcePlanesStack( lens_galaxies=[g0, g1, g2], source_galaxies=[g0, g1], image_plane_grid_stacks=[padded_grid_stack, padded_grid_stack]) unmasked_blurred_image_of_datas_planes_and_galaxies = \ stack_util.unmasked_blurred_image_of_datas_planes_and_galaxies_from_padded_grid_stacks_and_psf( planes=tracer.planes, padded_grid_stacks=[padded_grid_stack, padded_grid_stack], psfs=[psf_0, psf_1]) assert unmasked_blurred_image_of_datas_planes_and_galaxies[0][0][ 0] == None assert type(unmasked_blurred_image_of_datas_planes_and_galaxies[0][0] [1]) == scaled_array.ScaledSquarePixelArray assert unmasked_blurred_image_of_datas_planes_and_galaxies[1][0][ 0] == None assert type(unmasked_blurred_image_of_datas_planes_and_galaxies[1][0] [1]) == scaled_array.ScaledSquarePixelArray assert unmasked_blurred_image_of_datas_planes_and_galaxies[0][1][ 0] == None assert type(unmasked_blurred_image_of_datas_planes_and_galaxies[0][1] [1]) == scaled_array.ScaledSquarePixelArray assert unmasked_blurred_image_of_datas_planes_and_galaxies[1][1][ 0] == None assert type(unmasked_blurred_image_of_datas_planes_and_galaxies[1][1] [1]) == scaled_array.ScaledSquarePixelArray g0 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=0.1)) g1 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=0.2), pixelization=pix.Rectangular(), regularization=reg.Constant()) g2 = g.Galaxy(mass_profile=mp.SphericalIsothermal(einstein_radius=1.0)) tracer = ray_tracing_stack.TracerImageSourcePlanesStack( lens_galaxies=[g0, g1, g2], source_galaxies=[g0, g1], image_plane_grid_stacks=[padded_grid_stack, padded_grid_stack]) unmasked_blurred_image_of_datas_planes_and_galaxies = \ stack_util.unmasked_blurred_image_of_datas_planes_and_galaxies_from_padded_grid_stacks_and_psf( planes=tracer.planes, padded_grid_stacks=[padded_grid_stack, padded_grid_stack], psfs=[psf_0, psf_1]) assert type(unmasked_blurred_image_of_datas_planes_and_galaxies[0][0] [0]) == scaled_array.ScaledSquarePixelArray assert unmasked_blurred_image_of_datas_planes_and_galaxies[0][0][ 1] == None assert type(unmasked_blurred_image_of_datas_planes_and_galaxies[1][0] [0]) == scaled_array.ScaledSquarePixelArray assert unmasked_blurred_image_of_datas_planes_and_galaxies[1][0][ 1] == None assert type(unmasked_blurred_image_of_datas_planes_and_galaxies[0][1] [0]) == scaled_array.ScaledSquarePixelArray assert unmasked_blurred_image_of_datas_planes_and_galaxies[0][1][ 1] == None assert type(unmasked_blurred_image_of_datas_planes_and_galaxies[1][1] [0]) == scaled_array.ScaledSquarePixelArray assert unmasked_blurred_image_of_datas_planes_and_galaxies[1][1][ 1] == None
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
# - If two or more source galaxies are at the same redshift in the source-plane, their light is ray-traced in the # same way. Therefore, when determining their lensed images, we can sum the lensed images of each galaxy's light-profiles. # So, lets do it - lets use the 'plane' module in AutoLens to create a strong lensing system like the one pictured # above. For simplicity, we'll assume 1 lens galaxy and 1 source galaxy. # As always, we need grids, where our grids are the coordinates we'll 'trace' from the image-plane to the source-plane # in the lensing configuration above. Our grid-stack is therefore no longer just a 'grid-stack', but the grid-stack # representing our image-plane coordinates. Thus, lets name as such. image_plane_grid_stack = grids.GridStack.from_shape_pixel_scale_and_sub_grid_size(shape=(100, 100), pixel_scale=0.05, sub_grid_size=2) # Whereas before we called our galaxy's things like 'galaxy_with_light_profile', lets now refer to them by their role # in lensing, e.g. 'lens_galaxy' and 'source_galaxy'. mass_profile = mass_profiles.SphericalIsothermal(centre=(0.0, 0.0), einstein_radius=1.6) lens_galaxy = galaxy.Galaxy(mass=mass_profile) 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=light_profile) # Lets setup our image-plane. This plane takes the lens galaxy we made above and the grid-stack of # image-plane coordinates. image_plane = plane.Plane(galaxies=[lens_galaxy], grid_stack=image_plane_grid_stack) # Up to now, we've kept our galaxies and grids separate, and passed the grid to a galaxy object to compute # its quantities (e.g. to compute light-profile intensities, we'd write galaxy.intensities_from_grid(grid=grid)). # # Plane's combine the galaxies and grids into one object, thus once we've setup a plane there is no longer any need # have to pass it a grid to compute its quantities. Furthermore, once these quantities are in a plane, they are # automatically mapped back to their original 2D grid-arrays. print('deflection-angles of planes regular-grid pixel 1:')
def make_mass_profile(): return mp.SphericalIsothermal(einstein_radius=1.0)
def make_galaxy_mass(): return g.Galaxy(mass=mp.SphericalIsothermal(einstein_radius=1.0), redshift=1.0)
def test__no_light_profile__returns_none(self): gal = g.Galaxy(redshift=0.5, mass=mp.SphericalIsothermal()) assert gal.luminosity_within_circle(radius=1.0) == None assert gal.luminosity_within_ellipse(major_axis=1.0) == None
# We'll also give the lens galaxy some attributes we didn't in the last tutorial: # 1) A light-profile, meaning its light will appear in the image-plane image. # 2) An external shear, which accounts for the deflection of light due to line-of-sight structures. # 3) A redshift, which the tracer will use to convert arc second coordinates to kpc. lens_galaxy = g.Galaxy(light=lp.SphericalSersic(centre=(0.0, 0.0), intensity=2.0, effective_radius=0.5, sersic_index=2.5), mass=mp.EllipticalIsothermal(centre=(0.0, 0.0), axis_ratio=0.8, phi=90.0, einstein_radius=1.6), shear=mp.ExternalShear(magnitude=0.05, phi=45.0), redshift=0.5) print(lens_galaxy) # Lets also create a small satellite galaxy nearby the lens galaxy and at the same redshift. lens_satellite = g.Galaxy(light=lp.SphericalDevVaucouleurs(centre=(1.0, 0.0), intensity=2.0, effective_radius=0.2), mass=mp.SphericalIsothermal(centre=(1.0, 0.0), einstein_radius=0.4), redshift=0.5) print(lens_satellite) # Lets have a quick look at the appearance of our lens galaxy and its satellite galaxy_plotters.plot_intensities(galaxy=lens_galaxy, grid=image_plane_grid_stack.regular, title='Lens Galaxy') galaxy_plotters.plot_intensities(galaxy=lens_satellite, grid=image_plane_grid_stack.regular, title='Lens Satellite') # And their deflection angles - note that the satellite doesn't contribute as much to the deflections galaxy_plotters.plot_deflections_y(galaxy=lens_galaxy, grid=image_plane_grid_stack.regular, title='Lens Galaxy Deflections (y)') galaxy_plotters.plot_deflections_y(galaxy=lens_satellite, grid=image_plane_grid_stack.regular, title='Lens Satellite Deflections (y)') galaxy_plotters.plot_deflections_x(galaxy=lens_galaxy, grid=image_plane_grid_stack.regular, title='Lens Galalxy Deflections (x)') galaxy_plotters.plot_deflections_x(galaxy=lens_satellite, grid=image_plane_grid_stack.regular, title='Lens Satellite Deflections (x)') # Now, lets make two source galaxies at redshift 1.0. Lets not use the terms 'light' and 'mass' to setup the light and # mass profiles. Instead, lets use more descriptive names of what we think each component represents ( e.g. a 'bulge' and 'disk').
def make_galaxy_mass(): return g.Galaxy(mass_profile=mp.SphericalIsothermal(einstein_radius=1.0))