Example #1
0
    def test__setup_pixelization__galaxy_has_pixelization__returns_grids_with_pix_grid(
            self):

        ma = mask.Mask(np.array([[False, False, False], [False, False, False],
                                 [False, True, False]]),
                       pixel_scale=1.0)

        grid_stack = grids.GridStack.grid_stack_from_mask_sub_grid_size_and_psf_shape(
            mask=ma, sub_grid_size=1, psf_shape=(1, 1))

        galaxy = g.Galaxy(
            pixelization=pixelizations.AdaptiveMagnification(shape=(3, 3)),
            regularization=regularization.Constant())

        image_plane_pix_grids = \
            pixelizations.setup_image_plane_pixelization_grid_from_galaxies_and_grid_stack(galaxies=[galaxy, galaxy],
                                                                                           grid_stack=grid_stack)

        assert (image_plane_pix_grids.regular == grid_stack.regular).all()
        assert (image_plane_pix_grids.sub == grid_stack.sub).all()
        assert (image_plane_pix_grids.blurring == grid_stack.blurring).all()
        assert image_plane_pix_grids.pix == pytest.approx(
            np.array([[1.0, -1.0], [1.0, 0.0], [1.0, 1.0], [0.0, -1.0],
                      [0.0, 0.0], [0.0, 1.0], [-1.0, -1.0], [-1.0, 1.0]]),
            1.0e-4)
Example #2
0
    def test__3x3_simple_grid__include_mask__create_using_regular_grid(self):

        regular_grid = np.array([[1.0, 0.0], [0.0, -1.0], [0.0, 0.0],
                                 [0.0, 1.0], [-1.0, 0.0]])

        mask = msk.Mask(array=np.array([[True, False, True],
                                        [False, False, False],
                                        [True, False, True]]),
                        pixel_scale=1.0)

        sub_grid = np.array([[1.0, 0.0], [0.0, -1.0], [0.0, 0.0], [0.0, 1.0],
                             [-1.0, 0.0]])
        sub_to_regular = np.array([0, 1, 2, 3, 4])

        regular_grid = grids.RegularGrid(arr=regular_grid, mask=mask)
        sub_grid = MockSubGrid(sub_grid, sub_to_regular, sub_grid_size=1)

        pix = pixelizations.AdaptiveMagnification(shape=(3, 3))
        image_plane_pix = pix.image_plane_pix_grid_from_regular_grid(
            regular_grid=regular_grid)

        grid_stack = MockGridStack(
            regular=regular_grid,
            sub=sub_grid,
            pix=image_plane_pix.sparse_grid,
            regular_to_nearest_pix=image_plane_pix.regular_to_sparse)

        mapper = pix.mapper_from_grid_stack_and_border(grid_stack=grid_stack,
                                                       border=None)

        assert mapper.is_image_plane_pixelization == True
        assert mapper.geometry.shape_arc_seconds == pytest.approx((2.0, 2.0),
                                                                  1.0e-4)
        assert (mapper.geometry.pixel_centres == image_plane_pix.sparse_grid
                ).all()
        assert mapper.geometry.origin == pytest.approx((0.0, 0.0), 1.0e-4)

        assert isinstance(mapper, pm.VoronoiMapper)

        assert (mapper.mapping_matrix == np.array([[1.0, 0.0, 0.0, 0.0, 0.0],
                                                   [0.0, 1.0, 0.0, 0.0, 0.0],
                                                   [0.0, 0.0, 1.0, 0.0, 0.0],
                                                   [0.0, 0.0, 0.0, 1.0, 0.0],
                                                   [0.0, 0.0, 0.0, 0.0,
                                                    1.0]])).all()

        reg = regularization.Constant(coefficients=(1.0, ))
        regularization_matrix = reg.regularization_matrix_from_pixel_neighbors(
            mapper.geometry.pixel_neighbors,
            mapper.geometry.pixel_neighbors_size)

        assert (regularization_matrix == np.array(
            [[3.00000001, -1.0, -1.0, -1.0, 0.0],
             [-1.0, 3.00000001, -1.0, 0.0, -1.0],
             [-1.0, -1.0, 4.00000001, -1.0, -1.0],
             [-1.0, 0.0, -1.0, 3.00000001, -1.0],
             [0.0, -1.0, -1.0, -1.0, 3.00000001]])).all()
