def test__noise_map__with_and_without_hyper_galaxy(masked_imaging_7x7_no_blur):

    g0 = ag.Galaxy(redshift=0.5,
                   light_profile=MockLightProfile(image_2d_value=1.0))

    plane = ag.Plane(galaxies=[g0])

    fit = ag.FitImaging(dataset=masked_imaging_7x7_no_blur, plane=plane)

    assert fit.noise_map.slim == pytest.approx(
        np.full(fill_value=2.0, shape=(9, )), 1.0e-1)

    hyper_image = ag.Array2D.ones(shape_native=(3, 3), pixel_scales=1.0)

    g0 = ag.Galaxy(
        redshift=0.5,
        light_profile=MockLightProfile(image_2d_value=1.0),
        hyper_galaxy=ag.HyperGalaxy(contribution_factor=1.0,
                                    noise_factor=1.0,
                                    noise_power=1.0),
        hyper_model_image=hyper_image,
        hyper_galaxy_image=hyper_image,
        hyper_minimum_value=0.0,
    )

    plane = ag.Plane(galaxies=[g0])

    fit = ag.FitImaging(dataset=masked_imaging_7x7_no_blur, plane=plane)

    assert fit.noise_map.slim == pytest.approx(
        np.full(fill_value=4.0, shape=(9, )), 1.0e-1)
    assert fit.log_likelihood == pytest.approx(-20.7470, 1.0e-4)
Example #2
0
def test__all_individual_plotter__output_file_with_default_name(
    plane_7x7,
    sub_grid_2d_7x7,
    mask_2d_7x7,
    grid_2d_irregular_7x7_list,
    include_2d_all,
    plot_path,
    plot_patch,
):

    plane_plotter = aplt.PlanePlotter(
        plane=plane_7x7,
        grid=sub_grid_2d_7x7,
        include_2d=include_2d_all,
        mat_plot_2d=aplt.MatPlot2D(
            output=aplt.Output(plot_path, format="png")),
    )

    plane_plotter.figures_2d(image=True, plane_image=True, plane_grid=True)

    assert path.join(plot_path, "image_2d.png") in plot_patch.paths
    assert path.join(plot_path, "plane_image.png") in plot_patch.paths
    assert path.join(plot_path, "plane_grid.png") in plot_patch.paths

    plane_7x7.galaxies[0].hyper_galaxy = ag.HyperGalaxy()
    plane_7x7.galaxies[0].hyper_model_image = ag.Array2D.ones(shape_native=(7,
                                                                            7),
                                                              pixel_scales=0.1)
    plane_7x7.galaxies[0].hyper_galaxy_image = ag.Array2D.ones(
        shape_native=(7, 7), pixel_scales=0.1)

    plane_plotter.figures_2d(contribution_map=True)

    assert path.join(plot_path, "contribution_map_2d.png") in plot_patch.paths
Example #3
0
def test__figures_2d__all_are_output(
    gal_x1_lp_x1_mp,
    sub_grid_2d_7x7,
    mask_2d_7x7,
    grid_2d_irregular_7x7_list,
    include_2d_all,
    plot_path,
    plot_patch,
):

    galaxy_plotter = aplt.GalaxyPlotter(
        galaxy=gal_x1_lp_x1_mp,
        grid=sub_grid_2d_7x7,
        include_2d=include_2d_all,
        mat_plot_2d=aplt.MatPlot2D(
            output=aplt.Output(plot_path, format="png")),
    )
    galaxy_plotter.figures_2d(image=True, convergence=True)

    assert path.join(plot_path, "image_2d.png") in plot_patch.paths
    assert path.join(plot_path, "convergence_2d.png") in plot_patch.paths

    gal_x1_lp_x1_mp.hyper_galaxy = ag.HyperGalaxy()
    gal_x1_lp_x1_mp.hyper_model_image = ag.Array2D.ones(shape_native=(7, 7),
                                                        pixel_scales=0.1)
    gal_x1_lp_x1_mp.hyper_galaxy_image = ag.Array2D.ones(shape_native=(7, 7),
                                                         pixel_scales=0.1)

    galaxy_plotter.figures_2d(contribution_map=True)
    assert path.join(plot_path, "contribution_map_2d.png") in plot_patch.paths
