def make_li_blur_stack(): image_0 = np.array([[0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 1.0, 0.0], [0.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0]]) psf_0 = ccd.PSF(array=(np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]])), pixel_scale=1.0, renormalize=False) ccd_0 = ccd.CCDData(image_0, pixel_scale=1.0, psf=psf_0, noise_map=np.ones((4, 4))) mask_0 = np.array([[True, True, True, True], [True, False, False, True], [True, False, False, True], [True, True, True, True]]) mask_0 = msk.Mask(array=mask_0, pixel_scale=1.0) image_1 = np.array([[0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 1.0, 0.0], [0.0, 1.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0]]) psf_1 = ccd.PSF(array=(np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]])), pixel_scale=1.0, renormalize=False) ccd_1 = ccd.CCDData(image_1, pixel_scale=1.0, psf=psf_1, noise_map=np.ones((4, 4))) mask_1 = np.array([[True, True, True, True], [True, False, False, True], [True, False, False, True], [True, True, True, True]]) mask_1 = msk.Mask(array=mask_1, pixel_scale=1.0) return lds.LensDataStack(ccd_datas=[ccd_0, ccd_1], masks=[mask_0, mask_1], sub_grid_size=1)
def test__same_as_above__but_3x4_image(self): ma = mask.Mask(array=np.array([[True, False, True, True], [False, False, False, False], [True, False, True, True]]), pixel_scale=1.0) regular_grid = grids.RegularGrid.from_mask(mask=ma) adaptive_image_grid = pixelizations.ImagePlanePixelization(shape=(3, 4)) pix_grid = adaptive_image_grid.image_plane_pix_grid_from_regular_grid( regular_grid=regular_grid) assert pix_grid.total_sparse_pixels == 6 assert (pix_grid.sparse_to_unmasked_sparse == np.array( [1, 4, 5, 6, 7, 9])).all() assert (pix_grid.unmasked_sparse_to_sparse == np.array( [0, 0, 1, 1, 1, 2, 3, 4, 5, 5, 5, 5])).all() assert (pix_grid.regular_to_unmasked_sparse == np.array( [1, 4, 5, 6, 7, 9])).all() assert (pix_grid.regular_to_sparse == np.array([0, 1, 2, 3, 4, 5])).all() assert (pix_grid.sparse_grid == np.array([[1.0, -0.5], [0.0, -1.5], [0.0, -0.5], [0.0, 0.5], [0.0, 1.5], [-1.0, -0.5]])).all()
def test__solution_different_values__simple_blurred_mapping_matrix__correct_reconstructed_image(self): matrix_shape = (3,3) msk = mask.Mask(array=np.array([[True, True, True], [False, False, False], [True, True, True]]), pixel_scale=1.0) grid_stack = grids.GridStack.grid_stack_from_mask_sub_grid_size_and_psf_shape(mask=msk, sub_grid_size=1, psf_shape=(1,1)) inv = inversions.Inversion(image_1d=np.ones(9), noise_map_1d=np.ones(9), convolver=MockConvolver(matrix_shape), mapper=MockMapper(matrix_shape, grid_stack), regularization=MockRegularization(matrix_shape)) inv.solution_vector = np.array([1.0, 2.0, 3.0, 4.0]) inv.blurred_mapping_matrix = np.array([[1.0, 1.0, 1.0, 1.0], [1.0, 0.0, 1.0, 1.0], [1.0, 0.0, 0.0, 0.0]]) # # CCD pixel 0 maps to 4 pixs pixxels -> value is 1.0 + 2.0 + 3.0 + 4.0 = 10.0 # # CCD pixel 1 maps to 3 pixs pixxels -> value is 1.0 + 3.0 + 4.0 # # CCD pixel 2 maps to 1 pixs pixxels -> value is 1.0 assert (inv.reconstructed_data_vector == np.array([10.0, 8.0, 1.0])).all() assert (inv.reconstructed_data == np.array([[0.0, 0.0, 0.0], [10.0, 8.0, 1.0], [0.0, 0.0, 0.0]]))
def test__pixelization_regular_grid_from_regular_grid__sets_up_with_correct_shape_and_pixel_scales( self): ma = mask.Mask(array=np.array([[False, False, False], [False, False, False], [False, False, False]]), pixel_scale=1.0) regular_grid = grids.RegularGrid.from_mask(mask=ma) adaptive_image_grid = pixelizations.ImagePlanePixelization(shape=(3, 3)) pix_grid = adaptive_image_grid.image_plane_pix_grid_from_regular_grid( regular_grid=regular_grid) assert pix_grid.shape == (3, 3) assert pix_grid.pixel_scales == (1.0, 1.0) assert pix_grid.total_sparse_pixels == 9 assert (pix_grid.sparse_to_unmasked_sparse == np.array( [0, 1, 2, 3, 4, 5, 6, 7, 8])).all() assert (pix_grid.unmasked_sparse_to_sparse == np.array( [0, 1, 2, 3, 4, 5, 6, 7, 8])).all() assert (pix_grid.regular_to_unmasked_sparse == np.array( [0, 1, 2, 3, 4, 5, 6, 7, 8])).all() assert (pix_grid.regular_to_sparse == np.array( [0, 1, 2, 3, 4, 5, 6, 7, 8])).all() assert (pix_grid.sparse_grid == np.array([[1.0, -1.0], [1.0, 0.0], [1.0, 1.0], [0.0, -1.0], [0.0, 0.0], [0.0, 1.0], [-1.0, -1.0], [-1.0, 0.0], [-1.0, 1.0]])).all() assert pix_grid.regular_grid == pytest.approx(regular_grid, 1e-4)
def make_grid_stack(): ma = mask.Mask(np.array([[True, True, True, True], [True, False, False, True], [True, True, True, True]]), pixel_scale=6.0) grid_stack = grids.GridStack.grid_stack_from_mask_sub_grid_size_and_psf_shape( mask=ma, sub_grid_size=2, psf_shape=(3, 3)) # Manually overwrite a set of cooridnates to make tests of grid_stacks and defledctions straightforward grid_stack.regular[0] = np.array([1.0, 1.0]) grid_stack.regular[1] = np.array([1.0, 0.0]) grid_stack.sub[0] = np.array([1.0, 1.0]) grid_stack.sub[1] = np.array([1.0, 0.0]) grid_stack.sub[2] = np.array([1.0, 1.0]) grid_stack.sub[3] = np.array([1.0, 0.0]) grid_stack.sub[4] = np.array([-1.0, 2.0]) grid_stack.sub[5] = np.array([-1.0, 4.0]) grid_stack.sub[6] = np.array([1.0, 2.0]) grid_stack.sub[7] = np.array([1.0, 4.0]) grid_stack.blurring[0] = np.array([1.0, 0.0]) grid_stack.blurring[1] = np.array([-6.0, -3.0]) grid_stack.blurring[2] = np.array([-6.0, 3.0]) grid_stack.blurring[3] = np.array([-6.0, 9.0]) grid_stack.blurring[4] = np.array([0.0, -9.0]) grid_stack.blurring[5] = np.array([0.0, 9.0]) grid_stack.blurring[6] = np.array([6.0, -9.0]) grid_stack.blurring[7] = np.array([6.0, -3.0]) grid_stack.blurring[8] = np.array([6.0, 3.0]) grid_stack.blurring[9] = np.array([6.0, 9.0]) return grid_stack
def test___3x3_padded_image__one_psf_blurs__other_asymmetric(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) unmasked_image_1d_1 = np.zeros(25) unmasked_image_1d_1[12] = 1.0 unmasked_blurred_images_of_datas = \ stack_util.unmasked_blurred_image_of_datas_from_padded_grid_stacks_psfs_and_unmasked_images( padded_grid_stacks=[padded_grid_stack, padded_grid_stack], psfs=[psf_0, psf_1], unmasked_images_1d=[np.ones(25), unmasked_image_1d_1]) assert (unmasked_blurred_images_of_datas[0] == np.ones((3, 3))).all() assert (unmasked_blurred_images_of_datas[1] == np.array( [[0.0, 3.0, 0.0], [0.0, 1.0, 2.0], [0.0, 0.0, 0.0]])).all()
def grid_stack_for_simulation(cls, shape, pixel_scale, psf_shape, sub_grid_size=2): """Setup a grid-stack of grid_stack for simulating an image of a strong lens, whereby the grid's use padded-grid_stack \ to ensure that the PSF blurring in the simulation routine (*ccd.PrepatoryImage.simulate*) is not degraded \ due to edge effects. Parameters ----------- shape : (int, int) The 2D shape of the array, where all pixels are used to generate the grid-stack's grid_stack. pixel_scale : float The size of each pixel in arc seconds. psf_shape : (int, int) The shape of the PSF used in the analysis, which defines how much the grid's must be masked to mitigate \ edge effects. sub_grid_size : int The size of a sub-pixel's sub-grid (sub_grid_size x sub_grid_size). """ return cls.padded_grid_stack_from_mask_sub_grid_size_and_psf_shape( mask=msk.Mask(array=np.full(shape, False), pixel_scale=pixel_scale), sub_grid_size=sub_grid_size, psf_shape=psf_shape)
def test__setup_pixelization__galaxy_has_pixelization__returns_grids_with_pix_grid( self): ma = mask.Mask(np.array([[False, False, False], [False, False, False], [False, True, False]]), pixel_scale=1.0) grid_stack = grids.GridStack.grid_stack_from_mask_sub_grid_size_and_psf_shape( mask=ma, sub_grid_size=1, psf_shape=(1, 1)) galaxy = g.Galaxy( pixelization=pixelizations.AdaptiveMagnification(shape=(3, 3)), regularization=regularization.Constant()) image_plane_pix_grids = \ pixelizations.setup_image_plane_pixelization_grid_from_galaxies_and_grid_stack(galaxies=[galaxy, galaxy], grid_stack=grid_stack) assert (image_plane_pix_grids.regular == grid_stack.regular).all() assert (image_plane_pix_grids.sub == grid_stack.sub).all() assert (image_plane_pix_grids.blurring == grid_stack.blurring).all() assert image_plane_pix_grids.pix == pytest.approx( np.array([[1.0, -1.0], [1.0, 0.0], [1.0, 1.0], [0.0, -1.0], [0.0, 0.0], [0.0, 1.0], [-1.0, -1.0], [-1.0, 1.0]]), 1.0e-4)
def test__same_as_above__but_remove_some_centre_pixels_and_change_order__order_does_not_change_mapping( self): ma = mask.Mask(array=np.array([[False, False, False], [False, False, False], [False, False, False]]), pixel_scale=1.0) unmasked_sparse_grid_pixel_centres = np.array([[0, 0], [0, 1], [2, 2], [1, 1], [0, 2], [2, 0], [0, 2]]) total_masked_pixels = mask_util.total_sparse_pixels_from_mask( mask=ma, unmasked_sparse_grid_pixel_centres= unmasked_sparse_grid_pixel_centres) sparse_to_unmasked_sparse = mapping_util.sparse_to_unmasked_sparse_from_mask_and_pixel_centres( total_sparse_pixels=total_masked_pixels, mask=ma, unmasked_sparse_grid_pixel_centres= unmasked_sparse_grid_pixel_centres) assert (sparse_to_unmasked_sparse == np.array([0, 1, 2, 3, 4, 5, 6])).all()
def test__1x2_image__tracing_fits_data_with_chi_sq_5_and_chi_sq_4( self): psf_0 = ccd.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) ccd_0 = ccd.CCDData(5.0 * np.ones((3, 4)), pixel_scale=1.0, psf=psf_0, noise_map=np.ones((3, 4))) ccd_0.image[1, 2] = 4.0 mask_0 = msk.Mask(array=np.array([[True, True, True, True], [True, False, False, True], [True, True, True, True]]), pixel_scale=1.0) psf_1 = ccd.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) ccd_1 = ccd.CCDData(5.0 * np.ones((3, 4)), pixel_scale=1.0, psf=psf_1, noise_map=np.ones((3, 4))) mask_1 = msk.Mask(array=np.array([[True, True, True, True], [True, False, False, True], [True, True, True, True]]), pixel_scale=1.0) li_stack = lds.LensDataStack(ccd_datas=[ccd_0, ccd_1], masks=[mask_0, mask_1], sub_grid_size=1) # Setup as a ray trace instance, using a light profile for the lens g0 = g.Galaxy(light_profile=MockLightProfile(value=1.0, size=2)) tracer = ray_tracing_stack.TracerImagePlaneStack( lens_galaxies=[g0], image_plane_grid_stacks=li_stack.grid_stacks) fit = lens_fit_stack.LensProfileFitStack(lens_data_stack=li_stack, tracer=tracer) assert fit.chi_squared == 25.0 + 32.0 assert fit.reduced_chi_squared == (25.0 + 32.0) / 2.0 assert fit.likelihood == -0.5 * ( (25.0 + 2.0 * np.log(2 * np.pi * 1.0)) + (32.0 + 2.0 * np.log(2 * np.pi * 1.0)))
def test__3x3_simple_grid__include_mask__create_using_regular_grid(self): regular_grid = np.array([[1.0, 0.0], [0.0, -1.0], [0.0, 0.0], [0.0, 1.0], [-1.0, 0.0]]) mask = msk.Mask(array=np.array([[True, False, True], [False, False, False], [True, False, True]]), pixel_scale=1.0) sub_grid = np.array([[1.0, 0.0], [0.0, -1.0], [0.0, 0.0], [0.0, 1.0], [-1.0, 0.0]]) sub_to_regular = np.array([0, 1, 2, 3, 4]) regular_grid = grids.RegularGrid(arr=regular_grid, mask=mask) sub_grid = MockSubGrid(sub_grid, sub_to_regular, sub_grid_size=1) pix = pixelizations.AdaptiveMagnification(shape=(3, 3)) image_plane_pix = pix.image_plane_pix_grid_from_regular_grid( regular_grid=regular_grid) grid_stack = MockGridStack( regular=regular_grid, sub=sub_grid, pix=image_plane_pix.sparse_grid, regular_to_nearest_pix=image_plane_pix.regular_to_sparse) mapper = pix.mapper_from_grid_stack_and_border(grid_stack=grid_stack, border=None) assert mapper.is_image_plane_pixelization == True assert mapper.geometry.shape_arc_seconds == pytest.approx((2.0, 2.0), 1.0e-4) assert (mapper.geometry.pixel_centres == image_plane_pix.sparse_grid ).all() assert mapper.geometry.origin == pytest.approx((0.0, 0.0), 1.0e-4) assert isinstance(mapper, pm.VoronoiMapper) assert (mapper.mapping_matrix == np.array([[1.0, 0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 0.0, 1.0]])).all() reg = regularization.Constant(coefficients=(1.0, )) regularization_matrix = reg.regularization_matrix_from_pixel_neighbors( mapper.geometry.pixel_neighbors, mapper.geometry.pixel_neighbors_size) assert (regularization_matrix == np.array( [[3.00000001, -1.0, -1.0, -1.0, 0.0], [-1.0, 3.00000001, -1.0, 0.0, -1.0], [-1.0, -1.0, 4.00000001, -1.0, -1.0], [-1.0, 0.0, -1.0, 3.00000001, -1.0], [0.0, -1.0, -1.0, -1.0, 3.00000001]])).all()
def test___same_as_above__use_galaxies_to_make_plane_images(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)) tracer = ray_tracing_stack.TracerImagePlaneStack( lens_galaxies=[g0, g1], image_plane_grid_stacks=[padded_grid_stack, padded_grid_stack]) manual_blurred_image_00 = tracer.image_plane.image_plane_images_1d_of_galaxies[ 0][0] manual_blurred_image_00 = padded_grid_stack.regular.map_to_2d_keep_padded( padded_array_1d=manual_blurred_image_00) manual_blurred_image_00 = psf_0.convolve(array=manual_blurred_image_00) manual_blurred_image_01 = tracer.image_plane.image_plane_images_1d_of_galaxies[ 0][1] manual_blurred_image_01 = padded_grid_stack.regular.map_to_2d_keep_padded( padded_array_1d=manual_blurred_image_01) manual_blurred_image_01 = psf_0.convolve(array=manual_blurred_image_01) manual_blurred_image_10 = tracer.image_plane.image_plane_images_1d_of_galaxies[ 1][0] manual_blurred_image_10 = padded_grid_stack.regular.map_to_2d_keep_padded( padded_array_1d=manual_blurred_image_10) manual_blurred_image_10 = psf_1.convolve(array=manual_blurred_image_10) manual_blurred_image_11 = tracer.image_plane.image_plane_images_1d_of_galaxies[ 1][1] manual_blurred_image_11 = padded_grid_stack.regular.map_to_2d_keep_padded( padded_array_1d=manual_blurred_image_11) manual_blurred_image_11 = psf_1.convolve(array=manual_blurred_image_11) unmasked_blurred_image_of_datas_and_planes = \ stack_util.unmasked_blurred_image_of_datas_and_planes_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_and_planes[0][0] == \ pytest.approx(manual_blurred_image_00[1:4, 1:4] + manual_blurred_image_01[1:4, 1:4], 1.0e-4) assert unmasked_blurred_image_of_datas_and_planes[1][0] == \ pytest.approx(manual_blurred_image_10[1:4, 1:4] + manual_blurred_image_11[1:4, 1:4], 1.0e-4)
def test__grid_to_pixel__compare_to_array_utill(self): mask = np.array([[True, True, True], [True, False, False], [True, True, False]]) mask = msk.Mask(mask, pixel_scale=7.0) grid_to_pixel_util = util.masked_grid_1d_index_to_2d_pixel_index_from_mask( mask) assert mask.masked_grid_index_to_pixel == pytest.approx( grid_to_pixel_util, 1e-4)
def test__mask_extract_region__uses_the_limits_of_the_mask(self): mask = msk.Mask(array=np.array([[True, True, True, True], [True, False, False, True], [True, False, False, True], [True, True, True, True]]), pixel_scale=1.0) assert mask.extraction_region == [1, 3, 1, 3] mask = msk.Mask(array=np.array([[True, True, True, True], [True, False, False, True], [True, False, False, False], [True, True, True, True]]), pixel_scale=1.0) assert mask.extraction_region == [1, 3, 1, 4] mask = msk.Mask(array=np.array([[True, True, True, True], [True, False, False, True], [True, False, False, True], [True, True, False, True]]), pixel_scale=1.0) assert mask.extraction_region == [1, 4, 1, 3] mask = msk.Mask(array=np.array([[True, True, True, True], [True, False, False, True], [False, False, False, True], [True, True, True, True]]), pixel_scale=1.0) assert mask.extraction_region == [1, 3, 0, 3] mask = msk.Mask(array=np.array([[True, False, True, True], [True, False, False, True], [True, False, False, True], [True, True, True, True]]), pixel_scale=1.0) assert mask.extraction_region == [0, 3, 1, 3]
def test__array_finalize__masks_pass_attributes(self): mask = np.array([[True, True, True, True], [True, False, False, True], [True, True, True, True]]) mask = msk.Mask(mask, pixel_scale=1) mask_new = mask + mask assert mask_new.pixel_scale == 1.0 assert mask_new.origin == (0.0, 0.0) assert mask_new.centre == (0.0, 0.0)
def make_li_manual_stack(): image_0 = np.array([[0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 3.0, 0.0], [0.0, 4.0, 5.0, 6.0, 0.0], [0.0, 7.0, 8.0, 9.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0]]) psf_0 = ccd.PSF(array=(np.array([[1.0, 5.0, 9.0], [2.0, 5.0, 1.0], [3.0, 4.0, 0.0]])), pixel_scale=1.0) ccd_0 = ccd.CCDData(image_0, pixel_scale=1.0, psf=psf_0, noise_map=np.ones((5, 5))) mask_0 = msk.Mask(array=np.array([[True, True, True, True, True], [True, False, False, False, True], [True, False, False, False, True], [True, False, False, False, True], [True, True, True, True, True]]), pixel_scale=1.0) image_1 = np.array([[0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 3.0, 0.0], [0.0, 4.0, 6.0, 6.0, 0.0], [0.0, 7.0, 8.0, 9.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0]]) psf_1 = ccd.PSF(array=(np.array([[1.0, 1.0, 1.0], [2.0, 1.0, 1.0], [3.0, 1.0, 0.0]])), pixel_scale=1.0) ccd_1 = ccd.CCDData(image_1, pixel_scale=1.0, psf=psf_1, noise_map=np.ones((5, 5))) mask_1 = msk.Mask(array=np.array([[True, True, True, True, True], [True, False, False, False, True], [True, False, False, False, True], [True, False, False, True, True], [True, True, True, True, True]]), pixel_scale=1.0) return lds.LensDataStack(ccd_datas=[ccd_0, ccd_1], masks=[mask_0, mask_1], sub_grid_size=1)
def test__image__tracing_fits_data_perfectly__no_psf_blurring__lh_is_noise_normalization( self): psf_0 = ccd.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) ccd_0 = ccd.CCDData(np.ones((3, 3)), pixel_scale=1.0, psf=psf_0, noise_map=np.ones((3, 3))) mask_0 = msk.Mask(array=np.array([[True, True, True], [True, False, True], [True, True, True]]), pixel_scale=1.0) psf_1 = ccd.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) ccd_1 = ccd.CCDData(np.ones((3, 3)), pixel_scale=1.0, psf=psf_1, noise_map=np.ones((3, 3))) mask_1 = msk.Mask(array=np.array([[True, True, True], [True, False, True], [True, True, True]]), pixel_scale=1.0) li_stack = lds.LensDataStack(ccd_datas=[ccd_0, ccd_1], masks=[mask_0, mask_1], sub_grid_size=1) g0 = g.Galaxy(light_profile=MockLightProfile(value=1.0)) tracer = ray_tracing_stack.TracerImagePlaneStack( lens_galaxies=[g0], image_plane_grid_stacks=li_stack.grid_stacks) fit = lens_fit_stack.LensProfileFitStack(lens_data_stack=li_stack, tracer=tracer) assert fit.likelihood == 2.0 * -0.5 * np.log(2 * np.pi * 1.0)
def test__edge_image_pixels__compare_to_array_util(self): mask = np.array([[True, True, True, True, True, True, True], [True, True, True, True, True, True, True], [True, True, True, True, True, True, True], [True, True, True, False, True, True, True], [True, True, True, True, True, True, True], [True, True, True, True, True, True, True], [True, True, True, True, True, True, True]]) edge_pixels_util = util.edge_pixels_from_mask(mask) mask = msk.Mask(mask, pixel_scale=3.0) assert mask.edge_pixels == pytest.approx(edge_pixels_util, 1e-4)
def test__map_2d_array_to_masked_1d_array__compare_to_array_util(self): array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) mask = np.array([[True, False, True], [False, False, False], [True, False, True], [True, True, True]]) array_1d_util = mapping_util.map_2d_array_to_masked_1d_array_from_array_2d_and_mask( mask, array_2d) mask = msk.Mask(mask, pixel_scale=3.0) array_1d = mask.map_2d_array_to_masked_1d_array(array_2d) assert (array_1d == array_1d_util).all()
def test__constructor(self): mask = np.array([[True, True, True, True], [True, False, False, True], [True, True, True, True]]) mask = msk.Mask(mask, pixel_scale=1) assert (mask == np.array([[True, True, True, True], [True, False, False, True], [True, True, True, True]])).all() assert mask.pixel_scale == 1.0 assert mask.central_pixel_coordinates == (1.0, 1.5) assert mask.shape == (3, 4) assert mask.shape_arc_seconds == (3.0, 4.0)
def test__setup_pixelization__galaxies_have_no_pixelization__returns_normal_grids( self): ma = mask.Mask(np.array([[False, False, False], [False, False, False], [False, False, False]]), pixel_scale=1.0) grid_stack = grids.GridStack.grid_stack_from_mask_sub_grid_size_and_psf_shape( mask=ma, sub_grid_size=1, psf_shape=(1, 1)) galaxy = g.Galaxy() image_plane_pix_grids = \ pixelizations.setup_image_plane_pixelization_grid_from_galaxies_and_grid_stack(galaxies=[galaxy, galaxy], grid_stack=grid_stack) assert image_plane_pix_grids == grid_stack
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__same_as_above__different_mask_and_centres(self): ma = mask.Mask(array=np.array([[False, False, True], [False, False, False], [True, False, False]]), pixel_scale=1.0) unmasked_sparse_grid_pixel_centres = np.array([[0, 0], [0, 1], [0, 2], [0, 2], [0, 2], [1, 1]]) unmasked_sparse_to_sparse = mapping_util.unmasked_sparse_to_sparse_from_mask_and_pixel_centres( mask=ma, unmasked_sparse_grid_pixel_centres= unmasked_sparse_grid_pixel_centres, total_sparse_pixels=4) assert (unmasked_sparse_to_sparse == np.array([0, 1, 2, 2, 2, 2])).all()
def test__blurring_mask_for_psf_shape__compare_to_array_util(self): mask = np.array([[True, True, True, True, True, True, True, True], [True, False, True, True, True, False, True, True], [True, True, True, True, True, True, True, True], [True, True, True, True, True, True, True, True], [True, True, True, True, True, True, True, True], [True, False, True, True, True, False, True, True], [True, True, True, True, True, True, True, True], [True, True, True, True, True, True, True, True], [True, True, True, True, True, True, True, True]]) blurring_mask_util = util.mask_blurring_from_mask_and_psf_shape( mask=mask, psf_shape=(3, 3)) mask = msk.Mask(mask, pixel_scale=1.0) blurring_mask = mask.blurring_mask_for_psf_shape(psf_shape=(3, 3)) assert (blurring_mask == blurring_mask_util).all()
def test__mask_is_cross__some_pix_pixels_are_masked__omitted_from_mapping( self): ma = mask.Mask(array=np.array([[True, False, True], [False, False, False], [True, False, True]]), pixel_scale=1.0) unmasked_sparse_grid_pixel_centres = np.array \ ([[0 ,0], [0 ,1], [0 ,2], [1 ,0], [1 ,1], [1 ,2], [2 ,0], [2 ,1], [2 ,2]]) unmasked_sparse_to_sparse = mapping_util.unmasked_sparse_to_sparse_from_mask_and_pixel_centres( mask=ma, unmasked_sparse_grid_pixel_centres= unmasked_sparse_grid_pixel_centres, total_sparse_pixels=5) assert (unmasked_sparse_to_sparse == np.array( [0, 0, 1, 1, 2, 3, 4, 4, 4])).all()
def test__setup_pixelization__galaxy_has_pixelization__but_grid_is_padded_grid__returns_normal_grids( self): ma = mask.Mask(np.array([[False, False, False], [False, False, False], [False, True, False]]), pixel_scale=1.0) grid_stack = grids.GridStack.padded_grid_stack_from_mask_sub_grid_size_and_psf_shape( mask=ma, sub_grid_size=1, psf_shape=(1, 1)) galaxy = g.Galaxy( pixelization=pixelizations.AdaptiveMagnification(shape=(3, 3)), regularization=regularization.Constant()) image_plane_pix_grids = \ pixelizations.setup_image_plane_pixelization_grid_from_galaxies_and_grid_stack(galaxies=[galaxy, galaxy], grid_stack=grid_stack) assert image_plane_pix_grids == grid_stack
def test__mask_full_false__image_mask_and_pixel_centres_fully_overlap__each_pix_maps_to_unmaked_pix( self): ma = mask.Mask(array=np.array([[False, False, False], [False, False, False], [False, False, False]]), pixel_scale=1.0) unmasked_sparse_grid_pixel_centres = np.array \ ([[0 ,0], [0 ,1], [0 ,2], [1 ,0], [1 ,1], [1 ,2], [2 ,0], [2 ,1], [2 ,2]]) unmasked_sparse_to_sparse = mapping_util.unmasked_sparse_to_sparse_from_mask_and_pixel_centres( mask=ma, unmasked_sparse_grid_pixel_centres= unmasked_sparse_grid_pixel_centres, total_sparse_pixels=9) assert (unmasked_sparse_to_sparse == np.array( [0, 1, 2, 3, 4, 5, 6, 7, 8])).all()
def make_lens_data_blur(): psf = ccd.PSF(array=(np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]])), pixel_scale=1.0, renormalize=False) im = ccd.CCDData(image=5.0 * np.ones((6, 6)), pixel_scale=1.0, psf=psf, noise_map=np.ones((6, 6)), exposure_time_map=3.0 * np.ones((6, 6)), background_sky_map=4.0 * np.ones((6, 6))) ma = np.array([[True, True, True, True, True, True], [True, False, False, False, False, True], [True, False, False, False, False, True], [True, False, False, False, False, True], [True, False, False, False, False, True], [True, True, True, True, True, True]]) ma = mask.Mask(array=ma, pixel_scale=1.0) return ld.LensData(ccd_data=im, mask=ma, sub_grid_size=2)
def test__same_as_above__but_3x4_mask(self): ma = mask.Mask(array=np.array([[True, True, False, True], [False, False, False, False], [True, True, False, True]]), pixel_scale=1.0) unmasked_sparse_grid_pixel_centres = np.array([[0, 0], [0, 1], [0, 2], [0, 2], [0, 2], [1, 1], [2, 3], [2, 2]]) total_masked_pixels = mask_util.total_sparse_pixels_from_mask( mask=ma, unmasked_sparse_grid_pixel_centres= unmasked_sparse_grid_pixel_centres) sparse_to_unmasked_sparse = mapping_util.sparse_to_unmasked_sparse_from_mask_and_pixel_centres( total_sparse_pixels=total_masked_pixels, mask=ma, unmasked_sparse_grid_pixel_centres= unmasked_sparse_grid_pixel_centres) assert (sparse_to_unmasked_sparse == np.array([2, 3, 4, 5, 7])).all()
def test__pixelization_regular_grid_from_regular_grid__offset_mask__origin_shift_corrects( self): ma = mask.Mask(array=np.array([[True, True, False, False, False], [True, True, False, False, False], [True, True, False, False, False], [True, True, True, True, True], [True, True, True, True, True]]), pixel_scale=1.0, centre=(1.0, 1.0)) regular_grid = grids.RegularGrid.from_mask(mask=ma) adaptive_image_grid = pixelizations.ImagePlanePixelization(shape=(3, 3)) pix_grid = adaptive_image_grid.image_plane_pix_grid_from_regular_grid( regular_grid=regular_grid) assert pix_grid.shape == (3, 3) assert pix_grid.pixel_scales == (1.0, 1.0) assert pix_grid.total_sparse_pixels == 9 assert (pix_grid.sparse_to_unmasked_sparse == np.array( [0, 1, 2, 3, 4, 5, 6, 7, 8])).all() assert (pix_grid.unmasked_sparse_to_sparse == np.array( [0, 1, 2, 3, 4, 5, 6, 7, 8])).all() assert (pix_grid.regular_to_unmasked_sparse == np.array( [0, 1, 2, 3, 4, 5, 6, 7, 8])).all() assert (pix_grid.regular_to_sparse == np.array( [0, 1, 2, 3, 4, 5, 6, 7, 8])).all() assert (pix_grid.sparse_grid == np.array([[2.0, 0.0], [2.0, 1.0], [2.0, 2.0], [1.0, 0.0], [1.0, 1.0], [1.0, 2.0], [0.0, 0.0], [0.0, 1.0], [0.0, 2.0]])).all() assert pix_grid.regular_grid == pytest.approx(regular_grid, 1e-4)