Example #3
0
    def test__5_simple_grid__include_sub_grid__sets_up_correct_mapper(self):

        regular_grid = np.array([[1.0, 1.0], [-1.0, 1.0], [0.0, 0.0],
                                 [1.0, -1.0], [-1.0, -1.0]])

        sub_grid = np.array([[1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [0.0, 0.0],
                             [-1.0, 1.0], [-1.0, 1.0], [-1.0, 1.0], [0.0, 0.0],
                             [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0],
                             [1.0, -1.0], [1.0, -1.0], [1.0, -1.0], [0.0, 0.0],
                             [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0],
                             [0.0, 0.0]])

        sub_to_regular = np.array(
            [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4])

        pixel_centers = regular_grid

        regular_to_sparse = np.array([0, 1, 2, 3, 4])

        grid_stack = MockGridStack(regular=regular_grid,
                                   sub=MockSubGrid(sub_grid,
                                                   sub_to_regular,
                                                   sub_grid_size=2),
                                   pix=pixel_centers,
                                   regular_to_nearest_pix=regular_to_sparse)

        pix = pixelizations.AdaptiveMagnification(shape=(5, 1))

        mapper = pix.mapper_from_grid_stack_and_border(grid_stack=grid_stack,
                                                       border=None)

        assert mapper.is_image_plane_pixelization == True
        assert mapper.geometry.shape_arc_seconds == pytest.approx((2.0, 2.0),
                                                                  1.0e-4)
        assert (mapper.geometry.pixel_centres == pixel_centers).all()
        assert mapper.geometry.origin == (0.0, 0.0)

        assert isinstance(mapper, pm.VoronoiMapper)

        assert (mapper.mapping_matrix == np.array([[0.75, 0.0, 0.25, 0.0, 0.0],
                                                   [0.0, 0.75, 0.25, 0.0, 0.0],
                                                   [0.0, 0.0, 1.0, 0.0, 0.0],
                                                   [0.0, 0.0, 0.25, 0.75, 0.0],
                                                   [0.0, 0.0, 0.25, 0.0,
                                                    0.75]])).all()

        reg = regularization.Constant(coefficients=(1.0, ))
        regularization_matrix = reg.regularization_matrix_from_pixel_neighbors(
            mapper.geometry.pixel_neighbors,
            mapper.geometry.pixel_neighbors_size)

        assert (regularization_matrix == np.array(
            [[3.00000001, -1.0, -1.0, -1.0, 0.0],
             [-1.0, 3.00000001, -1.0, 0.0, -1.0],
             [-1.0, -1.0, 4.00000001, -1.0, -1.0],
             [-1.0, 0.0, -1.0, 3.00000001, -1.0],
             [0.0, -1.0, -1.0, -1.0, 3.00000001]])).all()
Example #4
0
    def test__5_simple_grid__include_sub_grid(self):
        # Source-plane comprises 5 grid, so 5 masked_image pixels traced to the pix-plane.
        regular_grid = np.array([[1.0, -1.0], [1.0, 1.0], [0.0, 0.0],
                                 [-1.0, -1.0], [-1.0, 1.0]])
        # Assume a 2x2 sub-grid, so each of our 5 masked_image-pixels are split into 4.
        # The grid below is unphysical in that the (0.0, 0.0) terms on the end of each sub-grid probably couldn't
        # happen for a real lensing calculation. This is to make a mapping_matrix matrix which explicitly tests the
        # sub-grid.
        sub_grid = np.array([[1.0, -1.0], [1.0, -1.0], [1.0, -1.0], [0.0, 0.0],
                             [1.0, 1.0], [1.0, 1.0], [1.0, 1.0], [0.0, 0.0],
                             [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0],
                             [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0],
                             [0.0, 0.0], [-1.0, 1.0], [-1.0, 1.0], [-1.0, 1.0],
                             [0.0, 0.0]])

        sub_to_regular = np.array(
            [0, 0, 0, 2, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 2, 4, 4, 4, 2])

        grid_stack = MockGridStack(regular=regular_grid,
                                   sub=MockSubGrid(sub_grid,
                                                   sub_to_regular,
                                                   sub_grid_size=2))

        pix = pixelizations.Rectangular(shape=(3, 3))

        mapper = pix.mapper_from_grid_stack_and_border(grid_stack=grid_stack,
                                                       border=None)

        assert mapper.is_image_plane_pixelization == False
        assert mapper.geometry.shape_arc_seconds == pytest.approx((2.0, 2.0),
                                                                  1.0e-4)
        assert mapper.geometry.origin == pytest.approx((0.0, 0.0), 1.0e-4)

        assert (mapper.mapping_matrix == np.array(
            [[0.75, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
             [0.0, 0.0, 0.75, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
             [0.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0],
             [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.75, 0.0, 0.0],
             [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.75]])).all()
        assert mapper.shape == (3, 3)

        reg = regularization.Constant(coefficients=(1.0, ))
        regularization_matrix = reg.regularization_matrix_from_pixel_neighbors(
            mapper.geometry.pixel_neighbors,
            mapper.geometry.pixel_neighbors_size)

        assert (regularization_matrix == np.array(
            [[2.00000001, -1.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0],
             [-1.0, 3.00000001, -1.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0],
             [0.0, -1.0, 2.00000001, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0],
             [-1.0, 0.0, 0.0, 3.00000001, -1.0, 0.0, -1.0, 0.0, 0.0],
             [0.0, -1.0, 0.0, -1.0, 4.00000001, -1.0, 0.0, -1.0, 0.0],
             [0.0, 0.0, -1.0, 0.0, -1.0, 3.00000001, 0.0, 0.0, -1.0],
             [0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 2.00000001, -1.0, 0.0],
             [0.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0, 3.00000001, -1.0],
             [0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0, 2.00000001]])).all()
Example #5
0
    def test_fixed_regularization(self):
        galaxy_prior = gp.GalaxyModel(variable_redshift=True,
                                      pixelization=pixelizations.Voronoi(),
                                      regularization=regularization.Constant())

        arguments = {galaxy_prior.redshift.redshift: 2.0}

        galaxy = galaxy_prior.instance_for_arguments(arguments)

        assert galaxy.regularization.coefficients == (1., )
Example #6
0
    def test__same_as_above_but_grid_requires_border_relocation(self):

        regular_grid = np.array([[1.0, -1.0], [1.0, 1.0], [0.0, 0.0],
                                 [-1.0, -1.0], [-1.0, 1.0]])
        sub_grid = np.array([[2.0, 2.0], [2.0, 2.0], [2.0, 2.0], [2.0, 2.0],
                             [-2.0, -2.0]])
        # These will all be relocated to the regular grid edge.
        pix_grid = np.array([[1.1, -1.1], [1.1, 1.1], [0.0, 0.0], [-1.1, -1.1],
                             [-1.1, 1.1]])

        border = grids.RegularGridBorder(arr=np.array([0, 1, 3, 4]))

        sub_to_regular = np.array([0, 1, 2, 3, 4])

        regular_to_sparse = np.array([0, 1, 2, 3, 4])

        grid_stack = MockGridStack(regular=regular_grid,
                                   sub=MockSubGrid(sub_grid,
                                                   sub_to_regular,
                                                   sub_grid_size=1),
                                   pix=pix_grid,
                                   regular_to_nearest_pix=regular_to_sparse)

        pix = pixelizations.AdaptiveMagnification(shape=(5, 1))

        mapper = pix.mapper_from_grid_stack_and_border(grid_stack=grid_stack,
                                                       border=border)

        assert mapper.is_image_plane_pixelization == True
        assert mapper.geometry.shape_arc_seconds == pytest.approx((2.0, 2.0),
                                                                  1.0e-4)
        assert mapper.geometry.pixel_centres == pytest.approx(
            regular_grid, 1e-4)
        assert mapper.geometry.origin == (0.0, 0.0)

        assert isinstance(mapper, pm.VoronoiMapper)

        assert (mapper.mapping_matrix == np.array([[0.0, 1.0, 0.0, 0.0, 0.0],
                                                   [0.0, 1.0, 0.0, 0.0, 0.0],
                                                   [0.0, 1.0, 0.0, 0.0, 0.0],
                                                   [0.0, 1.0, 0.0, 0.0, 0.0],
                                                   [0.0, 0.0, 0.0, 1.0,
                                                    0.0]])).all()

        reg = regularization.Constant(coefficients=(1.0, ))
        regularization_matrix = reg.regularization_matrix_from_pixel_neighbors(
            mapper.geometry.pixel_neighbors,
            mapper.geometry.pixel_neighbors_size)

        assert (regularization_matrix == np.array(
            [[3.00000001, -1.0, -1.0, -1.0, 0.0],
             [-1.0, 3.00000001, -1.0, 0.0, -1.0],
             [-1.0, -1.0, 4.00000001, -1.0, -1.0],
             [-1.0, 0.0, -1.0, 3.00000001, -1.0],
             [0.0, -1.0, -1.0, -1.0, 3.00000001]])).all()
Example #7
0
    def test_fixed_pixelization(self):
        galaxy_prior = gp.GalaxyModel(variable_redshift=True,
                                      pixelization=pixelizations.Rectangular(),
                                      regularization=regularization.Constant())

        arguments = {galaxy_prior.redshift.redshift: 2.0}

        galaxy = galaxy_prior.instance_for_arguments(arguments)

        assert galaxy.pixelization.shape[0] == 3
        assert galaxy.pixelization.shape[1] == 3
Example #8
0
    def test__5_simple_grid__no_sub_grid(self):
        # Source-plane comprises 5 grid, so 5 masked_image pixels traced to the pix-plane.
        regular_grid = np.array([[1.0, -1.0], [1.0, 1.0], [0.0, 0.0],
                                 [-1.0, -1.0], [-1.0, 1.0]])
        sub_grid = np.array([[1.0, -1.0], [1.0, 1.0], [0.0, 0.0], [-1.0, -1.0],
                             [-1.0, 1.0]])

        sub_to_regular = np.array([0, 1, 2, 3, 4])

        grid_stack = MockGridStack(regular=regular_grid,
                                   sub=MockSubGrid(
                                       sub_grid=sub_grid,
                                       sub_to_regular=sub_to_regular,
                                       sub_grid_size=1))

        # There is no sub-grid, so our sub_grid are just the masked_image grid (note the NumPy weighted_data structure
        # ensures this has no sub-gridding)

        pix = pixelizations.Rectangular(shape=(3, 3))

        mapper = pix.mapper_from_grid_stack_and_border(grid_stack=grid_stack,
                                                       border=None)

        assert mapper.is_image_plane_pixelization == False
        assert mapper.geometry.shape_arc_seconds == pytest.approx((2.0, 2.0),
                                                                  1.0e-4)
        assert mapper.geometry.origin == pytest.approx((0.0, 0.0), 1.0e-4)

        assert (mapper.mapping_matrix == np.array(
            [[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
             [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
             [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0],
             [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0],
             [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0]])).all()
        assert mapper.shape == (3, 3)

        reg = regularization.Constant(coefficients=(1.0, ))
        regularization_matrix = reg.regularization_matrix_from_pixel_neighbors(
            mapper.geometry.pixel_neighbors,
            mapper.geometry.pixel_neighbors_size)

        assert (regularization_matrix == np.array(
            [[2.00000001, -1.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0],
             [-1.0, 3.00000001, -1.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0],
             [0.0, -1.0, 2.00000001, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0],
             [-1.0, 0.0, 0.0, 3.00000001, -1.0, 0.0, -1.0, 0.0, 0.0],
             [0.0, -1.0, 0.0, -1.0, 4.00000001, -1.0, 0.0, -1.0, 0.0],
             [0.0, 0.0, -1.0, 0.0, -1.0, 3.00000001, 0.0, 0.0, -1.0],
             [0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 2.00000001, -1.0, 0.0],
             [0.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0, 3.00000001, -1.0],
             [0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0, 2.00000001]])).all()
Example #9
0
    def test__grid__requires_border_relocation(self):

        regular_grid = np.array([[1.0, -1.0], [1.0, 1.0], [0.0, 0.0],
                                 [-1.0, -1.0], [-1.0, 1.0]])
        sub_grid = np.array([[2.0, 2.0], [2.0, 2.0], [2.0, 2.0], [2.0, 2.0],
                             [-2.0, -2.0]])

        border = grids.RegularGridBorder(arr=np.array([0, 1, 3, 4]))

        sub_to_regular = np.array([0, 1, 2, 3, 4])

        grid_stack = MockGridStack(regular=regular_grid,
                                   sub=MockSubGrid(sub_grid,
                                                   sub_to_regular,
                                                   sub_grid_size=1))

        pix = pixelizations.Rectangular(shape=(3, 3))

        mapper = pix.mapper_from_grid_stack_and_border(grid_stack, border)

        assert mapper.is_image_plane_pixelization == False
        assert mapper.geometry.shape_arc_seconds == pytest.approx((2.0, 2.0),
                                                                  1.0e-4)
        assert mapper.geometry.origin == pytest.approx((0.0, 0.0), 1.0e-4)

        assert (mapper.mapping_matrix == np.array(
            [[0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
             [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
             [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
             [0.0, 0.0, 1.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, 1.0, 0.0, 0.0]])).all()
        assert mapper.shape == (3, 3)

        reg = regularization.Constant(coefficients=(1.0, ))
        regularization_matrix = reg.regularization_matrix_from_pixel_neighbors(
            mapper.geometry.pixel_neighbors,
            mapper.geometry.pixel_neighbors_size)

        assert (regularization_matrix == np.array(
            [[2.00000001, -1.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0],
             [-1.0, 3.00000001, -1.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0],
             [0.0, -1.0, 2.00000001, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0],
             [-1.0, 0.0, 0.0, 3.00000001, -1.0, 0.0, -1.0, 0.0, 0.0],
             [0.0, -1.0, 0.0, -1.0, 4.00000001, -1.0, 0.0, -1.0, 0.0],
             [0.0, 0.0, -1.0, 0.0, -1.0, 3.00000001, 0.0, 0.0, -1.0],
             [0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 2.00000001, -1.0, 0.0],
             [0.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0, 3.00000001, -1.0],
             [0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0, 2.00000001]])).all()
Example #10
0
    def test__setup_pixelization__galaxy_has_pixelization__but_grid_is_padded_grid__returns_normal_grids(
            self):

        ma = mask.Mask(np.array([[False, False, False], [False, False, False],
                                 [False, True, False]]),
                       pixel_scale=1.0)

        grid_stack = grids.GridStack.padded_grid_stack_from_mask_sub_grid_size_and_psf_shape(
            mask=ma, sub_grid_size=1, psf_shape=(1, 1))

        galaxy = g.Galaxy(
            pixelization=pixelizations.AdaptiveMagnification(shape=(3, 3)),
            regularization=regularization.Constant())

        image_plane_pix_grids = \
            pixelizations.setup_image_plane_pixelization_grid_from_galaxies_and_grid_stack(galaxies=[galaxy, galaxy],
                                                                                           grid_stack=grid_stack)

        assert image_plane_pix_grids == grid_stack
    def test__regularization_matrix__compare_to_regularization_util(self):

        pixel_neighbors = np.array([[1, 3, 7, 2], [4, 2, 0, -1], [1, 5, 3, -1],
                                    [4, 6, 0, -1], [7, 1, 5, 3], [4, 2, 8, -1],
                                    [7, 3, 0, -1], [4, 8, 6, -1],
                                    [7, 5, -1, -1]])

        pixel_neighbors_size = np.array([4, 3, 3, 3, 4, 3, 3, 3, 2])

        reg = regularization.Constant(coefficients=(1.0, ))
        regularization_matrix = reg.regularization_matrix_from_pixel_neighbors(
            pixel_neighbors, pixel_neighbors_size)

        regularization_matrix_util = reg_util.constant_regularization_matrix_from_pixel_neighbors(
            coefficients=(1.0, ),
            pixel_neighbors=pixel_neighbors,
            pixel_neighbors_size=pixel_neighbors_size)

        assert (regularization_matrix == regularization_matrix_util).all()
Example #12
0
    def test__fit_figure_of_merit__matches_correct_fit_given_galaxy_profiles(
            self, ccd_data):

        lens_galaxy = g.Galaxy(light=lp.EllipticalSersic(intensity=0.1))
        source_galaxy = g.Galaxy(
            pixelization=pix.Rectangular(shape=(4, 4)),
            regularization=reg.Constant(coefficients=(1.0, )))

        phase = ph.LensPlanePhase(lens_galaxies=[lens_galaxy],
                                  mask_function=ph.default_mask_function,
                                  cosmology=cosmo.FLRW,
                                  phase_name='test_phase')
        analysis = phase.make_analysis(data=ccd_data)
        instance = phase.constant
        fit_figure_of_merit = analysis.fit(instance=instance)

        mask = phase.mask_function(image=ccd_data.image)
        lens_data = li.LensData(ccd_data=ccd_data, mask=mask)
        tracer = analysis.tracer_for_instance(instance=instance)
        fit = lens_fit.LensProfileFit(lens_data=lens_data, tracer=tracer)

        assert fit.likelihood == fit_figure_of_merit

        phase = ph.LensSourcePlanePhase(lens_galaxies=[lens_galaxy],
                                        source_galaxies=[source_galaxy],
                                        mask_function=ph.default_mask_function,
                                        cosmology=cosmo.FLRW,
                                        phase_name='test_phase')
        analysis = phase.make_analysis(data=ccd_data)
        instance = phase.constant
        fit_figure_of_merit = analysis.fit(instance=instance)

        mask = phase.mask_function(image=ccd_data.image)
        lens_data = li.LensData(ccd_data=ccd_data, mask=mask)
        tracer = analysis.tracer_for_instance(instance=instance)
        fit = lens_fit.LensProfileInversionFit(lens_data=lens_data,
                                               tracer=tracer)

        assert fit.evidence == fit_figure_of_merit
Example #13
0
    def test___if_galaxy_has_pixelization__unmasked_image_is_none(self):

        mask = msk.Mask(array=np.array([[True, True,
                                         True], [True, False, True],
                                        [True, True, True]]),
                        pixel_scale=1.0)

        padded_grid_stack = grids.GridStack.padded_grid_stack_from_mask_sub_grid_size_and_psf_shape(
            mask=mask, sub_grid_size=1, psf_shape=(3, 3))

        psf_0 = im.PSF(array=(np.array([[0.0, 0.0, 0.0], [0.0, 1.0, 0.0],
                                        [0.0, 0.0, 0.0]])),
                       pixel_scale=1.0)

        psf_1 = im.PSF(array=(np.array([[0.0, 3.0, 0.0], [0.0, 1.0, 2.0],
                                        [0.0, 0.0, 0.0]])),
                       pixel_scale=1.0)

        g0 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=0.1),
                      pixelization=pix.Rectangular(),
                      regularization=reg.Constant())
        g1 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=0.2))
        g2 = g.Galaxy(mass_profile=mp.SphericalIsothermal(einstein_radius=1.0))

        tracer = ray_tracing_stack.TracerImageSourcePlanesStack(
            lens_galaxies=[g0, g1, g2],
            source_galaxies=[g0, g1],
            image_plane_grid_stacks=[padded_grid_stack, padded_grid_stack])

        unmasked_blurred_image_of_datas_planes_and_galaxies = \
            stack_util.unmasked_blurred_image_of_datas_planes_and_galaxies_from_padded_grid_stacks_and_psf(
                planes=tracer.planes, padded_grid_stacks=[padded_grid_stack, padded_grid_stack], psfs=[psf_0, psf_1])

        assert unmasked_blurred_image_of_datas_planes_and_galaxies[0][0][
            0] == None
        assert type(unmasked_blurred_image_of_datas_planes_and_galaxies[0][0]
                    [1]) == scaled_array.ScaledSquarePixelArray
        assert unmasked_blurred_image_of_datas_planes_and_galaxies[1][0][
            0] == None
        assert type(unmasked_blurred_image_of_datas_planes_and_galaxies[1][0]
                    [1]) == scaled_array.ScaledSquarePixelArray
        assert unmasked_blurred_image_of_datas_planes_and_galaxies[0][1][
            0] == None
        assert type(unmasked_blurred_image_of_datas_planes_and_galaxies[0][1]
                    [1]) == scaled_array.ScaledSquarePixelArray
        assert unmasked_blurred_image_of_datas_planes_and_galaxies[1][1][
            0] == None
        assert type(unmasked_blurred_image_of_datas_planes_and_galaxies[1][1]
                    [1]) == scaled_array.ScaledSquarePixelArray

        g0 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=0.1))
        g1 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=0.2),
                      pixelization=pix.Rectangular(),
                      regularization=reg.Constant())
        g2 = g.Galaxy(mass_profile=mp.SphericalIsothermal(einstein_radius=1.0))

        tracer = ray_tracing_stack.TracerImageSourcePlanesStack(
            lens_galaxies=[g0, g1, g2],
            source_galaxies=[g0, g1],
            image_plane_grid_stacks=[padded_grid_stack, padded_grid_stack])

        unmasked_blurred_image_of_datas_planes_and_galaxies = \
            stack_util.unmasked_blurred_image_of_datas_planes_and_galaxies_from_padded_grid_stacks_and_psf(
                planes=tracer.planes, padded_grid_stacks=[padded_grid_stack, padded_grid_stack], psfs=[psf_0, psf_1])

        assert type(unmasked_blurred_image_of_datas_planes_and_galaxies[0][0]
                    [0]) == scaled_array.ScaledSquarePixelArray
        assert unmasked_blurred_image_of_datas_planes_and_galaxies[0][0][
            1] == None
        assert type(unmasked_blurred_image_of_datas_planes_and_galaxies[1][0]
                    [0]) == scaled_array.ScaledSquarePixelArray
        assert unmasked_blurred_image_of_datas_planes_and_galaxies[1][0][
            1] == None
        assert type(unmasked_blurred_image_of_datas_planes_and_galaxies[0][1]
                    [0]) == scaled_array.ScaledSquarePixelArray
        assert unmasked_blurred_image_of_datas_planes_and_galaxies[0][1][
            1] == None
        assert type(unmasked_blurred_image_of_datas_planes_and_galaxies[1][1]
                    [0]) == scaled_array.ScaledSquarePixelArray
        assert unmasked_blurred_image_of_datas_planes_and_galaxies[1][1][
            1] == None
