def _update_resized_properties(self, reduc_indices, reduced_num_pix): self._reduc_indices_1d = util.image2array(reduc_indices).astype(bool) # update resized coordinates self._num_pix_resized = reduced_num_pix self._x_grid_1d_resized = np.copy( self._x_grid_1d[self._reduc_indices_1d]) self._y_grid_1d_resized = np.copy( self._y_grid_1d[self._reduc_indices_1d]) # can only apply reduc_indices_1d on 1D arrays, hence reshaping operations below effective_mask_1d = np.copy(util.image2array(self._effective_mask)) self._effective_mask_resized = util.array2image( effective_mask_1d[self._reduc_indices_1d]) if self._verbose: print( "SizeablePlaneGrid: source grid has been reduced from {} to {} side pixels" .format(self._num_pix, self._num_pix_resized))
def test_image2array2image(): image = np.zeros((20, 10)) nx, ny = np.shape(image) image[1, 2] = 1 array = util.image2array(image) image_new = util.array2image(array, nx, ny) assert image_new[1, 2] == image[1, 2]
def project_on_original_grid(self, image): if hasattr(self, '_reduc_indices_1d'): input_is_1d = (len(image.shape) == 1) array_large = np.zeros(self._num_pix**2) if input_is_1d: array_large[self._reduc_indices_1d] = image[:] return array_large else: array_large[self._reduc_indices_1d] = util.image2array( image)[:] return util.array2image(array_large) else: return 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
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
def __init__(self, data_class, lensing_operator_class, numerics_class, thread_count=1, random_seed=None): self._lensing_op = lensing_operator_class self._ss_factor = numerics_class.grid_supersampling_factor self._conv = numerics_class.convolution_class if self._conv is not None: self._conv_transpose = self._conv.copy_transpose() else: self._conv_transpose = None self._prepare_data(data_class, self._lensing_op.source_subgrid_resolution) self._no_source_light = True self._no_lens_light = True self._no_point_source = True self._thread_count = thread_count self._mask = np.ones_like(data_class.data) self._mask_1d = util.image2array(self._mask) self.random_seed = random_seed
def test_image2array(): image = np.zeros((10, 10)) image[1, 2] = 1 array = util.image2array(image) assert array[12] == 1
def source2image_2d(self, source, **kwargs): source_1d = util.image2array(source) return util.array2image(self.source2image(source_1d, **kwargs))
def image2source_2d(self, image, **kwargs): image_1d = util.image2array(image) return util.array2image(self.image2source(image_1d, **kwargs))
def _set_likelihood_mask(self, mask): self._mask = mask self._mask_1d = util.image2array(mask) self._lensing_op.set_likelihood_mask(mask)