Ejemplo n.º 1
0
    def test__solution_and_regularization_matrix_range_of_values(self):

        matrix_shape = (3,3)

        inv = inversions.Inversion(image_1d=np.ones(9), noise_map_1d=np.ones(9), convolver=MockConvolver(matrix_shape),
                                   mapper=MockMapper(matrix_shape), regularization=MockRegularization(matrix_shape))

        # G_l term, Warren & Dye 2003 / Nightingale /2015 2018

        # G_l = s_T * H * s

        # Matrix multiplication:

        # s_T * H = [2.0, 3.0, 5.0] * [2.0,  -1.0,  0.0] = [(2.0* 2.0) + (3.0*-1.0) + (5.0 *0.0)] = [1.0, -1.0, 7.0]
        #                             [-1.0,  2.0, -1.0]   [(2.0*-1.0) + (3.0* 2.0) + (5.0*-1.0)]
        #                             [ 0.0, -1.0,  2.0]   [(2.0* 0.0) + (3.0*-1.0) + (5.0 *2.0)]

        # (s_T * H) * s = [1.0, -1.0, 7.0] * [2.0] = 34.0
        #                                    [3.0]
        #                                    [5.0]

        inv.solution_vector = np.array([2.0, 3.0, 5.0])

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

        assert inv.regularization_term == 34.0
Ejemplo n.º 2
0
    def test__solution_all_1s__regularization_matrix_simple(self):

        matrix_shape = (3,3)

        inv = inversions.Inversion(image_1d=np.ones(9), noise_map_1d=np.ones(9), convolver=MockConvolver(matrix_shape),
                                   mapper=MockMapper(matrix_shape), regularization=MockRegularization(matrix_shape))

        inv.solution_vector = np.array([1.0, 1.0, 1.0])

        inv.regularization_matrix = np.array([[1.0, 0.0, 0.0],
                                              [0.0, 1.0, 0.0],
                                              [0.0, 0.0, 1.0]])

        # G_l term, Warren & Dye 2003 / Nightingale /2015 2018

        # G_l = s_T * H * s

        # Matrix multiplication:

        # s_T * H = [1.0, 1.0, 1.0] * [1.0, 1.0, 1.0] = [(1.0*1.0) + (1.0*0.0) + (1.0*0.0)] = [1.0, 1.0, 1.0]
        #                             [1.0, 1.0, 1.0]   [(1.0*0.0) + (1.0*1.0) + (1.0*0.0)]
        #                             [1.0, 1.0, 1.0]   [(1.0*0.0) + (1.0*0.0) + (1.0*1.0)]

        # (s_T * H) * s = [1.0, 1.0, 1.0] * [1.0] = 3.0
        #                                   [1.0]
        #                                   [1.0]

        assert inv.regularization_term == 3.0
Ejemplo n.º 3
0
    def test__solution_different_values__simple_blurred_mapping_matrix__correct_reconstructed_image(self):

        matrix_shape = (3,3)

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

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

        inv = inversions.Inversion(image_1d=np.ones(9), noise_map_1d=np.ones(9), convolver=MockConvolver(matrix_shape),
                                   mapper=MockMapper(matrix_shape, grid_stack), regularization=MockRegularization(matrix_shape))

        inv.solution_vector = np.array([1.0, 2.0, 3.0, 4.0])

        inv.blurred_mapping_matrix = np.array([[1.0, 1.0, 1.0, 1.0],
                                               [1.0, 0.0, 1.0, 1.0],
                                               [1.0, 0.0, 0.0, 0.0]])

        # # CCD pixel 0 maps to 4 pixs pixxels -> value is 1.0 + 2.0 + 3.0 + 4.0 = 10.0
        # # CCD pixel 1 maps to 3 pixs pixxels -> value is 1.0 + 3.0 + 4.0
        # # CCD pixel 2 maps to 1 pixs pixxels -> value is 1.0

        assert (inv.reconstructed_data_vector == np.array([10.0, 8.0, 1.0])).all()
        assert (inv.reconstructed_data == np.array([[0.0, 0.0, 0.0],
                                                    [10.0, 8.0, 1.0],
                                                    [0.0, 0.0, 0.0]]))
Ejemplo n.º 4
0
    def test__matrix_not_positive_definite__raises_reconstruction_exception(self):

        matrix_shape = (3,3)

        inv = inversions.Inversion(image_1d=np.ones(9), noise_map_1d=np.ones(9), convolver=MockConvolver(matrix_shape),
                                   mapper=MockMapper(matrix_shape), regularization=MockRegularization(matrix_shape))

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

        with pytest.raises(exc.InversionException):
            assert pytest.approx(inv.log_determinant_of_matrix_cholesky(matrix), 1e-4)
Ejemplo n.º 5
0
    def test__determinant_of_positive_definite_matrix_2_via_cholesky(self):

        matrix_shape = (3,3)

        inv = inversions.Inversion(image_1d=np.ones(9), noise_map_1d=np.ones(9), convolver=MockConvolver(matrix_shape),
                                   mapper=MockMapper(matrix_shape), regularization=MockRegularization(matrix_shape))

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

        log_determinant = np.log(np.linalg.det(matrix))

        assert log_determinant == pytest.approx(inv.log_determinant_of_matrix_cholesky(matrix), 1e-4)
Ejemplo n.º 6
0
# 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, )))

# Our inversion has a reconstructed image and pixeilzation, whcih we can plot using an inversion plotter
inversion_plotters.plot_reconstructed_image(inversion=inversion, mask=mask)
inversion_plotters.plot_reconstructed_pixelization(inversion=inversion,
                                                   should_plot_grid=True)

# And there we have it, we've successfully reconstructed, or, *inverted*, our source using the mapper's rectangular
# grid. Whilst this source was simple (a blob of light in the centre of the source-plane), inversions come into their
# own when fitting sources with complex morphologies. Infact, given we're having so much fun inverting things, lets
# simulate a really complex source and invert it!


def simulate_complex_source():