Example #4
0
    def test__uses_hyper_fit_correctly(self, masked_imaging_7x7):

        galaxies = af.ModelInstance()
        galaxies.galaxy = ag.Galaxy(redshift=0.5,
                                    light=ag.lp.EllSersic(intensity=1.0),
                                    mass=ag.mp.SphIsothermal)
        galaxies.source = ag.Galaxy(redshift=1.0, light=ag.lp.EllSersic())

        instance = af.ModelInstance()
        instance.galaxies = galaxies

        galaxy_hyper_image = ag.Array2D.ones(shape_native=(3, 3),
                                             pixel_scales=0.1)
        galaxy_hyper_image[4] = 10.0
        hyper_model_image = ag.Array2D.full(fill_value=0.5,
                                            shape_native=(3, 3),
                                            pixel_scales=0.1)

        hyper_galaxy_image_path_dict = {
            ("galaxies", "galaxy"): galaxy_hyper_image
        }

        result = mock.MockResult(
            hyper_galaxy_image_path_dict=hyper_galaxy_image_path_dict,
            hyper_model_image=hyper_model_image,
        )

        analysis = ag.AnalysisImaging(dataset=masked_imaging_7x7,
                                      hyper_dataset_result=result)

        hyper_galaxy = ag.HyperGalaxy(contribution_factor=1.0,
                                      noise_factor=1.0,
                                      noise_power=1.0)

        instance.galaxies.galaxy.hyper_galaxy = hyper_galaxy

        fit_likelihood = analysis.log_likelihood_function(instance=instance)

        g0 = ag.Galaxy(
            redshift=0.5,
            light_profile=instance.galaxies.galaxy.light,
            mass_profile=instance.galaxies.galaxy.mass,
            hyper_galaxy=hyper_galaxy,
            hyper_model_image=hyper_model_image,
            hyper_galaxy_image=galaxy_hyper_image,
            hyper_minimum_value=0.0,
        )
        g1 = ag.Galaxy(redshift=1.0,
                       light_profile=instance.galaxies.source.light)

        plane = ag.Plane(galaxies=[g0, g1])

        fit = ag.FitImaging(dataset=masked_imaging_7x7, plane=plane)

        assert (fit.plane.galaxies[0].hyper_galaxy_image == galaxy_hyper_image
                ).all()
        assert fit_likelihood == fit.log_likelihood
Example #5
0
    def test__hyper_noise_map_from(self):

        noise_map = np.array([1.0, 2.0, 3.0])
        contribution_map = np.ones((3, 1))

        hyper_galaxy = ag.HyperGalaxy(
            contribution_factor=0.0, noise_factor=2.0, noise_power=1.0
        )

        hyper_noise_map = hyper_galaxy.hyper_noise_map_from(
            noise_map=noise_map, contribution_map=contribution_map
        )

        assert (hyper_noise_map == np.array([2.0, 4.0, 6.0])).all()

        noise_map = np.array([1.0, 2.0, 3.0])
        contribution_map = np.array([[0.0, 0.5, 1.0]])

        hyper_galaxy = ag.HyperGalaxy(
            contribution_factor=0.0, noise_factor=2.0, noise_power=1.0
        )

        hyper_noise_map = hyper_galaxy.hyper_noise_map_from(
            noise_map=noise_map, contribution_map=contribution_map
        )

        assert (hyper_noise_map == np.array([0.0, 2.0, 6.0])).all()

        noise_map = np.array([1.0, 2.0, 3.0])
        contribution_map = np.array([[0.0, 0.5, 1.0]])

        hyper_galaxy = ag.HyperGalaxy(
            contribution_factor=0.0, noise_factor=2.0, noise_power=2.0
        )

        hyper_noise_map = hyper_galaxy.hyper_noise_map_from(
            noise_map=noise_map, contribution_map=contribution_map
        )

        assert (hyper_noise_map == np.array([0.0, 2.0, 18.0])).all()
