def test__1x1_image__light_profile_fits_data_perfectly__lh_is_noise(self): image = al.ScaledSquarePixelArray(array=np.ones((3, 3)), pixel_scale=1.0) noise_map = al.ScaledSquarePixelArray(array=np.ones((3, 3)), pixel_scale=1.0) galaxy_data = al.GalaxyData(image=image, noise_map=noise_map, pixel_scale=3.0) mask = al.Mask( array=np.array([[True, True, True], [True, False, True], [True, True, True]]), pixel_scale=1.0, sub_size=1, ) g0 = MockGalaxy(value=1.0) galaxy_fit_data = al.GalaxyFitData(galaxy_data=galaxy_data, mask=mask, use_image=True) fit = al.GalaxyFit(galaxy_data=galaxy_fit_data, model_galaxies=[g0]) assert fit.model_galaxies == [g0] assert fit.likelihood == -0.5 * np.log(2 * np.pi * 1.0) galaxy_fit_data = al.GalaxyFitData(galaxy_data=galaxy_data, mask=mask, use_convergence=True) fit = al.GalaxyFit(galaxy_data=galaxy_fit_data, model_galaxies=[g0]) assert fit.model_galaxies == [g0] assert fit.likelihood == -0.5 * np.log(2 * np.pi * 1.0) galaxy_fit_data = al.GalaxyFitData(galaxy_data=galaxy_data, mask=mask, use_potential=True) fit = al.GalaxyFit(galaxy_data=galaxy_fit_data, model_galaxies=[g0]) assert fit.model_galaxies == [g0] assert fit.likelihood == -0.5 * np.log(2 * np.pi * 1.0) galaxy_fit_data = al.GalaxyFitData(galaxy_data=galaxy_data, mask=mask, use_deflections_y=True) fit = al.GalaxyFit(galaxy_data=galaxy_fit_data, model_galaxies=[g0]) assert fit.model_galaxies == [g0] assert fit.likelihood == -0.5 * np.log(2 * np.pi * 1.0) galaxy_fit_data = al.GalaxyFitData(galaxy_data=galaxy_data, mask=mask, use_deflections_x=True) fit = al.GalaxyFit(galaxy_data=galaxy_fit_data, model_galaxies=[g0]) assert fit.model_galaxies == [g0] assert fit.likelihood == -0.5 * np.log(2 * np.pi * 1.0)
def test__multiple_use_methods__raises_exception(self, image_7x7, sub_mask_7x7): gal_data_7x7 = al.GalaxyData(image=image_7x7, noise_map=2.0 * np.ones((7, 7)), pixel_scale=3.0) with pytest.raises(exc.GalaxyException): al.GalaxyFitData( galaxy_data=gal_data_7x7, mask=sub_mask_7x7, use_image=True, use_convergence=True, ) with pytest.raises(exc.GalaxyException): al.GalaxyFitData( galaxy_data=gal_data_7x7, mask=sub_mask_7x7, use_image=True, use_potential=True, ) with pytest.raises(exc.GalaxyException): al.GalaxyFitData( galaxy_data=gal_data_7x7, mask=sub_mask_7x7, use_image=True, use_deflections_y=True, ) with pytest.raises(exc.GalaxyException): al.GalaxyFitData( galaxy_data=gal_data_7x7, mask=sub_mask_7x7, use_image=True, use_convergence=True, use_potential=True, ) with pytest.raises(exc.GalaxyException): al.GalaxyFitData( galaxy_data=gal_data_7x7, mask=sub_mask_7x7, use_image=True, use_convergence=True, use_potential=True, use_deflections_x=True, )
def test__grid(self, gal_data_7x7, sub_mask_7x7, sub_grid_7x7): galaxy_fit_data = al.GalaxyFitData(galaxy_data=gal_data_7x7, mask=sub_mask_7x7, use_image=True) assert (galaxy_fit_data.grid == sub_grid_7x7).all()
def test__no_use_method__raises_exception(self, image_7x7, sub_mask_7x7): gal_data_7x7 = al.GalaxyData(image=image_7x7, noise_map=2.0 * np.ones((7, 7)), pixel_scale=3.0) with pytest.raises(exc.GalaxyException): al.GalaxyFitData(galaxy_data=gal_data_7x7, mask=sub_mask_7x7)
def test__deflections_y(self, gal_data_7x7, sub_mask_7x7): galaxy_fit_data = al.GalaxyFitData(galaxy_data=gal_data_7x7, mask=sub_mask_7x7, use_deflections_y=True) galaxy = al.Galaxy( redshift=0.5, mass=al.mass_profiles.SphericalIsothermal(centre=(1.0, 2.0), einstein_radius=1.0), ) fit = al.GalaxyFit(galaxy_data=galaxy_fit_data, model_galaxies=[galaxy]) assert fit.model_galaxies == [galaxy] model_data_2d = galaxy.deflections_from_grid( grid=galaxy_fit_data.grid, return_in_2d=True, return_binned=True, bypass_decorator=False, )[:, :, 0] residual_map_2d = af.fit_util.residual_map_from_data_mask_and_model_data( data=galaxy_fit_data.image(return_in_2d=True), mask=galaxy_fit_data.mask, model_data=model_data_2d, ) assert residual_map_2d == pytest.approx( fit.residual_map(return_in_2d=True), 1e-4) chi_squared_map_2d = af.fit_util.chi_squared_map_from_residual_map_noise_map_and_mask( residual_map=residual_map_2d, mask=galaxy_fit_data.mask, noise_map=galaxy_fit_data.noise_map(return_in_2d=True), ) assert chi_squared_map_2d == pytest.approx( fit.chi_squared_map(return_in_2d=True), 1e-4) chi_squared = af.fit_util.chi_squared_from_chi_squared_map_and_mask( chi_squared_map=chi_squared_map_2d, mask=sub_mask_7x7) noise_normalization = af.fit_util.noise_normalization_from_noise_map_and_mask( mask=galaxy_fit_data.mask, noise_map=galaxy_fit_data.noise_map(return_in_2d=True), ) likelihood = af.fit_util.likelihood_from_chi_squared_and_noise_normalization( chi_squared=chi_squared, noise_normalization=noise_normalization) assert likelihood == pytest.approx(fit.likelihood, 1e-4)
def test__image_noise_map_and_mask(self, gal_data_7x7, sub_mask_7x7): galaxy_fit_data = al.GalaxyFitData(galaxy_data=gal_data_7x7, mask=sub_mask_7x7, use_image=True) assert galaxy_fit_data.pixel_scale == 1.0 assert (galaxy_fit_data.image(return_in_2d=True, return_masked=False) == np.ones( (7, 7))).all() assert (galaxy_fit_data.noise_map(return_in_2d=True, return_masked=False) == 2.0 * np.ones((7, 7))).all() assert (galaxy_fit_data._image_1d == np.ones(9)).all() assert (galaxy_fit_data._noise_map_1d == 2.0 * np.ones(9)).all() assert (galaxy_fit_data._mask_1d == np.full(fill_value=False, shape=(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(return_in_2d=True) == 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(return_in_2d=True) == 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__pixel_scale_interpolation_grid(self, image_7x7, sub_mask_7x7): noise_map = al.ScaledSquarePixelArray(array=2.0 * np.ones((7, 7)), pixel_scale=3.0) gal_data_7x7 = al.GalaxyData(image=image_7x7, noise_map=noise_map, pixel_scale=3.0) gal_data_7x7 = al.GalaxyFitData( galaxy_data=gal_data_7x7, mask=sub_mask_7x7, pixel_scale_interpolation_grid=1.0, use_image=True, ) grid = al.Grid.from_mask(mask=sub_mask_7x7) new_grid = grid.new_grid_with_interpolator( pixel_scale_interpolation_grid=1.0) assert (gal_data_7x7.grid == new_grid).all() assert (gal_data_7x7.grid.interpolator.vtx == new_grid.interpolator.vtx ).all() assert (gal_data_7x7.grid.interpolator.wts == new_grid.interpolator.wts ).all()
def test__1x2_image__noise_not_1__alls_correct(self): image = al.ScaledSquarePixelArray(array=5.0 * np.ones((3, 4)), pixel_scale=1.0) image[1, 2] = 4.0 noise_map = al.ScaledSquarePixelArray(array=2.0 * np.ones((3, 4)), pixel_scale=1.0) galaxy_data = al.GalaxyData(image=image, noise_map=noise_map, pixel_scale=3.0) mask = al.Mask( array=np.array([ [True, True, True, True], [True, False, False, True], [True, True, True, True], ]), pixel_scale=1.0, sub_size=1, ) g0 = MockGalaxy(value=1.0, shape=2) galaxy_fit_data = al.GalaxyFitData(galaxy_data=galaxy_data, mask=mask, use_image=True) fit = al.GalaxyFit(galaxy_data=galaxy_fit_data, 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.likelihood == -0.5 * ( (25.0 / 4.0) + 2.0 * np.log(2 * np.pi * 2.0**2)) galaxy_fit_data = al.GalaxyFitData(galaxy_data=galaxy_data, mask=mask, use_convergence=True) fit = al.GalaxyFit(galaxy_data=galaxy_fit_data, 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.likelihood == -0.5 * ( (25.0 / 4.0) + 2.0 * np.log(2 * np.pi * 2.0**2)) galaxy_fit_data = al.GalaxyFitData(galaxy_data=galaxy_data, mask=mask, use_potential=True) fit = al.GalaxyFit(galaxy_data=galaxy_fit_data, 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.likelihood == -0.5 * ( (25.0 / 4.0) + 2.0 * np.log(2 * np.pi * 2.0**2)) galaxy_fit_data = al.GalaxyFitData(galaxy_data=galaxy_data, mask=mask, use_deflections_y=True) fit = al.GalaxyFit(galaxy_data=galaxy_fit_data, model_galaxies=[g0]) assert fit.chi_squared == (25.0 / 4.0) assert fit.reduced_chi_squared == (25.0 / 4.0) / 2.0 assert fit.likelihood == -0.5 * ( (25.0 / 4.0) + 2.0 * np.log(2 * np.pi * 2.0**2)) galaxy_fit_data = al.GalaxyFitData(galaxy_data=galaxy_data, mask=mask, use_deflections_x=True) fit = al.GalaxyFit(galaxy_data=galaxy_fit_data, model_galaxies=[g0]) assert fit.chi_squared == (25.0 / 4.0) assert fit.reduced_chi_squared == (25.0 / 4.0) / 2.0 assert fit.likelihood == -0.5 * ( (25.0 / 4.0) + 2.0 * np.log(2 * np.pi * 2.0**2))
def test__gal_data_7x7_image(self, gal_data_7x7, sub_mask_7x7): galaxy_fit_data = al.GalaxyFitData(galaxy_data=gal_data_7x7, mask=sub_mask_7x7, use_image=True) assert galaxy_fit_data.pixel_scale == 1.0 assert (galaxy_fit_data.image(return_in_2d=True, return_masked=False) == np.ones( (7, 7))).all() assert (galaxy_fit_data.noise_map(return_in_2d=True, return_masked=False) == 2.0 * np.ones((7, 7))).all() assert (galaxy_fit_data._image_1d == np.ones(9)).all() assert (galaxy_fit_data._noise_map_1d == 2.0 * np.ones(9)).all() assert (galaxy_fit_data._mask_1d == np.full(fill_value=False, shape=(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(return_in_2d=True) == 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(return_in_2d=True) == 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_galaxy.MockGalaxy(value=1, shape=36) image = galaxy_fit_data.profile_quantity_from_galaxies( galaxies=[galaxy]) assert (image == np.ones(9)).all() galaxy = al.Galaxy( redshift=0.5, light=al.light_profiles.SphericalSersic(intensity=1.0)) image_gal = galaxy.profile_image_from_grid(grid=galaxy_fit_data.grid, return_in_2d=False, return_binned=True) image_gd = galaxy_fit_data.profile_quantity_from_galaxies( galaxies=[galaxy]) assert (image_gal == image_gd).all()
def test__gal_data_7x7_deflections_x(self, gal_data_7x7, sub_mask_7x7): galaxy_fit_data = al.GalaxyFitData(galaxy_data=gal_data_7x7, mask=sub_mask_7x7, use_deflections_x=True) assert galaxy_fit_data.pixel_scale == 1.0 assert (galaxy_fit_data.image(return_in_2d=True, return_masked=False) == np.ones( (7, 7))).all() assert (galaxy_fit_data.noise_map(return_in_2d=True, return_masked=False) == 2.0 * np.ones((7, 7))).all() assert (galaxy_fit_data._image_1d == np.ones(9)).all() assert (galaxy_fit_data._noise_map_1d == 2.0 * np.ones(9)).all() assert (galaxy_fit_data._mask_1d == np.full(fill_value=False, shape=(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(return_in_2d=True) == 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(return_in_2d=True) == 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_galaxy.MockGalaxy(value=1, shape=36) deflections_x = galaxy_fit_data.profile_quantity_from_galaxies( galaxies=[galaxy]) assert (deflections_x == np.ones(9)).all() galaxy = al.Galaxy( redshift=0.5, mass=al.mass_profiles.SphericalIsothermal(einstein_radius=1.0)) deflections_gal = galaxy.deflections_from_grid( grid=galaxy_fit_data.grid, return_in_2d=False, return_binned=False) deflections_gal = np.asarray([ galaxy_fit_data.grid.mapping.array_1d_binned_from_sub_array_1d( deflections_gal[:, 0]), galaxy_fit_data.grid.mapping.array_1d_binned_from_sub_array_1d( deflections_gal[:, 1]), ]).T deflections_gd = galaxy_fit_data.profile_quantity_from_galaxies( galaxies=[galaxy]) assert (deflections_gal[:, 1] == deflections_gd).all()
def test__gal_data_7x7_convergence(self, gal_data_7x7, sub_mask_7x7): galaxy_fit_data = al.GalaxyFitData(galaxy_data=gal_data_7x7, mask=sub_mask_7x7, use_convergence=True) assert galaxy_fit_data.pixel_scale == 1.0 assert (galaxy_fit_data.image(return_in_2d=True, return_masked=False) == np.ones( (7, 7))).all() assert (galaxy_fit_data.noise_map(return_in_2d=True, return_masked=False) == 2.0 * np.ones((7, 7))).all() assert (galaxy_fit_data._image_1d == np.ones(9)).all() assert (galaxy_fit_data._noise_map_1d == 2.0 * np.ones(9)).all() assert (galaxy_fit_data._mask_1d == np.full(fill_value=False, shape=(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(return_in_2d=True) == 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(return_in_2d=True) == 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_galaxy.MockGalaxy(value=1, shape=36) convergence = galaxy_fit_data.profile_quantity_from_galaxies( galaxies=[galaxy]) assert (convergence == np.ones(9)).all() galaxy = al.Galaxy( redshift=0.5, mass=al.mass_profiles.SphericalIsothermal(einstein_radius=1.0)) convergence_gal = galaxy.convergence_from_grid( grid=galaxy_fit_data.grid, return_in_2d=False, return_binned=True) convergence_gd = galaxy_fit_data.profile_quantity_from_galaxies( galaxies=[galaxy]) assert (convergence_gal == convergence_gd).all()
def make_gal_fit_data_7x7_deflections_x(gal_data_7x7, sub_mask_7x7): return al.GalaxyFitData(galaxy_data=gal_data_7x7, mask=sub_mask_7x7, use_deflections_x=True)
def make_gal_fit_data_7x7_potential(gal_data_7x7, sub_mask_7x7): return al.GalaxyFitData(galaxy_data=gal_data_7x7, mask=sub_mask_7x7, use_potential=True)
def make_gal_fit_data_7x7_convergence(gal_data_7x7, sub_mask_7x7): return al.GalaxyFitData(galaxy_data=gal_data_7x7, mask=sub_mask_7x7, use_convergence=True)
def make_gal_fit_data_7x7_image(gal_data_7x7, sub_mask_7x7): return al.GalaxyFitData(galaxy_data=gal_data_7x7, mask=sub_mask_7x7, use_image=True)