Ejemplo n.º 1
0
def test__noise_map__with_hyper_galaxy_reaches_upper_limit(
        masked_imaging_7x7_no_blur):

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

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

    tracer = al.Tracer.from_galaxies(galaxies=[g0])

    fit = al.FitImaging(dataset=masked_imaging_7x7_no_blur, tracer=tracer)

    assert fit.noise_map.slim == pytest.approx(
        np.full(fill_value=1.0e8, shape=(9, )), 1.0e-1)
    assert fit.log_likelihood == pytest.approx(-174.0565, 1.0e-4)
Ejemplo n.º 2
0
    def test_combine_models(self, hyper_combined):
        result = MockResult()
        hyper_galaxy_result = MockResult()
        inversion_result = MockResult()

        hyper_galaxy_result.model = af.ModelMapper()
        inversion_result.model = af.ModelMapper()

        hyper_galaxy_result.model.hyper_galaxy = al.HyperGalaxy
        hyper_galaxy_result.model.pixelization = al.pix.Pixelization()
        inversion_result.model.pixelization = al.pix.Pixelization
        inversion_result.model.hyper_galaxy = al.HyperGalaxy()

        result.hyper_galaxy = hyper_galaxy_result
        result.inversion = inversion_result

        model = hyper_combined.combine_models(result)

        assert isinstance(model.hyper_galaxy, af.PriorModel)
        assert isinstance(model.pixelization, af.PriorModel)

        assert model.hyper_galaxy.cls == al.HyperGalaxy
        assert model.pixelization.cls == al.pix.Pixelization
Ejemplo n.º 3
0
def test__all_individual_plotter(
    tracer_x2_plane_7x7,
    sub_grid_2d_7x7,
    mask_2d_7x7,
    include_2d_all,
    plot_path,
    plot_patch,
):

    tracer_plotter = aplt.TracerPlotter(
        tracer=tracer_x2_plane_7x7,
        grid=sub_grid_2d_7x7,
        mat_plot_2d=aplt.MatPlot2D(
            output=aplt.Output(plot_path, format="png")),
    )

    tracer_plotter.figures_2d(
        image=True,
        source_plane=True,
        convergence=True,
        potential=True,
        deflections_y=True,
        deflections_x=True,
        magnification=True,
    )

    assert path.join(plot_path, "image_2d.png") in plot_patch.paths
    assert path.join(plot_path,
                     "plane_image_of_plane_1.png") in plot_patch.paths
    assert path.join(plot_path, "convergence_2d.png") in plot_patch.paths
    assert path.join(plot_path, "potential_2d.png") in plot_patch.paths
    assert path.join(plot_path, "deflections_y_2d.png") in plot_patch.paths
    assert path.join(plot_path, "deflections_x_2d.png") in plot_patch.paths
    assert path.join(plot_path, "magnification_2d.png") in plot_patch.paths

    tracer_x2_plane_7x7.planes[0].galaxies[0].hyper_galaxy = al.HyperGalaxy()
    tracer_x2_plane_7x7.planes[0].galaxies[
        0].hyper_model_image = al.Array2D.ones(shape_native=(7, 7),
                                               pixel_scales=0.1)
    tracer_x2_plane_7x7.planes[0].galaxies[
        0].hyper_galaxy_image = al.Array2D.ones(shape_native=(7, 7),
                                                pixel_scales=0.1)

    tracer_plotter.figures_2d(contribution_map=True)

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

    plot_patch.paths = []

    tracer_plotter = aplt.TracerPlotter(
        tracer=tracer_x2_plane_7x7,
        grid=sub_grid_2d_7x7,
        include_2d=include_2d_all,
        mat_plot_2d=aplt.MatPlot2D(
            output=aplt.Output(plot_path, format="png")),
    )

    tracer_plotter.figures_2d(image=True,
                              source_plane=True,
                              potential=True,
                              magnification=True)

    assert path.join(plot_path, "image_2d.png") in plot_patch.paths
    assert path.join(plot_path,
                     "plane_image_of_plane_1.png") in plot_patch.paths
    assert path.join(plot_path, "convergence_2d.png") not in plot_patch.paths
    assert path.join(plot_path, "potential_2d.png") in plot_patch.paths
    assert path.join(plot_path, "deflections_y_2d.png") not in plot_patch.paths
    assert path.join(plot_path, "deflections_x_2d.png") not in plot_patch.paths
    assert path.join(plot_path, "magnification_2d.png") in plot_patch.paths
