def test__compare_to_imaging_util(self): pix = mappers.RectangularMapper(pixels=9, shape=(4, 3), grid_stack=None, border=None, geometry=MockGeometry()) solution = np.array( [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 1.0, 2.0, 3.0]) recon_pix = pix.reconstructed_pixelization_from_solution_vector( solution_vector=solution) recon_pix_util = mapping_util.map_unmasked_1d_array_to_2d_array_from_array_1d_and_shape( array_1d=solution, shape=(4, 3)) assert (recon_pix == recon_pix_util).all() assert recon_pix.shape == (4, 3) pix = mappers.RectangularMapper(pixels=9, shape=(3, 4), grid_stack=None, border=None, geometry=MockGeometry()) solution = np.array( [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 1.0, 2.0, 3.0]) recon_pix = pix.reconstructed_pixelization_from_solution_vector( solution_vector=solution) recon_pix_util = mapping_util.map_unmasked_1d_array_to_2d_array_from_array_1d_and_shape( array_1d=solution, shape=(3, 4)) assert (recon_pix == recon_pix_util).all() assert recon_pix.shape == (3, 4)
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]]
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]]
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)
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]]
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]]
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]]
def mapper_from_grid_stack_and_border(self, grid_stack, border): """Setup a rectangular mapper from a rectangular pixelization, as follows: 1) If a border is supplied, relocate all of the grid-stack's regular and sub grid pixels beyond the border. 2) Determine the rectangular pixelization's geometry, by laying the pixelization over the sub-grid. 3) Setup the rectangular mapper from the relocated grid-stack and rectangular pixelization. Parameters ---------- grid_stack : grids.GridStack A stack of grid describing the observed image's pixel coordinates (e.g. an image-grid, sub-grid, etc.). border : grids.RegularGridBorder The border of the grid-stack's regular-grid. """ if border is not None: relocated_grid_stack = border.relocated_grid_stack_from_grid_stack( grid_stack) else: relocated_grid_stack = grid_stack geometry = self.geometry_from_grid(grid=relocated_grid_stack.sub) return mappers.RectangularMapper(pixels=self.pixels, grid_stack=relocated_grid_stack, border=border, shape=self.shape, geometry=geometry)
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]]
def mapper_from_grid_and_pixelization_grid( self, grid, pixelization_grid=None, inversion_uses_border=False, hyper_image=None, ): """Setup a rectangular mapper from a rectangular pixelization, as follows: 1) If a border is supplied, relocate all of the grid's and sub grid pixels beyond the border. 2) Determine the rectangular pixelization's geometry, by laying the pixelization over the sub-grid. 3) Setup the rectangular mapper from the relocated grid and rectangular pixelization. Parameters ---------- grid : grids.Grid A stack of grid describing the observed image's pixel coordinates (e.g. an image-grid, sub-grid, etc.). border : grids.GridBorder | None The border of the grid's grid. hyper_image : ndarray A pre-computed hyper_galaxies-image of the image the mapper is expected to reconstruct, used for adaptive analysis. """ if inversion_uses_border: relocated_grid = grid.relocated_grid_from_grid(grid=grid) else: relocated_grid = grid geometry = self.geometry_from_grid(grid=relocated_grid) return mappers.RectangularMapper( pixels=self.pixels, grid=relocated_grid, pixelization_grid=geometry.pixel_centres, shape=self.shape, geometry=geometry, hyper_image=hyper_image, )