Example #1
0
def galaxy_fit_phase():

    pixel_scales = 0.1
    image_shape = (150, 150)

    integration_util.reset_paths(test_name=test_name, output_path=output_path)

    grid = al.grid.uniform(shape_2d=image_shape, pixel_scales=pixel_scales, sub_size=4)

    galaxy = al.Galaxy(
        redshift=0.5,
        light=al.lp.SphericalExponential(
            centre=(0.0, 0.0), intensity=1.0, effective_radius=0.5
        ),
    )

    image = galaxy.profile_image_from_grid(galaxies=[galaxy], grid=grid)

    noise_map = al.array.manual_2d(
        array=np.ones(image.shape), pixel_scales=pixel_scales
    )

    data = al.GalaxyData(image=image, noise_map=noise_map, pixel_scales=pixel_scales)

    phase1 = al.PhaseGalaxy(
        phase_name=test_name + "/",
        galaxies=dict(
            gal=al.GalaxyModel(redshift=0.5, light=al.lp.SphericalExponential)
        ),
        use_image=True,
        sub_size=4,
        optimizer_class=af.MultiNest,
    )

    phase1.run(galaxy_data=[data])
    def test__no_use_method__raises_exception(self, image_7x7, sub_mask_7x7):

        gal_data_7x7 = al.GalaxyData(image=image_7x7,
                                     noise_map=2.0 * np.ones((7, 7)),
                                     pixel_scale=3.0)

        with pytest.raises(exc.GalaxyException):
            al.GalaxyFitData(galaxy_data=gal_data_7x7, mask=sub_mask_7x7)
Example #3
0
def galaxy_fit_phase():

    pixel_scale = 0.1
    image_shape = (150, 150)

    integration_util.reset_paths(test_name=test_name, output_path=output_path)

    grid = al.Grid.from_shape_pixel_scale_and_sub_size(shape=image_shape,
                                                       pixel_scale=pixel_scale,
                                                       sub_size=4)

    galaxy = al.Galaxy(
        redshift=0.5,
        mass=al.mass_profiles.SphericalIsothermal(centre=(0.0, 0.0),
                                                  einstein_radius=1.0),
    )

    deflections = galaxy.deflections_from_grid(galaxies=[galaxy],
                                               grid=grid,
                                               return_in_2d=True,
                                               bypass_decorator=False)

    noise_map = al.ScaledSquarePixelArray(array=np.ones(deflections[:,
                                                                    0].shape),
                                          pixel_scale=pixel_scale)

    data_y = al.GalaxyData(image=deflections[:, 0],
                           noise_map=noise_map,
                           pixel_scale=pixel_scale)
    data_x = al.GalaxyData(image=deflections[:, 1],
                           noise_map=noise_map,
                           pixel_scale=pixel_scale)

    phase1 = al.PhaseGalaxy(
        phase_name=test_name + "/",
        galaxies=dict(gal=al.GalaxyModel(
            redshift=0.5, light=al.mass_profiles.SphericalIsothermal)),
        use_deflections=True,
        sub_size=4,
        optimizer_class=af.MultiNest,
    )

    phase1.run(galaxy_data=[data_y, data_x])
