def test__different_imaging_without_mock_objects__customize_constructor_inputs( self ): psf = aa.kernel.ones(shape_2d=(7, 7), pixel_scales=3.0) imaging = aa.imaging( image=aa.array.ones(shape_2d=(19, 19), pixel_scales=3.0), psf=psf, noise_map=aa.array.full( fill_value=2.0, shape_2d=(19, 19), pixel_scales=3.0 ), ) mask = aa.mask.unmasked( shape_2d=(19, 19), pixel_scales=1.0, invert=True, sub_size=8 ) mask[9, 9] = False masked_imaging = aa.masked.imaging.manual( imaging=imaging, mask=mask, psf_shape_2d=(7, 7) ) assert (masked_imaging.imaging.image.in_2d == np.ones((19, 19))).all() assert (masked_imaging.imaging.noise_map.in_2d == 2.0 * np.ones((19, 19))).all() assert (masked_imaging.psf.in_2d == np.ones((7, 7))).all() assert masked_imaging.convolver.kernel.shape_2d == (7, 7) assert (masked_imaging.image == np.array([1.0])).all() assert (masked_imaging.noise_map == np.array([2.0])).all()
def test__image_and_model_are_identical__inversion_included__changes_certain_properties( self): mask = aa.mask.manual( mask_2d=np.array([[False, False], [False, False]]), sub_size=1, pixel_scales=(1.0, 1.0), ) data = aa.masked.array.manual_1d(array=np.array([1.0, 2.0, 3.0, 4.0]), mask=mask) noise_map = aa.masked.array.manual_1d(array=np.array( [2.0, 2.0, 2.0, 2.0]), mask=mask) imaging = aa.imaging(image=data, noise_map=noise_map) masked_imaging = aa.masked.imaging(imaging=imaging, mask=mask) model_data = aa.masked.array.manual_1d(array=np.array( [1.0, 2.0, 3.0, 4.0]), mask=mask) inversion = mock_inversion.MockFitInversion( regularization_term=2.0, log_det_curvature_reg_matrix_term=3.0, log_det_regularization_matrix_term=4.0, ) fit = aa.fit(masked_dataset=masked_imaging, model_data=model_data, inversion=inversion) assert fit.chi_squared == 0.0 assert fit.reduced_chi_squared == 0.0 assert fit.noise_normalization == np.sum( np.log(2 * np.pi * noise_map**2.0)) assert fit.likelihood == -0.5 * (fit.chi_squared + fit.noise_normalization) assert fit.likelihood_with_regularization == -0.5 * ( fit.chi_squared + 2.0 + fit.noise_normalization) assert fit.evidence == -0.5 * (fit.chi_squared + 2.0 + 3.0 - 4.0 + fit.noise_normalization) assert fit.figure_of_merit == fit.evidence
def make_imaging_7x7( image_7x7, psf_3x3, noise_map_7x7, background_noise_map_7x7, poisson_noise_map_7x7, exposure_time_map_7x7, background_sky_map_7x7, ): return aa.imaging( image=image_7x7, pixel_scales=image_7x7.pixel_scales, psf=psf_3x3, noise_map=noise_map_7x7, background_noise_map=background_noise_map_7x7, poisson_noise_map=poisson_noise_map_7x7, exposure_time_map=exposure_time_map_7x7, background_sky_map=background_sky_map_7x7, name="mock_imaging_7x7", )
def make_imaging_6x6(): image = aa.array.full(shape_2d=(6, 6), fill_value=1.0) psf = aa.kernel.full(shape_2d=(3, 3), fill_value=1.0) noise_map = aa.array.full(shape_2d=(6, 6), fill_value=2.0) background_noise_map = aa.array.full(shape_2d=(6, 6), fill_value=3.0) poisson_noise_map = aa.array.full(shape_2d=(6, 6), fill_value=4.0) exposure_time_map = aa.array.full(shape_2d=(6, 6), fill_value=5.0) background_sky_map = aa.array.full(shape_2d=(6, 6), fill_value=6.0) return aa.imaging( image=image, pixel_scales=1.0, psf=psf, noise_map=noise_map, background_noise_map=background_noise_map, poisson_noise_map=poisson_noise_map, exposure_time_map=exposure_time_map, background_sky_map=background_sky_map, name="mock_imaging_6x6", )
import autoarray as aa import autoarray.plot as aplt import numpy as np mask = aa.mask.circular(shape_2d=(7, 7), pixel_scales=0.3, radius=0.6) imaging = aa.imaging( image=aa.array.ones(shape_2d=(7, 7), pixel_scales=0.3), noise_map=aa.array.ones(shape_2d=(7, 7), pixel_scales=0.3), psf=aa.kernel.ones(shape_2d=(3, 3), pixel_scales=0.3), ) masked_imaging = aa.masked.imaging(imaging=imaging, mask=mask) grid_7x7 = aa.grid.from_mask(mask=mask) grid_9 = aa.grid.manual_1d( grid=[ [0.6, -0.3], [0.5, -0.8], [0.2, 0.1], [0.0, 0.5], [-0.3, -0.8], [-0.6, -0.5], [-0.4, -1.1], [-1.2, 0.8], [-1.5, 0.9], ], shape_2d=(3, 3), pixel_scales=1.0, )
import autoarray as aa import autoarray.plot as aplt array = aa.array.ones(shape_2d=(31, 31), pixel_scales=(1.0, 1.0), sub_size=1) array[0] = 3.0 kernel = aa.kernel.ones(shape_2d=(3, 3), pixel_scales=(1.0, 1.0)) kernel[0] = 3.0 imaging = aa.imaging(image=array, noise_map=array, psf=kernel) # aplt.imaging.image(imaging=imaging) aplt.imaging.subplot_imaging(imaging=imaging)
def test__15_grid__no_sub_grid(self): mask = np.array([ [True, True, True, True, True, True, True], [True, True, True, True, True, True, True], [True, False, False, False, False, False, True], [True, False, False, False, False, False, True], [True, False, False, False, False, False, True], [True, True, True, True, True, True, True], [True, True, True, True, True, True, True], ]) mask = aa.mask.manual(mask_2d=mask, pixel_scales=1.0, sub_size=1) # There is no sub-grid, so our grid are just the masked_image grid (note the NumPy weighted_data structure # ensures this has no sub-gridding) grid = aa.masked.grid.manual_1d( grid=np.array([ [0.9, -0.9], [1.0, -1.0], [1.1, -1.1], [0.9, 0.9], [1.0, 1.0], [1.1, 1.1], [-0.01, 0.01], [0.0, 0.0], [0.01, 0.01], [-0.9, -0.9], [-1.0, -1.0], [-1.1, -1.1], [-0.9, 0.9], [-1.0, 1.0], [-1.1, 1.1], ]), mask=mask, ) pix = aa.pix.Rectangular(shape=(3, 3)) mapper = pix.mapper_from_grid_and_sparse_grid( grid=grid, sparse_grid=None, inversion_uses_border=False) assert mapper.is_image_plane_pixelization == False assert mapper.pixelization_grid.shape_2d_scaled == pytest.approx( (2.2, 2.2), 1.0e-4) assert mapper.pixelization_grid.origin == pytest.approx((0.0, 0.0), 1.0e-4) assert (mapper.mapping_matrix == np.array([ [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0], ])).all() assert mapper.shape_2d == (3, 3) reg = aa.reg.Constant(coefficient=1.0) regularization_matrix = reg.regularization_matrix_from_mapper( mapper=mapper) assert (regularization_matrix == np.array([ [2.00000001, -1.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0], [-1.0, 3.00000001, -1.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0], [0.0, -1.0, 2.00000001, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0], [-1.0, 0.0, 0.0, 3.00000001, -1.0, 0.0, -1.0, 0.0, 0.0], [0.0, -1.0, 0.0, -1.0, 4.00000001, -1.0, 0.0, -1.0, 0.0], [0.0, 0.0, -1.0, 0.0, -1.0, 3.00000001, 0.0, 0.0, -1.0], [0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 2.00000001, -1.0, 0.0], [0.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0, 3.00000001, -1.0], [0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0, 2.00000001], ])).all() image = aa.array.ones(shape_2d=(7, 7)) noise_map = aa.array.ones(shape_2d=(7, 7)) psf = aa.kernel.no_blur() imaging = aa.imaging(image=image, noise_map=noise_map, psf=psf) masked_data = aa.masked.imaging(imaging=imaging, mask=mask) inversion = aa.inversion(masked_dataset=masked_data, mapper=mapper, regularization=reg) assert ( inversion.blurred_mapping_matrix == mapper.mapping_matrix).all() assert (inversion.regularization_matrix == regularization_matrix).all() assert inversion.mapped_reconstructed_image == pytest.approx( np.ones(15), 1.0e-4)
def test__3x3_simple_grid__include_mask_with_offset_centre(self): mask = aa.mask.manual( mask_2d=np.array([ [True, True, True, True, True, True, True], [True, True, True, True, False, True, True], [True, True, True, False, False, False, 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], ]), pixel_scales=1.0, sub_size=1, ) grid = np.array([[2.0, 1.0], [1.0, 0.0], [1.0, 1.0], [1.0, 2.0], [0.0, 1.0]]) grid = aa.masked.grid.manual_1d(grid=grid, mask=mask) pix = aa.pix.VoronoiMagnification(shape=(3, 3)) sparse_grid = grids.SparseGrid.from_grid_and_unmasked_2d_grid_shape( grid=grid, unmasked_sparse_shape=pix.shape) pixelization_grid = aa.grid_voronoi( grid_1d=sparse_grid.sparse, nearest_pixelization_1d_index_for_mask_1d_index=sparse_grid. sparse_1d_index_for_mask_1d_index, ) mapper = pix.mapper_from_grid_and_sparse_grid( grid=grid, sparse_grid=pixelization_grid, inversion_uses_border=False) assert mapper.is_image_plane_pixelization == True assert mapper.pixelization_grid.shape_2d_scaled == pytest.approx( (2.0, 2.0), 1.0e-4) assert (mapper.pixelization_grid == sparse_grid.sparse).all() # assert mapper.pixelization_grid.origin == pytest.approx((1.0, 1.0), 1.0e-4) assert isinstance(mapper, mappers.MapperVoronoi) 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 = aa.reg.Constant(coefficient=1.0) regularization_matrix = reg.regularization_matrix_from_mapper( mapper=mapper) 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() image = aa.array.ones(shape_2d=(7, 7)) noise_map = aa.array.ones(shape_2d=(7, 7)) psf = aa.kernel.no_blur() imaging = aa.imaging(image=image, noise_map=noise_map, psf=psf) masked_data = aa.masked.imaging(imaging=imaging, mask=mask) inversion = aa.inversion(masked_dataset=masked_data, mapper=mapper, regularization=reg) assert ( inversion.blurred_mapping_matrix == mapper.mapping_matrix).all() assert (inversion.regularization_matrix == regularization_matrix).all() assert inversion.mapped_reconstructed_image == pytest.approx( np.ones(5), 1.0e-4)
def test__5_simple_grid__include_sub_grid(self): mask = np.array([ [True, True, True, True, True, True, True], [True, True, True, True, True, True, True], [True, True, True, False, True, True, True], [True, True, False, False, False, True, True], [True, True, True, False, True, True, True], [True, True, True, True, True, True, True], [True, True, True, True, True, True, True], ]) mask = aa.mask.manual(mask_2d=mask, pixel_scales=2.0, sub_size=2) # Assume a 2x2 sub-grid, so each of our 5 masked_image-pixels are split into 4. # The grid below is unphysical in that the (0.0, 0.0) terms on the end of each sub-grid probably couldn't # happen for a real lens calculation. This is to make a mapping_matrix matrix which explicitly tests the # sub-grid. grid = aa.masked.grid.manual_1d( grid=np.array([ [1.0, -1.0], [1.0, -1.0], [1.0, -1.0], [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, 1.0], [-1.0, 1.0], [-1.0, 1.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], ]), mask=mask, ) pix = aa.pix.Rectangular(shape=(3, 3)) mapper = pix.mapper_from_grid_and_sparse_grid( grid=grid, sparse_grid=None, inversion_uses_border=False) assert mapper.is_image_plane_pixelization == False assert mapper.pixelization_grid.shape_2d_scaled == pytest.approx( (2.0, 2.0), 1.0e-4) assert mapper.pixelization_grid.origin == pytest.approx((0.0, 0.0), 1.0e-4) assert (mapper.mapping_matrix == np.array([ [0.75, 0.0, 0.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.25, 0.0, 0.75], [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0], ])).all() assert mapper.shape_2d == (3, 3) reg = aa.reg.Constant(coefficient=1.0) regularization_matrix = reg.regularization_matrix_from_mapper( mapper=mapper) assert (regularization_matrix == np.array([ [2.00000001, -1.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0], [-1.0, 3.00000001, -1.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0], [0.0, -1.0, 2.00000001, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0], [-1.0, 0.0, 0.0, 3.00000001, -1.0, 0.0, -1.0, 0.0, 0.0], [0.0, -1.0, 0.0, -1.0, 4.00000001, -1.0, 0.0, -1.0, 0.0], [0.0, 0.0, -1.0, 0.0, -1.0, 3.00000001, 0.0, 0.0, -1.0], [0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 2.00000001, -1.0, 0.0], [0.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0, 3.00000001, -1.0], [0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0, 2.00000001], ])).all() image = aa.array.ones(shape_2d=(7, 7)) noise_map = aa.array.ones(shape_2d=(7, 7)) psf = aa.kernel.no_blur() imaging = aa.imaging(image=image, noise_map=noise_map, psf=psf) masked_data = aa.masked.imaging(imaging=imaging, mask=mask) inversion = aa.inversion(masked_dataset=masked_data, mapper=mapper, regularization=reg) assert ( inversion.blurred_mapping_matrix == mapper.mapping_matrix).all() assert (inversion.regularization_matrix == regularization_matrix).all() assert inversion.mapped_reconstructed_image == pytest.approx( np.ones(5), 1.0e-4)
def test__image_and_model_are_identical__no_masking__check_values_are_correct( self): mask = aa.mask.manual( mask_2d=np.array([[False, False], [False, False]]), sub_size=1, pixel_scales=(1.0, 1.0), ) data = aa.masked.array.manual_1d(array=np.array([1.0, 2.0, 3.0, 4.0]), mask=mask) noise_map = aa.masked.array.manual_1d(array=np.array( [2.0, 2.0, 2.0, 2.0]), mask=mask) imaging = aa.imaging(image=data, noise_map=noise_map) masked_imaging = aa.masked.imaging(imaging=imaging, mask=mask) model_data = aa.masked.array.manual_1d(array=np.array( [1.0, 2.0, 3.0, 4.0]), mask=mask) fit = aa.fit(masked_dataset=masked_imaging, model_data=model_data) assert (fit.mask == np.array([[False, False], [False, False]])).all() assert (fit.image.in_1d == np.array([1.0, 2.0, 3.0, 4.0])).all() assert (fit.image.in_2d == np.array([[1.0, 2.0], [3.0, 4.0]])).all() assert (fit.noise_map.in_1d == np.array([2.0, 2.0, 2.0, 2.0])).all() assert (fit.noise_map.in_2d == np.array([[2.0, 2.0], [2.0, 2.0]])).all() assert (fit.signal_to_noise_map.in_1d == np.array([0.5, 1.0, 1.5, 2.0])).all() assert (fit.signal_to_noise_map.in_2d == np.array([[0.5, 1.0], [1.5, 2.0]])).all() assert (fit.model_image.in_1d == np.array([1.0, 2.0, 3.0, 4.0])).all() assert (fit.model_image.in_2d == np.array([[1.0, 2.0], [3.0, 4.0]])).all() assert (fit.residual_map.in_1d == np.array([0.0, 0.0, 0.0, 0.0])).all() assert (fit.residual_map.in_2d == np.array([[0.0, 0.0], [0.0, 0.0]])).all() assert (fit.normalized_residual_map.in_1d == np.array( [0.0, 0.0, 0.0, 0.0])).all() assert (fit.normalized_residual_map.in_2d == np.array([[0.0, 0.0], [0.0, 0.0]])).all() assert (fit.chi_squared_map.in_1d == np.array([0.0, 0.0, 0.0, 0.0])).all() assert (fit.chi_squared_map.in_2d == np.array([[0.0, 0.0], [0.0, 0.0]])).all() assert fit.chi_squared == 0.0 assert fit.reduced_chi_squared == 0.0 assert fit.noise_normalization == np.sum( np.log(2 * np.pi * noise_map**2.0)) assert fit.likelihood == -0.5 * (fit.chi_squared + fit.noise_normalization)
import autoarray as aa import autoarray.plot as aplt grid_7x7 = aa.grid.uniform(shape_2d=(7, 7), pixel_scales=1.0) rectangular_grid = aa.grid_rectangular.overlay_grid(grid=grid_7x7, shape_2d=(3, 3)) rectangular_mapper = aa.mapper(grid=grid_7x7, pixelization_grid=rectangular_grid) image = aa.array.ones(shape_2d=(7, 7), pixel_scales=1.0) image[0:4] = 5.0 noise_map = aa.array.ones(shape_2d=(7, 7), pixel_scales=1.0) imaging = aa.imaging(image=image, noise_map=noise_map) aplt.mapper.subplot_image_and_mapper( image=imaging, mapper=rectangular_mapper, include=aplt.Include( inversion_grid=False, inversion_border=True, inversion_pixelization_grid=False ), image_pixel_indexes=[0, 1, 2, 3], source_pixel_indexes=[[3, 4], [5]], )