def fit_with_offset_centre(centre):

    mask = al.mask.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,
                                        axis_ratio=0.7,
                                        phi=45.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.masked_imaging(imaging=imaging, mask=mask)

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

    return fit
Ejemplo n.º 2
0
    def test__masked_imaging_7x7_with_signal_to_noise_limit(
            self, imaging_7x7, sub_mask_7x7):

        masked_imaging_7x7 = al.masked_imaging(imaging=imaging_7x7,
                                               mask=sub_mask_7x7)

        masked_imaging_snr_limit = masked_imaging_7x7.signal_to_noise_limited_from_signal_to_noise_limit(
            signal_to_noise_limit=0.25)

        assert (masked_imaging_snr_limit.psf.in_2d == np.ones((3, 3))).all()
        assert masked_imaging_snr_limit.psf_shape_2d == (3, 3)

        assert (masked_imaging_snr_limit.image.in_1d == np.ones(9)).all()
        assert (masked_imaging_snr_limit.noise_map.in_1d == 4.0 *
                np.ones(9)).all()

        assert (masked_imaging_snr_limit.noise_map.in_2d == np.array([
            [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 4.0, 4.0, 4.0, 0.0, 0.0],
            [0.0, 0.0, 4.0, 4.0, 4.0, 0.0, 0.0],
            [0.0, 0.0, 4.0, 4.0, 4.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
        ])).all()
Ejemplo n.º 3
0
    def test__masked_imaging_is_binned_up(self, imaging_7x7, mask_7x7_1_pix):
        binned_up_imaging = imaging_7x7.binned_from_bin_up_factor(
            bin_up_factor=2)

        binned_up_mask = mask_7x7_1_pix.mapping.binned_mask_from_bin_up_factor(
            bin_up_factor=2)

        phase_imaging_7x7 = al.PhaseImaging(phase_name="phase_imaging_7x7",
                                            bin_up_factor=2)

        analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7,
                                                   mask=mask_7x7_1_pix)
        assert (analysis.masked_dataset.image.in_2d ==
                binned_up_imaging.image.in_2d *
                np.invert(binned_up_mask)).all()
        assert (analysis.masked_dataset.psf == binned_up_imaging.psf).all()
        assert (analysis.masked_dataset.noise_map.in_2d ==
                binned_up_imaging.noise_map.in_2d *
                np.invert(binned_up_mask)).all()

        assert (analysis.masked_dataset.mask == binned_up_mask).all()

        masked_imaging = al.masked_imaging(imaging=imaging_7x7,
                                           mask=mask_7x7_1_pix)

        binned_up_masked_imaging = masked_imaging.binned_from_bin_up_factor(
            bin_up_factor=2)

        assert (analysis.masked_dataset.image.in_2d ==
                binned_up_masked_imaging.image.in_2d *
                np.invert(binned_up_mask)).all()
        assert (
            analysis.masked_dataset.psf == binned_up_masked_imaging.psf).all()
        assert (analysis.masked_dataset.noise_map.in_2d ==
                binned_up_masked_imaging.noise_map.in_2d *
                np.invert(binned_up_mask)).all()

        assert (analysis.masked_dataset.mask == binned_up_masked_imaging.mask
                ).all()

        assert (analysis.masked_dataset.image.in_1d ==
                binned_up_masked_imaging.image.in_1d).all()
        assert (analysis.masked_dataset.noise_map.in_1d ==
                binned_up_masked_imaging.noise_map.in_1d).all()
Ejemplo n.º 4
0
    def test__masked_imaging_6x6_with_binned_up_imaging(
            self, imaging_6x6, mask_6x6):

        masked_imaging_6x6 = al.masked_imaging(imaging=imaging_6x6,
                                               mask=mask_6x6)

        binned_mask = np.array([[True, True, True], [True, False, True],
                                [True, True, True]])

        masked_imaging_3x3 = masked_imaging_6x6.binned_from_bin_up_factor(
            bin_up_factor=2)

        assert (masked_imaging_3x3.image.in_2d == np.ones(
            (3, 3)) * np.invert(binned_mask)).all()
        assert (masked_imaging_3x3.psf.in_2d == np.ones((3, 3))).all()
        assert (masked_imaging_3x3.noise_map.in_2d == np.ones(
            (3, 3)) * np.invert(binned_mask)).all()

        assert (masked_imaging_3x3.mask == binned_mask).all()

        assert (masked_imaging_3x3.image.in_1d == np.ones((1))).all()
        assert (masked_imaging_3x3.noise_map.in_1d == np.ones((1))).all()
