예제 #1
0
def test__subtracted_images_of_planes(masked_imaging_7x7_no_blur):

    g0 = al.Galaxy(redshift=0.5,
                   light_profile=MockLightProfile(image_2d=np.ones(1)))

    g1 = al.Galaxy(redshift=0.75,
                   light_profile=MockLightProfile(image_2d=2.0 * np.ones(1)))

    g2 = al.Galaxy(redshift=1.0,
                   light_profile=MockLightProfile(image_2d=3.0 * np.ones(1)))

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

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

    assert fit.subtracted_images_of_planes[0].slim[0] == -4.0
    assert fit.subtracted_images_of_planes[1].slim[0] == -3.0
    assert fit.subtracted_images_of_planes[2].slim[0] == -2.0

    g0 = al.Galaxy(redshift=0.5,
                   light_profile=MockLightProfile(image_2d=np.ones(1)))

    g1 = al.Galaxy(redshift=1.0,
                   light_profile=MockLightProfile(image_2d=2.0 * np.ones(1)))

    g2 = al.Galaxy(redshift=1.0,
                   light_profile=MockLightProfile(image_2d=3.0 * np.ones(1)))

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

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

    assert fit.subtracted_images_of_planes[0].slim[0] == -4.0
    assert fit.subtracted_images_of_planes[1].slim[0] == -0.0
예제 #2
0
def test__image__with_and_without_hyper_background_sky(
        masked_imaging_7x7_no_blur):

    g0 = al.Galaxy(redshift=0.5,
                   light_profile=al.m.MockLightProfile(image_2d_value=1.0))
    tracer = al.Tracer.from_galaxies(galaxies=[g0])

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

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

    g0 = al.Galaxy(redshift=0.5,
                   light_profile=al.m.MockLightProfile(image_2d_value=1.0))
    tracer = al.Tracer.from_galaxies(galaxies=[g0])

    hyper_image_sky = al.hyper_data.HyperImageSky(sky_scale=1.0)

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

    assert fit.image.slim == pytest.approx(
        np.full(fill_value=2.0, shape=(9, )), 1.0e-1)
    assert fit.log_likelihood == pytest.approx(-15.6337, 1.0e-4)
예제 #3
0
def test__noise_map__with_and_without_hyper_background(
        masked_imaging_7x7_no_blur):

    g0 = al.Galaxy(redshift=0.5,
                   light_profile=al.m.MockLightProfile(image_2d_value=1.0))
    tracer = al.Tracer.from_galaxies(galaxies=[g0])

    fit = al.FitImaging(
        dataset=masked_imaging_7x7_no_blur,
        tracer=tracer,
        settings_inversion=al.SettingsInversion(use_w_tilde=False),
    )

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

    hyper_background_noise = al.hyper_data.HyperBackgroundNoise(
        noise_scale=1.0)

    fit = al.FitImaging(
        dataset=masked_imaging_7x7_no_blur,
        tracer=tracer,
        hyper_background_noise=hyper_background_noise,
        settings_inversion=al.SettingsInversion(use_w_tilde=False),
    )

    assert fit.noise_map.slim == pytest.approx(
        np.full(fill_value=3.0, shape=(9, )), 1.0e-1)
    assert fit.log_likelihood == pytest.approx(-18.1579, 1.0e-4)
예제 #4
0
def test__noise_map__with_and_without_hyper_galaxy(masked_imaging_7x7_no_blur):

    g0 = al.Galaxy(redshift=0.5,
                   light_profile=al.m.MockLightProfile(image_2d_value=1.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=2.0, shape=(9, )), 1.0e-1)

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

    g0 = al.Galaxy(
        redshift=0.5,
        light_profile=al.m.MockLightProfile(image_2d_value=1.0),
        hyper_galaxy=al.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,
    )

    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=4.0, shape=(9, )), 1.0e-1)
    assert fit.log_likelihood == pytest.approx(-20.7470, 1.0e-4)
