Example #1
0
    def test__image_2d_from(self, sub_grid_2d_7x7, lp_0, gal_x1_lp, lp_1, gal_x2_lp):
        galaxy = ag.Galaxy(redshift=0.5)

        image = galaxy.image_2d_from(grid=sub_grid_2d_7x7)

        assert (image == np.zeros(shape=sub_grid_2d_7x7.sub_shape_slim)).all()

        lp_image = lp_0.image_2d_from(grid=np.array([[1.05, -0.55]]))

        gal_image = gal_x1_lp.image_2d_from(grid=np.array([[1.05, -0.55]]))

        assert lp_image == gal_image

        lp_image = lp_0.image_2d_from(grid=np.array([[1.05, -0.55]]))
        lp_image += lp_1.image_2d_from(grid=np.array([[1.05, -0.55]]))

        gal_image = gal_x2_lp.image_2d_from(grid=np.array([[1.05, -0.55]]))

        assert lp_image == gal_image

        lp_image = lp_0.image_2d_from(grid=ag.Grid2DIrregular([(1.05, -0.55)]))

        gal_image = gal_x1_lp.image_2d_from(grid=ag.Grid2DIrregular([(1.05, -0.55)]))

        assert lp_image.in_list[0] == gal_image.in_list[0]
Example #2
0
def test__grid_2d_irrergular__moves_radial_coordinates__does_not_double_transform():

    grid_2d_irregular = ag.Grid2DIrregular(grid=[[0.0, 0.0]])
    grid_2d_irregular_offset = ag.Grid2DIrregular(grid=[[0.0001, 0.0001]])

    isothermal = ag.mp.EllIsothermal(centre=(0.0, 0.0), einstein_radius=1.0)

    convergence_1 = isothermal.convergence_2d_from(grid=grid_2d_irregular)
    convergence_0 = isothermal.convergence_2d_from(grid=grid_2d_irregular_offset)

    assert convergence_0 == pytest.approx(convergence_1, 1.0e-8)

    grid_2d_irregular = ag.Grid2DIrregular(grid=[[0.5, 0.5]])
    grid_2d_irregular_offset = ag.Grid2DIrregular(grid=[[0.5001, 0.5001]])

    isothermal = ag.mp.EllIsothermal(centre=(0.0, 0.0), einstein_radius=1.0)

    convergence_1 = isothermal.convergence_2d_from(grid=grid_2d_irregular)
    convergence_0 = isothermal.convergence_2d_from(grid=grid_2d_irregular_offset)

    assert convergence_0 != pytest.approx(convergence_1, 1.0e-8)

    isothermal = ag.mp.EllIsothermal(centre=(0.5, 0.5), einstein_radius=1.0)

    convergence_1 = isothermal.convergence_2d_from(grid=grid_2d_irregular)
    convergence_0 = isothermal.convergence_2d_from(grid=grid_2d_irregular_offset)

    assert convergence_0 == pytest.approx(convergence_1, 1.0e-8)
Example #3
0
    def test__hessian_from_grid(self):

        sie = MockEllIsothermal(centre=(0.0, 0.0),
                                elliptical_comps=(0.0, -0.111111),
                                einstein_radius=2.0)

        grid = ag.Grid2DIrregular(grid=[(0.5, 0.5), (1.0, 1.0)])

        hessian_yy, hessian_xy, hessian_yx, hessian_xx = sie.hessian_from_grid(
            grid=grid)

        print(hessian_yy, hessian_xy, hessian_yx, hessian_xx)

        assert hessian_yy == pytest.approx(np.array([1.3883822, 0.694127]),
                                           1.0e-4)
        assert hessian_xy == pytest.approx(np.array([-1.388124, -0.694094]),
                                           1.0e-4)
        assert hessian_yx == pytest.approx(np.array([-1.388165, -0.694099]),
                                           1.0e-4)
        assert hessian_xx == pytest.approx(np.array([1.3883824, 0.694127]),
                                           1.0e-4)

        grid = ag.Grid2DIrregular(grid=[(1.0, 0.0), (0.0, 1.0)])

        hessian_yy, hessian_xy, hessian_yx, hessian_xx = sie.hessian_from_grid(
            grid=grid)

        assert hessian_yy == pytest.approx(np.array([0.0, 1.777699]), 1.0e-4)
        assert hessian_xy == pytest.approx(np.array([0.0, 0.0]), 1.0e-4)
        assert hessian_yx == pytest.approx(np.array([0.0, 0.0]), 1.0e-4)
        assert hessian_xx == pytest.approx(np.array([2.22209, 0.0]), 1.0e-4)
