def test__multiple_use_methods__raises_exception(self, image_7x7, noise_map_7x7, sub_mask_2d_7x7): gal_data_7x7 = ag.GalaxyData(image=image_7x7, noise_map=noise_map_7x7, pixel_scales=3.0) with pytest.raises(exc.GalaxyException): ag.MaskedGalaxyDataset( galaxy_data=gal_data_7x7, mask=sub_mask_2d_7x7, use_image=True, use_convergence=True, ) with pytest.raises(exc.GalaxyException): ag.MaskedGalaxyDataset( galaxy_data=gal_data_7x7, mask=sub_mask_2d_7x7, use_image=True, use_potential=True, ) with pytest.raises(exc.GalaxyException): ag.MaskedGalaxyDataset( galaxy_data=gal_data_7x7, mask=sub_mask_2d_7x7, use_image=True, use_deflections_y=True, ) with pytest.raises(exc.GalaxyException): ag.MaskedGalaxyDataset( galaxy_data=gal_data_7x7, mask=sub_mask_2d_7x7, use_image=True, use_convergence=True, use_potential=True, ) with pytest.raises(exc.GalaxyException): ag.MaskedGalaxyDataset( galaxy_data=gal_data_7x7, mask=sub_mask_2d_7x7, use_image=True, use_convergence=True, use_potential=True, use_deflections_x=True, )
def test__image(self, gal_data_7x7, sub_mask_2d_7x7): masked_galaxy_dataset = ag.MaskedGalaxyDataset( galaxy_data=gal_data_7x7, mask=sub_mask_2d_7x7, use_image=True) galaxy = ag.Galaxy(redshift=0.5, light=ag.lp.SphSersic(centre=(1.0, 2.0), intensity=1.0)) fit = ag.FitGalaxy(masked_galaxy_dataset=masked_galaxy_dataset, model_galaxies=[galaxy]) assert fit.model_galaxies == [galaxy] model_data = galaxy.image_2d_from_grid(grid=masked_galaxy_dataset.grid) residual_map = ag.util.fit.residual_map_from( data=masked_galaxy_dataset.image, model_data=model_data.binned) assert residual_map == pytest.approx(fit.residual_map, 1e-4) chi_squared_map = ag.util.fit.chi_squared_map_from( residual_map=residual_map, noise_map=masked_galaxy_dataset.noise_map) assert chi_squared_map == pytest.approx(fit.chi_squared_map, 1e-4) chi_squared = ag.util.fit.chi_squared_from( chi_squared_map=chi_squared_map) noise_normalization = ag.util.fit.noise_normalization_from( noise_map=masked_galaxy_dataset.noise_map) log_likelihood = ag.util.fit.log_likelihood_from( chi_squared=chi_squared, noise_normalization=noise_normalization) assert log_likelihood == pytest.approx(fit.log_likelihood, 1e-4)
def test__gal_data_7x7_image(self, gal_data_7x7, sub_mask_2d_7x7): galaxy_fit_data = ag.MaskedGalaxyDataset(galaxy_data=gal_data_7x7, mask=sub_mask_2d_7x7, use_image=True) assert galaxy_fit_data.pixel_scales == (1.0, 1.0) assert (galaxy_fit_data.galaxy_data.image.native == np.ones( (7, 7))).all() assert (galaxy_fit_data.galaxy_data.noise_map.native == 2.0 * np.ones( (7, 7))).all() assert (galaxy_fit_data.image.slim == np.ones(9)).all() assert (galaxy_fit_data.noise_map.slim == 2.0 * np.ones(9)).all() assert (galaxy_fit_data.mask == np.array([ [True, True, True, True, True, True, True], [True, True, True, True, True, True, True], [True, True, False, False, False, True, True], [True, True, False, False, False, True, True], [True, True, False, False, False, True, True], [True, True, True, True, True, True, True], [True, True, True, True, True, True, True], ])).all() assert (galaxy_fit_data.image.native == np.array([ [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, 1.0, 1.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0], [0.0, 0.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], ])).all() assert (galaxy_fit_data.noise_map.native == np.array([ [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, 2.0, 2.0, 2.0, 0.0, 0.0], [0.0, 0.0, 2.0, 2.0, 2.0, 0.0, 0.0], [0.0, 0.0, 2.0, 2.0, 2.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], ])).all() galaxy = mock.MockGalaxy(value=1, shape=36) image = galaxy_fit_data.profile_quantity_from_galaxies( galaxies=[galaxy]) assert (image.binned == np.ones(9)).all() galaxy = ag.Galaxy(redshift=0.5, light=ag.lp.SphSersic(intensity=1.0)) image_gal = galaxy.image_2d_from_grid(grid=galaxy_fit_data.grid) image_gd = galaxy_fit_data.profile_quantity_from_galaxies( galaxies=[galaxy]) assert (image_gal == image_gd).all()
def test__grid( self, gal_data_7x7, sub_mask_2d_7x7, grid_2d_7x7, sub_grid_2d_7x7, blurring_grid_2d_7x7, grid_2d_iterate_7x7, ): galaxy_fit_data = ag.MaskedGalaxyDataset( galaxy_data=gal_data_7x7, mask=sub_mask_2d_7x7, use_image=True, grid_class=ag.Grid2D, ) assert isinstance(galaxy_fit_data.grid, ag.Grid2D) assert (galaxy_fit_data.grid == sub_grid_2d_7x7).all() galaxy_fit_data = ag.MaskedGalaxyDataset( galaxy_data=gal_data_7x7, mask=sub_mask_2d_7x7, use_image=True, grid_class=ag.Grid2DIterate, ) assert isinstance(galaxy_fit_data.grid, ag.Grid2DIterate) assert (galaxy_fit_data.grid.binned == grid_2d_iterate_7x7).all() galaxy_fit_data = ag.MaskedGalaxyDataset( galaxy_data=gal_data_7x7, mask=sub_mask_2d_7x7, use_image=True, grid_class=ag.Grid2DInterpolate, pixel_scales_interp=1.0, ) grid = ag.Grid2DInterpolate.from_mask(mask=sub_mask_2d_7x7, pixel_scales_interp=1.0) assert isinstance(galaxy_fit_data.grid, ag.Grid2DInterpolate) assert (galaxy_fit_data.grid == grid).all() assert (galaxy_fit_data.grid.grid_interp == grid.grid_interp).all() assert (galaxy_fit_data.grid.vtx == grid.vtx).all() assert (galaxy_fit_data.grid.wts == grid.wts).all()
def test__no_use_method__raises_exception(self, image_7x7, noise_map_7x7, sub_mask_2d_7x7): gal_data_7x7 = ag.GalaxyData(image=image_7x7, noise_map=noise_map_7x7, pixel_scales=3.0) with pytest.raises(exc.GalaxyException): ag.MaskedGalaxyDataset(galaxy_data=gal_data_7x7, mask=sub_mask_2d_7x7)
def test__image_noise_map_and_mask(self, gal_data_7x7, sub_mask_2d_7x7): galaxy_fit_data = ag.MaskedGalaxyDataset(galaxy_data=gal_data_7x7, mask=sub_mask_2d_7x7, use_image=True) assert galaxy_fit_data.pixel_scales == (1.0, 1.0) assert (galaxy_fit_data.galaxy_data.image.native == np.ones( (7, 7))).all() assert (galaxy_fit_data.galaxy_data.noise_map.native == 2.0 * np.ones( (7, 7))).all() assert (galaxy_fit_data.image.slim == np.ones(9)).all() assert (galaxy_fit_data.noise_map.slim == 2.0 * np.ones(9)).all() assert (galaxy_fit_data.mask == np.array([ [True, True, True, True, True, True, True], [True, True, True, True, True, True, True], [True, True, False, False, False, True, True], [True, True, False, False, False, True, True], [True, True, False, False, False, True, True], [True, True, True, True, True, True, True], [True, True, True, True, True, True, True], ])).all() assert (galaxy_fit_data.image.native == np.array([ [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, 1.0, 1.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0], [0.0, 0.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], ])).all() assert (galaxy_fit_data.noise_map.native == np.array([ [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, 2.0, 2.0, 2.0, 0.0, 0.0], [0.0, 0.0, 2.0, 2.0, 2.0, 0.0, 0.0], [0.0, 0.0, 2.0, 2.0, 2.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], ])).all()
def test__deflections_y(self, gal_data_7x7, sub_mask_2d_7x7): masked_galaxy_dataset = ag.MaskedGalaxyDataset( galaxy_data=gal_data_7x7, mask=sub_mask_2d_7x7, use_deflections_y=True) galaxy = ag.Galaxy( redshift=0.5, mass=ag.mp.SphIsothermal(centre=(1.0, 2.0), einstein_radius=1.0), ) fit = ag.FitGalaxy(masked_galaxy_dataset=masked_galaxy_dataset, model_galaxies=[galaxy]) assert fit.model_galaxies == [galaxy] model_data = galaxy.deflections_2d_from_grid( grid=masked_galaxy_dataset.grid).binned[:, 0] residual_map = ag.util.fit.residual_map_from( data=masked_galaxy_dataset.image, model_data=model_data) assert residual_map == pytest.approx(fit.residual_map, 1e-4) chi_squared_map = ag.util.fit.chi_squared_map_from( residual_map=residual_map, noise_map=masked_galaxy_dataset.noise_map) assert chi_squared_map == pytest.approx(fit.chi_squared_map, 1e-4) chi_squared = ag.util.fit.chi_squared_from( chi_squared_map=chi_squared_map) noise_normalization = ag.util.fit.noise_normalization_from( noise_map=masked_galaxy_dataset.noise_map) log_likelihood = ag.util.fit.log_likelihood_from( chi_squared=chi_squared, noise_normalization=noise_normalization) assert log_likelihood == pytest.approx(fit.log_likelihood, 1e-4)
def test__1x1_image__light_profile_fits_data_perfectly__lh_is_noise(self): image = ag.Array2D.ones(shape_native=(3, 3), pixel_scales=1.0) noise_map = ag.Array2D.ones(shape_native=(3, 3), pixel_scales=1.0) galaxy_data = ag.GalaxyData(image=image, noise_map=noise_map, pixel_scales=3.0) mask = ag.Mask2D.manual( mask=np.array([[True, True, True], [True, False, True], [True, True, True]]), pixel_scales=1.0, sub_size=1, ) g0 = MockGalaxy(value=1.0) masked_galaxy_dataset = ag.MaskedGalaxyDataset(galaxy_data=galaxy_data, mask=mask, use_image=True) fit = ag.FitGalaxy(masked_galaxy_dataset=masked_galaxy_dataset, model_galaxies=[g0]) assert fit.model_galaxies == [g0] assert fit.log_likelihood == pytest.approx( -0.5 * np.log(2 * np.pi * 1.0), 1.0e-4) masked_galaxy_dataset = ag.MaskedGalaxyDataset(galaxy_data=galaxy_data, mask=mask, use_convergence=True) fit = ag.FitGalaxy(masked_galaxy_dataset=masked_galaxy_dataset, model_galaxies=[g0]) assert fit.model_galaxies == [g0] assert fit.log_likelihood == pytest.approx( -0.5 * np.log(2 * np.pi * 1.0), 1.0e-4) masked_galaxy_dataset = ag.MaskedGalaxyDataset(galaxy_data=galaxy_data, mask=mask, use_potential=True) fit = ag.FitGalaxy(masked_galaxy_dataset=masked_galaxy_dataset, model_galaxies=[g0]) assert fit.model_galaxies == [g0] assert fit.log_likelihood == pytest.approx( -0.5 * np.log(2 * np.pi * 1.0), 1.0e-4) masked_galaxy_dataset = ag.MaskedGalaxyDataset(galaxy_data=galaxy_data, mask=mask, use_deflections_y=True) fit = ag.FitGalaxy(masked_galaxy_dataset=masked_galaxy_dataset, model_galaxies=[g0]) assert fit.model_galaxies == [g0] assert fit.log_likelihood == pytest.approx( -0.5 * np.log(2 * np.pi * 1.0), 1.0e-4) masked_galaxy_dataset = ag.MaskedGalaxyDataset(galaxy_data=galaxy_data, mask=mask, use_deflections_x=True) fit = ag.FitGalaxy(masked_galaxy_dataset=masked_galaxy_dataset, model_galaxies=[g0]) assert fit.model_galaxies == [g0] assert fit.log_likelihood == pytest.approx( -0.5 * np.log(2 * np.pi * 1.0), 1.0e-4)
def test__1x2_image__noise_not_1__alls_correct(self): image = ag.Array2D.full(fill_value=5.0, shape_native=(3, 4), pixel_scales=1.0) image[6] = 4.0 noise_map = ag.Array2D.full(fill_value=2.0, shape_native=(3, 4), pixel_scales=1.0) galaxy_data = ag.GalaxyData(image=image, noise_map=noise_map, pixel_scales=3.0) mask = ag.Mask2D.manual( mask=np.array([ [True, True, True, True], [True, False, False, True], [True, True, True, True], ]), pixel_scales=1.0, sub_size=1, ) g0 = MockGalaxy(value=1.0, shape=2) masked_galaxy_dataset = ag.MaskedGalaxyDataset(galaxy_data=galaxy_data, mask=mask, use_image=True) fit = ag.FitGalaxy(masked_galaxy_dataset=masked_galaxy_dataset, model_galaxies=[g0]) assert fit.model_galaxies == [g0] assert fit.chi_squared == (25.0 / 4.0) assert fit.reduced_chi_squared == (25.0 / 4.0) / 2.0 assert fit.log_likelihood == pytest.approx( -0.5 * ((25.0 / 4.0) + 2.0 * np.log(2 * np.pi * 2.0**2)), 1.0e-4) masked_galaxy_dataset = ag.MaskedGalaxyDataset(galaxy_data=galaxy_data, mask=mask, use_convergence=True) fit = ag.FitGalaxy(masked_galaxy_dataset=masked_galaxy_dataset, model_galaxies=[g0]) assert fit.model_galaxies == [g0] assert fit.chi_squared == (25.0 / 4.0) assert fit.reduced_chi_squared == (25.0 / 4.0) / 2.0 assert fit.log_likelihood == pytest.approx( -0.5 * ((25.0 / 4.0) + 2.0 * np.log(2 * np.pi * 2.0**2)), 1.0e-4) masked_galaxy_dataset = ag.MaskedGalaxyDataset(galaxy_data=galaxy_data, mask=mask, use_potential=True) fit = ag.FitGalaxy(masked_galaxy_dataset=masked_galaxy_dataset, model_galaxies=[g0]) assert fit.model_galaxies == [g0] assert fit.chi_squared == (25.0 / 4.0) assert fit.reduced_chi_squared == (25.0 / 4.0) / 2.0 assert fit.log_likelihood == pytest.approx( -0.5 * ((25.0 / 4.0) + 2.0 * np.log(2 * np.pi * 2.0**2)), 1.0e-4) masked_galaxy_dataset = ag.MaskedGalaxyDataset(galaxy_data=galaxy_data, mask=mask, use_deflections_y=True) fit = ag.FitGalaxy(masked_galaxy_dataset=masked_galaxy_dataset, model_galaxies=[g0]) assert fit.chi_squared == (25.0 / 4.0) assert fit.reduced_chi_squared == (25.0 / 4.0) / 2.0 assert fit.log_likelihood == pytest.approx( -0.5 * ((25.0 / 4.0) + 2.0 * np.log(2 * np.pi * 2.0**2)), 1.0e-4) masked_galaxy_dataset = ag.MaskedGalaxyDataset(galaxy_data=galaxy_data, mask=mask, use_deflections_x=True) fit = ag.FitGalaxy(masked_galaxy_dataset=masked_galaxy_dataset, model_galaxies=[g0]) assert fit.chi_squared == (25.0 / 4.0) assert fit.reduced_chi_squared == (25.0 / 4.0) / 2.0 assert fit.log_likelihood == pytest.approx( -0.5 * ((25.0 / 4.0) + 2.0 * np.log(2 * np.pi * 2.0**2)), 1.0e-4)
def test__gal_data_7x7_deflections_x(self, gal_data_7x7, sub_mask_2d_7x7): galaxy_fit_data = ag.MaskedGalaxyDataset(galaxy_data=gal_data_7x7, mask=sub_mask_2d_7x7, use_deflections_x=True) assert galaxy_fit_data.pixel_scales == (1.0, 1.0) assert (galaxy_fit_data.galaxy_data.image.native == np.ones( (7, 7))).all() assert (galaxy_fit_data.galaxy_data.noise_map.native == 2.0 * np.ones( (7, 7))).all() assert (galaxy_fit_data.image.slim == np.ones(9)).all() assert (galaxy_fit_data.noise_map.slim == 2.0 * np.ones(9)).all() assert (galaxy_fit_data.mask == np.array([ [True, True, True, True, True, True, True], [True, True, True, True, True, True, True], [True, True, False, False, False, True, True], [True, True, False, False, False, True, True], [True, True, False, False, False, True, True], [True, True, True, True, True, True, True], [True, True, True, True, True, True, True], ])).all() assert (galaxy_fit_data.image.native == np.array([ [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, 1.0, 1.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0], [0.0, 0.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], ])).all() assert (galaxy_fit_data.noise_map.native == np.array([ [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, 2.0, 2.0, 2.0, 0.0, 0.0], [0.0, 0.0, 2.0, 2.0, 2.0, 0.0, 0.0], [0.0, 0.0, 2.0, 2.0, 2.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], ])).all() galaxy = mock.MockGalaxy(value=1, shape=36) deflections_x = galaxy_fit_data.profile_quantity_from_galaxies( galaxies=[galaxy]) assert (deflections_x.binned == np.ones(9)).all() galaxy = ag.Galaxy(redshift=0.5, mass=ag.mp.SphIsothermal(einstein_radius=1.0)) deflections_gal = galaxy.deflections_2d_from_grid( grid=galaxy_fit_data.grid) deflections_gal = np.asarray([ np.multiply( deflections_gal.mask.sub_fraction, deflections_gal[:, 0].reshape( -1, deflections_gal.mask.sub_length).sum(axis=1), ), np.multiply( deflections_gal.mask.sub_fraction, deflections_gal[:, 1].reshape( -1, deflections_gal.mask.sub_length).sum(axis=1), ), ]).T deflections_gd = galaxy_fit_data.profile_quantity_from_galaxies( galaxies=[galaxy]) assert (deflections_gal[:, 1] == deflections_gd.binned).all()
def test__gal_data_7x7_potential(self, gal_data_7x7, sub_mask_2d_7x7): galaxy_fit_data = ag.MaskedGalaxyDataset(galaxy_data=gal_data_7x7, mask=sub_mask_2d_7x7, use_potential=True) assert galaxy_fit_data.pixel_scales == (1.0, 1.0) assert (galaxy_fit_data.galaxy_data.image.native == np.ones( (7, 7))).all() assert (galaxy_fit_data.galaxy_data.noise_map.native == 2.0 * np.ones( (7, 7))).all() assert (galaxy_fit_data.image.slim == np.ones(9)).all() assert (galaxy_fit_data.noise_map.slim == 2.0 * np.ones(9)).all() assert (galaxy_fit_data.mask == np.array([ [True, True, True, True, True, True, True], [True, True, True, True, True, True, True], [True, True, False, False, False, True, True], [True, True, False, False, False, True, True], [True, True, False, False, False, True, True], [True, True, True, True, True, True, True], [True, True, True, True, True, True, True], ])).all() assert (galaxy_fit_data.image.native == np.array([ [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, 1.0, 1.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0], [0.0, 0.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], ])).all() assert (galaxy_fit_data.noise_map.native == np.array([ [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, 2.0, 2.0, 2.0, 0.0, 0.0], [0.0, 0.0, 2.0, 2.0, 2.0, 0.0, 0.0], [0.0, 0.0, 2.0, 2.0, 2.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], ])).all() galaxy = mock.MockGalaxy(value=1, shape=36) potential = galaxy_fit_data.profile_quantity_from_galaxies( galaxies=[galaxy]) assert (potential.binned == np.ones(9)).all() galaxy = ag.Galaxy(redshift=0.5, mass=ag.mp.SphIsothermal(einstein_radius=1.0)) potential_gal = galaxy.potential_2d_from_grid( grid=galaxy_fit_data.grid) potential_gd = galaxy_fit_data.profile_quantity_from_galaxies( galaxies=[galaxy]) assert (potential_gal == potential_gd).all()
def make_gal_fit_data_7x7_deflections_x(): return ag.MaskedGalaxyDataset( galaxy_data=make_gal_data_7x7(), mask=make_sub_mask_2d_7x7(), use_deflections_x=True, )
def make_gal_fit_data_7x7_potential(): return ag.MaskedGalaxyDataset(galaxy_data=make_gal_data_7x7(), mask=make_sub_mask_2d_7x7(), use_potential=True)
def make_gal_fit_data_7x7_convergence(): return ag.MaskedGalaxyDataset( galaxy_data=make_gal_data_7x7(), mask=make_sub_mask_2d_7x7(), use_convergence=True, )
def make_gal_fit_data_7x7_image(): return ag.MaskedGalaxyDataset(galaxy_data=make_gal_data_7x7(), mask=make_sub_mask_2d_7x7(), use_image=True)