예제 #5
0
def test__total_mappers(masked_imaging_7x7):

    g0 = al.Galaxy(redshift=0.5)

    g1 = al.Galaxy(redshift=1.0)

    g2 = al.Galaxy(redshift=2.0)

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

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

    assert fit.total_mappers == 0

    g2 = al.Galaxy(
        redshift=2.0,
        pixelization=al.pix.Rectangular(),
        regularization=al.reg.Constant(),
    )

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

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

    assert fit.total_mappers == 1

    g0 = al.Galaxy(
        redshift=0.5,
        pixelization=al.pix.Rectangular(),
        regularization=al.reg.Constant(),
    )

    g1 = al.Galaxy(
        redshift=1.0,
        pixelization=al.pix.Rectangular(),
        regularization=al.reg.Constant(),
    )

    g2 = al.Galaxy(
        redshift=2.0,
        pixelization=al.pix.Rectangular(),
        regularization=al.reg.Constant(),
    )

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

    fit = al.FitImaging(
        dataset=masked_imaging_7x7,
        tracer=tracer,
        settings_inversion=al.SettingsInversion(use_w_tilde=False),
    )

    assert fit.total_mappers == 3
예제 #6
0
def test__fit_figure_of_merit(masked_imaging_7x7):

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

    assert (fit.image == np.full(fill_value=1.0, shape=(9, ))).all()
    assert (fit.noise_map == np.full(fill_value=2.0, shape=(9, ))).all()
    assert fit.log_likelihood == pytest.approx(-1168351.9731, 1e-4)
    assert fit.figure_of_merit == pytest.approx(-1168351.9731, 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)

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

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

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

    galaxy_light = al.Galaxy(redshift=0.5,
                             light_profile=al.lp.EllSersic(intensity=1.0))

    pix = al.pix.Rectangular(shape=(3, 3))
    reg = al.reg.Constant(coefficient=1.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)

    assert (fit.image == np.full(fill_value=1.0, shape=(9, ))).all()
    assert (fit.noise_map == np.full(fill_value=2.0, shape=(9, ))).all()
    assert fit.log_evidence == pytest.approx(-37667.0303, 1e-4)
    assert fit.figure_of_merit == pytest.approx(-37667.0303, 1.0e-4)
    def test__figure_of_merit__includes_hyper_image_and_noise__matches_fit(
            self, masked_imaging_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.EllSersic(intensity=0.1))

        model = af.Collection(
            hyper_image_sky=hyper_image_sky,
            hyper_background_noise=hyper_background_noise,
            galaxies=af.Collection(lens=lens_galaxy),
        )

        analysis = al.AnalysisImaging(dataset=masked_imaging_7x7)
        instance = model.instance_from_unit_vector([])
        analysis_log_likelihood = analysis.log_likelihood_function(
            instance=instance)

        tracer = analysis.tracer_for_instance(instance=instance)
        fit = al.FitImaging(
            dataset=masked_imaging_7x7,
            tracer=tracer,
            hyper_image_sky=hyper_image_sky,
            hyper_background_noise=hyper_background_noise,
        )

        assert fit.log_likelihood == analysis_log_likelihood
예제 #8
0
    def test__figure_of_merit__matches_correct_fit_given_galaxy_profiles(
            self, imaging_7x7, mask_7x7):
        lens_galaxy = al.Galaxy(redshift=0.5,
                                light=al.lp.EllipticalSersic(intensity=0.1))

        phase_imaging_7x7 = al.PhaseImaging(
            galaxies=dict(lens=lens_galaxy),
            settings=al.SettingsPhaseImaging(
                settings_masked_imaging=al.SettingsMaskedImaging(sub_size=1)),
            search=mock.MockSearch(),
        )

        analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7,
                                                   mask=mask_7x7,
                                                   results=mock.MockResults())
        instance = phase_imaging_7x7.model.instance_from_unit_vector([])
        fit_figure_of_merit = analysis.log_likelihood_function(
            instance=instance)

        masked_imaging = al.MaskedImaging(
            imaging=imaging_7x7,
            mask=mask_7x7,
            settings=al.SettingsMaskedImaging(sub_size=1),
        )
        tracer = analysis.tracer_for_instance(instance=instance)

        fit = al.FitImaging(masked_imaging=masked_imaging, tracer=tracer)

        assert fit.log_likelihood == fit_figure_of_merit
def make_fit_imaging_generator(fit):

    imaging = make_imaging_gen(fit=fit)

    tracer = al.Tracer.from_galaxies(galaxies=fit.instance.galaxies)

    return al.FitImaging(dataset=imaging, tracer=tracer)
