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_matrix_product(self): lensing_op = LensingOperator(self.lens_model, self.image_grid_class, self.source_grid_class_default, self.num_pix, source_interpolation='nearest_legacy') lensing_op.update_mapping(self.kwargs_lens) lensing_op_mat = LensingOperator(self.lens_model, self.image_grid_class, self.source_grid_class_default, self.num_pix, source_interpolation='nearest') lensing_op_mat.update_mapping(self.kwargs_lens) source_1d = util.image2array(self.source_light_delensed) image_1d = util.image2array(self.source_light_lensed) npt.assert_equal(lensing_op.source2image(source_1d), lensing_op_mat.source2image(source_1d)) npt.assert_equal(lensing_op.image2source(image_1d), lensing_op_mat.image2source(image_1d))
def test_interpol_mapping(self): """testing than image2source / source2image are close to the parametric mapping""" lensing_op = LensingOperator(self.lens_model, self.image_grid_class, self.source_grid_class_default, self.num_pix, source_interpolation='bilinear') lensing_op.update_mapping(self.kwargs_lens) source_1d = util.image2array(self.source_light_delensed) image_1d = util.image2array(self.source_light_lensed) source_1d_lensed = lensing_op.source2image(source_1d) image_1d_delensed = lensing_op.image2source(image_1d) assert source_1d_lensed.shape == image_1d.shape assert image_1d_delensed.shape == source_1d.shape npt.assert_almost_equal(source_1d_lensed / source_1d_lensed.max(), image_1d / image_1d.max(), decimal=0.8) npt.assert_almost_equal(image_1d_delensed / image_1d_delensed.max(), source_1d / source_1d.max(), decimal=0.8)
def test_minimal_source_plane(self): source_1d = util.image2array(self.source_light_delensed) # test with no mask lensing_op = LensingOperator(self.lens_model, self.image_grid_class, self.source_grid_class_default, self.num_pix, source_interpolation='nearest', minimal_source_plane=True) lensing_op.update_mapping(self.kwargs_lens) image_1d = util.image2array(self.source_light_lensed) assert lensing_op.image2source(image_1d).size < source_1d.size # test with mask lensing_op = LensingOperator(self.lens_model, self.image_grid_class, self.source_grid_class_default, self.num_pix, source_interpolation='nearest', minimal_source_plane=True) lensing_op.set_likelihood_mask(self.likelihood_mask) lensing_op.update_mapping(self.kwargs_lens) image_1d = util.image2array(self.source_light_lensed) assert lensing_op.image2source(image_1d).size < source_1d.size # for 'bilinear' operator, only works with no mask (for now) lensing_op = LensingOperator(self.lens_model, self.image_grid_class, self.source_grid_class_default, self.num_pix, source_interpolation='bilinear', minimal_source_plane=True) lensing_op.update_mapping(self.kwargs_lens) image_1d = util.image2array(self.source_light_lensed) assert lensing_op.image2source(image_1d).size < source_1d.size
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)