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__1d_array_in__can_map_it_to_3x2_2d_array(self): array_1d = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]) array_2d = mapping_util.map_unmasked_1d_array_to_2d_array_from_array_1d_and_shape( array_1d, shape=(3, 2)) assert (array_2d == np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])).all()
def plane_image_of_galaxies_from_grid(shape, grid, galaxies, buffer=1.0e-2): y_min = np.min(grid[:, 0]) - buffer y_max = np.max(grid[:, 0]) + buffer x_min = np.min(grid[:, 1]) - buffer x_max = np.max(grid[:, 1]) + buffer pixel_scales = (float( (y_max - y_min) / shape[0]), float((x_max - x_min) / shape[1])) origin = ((y_max + y_min) / 2.0, (x_max + x_min) / 2.0) uniform_grid = grid_util.regular_grid_1d_masked_from_mask_pixel_scales_and_origin( mask=np.full(shape=shape, fill_value=False), pixel_scales=pixel_scales, origin=origin) image_1d = sum([ galaxy_util.intensities_of_galaxies_from_grid(grid=uniform_grid, galaxies=[galaxy]) for galaxy in galaxies ]) image_2d = mapping_util.map_unmasked_1d_array_to_2d_array_from_array_1d_and_shape( array_1d=image_1d, shape=shape) return pl.PlaneImage(array=image_2d, pixel_scales=pixel_scales, grid=grid, origin=origin)
def reconstructed_pixelization_from_solution_vector(self, solution_vector): """Given the solution vector of an inversion (see *inversions.Inversion*), determine the reconstructed \ pixelization of the rectangular pixelization by using the mapper.""" recon = mapping_util.map_unmasked_1d_array_to_2d_array_from_array_1d_and_shape( array_1d=solution_vector, shape=self.shape) return scaled_array.ScaledRectangularPixelArray( array=recon, pixel_scales=self.geometry.pixel_scales, origin=self.geometry.origin)
def map_to_2d_keep_padded(self, padded_array_1d): """ Map a padded 1D array of values to its padded 2D array. Parameters ----------- padded_array_1d : ndarray A 1D array of values which were computed using the *PaddedRegularGrid*. """ return mapping_util.map_unmasked_1d_array_to_2d_array_from_array_1d_and_shape( array_1d=padded_array_1d, shape=self.mask.shape)
def test__1d_array_in__maps_it_to_4x4_2d_array(self): array_1d = np.array([ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0 ]) array_2d = mapping_util.map_unmasked_1d_array_to_2d_array_from_array_1d_and_shape( array_1d, shape=(4, 4)) assert (array_2d == np.array([[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], [9.0, 10.0, 11.0, 12.0], [13.0, 14.0, 15.0, 16.0]])).all()
def convolve_array_1d_with_psf(self, padded_array_1d, psf): """Convolve a 1d padded array of values (e.g. intensities before PSF blurring) with a PSF, and then trim \ the convolved array to its original 2D shape. Parameters ----------- padded_array_1d: ndarray A 1D array of values which were computed using the *PaddedRegularGrid*. psf : ndarray An array describing the PSF kernel of the image. """ padded_array_2d = mapping_util.map_unmasked_1d_array_to_2d_array_from_array_1d_and_shape( array_1d=padded_array_1d, shape=self.mask.shape) blurred_padded_array_2d = psf.convolve(array=padded_array_2d) return mapping_util.map_2d_array_to_masked_1d_array_from_array_2d_and_mask( array_2d=blurred_padded_array_2d, mask=np.full(self.mask.shape, False))
def test__3x2_grid__shape_change_correct_and_coordinates_shift(self): galaxy = g.Galaxy(light=lp.EllipticalSersic(intensity=1.0)) grid = np.array([[-1.5, -1.5], [1.5, 1.5]]) plane_image = lens_util.plane_image_of_galaxies_from_grid( shape=(3, 2), grid=grid, galaxies=[galaxy], buffer=0.0) plane_image_galaxy = galaxy.intensities_from_grid( grid=np.array([[-1.0, -0.75], [-1.0, 0.75], [0.0, -0.75], [0.0, 0.75], [1.0, -0.75], [1.0, 0.75]])) plane_image_galaxy = mapping_util.map_unmasked_1d_array_to_2d_array_from_array_1d_and_shape( array_1d=plane_image_galaxy, shape=(3, 2)) assert (plane_image == plane_image_galaxy).all()
def test__3x3_grid__extracts_max_min_coordinates__creates_regular_grid_including_half_pixel_offset_from_edge( self): galaxy = g.Galaxy(light=lp.EllipticalSersic(intensity=1.0)) grid = np.array([[-1.5, -1.5], [1.5, 1.5]]) plane_image = lens_util.plane_image_of_galaxies_from_grid( shape=(3, 3), grid=grid, galaxies=[galaxy], buffer=0.0) plane_image_galaxy = galaxy.intensities_from_grid(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]])) plane_image_galaxy = mapping_util.map_unmasked_1d_array_to_2d_array_from_array_1d_and_shape( array_1d=plane_image_galaxy, shape=(3, 3)) assert (plane_image == plane_image_galaxy).all()
def test__3x3_grid__extracts_max_min_coordinates__ignores_other_coordinates_more_central( self): galaxy = g.Galaxy(light=lp.EllipticalSersic(intensity=1.0)) grid = np.array([[-1.5, -1.5], [1.5, 1.5], [0.1, -0.1], [-1.0, 0.6], [1.4, -1.3], [1.5, 1.5]]) plane_image = lens_util.plane_image_of_galaxies_from_grid( shape=(3, 3), grid=grid, galaxies=[galaxy], buffer=0.0) plane_image_galaxy = galaxy.intensities_from_grid(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]])) plane_image_galaxy = mapping_util.map_unmasked_1d_array_to_2d_array_from_array_1d_and_shape( array_1d=plane_image_galaxy, shape=(3, 3)) assert (plane_image == plane_image_galaxy).all()
def test__3x3_grid__buffer_aligns_two_grids(self): galaxy = g.Galaxy(light=lp.EllipticalSersic(intensity=1.0)) grid_without_buffer = np.array([[-1.48, -1.48], [1.48, 1.48]]) plane_image = lens_util.plane_image_of_galaxies_from_grid( shape=(3, 3), grid=grid_without_buffer, galaxies=[galaxy], buffer=0.02) plane_image_galaxy = galaxy.intensities_from_grid(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]])) plane_image_galaxy = mapping_util.map_unmasked_1d_array_to_2d_array_from_array_1d_and_shape( array_1d=plane_image_galaxy, shape=(3, 3)) assert (plane_image == plane_image_galaxy).all()