# This is where noise-map scaling comes in. If we have no alternative, the best way to get Gaussian-distribution (e.g.
# more uniform) chi-squared fit is to increase the variances of image pixels with high chi-squared values. So, that's
# what we're going to do, by making our source galaxy a 'hyper-galaxy', a galaxy which use's its hyper-galaxy image to
# increase the noise in pixels where it has a large signal. Let take a look.

source_hyper_galaxy = al.Galaxy(
    redshift=1.0,
    pixelization=al.pix.VoronoiBrightnessImage(pixels=500,
                                               weight_floor=0.0,
                                               weight_power=5.0),
    regularization=al.reg.AdaptiveBrightness(inner_coefficient=0.001,
                                             outer_coefficient=0.2,
                                             signal_scale=2.0),
    hyper_galaxy=al.HyperGalaxy(contribution_factor=1.0,
                                noise_factor=1.5,
                                noise_power=1.0),
    hyper_galaxy_image=hyper_image,
    hyper_model_image=hyper_image,
)

fit = fit_masked_imaging_with_source_galaxy(masked_imaging=masked_imaging,
                                            source_galaxy=source_hyper_galaxy)

aplt.fit_imaging.subplot_fit_imaging(
    fit=fit,
    include=aplt.Include(inversion_image_pixelization_grid=True, mask=True))

# As expected, the chi-squared distribution looks *alot* better. The chi-squareds have reduced from the 200's to the
# 50's, because the variances were increased. This is what we want, so lets make sure we see an appropriate increase in
# Bayesian evidence
lens_galaxy_hyper = al.Galaxy(
    redshift=0.5,
    bulge=al.lp.EllSersic(
        centre=(0.0, 0.0),
        elliptical_comps=al.convert.elliptical_comps_from(axis_ratio=0.9,
                                                          angle=45.0),
        intensity=0.8,
        effective_radius=0.8,
        sersic_index=4.0,
    ),
    mass=al.mp.EllIsothermal(centre=(0.0, 0.0),
                             elliptical_comps=(0.111111, 0.0),
                             einstein_radius=1.6),
    hyper_galaxy=al.HyperGalaxy(contribution_factor=0.3,
                                noise_factor=4.0,
                                noise_power=1.5),
    hyper_model_image=hyper_image,
    hyper_galaxy_image=
    hyper_image_lens,  # <- The lens get its own hyper-galaxy image.
)

