def kwargs_data(self): """ :return: keyword arguments for ImageData class instance """ # default pixel grid if self._kwargs_pixel_grid is None: _, _, ra_at_xy_0, dec_at_xy_0, _, _, transform_pix2angle, _ = util.make_grid_with_coordtransform( numPix=self.numpix, deltapix=self.pixel_scale, subgrid_res=1, left_lower=False, inverse=False) # user defined pixel grid else: ra_at_xy_0 = self._kwargs_pixel_grid['ra_at_xy_0'] dec_at_xy_0 = self._kwargs_pixel_grid['dec_at_xy_0'] transform_pix2angle = self._kwargs_pixel_grid[ 'transform_pix2angle'] # CCD gain corrected exposure time to allow a direct Poisson estimates based on IID counts scaled_exposure_time = self.flux_iid(1) kwargs_data = { 'image_data': np.zeros((self.numpix, self.numpix)), 'ra_at_xy_0': ra_at_xy_0, 'dec_at_xy_0': dec_at_xy_0, 'transform_pix2angle': transform_pix2angle, 'background_rms': self.background_noise, 'exposure_time': scaled_exposure_time } return kwargs_data
def set_kwargs_data_joint(self, image, measured_td, measured_td_sigma, survey_object_dict, eff_exposure_time=5400.0, inverse=False): """Feed in time delay and imaging data, different across lenses Parameters ---------- image : np.array, of shape [n_pix, n_pix] measured_td : np.array, of shape [n_img - 1,] measured_td_sigma : float or np.array of shape [n_img - 1,] """ num_pix = image.shape[0] for i, (bp, survey_object) in enumerate( survey_object_dict.items()): # FIXME: only single band noise_kwargs = survey_object.kwargs_single_band() noise_kwargs['exposure_time'] = eff_exposure_time psf_kernel_size = survey_object.psf_kernel_size which_psf_maps = survey_object.which_psf_maps _, _, ra_0, dec_0, _, _, Mpix2coord, _ = util.make_grid_with_coordtransform( numPix=num_pix, deltapix=noise_kwargs['pixel_scale'], center_ra=0, center_dec=0, subgrid_res=1, inverse=inverse) noise_dict = noise_lenstronomy.get_noise_sigma2_lenstronomy( image, **noise_kwargs) pixel_rms = (noise_dict['sky'] + noise_dict['readout'])**0.5 total_exptime = noise_kwargs['exposure_time'] * noise_kwargs[ 'num_exposures'] kwargs_data = dict( background_rms=pixel_rms, exposure_time=total_exptime, ra_at_xy_0=ra_0, dec_at_xy_0=dec_0, transform_pix2angle=Mpix2coord, image_data=image, ) psf_model = get_PSF_model(psf_type=noise_kwargs['psf_type'], pixel_scale=noise_kwargs['pixel_scale'], seeing=noise_kwargs['seeing'], kernel_size=psf_kernel_size, which_psf_maps=which_psf_maps) kwargs_psf = dict( psf_type=psf_model.psf_type, pixel_size=psf_model._pixel_size, kernel_point_source=psf_model._kernel_point_source, ) image_band = [kwargs_data, kwargs_psf, self.kwargs_numerics] self.multi_band_list = [image_band] # single band self.kwargs_data_joint = dict( multi_band_list=self.multi_band_list, multi_band_type='multi-linear', time_delays_measured=measured_td, time_delays_uncertainties=measured_td_sigma)
def setup(self): self.num_pix = 20 # cutout pixel size delta_pix = 0.2 background_rms = 0.05 _, _, 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) # imaging data class 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)), 'background_rms': background_rms, 'noise_map': background_rms * np.ones( (self.num_pix, self.num_pix)), } data_class = ImageData(**kwargs_data) # lens mass class lens_model_class = LensModel(['SPEP']) self.kwargs_lens = [{ 'theta_E': 1, 'gamma': 2, 'center_x': 0, 'center_y': 0, 'e1': -0.05, 'e2': 0.05 }] # source light class source_model_class = LightModel(['SLIT_STARLETS']) self.kwargs_source = [{ 'coeffs': 0, 'n_scales': 3, 'n_pixels': self.num_pix**2 }] # define numerics classes image_numerics_class = NumericsSubFrame(pixel_grid=data_class, psf=PSF(psf_type='NONE')) source_numerics_class = NumericsSubFrame(pixel_grid=data_class, psf=PSF(psf_type='NONE'), supersampling_factor=1) # init sparse solver self.solver = SparseSolverSource(data_class, lens_model_class, image_numerics_class, source_numerics_class, source_model_class, num_iter_source=10) # init the tracker self.tracker_alone = SolverTracker(self.solver, verbose=True)
def setup(self): self.num_pix = 25 # cutout pixel size self.delta_pix = 0.24 _, _, ra_at_xy_0, dec_at_xy_0, _, _, Mpix2coord, _ \ = l_util.make_grid_with_coordtransform(numPix=self.num_pix, deltapix=self.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)) } numerics = NumericsSubFrame(ImageData(**kwargs_data), PSF('NONE')) self.plane_grid = PlaneGrid(numerics.grid_class)
def __init__(self, *args, **kwargs): super(TestRaise, self).__init__(*args, **kwargs) self.num_pix = 49 # cutout pixel size self.subgrid_res_source = 1 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) image_data = np.random.rand(self.num_pix, self.num_pix) self.kwargs_data = { 'ra_at_xy_0': ra_at_xy_0, 'dec_at_xy_0': dec_at_xy_0, 'transform_pix2angle': Mpix2coord, 'image_data': image_data, 'background_rms': 0.01, 'noise_map': 0.01 * np.ones_like(image_data), } self.data = ImageData(**self.kwargs_data) self.lens_model_class = LensModel(['SPEP']) self.kwargs_lens = [{ 'theta_E': 1, 'gamma': 2, 'center_x': 0, 'center_y': 0, 'e1': -0.05, 'e2': 0.05 }] self.source_model_class = LightModel(['SLIT_STARLETS']) self.lens_light_model_class = LightModel(['SLIT_STARLETS']) self.kwargs_source = [{'n_scales': 4}] self.kwargs_lens_light = [{'n_scales': 4}] psf = PSF(psf_type='NONE') self.numerics = NumericsSubFrame(pixel_grid=self.data, psf=psf) self.source_numerics = NumericsSubFrame( pixel_grid=self.data, psf=psf, supersampling_factor=self.subgrid_res_source) self.solver_source_lens = SparseSolverSourceLens( self.data, self.lens_model_class, self.numerics, self.source_numerics, self.source_model_class, self.lens_light_model_class, num_iter_source=1, num_iter_lens=1, num_iter_weights=1)
def test_raise(self): with self.assertRaises(ValueError): # test rectangular size num_pix_x = 25 num_pix_y = 30 delta_pix = 0.24 _, _, ra_at_xy_0, dec_at_xy_0, _, _, Mpix2coord, _ \ = l_util.make_grid_with_coordtransform(numPix=num_pix_x, 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((num_pix_x, num_pix_y)) } numerics = NumericsSubFrame(ImageData(**kwargs_data), PSF('NONE')) plane_grid = PlaneGrid(numerics.grid_class)
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 __init__(self, *args, **kwargs): super(TestRaise, self).__init__(*args, **kwargs) self.num_pix = 10 _, _, ra_at_xy_0, dec_at_xy_0, _, _, Mpix2coord, _ \ = l_util.make_grid_with_coordtransform(numPix=self.num_pix, deltapix=0.5, subgrid_res=1, inverse=False, left_lower=False) kwargs_data_nonsquare = { '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 + 10)), # non-square image } 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)), # non-square image } self.data_nonsquare = ImageData(**kwargs_data_nonsquare) self.data = ImageData(**kwargs_data) psf = PSF('NONE') self.numerics = NumericsSubFrame(self.data, psf) lens_model = LensModel(['SPEP']) self.source_model_class = LightModel(['SLIT_STARLETS']) self.lens_light_model_class = LightModel(['SLIT_STARLETS']) # get grid classes image_grid_class = self.numerics.grid_class source_numerics = NumericsSubFrame(self.data, psf, supersampling_factor=1) source_grid_class = source_numerics.grid_class self.lensing_op = LensingOperator(lens_model, image_grid_class, source_grid_class, self.num_pix) self.model_op = ModelOperators(self.data, self.lensing_op, self.numerics) self.model_op.add_lens_light(self.lens_light_model_class) self.model_op_nolens = ModelOperators(self.data, self.lensing_op, self.numerics)
def data_class(self): """ creates a Data() instance of lenstronomy based on knowledge of the observation :return: instance of Data() class """ x_grid, y_grid, ra_at_xy_0, dec_at_xy_0, x_at_radec_0, y_at_radec_0, Mpix2coord, Mcoord2pix = util.make_grid_with_coordtransform( numPix=self.numpix, deltapix=self.pixel_scale, subgrid_res=1, left_lower=False, inverse=False) kwargs_data = {'image_data': np.zeros((self.numpix, self.numpix)), 'ra_at_xy_0': ra_at_xy_0, 'dec_at_xy_0': dec_at_xy_0, 'transform_pix2angle': Mpix2coord, 'background_rms': self.background_noise, 'exposure_time': self.scaled_exposure_time} data_class = ImageData(**kwargs_data) return data_class
def test_grid_with_coords(): numPix = 11 deltaPix = 1. x_grid, y_grid, ra_at_xy_0, dec_at_xy_0, x_at_radec_0, y_at_radec_0, Mpix2coord, Mcoord2pix = util.make_grid_with_coordtransform( numPix, deltaPix, subgrid_res=1, left_lower=False) ra = 0 dec = 0 x, y = util.map_coord2pix(ra, dec, x_at_radec_0, y_at_radec_0, Mcoord2pix) assert x == 5 assert y == 5 numPix = 11 deltaPix = .1 x_grid, y_grid, ra_at_xy_0, dec_at_xy_0, x_at_radec_0, y_at_radec_0, Mpix2coord, Mcoord2pix = util.make_grid_with_coordtransform( numPix, deltaPix, subgrid_res=1, left_lower=False) ra = 0 dec = 0 x, y = util.map_coord2pix(ra, dec, x_at_radec_0, y_at_radec_0, Mcoord2pix) assert x == 5 assert y == 5 numPix = 11 deltaPix = 1. x_grid, y_grid, ra_at_xy_0, dec_at_xy_0, x_at_radec_0, y_at_radec_0, Mpix2coord, Mcoord2pix = util.make_grid_with_coordtransform( numPix, deltaPix, subgrid_res=1, left_lower=False, inverse=True) x_, y_ = 0, 0 ra, dec = util.map_coord2pix(x_, y_, ra_at_xy_0, dec_at_xy_0, Mpix2coord) assert ra == 5 assert dec == -5 numPix = 11 deltaPix = 1. x_grid, y_grid, ra_at_xy_0, dec_at_xy_0, x_at_radec_0, y_at_radec_0, Mpix2coord, Mcoord2pix = util.make_grid_with_coordtransform( numPix, deltaPix, subgrid_res=1, left_lower=False, inverse=False) x_, y_ = 0, 0 ra, dec = util.map_coord2pix(x_, y_, ra_at_xy_0, dec_at_xy_0, Mpix2coord) assert ra == -5 assert dec == -5 numPix = 11 deltaPix = .1 x_grid, y_grid, ra_at_xy_0, dec_at_xy_0, x_at_radec_0, y_at_radec_0, Mpix2coord, Mcoord2pix = util.make_grid_with_coordtransform( numPix, deltaPix, subgrid_res=1, left_lower=False) x_, y_ = 0, 0 ra, dec = util.map_coord2pix(x_, y_, ra_at_xy_0, dec_at_xy_0, Mpix2coord) assert ra == .5 assert dec == -.5 x__, y__ = util.map_coord2pix(ra, dec, x_at_radec_0, y_at_radec_0, Mcoord2pix) assert x__ == x_ assert y__ == y_ numPix = 11 deltaPix = .1 x_grid, y_grid, ra_at_xy_0, dec_at_xy_0, x_at_radec_0, y_at_radec_0, Mpix2coord, Mcoord2pix = util.make_grid_with_coordtransform( numPix, deltaPix, subgrid_res=1, left_lower=True) assert ra_at_xy_0 == 0 assert dec_at_xy_0 == 0
def data_configure(self, numPix, deltaPix, exposure_time=1, sigma_bkg=1): """ configures the data keyword arguments with a coordinate grid centered at zero. :param numPix: number of pixel (numPix x numPix) :param deltaPix: pixel size :param exposure_time: exposure time :param sigma_bkg: background noise (Gaussian sigma) :return: """ mean = 0. # background mean flux (default zero) # 1d list of coordinates (x,y) of a numPix x numPix square grid, centered to zero x_grid, y_grid, ra_at_xy_0, dec_at_xy_0, x_at_radec_0, y_at_radec_0, Mpix2coord, Mcoord2pix = util.make_grid_with_coordtransform(numPix=numPix, deltapix=deltaPix, subgrid_res=1) # mask (1= model this pixel, 0= leave blanck) exposure_map = np.ones((numPix, numPix)) * exposure_time # individual exposure time/weight per pixel kwargs_data = { 'background_rms': sigma_bkg, 'mean_background': mean, 'exposure_map': exposure_map , 'x_coords': x_grid, 'y_coords': y_grid , 'ra_at_xy_0': ra_at_xy_0, 'dec_at_xy_0': dec_at_xy_0, 'transform_pix2angle': Mpix2coord , 'image_data': np.zeros((numPix, numPix)) } data_class = Data(kwargs_data) #return kwargs_data return data_class
def data_configure_simple(numPix, deltaPix, exposure_time=1, sigma_bkg=1, inverse=False): """ configures the data keyword arguments with a coordinate grid centered at zero. :param numPix: number of pixel (numPix x numPix) :param deltaPix: pixel size (in angular units) :param exposure_time: exposure time :param sigma_bkg: background noise (Gaussian sigma) :param inverse: if True, coordinate system is ra to the left, if False, to the right :return: keyword arguments that can be used to construct a Data() class instance of lenstronomy """ mean = 0. # background mean flux (default zero) # 1d list of coordinates (x,y) of a numPix x numPix square grid, centered to zero x_grid, y_grid, ra_at_xy_0, dec_at_xy_0, x_at_radec_0, y_at_radec_0, Mpix2coord, Mcoord2pix = util.make_grid_with_coordtransform( numPix=numPix, deltapix=deltaPix, subgrid_res=1, inverse=inverse) # mask (1= model this pixel, 0= leave blanck) exposure_map = np.ones( (numPix, numPix)) * exposure_time # individual exposure time/weight per pixel kwargs_data = { 'background_rms': sigma_bkg, 'exposure_map': exposure_map, 'ra_at_xy_0': ra_at_xy_0, 'dec_at_xy_0': dec_at_xy_0, 'transform_pix2angle': Mpix2coord, 'image_data': np.zeros((numPix, numPix)) } return kwargs_data
def setup(self): self.num_pix = 20 # cutout pixel size delta_pix = 0.2 _, _, 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) # imaging data class gaussian = Gaussian() x, y = l_util.make_grid(self.num_pix, 1) gaussian1 = gaussian.function(x, y, amp=5, sigma=1, center_x=-7, center_y=-7) gaussian2 = gaussian.function(x, y, amp=20, sigma=2, center_x=-3, center_y=-3) gaussian3 = gaussian.function(x, y, amp=60, sigma=4, center_x=+5, center_y=+5) image_data = util.array2image(gaussian1 + gaussian2 + gaussian3) background_rms = 0.1 image_data += background_rms * np.random.randn(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': image_data, 'background_rms': background_rms, 'noise_map': background_rms * np.ones_like(image_data), } data_class = ImageData(**kwargs_data) # lens mass class lens_model_class = LensModel(['SPEP']) self.kwargs_lens = [{ 'theta_E': 1, 'gamma': 2, 'center_x': 0, 'center_y': 0, 'e1': -0.05, 'e2': 0.05 }] # source light class source_model_class = LightModel(['SLIT_STARLETS']) self.kwargs_source = [{ 'coeffs': 0, 'n_scales': 3, 'n_pixels': self.num_pix**2 }] # define numerics classes image_numerics_class = NumericsSubFrame(pixel_grid=data_class, psf=PSF(psf_type='NONE')) source_numerics_class = NumericsSubFrame(pixel_grid=data_class, psf=PSF(psf_type='NONE'), supersampling_factor=1) # init sparse solver self.solver = SparseSolverSource(data_class, lens_model_class, image_numerics_class, source_numerics_class, source_model_class, num_iter_source=10) # init the plotter self.plotter = SolverPlotter(self.solver, show_now=False)
def source(self, numPix, deltaPix, center=None, image_orientation=True): """ :param numPix: number of pixels per axes :param deltaPix: pixel size :param image_orientation: bool, if True, uses frame in orientation of the image, otherwise in RA-DEC coordinates :return: 2d surface brightness grid of the reconstructed source and Coordinates() instance of source grid """ if image_orientation is True: Mpix2coord = self._coords.transform_pix2angle * deltaPix / self._deltaPix x_grid_source, y_grid_source = util.make_grid_transformed( numPix, Mpix2Angle=Mpix2coord) ra_at_xy_0, dec_at_xy_0 = x_grid_source[0], y_grid_source[0] else: x_grid_source, y_grid_source, ra_at_xy_0, dec_at_xy_0, x_at_radec_0, y_at_radec_0, Mpix2coord, Mcoord2pix = util.make_grid_with_coordtransform( numPix, deltaPix) center_x = 0 center_y = 0 if center is not None: center_x, center_y = center[0], center[1] elif len(self._kwargs_source_partial) > 0: center_x = self._kwargs_source_partial[0]['center_x'] center_y = self._kwargs_source_partial[0]['center_y'] x_grid_source += center_x y_grid_source += center_y coords_source = Coordinates(transform_pix2angle=Mpix2coord, ra_at_xy_0=ra_at_xy_0 + center_x, dec_at_xy_0=dec_at_xy_0 + center_y) source = self._bandmodel.SourceModel.surface_brightness( x_grid_source, y_grid_source, self._kwargs_source_partial) source = util.array2image(source) * deltaPix**2 return source, coords_source
def select_image(self, image_index, block=True): """ Select the image to conduct forward modeling on. Parameters: image_index (int): The index of the image to use. """ # Load the metadata file metadata = pd.read_csv( os.path.join(self.cfg['validation_params']['root_path'], 'metadata.csv')) # Get the image filename img_filename = 'X_{0:07d}.npy'.format(image_index) # Load the true image. self.true_image = np.load( os.path.join(self.cfg['validation_params']['root_path'], img_filename)).astype(np.float32) # Show the image without noise print('True image without noise.') plt.imshow(self.true_image, cmap=cm.magma) plt.colorbar() plt.show(block=block) # Set the random seed since we will be using it to add # noise tf.random.set_seed(self.cfg['training_params']['random_seed']) # Add noise and show the new image_index self.true_image_noise = self.noise_function.add_noise( self.true_image).numpy() print('True image with noise.') plt.imshow(self.true_image_noise, cmap=cm.magma) plt.colorbar() plt.show(block=block) # Extract the data kwargs (including noise kwargs) being used # by lenstronomy. _, _, ra_0, dec_0, _, _, Mpix2coord, _ = ( util.make_grid_with_coordtransform( numPix=self.baobab_cfg.image['num_pix'], deltapix=self.baobab_cfg.instrument['pixel_scale'], center_ra=0, center_dec=0, subgrid_res=1, inverse=self.baobab_cfg.image['inverse'])) # Update the lenstronomy kwargs with the image information noise_dict = noise_lenstronomy.get_noise_sigma2_lenstronomy( self.true_image_noise, **self.noise_kwargs) self.ls_kwargs_data = { 'background_rms': np.sqrt(noise_dict['sky'] + noise_dict['readout']), 'exposure_time': (self.noise_kwargs['exposure_time'] * self.noise_kwargs['num_exposures']), 'ra_at_xy_0': ra_0, 'dec_at_xy_0': dec_0, 'transform_pix2angle': Mpix2coord, 'image_data': self.true_image_noise } self.ls_multi_band_list = [[ self.ls_kwargs_data, self.ls_kwargs_psf, self.ls_kwargs_numerics ]] # Find, save, and print the parameters for this image. image_data = metadata[metadata['img_filename'] == img_filename] self.true_values = image_data.to_dict(orient='index')[image_index] print('Image data') print(self.true_values) # Note that image has been selected. self.image_selected = True
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 kwargs_data(self): """ :return: keyword arguments for ImageData class instance """ x_grid, y_grid, ra_at_xy_0, dec_at_xy_0, x_at_radec_0, y_at_radec_0, Mpix2coord, Mcoord2pix = util.make_grid_with_coordtransform( numPix=self.numpix, deltapix=self.pixel_scale, subgrid_res=1, left_lower=False, inverse=False) # CCD gain corrected exposure time to allow a direct Poisson estimates based on IID counts scaled_exposure_time = self.flux_iid(1) kwargs_data = { 'image_data': np.zeros((self.numpix, self.numpix)), 'ra_at_xy_0': ra_at_xy_0, 'dec_at_xy_0': dec_at_xy_0, 'transform_pix2angle': Mpix2coord, 'background_rms': self.background_noise, 'exposure_time': scaled_exposure_time } return kwargs_data
def test_shift_coords(self): numPix = 10 deltaPix = 0.05 x_grid, y_grid, ra_at_xy_0, dec_at_xy_0, x_at_radec_0, y_at_radec_0, Mpix2coord, Mcoord2pix = util.make_grid_with_coordtransform(numPix=numPix, deltapix=deltaPix, subgrid_res=1, inverse=True) # mask (1= model this pixel, 0= leave blanck) kwargs_data = {'ra_at_xy_0': ra_at_xy_0, 'dec_at_xy_0': dec_at_xy_0, 'transform_pix2angle': Mpix2coord, 'image_data': np.ones((numPix, numPix))} data = ImageData(**kwargs_data) ra_shift = 0.05 dec_shift = 0. kwargs_data['ra_shift'] = ra_shift kwargs_data['dec_shift'] = dec_shift data_shift = ImageData(**kwargs_data) ra, dec = data.map_pix2coord(1, 1) ra_new, dec_new = data_shift.map_pix2coord(1, 1) npt.assert_almost_equal(ra_new - ra, ra_shift, decimal=10) npt.assert_almost_equal(dec_new - dec, dec_shift, decimal=10) ra_2, dec_2 = data_shift.map_pix2coord(2, 1) npt.assert_almost_equal(ra, ra_2, decimal=10) npt.assert_almost_equal(dec, dec_2, decimal=10) x, y = data.map_coord2pix(0, 0) x_new, y_new = data_shift.map_coord2pix(ra_shift, dec_shift) npt.assert_almost_equal(x, x_new, decimal=10) npt.assert_almost_equal(y, y_new, decimal=10)
def setup(self): self.num_pix = 25 # cutout pixel size 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) kwargs_data = { #'background_rms': background_rms, #'exposure_time': np.ones((self.num_pix, self.num_pix)) * exp_time, # individual exposure time/weight per pixel '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)) } self.data = ImageData(**kwargs_data) self.lens_model = LensModel(['SPEP']) self.kwargs_lens = [{ 'theta_E': 1, 'gamma': 2, 'center_x': 0, 'center_y': 0, 'e1': -0.05, 'e2': 0.05 }] self.kwargs_lens_null = [{ 'theta_E': 0, 'gamma': 2, 'center_x': 0, 'center_y': 0, 'e1': 0, 'e2': 0 }] # PSF specification kwargs_psf = {'psf_type': 'NONE'} self.psf = PSF(**kwargs_psf) # list of source light profiles source_model_list = ['SERSIC_ELLIPSE'] kwargs_sersic_ellipse_source = { 'amp': 2000, 'R_sersic': 0.6, 'n_sersic': 1, 'e1': 0.1, 'e2': 0.1, 'center_x': 0.3, 'center_y': 0.3 } kwargs_source = [kwargs_sersic_ellipse_source] source_model = LightModel(light_model_list=source_model_list) # list of lens light profiles lens_light_model_list = [] kwargs_lens_light = [{}] lens_light_model = LightModel(light_model_list=lens_light_model_list) kwargs_numerics = { 'supersampling_factor': 1, 'supersampling_convolution': False } self.image_model = ImageModel(self.data, self.psf, self.lens_model, source_model, lens_light_model, point_source_class=None, kwargs_numerics=kwargs_numerics) self.image_grid_class = self.image_model.ImageNumerics.grid_class self.source_grid_class_default = NumericsSubFrame(self.data, self.psf).grid_class # create simulated image image_sim_no_noise = self.image_model.image(self.kwargs_lens, kwargs_source, kwargs_lens_light) self.source_light_lensed = image_sim_no_noise self.data.update_data(image_sim_no_noise) # source only, in source plane, on same grid as data self.source_light_delensed = self.image_model.source_surface_brightness( kwargs_source, unconvolved=False, de_lensed=True) # define some auto mask for tests self.likelihood_mask = np.zeros_like(self.source_light_lensed) self.likelihood_mask[self.source_light_lensed > 0.1 * self.source_light_lensed.max()] = 1
def test_rebin_coord_transform(): x_grid, y_grid, ra_at_xy_0, dec_at_xy_0, x_at_radec_0, y_at_radec_0, Mpix2coord, Mcoord2pix = util.make_grid_with_coordtransform( numPix=3, deltapix=0.03, subgrid_res=1) x_grid, y_grid, ra_at_xy_0_re, dec_at_xy_0_re, x_at_radec_0_re, y_at_radec_0_re, Mpix2coord_re, Mcoord2pix_re = util.make_grid_with_coordtransform( numPix=1, deltapix=0.09, subgrid_res=1) ra_at_xy_0_resized, dec_at_xy_0_resized, x_at_radec_0_resized, y_at_radec_0_resized, Mpix2coord_resized, Mcoord2pix_resized = image_util.rebin_coord_transform( 3, x_at_radec_0, y_at_radec_0, Mpix2coord, Mcoord2pix) assert ra_at_xy_0_resized == ra_at_xy_0_re assert dec_at_xy_0_resized == dec_at_xy_0_re assert x_at_radec_0_resized == x_at_radec_0_re assert y_at_radec_0_resized == y_at_radec_0_re npt.assert_almost_equal(Mcoord2pix_resized[0][0], Mcoord2pix_re[0][0], decimal=8) npt.assert_almost_equal(Mpix2coord_re[0][0], Mpix2coord_resized[0][0], decimal=8) x_grid, y_grid, ra_at_xy_0, dec_at_xy_0, x_at_radec_0, y_at_radec_0, Mpix2coord, Mcoord2pix = util.make_grid_with_coordtransform( numPix=100, deltapix=0.05, subgrid_res=1) x_grid, y_grid, ra_at_xy_0_re, dec_at_xy_0_re, x_at_radec_0_re, y_at_radec_0_re, Mpix2coord_re, Mcoord2pix_re = util.make_grid_with_coordtransform( numPix=50, deltapix=0.1, subgrid_res=1) ra_at_xy_0_resized, dec_at_xy_0_resized, x_at_radec_0_resized, y_at_radec_0_resized, Mpix2coord_resized, Mcoord2pix_resized = image_util.rebin_coord_transform( 2, x_at_radec_0, y_at_radec_0, Mpix2coord, Mcoord2pix) assert ra_at_xy_0_resized == ra_at_xy_0_re assert dec_at_xy_0_resized == dec_at_xy_0_re assert x_at_radec_0_resized == x_at_radec_0_re assert y_at_radec_0_resized == y_at_radec_0_re npt.assert_almost_equal(Mcoord2pix_resized[0][0], Mcoord2pix_re[0][0], decimal=8) npt.assert_almost_equal(Mpix2coord_re[0][0], Mpix2coord_resized[0][0], decimal=8) x_grid, y_grid, ra_at_xy_0, dec_at_xy_0, x_at_radec_0, y_at_radec_0, Mpix2coord, Mcoord2pix = util.make_grid_with_coordtransform( numPix=99, deltapix=0.1, subgrid_res=1) x_grid, y_grid, ra_at_xy_0_re, dec_at_xy_0_re, x_at_radec_0_re, y_at_radec_0_re, Mpix2coord_re, Mcoord2pix_re = util.make_grid_with_coordtransform( numPix=33, deltapix=0.3, subgrid_res=1) assert x_at_radec_0 == 49 ra_at_xy_0_resized, dec_at_xy_0_resized, x_at_radec_0_resized, y_at_radec_0_resized, Mpix2coord_resized, Mcoord2pix_resized = image_util.rebin_coord_transform( 3, x_at_radec_0, y_at_radec_0, Mpix2coord, Mcoord2pix) assert x_at_radec_0_resized == 16 npt.assert_almost_equal(ra_at_xy_0_resized, ra_at_xy_0_re, decimal=8) npt.assert_almost_equal(dec_at_xy_0_resized, dec_at_xy_0_re, decimal=8) npt.assert_almost_equal(x_at_radec_0_resized, x_at_radec_0_re, decimal=8) npt.assert_almost_equal(y_at_radec_0_resized, y_at_radec_0_re, decimal=8) npt.assert_almost_equal(Mcoord2pix_resized[0][0], Mcoord2pix_re[0][0], decimal=8) npt.assert_almost_equal(Mpix2coord_re[0][0], Mpix2coord_resized[0][0], decimal=8) x_in, y_in = 10., 10. ra, dec = util.map_coord2pix(x_in, y_in, ra_at_xy_0, dec_at_xy_0, Mpix2coord) x_out, y_out = util.map_coord2pix(ra, dec, x_at_radec_0, y_at_radec_0, Mcoord2pix) assert x_in == x_out assert y_in == y_out x_in, y_in = 10., 10. ra, dec = util.map_coord2pix(x_in, y_in, ra_at_xy_0_resized, dec_at_xy_0_resized, Mpix2coord_resized) x_out, y_out = util.map_coord2pix(ra, dec, x_at_radec_0_resized, y_at_radec_0_resized, Mcoord2pix_resized) assert x_in == x_out assert y_in == y_out
def test_shift_coordinate_system(self): x_shift = 0.05 y_shift = 0 numPix = 10 deltaPix = 0.05 x_grid, y_grid, ra_at_xy_0, dec_at_xy_0, x_at_radec_0, y_at_radec_0, Mpix2coord, Mcoord2pix = util.make_grid_with_coordtransform( numPix=numPix, deltapix=deltaPix, subgrid_res=1, inverse=True) kwargs_data = {'ra_at_xy_0': ra_at_xy_0, 'dec_at_xy_0': dec_at_xy_0, 'transform_pix2angle': Mpix2coord, 'image_data': np.ones((numPix, numPix))} data = ImageData(**kwargs_data) data_new = copy.deepcopy(data) data_new.shift_coordinate_system(x_shift, y_shift, pixel_unit=False) ra, dec = 0, 0 x, y = data.map_coord2pix(ra, dec) x_new, y_new = data_new.map_coord2pix(ra + x_shift, dec + y_shift) npt.assert_almost_equal(x, x_new, decimal=10) npt.assert_almost_equal(y, y_new, decimal=10) ra, dec = data.map_pix2coord(x, y) ra_new, dec_new = data_new.map_pix2coord(x, y) npt.assert_almost_equal(ra, ra_new-x_shift, decimal=10) npt.assert_almost_equal(dec, dec_new-y_shift, decimal=10) x_coords, y_coords = data.pixel_coordinates x_coords_new, y_coords_new = data_new.pixel_coordinates npt.assert_almost_equal(x_coords[0], x_coords_new[0]-x_shift, decimal=10) npt.assert_almost_equal(y_coords[0], y_coords_new[0]-y_shift, decimal=10)
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
mask = circular_mask(60, [30, 30], 20) numPix = 50 # cutout pixel size deltaPix = 0.17 exp_timeI = 130 # exposure time (arbitrary units, flux per pixel is in units #photons/exp_time unit) fwhmI = 0.64 exp_timeR = 70 # exposure time (arbitrary units, flux per pixel is in units #photons/exp_time unit) fwhmR = 0.62 exp_timeG = 70 # exposure time (arbitrary units, flux per pixel is in units #photons/exp_time unit) fwhmG = 0.74 num_images = 4 _, _, ra_at_xy_0, dec_at_xy_0, _, _, Mpix2coord, _ \ \ = util.make_grid_with_coordtransform(numPix=numPix,deltapix=deltaPix,subgrid_res=1,inverse=False) # !! it = 0 for name in listname: start = timer() imagebrutR = pyfits.open(pathR + name)[1].data image_dataR = imagebrutR[0:60, 0:60] background_rmsR, mean, med = noisemap.estim_sigma_bkg_margin( image_dataR) # background noise per pixel nidR = int(name[-36:-34]) if nidR >= 0: nidI = nidR + 1 print 'NR', nidR
def setup(self): # we define a model consisting of a singe Sersric profile from lenstronomy.LightModel.light_model import LightModel light_model_list = ['SERSIC_ELLIPSE'] self.lightModel = LightModel(light_model_list=light_model_list) self.kwargs_light = [ {'amp': 100, 'R_sersic': 0.5, 'n_sersic': 3, 'e1': 0, 'e2': 0, 'center_x': 0.02, 'center_y': 0}] # we define a pixel grid and a higher resolution super sampling factor self._supersampling_factor = 5 numPix = 61 # cutout pixel size deltaPix = 0.05 # pixel size in arcsec (area per pixel = deltaPix**2) x, y, ra_at_xy_0, dec_at_xy_0, x_at_radec_0, y_at_radec_0, Mpix2coord, Mcoord2pix = util.make_grid_with_coordtransform( numPix=numPix, deltapix=deltaPix, subgrid_res=1, left_lower=False, inverse=False) flux = self.lightModel.surface_brightness(x, y, kwargs_list=self.kwargs_light) flux = util.array2image(flux) flux_max = np.max(flux) conv_pixels_partial = np.zeros((numPix, numPix), dtype=bool) conv_pixels_partial[flux >= flux_max / 20] = True self._conv_pixels_partial = conv_pixels_partial # high resolution ray-tracing and high resolution convolution, the full calculation self.kwargs_numerics_true = {'supersampling_factor': self._supersampling_factor, # super sampling factor of (partial) high resolution ray-tracing 'compute_mode': 'regular', # 'regular' or 'adaptive' 'supersampling_convolution': True, # bool, if True, performs the supersampled convolution (either on regular or adaptive grid) 'supersampling_kernel_size': None, # size of the higher resolution kernel region (can be smaller than the original kernel). None leads to use the full size 'flux_evaluate_indexes': None, # bool mask, if None, it will evaluate all (sub) pixels 'supersampled_indexes': None, # bool mask of pixels to be computed in supersampled grid (only for adaptive mode) 'compute_indexes': None, # bool mask of pixels to be computed the PSF response (flux being added to). Only used for adaptive mode and can be set =likelihood mask. 'point_source_supersampling_factor': 1, # int, supersampling factor when rendering a point source (not used in this script) } # high resolution convolution on a smaller PSF with low resolution convolution on the edges of the PSF and high resolution ray tracing self.kwargs_numerics_high_res_narrow = {'supersampling_factor': self._supersampling_factor, 'compute_mode': 'regular', 'supersampling_convolution': True, 'supersampling_kernel_size': 5, } # low resolution convolution based on high resolution ray-tracing grid self.kwargs_numerics_low_conv_high_grid = {'supersampling_factor': self._supersampling_factor, 'compute_mode': 'regular', 'supersampling_convolution': False, # does not matter for supersampling_factor=1 'supersampling_kernel_size': None, # does not matter for supersampling_factor=1 } # low resolution convolution with a subset of pixels with high resolution ray-tracing self.kwargs_numerics_low_conv_high_adaptive = {'supersampling_factor': self._supersampling_factor, 'compute_mode': 'adaptive', 'supersampling_convolution': False, # does not matter for supersampling_factor=1 'supersampling_kernel_size': None, # does not matter for supersampling_factor=1 'supersampled_indexes': self._conv_pixels_partial, 'convolution_kernel_size': 9, } # low resolution convolution with a subset of pixels with high resolution ray-tracing and high resoluton convolution on smaller kernel size self.kwargs_numerics_high_adaptive = {'supersampling_factor': self._supersampling_factor, 'compute_mode': 'adaptive', 'supersampling_convolution': True, # does not matter for supersampling_factor=1 'supersampling_kernel_size': 5, # does not matter for supersampling_factor=1 'supersampled_indexes': self._conv_pixels_partial, 'convolution_kernel_size': 9, } # low resolution convolution and low resolution ray tracing, the simplest calculation self.kwargs_numerics_low_res = {'supersampling_factor': 1, 'compute_mode': 'regular', 'supersampling_convolution': False, # does not matter for supersampling_factor=1 'supersampling_kernel_size': None, # does not matter for supersampling_factor=1 'convolution_kernel_size': 9, } flux_evaluate_indexes = np.zeros((numPix, numPix), dtype=bool) flux_evaluate_indexes[flux >= flux_max / 1000] = True # low resolution convolution on subframe self.kwargs_numerics_partial = {'supersampling_factor': 1, 'compute_mode': 'regular', 'supersampling_convolution': False, # does not matter for supersampling_factor=1 'supersampling_kernel_size': None, # does not matter for supersampling_factor=1 'flux_evaluate_indexes': flux_evaluate_indexes, 'convolution_kernel_size': 9 } # import PSF file kernel_super = kernel_util.kernel_gaussian(kernel_numPix=11 * self._supersampling_factor, deltaPix=deltaPix / self._supersampling_factor, fwhm=0.1) kernel_size = 9 kernel_super = kernel_util.cut_psf(psf_data=kernel_super, psf_size=kernel_size * self._supersampling_factor) # make instance of the PixelGrid class from lenstronomy.Data.pixel_grid import PixelGrid kwargs_grid = {'nx': numPix, 'ny': numPix, 'transform_pix2angle': Mpix2coord, 'ra_at_xy_0': ra_at_xy_0, 'dec_at_xy_0': dec_at_xy_0} self.pixel_grid = PixelGrid(**kwargs_grid) # make instance of the PSF class from lenstronomy.Data.psf import PSF kwargs_psf = {'psf_type': 'PIXEL', 'kernel_point_source': kernel_super, 'point_source_supersampling_factor': self._supersampling_factor} self.psf_class = PSF(**kwargs_psf) # without convolution image_model_true = ImageModel(self.pixel_grid, self.psf_class, lens_light_model_class=self.lightModel, kwargs_numerics=self.kwargs_numerics_true) self.image_true = image_model_true.image(kwargs_lens_light=self.kwargs_light)