Example #4
0
    def test__compare_sis_deflection_magnitudes_to_known_values(self):
        sis = MockSphIsothermal(centre=(0.0, 0.0), einstein_radius=1.0)

        grid = ag.Grid2DIrregular([(1.0, 0.0), (0.0, 1.0)])

        deflection_magnitudes = sis.deflection_magnitudes_from_grid(grid=grid)

        assert deflection_magnitudes == pytest.approx(np.array([1.0, 1.0]),
                                                      1.0e-4)

        sis = MockSphIsothermal(centre=(0.0, 0.0), einstein_radius=2.0)

        grid = ag.Grid2DIrregular([(2.0, 0.0), (0.0, 2.0)])

        deflection_magnitudes = sis.deflection_magnitudes_from_grid(grid=grid)

        assert deflection_magnitudes == pytest.approx(np.array([2.0, 2.0]),
                                                      1.0e-4)

        grid = ag.Grid2D.uniform(shape_native=(5, 5),
                                 pixel_scales=0.1,
                                 sub_size=1)

        deflections = sis.deflections_2d_from_grid(grid=grid)
        magitudes_manual = np.sqrt(
            np.square(deflections[:, 0]) + np.square(deflections[:, 1]))

        deflection_magnitudes = sis.deflection_magnitudes_from_grid(grid=grid)

        assert deflection_magnitudes == pytest.approx(magitudes_manual, 1.0e-4)
Example #5
0
    def test__potential_2d_from__no_mass_profile_list__potential_returned_as_0s_of_shape_grid(
        self, sub_grid_2d_7x7, mp_0, gal_x1_mp, mp_1, gal_x2_mp
    ):

        grid = ag.Grid2D.manual_native(grid=[[[1.05, -0.55]]], pixel_scales=1.0)

        galaxy = ag.Galaxy(redshift=0.5)

        potential = galaxy.potential_2d_from(grid=sub_grid_2d_7x7)

        assert (potential.slim == np.zeros(shape=sub_grid_2d_7x7.sub_shape_slim)).all()

        mp_potential = mp_0.potential_2d_from(grid=grid)

        gal_potential = gal_x1_mp.potential_2d_from(grid=np.array([[1.05, -0.55]]))

        assert mp_potential == gal_potential

        mp_potential = mp_0.potential_2d_from(grid=grid)
        mp_potential += mp_1.potential_2d_from(grid=grid)

        gal_potential = gal_x2_mp.potential_2d_from(grid=grid)

        assert mp_potential == gal_potential

        grid = ag.Grid2DIrregular([(1.05, -0.55)])

        mp_potential = mp_0.potential_2d_from(grid=grid)

        gal_potential = gal_x1_mp.potential_2d_from(grid=grid)

        assert mp_potential == gal_potential

        assert mp_potential.in_list[0] == gal_potential.in_list[0]