Example #4
0
    def test__1x1_image__light_profile_fits_data_perfectly__lh_is_noise(self):
        image = al.ScaledSquarePixelArray(array=np.ones((3, 3)),
                                          pixel_scale=1.0)

        noise_map = al.ScaledSquarePixelArray(array=np.ones((3, 3)),
                                              pixel_scale=1.0)

        galaxy_data = al.GalaxyData(image=image,
                                    noise_map=noise_map,
                                    pixel_scale=3.0)

        mask = al.Mask(
            array=np.array([[True, True, True], [True, False, True],
                            [True, True, True]]),
            pixel_scale=1.0,
            sub_size=1,
        )
        g0 = MockGalaxy(value=1.0)

        galaxy_fit_data = al.GalaxyFitData(galaxy_data=galaxy_data,
                                           mask=mask,
                                           use_image=True)
        fit = al.GalaxyFit(galaxy_data=galaxy_fit_data, model_galaxies=[g0])
        assert fit.model_galaxies == [g0]
        assert fit.likelihood == -0.5 * np.log(2 * np.pi * 1.0)

        galaxy_fit_data = al.GalaxyFitData(galaxy_data=galaxy_data,
                                           mask=mask,
                                           use_convergence=True)
        fit = al.GalaxyFit(galaxy_data=galaxy_fit_data, model_galaxies=[g0])
        assert fit.model_galaxies == [g0]
        assert fit.likelihood == -0.5 * np.log(2 * np.pi * 1.0)

        galaxy_fit_data = al.GalaxyFitData(galaxy_data=galaxy_data,
                                           mask=mask,
                                           use_potential=True)
        fit = al.GalaxyFit(galaxy_data=galaxy_fit_data, model_galaxies=[g0])
        assert fit.model_galaxies == [g0]
        assert fit.likelihood == -0.5 * np.log(2 * np.pi * 1.0)

        galaxy_fit_data = al.GalaxyFitData(galaxy_data=galaxy_data,
                                           mask=mask,
                                           use_deflections_y=True)
        fit = al.GalaxyFit(galaxy_data=galaxy_fit_data, model_galaxies=[g0])
        assert fit.model_galaxies == [g0]
        assert fit.likelihood == -0.5 * np.log(2 * np.pi * 1.0)

        galaxy_fit_data = al.GalaxyFitData(galaxy_data=galaxy_data,
                                           mask=mask,
                                           use_deflections_x=True)
        fit = al.GalaxyFit(galaxy_data=galaxy_fit_data, model_galaxies=[g0])
        assert fit.model_galaxies == [g0]
        assert fit.likelihood == -0.5 * np.log(2 * np.pi * 1.0)
    def test__multiple_use_methods__raises_exception(self, image_7x7,
                                                     sub_mask_7x7):

        gal_data_7x7 = al.GalaxyData(image=image_7x7,
                                     noise_map=2.0 * np.ones((7, 7)),
                                     pixel_scale=3.0)

        with pytest.raises(exc.GalaxyException):
            al.GalaxyFitData(
                galaxy_data=gal_data_7x7,
                mask=sub_mask_7x7,
                use_image=True,
                use_convergence=True,
            )

        with pytest.raises(exc.GalaxyException):
            al.GalaxyFitData(
                galaxy_data=gal_data_7x7,
                mask=sub_mask_7x7,
                use_image=True,
                use_potential=True,
            )

        with pytest.raises(exc.GalaxyException):
            al.GalaxyFitData(
                galaxy_data=gal_data_7x7,
                mask=sub_mask_7x7,
                use_image=True,
                use_deflections_y=True,
            )

        with pytest.raises(exc.GalaxyException):
            al.GalaxyFitData(
                galaxy_data=gal_data_7x7,
                mask=sub_mask_7x7,
                use_image=True,
                use_convergence=True,
                use_potential=True,
            )

        with pytest.raises(exc.GalaxyException):
            al.GalaxyFitData(
                galaxy_data=gal_data_7x7,
                mask=sub_mask_7x7,
                use_image=True,
                use_convergence=True,
                use_potential=True,
                use_deflections_x=True,
            )
Example #6
0
def galaxy_fit_phase():

    pixel_scale = 0.1
    image_shape = (150, 150)

    integration_util.reset_paths(test_name=test_name, output_path=output_path)

    grid = al.Grid.from_shape_pixel_scale_and_sub_size(shape=image_shape,
                                                       pixel_scale=pixel_scale,
                                                       sub_size=4)

    galaxy = al.Galaxy(
        redshift=0.5,
        light=al.light_profiles.SphericalExponential(centre=(0.0, 0.0),
                                                     intensity=1.0,
                                                     effective_radius=0.5),
    )

    image = galaxy.profile_image_from_grid(galaxies=[galaxy],
                                           grid=grid,
                                           return_in_2d=True,
                                           bypass_decorator=False)

    noise_map = al.ScaledSquarePixelArray(array=np.ones(image.shape),
                                          pixel_scale=pixel_scale)

    data = al.GalaxyData(image=image,
                         noise_map=noise_map,
                         pixel_scale=pixel_scale)

    phase1 = al.PhaseGalaxy(
        phase_name=test_name + "/",
        galaxies=dict(gal=al.GalaxyModel(
            redshift=0.5, light=al.light_profiles.SphericalExponential)),
        use_image=True,
        sub_size=4,
        optimizer_class=af.MultiNest,
    )

    phase1.run(galaxy_data=[data])
    def test__pixel_scale_interpolation_grid(self, image_7x7, sub_mask_7x7):

        noise_map = al.ScaledSquarePixelArray(array=2.0 * np.ones((7, 7)),
                                              pixel_scale=3.0)
        gal_data_7x7 = al.GalaxyData(image=image_7x7,
                                     noise_map=noise_map,
                                     pixel_scale=3.0)
        gal_data_7x7 = al.GalaxyFitData(
            galaxy_data=gal_data_7x7,
            mask=sub_mask_7x7,
            pixel_scale_interpolation_grid=1.0,
            use_image=True,
        )

        grid = al.Grid.from_mask(mask=sub_mask_7x7)
        new_grid = grid.new_grid_with_interpolator(
            pixel_scale_interpolation_grid=1.0)
        assert (gal_data_7x7.grid == new_grid).all()
        assert (gal_data_7x7.grid.interpolator.vtx == new_grid.interpolator.vtx
                ).all()
        assert (gal_data_7x7.grid.interpolator.wts == new_grid.interpolator.wts
                ).all()