Example #14
0
    def test__tracers_are_different__likelihood_is_non_zero(
            self, lens_data_blur):

        pixelization = pix.Rectangular(shape=(3, 3))
        regularization = reg.Constant(coefficients=(1.0, ))

        g0 = g.Galaxy(mass_profile=mp.SphericalIsothermal(einstein_radius=1.0))
        g0_subhalo = g.Galaxy(subhalo=mp.SphericalIsothermal(
            einstein_radius=0.1))
        g1 = g.Galaxy(pixelization=pixelization, regularization=regularization)

        tracer_normal = ray_tracing.TracerImageSourcePlanes(
            lens_galaxies=[g0],
            source_galaxies=[g1],
            image_plane_grid_stack=lens_data_blur.grid_stack)

        tracer_sensitive = ray_tracing.TracerImageSourcePlanes(
            lens_galaxies=[g0, g0_subhalo],
            source_galaxies=[g1],
            image_plane_grid_stack=lens_data_blur.grid_stack)

        fit = sensitivity_fit.SensitivityInversionFit(
            lens_data=lens_data_blur,
            tracer_normal=tracer_normal,
            tracer_sensitive=tracer_sensitive)

        assert (fit.fit_normal.image == lens_data_blur.image).all()
        assert (fit.fit_normal.noise_map == lens_data_blur.noise_map).all()

        mapper = pixelization.mapper_from_grid_stack_and_border(
            grid_stack=tracer_normal.source_plane.grid_stack, border=None)
        inversion = inv.inversion_from_image_mapper_and_regularization(
            mapper=mapper,
            regularization=regularization,
            image_1d=lens_data_blur.image_1d,
            noise_map_1d=lens_data_blur.noise_map_1d,
            convolver=lens_data_blur.convolver_mapping_matrix)

        assert fit.fit_normal.model_image == pytest.approx(
            inversion.reconstructed_data, 1.0e-4)

        residual_map = fit_util.residual_map_from_data_mask_and_model_data(
            data=lens_data_blur.image,
            mask=lens_data_blur.mask,
            model_data=inversion.reconstructed_data)

        assert fit.fit_normal.residual_map == pytest.approx(
            residual_map, 1.0e-4)

        chi_squared_map = fit_util.chi_squared_map_from_residual_map_noise_map_and_mask(
            residual_map=residual_map,
            mask=lens_data_blur.mask,
            noise_map=lens_data_blur.noise_map)

        assert fit.fit_normal.chi_squared_map == pytest.approx(
            chi_squared_map, 1.0e-4)

        assert (fit.fit_sensitive.image == lens_data_blur.image).all()
        assert (fit.fit_sensitive.noise_map == lens_data_blur.noise_map).all()

        mapper = pixelization.mapper_from_grid_stack_and_border(
            grid_stack=tracer_sensitive.source_plane.grid_stack, border=None)
        inversion = inv.inversion_from_image_mapper_and_regularization(
            mapper=mapper,
            regularization=regularization,
            image_1d=lens_data_blur.image_1d,
            noise_map_1d=lens_data_blur.noise_map_1d,
            convolver=lens_data_blur.convolver_mapping_matrix)

        assert fit.fit_sensitive.model_image == pytest.approx(
            inversion.reconstructed_data, 1.0e-4)

        residual_map = fit_util.residual_map_from_data_mask_and_model_data(
            data=lens_data_blur.image,
            mask=lens_data_blur.mask,
            model_data=inversion.reconstructed_data)

        assert fit.fit_sensitive.residual_map == pytest.approx(
            residual_map, 1.0e-4)

        chi_squared_map = fit_util.chi_squared_map_from_residual_map_noise_map_and_mask(
            residual_map=residual_map,
            mask=lens_data_blur.mask,
            noise_map=lens_data_blur.noise_map)

        assert fit.fit_sensitive.chi_squared_map == pytest.approx(
            chi_squared_map, 1.0e-4)

        chi_squared_normal = fit_util.chi_squared_from_chi_squared_map_and_mask(
            chi_squared_map=fit.fit_normal.chi_squared_map,
            mask=lens_data_blur.mask)
        chi_squared_sensitive = fit_util.chi_squared_from_chi_squared_map_and_mask(
            chi_squared_map=fit.fit_sensitive.chi_squared_map,
            mask=lens_data_blur.mask)
        noise_normalization = fit_util.noise_normalization_from_noise_map_and_mask(
            mask=lens_data_blur.mask, noise_map=lens_data_blur.noise_map)
        assert fit.fit_normal.likelihood == -0.5 * (chi_squared_normal +
                                                    noise_normalization)
        assert fit.fit_sensitive.likelihood == -0.5 * (chi_squared_sensitive +
                                                       noise_normalization)

        assert fit.figure_of_merit == fit.fit_sensitive.likelihood - fit.fit_normal.likelihood

        fit_from_factory = sensitivity_fit.fit_lens_data_with_sensitivity_tracers(
            lens_data=lens_data_blur,
            tracer_normal=tracer_normal,
            tracer_sensitive=tracer_sensitive)

        assert fit.figure_of_merit == fit_from_factory.figure_of_merit