Ejemplo n.º 5
0
    def test__inheritance_from_autoarray(self, imaging_7x7, sub_mask_7x7,
                                         blurring_grid_7x7):

        masked_imaging_7x7 = al.masked_imaging(
            imaging=imaging_7x7,
            mask=sub_mask_7x7,
            pixel_scale_interpolation_grid=1.0,
            psf_shape_2d=(3, 3),
            inversion_pixel_limit=20.0,
            inversion_uses_border=False,
            preload_sparse_grids_of_planes=1,
        )

        assert masked_imaging_7x7.inversion_pixel_limit == 20.0
        assert masked_imaging_7x7.inversion_uses_border == False
        assert masked_imaging_7x7.preload_sparse_grids_of_planes == 1

        grid = al.masked_grid.from_mask(mask=sub_mask_7x7)
        new_grid = grid.new_grid_with_interpolator(
            pixel_scale_interpolation_grid=1.0)

        assert (masked_imaging_7x7.grid == new_grid).all()
        assert (masked_imaging_7x7.grid.interpolator.vtx ==
                new_grid.interpolator.vtx).all()
        assert (masked_imaging_7x7.grid.interpolator.wts ==
                new_grid.interpolator.wts).all()

        blurring_grid = grid.blurring_grid_from_kernel_shape(
            kernel_shape_2d=(3, 3))
        new_blurring_grid = blurring_grid.new_grid_with_interpolator(
            pixel_scale_interpolation_grid=1.0)

        assert (
            masked_imaging_7x7.blurring_grid.in_1d == blurring_grid_7x7).all()
        assert (masked_imaging_7x7.blurring_grid == new_blurring_grid).all()
        assert (masked_imaging_7x7.blurring_grid.interpolator.vtx ==
                new_blurring_grid.interpolator.vtx).all()
        assert (masked_imaging_7x7.blurring_grid.interpolator.wts ==
                new_blurring_grid.interpolator.wts).all()
Ejemplo n.º 6
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=[lens_galaxy],
            cosmology=cosmo.FLRW,
            sub_size=1,
            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([])
        fit_figure_of_merit = analysis.fit(instance=instance)

        masked_imaging = al.masked_imaging(imaging=imaging_7x7, mask=mask_7x7)
        tracer = analysis.tracer_for_instance(instance=instance)

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

        assert fit.likelihood == fit_figure_of_merit
Ejemplo n.º 7
0
    def test__figure_of_merit__includes_hyper_image_and_noise__matches_fit(
            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=[lens_galaxy],
            hyper_image_sky=hyper_image_sky,
            hyper_background_noise=hyper_background_noise,
            cosmology=cosmo.FLRW,
            sub_size=4,
            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([])
        fit_figure_of_merit = analysis.fit(instance=instance)

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

        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,
        )

        assert fit.likelihood == fit_figure_of_merit
Ejemplo n.º 8
0
    def test__methods_for_new_data_pass_lensing_only_attributes(
            self, imaging_7x7, sub_mask_7x7):

        masked_imaging_7x7 = al.masked_imaging(
            imaging=imaging_7x7,
            mask=sub_mask_7x7,
            positions=[1],
            positions_threshold=2,
            preload_sparse_grids_of_planes=3,
        )

        masked_imaging_new = masked_imaging_7x7.binned_from_bin_up_factor(
            bin_up_factor=2)

        assert masked_imaging_new.positions == [1]
        assert masked_imaging_new.positions_threshold == 2
        assert masked_imaging_new.preload_sparse_grids_of_planes == 3

        masked_imaging_new = masked_imaging_7x7.signal_to_noise_limited_from_signal_to_noise_limit(
            signal_to_noise_limit=0.25)

        assert masked_imaging_new.positions == [1]
        assert masked_imaging_new.positions_threshold == 2
        assert masked_imaging_new.preload_sparse_grids_of_planes == 3