예제 #10
0
def test___unmasked_blurred_images(masked_imaging_7x7):

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

    blurred_images_of_planes = tracer.blurred_image_2d_list_via_convolver_from(
        grid=masked_imaging_7x7.grid,
        convolver=masked_imaging_7x7.convolver,
        blurring_grid=masked_imaging_7x7.blurring_grid,
    )

    unmasked_blurred_image = tracer.unmasked_blurred_image_2d_via_psf_from(
        grid=masked_imaging_7x7.grid, psf=masked_imaging_7x7.psf)

    assert (fit.unmasked_blurred_image == unmasked_blurred_image).all()

    unmasked_blurred_image_of_planes_list = tracer.unmasked_blurred_image_2d_list_via_psf_from(
        grid=masked_imaging_7x7.grid, psf=masked_imaging_7x7.psf)

    assert (unmasked_blurred_image_of_planes_list[0] ==
            fit.unmasked_blurred_image_of_planes_list[0]).all()
    assert (unmasked_blurred_image_of_planes_list[1] ==
            fit.unmasked_blurred_image_of_planes_list[1]).all()
예제 #11
0
def test__model_images_of_planes_list(masked_imaging_7x7):

    g0 = al.Galaxy(
        redshift=0.5,
        light_profile=al.lp.EllSersic(intensity=1.0),
        mass_profile=al.mp.SphIsothermal(einstein_radius=1.0),
    )

    g1_linear = al.Galaxy(redshift=0.75,
                          light_profile=al.lp_linear.EllSersic())

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

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

    tracer = al.Tracer.from_galaxies(
        galaxies=[g0, g1_linear, galaxy_pix_0, galaxy_pix_1])

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

    assert fit.model_images_of_planes_list[0] == pytest.approx(
        fit.galaxy_model_image_dict[g0], 1.0e-4)
    assert fit.model_images_of_planes_list[1] == pytest.approx(
        fit.galaxy_model_image_dict[g1_linear], 1.0e-4)
    assert fit.model_images_of_planes_list[2] == pytest.approx(
        fit.galaxy_model_image_dict[galaxy_pix_0] +
        fit.galaxy_model_image_dict[galaxy_pix_1],
        1.0e-4,
    )
def perform_fit_with_source_galaxy(imaging, source_galaxy):

    mask = al.Mask2D.circular_annular(
        shape_native=imaging.shape_native,
        pixel_scales=imaging.pixel_scales,
        inner_radius=0.3,
        outer_radius=2.6,
    )

    imaging = imaging.apply_mask(mask=mask)

    lens_galaxy = al.Galaxy(
        redshift=0.5,
        mass=al.mp.EllIsothermal(
            centre=(0.0, 0.0),
            einstein_radius=1.6,
            elliptical_comps=al.convert.elliptical_comps_from(
                axis_ratio=0.9, angle=45.0
            ),
        ),
        shear=al.mp.ExternalShear(elliptical_comps=(0.05, 0.05)),
    )

    tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

    return al.FitImaging(dataset=imaging, tracer=tracer)
def perform_fit_x2_lenses_with_source_galaxy_mask_and_border(
        imaging, source_galaxy, mask, settings_pixelization):

    imaging = imaging.apply_mask(mask=mask)

    lens_galaxy_0 = al.Galaxy(
        redshift=0.5,
        mass=al.mp.EllIsothermal(centre=(1.1, 0.51),
                                 elliptical_comps=(0.0, 0.15),
                                 einstein_radius=1.07),
    )

    lens_galaxy_1 = al.Galaxy(
        redshift=0.5,
        mass=al.mp.EllIsothermal(centre=(-0.20, -0.35),
                                 elliptical_comps=(0.06, 0.1053),
                                 einstein_radius=0.71),
    )

    tracer = al.Tracer.from_galaxies(
        galaxies=[lens_galaxy_0, lens_galaxy_1, source_galaxy])

    return al.FitImaging(dataset=imaging,
                         tracer=tracer,
                         settings_pixelization=settings_pixelization)