Example #15
0
    results = pd.concat(list_)


image_plane_grid_stack = grids.GridStack.from_shape_pixel_scale_and_sub_size(shape_2d=(301, 301), pixel_scales=0.03,
                                                                      sub_size=2)

for i in range(len(results)):
    lens_galaxy = al.Galaxy(mass=al.mp.EllipticalIsothermal(centre=(results['lens_mass_centre_0'][i], results['lens_mass_centre_1'][i]),
                                                    axis_ratio=results['lens_mass_axis_ratio'][i], phi=results['lens_mass_phi'][i],
                                                    einstein_radius=results['lens_mass_einstein_radius'][i]),
                       shear=al.mp.ExternalShear(magnitude=results['lens_shear_magnitude'][i], phi=results['lens_shear_phi'][i]),
                        redshift = slacs['z_lens'][i])

    source_galaxy = al.Galaxy(pixelization=pix.AdaptiveMagnification(shape_2d=(results['source_pixelization_shape_0'][i],
                                                                       results['source_pixelization_shape_1'][i])),
                         regularization=reg.Constant(results['source_regularization_coefficients_0'][i]),
                         redshift=slacs['z_source'][i])

    tracer = ray_tracing.TracerImageSourcePlanes(lens_galaxies=[lens_galaxy], source_galaxies=[source_galaxy],
                                             image_plane_grid_stack=image_plane_grid_stack, cosmology=cosmology.Planck15)
    einstein_mass = tracer.einstein_masses_of_planes[0]

    M_Ein.append(einstein_mass)