source_magnification_hyper = al.Galaxy(
    redshift=1.0,
    pixelization=al.pix.VoronoiMagnification(shape=(30, 30)),
    regularization=al.reg.Constant(coefficient=3.3),
    hyper_galaxy=al.HyperGalaxy(contribution_factor=2.0,
                                noise_factor=2.0,
                                noise_power=3.0),
    hyper_galaxy_image=hyper_image,
    hyper_model_image=
Ejemplo n.º 6
0
    def test__uses_hyper_fit_correctly(self, masked_imaging_7x7):

        galaxies = af.ModelInstance()
        galaxies.lens = al.Galaxy(
            redshift=0.5,
            light=al.lp.EllipticalSersic(intensity=1.0),
            mass=al.mp.SphericalIsothermal,
        )
        galaxies.source = al.Galaxy(redshift=1.0,
                                    light=al.lp.EllipticalSersic())

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

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

        hyper_galaxy_image_path_dict = {("galaxies", "lens"): lens_hyper_image}

        results = mock.MockResults(
            use_as_hyper_dataset=True,
            hyper_galaxy_image_path_dict=hyper_galaxy_image_path_dict,
            hyper_model_image=hyper_model_image,
        )

        analysis = al.PhaseImaging.Analysis(
            masked_imaging=masked_imaging_7x7,
            settings=al.SettingsPhaseImaging(),
            results=results,
            cosmology=cosmo.Planck15,
        )

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

        instance.galaxies.lens.hyper_galaxy = hyper_galaxy

        fit_likelihood = analysis.log_likelihood_function(instance=instance)

        g0 = al.Galaxy(
            redshift=0.5,
            light_profile=instance.galaxies.lens.light,
            mass_profile=instance.galaxies.lens.mass,
            hyper_galaxy=hyper_galaxy,
            hyper_model_image=hyper_model_image,
            hyper_galaxy_image=lens_hyper_image,
            hyper_minimum_value=0.0,
        )
        g1 = al.Galaxy(redshift=1.0,
                       light_profile=instance.galaxies.source.light)

        tracer = al.Tracer.from_galaxies(galaxies=[g0, g1])

        fit = FitImaging(masked_imaging=masked_imaging_7x7, tracer=tracer)

        assert (fit.tracer.galaxies[0].hyper_galaxy_image == lens_hyper_image
                ).all()
        assert fit_likelihood == fit.log_likelihood
Ejemplo n.º 7
0
def test__all_individual_plotters__output_file_with_default_name(
    plane_7x7,
    sub_grid_7x7,
    mask_7x7,
    positions_7x7,
    include_all,
    plane_plotter_path,
    plot_patch,
):

    aplt.Plane.profile_image(
        plane=plane_7x7,
        grid=sub_grid_7x7,
        positions=positions_7x7,
        include=include_all,
        plotter=aplt.Plotter(output=aplt.Output(plane_plotter_path, format="png")),
    )

    assert plane_plotter_path + "profile_image.png" in plot_patch.paths

    aplt.Plane.plane_image(
        plane=plane_7x7,
        grid=sub_grid_7x7,
        positions=positions_7x7,
        include=include_all,
        plotter=aplt.Plotter(output=aplt.Output(plane_plotter_path, format="png")),
    )

    assert plane_plotter_path + "plane_image.png" in plot_patch.paths

    aplt.Plane.convergence(
        plane=plane_7x7,
        grid=sub_grid_7x7,
        include=include_all,
        plotter=aplt.Plotter(output=aplt.Output(plane_plotter_path, format="png")),
    )

    assert plane_plotter_path + "convergence.png" in plot_patch.paths

    aplt.Plane.potential(
        plane=plane_7x7,
        grid=sub_grid_7x7,
        include=include_all,
        plotter=aplt.Plotter(output=aplt.Output(plane_plotter_path, format="png")),
    )

    assert plane_plotter_path + "potential.png" in plot_patch.paths

    aplt.Plane.deflections_y(
        plane=plane_7x7,
        grid=sub_grid_7x7,
        include=include_all,
        plotter=aplt.Plotter(output=aplt.Output(plane_plotter_path, format="png")),
    )

    assert plane_plotter_path + "deflections_y.png" in plot_patch.paths

    aplt.Plane.deflections_x(
        plane=plane_7x7,
        grid=sub_grid_7x7,
        include=include_all,
        plotter=aplt.Plotter(output=aplt.Output(plane_plotter_path, format="png")),
    )

    assert plane_plotter_path + "deflections_x.png" in plot_patch.paths

    aplt.Plane.magnification(
        plane=plane_7x7,
        grid=sub_grid_7x7,
        include=include_all,
        plotter=aplt.Plotter(output=aplt.Output(plane_plotter_path, format="png")),
    )

    assert plane_plotter_path + "magnification.png" in plot_patch.paths

    aplt.Plane.plane_grid(
        plane=plane_7x7,
        grid=sub_grid_7x7,
        include=include_all,
        plotter=aplt.Plotter(output=aplt.Output(plane_plotter_path, format="png")),
    )

    assert plane_plotter_path + "plane_grid.png" in plot_patch.paths

    plane_7x7.galaxies[0].hyper_galaxy = al.HyperGalaxy()
    plane_7x7.galaxies[0].hyper_model_image = al.Array.ones(
        shape_2d=(7, 7), pixel_scales=0.1
    )
    plane_7x7.galaxies[0].hyper_galaxy_image = al.Array.ones(
        shape_2d=(7, 7), pixel_scales=0.1
    )

    aplt.Plane.contribution_map(
        plane=plane_7x7,
        mask=mask_7x7,
        positions=positions_7x7,
        include=include_all,
        plotter=aplt.Plotter(output=aplt.Output(plane_plotter_path, format="png")),
    )

    assert plane_plotter_path + "contribution_map.png" in plot_patch.paths
Ejemplo n.º 8
0
    def test__likelihood_function_is_same_as_normal_phase_likelihood_function(
        self, imaging_7x7, mask_7x7
    ):

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

        lens_galaxy = al.Galaxy(
            redshift=0.5, light=al.lp.EllipticalSersic(intensity=0.1)
        )

        phase_imaging_7x7 = al.PhaseImaging(
            galaxies=dict(lens=lens_galaxy),
            hyper_image_sky=hyper_image_sky,
            hyper_background_noise=hyper_background_noise,
            sub_size=2,
            cosmology=cosmo.FLRW,
            phase_name="test_phase",
        )

        analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7, mask=mask_7x7)
        instance = phase_imaging_7x7.model.instance_from_unit_vector([])

        mask = phase_imaging_7x7.meta_dataset.mask_with_phase_sub_size_from_mask(
            mask=mask_7x7
        )
        assert mask.sub_size == 2

        masked_imaging = al.masked_imaging(imaging=imaging_7x7, mask=mask)
        tracer = analysis.tracer_for_instance(instance=instance)
        fit = ImagingFit(
            masked_imaging=masked_imaging,
            tracer=tracer,
            hyper_image_sky=hyper_image_sky,
            hyper_background_noise=hyper_background_noise,
        )

        phase_imaging_7x7_hyper = phase_imaging_7x7.extend_with_multiple_hyper_phases(
            hyper_galaxy=True
        )

        instance = phase_imaging_7x7_hyper.model.instance_from_unit_vector([])

        instance.hyper_galaxy = al.HyperGalaxy(noise_factor=0.0)

        analysis = phase_imaging_7x7_hyper.hyper_phases[0].Analysis(
            masked_imaging=masked_imaging,
            hyper_model_image=fit.model_image,
            hyper_galaxy_image=fit.model_image,
            image_path="",
        )

        fit_hyper = analysis.fit_for_hyper_galaxy(
            hyper_galaxy=al.HyperGalaxy(noise_factor=0.0),
            hyper_image_sky=hyper_image_sky,
            hyper_background_noise=hyper_background_noise,
        )

        assert fit_hyper.likelihood == fit.likelihood

        fit_hyper = analysis.fit_for_hyper_galaxy(
            hyper_galaxy=al.HyperGalaxy(noise_factor=1.0),
            hyper_image_sky=hyper_image_sky,
            hyper_background_noise=hyper_background_noise,
        )

        assert fit_hyper.likelihood != fit.likelihood

        instance.hyper_galaxy = al.HyperGalaxy(noise_factor=0.0)

        likelihood = analysis.fit(instance=instance)

        assert likelihood == fit.likelihood
