def test__same_as_above__multiple_galaxies(self, grid_stack_0, grid_stack_1): # Overwrite one value so intensity in each pixel is different grid_stack_0.blurring[1] = np.array([2.0, 2.0]) grid_stack_1.blurring[1] = np.array([2.0, 2.0]) g0 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=1.0)) g1 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=2.0)) g0_image_grid_0 = galaxy_util.intensities_of_galaxies_from_grid( grid_stack_0.blurring, galaxies=[g0]) g1_image_grid_0 = galaxy_util.intensities_of_galaxies_from_grid( grid_stack_0.blurring, galaxies=[g1]) plane_stack = pl_stack.PlaneStack( galaxies=[g0, g1], grid_stacks=[grid_stack_0, grid_stack_1]) assert (plane_stack.image_plane_blurring_images_1d[0] == g0_image_grid_0 + g1_image_grid_0).all() g0_image_grid_1 = galaxy_util.intensities_of_galaxies_from_grid( grid_stack_1.blurring, galaxies=[g0]) g1_image_grid_1 = galaxy_util.intensities_of_galaxies_from_grid( grid_stack_1.blurring, galaxies=[g1]) plane_stack = pl_stack.PlaneStack( galaxies=[g0, g1], grid_stacks=[grid_stack_0, grid_stack_1]) assert (plane_stack.image_plane_blurring_images_1d[1] == g0_image_grid_1 + g1_image_grid_1).all()
def test__same_as_above__x2_galaxy_in_plane__or_galaxy_x2_sis__deflections_double( self, grid_stack_0, galaxy_mass, galaxy_mass_x2): plane_stack = pl_stack.PlaneStack( galaxies=[galaxy_mass_x2], grid_stacks=[grid_stack_0, grid_stack_0], compute_deflections=True) sub_galaxy_deflections = galaxy_mass_x2.deflections_from_grid( grid_stack_0.sub) blurring_galaxy_deflections = galaxy_mass_x2.deflections_from_grid( grid_stack_0.blurring) assert plane_stack.deflection_stacks[0].regular == pytest.approx \ (np.array([[2.0 * 0.707, 2.0 * 0.707], [2.0, 0.0]]), 1e-3) assert (plane_stack.deflection_stacks[0].sub == sub_galaxy_deflections).all() assert (plane_stack.deflection_stacks[0].blurring == blurring_galaxy_deflections).all() assert plane_stack.deflection_stacks[1].regular == pytest.approx \ (np.array([[2.0 * 0.707, 2.0 * 0.707], [2.0, 0.0]]), 1e-3) assert (plane_stack.deflection_stacks[1].sub == sub_galaxy_deflections).all() assert (plane_stack.deflection_stacks[1].blurring == blurring_galaxy_deflections).all() plane_stack = pl_stack.PlaneStack( galaxies=[galaxy_mass, galaxy_mass], grid_stacks=[grid_stack_0, grid_stack_0], compute_deflections=True) sub_galaxy_deflections = galaxy_mass.deflections_from_grid( grid_stack_0.sub) blurring_galaxy_deflections = galaxy_mass.deflections_from_grid( grid_stack_0.blurring) assert plane_stack.deflection_stacks[0].regular == pytest.approx \ (np.array([[2.0 * 0.707, 2.0 * 0.707], [2.0, 0.0]]), 1e-3) assert (plane_stack.deflection_stacks[0].sub == 2.0 * sub_galaxy_deflections).all() assert (plane_stack.deflection_stacks[0].blurring == 2.0 * blurring_galaxy_deflections).all() assert plane_stack.deflection_stacks[1].regular == pytest.approx \ (np.array([[2.0 * 0.707, 2.0 * 0.707], [2.0, 0.0]]), 1e-3) assert (plane_stack.deflection_stacks[1].sub == 2.0 * sub_galaxy_deflections).all() assert (plane_stack.deflection_stacks[1].blurring == 2.0 * blurring_galaxy_deflections).all()
def __init__(self, lens_galaxies, source_galaxies, image_plane_grid_stacks, borders=None, cosmology=cosmo.Planck15): """Ray-tracer for a lens system with two planes, an image-plane and source-plane. This tracer has a list of grid-stacks (see grid_stack.GridStack) which are all used for ray-tracing. Parameters ---------- lens_galaxies : [Galaxy] The list of galaxies in the image-plane. source_galaxies : [Galaxy] The list of galaxies in the source-plane. image_plane_grid_stacks : [grid_stacks.GridStack] The image-plane grid stacks which are traced. (each stack includes the regular-grid, sub-grid, \ blurring-grid, etc.). borders : [masks.RegularGridBorder] The border of each grid-stacks's regular-grid, which is used to relocate demagnified traced pixels to the \ source-plane border. cosmology : astropy.cosmology.Planck15 The cosmology of the ray-tracing calculation. """ image_plane_grid_stacks = list( map( lambda data_grids: pix. setup_image_plane_pixelization_grid_from_galaxies_and_grid_stack( galaxies=source_galaxies, grid_stack=data_grids), image_plane_grid_stacks)) image_plane = plane_stack.PlaneStack( galaxies=lens_galaxies, grid_stacks=image_plane_grid_stacks, borders=borders, compute_deflections=True) source_plane_grid_stacks = image_plane.trace_grids_to_next_plane() source_plane = plane_stack.PlaneStack( galaxies=source_galaxies, grid_stacks=source_plane_grid_stacks, borders=borders, compute_deflections=False, cosmology=cosmology) super(TracerImageSourcePlanesStack, self).__init__(planes=[image_plane, source_plane], cosmology=cosmology)
def test__padded_grid_in__tracer_has_padded_grid_property( self, grid_stack_0, padded_grid_stack, galaxy_light): plane_stack = pl_stack.PlaneStack(grid_stacks=[grid_stack_0], galaxies=[galaxy_light]) assert plane_stack.has_padded_grid_stack == False plane_stack = pl_stack.PlaneStack(grid_stacks=[padded_grid_stack], galaxies=[galaxy_light]) assert plane_stack.has_padded_grid_stack == True plane_stack = pl_stack.PlaneStack( grid_stacks=[grid_stack_0, padded_grid_stack], galaxies=[galaxy_light]) assert plane_stack.has_padded_grid_stack == True
def test__total_images(self, grid_stack_0): plane_stack = pl_stack.PlaneStack(galaxies=[g.Galaxy()], grid_stacks=[grid_stack_0]) assert plane_stack.total_grid_stacks == 1 plane_stack = pl_stack.PlaneStack( galaxies=[g.Galaxy()], grid_stacks=[grid_stack_0, grid_stack_0]) assert plane_stack.total_grid_stacks == 2 plane_stack = pl_stack.PlaneStack( galaxies=[g.Galaxy()], grid_stacks=[grid_stack_0, grid_stack_0, grid_stack_0]) assert plane_stack.total_grid_stacks == 3
def test__same_as_above_but_test_deflections(self, grid_stack_0, galaxy_mass): plane_stack = pl_stack.PlaneStack( galaxies=[galaxy_mass], grid_stacks=[grid_stack_0, grid_stack_0], compute_deflections=True) sub_galaxy_deflections = galaxy_mass.deflections_from_grid( grid_stack_0.sub) blurring_galaxy_deflections = galaxy_mass.deflections_from_grid( grid_stack_0.blurring) assert plane_stack.deflection_stacks[0].regular == pytest.approx( np.array([[0.707, 0.707], [1.0, 0.0]]), 1e-3) assert (plane_stack.deflection_stacks[0].sub == sub_galaxy_deflections).all() assert (plane_stack.deflection_stacks[0].blurring == blurring_galaxy_deflections).all() assert plane_stack.deflection_stacks[1].regular == pytest.approx( np.array([[0.707, 0.707], [1.0, 0.0]]), 1e-3) assert (plane_stack.deflection_stacks[1].sub == sub_galaxy_deflections).all() assert (plane_stack.deflection_stacks[1].blurring == blurring_galaxy_deflections).all()
def test__images_from_plane__same_as_light_profile_images( self, grid_stack_0, galaxy_light): # Overwrite one value so intensity in each pixel is different grid_stack_0.sub[5] = np.array([2.0, 2.0]) lp = galaxy_light.light_profiles[0] lp_sub_image = lp.intensities_from_grid(grid_stack_0.sub) # Perform sub gridding average manually lp_image_pixel_0 = (lp_sub_image[0] + lp_sub_image[1] + lp_sub_image[2] + lp_sub_image[3]) / 4 lp_image_pixel_1 = (lp_sub_image[4] + lp_sub_image[5] + lp_sub_image[6] + lp_sub_image[7]) / 4 plane_stack = pl_stack.PlaneStack(galaxies=[galaxy_light], grid_stacks=[grid_stack_0]) assert (plane_stack.image_plane_images_1d[0][0] == lp_image_pixel_0 ).all() assert (plane_stack.image_plane_images_1d[0][1] == lp_image_pixel_1 ).all() assert (plane_stack.image_plane_images[0] == grid_stack_0.regular.scaled_array_from_array_1d( plane_stack.image_plane_images_1d[0])).all()
def test__grid_stack_0_setup_for_regular_sub_and_blurring__no_deflections( self, grid_stack_0, galaxy_mass): plane_stack = pl_stack.PlaneStack( galaxies=[galaxy_mass], grid_stacks=[grid_stack_0, grid_stack_0], compute_deflections=False) assert plane_stack.grid_stacks[0].regular == pytest.approx( np.array([[1.0, 1.0], [1.0, 0.0]]), 1e-3) assert plane_stack.grid_stacks[0].sub == pytest.approx( np.array([[1.0, 1.0], [1.0, 0.0], [1.0, 1.0], [1.0, 0.0], [-1.0, 2.0], [-1.0, 4.0], [1.0, 2.0], [1.0, 4.0]]), 1e-3) assert plane_stack.grid_stacks[0].blurring == pytest.approx( np.array([[1.0, 0.0], [-6.0, -3.0], [-6.0, 3.0], [-6.0, 9.0], [0.0, -9.0], [0.0, 9.0], [6.0, -9.0], [6.0, -3.0], [6.0, 3.0], [6.0, 9.0]]), 1e-3) assert plane_stack.grid_stacks[1].regular == pytest.approx( np.array([[1.0, 1.0], [1.0, 0.0]]), 1e-3) assert plane_stack.grid_stacks[1].sub == pytest.approx( np.array([[1.0, 1.0], [1.0, 0.0], [1.0, 1.0], [1.0, 0.0], [-1.0, 2.0], [-1.0, 4.0], [1.0, 2.0], [1.0, 4.0]]), 1e-3) assert plane_stack.grid_stacks[1].blurring == pytest.approx( np.array([[1.0, 0.0], [-6.0, -3.0], [-6.0, 3.0], [-6.0, 9.0], [0.0, -9.0], [0.0, 9.0], [6.0, -9.0], [6.0, -3.0], [6.0, 3.0], [6.0, 9.0]]), 1e-3) assert plane_stack.deflection_stacks == None
def test__ensure_index_of_plane_image_has_negative_arcseconds_at_start( self, grid_stack_0): # The grid coordinates -2.0 -> 2.0 mean a plane of shape (5,5) has arc second coordinates running over # -1.6, -0.8, 0.0, 0.8, 1.6. The origin -1.6, -1.6 of the model_galaxy means its brighest pixel should be # index 0 of the 1D grid and (0,0) of the 2d plane datas_. msk = mask.Mask(array=np.full((5, 5), False), pixel_scale=1.0) grid_stack_0.regular = grids.RegularGrid(np.array([[-2.0, -2.0], [2.0, 2.0]]), mask=msk) g0 = g.Galaxy(light_profile=lp.EllipticalSersic(centre=(1.6, -1.6), intensity=1.0)) plane_stack = pl_stack.PlaneStack(galaxies=[g0], grid_stacks=[grid_stack_0]) assert plane_stack.plane_images[0].shape == (5, 5) assert np.unravel_index(plane_stack.plane_images[0].argmax(), plane_stack.plane_images[0].shape) == (0, 0) g0 = g.Galaxy(light_profile=lp.EllipticalSersic(centre=(1.6, 1.6), intensity=1.0)) plane_stack = pl_stack.PlaneStack(galaxies=[g0], grid_stacks=[grid_stack_0]) assert np.unravel_index(plane_stack.plane_images[0].argmax(), plane_stack.plane_images[0].shape) == (0, 4) g0 = g.Galaxy(light_profile=lp.EllipticalSersic( centre=(-1.6, -1.6), intensity=1.0)) plane_stack = pl_stack.PlaneStack(galaxies=[g0], grid_stacks=[grid_stack_0]) assert np.unravel_index(plane_stack.plane_images[0].argmax(), plane_stack.plane_images[0].shape) == (4, 0) g0 = g.Galaxy(light_profile=lp.EllipticalSersic(centre=(-1.6, 1.6), intensity=1.0)) plane_stack = pl_stack.PlaneStack(galaxies=[g0], grid_stacks=[grid_stack_0]) assert np.unravel_index(plane_stack.plane_images[0].argmax(), plane_stack.plane_images[0].shape) == (4, 4)
def test__no_galaxies_with_pixelizations_in_plane__returns_none( self, grid_stack_0): galaxy_no_pix = g.Galaxy() plane_stack = pl_stack.PlaneStack(galaxies=[galaxy_no_pix], grid_stacks=[grid_stack_0], borders=[MockBorders()]) assert plane_stack.regularization is None
def test__1_galaxy_in_plane__it_has_pixelization__returns_mapper( self, grid_stack_0): galaxy_pix = g.Galaxy(pixelization=MockPixelization(value=1), regularization=MockRegularization(value=0)) plane_stack = pl_stack.PlaneStack(galaxies=[galaxy_pix], grid_stacks=[grid_stack_0], borders=[MockBorders()]) assert plane_stack.regularization.value == 0
def test__plane_has_no_border__still_returns_mapper( self, grid_stack_0): galaxy_pix = g.Galaxy(pixelization=MockPixelization(value=1), regularization=MockRegularization(value=0)) galaxy_no_pix = g.Galaxy() plane_stack = pl_stack.PlaneStack( galaxies=[galaxy_no_pix, galaxy_pix], grid_stacks=[grid_stack_0]) assert plane_stack.mapper == 1
def test__2_galaxies_in_plane__1_has_pixelization__extracts_reconstructor( self, grid_stack_0): galaxy_pix = g.Galaxy(pixelization=MockPixelization(value=1), regularization=MockRegularization(value=0)) galaxy_no_pix = g.Galaxy() plane_stack = pl_stack.PlaneStack( galaxies=[galaxy_no_pix, galaxy_pix], grid_stacks=[grid_stack_0], borders=[MockBorders()]) assert plane_stack.regularization.value == 0
def test__2_galaxies_in_plane__both_have_pixelization__raises_error( self, grid_stack_0): galaxy_pix_0 = g.Galaxy(pixelization=MockPixelization(value=1), regularization=MockRegularization(value=0)) galaxy_pix_1 = g.Galaxy(pixelization=MockPixelization(value=2), regularization=MockRegularization(value=0)) plane_stack = pl_stack.PlaneStack( galaxies=[galaxy_pix_0, galaxy_pix_1], grid_stacks=[grid_stack_0], borders=[MockBorders()]) with pytest.raises(exc.PixelizationException): plane_stack.regularization
def test__image_plane_image_of_galaxies__use_multiple_grids__get_multiple_images( self, grid_stack_0, grid_stack_1): # Overwrite one value so intensity in each pixel is different grid_stack_0.sub[5] = np.array([2.0, 2.0]) g0 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=1.0)) g1 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=2.0)) plane_stack = pl_stack.PlaneStack( galaxies=[g0, g1], grid_stacks=[grid_stack_0, grid_stack_1]) g0_image_grid_0 = galaxy_util.intensities_of_galaxies_from_grid( grid_stack_0.sub, galaxies=[g0]) g1_image_grid_0 = galaxy_util.intensities_of_galaxies_from_grid( grid_stack_0.sub, galaxies=[g1]) assert plane_stack.image_plane_images_1d[0] == pytest.approx( g0_image_grid_0 + g1_image_grid_0, 1.0e-4) assert (plane_stack.image_plane_images[0] == grid_stack_0.regular.scaled_array_from_array_1d( plane_stack.image_plane_images_1d[0])).all() assert (plane_stack.image_plane_images_1d_of_galaxies[0][0] == g0_image_grid_0).all() assert (plane_stack.image_plane_images_1d_of_galaxies[0][1] == g1_image_grid_0).all() g0_image_grid_1 = galaxy_util.intensities_of_galaxies_from_grid( grid_stack_1.sub, galaxies=[g0]) g1_image_grid_1 = galaxy_util.intensities_of_galaxies_from_grid( grid_stack_1.sub, galaxies=[g1]) assert plane_stack.image_plane_images_1d[1] == pytest.approx( g0_image_grid_1 + g1_image_grid_1, 1.0e-4) assert (plane_stack.image_plane_images[1] == grid_stack_0.regular.scaled_array_from_array_1d( plane_stack.image_plane_images_1d[1])).all() assert (plane_stack.image_plane_images_1d_of_galaxies[1][0] == g0_image_grid_1).all() assert (plane_stack.image_plane_images_1d_of_galaxies[1][1] == g1_image_grid_1).all()
def test__same_as_above_but_for_galaxies(self, grid_stack_0, grid_stack_1, galaxy_light): # Overwrite one value so intensity in each pixel is different grid_stack_0.blurring[1] = np.array([2.0, 2.0]) galaxy_image = galaxy_util.intensities_of_galaxies_from_grid( grid_stack_0.blurring, galaxies=[galaxy_light]) galaxy_image_1 = galaxy_util.intensities_of_galaxies_from_grid( grid_stack_1.blurring, galaxies=[galaxy_light]) plane_stack = pl_stack.PlaneStack( galaxies=[galaxy_light], grid_stacks=[grid_stack_0, grid_stack_1]) assert (plane_stack.image_plane_blurring_images_1d[0] == galaxy_image).all() assert (plane_stack.image_plane_blurring_images_1d[1] == galaxy_image_1).all()
def test__images_from_plane__same_as_their_light_profile( self, grid_stack_0, grid_stack_1, galaxy_light): # Overwrite one value so intensity in each pixel is different grid_stack_0.blurring[1] = np.array([2.0, 2.0]) lp = galaxy_light.light_profiles[0] lp_blurring_image = lp.intensities_from_grid(grid_stack_0.blurring) lp_blurring_image_1 = lp.intensities_from_grid( grid_stack_1.blurring) plane_stack = pl_stack.PlaneStack( galaxies=[galaxy_light], grid_stacks=[grid_stack_0, grid_stack_1]) assert (plane_stack.image_plane_blurring_images_1d[0] == lp_blurring_image).all() assert (plane_stack.image_plane_blurring_images_1d[1] == lp_blurring_image_1).all()
def test__computes_x1_potential__uses_primary_grid_stack_0( self, grid_stack_0, grid_stack_1, galaxy_mass): mp = galaxy_mass.mass_profiles[0] mp_sub_image = mp.potential_from_grid( grid_stack_0.sub.unlensed_grid) # Perform sub gridding average manually mp_image_pixel_0 = (mp_sub_image[0] + mp_sub_image[1] + mp_sub_image[2] + mp_sub_image[3]) / 4 mp_image_pixel_1 = (mp_sub_image[4] + mp_sub_image[5] + mp_sub_image[6] + mp_sub_image[7]) / 4 plane_stack = pl_stack.PlaneStack( galaxies=[galaxy_mass], grid_stacks=[grid_stack_0, grid_stack_1]) assert (plane_stack.potential[1, 1] == mp_image_pixel_0).all() assert (plane_stack.potential[1, 2] == mp_image_pixel_1).all()
def test__x2_data_stacks__x2_plane_images(self, grid_stack_0, grid_stack_1): grid_stack_0.regular[1] = np.array([2.0, 2.0]) galaxy = g.Galaxy(light=lp.EllipticalSersic(intensity=1.0)) plane_stack = pl_stack.PlaneStack( galaxies=[galaxy], grid_stacks=[grid_stack_0, grid_stack_1], compute_deflections=False) plane_image_from_func = lens_util.plane_image_of_galaxies_from_grid( shape=(3, 4), grid=grid_stack_0.regular, galaxies=[galaxy]) plane_image_from_func_1 = lens_util.plane_image_of_galaxies_from_grid( shape=(3, 4), grid=grid_stack_1.regular, galaxies=[galaxy]) assert (plane_image_from_func == plane_stack.plane_images[0]).all() assert ( plane_image_from_func_1 == plane_stack.plane_images[1]).all()
def test__image_from_plane__same_as_galaxy_images( self, grid_stack_0, galaxy_light): # Overwrite one value so intensity in each pixel is different grid_stack_0.sub[5] = np.array([2.0, 2.0]) galaxy_image = galaxy_util.intensities_of_galaxies_from_grid( grid_stack_0.sub, galaxies=[galaxy_light]) plane_stack = pl_stack.PlaneStack( galaxies=[galaxy_light], grid_stacks=[grid_stack_0, grid_stack_0]) assert (plane_stack.image_plane_images_1d[0] == galaxy_image).all() assert (plane_stack.image_plane_images[0] == grid_stack_0.regular.scaled_array_from_array_1d( plane_stack.image_plane_images_1d[0])).all() assert (plane_stack.image_plane_images_1d[1] == galaxy_image).all() assert (plane_stack.image_plane_images[1] == grid_stack_0.regular.scaled_array_from_array_1d( plane_stack.image_plane_images_1d[1])).all()
def __init__(self, lens_galaxies, image_plane_grid_stacks, borders=None, cosmology=cosmo.Planck15): """Ray tracer for a lens system with just an image-plane. As there is only 1 plane, there are no ray-tracing calculations. This class is therefore only used for fitting \ image-plane galaxies with light profiles. This tracer has a list of grid-stacks (see grid_stack.GridStack) which are all used for ray-tracing. Parameters ---------- lens_galaxies : [Galaxy] The list of lens galaxies in the image-plane. image_plane_grid_stacks : [grid_stacks.GridStack] The image-plane grid stacks which are traced. (each stack includes the regular-grid, sub-grid, \ blurring-grid, etc.). borders : [masks.RegularGridBorder] The border of each grid-stacks's regular-grid, which is used to relocate demagnified traced pixels to the \ source-plane border. cosmology : astropy.cosmology.Planck15 The cosmology of the ray-tracing calculation. """ if not lens_galaxies: raise exc.RayTracingException( 'No lens galaxies have been input into the Tracer') image_plane = plane_stack.PlaneStack( galaxies=lens_galaxies, grid_stacks=image_plane_grid_stacks, borders=borders, compute_deflections=True, cosmology=cosmology) super(TracerImagePlaneStack, self).__init__(planes=[image_plane], cosmology=cosmology)
def test__computes_x1_deflections__uses_primary_grid_stack_0( self, grid_stack_0, grid_stack_1, galaxy_mass): mp = galaxy_mass.mass_profiles[0] mp_sub_image = mp.deflections_from_grid( grid_stack_0.sub.unlensed_grid) # Perform sub gridding average manually mp_image_pixel_0x = (mp_sub_image[0, 0] + mp_sub_image[1, 0] + mp_sub_image[2, 0] + mp_sub_image[3, 0]) / 4 mp_image_pixel_1x = (mp_sub_image[4, 0] + mp_sub_image[5, 0] + mp_sub_image[6, 0] + mp_sub_image[7, 0]) / 4 mp_image_pixel_0y = (mp_sub_image[0, 1] + mp_sub_image[1, 1] + mp_sub_image[2, 1] + mp_sub_image[3, 1]) / 4 mp_image_pixel_1y = (mp_sub_image[4, 1] + mp_sub_image[5, 1] + mp_sub_image[6, 1] + mp_sub_image[7, 1]) / 4 plane_stack = pl_stack.PlaneStack( galaxies=[galaxy_mass], grid_stacks=[grid_stack_0, grid_stack_1]) assert (plane_stack.deflections_1d[0, 0] == mp_image_pixel_0x).all() assert (plane_stack.deflections_1d[0, 1] == mp_image_pixel_0y).all() assert (plane_stack.deflections_1d[1, 0] == mp_image_pixel_1x).all() assert (plane_stack.deflections_1d[1, 1] == mp_image_pixel_1y).all() assert (plane_stack.deflections_y == grid_stack_0.regular.scaled_array_from_array_1d( plane_stack.deflections_1d[:, 0])).all() assert (plane_stack.deflections_x == grid_stack_0.regular.scaled_array_from_array_1d( plane_stack.deflections_1d[:, 1])).all()
def __init__(self, galaxies, image_plane_grid_stacks, borders=None, cosmology=cosmo.Planck15): """Ray-tracer for a lens system with any number of planes. To perform multi-plane ray-tracing, the cosmology that is input is used to rescale deflection-angles \ according to the lens-geometry of the multi-plane system. All galaxies input to the tracer must therefore \ have redshifts. This tracer has a list of grid-stacks (see grid_stack.GridStack) which are all used for ray-tracing. Parameters ---------- galaxies : [Galaxy] The list of galaxies in the ray-tracing calculation. image_plane_grid_stacks : [grid_stacks.GridStack] The image-plane grid stacks which are traced. (each stack includes the regular-grid, sub-grid, \ blurring-grid, etc.). borders : [masks.RegularGridBorder] The border of each grid-stacks's regular-grid, which is used to relocate demagnified traced pixels to the \ source-plane border. cosmology : astropy.cosmology The cosmology of the ray-tracing calculation. """ ordered_redshifts = lens_util.ordered_plane_redshifts_from_galaxies( galaxies=galaxies) galaxies_in_redshift_ordered_lists = \ lens_util.galaxies_in_redshift_ordered_planes_from_galaxies(galaxies=galaxies, plane_redshifts=ordered_redshifts) image_plane_grid_stacks = list( map( lambda grid_stack: pix. setup_image_plane_pixelization_grid_from_galaxies_and_grid_stack( galaxies=galaxies, grid_stack=grid_stack), image_plane_grid_stacks)) planes = [] for plane_index in range(0, len(ordered_redshifts)): if plane_index < len(ordered_redshifts) - 1: compute_deflections = True elif plane_index == len(ordered_redshifts) - 1: compute_deflections = False else: raise exc.RayTracingException( 'A galaxy was not correctly allocated its previous / next redshifts' ) new_grid_stacks = image_plane_grid_stacks if plane_index > 0: for previous_plane_index in range(plane_index): scaling_factor = lens_util.scaling_factor_between_redshifts_for_cosmology( z1=ordered_redshifts[previous_plane_index], z2=ordered_redshifts[plane_index], z_final=ordered_redshifts[-1], cosmology=cosmology) def scale(grid): return np.multiply(scaling_factor, grid) if planes[ previous_plane_index].deflection_stacks is not None: scaled_deflections = list( map( lambda deflection_stack: deflection_stack. apply_function(scale), planes[previous_plane_index].deflection_stacks) ) else: scaled_deflections = None if scaled_deflections is not None: def minus(grid, deflections): return grid - deflections new_grid_stacks = list( map( lambda grid, deflections: grid.map_function( minus, deflections), new_grid_stacks, scaled_deflections)) planes.append( plane_stack.PlaneStack( galaxies=galaxies_in_redshift_ordered_lists[plane_index], grid_stacks=new_grid_stacks, borders=borders, compute_deflections=compute_deflections, cosmology=cosmology)) super(TracerMultiPlanesStack, self).__init__(planes=planes, cosmology=cosmology)
def test__padded_grid_stack_in__image_plane_image_is_padded( self, padded_grid_stack, galaxy_light): lp = galaxy_light.light_profiles[0] lp_sub_image = lp.intensities_from_grid(padded_grid_stack.sub) # Perform sub gridding average manually lp_image_pixel_0 = (lp_sub_image[0] + lp_sub_image[1] + lp_sub_image[2] + lp_sub_image[3]) / 4 lp_image_pixel_1 = (lp_sub_image[4] + lp_sub_image[5] + lp_sub_image[6] + lp_sub_image[7]) / 4 lp_image_pixel_2 = (lp_sub_image[8] + lp_sub_image[9] + lp_sub_image[10] + lp_sub_image[11]) / 4 lp_image_pixel_3 = (lp_sub_image[12] + lp_sub_image[13] + lp_sub_image[14] + lp_sub_image[15]) / 4 lp_image_pixel_4 = (lp_sub_image[16] + lp_sub_image[17] + lp_sub_image[18] + lp_sub_image[19]) / 4 lp_image_pixel_5 = (lp_sub_image[20] + lp_sub_image[21] + lp_sub_image[22] + lp_sub_image[23]) / 4 lp_image_pixel_6 = (lp_sub_image[24] + lp_sub_image[25] + lp_sub_image[26] + lp_sub_image[27]) / 4 lp_image_pixel_7 = (lp_sub_image[28] + lp_sub_image[29] + lp_sub_image[30] + lp_sub_image[31]) / 4 lp_image_pixel_8 = (lp_sub_image[32] + lp_sub_image[33] + lp_sub_image[34] + lp_sub_image[35]) / 4 lp_image_pixel_9 = (lp_sub_image[36] + lp_sub_image[37] + lp_sub_image[38] + lp_sub_image[39]) / 4 lp_image_pixel_10 = (lp_sub_image[40] + lp_sub_image[41] + lp_sub_image[42] + lp_sub_image[43]) / 4 lp_image_pixel_11 = (lp_sub_image[44] + lp_sub_image[45] + lp_sub_image[46] + lp_sub_image[47]) / 4 plane_stack = pl_stack.PlaneStack( galaxies=[galaxy_light], grid_stacks=[padded_grid_stack, padded_grid_stack]) assert plane_stack.image_plane_images_for_simulation[0].shape == ( 3, 4) assert (plane_stack.image_plane_images_for_simulation[0][0, 0] == lp_image_pixel_0).all() assert (plane_stack.image_plane_images_for_simulation[0][0, 1] == lp_image_pixel_1).all() assert (plane_stack.image_plane_images_for_simulation[0][0, 2] == lp_image_pixel_2).all() assert (plane_stack.image_plane_images_for_simulation[0][0, 3] == lp_image_pixel_3).all() assert (plane_stack.image_plane_images_for_simulation[0][1, 0] == lp_image_pixel_4).all() assert (plane_stack.image_plane_images_for_simulation[0][1, 1] == lp_image_pixel_5).all() assert (plane_stack.image_plane_images_for_simulation[0][1, 2] == lp_image_pixel_6).all() assert (plane_stack.image_plane_images_for_simulation[0][1, 3] == lp_image_pixel_7).all() assert (plane_stack.image_plane_images_for_simulation[0][2, 0] == lp_image_pixel_8).all() assert (plane_stack.image_plane_images_for_simulation[0][2, 1] == lp_image_pixel_9).all() assert (plane_stack.image_plane_images_for_simulation[0][2, 2] == lp_image_pixel_10).all() assert (plane_stack.image_plane_images_for_simulation[0][2, 3] == lp_image_pixel_11).all() assert plane_stack.image_plane_images_for_simulation[1].shape == ( 3, 4) assert (plane_stack.image_plane_images_for_simulation[1][0, 0] == lp_image_pixel_0).all() assert (plane_stack.image_plane_images_for_simulation[1][0, 1] == lp_image_pixel_1).all() assert (plane_stack.image_plane_images_for_simulation[1][0, 2] == lp_image_pixel_2).all() assert (plane_stack.image_plane_images_for_simulation[1][0, 3] == lp_image_pixel_3).all() assert (plane_stack.image_plane_images_for_simulation[1][1, 0] == lp_image_pixel_4).all() assert (plane_stack.image_plane_images_for_simulation[1][1, 1] == lp_image_pixel_5).all() assert (plane_stack.image_plane_images_for_simulation[1][1, 2] == lp_image_pixel_6).all() assert (plane_stack.image_plane_images_for_simulation[1][1, 3] == lp_image_pixel_7).all() assert (plane_stack.image_plane_images_for_simulation[1][2, 0] == lp_image_pixel_8).all() assert (plane_stack.image_plane_images_for_simulation[1][2, 1] == lp_image_pixel_9).all() assert (plane_stack.image_plane_images_for_simulation[1][2, 2] == lp_image_pixel_10).all() assert (plane_stack.image_plane_images_for_simulation[1][2, 3] == lp_image_pixel_11).all()