예제 #14
0
def fit_imaging_from_agg_obj(
    agg_obj: af.SearchOutput,
    settings_imaging: al.SettingsImaging = None,
    settings_pixelization: al.SettingsPixelization = None,
    settings_inversion: al.SettingsInversion = None,
) -> "al.FitImaging":
    """
    Returns a `FitImaging` object from an aggregator's `SearchOutput` class, which we call an 'agg_obj' to describe
     that it acts as the aggregator object for one result in the `Aggregator`. This uses the aggregator's generator
     outputs such that the function can use the `Aggregator`'s map function to to create a `FitImaging` generator.

     The `FitImaging` is created.

    Parameters
    ----------
    agg_obj : af.SearchOutput
        A PyAutoFit aggregator's SearchOutput object containing the generators of the results of PyAutoLens model-fits.
    """
    imaging = imaging_from_agg_obj(agg_obj=agg_obj,
                                   settings_imaging=settings_imaging)
    tracer = tracer_from_agg_obj(agg_obj=agg_obj)

    if settings_pixelization is None:
        settings_pixelization = agg_obj.settings_pixelization

    if settings_inversion is None:
        settings_inversion = agg_obj.settings_inversion

    return al.FitImaging(
        imaging=imaging,
        tracer=tracer,
        settings_pixelization=settings_pixelization,
        settings_inversion=settings_inversion,
    )
def fit_with_offset_centre(centre):

    mask = al.Mask2D.elliptical(
        shape_2d=imaging.shape_2d,
        pixel_scales=imaging.pixel_scales,
        major_axis_radius=3.0,
        axis_ratio=1.0,
        phi=0.0,
        centre=centre,
    )

    # The lines of code below do everything we're used to, that is, setup an image and its grid, mask it, trace it
    # via a tracer, setup the rectangular mapper, etc.
    lens_galaxy = al.Galaxy(
        redshift=0.5,
        mass=al.mp.EllipticalIsothermal(centre=(2.0, 2.0),
                                        einstein_radius=1.2,
                                        elliptical_comps=(0.17647, 0.0)),
    )
    source_galaxy = al.Galaxy(
        redshift=1.0,
        pixelization=al.pix.VoronoiMagnification(shape=(20, 20)),
        regularization=al.reg.Constant(coefficient=1.0),
    )

    masked_imaging = al.MaskedImaging(imaging=imaging, mask=mask)

    tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])
    fit = al.FitImaging(masked_imaging=masked_imaging, tracer=tracer)

    return fit
예제 #16
0
def perform_fit_with_source_galaxy(imaging, source_galaxy):

    mask = al.Mask.circular_annular(
        shape_2d=imaging.shape_2d,
        pixel_scales=imaging.pixel_scales,
        sub_size=2,
        inner_radius=0.3,
        outer_radius=2.6,
    )

    masked_imaging = al.MaskedImaging(
        imaging=imaging,
        mask=mask,
        settings=al.SettingsMaskedImaging(sub_size=2))

    lens_galaxy = al.Galaxy(
        redshift=0.5,
        mass=al.mp.EllipticalIsothermal(centre=(0.0, 0.0),
                                        elliptical_comps=(0.1, 0.0),
                                        einstein_radius=1.6),
    )

    tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

    return al.FitImaging(masked_imaging=masked_imaging, tracer=tracer)
예제 #17
0
def fit_imaging_from_agg_obj(
    agg_obj,
    settings_masked_imaging=None,
    settings_pixelization=None,
    settings_inversion=None,
):
    """Compute a *FitImaging* object from an aggregator's *PhaseOutput* class, which we call an 'agg_obj' to describe 
     that it acts as the aggregator object for one result in the *Aggregator*. This uses the aggregator's generator 
     outputs such that the function can use the *Aggregator*'s map function to to create a *FitImaging* generator.

     The *FitImaging* is created following the same method as the PyAutoLens *Phase* classes. 

    Parameters
    ----------
    agg_obj : af.PhaseOutput
        A PyAutoFit aggregator's PhaseOutput object containing the generators of the results of PyAutoLens model-fits.
    """
    masked_imaging = masked_imaging_from_agg_obj(
        agg_obj=agg_obj, settings_masked_imaging=settings_masked_imaging)
    tracer = tracer_from_agg_obj(agg_obj=agg_obj)

    if settings_pixelization is None:
        settings_pixelization = agg_obj.settings.settings_pixelization

    if settings_inversion is None:
        settings_inversion = agg_obj.settings.settings_inversion

    return al.FitImaging(
        masked_imaging=masked_imaging,
        tracer=tracer,
        settings_pixelization=settings_pixelization,
        settings_inversion=settings_inversion,
    )