Ejemplo n.º 9
0
def test__fit_figure_of_merit__include_hyper_methods(masked_imaging_7x7):

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

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

    g0 = al.Galaxy(
        redshift=0.5,
        light_profile=al.lp.EllSersic(intensity=1.0),
        mass_profile=al.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 = al.Galaxy(redshift=1.0, light_profile=al.lp.EllSersic(intensity=1.0))

    tracer = al.Tracer.from_galaxies(galaxies=[g0, g1])

    fit = al.FitImaging(
        dataset=masked_imaging_7x7,
        tracer=tracer,
        hyper_image_sky=hyper_image_sky,
        hyper_background_noise=hyper_background_noise,
        use_hyper_scaling=True,
        settings_inversion=al.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_likelihood == pytest.approx(-186617.89365, 1e-4)
    assert fit.figure_of_merit == pytest.approx(-186617.89365, 1.0e-4)

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

    g0 = al.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,
    )

    tracer = al.Tracer.from_galaxies(galaxies=[al.Galaxy(redshift=0.5), g0])

    fit = al.FitImaging(
        dataset=masked_imaging_7x7,
        tracer=tracer,
        hyper_image_sky=hyper_image_sky,
        hyper_background_noise=hyper_background_noise,
        settings_inversion=al.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.14482, 1e-4)
    assert fit.figure_of_merit == pytest.approx(-30.14482, 1.0e-4)

    galaxy_light = al.Galaxy(
        redshift=0.5,
        light_profile=al.lp.EllSersic(intensity=1.0),
        hyper_galaxy=hyper_galaxy,
        hyper_model_image=al.Array2D.ones(shape_native=(3, 3),
                                          pixel_scales=1.0),
        hyper_galaxy_image=al.Array2D.ones(shape_native=(3, 3),
                                           pixel_scales=1.0),
        hyper_minimum_value=0.0,
    )

    galaxy_pix = al.Galaxy(redshift=1.0, pixelization=pix, regularization=reg)

    tracer = al.Tracer.from_galaxies(galaxies=[galaxy_light, galaxy_pix])

    fit = al.FitImaging(
        dataset=masked_imaging_7x7,
        tracer=tracer,
        hyper_image_sky=hyper_image_sky,
        hyper_background_noise=hyper_background_noise,
        settings_inversion=al.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(-6106.6402, 1e-4)
    assert fit.figure_of_merit == pytest.approx(-6106.6402, 1.0e-4)
Ejemplo n.º 10
0
def test__all_individual_plotters(
    tracer_x2_plane_7x7,
    sub_grid_7x7,
    mask_7x7,
    include_all,
    ray_tracing_plotter_path,
    plot_patch,
):
    aplt.Tracer.image(
        tracer=tracer_x2_plane_7x7,
        grid=sub_grid_7x7,
        include=include_all,
        plotter=aplt.Plotter(
            output=aplt.Output(ray_tracing_plotter_path, format="png")),
    )

    assert ray_tracing_plotter_path + "image.png" in plot_patch.paths

    aplt.Tracer.convergence(
        tracer=tracer_x2_plane_7x7,
        grid=sub_grid_7x7,
        include=include_all,
        plotter=aplt.Plotter(
            output=aplt.Output(ray_tracing_plotter_path, format="png")),
    )

    assert ray_tracing_plotter_path + "convergence.png" in plot_patch.paths

    aplt.Tracer.potential(
        tracer=tracer_x2_plane_7x7,
        grid=sub_grid_7x7,
        include=include_all,
        plotter=aplt.Plotter(
            output=aplt.Output(ray_tracing_plotter_path, format="png")),
    )

    assert ray_tracing_plotter_path + "potential.png" in plot_patch.paths

    aplt.Tracer.deflections_y(
        tracer=tracer_x2_plane_7x7,
        grid=sub_grid_7x7,
        include=include_all,
        plotter=aplt.Plotter(
            output=aplt.Output(ray_tracing_plotter_path, format="png")),
    )

    assert ray_tracing_plotter_path + "deflections_y.png" in plot_patch.paths

    aplt.Tracer.deflections_x(
        tracer=tracer_x2_plane_7x7,
        grid=sub_grid_7x7,
        include=include_all,
        plotter=aplt.Plotter(
            output=aplt.Output(ray_tracing_plotter_path, format="png")),
    )

    assert ray_tracing_plotter_path + "deflections_x.png" in plot_patch.paths

    aplt.Tracer.magnification(
        tracer=tracer_x2_plane_7x7,
        grid=sub_grid_7x7,
        include=include_all,
        plotter=aplt.Plotter(
            output=aplt.Output(ray_tracing_plotter_path, format="png")),
    )

    assert ray_tracing_plotter_path + "magnification.png" in plot_patch.paths

    tracer_x2_plane_7x7.planes[0].galaxies[0].hyper_galaxy = al.HyperGalaxy()
    tracer_x2_plane_7x7.planes[0].galaxies[
        0].hyper_model_image = al.Array.ones(shape_2d=(7, 7), pixel_scales=0.1)
    tracer_x2_plane_7x7.planes[0].galaxies[
        0].hyper_galaxy_image = al.Array.ones(shape_2d=(7, 7),
                                              pixel_scales=0.1)

    aplt.Tracer.contribution_map(
        tracer=tracer_x2_plane_7x7,
        mask=mask_7x7,
        include=include_all,
        plotter=aplt.Plotter(
            output=aplt.Output(ray_tracing_plotter_path, format="png")),
    )

    assert ray_tracing_plotter_path + "contribution_map.png" in plot_patch.paths
Ejemplo n.º 11
0
def make_hyper_galaxy():
    return al.HyperGalaxy(noise_factor=1.0,
                          noise_power=1.0,
                          contribution_factor=1.0)