Example #6
0
    def test__shear_via_hessian_from_grid(self):

        buffer = 0.00001
        grid = ag.Grid2DIrregular(
            grid=[(1.075, -0.125), (-0.875, -0.075), (-0.925,
                                                      -0.075), (0.075, 0.925)])

        sis = ag.mp.EllIsothermal(centre=(0.0, 0.0),
                                  elliptical_comps=(0.001, 0.001),
                                  einstein_radius=1.0)

        shear = sis.shear_via_hessian_from_grid(grid=grid, buffer=buffer)

        assert shear.in_list[0] == pytest.approx(0.461447, 1.0e-4)
        assert shear.in_list[1] == pytest.approx(0.568875, 1.0e-4)
        assert shear.in_list[2] == pytest.approx(0.538326, 1.0e-4)
        assert shear.in_list[3] == pytest.approx(0.539390, 1.0e-4)

        sis = ag.mp.EllIsothermal(centre=(0.2, 0.1),
                                  elliptical_comps=(0.3, 0.4),
                                  einstein_radius=1.5)

        shear = sis.shear_2d_from_grid(grid=grid)
        print((shear[:, 0]**2 + shear[:, 1]**2)**0.5)

        shear = sis.shear_via_hessian_from_grid(grid=grid, buffer=buffer)

        assert shear.in_list[0] == pytest.approx(0.41597, 1.0e-4)
        assert shear.in_list[1] == pytest.approx(0.38299, 1.0e-4)
        assert shear.in_list[2] == pytest.approx(0.36522, 1.0e-4)
        assert shear.in_list[3] == pytest.approx(0.82750, 1.0e-4)
Example #7
0
    def test__convergence_via_hessian_from_grid(self):

        buffer = 0.0001
        grid = ag.Grid2DIrregular(
            grid=[(1.075, -0.125), (-0.875, -0.075), (-0.925,
                                                      -0.075), (0.075, 0.925)])

        sis = MockEllIsothermal(centre=(0.0, 0.0),
                                elliptical_comps=(0.001, 0.001),
                                einstein_radius=1.0)

        convergence = sis.convergence_via_hessian_from_grid(grid=grid,
                                                            buffer=buffer)

        assert convergence.in_list[0] == pytest.approx(0.461447, 1.0e-4)
        assert convergence.in_list[1] == pytest.approx(0.568875, 1.0e-4)
        assert convergence.in_list[2] == pytest.approx(0.538326, 1.0e-4)
        assert convergence.in_list[3] == pytest.approx(0.539390, 1.0e-4)

        sis = ag.mp.EllIsothermal(centre=(0.0, 0.0),
                                  elliptical_comps=(0.3, 0.4),
                                  einstein_radius=1.5)

        convergence = sis.convergence_via_hessian_from_grid(grid=grid,
                                                            buffer=buffer)

        assert convergence.in_list[0] == pytest.approx(0.35313, 1.0e-4)
        assert convergence.in_list[1] == pytest.approx(0.46030, 1.0e-4)
        assert convergence.in_list[2] == pytest.approx(0.43484, 1.0e-4)
        assert convergence.in_list[3] == pytest.approx(1.00492, 1.0e-4)
    def test__convergence_1d_from__grid_2d_irregular_in__returns_1d_quantities(
            self):

        grid_2d = ag.Grid2DIrregular(grid=[[1.0, 1.0], [2.0, 2.0], [4.0, 4.0]])

        sie = ag.mp.EllIsothermal(centre=(0.0, 0.0),
                                  elliptical_comps=(0.0, 0.0),
                                  einstein_radius=1.0)

        convergence_1d = sie.convergence_1d_from_grid(grid=grid_2d)
        convergence_2d = sie.convergence_2d_from_grid(grid=grid_2d)

        print(convergence_1d)

        assert convergence_1d[0] == pytest.approx(convergence_2d[0], 1.0e-4)
        assert convergence_1d[1] == pytest.approx(convergence_2d[1], 1.0e-4)
        assert convergence_1d[2] == pytest.approx(convergence_2d[2], 1.0e-4)

        sie = ag.mp.EllIsothermal(centre=(0.2, 0.2),
                                  elliptical_comps=(0.3, 0.3),
                                  einstein_radius=1.0)

        convergence_1d = sie.convergence_1d_from_grid(grid=grid_2d)
        convergence_2d = sie.convergence_2d_from_grid(grid=grid_2d)

        assert convergence_1d[0] == pytest.approx(convergence_2d[0], 1.0e-4)
        assert convergence_1d[1] == pytest.approx(convergence_2d[1], 1.0e-4)
        assert convergence_1d[2] == pytest.approx(convergence_2d[2], 1.0e-4)
