def test__stochastic_model_from():
    model = af.Collection(galaxies=af.Collection(
        lens=af.Model(
            ag.Galaxy,
            redshift=0.5,
            light=ag.lp.SphSersic(),
            mass=ag.mp.SphIsothermal(),
        ),
        source=af.Model(
            ag.Galaxy,
            redshift=1.0,
            pixelization=ag.pix.VoronoiBrightnessImage(),
            regularization=ag.reg.AdaptiveBrightness(),
        ),
    ))

    instance = model.instance_from_prior_medians()

    model = af.Collection(galaxies=af.Collection(
        lens=af.Model(ag.Galaxy, redshift=0.5)))

    result = mock.MockResult(instance=instance, model=model)

    model = ag.util.model.stochastic_model_from(result=result)

    assert isinstance(model.galaxies.lens.mass.centre, af.TuplePrior)
    assert isinstance(model.galaxies.lens.light.intensity, float)
    assert isinstance(model.galaxies.source.pixelization.pixels, int)
    assert isinstance(model.galaxies.source.regularization.inner_coefficient,
                      float)

    model = ag.util.model.stochastic_model_from(result=result,
                                                include_lens_light=True)

    assert isinstance(model.galaxies.lens.mass.centre, af.TuplePrior)
    assert isinstance(model.galaxies.lens.light.intensity, af.LogUniformPrior)
    assert isinstance(model.galaxies.source.pixelization.pixels, int)
    assert isinstance(model.galaxies.source.regularization.inner_coefficient,
                      float)

    model = ag.util.model.stochastic_model_from(result=result,
                                                include_pixelization=True)

    assert isinstance(model.galaxies.lens.mass.centre, af.TuplePrior)
    assert isinstance(model.galaxies.lens.light.intensity, float)
    assert isinstance(model.galaxies.source.pixelization.pixels,
                      af.UniformPrior)
    assert not isinstance(
        model.galaxies.source.regularization.inner_coefficient,
        af.UniformPrior)

    model = ag.util.model.stochastic_model_from(result=result,
                                                include_regularization=True)

    assert isinstance(model.galaxies.lens.mass.centre, af.TuplePrior)
    assert isinstance(model.galaxies.lens.light.intensity, float)
    assert isinstance(model.galaxies.source.pixelization.pixels, int)
    assert isinstance(model.galaxies.source.regularization.inner_coefficient,
                      af.UniformPrior)
Exemple #2
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
def test__hyper_model_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),
        )
    )

    instance = model.instance_from_prior_medians()

    path_galaxy_tuples = [
        (("galaxies", "galaxy_0"), ag.Galaxy(redshift=0.5)),
        (("galaxies", "galaxy_1"), ag.Galaxy(redshift=1.0)),
    ]

    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_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.cls is ag.HyperGalaxy
    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_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.cls is ag.HyperGalaxy
    assert isinstance(model.galaxies.galaxy_1, af.Model)
    assert model.galaxies.galaxy_1.redshift == 1.0
    assert model.galaxies.galaxy_1.hyper_galaxy.cls is ag.HyperGalaxy
    def test__associate_hyper_images(self, masked_imaging_7x7):

        galaxies = af.ModelInstance()
        galaxies.galaxy = ag.Galaxy(redshift=0.5)
        galaxies.source = ag.Galaxy(redshift=1.0)

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

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

        result = mock.MockResult(
            instance=instance,
            hyper_galaxy_image_path_dict=hyper_galaxy_image_path_dict,
            hyper_model_image=ag.Array2D.full(
                fill_value=3.0, shape_native=(3, 3), pixel_scales=1.0
            ),
        )

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

        instance = analysis.associate_hyper_images(instance=instance)

        assert instance.galaxies.galaxy.hyper_galaxy_image.native == pytest.approx(
            np.ones((3, 3)), 1.0e-4
        )
        assert instance.galaxies.source.hyper_galaxy_image.native == pytest.approx(
            2.0 * np.ones((3, 3)), 1.0e-4
        )

        assert instance.galaxies.galaxy.hyper_model_image.native == pytest.approx(
            3.0 * np.ones((3, 3)), 1.0e-4
        )
        assert instance.galaxies.source.hyper_model_image.native == pytest.approx(
            3.0 * np.ones((3, 3)), 1.0e-4
        )
def test__hyper_model_from():

    model = af.Collection(
        galaxies=af.Collection(
            galaxy=af.Model(
                ag.Galaxy,
                redshift=0.5,
                pixelization=ag.pix.Rectangular,
                regularization=ag.reg.Constant,
            ),
            galaxy_1=af.Model(ag.Galaxy, redshift=1.0, bulge=ag.lp.EllSersic),
        )
    )

    instance = model.instance_from_prior_medians()

    result = mock.MockResult(instance=instance)

    model = ag.util.model.hyper_model_from(setup_hyper=ag.SetupHyper(), result=result)

    assert isinstance(model.galaxies.galaxy.pixelization, af.Model)
    assert isinstance(model.galaxies.galaxy.regularization, af.Model)

    assert model.galaxies.galaxy.pixelization.cls is ag.pix.Rectangular
    assert model.galaxies.galaxy.regularization.cls is ag.reg.Constant
    assert model.galaxies.galaxy_1.bulge.intensity == 1.0

    assert model.hyper_image_sky is None
    assert model.hyper_background_noise is None

    model = ag.util.model.hyper_model_from(result=result, setup_hyper=None)

    assert model == None

    model = ag.util.model.hyper_model_from(
        setup_hyper=ag.SetupHyper(
            hyper_image_sky=ag.hyper_data.HyperImageSky,
            hyper_background_noise=ag.hyper_data.HyperBackgroundNoise,
        ),
        result=result,
        include_hyper_image_sky=True,
    )

    assert isinstance(model.galaxies.galaxy.pixelization, af.Model)
    assert isinstance(model.galaxies.galaxy.regularization, af.Model)
    assert isinstance(model.hyper_image_sky, af.Model)
    assert isinstance(model.hyper_background_noise, af.Model)

    assert model.hyper_image_sky.cls == ag.hyper_data.HyperImageSky
    assert model.hyper_background_noise.cls == ag.hyper_data.HyperBackgroundNoise

    model = af.Collection(
        galaxies=af.Collection(
            galaxy=af.Model(ag.Galaxy, redshift=0.5),
            galaxy_1=af.Model(ag.Galaxy, redshift=1.0, bulge=ag.lp.EllSersic),
        )
    )

    instance = model.instance_from_prior_medians()

    result = mock.MockResult(instance=instance)

    model = ag.util.model.hyper_model_from(result=result, setup_hyper=ag.SetupHyper())

    assert model == None