Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test__image_all_1s__direct_image_to_source_mapping__perfect_fit_even_with_regularization(self):
im = np.array([[0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 1.0, 1.0, 0.0],
[0.0, 1.0, 1.0, 1.0, 0.0],
[0.0, 1.0, 1.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0]]).view(image.Image)
ma = np.array([[True, True, True, True, True],
[True, False, False, False, True],
[True, False, False, False, True],
[True, False, False, False, True],
[True, True, True, True, True]])
ma = mask.Mask(ma, pixel_scale=1.0)
psf = image.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)
im = image.Image(im, pixel_scale=1.0, psf=psf, noise_map=np.ones((5, 5)))
li = lensing_image.LensingImage(im, ma, sub_grid_size=2)
galaxy_pix = g.Galaxy(pixelization=pixelizations.Rectangular(shape=(3, 3)),
regularization=regularization.Constant(coefficients=(1.0,)))
tracer = ray_tracing.TracerImageSourcePlanes(lens_galaxies=[g.Galaxy()], source_galaxies=[galaxy_pix],
image_plane_grids=[li.grids], border=None)
fit = lensing_fitting.LensingInversionFit(lensing_images=[li], tracer=tracer)
cov_matrix = np.array([[1.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, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
def test__regular_grid_from_mask__compare_to_array_util(self):
mask = np.array([[True, True, False, False],
[True, False, True, True],
[True, True, False, False]])
mask = msk.Mask(array=mask, pixel_scale=2.0)
regular_grid_util = grid_util.regular_grid_1d_masked_from_mask_pixel_scales_and_origin(mask=mask,
pixel_scales=(2.0, 2.0))
regular_grid = grids.RegularGrid.from_mask(mask)
assert type(regular_grid) == grids.RegularGrid
assert regular_grid == pytest.approx(regular_grid_util, 1e-4)
assert regular_grid.pixel_scale == 2.0
assert (regular_grid.mask.masked_grid_index_to_pixel == mask.masked_grid_index_to_pixel).all()
def make_padded_grid_stack():
ma = mask.Mask(np.array([[True, False]]), pixel_scale=3.0)
return grids.GridStack.padded_grid_stack_from_mask_sub_grid_size_and_psf_shape(ma, 2, (3, 3))
def test__map_to_2d_keep_padded__5x3_from_1d(self):
mask = msk.Mask(array=np.full((5, 3), False), pixel_scale=1.0)
regular_padded_grid = grids.PaddedRegularGrid(arr=np.empty((0)), mask=mask, image_shape=(3, 1))
array_1d = 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,
4.0, 5.0, 6.0])
array_2d = regular_padded_grid.map_to_2d_keep_padded(padded_array_1d=array_1d)
assert (array_2d == 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],
[4.0, 5.0, 6.0]])).all()
def test__setup_pixelization__galaxy_has_pixelization__but_grid_is_padded_grid__returns_normal_grids(self):
ma = mask.Mask(np.array([[False, False, False],
[False, False, False],
[False, True, False]]), pixel_scale=1.0)
grid_stack_0 = grids.GridStack.padded_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.AdaptiveMagnification(shape=(3, 3)),
regularization=regularization.Constant(), redshift=2.0)
tracer = ray_tracing_stack.TracerMultiPlanesStack(galaxies=[galaxy, g.Galaxy(redshift=1.0)],
image_plane_grid_stacks=[grid_stack_0, grid_stack_0])
assert (tracer.planes[0].grid_stacks[0].pix == np.array([[0.0, 0.0]])).all()
assert (tracer.planes[1].grid_stacks[0].pix == np.array([[0.0, 0.0]])).all()
def test__mask_with_offset_centre__changing_origin_of_sparse_to_regular_grid_ensures_same_pairings(self):
ma = msk.Mask(array=np.array([[True, True, True, False, True],
[True, True, False, False, False],
[True, True, True, False, True],
[True, True, True, True, True],
[True, True, True, True, True]]), pixel_scale=1.0, centre=(1.0, 1.0))
regular_grid = grids.RegularGrid.from_mask(mask=ma)
# Without a change in origin, only the central 3 pixels are paired as the unmasked sparse grid overlaps
# the central (3x3) pixels only.
pix_grid = grids.SparseToRegularGrid(unmasked_sparse_grid_shape=(3, 3), pixel_scales=(1.0, 1.0),
regular_grid=regular_grid)
assert pix_grid.total_sparse_pixels == 3
assert (pix_grid.sparse_to_unmasked_sparse == np.array([1, 2, 5])).all()
assert (pix_grid.unmasked_sparse_to_sparse == np.array([0, 0, 1, 2, 2, 2, 2, 2, 2])).all()
def test__4x4_mask_with_4_pixels__3x3_interp_grid__image_coords_extend_beyond_mask(self):
msk = np.array([[True, True, True, True],
[True, False, False, True],
[True, False, False, True],
[True, True, True, True]])
msk = mask.Mask(msk, pixel_scale=1.0)
interp = interpolation.InterpolationScheme.from_mask(mask=msk, shape=(3, 3))
assert interp.image_coords[0] == pytest.approx(np.array([-1.5, -1.5]), 1e-4)
assert interp.image_coords[1] == pytest.approx(np.array([-1.5, 0.0]), 1e-4)
assert interp.image_coords[2] == pytest.approx(np.array([-1.5, 1.5]), 1e-4)
assert interp.image_coords[3] == pytest.approx(np.array([0.0, -1.5]), 1e-4)
assert interp.image_coords[4] == pytest.approx(np.array([0.0, 0.0]), 1e-4)
assert interp.image_coords[5] == pytest.approx(np.array([0.0, 1.5]), 1e-4)
assert interp.image_coords[6] == pytest.approx(np.array([1.5, -1.5]), 1e-4)
assert interp.image_coords[7] == pytest.approx(np.array([1.5, 0.0]), 1e-4)
assert interp.image_coords[8] == pytest.approx(np.array([1.5, 1.5]), 1e-4)
def mask_function():
return msk.Mask.circular_annular(shape=image.shape, pixel_scale=image.pixel_scale, inner_radius_arcsec=0.6,
outer_radius_arcsec=2.4)
# 3) Postage stamp size - On the other hand, the postage stamp must have enough padding in the border that our masks can
# include all pixels with signal in. In fact, it isn't just the masks that must be contained within the postage stamp,
# but also the masks's 'blurring region' - which corresponds to all unmasked regular pixels where light will blur into
# the masks after PSF convolution. Thus, we may need to pad an regular to include this region.
# This regular is an example of a stamp which is big enough to contain the lens and source galaxies, but when we
# apply a sensible masks we get an error, because the masks's blurring region hits the edge of the regular.
image_small_stamp = im.load_ccd_data_from_fits(image_path=path + 'datas/image_small_stamp/regular.fits', pixel_scale=0.1,
noise_map_path=path+'datas/image_small_stamp/noise_maps.fits',
psf_path=path+'datas/image_small_stamp/psf.fits')
imaging_plotters.plot_image_subplot(image=image_small_stamp)
# If we apply a masks to this regular, we'll get an error when we try to use it to set up a lensing regular, because its
# blurring region hits the regular edge.
mask = ma.Mask.circular(shape=image_small_stamp.shape, pixel_scale=image_small_stamp.pixel_scale,
radius_arcsec=2.0)
# lensing_image = li.LensImage(regular=regular, masks=masks)
# We can overcome this using the same input as before. However, now, the resized regular shape is bigger than the regular,
# thus a padding of zeros is introduced to the edges.
image_small_stamp_padded = im.load_ccd_data_from_fits(image_path=path + 'datas/image_small_stamp/regular.fits',
pixel_scale=0.1,
noise_map_path=path+'datas/image_small_stamp/noise_maps.fits',
psf_path=path+'datas/image_small_stamp/psf.fits',
resized_ccd_shape=(140, 140))
mask = ma.Mask.circular(shape=image_small_stamp_padded.shape, pixel_scale=image_small_stamp_padded.pixel_scale,
radius_arcsec=2.0)
imaging_plotters.plot_image_subplot(image=image_small_stamp_padded, mask=mask)
lensing_image = li.LensData(ccd_data=image_small_stamp_padded, mask=mask)
########## IVE INCLUDED THE TEXT FOR 5 BELOW SO YOU CAN BE AWARE OF CENTERING, BUT THE BUILT IN FUNCTIONALITY FOR #####
def mask_function_annular(image):
return msk.Mask.circular_annular(shape=image.shape, pixel_scale=image.pixel_scale,
inner_radius_arcsec=0.2, outer_radius_arcsec=3.3)