Example #9
0
    def test__convergence_1d_from(
        self, sub_grid_1d_7, mp_0, gal_x1_mp, mp_1, gal_x2_mp
    ):

        grid = ag.Grid2D.manual_native(
            [[[1.05, -0.55], [2.05, -0.55]]], pixel_scales=1.0
        )

        galaxy = ag.Galaxy(redshift=0.5)

        convergence = galaxy.convergence_1d_from(grid=sub_grid_1d_7)

        assert (convergence.slim == np.zeros(shape=sub_grid_1d_7.sub_shape_slim)).all()

        mp_convergence = mp_0.convergence_1d_from(grid=grid)

        gal_convergence = gal_x1_mp.convergence_1d_from(grid=grid)

        assert mp_convergence == gal_convergence

        mp_convergence = mp_0.convergence_1d_from(grid=grid)
        mp_convergence += mp_1.convergence_1d_from(grid=grid)

        gal_convergence = gal_x2_mp.convergence_1d_from(grid=grid)

        assert mp_convergence == gal_convergence

        grid = ag.Grid2DIrregular([(1.05, -0.55), (2.05, -0.55)])

        mp_convergence = mp_0.convergence_1d_from(grid=grid)

        gal_convergence = gal_x1_mp.convergence_1d_from(grid=grid)

        assert (mp_convergence == gal_convergence).all()

        assert mp_convergence.in_list[0] == gal_convergence.in_list[0]

        # Test explicitly for a profile with an offset centre and ellipticity, given the 1D to 2D projections are nasty.

        grid = ag.Grid2D.manual_native(
            [[(1.05, -0.55), (2.05, -0.55)]], pixel_scales=1.0
        )

        elliptical_mp = ag.mp.EllIsothermal(
            centre=(0.5, 1.0), elliptical_comps=(0.2, 0.3), einstein_radius=1.0
        )

        galaxy = ag.Galaxy(redshift=0.5, mass=elliptical_mp)

        mp_convergence = elliptical_mp.convergence_1d_from(grid=grid)
        gal_convergence = galaxy.convergence_1d_from(grid=grid)

        assert (mp_convergence == gal_convergence).all()
Example #10
0
    def test__magnification_via_hessian_from_grid(self):

        sie = MockEllIsothermal(centre=(0.0, 0.0),
                                elliptical_comps=(0.0, -0.111111),
                                einstein_radius=2.0)

        grid = ag.Grid2DIrregular(grid=[(0.5, 0.5), (1.0, 1.0)])

        magnification = sie.magnification_via_hessian_from_grid(grid=grid)

        assert magnification.in_list[0] == pytest.approx(-0.56303, 1.0e-4)
        assert magnification.in_list[1] == pytest.approx(-2.57591, 1.0e-4)
Example #11
0
    def test__image_1d_from(self, sub_grid_1d_7, lp_0, gal_x1_lp, lp_1, gal_x2_lp):

        grid = ag.Grid2D.manual_native([[[1.05, -0.55]]], pixel_scales=1.0)

        galaxy = ag.Galaxy(redshift=0.5)

        image_1d = galaxy.image_1d_from(grid=sub_grid_1d_7)

        assert (image_1d == np.zeros(shape=sub_grid_1d_7.sub_shape_slim)).all()

        lp_image = lp_0.image_1d_from(grid=grid)

        gal_image = gal_x1_lp.image_1d_from(grid=grid)

        assert lp_image == gal_image

        lp_image = lp_0.image_1d_from(grid=grid)
        lp_image += lp_1.image_1d_from(grid=grid)

        gal_image = gal_x2_lp.image_1d_from(grid=grid)

        assert lp_image == gal_image

        grid = ag.Grid2DIrregular([(1.05, -0.55)])

        lp_image = lp_0.image_1d_from(grid=grid)

        gal_image = gal_x1_lp.image_1d_from(grid=grid)

        assert lp_image.in_list[0] == gal_image.in_list[0]

        # Test explicitly for a profile with an offset centre and ellipticity, given the 1D to 2D projections are nasty.

        grid = ag.Grid2D.manual_native(
            [[(1.05, -0.55), (2.05, -0.55)]], pixel_scales=1.0
        )

        elliptical_lp = ag.lp.EllSersic(
            centre=(0.5, 1.0), elliptical_comps=(0.2, 0.3), intensity=1.0
        )

        galaxy = ag.Galaxy(redshift=0.5, mass=elliptical_lp)

        lp_image = elliptical_lp.image_1d_from(grid=grid)
        gal_lp_image = galaxy.image_1d_from(grid=grid)

        assert (lp_image == gal_lp_image).all()
