def test_padded_grid_stack(self, lens_data_stack): padded_image_util = grid_util.regular_grid_1d_masked_from_mask_pixel_scales_and_origin( mask=np.full((6, 6), False), pixel_scales=lens_data_stack.images[0].pixel_scales) assert (lens_data_stack.padded_grid_stacks[0].regular == padded_image_util).all() assert lens_data_stack.padded_grid_stacks[0].regular.image_shape == (4, 4) assert lens_data_stack.padded_grid_stacks[0].regular.padded_shape == ( 6, 6) padded_sub_util = grid_util.sub_grid_1d_masked_from_mask_pixel_scales_and_sub_grid_size( mask=np.full((6, 6), False), pixel_scales=lens_data_stack.images[0].pixel_scales, sub_grid_size=lens_data_stack.grid_stacks[0].sub.sub_grid_size) assert lens_data_stack.padded_grid_stacks[0].sub == pytest.approx( padded_sub_util, 1e-4) assert lens_data_stack.padded_grid_stacks[0].sub.image_shape == (4, 4) assert lens_data_stack.padded_grid_stacks[0].sub.padded_shape == (6, 6) assert (lens_data_stack.padded_grid_stacks[0].blurring == np.array( [[0.0, 0.0]])).all() padded_image_util = grid_util.regular_grid_1d_masked_from_mask_pixel_scales_and_origin( mask=np.full((6, 6), False), pixel_scales=lens_data_stack.images[1].pixel_scales) assert (lens_data_stack.padded_grid_stacks[1].regular == padded_image_util).all() assert lens_data_stack.padded_grid_stacks[1].regular.image_shape == (4, 4) assert lens_data_stack.padded_grid_stacks[1].regular.padded_shape == ( 6, 6) padded_sub_util = grid_util.sub_grid_1d_masked_from_mask_pixel_scales_and_sub_grid_size( mask=np.full((6, 6), False), pixel_scales=lens_data_stack.images[1].pixel_scales, sub_grid_size=lens_data_stack.grid_stacks[1].sub.sub_grid_size) assert lens_data_stack.padded_grid_stacks[1].sub == pytest.approx( padded_sub_util, 1e-4) assert lens_data_stack.padded_grid_stacks[1].sub.image_shape == (4, 4) assert lens_data_stack.padded_grid_stacks[1].sub.padded_shape == (6, 6) assert (lens_data_stack.padded_grid_stacks[1].blurring == np.array( [[0.0, 0.0]])).all() # Pixel scale is doubled, thus the grids of coordinates are doubled. assert (2.0 * lens_data_stack.padded_grid_stacks[0].regular == lens_data_stack.padded_grid_stacks[1].regular).all() assert (2.0 * lens_data_stack.padded_grid_stacks[0].sub == lens_data_stack.padded_grid_stacks[1].sub).all()
def padded_grid_from_shape_psf_shape_and_pixel_scale( self, shape, psf_shape, pixel_scale): """Setup a regular padded grid from a 2D array shape, psf-shape and pixel-scale. The center of every pixel is used to setup the grid's (y,x) arc-second coordinates, including padded pixels \ which are beyond the input shape but will blurred light into the 2D array's shape due to the psf. Parameters ---------- shape : (int, int) The (y,x) shape of the masked-grid's 2D image in units of pixels. psf_shape : (int, int) The shape of the psf which defines the blurring region and therefore size of padding. pixel_scale : float The scale of each pixel in arc seconds """ padded_shape = (shape[0] + psf_shape[0] - 1, shape[1] + psf_shape[1] - 1) padded_regular_grid = grid_util.regular_grid_1d_masked_from_mask_pixel_scales_and_origin( mask=np.full(padded_shape, False), pixel_scales=(pixel_scale, pixel_scale)) padded_mask = msk.Mask.unmasked_for_shape_and_pixel_scale( shape=padded_shape, pixel_scale=pixel_scale) return PaddedRegularGrid(arr=padded_regular_grid, mask=padded_mask, image_shape=shape)
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 test__setup_3x3_image_1_coordinate_in_mask(self): mask = np.array([[True, True, True], [True, False, True], [True, True, True]]) image_grid = grid_util.regular_grid_1d_masked_from_mask_pixel_scales_and_origin(mask=mask, pixel_scales=(3.0, 6.0)) assert (image_grid[0] == np.array([0.0, 0.0])).all()
def test__setup_3x4_image__six_grid(self): mask = np.array([[True, False, True, True], [False, False, False, True], [True, False, True, False]]) image_grid = grid_util.regular_grid_1d_masked_from_mask_pixel_scales_and_origin(mask=mask, pixel_scales=(3.0, 3.0)) assert (image_grid == np.array([[3., -1.5], [0., -4.5], [0., -1.5], [0., 1.5], [-3., -1.5], [-3., 4.5]])).all()
def test__setup_3x3_image__five_coordinates_in_mask(self): mask = np.array([[True, False, True], [False, False, False], [True, False, True]]) image_grid = grid_util.regular_grid_1d_masked_from_mask_pixel_scales_and_origin(mask=mask, pixel_scales=(6.0, 3.0)) assert (image_grid == np.array([[6., 0.], [0., -3.], [0., 0.], [0., 3.], [-6., 0.]])).all()
def test__setup_3x4_image__six_grid__include_nonzero_origin(self): mask = np.array([[True, False, True, True], [False, False, False, True], [True, False, True, False]]) image_grid = grid_util.regular_grid_1d_masked_from_mask_pixel_scales_and_origin(mask=mask, pixel_scales=(3.0, 3.0), origin=(1.0, 2.0)) assert image_grid == pytest.approx(np.array([[4., 0.5], [1., -2.5], [1., 0.5], [1., 3.5], [-2., 0.5], [-2., 6.5]]), 1e-4)
def test__setup_3x3_image__five_coordinates_in_mask__include_nonzero_origin(self): mask = np.array([[True, False, True], [False, False, False], [True, False, True]]) image_grid = grid_util.regular_grid_1d_masked_from_mask_pixel_scales_and_origin(mask=mask, pixel_scales=(6.0, 3.0), origin=(1.0, 1.0)) assert image_grid == pytest.approx(np.array([[7., 1.], [1., -2.], [1., 1.], [1., 4.], [-5., 1.]]), 1e-4)
def test__setup_4x4_image__ten_coordinates_in_grid__new_pixel_scale(self): mask = np.array([[True, False, False, True], [False, False, False, True], [True, False, False, True], [False, False, False, True]]) image_grid = grid_util.regular_grid_1d_masked_from_mask_pixel_scales_and_origin(mask=mask, pixel_scales=(1.0, 1.0)) assert (image_grid == np.array([[1.5, -0.5], [1.5, 0.5], [0.5, -1.5], [0.5, -0.5], [0.5, 0.5], [-0.5, -0.5], [-0.5, 0.5], [-1.5, -1.5], [-1.5, -0.5], [-1.5, 0.5]])).all()
def from_mask(cls, mask): """Setup a regular-grid from a mask, wehere the center of every unmasked pixel gives the grid's (y,x) \ arc-second coordinates. Parameters ----------- mask : Mask The mask whose unmasked pixels are used to setup the regular-pixel grid. """ array = grid_util.regular_grid_1d_masked_from_mask_pixel_scales_and_origin( mask=mask, pixel_scales=mask.pixel_scales) return cls(array, mask)
def from_shape_and_pixel_scale(cls, shape, pixel_scale): """Setup a regular-grid from a 2D array shape and pixel scale. Here, the center of every pixel on the 2D \ array gives the grid's (y,x) arc-second coordinates. This is equivalent to using a 2D mask consisting entirely of unmasked pixels. Parameters ----------- shape : (int, int) The 2D shape of the array, where all pixels are used to generate the grid-stack's grid_stack. pixel_scale : float The size of each pixel in arc seconds. """ mask = msk.Mask.unmasked_for_shape_and_pixel_scale( shape=shape, pixel_scale=pixel_scale) array = grid_util.regular_grid_1d_masked_from_mask_pixel_scales_and_origin( mask=mask, pixel_scales=mask.pixel_scales) return cls(array, mask)
def test_padded_grid_stack(self, lens_data): padded_image_util = grid_util.regular_grid_1d_masked_from_mask_pixel_scales_and_origin(mask=np.full((6, 6), False), pixel_scales=lens_data.image.pixel_scales) assert (lens_data.padded_grid_stack.regular == padded_image_util).all() assert lens_data.padded_grid_stack.regular.image_shape == (4, 4) assert lens_data.padded_grid_stack.regular.padded_shape == (6, 6) padded_sub_util = grid_util.sub_grid_1d_masked_from_mask_pixel_scales_and_sub_grid_size( mask=np.full((6, 6), False), pixel_scales=lens_data.image.pixel_scales, sub_grid_size=lens_data.grid_stack.sub.sub_grid_size) assert lens_data.padded_grid_stack.sub == pytest.approx(padded_sub_util, 1e-4) assert lens_data.padded_grid_stack.sub.image_shape == (4, 4) assert lens_data.padded_grid_stack.sub.padded_shape == (6, 6) assert (lens_data.padded_grid_stack.blurring == np.array([[0.0, 0.0]])).all()
def test__padded_grid_stack(self, galaxy_data): padded_image_util = grid_util.regular_grid_1d_masked_from_mask_pixel_scales_and_origin( mask=np.full((4, 4), False), pixel_scales=galaxy_data.image.pixel_scales) assert ( galaxy_data.padded_grid_stack.regular == padded_image_util).all() assert galaxy_data.padded_grid_stack.regular.image_shape == (4, 4) assert galaxy_data.padded_grid_stack.regular.padded_shape == (4, 4) padded_sub_util = grid_util.sub_grid_1d_masked_from_mask_pixel_scales_and_sub_grid_size( mask=np.full((4, 4), False), pixel_scales=galaxy_data.image.pixel_scales, sub_grid_size=galaxy_data.grid_stack.sub.sub_grid_size) assert galaxy_data.padded_grid_stack.sub == pytest.approx( padded_sub_util, 1e-4) assert galaxy_data.padded_grid_stack.sub.image_shape == (4, 4) assert galaxy_data.padded_grid_stack.sub.padded_shape == (4, 4)