Ejemplo n.º 9
0
# In this tutorial, we'll introduce a new pixelization, called an adaptive-pixelization. This pixelization doesn't use
# uniform grid of rectangular pixels, but instead uses ir'Voronoi' pixels. So, why would we want to do that?
# Lets take another look at the rectangular grid, and think about its weakness.

# Lets quickly remind ourselves of the image, and the 3.0" circular mask we'll use to mask it.
imaging = simulate_util.load_test_imaging(
    data_type="lens_light_dev_vaucouleurs", data_resolution="lsst")
mask = al.mask.circular(
    shape_2d=imaging.shape,
    pixel_scales=imaging.pixel_scales,
    radius=3.0,
    centre=(4.0, 4.0),
)

# 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,
    bulge=al.lp.EllipticalDevVaucouleurs(centre=(0.0, 0.0),
                                         axis_ratio=0.9,
                                         phi=45.0,
                                         intensity=0.1,
                                         effective_radius=1.0),
)

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

tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy])
fit = al.fit(masked_dataset=masked_imaging, tracer=tracer)
aplt.fit_imaging.subplot_fit_imaging(fit=fit, mask=True)
Ejemplo n.º 10
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
def test__simulate_imaging_data_and_fit__include_psf_blurring__chi_squared_is_0__noise_normalization_correct(
):

    psf = al.kernel.from_gaussian(shape_2d=(3, 3),
                                  pixel_scales=0.2,
                                  sigma=0.75)

    grid = al.grid.uniform(shape_2d=(11, 11), pixel_scales=0.2, sub_size=1)

    lens_galaxy = al.Galaxy(
        redshift=0.5,
        light=al.lp.EllipticalSersic(centre=(0.1, 0.1), intensity=0.1),
        mass=al.mp.EllipticalIsothermal(centre=(0.1, 0.1),
                                        einstein_radius=1.8),
    )
    source_galaxy = al.Galaxy(
        redshift=1.0,
        light=al.lp.EllipticalExponential(centre=(0.1, 0.1), intensity=0.5),
    )
    tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

    imaging_simulator = simulator.ImagingSimulator(
        shape_2d=(11, 11),
        pixel_scales=0.2,
        sub_size=1,
        exposure_time=300.0,
        psf=psf,
        background_level=0.0,
        add_noise=False,
    )

    imaging_simulated = imaging_simulator.from_image(
        image=tracer.padded_profile_image_from_grid_and_psf_shape(
            grid=grid, psf_shape_2d=psf.shape_2d))
    imaging_simulated.noise_map = al.array.ones(
        shape_2d=imaging_simulated.image.shape_2d)

    path = "{}/data_temp/simulate_and_fit".format(
        os.path.dirname(os.path.realpath(
            __file__)))  # Setup path so we can output the simulated image.

    try:
        shutil.rmtree(path)
    except FileNotFoundError:
        pass

    if os.path.exists(path) is False:
        os.makedirs(path)

    imaging_simulated.output_to_fits(
        image_path=path + "/image.fits",
        noise_map_path=path + "/noise_map.fits",
        psf_path=path + "/psf.fits",
    )

    imaging = al.imaging.from_fits(
        image_path=path + "/image.fits",
        noise_map_path=path + "/noise_map.fits",
        psf_path=path + "/psf.fits",
        pixel_scales=0.2,
    )

    mask = al.mask.circular(shape_2d=imaging.image.shape_2d,
                            pixel_scales=0.2,
                            sub_size=1,
                            radius=0.8)

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

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

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

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

    path = "{}/data_temp".format(os.path.dirname(os.path.realpath(
        __file__)))  # Setup path so we can output the simulated image.

    if os.path.exists(path) == True:
        shutil.rmtree(path)