fig, ax = plt.subplots()

cm = plt.get_cmap('gist_rainbow')
ax.set_color_cycle([cm(1.*i/len(slacs)) for i in range(len(slacs))])
marker = ['o', '+','v','<','s','p','*','D','h','x','8','1','2','3','4',]

for i in range(len(results)):
Example #16
0
#    results = pd.concat(list_)

image_plane_grid_stack = grids.GridStack.from_shape_pixel_scale_and_sub_size(
    shape_2d=ccd_data.shape, pixel_scales=ccd_data.pixel_scales, sub_size=2)

#print(data)
#print(data.iloc[0,16])

lens_galaxy = al.Galaxy(mass=al.mp.EllipticalIsothermal(
    centre=(data.iloc[0, 5], data.iloc[0, 6]),
    axis_ratio=data.iloc[0, 7],
    phi=data.iloc[0, 8],
    einstein_radius=data.iloc[0, 9]),
                        redshift=0.285)
source_galaxy = al.Galaxy(
    pixelization=pix.AdaptiveMagnification(shape_2d=(data.iloc[0, 14],
                                                     data.iloc[0, 15])),
    regularization=reg.Constant(data.iloc[0, 16]),
    redshift=0.575)

#image_plane_grid_stack = grids.GridStack.from_shape_pixel_scale_and_sub_size(shape_2d=(301, 301), pixel_scales=0.03,
#                                                                      sub_size=2)

