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__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 make_gal_fit_7x7_deflections_x():
    return ag.FitGalaxy(
        masked_galaxy_dataset=make_gal_fit_data_7x7_deflections_x(),
        model_galaxies=[make_gal_x1_mp()],
    )
def make_gal_fit_7x7_potential():
    return ag.FitGalaxy(
        masked_galaxy_dataset=make_gal_fit_data_7x7_potential(),
        model_galaxies=[make_gal_x1_mp()],
    )
def make_gal_fit_7x7_convergence():
    return ag.FitGalaxy(
        masked_galaxy_dataset=make_gal_fit_data_7x7_convergence(),
        model_galaxies=[make_gal_x1_mp()],
    )
def make_gal_fit_7x7_image():
    return ag.FitGalaxy(
        masked_galaxy_dataset=make_gal_fit_data_7x7_image(),
        model_galaxies=[make_gal_x1_lp()],
    )