Example #6
0
    def test__contribution_map_from(self):

        hyper_image = np.ones((3,))

        hyp = ag.HyperGalaxy(contribution_factor=0.0)
        contribution_map = hyp.contribution_map_from(
            hyper_model_image=hyper_image, hyper_galaxy_image=hyper_image
        )

        assert (contribution_map == np.ones((3,))).all()

        hyper_image = np.array([0.5, 1.0, 1.5])

        hyp = ag.HyperGalaxy(contribution_factor=1.0)
        contribution_map = hyp.contribution_map_from(
            hyper_model_image=hyper_image, hyper_galaxy_image=hyper_image
        )

        assert (
            contribution_map
            == np.array([(0.5 / 1.5) / (1.5 / 2.5), (1.0 / 2.0) / (1.5 / 2.5), 1.0])
        ).all()

        hyper_image = np.ones((3,))

        hyp = ag.HyperGalaxy(contribution_factor=0.0)

        galaxy = ag.Galaxy(
            redshift=0.5,
            hyper_galaxy=hyp,
            hyper_galaxy_image=hyper_image,
            hyper_model_image=hyper_image,
        )

        contribution_map = hyp.contribution_map_from(
            hyper_model_image=hyper_image, hyper_galaxy_image=hyper_image
        )

        assert (contribution_map == galaxy.contribution_map).all()
