示例#1
0
        def test__3x3_grid_of_pixel_grid__1_coordinate_per_square_pixel__near_edges_of_pixels(
                self):
            #   _ _ _
            #  |_|_|_| Boundaries for pixels x = 0 and y = 0  -1.0 to -(1/3)
            #  |_|_|_| Boundaries for pixels x = 1 and y = 1 - (1/3) to (1/3)
            #  |_|_|_| Boundaries for pixels x = 2 and y = 2 - (1/3)" to 1.0"

            pixelization_grid = np.array([[1.0, -1.0], [1.0, 0.0], [1.0, 1.0],
                                          [-0.32, -1.0], [-0.32, 0.32],
                                          [0.0, 1.0], [-0.34, -0.34],
                                          [-0.34, 0.325], [-1.0, 1.0]])

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

            geometry = pix.geometry_from_grid(grid=pixelization_grid)

            grids = MockGridStack(regular=pixelization_grid,
                                  sub=MockSubGrid(pixelization_grid,
                                                  sub_to_regular=np.ones((1)),
                                                  sub_grid_size=1))

            pix = mappers.RectangularMapper(pixels=9,
                                            shape=(3, 3),
                                            grid_stack=grids,
                                            border=None,
                                            geometry=geometry)

            assert (pix.regular_to_pix == np.array([0, 1, 2, 3, 4, 5, 6, 7,
                                                    8])).all()
            assert (pix.sub_to_pix == np.array([0, 1, 2, 3, 4, 5, 6, 7,
                                                8])).all()
            assert pix.pix_to_regular == [[0], [1], [2], [3], [4], [5], [6],
                                          [7], [8]]
            assert pix.pix_to_sub == [[0], [1], [2], [3], [4], [5], [6], [7],
                                      [8]]
示例#2
0
        def test__4x3_grid__non_symmetric_centre_shift(self):
            #   _ _ _
            #  |_|_|_|
            #  |_|_|_|
            #  |_|_|_|
            #  |_|_|_|

            pixelization_grid = np.array([[3.0, -0.5], [3.0,
                                                        0.51], [2.49, -0.5],
                                          [1.4, 1.0], [1.0, 2.5]])

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

            geometry = pix.geometry_from_grid(grid=pixelization_grid)

            grids = MockGridStack(regular=pixelization_grid,
                                  sub=MockSubGrid(pixelization_grid,
                                                  sub_to_regular=np.ones((1)),
                                                  sub_grid_size=1))

            pix = mappers.RectangularMapper(pixels=12,
                                            shape=(4, 3),
                                            grid_stack=grids,
                                            border=None,
                                            geometry=geometry)

            assert (pix.regular_to_pix == np.array([0, 1, 3, 10, 11])).all()
            assert (pix.sub_to_pix == np.array([0, 1, 3, 10, 11])).all()
            assert pix.pix_to_regular == [[0], [1], [], [2], [], [], [], [],
                                          [], [], [3], [4]]
            assert pix.pix_to_sub == [[0], [1], [], [2], [], [], [], [], [],
                                      [], [3], [4]]
示例#3
0
        def test__3x3_grid__buffer_is_small__grid_give_min_minus_1_max_1__sets_up_geometry_correctly(
                self):

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

            pixelization_grid = 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, 0.0],
                                          [-1.0, 1.0]])

            geometry = pix.geometry_from_grid(pixelization_grid, buffer=1e-8)

            assert geometry.shape == (3, 3)
            assert geometry.pixel_scales == pytest.approx((2. / 3., 2. / 3.),
                                                          1e-2)
            assert (geometry.pixel_neighbors[0] == [1, 3, -1, -1]).all()
            assert (geometry.pixel_neighbors[1] == [0, 2, 4, -1]).all()
            assert (geometry.pixel_neighbors[2] == [1, 5, -1, -1]).all()
            assert (geometry.pixel_neighbors[3] == [0, 4, 6, -1]).all()
            assert (geometry.pixel_neighbors[4] == [1, 3, 5, 7]).all()
            assert (geometry.pixel_neighbors[5] == [2, 4, 8, -1]).all()
            assert (geometry.pixel_neighbors[6] == [3, 7, -1, -1]).all()
            assert (geometry.pixel_neighbors[7] == [4, 6, 8, -1]).all()
            assert (geometry.pixel_neighbors[8] == [5, 7, -1, -1]).all()

            assert (geometry.pixel_neighbors_size == np.array(
                [2, 3, 2, 3, 4, 3, 2, 3, 2])).all()