def fit_imaging_with_lens_and_source_galaxy(imaging, lens_galaxy, source_galaxy):

    tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

    return al.FitImaging(
        dataset=imaging,
        tracer=tracer,
        settings_inversion=al.SettingsInversion(use_w_tilde=False),
    )
예제 #19
0
def make_fit_generator(agg_obj):

    samples = agg_obj.samples
    imaging = agg_obj.dataset

    tracer = al.Tracer.from_galaxies(
        galaxies=samples.max_log_likelihood_instance.galaxies)

    return al.FitImaging(imaging=imaging, tracer=tracer)
예제 #20
0
    def test__uses_hyper_fit_correctly(self, masked_imaging_7x7):

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

        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}

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

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

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

        instance.galaxies.lens.hyper_galaxy = hyper_galaxy

        analysis_log_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 = al.FitImaging(dataset=masked_imaging_7x7, tracer=tracer)

        assert (fit.tracer.galaxies[0].hyper_galaxy_image == lens_hyper_image
                ).all()
        assert analysis_log_likelihood == fit.log_likelihood
예제 #21
0
def test___stochastic_mode__gives_different_log_likelihoods(
        masked_imaging_7x7):

    pix = al.pix.VoronoiBrightnessImage(pixels=5)
    reg = al.reg.Constant(coefficient=1.0)

    g0 = al.Galaxy(
        redshift=0.5,
        pixelization=pix,
        regularization=reg,
        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),
    )

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

    fit_0 = al.FitImaging(
        dataset=masked_imaging_7x7,
        tracer=tracer,
        settings_pixelization=al.SettingsPixelization(is_stochastic=False),
    )
    fit_1 = al.FitImaging(
        dataset=masked_imaging_7x7,
        tracer=tracer,
        settings_pixelization=al.SettingsPixelization(is_stochastic=False),
    )

    assert fit_0.log_evidence == fit_1.log_evidence

    fit_0 = al.FitImaging(
        dataset=masked_imaging_7x7,
        tracer=tracer,
        settings_pixelization=al.SettingsPixelization(is_stochastic=True),
    )
    fit_1 = al.FitImaging(
        dataset=masked_imaging_7x7,
        tracer=tracer,
        settings_pixelization=al.SettingsPixelization(is_stochastic=True),
    )

    assert fit_0.log_evidence != fit_1.log_evidence
예제 #22
0
def test__preloads__blurred_image_uses_preload_when_passed(
        masked_imaging_7x7_no_blur):

    g0 = al.Galaxy(redshift=0.5,
                   light_profile=al.m.MockLightProfile(image_2d=np.ones(1)))

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

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

    assert (fit.blurred_image == np.array([1.0])).all()

    blurred_image = np.array([2.0])
    preloads = al.Preloads(blurred_image=blurred_image)

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

    assert (fit.blurred_image == np.array([2.0])).all()
예제 #23
0
def test__model_image__with_and_without_psf_blurring(
        masked_imaging_7x7_no_blur, masked_imaging_7x7):

    g0 = al.Galaxy(
        redshift=0.5,
        light_profile=al.m.MockLightProfile(image_2d_value=1.0,
                                            image_2d_first_value=2.0),
    )
    tracer = al.Tracer.from_galaxies(galaxies=[g0])

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

    assert fit.model_image.slim == pytest.approx(
        np.array([2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]), 1.0e-4)
    assert fit.log_likelihood == pytest.approx(-14.6337, 1.0e-4)

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

    assert fit.model_image.slim == pytest.approx(
        np.array([1.33, 1.16, 1.0, 1.16, 1.0, 1.0, 1.0, 1.0, 1.0]), 1.0e-1)
    assert fit.log_likelihood == pytest.approx(-14.52960, 1.0e-4)