def test__fit_figure_of_merit__include_hyper_methods(masked_imaging_7x7):

    hyper_image_sky = ag.hyper_data.HyperImageSky(sky_scale=1.0)
    hyper_background_noise = ag.hyper_data.HyperBackgroundNoise(
        noise_scale=1.0)

    hyper_galaxy = ag.HyperGalaxy(contribution_factor=1.0,
                                  noise_factor=1.0,
                                  noise_power=1.0)

    g0 = ag.Galaxy(
        redshift=0.5,
        light_profile=ag.lp.EllSersic(intensity=1.0),
        mass_profile=ag.mp.SphIsothermal(einstein_radius=1.0),
        hyper_galaxy=hyper_galaxy,
        hyper_model_image=np.ones(9),
        hyper_galaxy_image=np.ones(9),
        hyper_minimum_value=0.0,
    )
    g1 = ag.Galaxy(redshift=1.0, light_profile=ag.lp.EllSersic(intensity=1.0))

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

    fit = ag.FitImaging(
        dataset=masked_imaging_7x7,
        plane=plane,
        hyper_image_sky=hyper_image_sky,
        hyper_background_noise=hyper_background_noise,
    )

    assert (fit.image == np.full(fill_value=2.0, shape=(9, ))).all()
    assert (fit.noise_map == np.full(fill_value=5.0, shape=(9, ))).all()
    assert fit.log_likelihood == pytest.approx(-12104.68, 1e-4)
    assert fit.figure_of_merit == pytest.approx(-12104.68, 1.0e-4)

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

    g0 = ag.Galaxy(
        redshift=0.5,
        pixelization=pix,
        regularization=reg,
        hyper_galaxy=hyper_galaxy,
        hyper_model_image=np.ones(9),
        hyper_galaxy_image=np.ones(9),
        hyper_minimum_value=0.0,
    )

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

    fit = ag.FitImaging(
        dataset=masked_imaging_7x7,
        plane=plane,
        hyper_image_sky=hyper_image_sky,
        hyper_background_noise=hyper_background_noise,
        settings_inversion=ag.SettingsInversion(use_w_tilde=False),
    )

    assert (fit.image == np.full(fill_value=2.0, shape=(9, ))).all()
    assert (fit.noise_map == np.full(fill_value=5.0, shape=(9, ))).all()
    assert fit.log_evidence == pytest.approx(-30.1448, 1e-4)
    assert fit.figure_of_merit == pytest.approx(-30.1448, 1.0e-4)

    galaxy_light = ag.Galaxy(
        redshift=0.5,
        light_profile=ag.lp.EllSersic(intensity=1.0),
        hyper_galaxy=hyper_galaxy,
        hyper_model_image=np.ones(9),
        hyper_galaxy_image=np.ones(9),
        hyper_minimum_value=0.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.FitImaging(
        dataset=masked_imaging_7x7,
        plane=plane,
        hyper_image_sky=hyper_image_sky,
        hyper_background_noise=hyper_background_noise,
        settings_inversion=ag.SettingsInversion(use_w_tilde=False),
    )

    assert (fit.image == np.full(fill_value=2.0, shape=(9, ))).all()
    assert (fit.noise_map == np.full(fill_value=5.0, shape=(9, ))).all()
    assert fit.log_evidence == pytest.approx(-1132.9297, 1e-4)
    assert fit.figure_of_merit == pytest.approx(-1132.9297, 1.0e-4)
def test__hyper_model_inversion_from__adds_hyper_galaxies():
    model = af.Collection(galaxies=af.Collection(
        galaxy_0=af.Model(ag.Galaxy, redshift=0.5),
        galaxy_1=af.Model(
            ag.Galaxy,
            redshift=1.0,
            bulge=ag.lp.EllSersic,
            pixelization=ag.pix.Rectangular,
            regularization=ag.reg.Constant,
        ),
    ))

    instance = model.instance_from_prior_medians()

    path_galaxy_tuples = [
        (
            ("galaxies", "galaxy_0"),
            ag.Galaxy(redshift=0.5,
                      hyper_galaxy=ag.HyperGalaxy(contribution_factor=1)),
        ),
        (
            ("galaxies", "galaxy_1"),
            ag.Galaxy(redshift=1.0,
                      hyper_galaxy=ag.HyperGalaxy(contribution_factor=2)),
        ),
    ]

    hyper_galaxy_image_path_dict = {
        ("galaxies", "galaxy_0"):
        ag.Array2D.ones(shape_native=(3, 3), pixel_scales=1.0),
        ("galaxies", "galaxy_1"):
        ag.Array2D.full(fill_value=2.0, shape_native=(3, 3), pixel_scales=1.0),
    }

    result = mock.MockResult(
        instance=instance,
        path_galaxy_tuples=path_galaxy_tuples,
        hyper_galaxy_image_path_dict=hyper_galaxy_image_path_dict,
    )

    setup_hyper = ag.SetupHyper()
    setup_hyper.hyper_galaxy_names = ["galaxy_0"]

    model = ag.util.model.hyper_inversion_model_from(result=result,
                                                     setup_hyper=setup_hyper)

    assert isinstance(model.galaxies.galaxy_0, af.Model)
    assert model.galaxies.galaxy_0.redshift == 0.5
    assert model.galaxies.galaxy_0.hyper_galaxy.contribution_factor == 1
    assert model.galaxies.galaxy_1.hyper_galaxy is None

    setup_hyper = ag.SetupHyper()
    setup_hyper.hyper_galaxy_names = ["galaxy_0", "galaxy_1"]

    model = ag.util.model.hyper_inversion_model_from(result=result,
                                                     setup_hyper=setup_hyper)

    assert isinstance(model.galaxies.galaxy_0, af.Model)
    assert model.galaxies.galaxy_0.redshift == 0.5
    assert model.galaxies.galaxy_0.hyper_galaxy.contribution_factor == 1
    assert isinstance(model.galaxies.galaxy_1, af.Model)
    assert model.galaxies.galaxy_1.redshift == 1.0
    assert model.galaxies.galaxy_1.hyper_galaxy.contribution_factor == 2
def make_hyper_galaxy():
    return ag.HyperGalaxy(noise_factor=1.0,
                          noise_power=1.0,
                          contribution_factor=1.0)