Example #8
0
    def test__1x2_image__noise_not_1__alls_correct(self):
        image = al.ScaledSquarePixelArray(array=5.0 * np.ones((3, 4)),
                                          pixel_scale=1.0)
        image[1, 2] = 4.0

        noise_map = al.ScaledSquarePixelArray(array=2.0 * np.ones((3, 4)),
                                              pixel_scale=1.0)

        galaxy_data = al.GalaxyData(image=image,
                                    noise_map=noise_map,
                                    pixel_scale=3.0)

        mask = al.Mask(
            array=np.array([
                [True, True, True, True],
                [True, False, False, True],
                [True, True, True, True],
            ]),
            pixel_scale=1.0,
            sub_size=1,
        )

        g0 = MockGalaxy(value=1.0, shape=2)

        galaxy_fit_data = al.GalaxyFitData(galaxy_data=galaxy_data,
                                           mask=mask,
                                           use_image=True)
        fit = al.GalaxyFit(galaxy_data=galaxy_fit_data, model_galaxies=[g0])

        assert fit.model_galaxies == [g0]
        assert fit.chi_squared == (25.0 / 4.0)
        assert fit.reduced_chi_squared == (25.0 / 4.0) / 2.0
        assert fit.likelihood == -0.5 * (
            (25.0 / 4.0) + 2.0 * np.log(2 * np.pi * 2.0**2))

        galaxy_fit_data = al.GalaxyFitData(galaxy_data=galaxy_data,
                                           mask=mask,
                                           use_convergence=True)
        fit = al.GalaxyFit(galaxy_data=galaxy_fit_data, model_galaxies=[g0])
        assert fit.model_galaxies == [g0]
        assert fit.chi_squared == (25.0 / 4.0)
        assert fit.reduced_chi_squared == (25.0 / 4.0) / 2.0
        assert fit.likelihood == -0.5 * (
            (25.0 / 4.0) + 2.0 * np.log(2 * np.pi * 2.0**2))

        galaxy_fit_data = al.GalaxyFitData(galaxy_data=galaxy_data,
                                           mask=mask,
                                           use_potential=True)
        fit = al.GalaxyFit(galaxy_data=galaxy_fit_data, model_galaxies=[g0])
        assert fit.model_galaxies == [g0]
        assert fit.chi_squared == (25.0 / 4.0)
        assert fit.reduced_chi_squared == (25.0 / 4.0) / 2.0
        assert fit.likelihood == -0.5 * (
            (25.0 / 4.0) + 2.0 * np.log(2 * np.pi * 2.0**2))

        galaxy_fit_data = al.GalaxyFitData(galaxy_data=galaxy_data,
                                           mask=mask,
                                           use_deflections_y=True)
        fit = al.GalaxyFit(galaxy_data=galaxy_fit_data, model_galaxies=[g0])
        assert fit.chi_squared == (25.0 / 4.0)
        assert fit.reduced_chi_squared == (25.0 / 4.0) / 2.0
        assert fit.likelihood == -0.5 * (
            (25.0 / 4.0) + 2.0 * np.log(2 * np.pi * 2.0**2))

        galaxy_fit_data = al.GalaxyFitData(galaxy_data=galaxy_data,
                                           mask=mask,
                                           use_deflections_x=True)
        fit = al.GalaxyFit(galaxy_data=galaxy_fit_data, model_galaxies=[g0])
        assert fit.chi_squared == (25.0 / 4.0)
        assert fit.reduced_chi_squared == (25.0 / 4.0) / 2.0
        assert fit.likelihood == -0.5 * (
            (25.0 / 4.0) + 2.0 * np.log(2 * np.pi * 2.0**2))
Example #9
0
def make_gal_data_7x7(image_7x7, noise_map_7x7):
    return al.GalaxyData(image=image_7x7,
                         noise_map=noise_map_7x7,
                         pixel_scale=image_7x7.pixel_scale)