tracer = ray_tracing.TracerImageSourcePlanes(
    lens_galaxies=[lens_galaxy],
    source_galaxies=[source_galaxy],
    image_plane_grid_stack=lens_data.grid_stack,
    cosmology=cosmology.Planck15)

print(np.log10(tracer.einstein_masses_of_planes[0]))
Example #17
0
radius_arcsec = 3.0
psf_shape = (21, 21)
pixelization_shape = (30, 30)

print('sub grid size = ' + str(sub_grid_size))
print('circular mask radius = ' + str(radius_arcsec) + '\n')
print('psf shape = ' + str(psf_shape) + '\n')
print('pixelization shape = ' + str(pixelization_shape) + '\n')

lens_galaxy = g.Galaxy(mass=mp.EllipticalIsothermal(
    centre=(0.0, 0.0), einstein_radius=1.6, axis_ratio=0.7, phi=45.0))

pixelization = pix.AdaptiveMagnification(shape=pixelization_shape)

source_galaxy = g.Galaxy(pixelization=pixelization,
                         regularization=reg.Constant(coefficients=(1.0, )))

for image_type in ['LSST', 'Euclid', 'HST', 'HST_Up', 'AO']:

    ccd_data = tools.load_profiling_ccd_data(image_type=image_type,
                                             lens_name='no_lens_source_smooth',
                                             psf_shape=psf_shape)
    mask = msk.Mask.circular(shape=ccd_data.shape,
                             pixel_scale=ccd_data.pixel_scale,
                             radius_arcsec=radius_arcsec)
    lens_data = ld.LensData(ccd_data=ccd_data,
                            mask=mask,
                            sub_grid_size=sub_grid_size)

    print('AdaptiveMagnification Inversion fit run times for image type ' +
          image_type + '\n')
            centre=(results.loc[lens[i]]['param']['lens_mass_centre_0'],
                    results.loc[lens[i]]['param']['lens_mass_centre_1']),
            axis_ratio=results.loc[lens[i]]['+error']['lens_mass_axis_ratio'],
            phi=results.loc[lens[i]]['param']['lens_mass_phi'],
            einstein_radius=results.loc[
                lens[i]]['-error']['lens_mass_einstein_radius']),
        shear=al.mp.ExternalShear(
            magnitude=results.loc[lens[i]]['param']['lens_shear_magnitude'],
            phi=results.loc[lens[i]]['param']['lens_shear_phi']),
        redshift=slacs['z_lens'][i])

    source_galaxy = al.Galaxy(
        pixelization=pix.AdaptiveMagnification(shape_2d=(
            results.loc[lens[i]]['param']['source_pixelization_shape_0'],
            results.loc[lens[i]]['param']['source_pixelization_shape_1'])),
        regularization=reg.Constant(results.loc[lens[i]]['param']
                                    ['source_regularization_coefficients_0']),
        redshift=slacs['z_source'][i])

    tracer = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy],
        source_galaxies=[source_galaxy],
        image_plane_grid_stack=image_plane_grid_stack,
        cosmology=cosmology.Planck15)
    tracer_error_hi = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy_hi],
        source_galaxies=[source_galaxy],
        image_plane_grid_stack=image_plane_grid_stack,
        cosmology=cosmology.Planck15)
    tracer_error_low = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy_low],
        source_galaxies=[source_galaxy],