Beispiel #1
0
    def test___fit_galaxy_model_visibilities_dict__has_profile_visibilitiess_and_inversion_mapped_reconstructed_visibilities(
        self, interferometer_7
    ):
        g0 = ag.Galaxy(redshift=0.5, light_profile=ag.lp.EllSersic(intensity=1.0))
        g1 = ag.Galaxy(redshift=0.5, light_profile=ag.lp.EllSersic(intensity=2.0))
        g2 = ag.Galaxy(redshift=0.5)

        pix = ag.pix.Rectangular(shape=(3, 3))
        reg = ag.reg.Constant(coefficient=1.0)
        galaxy_pix = ag.Galaxy(redshift=1.0, pixelization=pix, regularization=reg)

        plane = ag.Plane(redshift=0.75, galaxies=[g0, g1, g2, galaxy_pix])

        fit = ag.FitInterferometer(interferometer=interferometer_7, plane=plane)

        g0_visibilities = g0.profile_visibilities_from_grid_and_transformer(
            grid=interferometer_7.grid, transformer=interferometer_7.transformer
        )

        g1_visibilities = g1.profile_visibilities_from_grid_and_transformer(
            grid=interferometer_7.grid, transformer=interferometer_7.transformer
        )

        profile_visibilities = g0_visibilities + g1_visibilities

        profile_subtracted_visibilities = (
            interferometer_7.visibilities - profile_visibilities
        )
        mapper = pix.mapper_from_grid_and_sparse_grid(
            grid=interferometer_7.grid,
            settings=ag.SettingsPixelization(use_border=False),
        )

        inversion = inversions.InversionInterferometerMatrix.from_data_mapper_and_regularization(
            visibilities=profile_subtracted_visibilities,
            noise_map=interferometer_7.noise_map,
            transformer=interferometer_7.transformer,
            mapper=mapper,
            regularization=reg,
        )

        assert (
            fit.galaxy_model_visibilities_dict[g2] == 0.0 + 0.0j * np.zeros((7,))
        ).all()

        assert fit.galaxy_model_visibilities_dict[g0].slim == pytest.approx(
            g0_visibilities.slim, 1.0e-4
        )
        assert fit.galaxy_model_visibilities_dict[g1].slim == pytest.approx(
            g1_visibilities.slim, 1.0e-4
        )
        assert fit.galaxy_model_visibilities_dict[galaxy_pix].slim == pytest.approx(
            inversion.mapped_reconstructed_visibilities.slim, 1.0e-4
        )

        assert fit.model_visibilities.slim == pytest.approx(
            fit.galaxy_model_visibilities_dict[g0].slim
            + fit.galaxy_model_visibilities_dict[g1].slim
            + inversion.mapped_reconstructed_visibilities.slim,
            1.0e-4,
        )
Beispiel #2
0
    def test___all_fit_quantities__no_hyper_methods(self, interferometer_7):
        galaxy_light = ag.Galaxy(
            redshift=0.5, light_profile=ag.lp.EllSersic(intensity=1.0)
        )

        pix = ag.pix.Rectangular(shape=(3, 3))
        reg = ag.reg.Constant(coefficient=1.0)
        galaxy_pix = ag.Galaxy(redshift=1.0, pixelization=pix, regularization=reg)

        plane = ag.Plane(redshift=0.75, galaxies=[galaxy_light, galaxy_pix])

        fit = ag.FitInterferometer(interferometer=interferometer_7, plane=plane)

        profile_visibilities = plane.profile_visibilities_from_grid_and_transformer(
            grid=interferometer_7.grid, transformer=interferometer_7.transformer
        )

        assert profile_visibilities.slim == pytest.approx(fit.profile_visibilities.slim)

        profile_subtracted_visibilities = (
            interferometer_7.visibilities - profile_visibilities
        )

        assert profile_subtracted_visibilities.slim == pytest.approx(
            fit.profile_subtracted_visibilities.slim
        )

        mapper = pix.mapper_from_grid_and_sparse_grid(
            grid=interferometer_7.grid,
            settings=ag.SettingsPixelization(use_border=False),
        )

        inversion = inversions.InversionInterferometerMatrix.from_data_mapper_and_regularization(
            visibilities=profile_subtracted_visibilities,
            noise_map=interferometer_7.noise_map,
            transformer=interferometer_7.transformer,
            mapper=mapper,
            regularization=reg,
        )

        model_visibilities = (
            profile_visibilities + inversion.mapped_reconstructed_visibilities
        )

        assert model_visibilities.slim == pytest.approx(fit.model_visibilities.slim)

        residual_map = ag.util.fit.residual_map_from(
            data=interferometer_7.visibilities, model_data=model_visibilities
        )

        assert residual_map.slim == pytest.approx(fit.residual_map.slim)

        normalized_residual_map = ag.util.fit.normalized_residual_map_complex_from(
            residual_map=residual_map, noise_map=interferometer_7.noise_map
        )

        assert normalized_residual_map.slim == pytest.approx(
            fit.normalized_residual_map.slim
        )

        chi_squared_map = ag.util.fit.chi_squared_map_complex_from(
            residual_map=residual_map, noise_map=interferometer_7.noise_map
        )

        assert chi_squared_map.slim == pytest.approx(fit.chi_squared_map.slim)

        chi_squared = ag.util.fit.chi_squared_complex_from(
            chi_squared_map=chi_squared_map
        )

        noise_normalization = ag.util.fit.noise_normalization_complex_from(
            noise_map=interferometer_7.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)

        log_likelihood_with_regularization = ag.util.fit.log_likelihood_with_regularization_from(
            chi_squared=chi_squared,
            regularization_term=inversion.regularization_term,
            noise_normalization=noise_normalization,
        )

        assert log_likelihood_with_regularization == pytest.approx(
            fit.log_likelihood_with_regularization, 1e-4
        )

        log_evidence = ag.util.fit.log_evidence_from(
            chi_squared=chi_squared,
            regularization_term=inversion.regularization_term,
            log_curvature_regularization_term=inversion.log_det_curvature_reg_matrix_term,
            log_regularization_term=inversion.log_det_regularization_matrix_term,
            noise_normalization=noise_normalization,
        )

        assert log_evidence == fit.log_evidence
        assert log_evidence == fit.figure_of_merit

        mapped_reconstructed_image = ag.util.inversion.mapped_reconstructed_data_from(
            mapping_matrix=fit.inversion.mapper.mapping_matrix,
            reconstruction=fit.inversion.reconstruction,
        )

        assert (
            fit.inversion.mapped_reconstructed_image.slim == mapped_reconstructed_image
        ).all()