Example #12
0
    def test__deflections_yx_2d_from__no_mass_profile_list__deflections_returned_as_0s_of_shape_grid(
        self, sub_grid_2d_7x7, mp_0, gal_x1_mp, mp_1, gal_x2_mp
    ):

        grid = ag.Grid2D.manual_native([[[1.05, -0.55]]], pixel_scales=1.0)

        galaxy = ag.Galaxy(redshift=0.5)

        deflections = galaxy.deflections_yx_2d_from(grid=sub_grid_2d_7x7)

        assert (
            deflections.slim == np.zeros(shape=(sub_grid_2d_7x7.sub_shape_slim, 2))
        ).all()

        galaxy = ag.Galaxy(redshift=0.5)

        deflections = galaxy.deflections_yx_2d_from(grid=sub_grid_2d_7x7)

        assert (deflections.binned.native == np.zeros(shape=(7, 7, 2))).all()

        mp_deflections = mp_0.deflections_yx_2d_from(grid=grid)

        gal_deflections = gal_x1_mp.deflections_yx_2d_from(
            grid=np.array([[1.05, -0.55]])
        )

        assert (mp_deflections == gal_deflections).all()

        mp_deflections = mp_0.deflections_yx_2d_from(grid=grid)
        mp_deflections += mp_1.deflections_yx_2d_from(grid=grid)

        gal_deflections = gal_x2_mp.deflections_yx_2d_from(
            grid=np.array([[1.05, -0.55]])
        )

        assert (mp_deflections == gal_deflections).all()

        grid = ag.Grid2DIrregular([(1.05, -0.55)])

        mp_deflections = mp_0.deflections_yx_2d_from(grid=grid)

        gal_deflections = gal_x1_mp.deflections_yx_2d_from(grid=grid)

        assert type(gal_deflections) == ag.VectorYX2DIrregular
        assert mp_deflections.in_list[0][0] == gal_deflections.in_list[0][0]
        assert mp_deflections.in_list[0][1] == gal_deflections.in_list[0][1]
Example #13
0
    def test__convergence_2d_from(
        self, sub_grid_2d_7x7, mp_0, gal_x1_mp, mp_1, gal_x2_mp
    ):

        grid = ag.Grid2D.manual_native([[[1.05, -0.55]]], pixel_scales=1.0)

        galaxy = ag.Galaxy(redshift=0.5)

        convergence = galaxy.convergence_2d_from(grid=sub_grid_2d_7x7)

        assert (
            convergence.slim == np.zeros(shape=sub_grid_2d_7x7.sub_shape_slim)
        ).all()

        mp_convergence = mp_0.convergence_2d_from(grid=grid)

        gal_convergence = gal_x1_mp.convergence_2d_from(grid=grid)

        assert mp_convergence == gal_convergence

        mp_convergence = mp_0.convergence_2d_from(grid=grid)
        mp_convergence += mp_1.convergence_2d_from(grid=grid)

        gal_convergence = gal_x2_mp.convergence_2d_from(grid=grid)

        assert mp_convergence == gal_convergence

        grid = ag.Grid2DIrregular([(1.05, -0.55)])

        mp_convergence = mp_0.convergence_2d_from(grid=grid)

        gal_convergence = gal_x1_mp.convergence_2d_from(grid=grid)

        assert mp_convergence == gal_convergence

        assert mp_convergence.in_list[0] == gal_convergence.in_list[0]