예제 #24
0
def test__subtracted_images_of_planes_list(masked_imaging_7x7_no_blur):

    g0 = al.Galaxy(redshift=0.5,
                   light_profile=al.m.MockLightProfile(image_2d=np.ones(1)))

    g1 = al.Galaxy(redshift=0.75,
                   light_profile=al.m.MockLightProfile(image_2d=2.0 *
                                                       np.ones(1)))

    g2 = al.Galaxy(redshift=1.0,
                   light_profile=al.m.MockLightProfile(image_2d=3.0 *
                                                       np.ones(1)))

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

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

    fit.subtracted_images_of_planes_list  # Fixes error where nan appears, weird.

    assert fit.subtracted_images_of_planes_list[0].slim[0] == -4.0 or np.nan
    assert fit.subtracted_images_of_planes_list[1].slim[0] == -3.0 or np.nan
    assert fit.subtracted_images_of_planes_list[2].slim[0] == -2.0 or np.nan

    g0 = al.Galaxy(redshift=0.5,
                   light_profile=al.m.MockLightProfile(image_2d=np.ones(1)))

    g1 = al.Galaxy(redshift=1.0,
                   light_profile=al.m.MockLightProfile(image_2d=2.0 *
                                                       np.ones(1)))

    g2 = al.Galaxy(redshift=1.0,
                   light_profile=al.m.MockLightProfile(image_2d=3.0 *
                                                       np.ones(1)))

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

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

    assert fit.subtracted_images_of_planes_list[0].slim[0] == -4.0 or np.nan
    assert fit.subtracted_images_of_planes_list[1].slim[0] == -0.0 or np.nan
예제 #25
0
def perform_fit_with_lens__source_galaxy(imaging, lens_galaxy, source_galaxy):

    mask = al.Mask2D.circular_annular(
        shape_native=imaging.shape_native,
        pixel_scales=imaging.pixel_scales,
        sub_size=1,
        inner_radius=0.5,
        outer_radius=2.2,
    )

    imaging = imaging.apply_mask(mask=mask)

    tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

    return al.FitImaging(dataset=imaging, tracer=tracer)
def make_fit_imaging_generator(fit):

    imaging = make_imaging_gen(fit=fit)

    settings_pixelization = fit.value(name="settings_pixelization")
    settings_inversion = fit.value(name="settings_inversion")

    tracer = al.Tracer.from_galaxies(galaxies=fit.instance.galaxies)

    return al.FitImaging(
        dataset=imaging,
        tracer=tracer,
        settings_pixelization=settings_pixelization,
        settings_inversion=settings_inversion,
    )
def fit_imaging_with_source_galaxy(imaging, source_galaxy):

    lens_galaxy = al.Galaxy(
        redshift=0.5,
        mass=al.mp.EllIsothermal(
            centre=(0.0, 0.0),
            einstein_radius=1.6,
            elliptical_comps=al.convert.elliptical_comps_from(axis_ratio=0.9,
                                                              angle=45.0),
        ),
        shear=al.mp.ExternalShear(elliptical_comps=(0.05, 0.05)),
    )

    tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

    return al.FitImaging(dataset=imaging, tracer=tracer)
예제 #28
0
def test__figure_of_merit__matches_correct_fit_given_galaxy_profiles(
        masked_imaging_7x7):
    lens = al.Galaxy(redshift=0.5, light=al.lp.EllSersic(intensity=0.1))

    model = af.Collection(galaxies=af.Collection(lens=lens))

    analysis = al.AnalysisImaging(dataset=masked_imaging_7x7)
    instance = model.instance_from_unit_vector([])
    analysis_log_likelihood = analysis.log_likelihood_function(
        instance=instance)

    tracer = analysis.tracer_via_instance_from(instance=instance)

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

    assert fit.log_likelihood == analysis_log_likelihood
예제 #29
0
def make_fit_generator(agg_obj):

    samples = agg_obj.samples
    imaging = agg_obj.dataset
    settings_pixelization = agg_obj.settings_pixelization
    settings_inversion = agg_obj.settings_inversion

    tracer = al.Tracer.from_galaxies(
        galaxies=samples.max_log_likelihood_instance.galaxies)

    return al.FitImaging(
        imaging=imaging,
        tracer=tracer,
        settings_pixelization=settings_pixelization,
        settings_inversion=settings_inversion,
    )
예제 #30
0
def perform_fit_with_lens__source_galaxy(imaging, lens_galaxy, source_galaxy):

    mask = al.Mask.circular_annular(
        shape_2d=imaging.shape_2d,
        pixel_scales=imaging.pixel_scales,
        sub_size=2,
        inner_radius=0.5,
        outer_radius=2.2,
    )

    masked_imaging = al.MaskedImaging(
        imaging=imaging, mask=mask, settings=al.SettingsMaskedImaging(sub_size=2)
    )

    tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

    return al.FitImaging(masked_imaging=masked_imaging, tracer=tracer)