示例#4
0
        def test__3x3_grid__change_arcsecond_dimensions__not_symmetric(self):
            #   _ _ _
            #  |_|_|_| Boundaries for pixels x = 0 and y = 0  -1.5 to -0.5
            #  |_|_|_| Boundaries for pixels x = 1 and y = 1 -0.5 to 0.5
            #  |_|_|_| Boundaries for pixels x = 2 and y = 2  0.5 to 1.5

            pixelization_grid = np.array([[1.0, -1.5], [1.0, -0.49],
                                          [0.32, -1.5], [0.32, 0.51],
                                          [-1.0, 1.5]])

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

            geometry = pix.geometry_from_grid(grid=pixelization_grid)

            grids = MockGridStack(regular=pixelization_grid,
                                  sub=MockSubGrid(pixelization_grid,
                                                  sub_to_regular=np.ones((1)),
                                                  sub_grid_size=1))

            pix = mappers.RectangularMapper(pixels=9,
                                            shape=(3, 3),
                                            grid_stack=grids,
                                            border=None,
                                            geometry=geometry)

            assert (pix.regular_to_pix == np.array([0, 1, 3, 5, 8])).all()
            assert (pix.sub_to_pix == np.array([0, 1, 3, 5, 8])).all()
            assert pix.pix_to_regular == [[0], [1], [], [2], [], [3], [], [],
                                          [4]]
            assert pix.pix_to_sub == [[0], [1], [], [2], [], [3], [], [], [4]]
示例#5
0
        def test__3x3_grid_of_pixel_grid__add_multiple_grid_to_1_pixel_pixel(
                self):
            #                  _ _ _
            # -1.0 to -(1/3)  |_|_|_|
            # -(1/3) to (1/3) |_|_|_|
            #  (1/3) to 1.0   |_|_|_|

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

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

            geometry = pix.geometry_from_grid(grid=pixelization_grid)

            grids = MockGridStack(regular=pixelization_grid,
                                  sub=MockSubGrid(pixelization_grid,
                                                  sub_to_regular=np.ones((1)),
                                                  sub_grid_size=1))

            pix = mappers.RectangularMapper(pixels=9,
                                            shape=(3, 3),
                                            grid_stack=grids,
                                            border=None,
                                            geometry=geometry)

            assert (pix.regular_to_pix == np.array([0, 4, 2, 4, 4, 4, 6, 4,
                                                    8])).all()
            assert (pix.sub_to_pix == np.array([0, 4, 2, 4, 4, 4, 6, 4,
                                                8])).all()
            assert pix.pix_to_regular == [[0], [], [2], [], [1, 3, 4, 5, 7],
                                          [], [6], [], [8]]
            assert pix.pix_to_sub == [[0], [], [2], [], [1, 3, 4, 5, 7], [],
                                      [6], [], [8]]
示例#6
0
        def test__4x3_grid__pixel_centres(self):

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

            pixelization_grid = 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, 0.0],
                                          [-1.0, 1.0]])

            geometry = pix.geometry_from_grid(pixelization_grid, buffer=1e-8)

            assert geometry.pixel_centres == pytest.approx(
                np.array([
                    [0.75, -2. / 3.],
                    [0.75, 0.0],
                    [0.75, 2. / 3.],
                    [0.25, -2. / 3.],
                    [0.25, 0.0],
                    [0.25, 2. / 3.],
                    [-0.25, -2. / 3.],
                    [-0.25, 0.0],
                    [-0.25, 2. / 3.],
                    [-0.75, -2. / 3.],
                    [-0.75, 0.0],
                    [-0.75, 2. / 3.],
                ]))
