def create_im_sim(multi_band_list, multi_band_type, kwargs_model, bands_compute=None, image_likelihood_mask_list=None, band_index=0, kwargs_pixelbased=None): """ :param multi_band_list: list of [[kwargs_data, kwargs_psf, kwargs_numerics], [], ..] :param multi_band_type: string, option when having multiple imaging data sets modelled simultaneously. Options are: - 'multi-linear': linear amplitudes are inferred on single data set - 'linear-joint': linear amplitudes ae jointly inferred - 'single-band': single band :param kwargs_model: model keyword arguments :param bands_compute: (optional), bool list to indicate which band to be included in the modeling :param image_likelihood_mask_list: list of image likelihood mask (same size as image_data with 1 indicating being evaluated and 0 being left out) :param band_index: integer, index of the imaging band to model (only applied when using 'single-band' as option) :param kwargs_pixelbased: keyword arguments with various settings related to the pixel-based solver (see SLITronomy documentation) :return: MultiBand class instance """ if multi_band_type == 'multi-linear': from lenstronomy.ImSim.MultiBand.multi_linear import MultiLinear multiband = MultiLinear(multi_band_list, kwargs_model, compute_bool=bands_compute, likelihood_mask_list=image_likelihood_mask_list) elif multi_band_type == 'joint-linear': from lenstronomy.ImSim.MultiBand.joint_linear import JointLinear multiband = JointLinear(multi_band_list, kwargs_model, compute_bool=bands_compute, likelihood_mask_list=image_likelihood_mask_list) elif multi_band_type == 'single-band': from lenstronomy.ImSim.MultiBand.single_band_multi_model import SingleBandMultiModel multiband = SingleBandMultiModel(multi_band_list, kwargs_model, likelihood_mask_list=image_likelihood_mask_list, band_index=band_index, kwargs_pixelbased=kwargs_pixelbased) else: raise ValueError("type %s is not supported!" % multi_band_type) return multiband
def psf_iteration(self, compute_bands=None, **kwargs_psf_iter): """ iterative PSF reconstruction :param compute_bands: bool list, if multiple bands, this process can be limited to a subset of bands :param kwargs_psf_iter: keyword arguments as used or available in PSFIteration.update_iterative() definition :return: 0, updated PSF is stored in self.multi_band_list """ kwargs_model = self._updateManager.kwargs_model kwargs_likelihood = self._updateManager.kwargs_likelihood likelihood_mask_list = kwargs_likelihood.get('image_likelihood_mask_list', None) kwargs_pixelbased = kwargs_likelihood.get('kwargs_pixelbased', None) kwargs_temp = self.best_fit(bijective=False) if compute_bands is None: compute_bands = [True] * len(self.multi_band_list) for band_index in range(len(self.multi_band_list)): if compute_bands[band_index] is True: kwargs_psf = self.multi_band_list[band_index][1] image_model = SingleBandMultiModel(self.multi_band_list, kwargs_model, likelihood_mask_list=likelihood_mask_list, band_index=band_index, kwargs_pixelbased=kwargs_pixelbased) psf_iter = PsfFitting(image_model_class=image_model) kwargs_psf = psf_iter.update_iterative(kwargs_psf, kwargs_params=kwargs_temp, **kwargs_psf_iter) self.multi_band_list[band_index][1] = kwargs_psf return 0
def __init__(self, multi_band_list, kwargs_model, likelihood_mask_list=None, compute_bool=None): self.type = 'multi-linear' imageModel_list = [] for band_index in range(len(multi_band_list)): imageModel = SingleBandMultiModel(multi_band_list, kwargs_model, likelihood_mask_list=likelihood_mask_list, band_index=band_index) imageModel_list.append(imageModel) super(MultiLinear, self).__init__(imageModel_list, compute_bool=compute_bool)
def __init__(self, multi_band_list, kwargs_model, likelihood_mask_list=None, compute_bool=None, kwargs_pixelbased=None, linear_solver=True): """ :param multi_band_list: list of imaging band configurations [[kwargs_data, kwargs_psf, kwargs_numerics],[...], ...] :param kwargs_model: model option keyword arguments :param likelihood_mask_list: list of likelihood masks (booleans with size of the individual images) :param compute_bool: (optional), bool list to indicate which band to be included in the modeling :param linear_solver: bool, if True (default) fixes the linear amplitude parameters 'amp' (avoid sampling) such that they get overwritten by the linear solver solution. """ self.type = 'multi-linear' imageModel_list = [] if linear_solver is False and len(multi_band_list) > 1: raise ValueError( 'Multi-linear mode with more than one band does not support "linear_solver" = False.' ) for band_index in range(len(multi_band_list)): imageModel = SingleBandMultiModel( multi_band_list, kwargs_model, likelihood_mask_list=likelihood_mask_list, band_index=band_index, kwargs_pixelbased=kwargs_pixelbased, linear_solver=linear_solver) imageModel_list.append(imageModel) super(MultiLinear, self).__init__(imageModel_list, compute_bool=compute_bool)
def create_im_sim(multi_band_list, multi_band_type, kwargs_model, bands_compute=None, likelihood_mask_list=None, band_index=0): """ :param multi_band_type: string, option when having multiple imaging data sets modelled simultaneously. Options are: - 'multi-linear': linear amplitudes are inferred on single data set - 'linear-joint': linear amplitudes ae jointly inferred - 'single-band': single band :return: MultiBand class instance """ if multi_band_type == 'multi-linear': from lenstronomy.ImSim.MultiBand.multi_linear import MultiLinear multiband = MultiLinear(multi_band_list, kwargs_model, compute_bool=bands_compute, likelihood_mask_list=likelihood_mask_list) elif multi_band_type == 'joint-linear': from lenstronomy.ImSim.MultiBand.joint_linear import JointLinear multiband = JointLinear(multi_band_list, kwargs_model, compute_bool=bands_compute, likelihood_mask_list=likelihood_mask_list) elif multi_band_type == 'single-band': from lenstronomy.ImSim.MultiBand.single_band_multi_model import SingleBandMultiModel multiband = SingleBandMultiModel(multi_band_list, kwargs_model, likelihood_mask_list=likelihood_mask_list, band_index=band_index) else: raise ValueError("type %s is not supported!" % multi_band_type) return multiband
def psf_iteration(self, num_iter=10, no_break=True, stacking_method='median', block_center_neighbour=0, keep_psf_error_map=True, psf_symmetry=1, psf_iter_factor=1, error_map_radius=None, verbose=True, compute_bands=None): """ iterative PSF reconstruction :param num_iter: number of iterations in the process :param no_break: bool, if False will break the process as soon as one step lead to a wors reconstruction then the previous step :param stacking_method: string, 'median' and 'mean' supported :param block_center_neighbour: radius of neighbouring point source to be blocked in the reconstruction :param keep_psf_error_map: bool, whether or not to keep the previous psf_error_map :param psf_symmetry: int, number of invariant rotations in the reconstructed PSF :param psf_iter_factor: factor of new estimated PSF relative to the old one PSF_updated = (1-psf_iter_factor) * PSF_old + psf_iter_factor*PSF_new :param error_map_radius: float, radius (in arc seconds) of the outermost error in the PSF estimate (e.g. to avoid double counting of overlapping PSF errors) :param verbose: bool, print statements :param compute_bands: bool list, if multiple bands, this process can be limited to a subset of bands :return: 0, updated PSF is stored in self.multi_band_list """ kwargs_model = self._updateManager.kwargs_model kwargs_likelihood = self._updateManager.kwargs_likelihood likelihood_mask_list = kwargs_likelihood.get( 'image_likelihood_mask_list', None) kwargs_pixelbased = kwargs_likelihood.get('kwargs_pixelbased', None) kwargs_temp = self.best_fit(bijective=False) if compute_bands is None: compute_bands = [True] * len(self.multi_band_list) for band_index in range(len(self.multi_band_list)): if compute_bands[band_index] is True: kwargs_psf = self.multi_band_list[band_index][1] image_model = SingleBandMultiModel( self.multi_band_list, kwargs_model, likelihood_mask_list=likelihood_mask_list, band_index=band_index, kwargs_pixelbased=kwargs_pixelbased) psf_iter = PsfFitting(image_model_class=image_model) kwargs_psf = psf_iter.update_iterative( kwargs_psf, kwargs_params=kwargs_temp, num_iter=num_iter, no_break=no_break, stacking_method=stacking_method, block_center_neighbour=block_center_neighbour, keep_psf_error_map=keep_psf_error_map, psf_symmetry=psf_symmetry, psf_iter_factor=psf_iter_factor, error_map_radius=error_map_radius, verbose=verbose) self.multi_band_list[band_index][1] = kwargs_psf return 0
def __init__(self, multi_band_list, kwargs_model, likelihood_mask_list=None, compute_bool=None, kwargs_pixelbased=None): """ :param multi_band_list: list of imaging band configurations [[kwargs_data, kwargs_psf, kwargs_numerics],[...], ...] :param kwargs_model: model option keyword arguments :param likelihood_mask_list: list of likelihood masks (booleans with size of the individual images :param compute_bool: (optinal), bool list to indicate which band to be included in the modeling """ self.type = 'multi-linear' imageModel_list = [] for band_index in range(len(multi_band_list)): imageModel = SingleBandMultiModel( multi_band_list, kwargs_model, likelihood_mask_list=likelihood_mask_list, band_index=band_index, kwargs_pixelbased=kwargs_pixelbased) imageModel_list.append(imageModel) super(MultiLinear, self).__init__(imageModel_list, compute_bool=compute_bool)
def __init__(self, multi_band_list, kwargs_model, model, error_map, cov_param, param, kwargs_params, likelihood_mask_list=None, band_index=0, arrow_size=0.02, cmap_string="gist_heat"): self.bandmodel = SingleBandMultiModel( multi_band_list, kwargs_model, likelihood_mask_list=likelihood_mask_list, band_index=band_index) self._kwargs_special_partial = kwargs_params.get( 'kwargs_special', None) kwarks_lens_partial, kwargs_source_partial, kwargs_lens_light_partial, kwargs_ps_partial, self._kwargs_extinction_partial = self.bandmodel.select_kwargs( **kwargs_params) self._kwargs_lens_partial, self._kwargs_source_partial, self._kwargs_lens_light_partial, self._kwargs_ps_partial = self.bandmodel.update_linear_kwargs( param, kwarks_lens_partial, kwargs_source_partial, kwargs_lens_light_partial, kwargs_ps_partial) self._norm_residuals = self.bandmodel.reduced_residuals( model, error_map=error_map) self._reduced_x2 = self.bandmodel.reduced_chi2(model, error_map=error_map) print("reduced chi^2 of data ", band_index, "= ", self._reduced_x2) self._model = model self._cov_param = cov_param self._param = param self._lensModel = self.bandmodel.LensModel self._lensModelExt = LensModelExtensions(self._lensModel) log_model = np.log10(model) log_model[np.isnan(log_model)] = -5 self._v_min_default = max(np.min(log_model), -5) self._v_max_default = min(np.max(log_model), 10) self._coords = self.bandmodel.Data self._data = self._coords.data self._deltaPix = self._coords.pixel_width self._frame_size = np.max(self._coords.width) x_grid, y_grid = self._coords.pixel_coordinates self._x_grid = util.image2array(x_grid) self._y_grid = util.image2array(y_grid) if isinstance(cmap_string, str): cmap = plt.get_cmap(cmap_string) else: cmap = cmap_string cmap.set_bad(color='k', alpha=1.) cmap.set_under('k') self._cmap = cmap self._arrow_size = arrow_size
def _likelihood(self, args): """ routine to compute X2 given variable parameters for a MCMC/PSO chainF """ #generate image and computes likelihood multi_band_list = self.update_multi_band(args) image_model = SingleBandMultiModel(multi_band_list, self._kwargs_model, likelihood_mask_list=self._likelihood_mask_list, band_index=self._band_index) logL = image_model.likelihood_data_given_model(source_marg=self._source_marg, **self._kwargs_params) return logL
def __init__(self, multi_band_list, kwargs_model, model, error_map, cov_param, param, kwargs_params, image_likelihood_mask_list=None, band_index=0, verbose=True): """ :param multi_band_list: list of imaging data configuration [[kwargs_data, kwargs_psf, kwargs_numerics], [...]] :param kwargs_model: model keyword argument list for the full multi-band modeling :param model: 2d numpy array of modeled image for the specified band :param error_map: 2d numpy array of size of the image, additional error in the pixels coming from PSF uncertainties :param cov_param: covariance matrix of the linear inversion :param param: 1d numpy array of the linear coefficients of this imaging band :param kwargs_params: keyword argument of keyword argument lists of the different model components selected for the imaging band, NOT including linear amplitudes (not required as being overwritten by the param list) :param image_likelihood_mask_list: list of 2d numpy arrays of likelihood masks (for all bands) :param band_index: integer of the band to be considered in this class :param verbose: if True (default), prints the reduced chi2 value for the current band. """ self._bandmodel = SingleBandMultiModel( multi_band_list, kwargs_model, likelihood_mask_list=image_likelihood_mask_list, band_index=band_index) self._kwargs_special_partial = kwargs_params.get( 'kwargs_special', None) kwarks_lens_partial, kwargs_source_partial, kwargs_lens_light_partial, kwargs_ps_partial, self._kwargs_extinction_partial = self._bandmodel.select_kwargs( **kwargs_params) self._kwargs_lens_partial, self._kwargs_source_partial, self._kwargs_lens_light_partial, self._kwargs_ps_partial = self._bandmodel.update_linear_kwargs( param, kwarks_lens_partial, kwargs_source_partial, kwargs_lens_light_partial, kwargs_ps_partial) # this is an (out-commented) example of how to re-create the model in this band # model_new = self.bandmodel.image(self._kwargs_lens_partial, self._kwargs_source_partial, self._kwargs_lens_light_partial, self._kwargs_ps_partial, self._kwargs_special_partial, self._kwargs_extinction_partial) self._norm_residuals = self._bandmodel.reduced_residuals( model, error_map=error_map) self._reduced_x2 = self._bandmodel.reduced_chi2(model, error_map=error_map) if verbose: print("reduced chi^2 of data ", band_index, "= ", self._reduced_x2) self._model = model self._cov_param = cov_param self._param = param self._error_map = error_map
def psf_iteration(self, num_iter=10, no_break=True, stacking_method='median', block_center_neighbour=0, keep_psf_error_map=True, psf_symmetry=1, psf_iter_factor=1, verbose=True, compute_bands=None): """ iterative PSF reconstruction :param num_iter: number of iterations in the process :param no_break: bool, if False will break the process as soon as one step lead to a wors reconstruction then the previous step :param stacking_method: string, 'median' and 'mean' supported :param block_center_neighbour: radius of neighbouring point source to be blocked in the reconstruction :param keep_psf_error_map: bool, whether or not to keep the previous psf_error_map :param psf_symmetry: int, number of invariant rotations in the reconstructed PSF :param psf_iter_factor: factor of new estimated PSF relative to the old one PSF_updated = (1-psf_iter_factor) * PSF_old + psf_iter_factor*PSF_new :param verbose: bool, print statements :param compute_bands: bool list, if multiple bands, this process can be limited to a subset of bands :return: 0, updated PSF is stored in self.mult_iband_list """ kwargs_model = self._updateManager.kwargs_model kwargs_likelihood = self._updateManager.kwargs_likelihood likelihood_mask_list = kwargs_likelihood.get('image_likelihood_mask_list', None) param_class = self._param_class lens_updated = param_class.update_lens_scaling(self._cosmo_temp, self._lens_temp) source_updated = param_class.image2source_plane(self._source_temp, lens_updated) if compute_bands is None: compute_bands = [True] * len(self.multi_band_list) for band_index in range(len(self.multi_band_list)): if compute_bands[band_index] is True: kwargs_psf = self.multi_band_list[band_index][1] image_model = SingleBandMultiModel(self.multi_band_list, kwargs_model, likelihood_mask_list=likelihood_mask_list, band_index=band_index) psf_iter = PsfFitting(image_model_class=image_model) kwargs_psf = psf_iter.update_iterative(kwargs_psf, lens_updated, source_updated, self._lens_light_temp, self._ps_temp, num_iter=num_iter, no_break=no_break, stacking_method=stacking_method, block_center_neighbour=block_center_neighbour, keep_psf_error_map=keep_psf_error_map, psf_symmetry=psf_symmetry, psf_iter_factor=psf_iter_factor, verbose=verbose) self.multi_band_list[band_index][1] = kwargs_psf return 0
def create_im_sim(multi_band_list, multi_band_type, kwargs_model, bands_compute=None, likelihood_mask_list=None, band_index=0): """ :param multi_band_type: string, option when having multiple imaging data sets modelled simultaneously. Options are: - 'multi-band': linear amplitudes are inferred on single data set - 'multi-exposure': linear amplitudes ae jointly inferred - 'multi-frame': multiple frames (as single exposures with disjoint lens model :return: MultiBand class instance """ if multi_band_type == 'multi-linear': multiband = MultiLinear(multi_band_list, kwargs_model, compute_bool=bands_compute, likelihood_mask_list=likelihood_mask_list) elif multi_band_type == 'joint-linear': multiband = JointLinear(multi_band_list, kwargs_model, compute_bool=bands_compute, likelihood_mask_list=likelihood_mask_list) elif multi_band_type == 'single-band': multiband = SingleBandMultiModel(multi_band_list, kwargs_model, likelihood_mask_list=likelihood_mask_list, band_index=band_index) else: raise ValueError("type %s is not supported!" % multi_band_type) return multiband
def setup(self): # data specifics sigma_bkg = 0.05 # background noise per pixel exp_time = 100 # exposure time (arbitrary units, flux per pixel is in units #photons/exp_time unit) numPix = 10 # cutout pixel size deltaPix = 0.1 # pixel size in arcsec (area per pixel = deltaPix**2) fwhm = 0.5 # full width half max of PSF # PSF specification kwargs_data = simulation_util.data_configure_simple( numPix, deltaPix, exp_time, sigma_bkg) data_class = ImageData(**kwargs_data) kwargs_psf = { 'psf_type': 'GAUSSIAN', 'fwhm': fwhm, 'pixel_size': deltaPix } psf_class = PSF(**kwargs_psf) kwargs_spemd = { 'theta_E': 1., 'gamma': 1.8, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1 } lens_model_list = ['SPEP'] kwargs_lens = [kwargs_spemd] lens_model_class = LensModel(lens_model_list=lens_model_list) kwargs_sersic = { 'amp': 1., 'R_sersic': 0.1, 'n_sersic': 2, 'center_x': 0, 'center_y': 0 } # 'SERSIC_ELLIPSE': elliptical Sersic profile kwargs_sersic_ellipse = { 'amp': 1., 'R_sersic': .6, 'n_sersic': 3, 'center_x': 0, 'center_y': 0, 'e1': 0.1, 'e2': 0.1 } lens_light_model_list = ['SERSIC'] kwargs_lens_light = [kwargs_sersic] lens_light_model_class = LightModel( light_model_list=lens_light_model_list) source_model_list = ['SERSIC_ELLIPSE'] kwargs_source = [kwargs_sersic_ellipse] source_model_class = LightModel(light_model_list=source_model_list) # Point Source point_source_model_list = ['UNLENSED'] kwargs_ps = [{ 'ra_image': [0.4], 'dec_image': [-0.2], 'point_amp': [2] }] point_source_class = PointSource( point_source_type_list=point_source_model_list) kwargs_numerics = { 'supersampling_factor': 1, 'supersampling_convolution': False, 'compute_mode': 'regular' } imageModel = ImageModel(data_class, psf_class, lens_model_class, source_model_class, lens_light_model_class, point_source_class=point_source_class, kwargs_numerics=kwargs_numerics) image_sim = simulation_util.simulate_simple(imageModel, kwargs_lens, kwargs_source, kwargs_lens_light, kwargs_ps) data_class.update_data(image_sim) kwargs_data['image_data'] = image_sim self.multi_band_list = [[kwargs_data, kwargs_psf, kwargs_numerics]] self.kwargs_model = { 'lens_model_list': lens_model_list, 'source_light_model_list': source_model_list, 'lens_light_model_list': lens_light_model_list, 'point_source_model_list': point_source_model_list, 'fixed_magnification_list': [False], 'index_lens_model_list': [[0]], 'index_lens_light_model_list': [[0]], 'index_source_light_model_list': [[0]], 'index_point_source_model_list': [[0]], } self.kwargs_params = { 'kwargs_lens': kwargs_lens, 'kwargs_source': kwargs_source, 'kwargs_lens_light': kwargs_lens_light, 'kwargs_ps': kwargs_ps } self.single_band = SingleBandMultiModel( multi_band_list=self.multi_band_list, kwargs_model=self.kwargs_model, linear_solver=True) self.single_band_no_linear = SingleBandMultiModel( multi_band_list=self.multi_band_list, kwargs_model=self.kwargs_model, linear_solver=False)