def setup(self): self.num_pix = 25 # cutout pixel size self.subgrid_res_source = 2 delta_pix = 0.32 _, _, ra_at_xy_0, dec_at_xy_0, _, _, Mpix2coord, _ \ = l_util.make_grid_with_coordtransform(numPix=self.num_pix, deltapix=delta_pix, subgrid_res=1, inverse=False, left_lower=False) kwargs_data = { 'ra_at_xy_0': ra_at_xy_0, 'dec_at_xy_0': dec_at_xy_0, 'transform_pix2angle': Mpix2coord, 'image_data': np.zeros((self.num_pix, self.num_pix)) } data_class = ImageData(**kwargs_data) numerics_image = NumericsSubFrame(data_class, PSF('NONE')) numerics_source = NumericsSubFrame( data_class, PSF('NONE'), supersampling_factor=self.subgrid_res_source) self.source_plane = SizeablePlaneGrid(numerics_source.grid_class, verbose=True) # create a mask mimicking the real case of lensing operation lens_model_class = LensModel(['SIE']) kwargs_lens = [{ 'theta_E': 1.5, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1 }] lensing_op = LensingOperator(lens_model_class, numerics_image.grid_class, numerics_source.grid_class, self.num_pix, self.subgrid_res_source) lensing_op.update_mapping(kwargs_lens) unit_image = np.ones((self.num_pix, self.num_pix)) mask_image = np.zeros((self.num_pix, self.num_pix)) mask_image[2:-2, 2:-2] = 1 # some binary image that mask out borders self.unit_image_mapped = lensing_op.image2source_2d(unit_image, no_flux_norm=False) self.mask_mapped = lensing_op.image2source_2d(mask_image)
def test_image2source(self): lensing_op = LensingOperator(self.lens_model, self.image_grid_class, self.source_grid_class_default, self.num_pix) image_1d = util.image2array(self.source_light_lensed) image_1d_delensed = lensing_op.image2source( image_1d, kwargs_lens=self.kwargs_lens) assert len(image_1d_delensed.shape) == 1 image_2d = self.source_light_lensed image_2d_delensed = lensing_op.image2source_2d( image_2d, kwargs_lens=self.kwargs_lens, update_mapping=True) assert len(image_2d_delensed.shape) == 2
class TestModelOperators(object): """ tests the Lensing Operator classes """ def setup(self): self.num_pix = 49 # cutout pixel size self.subgrid_res_source = 2 self.num_pix_source = self.num_pix * self.subgrid_res_source delta_pix = 0.24 _, _, ra_at_xy_0, dec_at_xy_0, _, _, Mpix2coord, _ \ = l_util.make_grid_with_coordtransform(numPix=self.num_pix, deltapix=delta_pix, subgrid_res=1, inverse=False, left_lower=False) self.image_data = np.random.rand(self.num_pix, self.num_pix) kwargs_data = { 'ra_at_xy_0': ra_at_xy_0, 'dec_at_xy_0': dec_at_xy_0, 'transform_pix2angle': Mpix2coord, 'image_data': self.image_data, } data = ImageData(**kwargs_data) lens_model = LensModel(['SPEP']) kwargs_lens = [{ 'theta_E': 1, 'gamma': 2, 'center_x': 0, 'center_y': 0, 'e1': -0.05, 'e2': 0.05 }] # PSF kernel_pixel = np.zeros((self.num_pix, self.num_pix)) kernel_pixel[int(self.num_pix / 2), int(self.num_pix / 2)] = 1 # just a dirac here kwargs_psf = {'psf_type': 'PIXEL', 'kernel_point_source': kernel_pixel} psf = PSF(**kwargs_psf) # wavelets scales for lens and source self.n_scales_source = 4 self.n_scales_lens = 3 # list of source light profiles source_model = LightModel(['SLIT_STARLETS']) self.kwargs_source = [{'n_scales': self.n_scales_source}] # list of lens light profiles lens_light_model = LightModel(['SLIT_STARLETS']) self.kwargs_lens_light = [{'n_scales': self.n_scales_lens}] # define some mask likelihood_mask = np.ones((self.num_pix, self.num_pix)) # get grid classes self.numerics = NumericsSubFrame(data, psf) image_grid_class = self.numerics.grid_class source_numerics = NumericsSubFrame( data, psf, supersampling_factor=self.subgrid_res_source) source_grid_class = source_numerics.grid_class # get a lensing operator self.lensing_op = LensingOperator(lens_model, image_grid_class, source_grid_class, self.num_pix) self.lensing_op.update_mapping(kwargs_lens) self.model_op = ModelOperators(data, self.lensing_op, self.numerics) self.model_op._set_likelihood_mask(likelihood_mask) self.model_op.add_source_light(source_model) self.model_op.add_lens_light(lens_light_model) self.model_op_nolens = ModelOperators(data, self.lensing_op, self.numerics) self.model_op_nolens._set_likelihood_mask(likelihood_mask) self.model_op_nolens.add_source_light(source_model) # define some test images in direct space self.X_s = np.random.rand(self.num_pix_source, self.num_pix_source) # source light self.X_l = np.random.rand(self.num_pix, self.num_pix) # lens light # define some test images in wavelets space self.alpha_s = np.random.rand(self.n_scales_source, self.num_pix_source, self.num_pix_source) # source light self.alpha_l = np.random.rand(self.n_scales_lens, self.num_pix, self.num_pix) # lens light def test_set_wavelet_scales(self): self.model_op.set_source_wavelet_scales(self.n_scales_source) Phi_T_s_X = self.model_op.Phi_T_s(self.X_s) self.model_op.set_lens_wavelet_scales(self.n_scales_lens) Phi_T_l_X = self.model_op.Phi_T_l(self.X_l) # test that transformed image has the right shape in terms of number of scales assert Phi_T_s_X.shape[0] == self.n_scales_source assert Phi_T_l_X.shape[0] == self.n_scales_lens def test_subtract_from_data_and_reset(self): image_to_subtract = np.eye(self.num_pix, self.num_pix) self.model_op.subtract_from_data(image_to_subtract) npt.assert_equal(self.model_op.Y, self.image_data) npt.assert_equal(self.model_op.Y_eff, self.image_data - image_to_subtract) self.model_op.reset_data() npt.assert_equal(self.model_op.Y, self.image_data) npt.assert_equal(self.model_op.Y_eff, self.image_data) def test_spectral_norm_source(self): self.model_op.set_source_wavelet_scales(self.n_scales_source) npt.assert_almost_equal(self.model_op.spectral_norm_source, 0.97, decimal=2) def test_spectral_norm_lens(self): self.model_op.set_lens_wavelet_scales(self.n_scales_lens) npt.assert_almost_equal(self.model_op.spectral_norm_lens, 0.99, decimal=2) def test_data_terms(self): npt.assert_equal(self.model_op.Y, self.image_data) npt.assert_equal(self.model_op.Y_eff, self.image_data) def test_convolution(self): H_X_s = self.model_op.H(self.X_s) npt.assert_equal( H_X_s, self.numerics.convolution_class.convolution2d(self.X_s)) H_T_X_s = self.model_op.H_T(self.X_s) conv_transpose = self.numerics.convolution_class.copy_transpose() npt.assert_equal(H_T_X_s, conv_transpose.convolution2d(self.X_s)) def test_lensing(self): F_X_s = self.model_op.F(self.X_s) npt.assert_equal(F_X_s, self.lensing_op.source2image_2d(self.X_s)) F_T_X_l = self.model_op.F_T(self.X_l) npt.assert_equal(F_T_X_l, self.lensing_op.image2source_2d(self.X_l)) def test_wavelet_transform(self): # TODO : do more accurate tests here self.model_op.set_source_wavelet_scales(self.n_scales_source) self.model_op.set_lens_wavelet_scales(self.n_scales_lens) Phi_alpha_s = self.model_op.Phi_s(self.alpha_s) Phi_alpha_l = self.model_op.Phi_l(self.alpha_l) assert Phi_alpha_s.shape == (self.num_pix * self.subgrid_res_source, self.num_pix * self.subgrid_res_source) assert Phi_alpha_l.shape == (self.num_pix, self.num_pix) Phi_T_X_s = self.model_op.Phi_T_s(self.X_s) Phi_T_X_l = self.model_op.Phi_T_l(self.X_l) assert Phi_T_X_s.shape == (self.n_scales_source, self.num_pix * self.subgrid_res_source, self.num_pix * self.subgrid_res_source) assert Phi_T_X_l.shape == (self.n_scales_lens, self.num_pix, self.num_pix)
class TestNoiseLevels(object): """ tests the Lensing Operator classes """ def setup(self): self.num_pix = 49 # cutout pixel size self.subgrid_res_source = 2 self.num_pix_source = self.num_pix * self.subgrid_res_source self.background_rms = 0.05 self.noise_map = self.background_rms * np.ones( (self.num_pix, self.num_pix)) delta_pix = 0.24 _, _, ra_at_xy_0, dec_at_xy_0, _, _, Mpix2coord, _ \ = l_util.make_grid_with_coordtransform(numPix=self.num_pix, deltapix=delta_pix, subgrid_res=1, inverse=False, left_lower=False) self.image_data = np.random.rand(self.num_pix, self.num_pix) kwargs_data = { 'ra_at_xy_0': ra_at_xy_0, 'dec_at_xy_0': dec_at_xy_0, 'transform_pix2angle': Mpix2coord, 'image_data': self.image_data, 'background_rms': self.background_rms, 'noise_map': self.noise_map, } data = ImageData(**kwargs_data) gaussian_func = Gaussian() x, y = l_util.make_grid(41, 1) gaussian = gaussian_func.function(x, y, amp=1, sigma=0.02, center_x=0, center_y=0) self.psf_kernel = gaussian / gaussian.sum() lens_model = LensModel(['SPEP']) self.kwargs_lens = [{ 'theta_E': 1, 'gamma': 2, 'center_x': 0, 'center_y': 0, 'e1': -0.05, 'e2': 0.05 }] # wavelets scales for lens and source self.n_scales_source = 4 self.n_scales_lens = 3 # list of source light profiles self.source_model = LightModel(['SLIT_STARLETS']) self.kwargs_source = [{'n_scales': self.n_scales_source}] # list of lens light profiles self.lens_light_model = LightModel(['SLIT_STARLETS']) self.kwargs_lens_light = [{'n_scales': self.n_scales_lens}] # get grid classes image_grid_class = NumericsSubFrame(data, PSF('NONE')).grid_class source_grid_class = NumericsSubFrame( data, PSF('NONE'), supersampling_factor=self.subgrid_res_source).grid_class # get a lensing operator self.lensing_op = LensingOperator(lens_model, image_grid_class, source_grid_class, self.num_pix) self.noise_class = NoiseLevels( data, subgrid_res_source=self.subgrid_res_source, include_regridding_error=False) self.noise_class_regrid = NoiseLevels( data, subgrid_res_source=self.subgrid_res_source, include_regridding_error=True) def test_background_rms(self): assert self.background_rms == self.noise_class.background_rms def test_noise_map(self): npt.assert_equal(self.noise_map, self.noise_class.noise_map) npt.assert_equal(self.noise_map, self.noise_class_regrid.noise_map) npt.assert_equal(self.noise_map, self.noise_class.effective_noise_map) def test_update_source_levels(self): wavelet_transform_source = lambda x: self.source_model.func_list[ 0].decomposition_2d(x, self.kwargs_source[0]['n_scales']) image2source_transform = lambda x: self.lensing_op.image2source_2d( x, kwargs_lens=self.kwargs_lens) upscale_transform = lambda x: x self.noise_class.update_source_levels( self.num_pix, self.num_pix_source, wavelet_transform_source, image2source_transform, upscale_transform, psf_kernel=None) # without psf_kernel specified assert self.noise_class.levels_source.shape == (self.n_scales_source, self.num_pix_source, self.num_pix_source) self.noise_class.update_source_levels(self.num_pix, self.num_pix_source, wavelet_transform_source, image2source_transform, upscale_transform, psf_kernel=self.psf_kernel) assert self.noise_class.levels_source.shape == (self.n_scales_source, self.num_pix_source, self.num_pix_source) def test_update_image_levels(self): wavelet_transform_image = lambda x: self.lens_light_model.func_list[ 0].decomposition_2d(x, self.kwargs_lens_light[0]['n_scales']) self.noise_class.update_image_levels(self.num_pix, wavelet_transform_image) assert self.noise_class.levels_image.shape == (self.n_scales_lens, self.num_pix, self.num_pix) def test_update_regridding_error(self): magnification_map = self.lensing_op.magnification_map(self.kwargs_lens) self.noise_class.update_regridding_error( magnification_map) # should do nothing npt.assert_equal(self.noise_class.effective_noise_map, self.noise_map) self.noise_class_regrid.update_regridding_error(magnification_map) npt.assert_equal( self.noise_class_regrid.effective_noise_map, np.sqrt(self.noise_map**2 + self.noise_class_regrid.regridding_error_map**2))