Beispiel #3
0
    def test___all_fit_quantities__uses_linear_operator_inversion(
        self, interferometer_7_lop
    ):
        # Ensures the inversion grid is used, as this would cause the test to fail.
        interferometer_7_lop.grid[0, 0] = -100.0

        pix = ag.pix.Rectangular(shape=(3, 3))
        reg = ag.reg.Constant(coefficient=0.01)

        g0 = ag.Galaxy(redshift=0.5, pixelization=pix, regularization=reg)

        plane = ag.Plane(galaxies=[ag.Galaxy(redshift=0.5), g0])

        fit = ag.FitInterferometer(
            interferometer=interferometer_7_lop,
            plane=plane,
            settings_inversion=ag.SettingsInversion(use_linear_operators=True),
        )

        mapper = pix.mapper_from_grid_and_sparse_grid(
            grid=interferometer_7_lop.grid_inversion, sparse_grid=None
        )

        inversion = inversions.InversionInterferometerLinearOperator.from_data_mapper_and_regularization(
            mapper=mapper,
            regularization=reg,
            visibilities=interferometer_7_lop.visibilities,
            noise_map=interferometer_7_lop.noise_map,
            transformer=interferometer_7_lop.transformer,
            settings=ag.SettingsInversion(use_linear_operators=True),
        )

        assert inversion.mapped_reconstructed_visibilities == pytest.approx(
            fit.model_visibilities, 1.0e-4
        )

        residual_map = ag.util.fit.residual_map_from(
            data=interferometer_7_lop.visibilities,
            model_data=inversion.mapped_reconstructed_visibilities,
        )

        assert residual_map.slim == pytest.approx(fit.residual_map.slim, 1.0e-4)

        normalized_residual_map = ag.util.fit.normalized_residual_map_complex_from(
            residual_map=residual_map, noise_map=interferometer_7_lop.noise_map
        )

        assert normalized_residual_map.slim == pytest.approx(
            fit.normalized_residual_map.slim, 1.0e-4
        )

        chi_squared_map = ag.util.fit.chi_squared_map_complex_from(
            residual_map=residual_map, noise_map=interferometer_7_lop.noise_map
        )

        assert chi_squared_map.slim == pytest.approx(fit.chi_squared_map.slim, 1.0e-4)

        chi_squared = ag.util.fit.chi_squared_complex_from(
            chi_squared_map=chi_squared_map
        )

        noise_normalization = ag.util.fit.noise_normalization_complex_from(
            noise_map=interferometer_7_lop.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)

        log_likelihood_with_regularization = ag.util.fit.log_likelihood_with_regularization_from(
            chi_squared=chi_squared,
            regularization_term=inversion.regularization_term,
            noise_normalization=noise_normalization,
        )

        assert log_likelihood_with_regularization == pytest.approx(
            fit.log_likelihood_with_regularization, 1e-4
        )

        log_evidence = ag.util.fit.log_evidence_from(
            chi_squared=chi_squared,
            regularization_term=inversion.regularization_term,
            log_curvature_regularization_term=inversion.log_det_curvature_reg_matrix_term,
            log_regularization_term=inversion.log_det_regularization_matrix_term,
            noise_normalization=noise_normalization,
        )

        assert log_evidence == fit.log_evidence
        assert log_evidence == fit.figure_of_merit

        mapped_reconstructed_image = ag.util.inversion.mapped_reconstructed_data_from(
            mapping_matrix=fit.inversion.mapper.mapping_matrix,
            reconstruction=fit.inversion.reconstruction,
        )

        assert (
            fit.inversion.mapped_reconstructed_image.slim == mapped_reconstructed_image
        ).all()
def make_gal_x1_lp_x1_mp():
    return ag.Galaxy(
        redshift=0.5, light_profile_0=make_lp_0(), mass_profile_0=make_mp_0()
    )
def make_gal_x2_mp():
    return ag.Galaxy(
        redshift=0.5, mass_profile_0=make_mp_0(), mass_profile_1=make_mp_1()
    )
def make_gal_x2_lp():
    return ag.Galaxy(
        redshift=0.5, light_profile_0=make_lp_0(), light_profile_1=make_lp_1()
    )