示例#7
0
        def test__3x3_pixelization__solution_vector_ascending(self):

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

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

            geometry = pix.geometry_from_grid(grid=pixelization_grid)

            pix = mappers.RectangularMapper(pixels=9,
                                            shape=(3, 3),
                                            grid_stack=None,
                                            border=None,
                                            geometry=geometry)

            recon_pix = pix.reconstructed_pixelization_from_solution_vector(
                solution_vector=np.array(
                    [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]))

            assert (recon_pix == np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0],
                                           [7.0, 8.0, 9.0]])).all()
            assert recon_pix.pixel_scales == pytest.approx((4. / 3., 2. / 3.),
                                                           1e-2)
            assert recon_pix.origin == (0.0, 0.0)
示例#8
0
        def test__3x4_grid__change_arcsecond_dimensions__not_symmetric(self):
            #   _ _ _ _
            #  |_|_|_|_|
            #  |_|_|_|_|
            #  |_|_|_|_|

            pixelization_grid = np.array([[1.0, -1.5], [1.0, -0.49],
                                          [0.32, -1.5], [-0.34, 0.49],
                                          [-1.0, 1.5]])

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

            grids = MockGridStack(regular=pixelization_grid,
                                  sub=MockSubGrid(pixelization_grid,
                                                  sub_to_regular=np.ones((1)),
                                                  sub_grid_size=1))

            geometry = pix.geometry_from_grid(grid=pixelization_grid)

            pix = mappers.RectangularMapper(pixels=12,
                                            shape=(3, 4),
                                            grid_stack=grids,
                                            border=None,
                                            geometry=geometry)

            assert (pix.regular_to_pix == np.array([0, 1, 4, 10, 11])).all()
            assert (pix.sub_to_pix == np.array([0, 1, 4, 10, 11])).all()
            assert pix.pix_to_regular == [[0], [1], [], [], [2], [], [], [],
                                          [], [], [3], [4]]
            assert pix.pix_to_sub == [[0], [1], [], [], [2], [], [], [], [],
                                      [], [3], [4]]
示例#9
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()
示例#10
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
示例#11
0
        def test__3x3_grid__larger_range_of_grid(self):

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

            pixelization_grid = np.array([[2.0, 1.0], [4.0, 3.0], [6.0, 5.0],
                                          [8.0, 7.0]])

            geometry = pix.geometry_from_grid(pixelization_grid, buffer=1e-8)

            assert geometry.shape == (3, 3)
            assert geometry.pixel_scales == pytest.approx((6. / 3., 6. / 3.),
                                                          1e-2)
示例#12
0
        def test__3x3_grid__same_as_above_change_buffer(self):
            pix = pixelizations.Rectangular(shape=(3, 3))

            pixelization_grid = 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, 0.0],
                                          [1.0, 1.0]])

            geometry = pix.geometry_from_grid(pixelization_grid, buffer=1e-4)

            assert geometry.shape == (3, 3)
            assert geometry.pixel_scales == pytest.approx((2. / 3., 2. / 3.),
                                                          1e-2)
示例#13
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()
示例#14
0
        def test__5x4_grid__buffer_is_small(self):

            pix = pixelizations.Rectangular(shape=(5, 4))

            pixelization_grid = 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, 0.0],
                                          [-1.0, 1.0]])

            geometry = pix.geometry_from_grid(pixelization_grid, buffer=1e-8)

            assert geometry.shape == (5, 4)
            assert geometry.pixel_scales == pytest.approx((2. / 5., 2. / 4.),
                                                          1e-2)
示例#15
0
        def test__compare_to_pixelization_util(self):
            # |0 | 1| 2| 3|
            # |4 | 5| 6| 7|
            # |8 | 9|10|11|
            # |12|13|14|15|

            pix = pixelizations.Rectangular(shape=(7, 5))

            pixel_neighbors, pixel_neighbors_size = pix.neighbors_from_pixelization(
            )
            pixel_neighbors_util, pixel_neighbors_size_util = \
                pixelization_util.rectangular_neighbors_from_shape(shape=(7, 5))

            assert (pixel_neighbors == pixel_neighbors_util).all()
            assert (pixel_neighbors_size == pixel_neighbors_size_util).all()
示例#16
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()
示例#17
0
    def test__setup_pixelization__galaxies_have_other_pixelization__returns_normal_grids(
            self):

        ma = mask.Mask(np.array([[False, False, False], [False, False, False],
                                 [False, False, 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.Rectangular(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
示例#18
0
        def test__4x3_grid_of_pixel_grid__1_coordinate_in_each_pixel(self):
            #   _ _ _
            #  |_|_|_|
            #  |_|_|_|
            #  |_|_|_|
            #  |_|_|_|

            # Boundaries for column pixel 0 -1.0 to -(1/3)
            # Boundaries for column pixel 1 -(1/3) to (1/3)
            # Boundaries for column pixel 2  (1/3) to 1.0

            # Bounadries for row pixel 0 -1.0 to -0.5
            # Bounadries for row pixel 1 -0.5 to 0.0
            # Bounadries for row pixel 2  0.0 to 0.5
            # Bounadries for row pixel 3  0.5 to 1.0

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

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

            geometry = pix.geometry_from_grid(grid=pixelization_grid)

            grids = MockGridStack(regular=pixelization_grid,
                                  sub=MockSubGrid(pixelization_grid,
                                                  sub_to_regular=np.ones((1)),
                                                  sub_grid_size=1))

            pix = mappers.RectangularMapper(pixels=12,
                                            shape=(4, 3),
                                            grid_stack=grids,
                                            border=None,
                                            geometry=geometry)

            assert (pix.regular_to_pix == np.array([0, 1, 2, 3, 8, 11])).all()
            assert (pix.sub_to_pix == np.array([0, 1, 2, 3, 8, 11])).all()
            assert pix.pix_to_regular == [[0], [1], [2], [3], [], [], [], [],
                                          [4], [], [], [5]]
            assert pix.pix_to_sub == [[0], [1], [2], [3], [], [], [], [], [4],
                                      [], [], [5]]
示例#19
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
示例#20
0
def make_rectangular_pixelization():
    return pix.Rectangular(shape=(25, 25))
示例#21
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
示例#22
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
示例#23
0
                                 inner_radius_arcsec=1.0,
                                 outer_radius_arcsec=2.2)
ccd_plotters.plot_image(ccd_data=ccd_data, mask=mask)

# Next, lets set this image up as lens data, and setup a tracer using the input lens galaxy model (we don't need
# to provide the source's light profile, as we're using a mapper to reconstruct it).
lens_data = ld.LensData(ccd_data=ccd_data, mask=mask, sub_grid_size=1)
lens_galaxy = g.Galaxy(mass=mp.EllipticalIsothermal(
    centre=(0.0, 0.0), axis_ratio=0.8, phi=135.0, einstein_radius=1.6))
tracer = ray_tracing.TracerImageSourcePlanes(
    lens_galaxies=[lens_galaxy],
    source_galaxies=[g.Galaxy()],
    image_plane_grid_stack=lens_data.grid_stack)

# We'll use another rectangular pixelization and mapper to perform the reconstruction
rectangular = pix.Rectangular(shape=(25, 25))
mapper = rectangular.mapper_from_grid_stack_and_border(
    grid_stack=tracer.source_plane.grid_stack, border=None)
mapper_plotters.plot_image_and_mapper(ccd_data=ccd_data,
                                      mask=mask,
                                      mapper=mapper,
                                      should_plot_grid=True)

# And now, finally, we're going to use our mapper to invert the image using the 'inversions' module, which is imported
# as 'inv'. I'll explain how this works in a second - but lets just go ahead and perform the inversion first.
# (Ignore the 'regularization' input below for now, we'll cover this in the next tutorial).
inversion = inv.Inversion(image_1d=lens_data.image_1d,
                          noise_map_1d=lens_data.noise_map_1d,
                          convolver=lens_data.convolver_mapping_matrix,
                          mapper=mapper,
                          regularization=reg.Constant(coefficients=(1.0, )))
    shape=(100, 100), pixel_scale=0.05, sub_grid_size=2)

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

# (Our source galaxy doesn't have a light profile from here on, as we're reconstructing its light using a pixelization).
tracer = ray_tracing.TracerImageSourcePlanes(
    lens_galaxies=[lens_galaxy],
    source_galaxies=[g.Galaxy()],
    image_plane_grid_stack=image_plane_grid_stack)

# Next, lets set up a pixelization using the 'pixelizations' module, which we've imported as 'pix'.

# There are multiple pixelizations available in PyAutoLens, but for now we'll keep it simple and use a uniform
# rectangular grid. As usual, the grid's 'shape' defines its (y,x) dimensions.
rectangular = pix.Rectangular(shape=(25, 25))

# By itself, a pixelization doesn't tell us much. It has no grid of coordinates, no image, and nothing which tells it
# about the lens we're fitting. This information comes when we use the pixelization to set up a 'mapper'.
# (The 'border=None' will be covered in tutorial 5, so just ignore it for now!)
mapper = rectangular.mapper_from_grid_stack_and_border(
    grid_stack=tracer.source_plane.grid_stack, border=None)

# This mapper is a 'RectangularMapper' - every pixelization generates it owns mapper.
print(type(mapper))

# By plotting our mapper, we now see our pixelization. Its a fairly boring grid of rectangular pixels.
mapper_plotters.plot_rectangular_mapper(
    mapper=mapper,
    should_plot_grid=False,
    title='Fairly Boring Grid of Rectangular Pixels')
                         radius_arcsec=3.0)
ccd_plotters.plot_ccd_subplot(ccd_data=ccd_data, mask=mask)

# The lines of code below do everything we're used to, that is, setup an image and its grid stack, mask it, trace it
# via a tracer, setup the rectangular mapper, etc.
lens_galaxy = g.Galaxy(mass=mp.EllipticalIsothermal(
    centre=(0.0, 0.0), axis_ratio=0.8, phi=45.0, einstein_radius=1.6))

lens_data = ld.LensData(ccd_data=ccd_data, mask=mask)

tracer = ray_tracing.TracerImageSourcePlanes(
    lens_galaxies=[lens_galaxy],
    source_galaxies=[g.Galaxy()],
    image_plane_grid_stack=lens_data.grid_stack)

rectangular = pix.Rectangular(shape=(50, 50))
mapper = rectangular.mapper_from_grid_stack_and_border(
    grid_stack=tracer.source_plane.grid_stack, border=None)

inversion = inv.Inversion(image_1d=lens_data.image_1d,
                          noise_map_1d=lens_data.noise_map_1d,
                          convolver=lens_data.convolver_mapping_matrix,
                          mapper=mapper,
                          regularization=reg.Constant(coefficients=(1.0, )))

# Now lets plot our rectangular mapper with the image.
mapper_plotters.plot_image_and_mapper(ccd_data=ccd_data,
                                      mapper=mapper,
                                      mask=mask,
                                      should_plot_grid=True)
示例#26
0
        def test__number_of_pixels_and_regularization_set_up_correctly(self):
            pix = pixelizations.Rectangular(shape=(3, 3))

            assert pix.shape == (3, 3)
            assert pix.pixels == 9