Esempio n. 1
0
    def setup(self):
        lens_model_list = ['SPEP', 'SHEAR']
        lensModel = LensModel(lens_model_list=lens_model_list)
        lensModelExtensions = LensModelExtensions(lensModel=lensModel)
        lensEquationSolver = LensEquationSolver(lensModel=lensModel)

        x_source, y_source = 0.02, 0.01
        kwargs_lens = [{'theta_E': 1., 'e1': 0.1, 'e2': 0.1, 'gamma': 2., 'center_x': 0, 'center_y': 0},
                       {'gamma1': 0.06, 'gamma2': -0.03}]

        x_img, y_img = lensEquationSolver.image_position_from_source(kwargs_lens=kwargs_lens, sourcePos_x=x_source,
                                                                     sourcePos_y=y_source)
        print('image positions are: ', x_img, y_img)
        mag_inf = lensModel.magnification(x_img, y_img, kwargs_lens)
        print('point source magnification: ', mag_inf)

        source_size_arcsec = 0.001
        window_size = 0.1
        grid_number = 100
        print('source size in arcsec: ', source_size_arcsec)
        mag_finite = lensModelExtensions.magnification_finite(x_pos=x_img, y_pos=y_img, kwargs_lens=kwargs_lens,
                                                              source_sigma=source_size_arcsec, window_size=window_size,
                                                              grid_number=grid_number)
        flux_ratios = mag_finite[1:] / mag_finite[0]
        flux_ratio_errors = [0.1, 0.1, 0.1]
        self.flux_likelihood = FluxRatioLikelihood(lens_model_class=lensModel, flux_ratios=flux_ratios, flux_ratio_errors=flux_ratio_errors,
                 source_type='GAUSSIAN', window_size=window_size, grid_number=grid_number)

        self.flux_likelihood_inf = FluxRatioLikelihood(lens_model_class=lensModel, flux_ratios=flux_ratios,
                                                   flux_ratio_errors=flux_ratio_errors,
                                                   source_type='INF', window_size=window_size,
                                                   grid_number=grid_number)
        self.kwargs_cosmo = {'source_size': source_size_arcsec}
        self.x_img, self.y_img = x_img, y_img
        self.kwargs_lens = kwargs_lens
Esempio n. 2
0
    def test__logL(self):
        lensModel = LensModel(lens_model_list=[])
        flux_ratios_init = np.array([1., 1., 1.])
        flux_ratio_errors = np.array([1., 1., 1.])
        flux_likelihood = FluxRatioLikelihood(lens_model_class=lensModel, flux_ratios=flux_ratios_init,
                            flux_ratio_errors=flux_ratio_errors)

        flux_ratios = np.array([0, 1, np.nan])
        logL = flux_likelihood._logL(flux_ratios)
        assert logL == -10 ** 15

        flux_likelihood = FluxRatioLikelihood(lens_model_class=lensModel, flux_ratios=flux_ratios_init,
                                              flux_ratio_errors=np.array([0., 1., 1.]))
        flux_ratios = np.array([1., 1., 1.])
        logL = flux_likelihood._logL(flux_ratios)
        assert logL == -10 ** 15
Esempio n. 3
0
    def _class_instances(self, kwargs_model, kwargs_imaging, kwargs_position,
                         kwargs_flux, kwargs_time_delay):
        """

        :param kwargs_model: lenstronomy model keyword arguments
        :param kwargs_imaging: keyword arguments for imaging likelihood
        :param kwargs_position: keyword arguments for positional likelihood
        :param kwargs_flux: keyword arguments for flux ratio likelihood
        :param kwargs_time_delay: keyword arguments for time delay likelihood
        :return: updated model instances of this class
        """

        lens_model_class, source_model_class, lens_light_model_class, point_source_class, extinction_class = class_creator.create_class_instances(
            **kwargs_model)
        self.PointSource = point_source_class

        if self._time_delay_likelihood is True:
            self.time_delay_likelihood = TimeDelayLikelihood(
                lens_model_class=lens_model_class,
                point_source_class=point_source_class,
                **kwargs_time_delay)

        if self._image_likelihood is True:
            self.image_likelihood = ImageLikelihood(kwargs_model=kwargs_model,
                                                    **kwargs_imaging)
        self._position_likelihood = PositionLikelihood(point_source_class,
                                                       **kwargs_position)
        if self._flux_ratio_likelihood is True:
            self.flux_ratio_likelihood = FluxRatioLikelihood(
                lens_model_class, **kwargs_flux)
Esempio n. 4
0
    def __init__(self,
                 kwargs_data_joint,
                 kwargs_model,
                 param_class,
                 image_likelihood=True,
                 check_bounds=True,
                 check_matched_source_position=False,
                 astrometric_likelihood=False,
                 image_position_likelihood=False,
                 source_position_likelihood=False,
                 image_position_uncertainty=0.004,
                 check_positive_flux=False,
                 source_position_tolerance=0.001,
                 source_position_sigma=0.001,
                 force_no_add_image=False,
                 source_marg=False,
                 linear_prior=None,
                 restrict_image_number=False,
                 max_num_images=None,
                 bands_compute=None,
                 time_delay_likelihood=False,
                 force_minimum_source_surface_brightness=False,
                 flux_min=0,
                 image_likelihood_mask_list=None,
                 flux_ratio_likelihood=False,
                 kwargs_flux_compute={},
                 prior_lens=[],
                 prior_source=[],
                 prior_extinction=[],
                 prior_lens_light=[],
                 prior_ps=[],
                 prior_special=[],
                 prior_lens_kde=[],
                 prior_source_kde=[],
                 prior_lens_light_kde=[],
                 prior_ps_kde=[],
                 prior_special_kde=[],
                 prior_extinction_kde=[],
                 prior_lens_lognormal=[],
                 prior_source_lognormal=[],
                 prior_extinction_lognormal=[],
                 prior_lens_light_lognormal=[],
                 prior_ps_lognormal=[],
                 prior_special_lognormal=[],
                 custom_logL_addition=None):
        """
        initializing class


        :param param_class: instance of a Param() class that can cast the sorted list of parameters that are sampled into the
        conventions of the imSim_class
        :param image_likelihood: bool, option to compute the imaging likelihood
        :param source_position_likelihood: bool, if True, ray-traces image positions back to source plane and evaluates
        relative errors in respect ot the position_uncertainties in the image plane
        :param check_bounds:  bool, option to punish the hard bounds in parameter space
        :param check_matched_source_position: bool, option to check whether point source position solver finds a solution to match all
         the image positions in the same source plane coordinate
        :param astrometric_likelihood: bool, additional likelihood term of the predicted vs modelled point source position
        :param flaot, image_position_uncertainty: 1-sigma Gaussian uncertainty on the point source position
        (only used if point_source_likelihood=True)
        :param check_positive_flux: bool, option to punish models that do not have all positive linear amplitude parameters
        :param source_position_tolerance: float, punishment of check_solver occurs when image positions are predicted further
        away than this number
        :param image_likelihood_mask_list: list of boolean 2d arrays of size of images marking the pixels to be evaluated in the likelihood
        :param force_no_add_image: bool, if True: computes ALL image positions of the point source. If there are more
        images predicted than modelled, a punishment occures
        :param source_marg: marginalization addition on the imaging likelihood based on the covariance of the infered
        linear coefficients
        :param linear_prior: float or list of floats (when multi-linear setting is chosen) indicating the range of
        linear amplitude priors when computing the marginalization term.
        :param restrict_image_number: bool, if True: computes ALL image positions of the point source. If there are more
        images predicted than indicated in max_num_images, a punishment occurs
        :param max_num_images: int, see restrict_image_number
        :param bands_compute: list of bools with same length as data objects, indicates which "band" to include in the fitting
        :param time_delay_likelihood: bool, if True computes the time-delay likelihood of the FIRST point source
        :param force_minimum_source_surface_brightness: bool, if True, evaluates the source surface brightness on a grid
        and evaluates if all positions have positive flux
        :param kwargs_flux_compute: keyword arguments of how to compute the image position fluxes (see FluxRatioLikeliood)
        :param custom_logL_addition: a definition taking as arguments (kwargs_lens, kwargs_source, kwargs_lens_light, kwargs_ps, kwargs_special, kwargs_extinction)
        and returns a logL (punishing) value.
        """
        multi_band_list, image_type, time_delays_measured, time_delays_uncertainties, flux_ratios, flux_ratio_errors, ra_image_list, dec_image_list = self._unpack_data(
            **kwargs_data_joint)
        if len(multi_band_list) == 0:
            image_likelihood = False

        self.param = param_class
        self._lower_limit, self._upper_limit = self.param.param_limits()
        lens_model_class, source_model_class, lens_light_model_class, point_source_class, extinction_class = class_creator.create_class_instances(
            **kwargs_model)
        self.PointSource = point_source_class

        self._prior_likelihood = PriorLikelihood(
            prior_lens,
            prior_source,
            prior_lens_light,
            prior_ps,
            prior_special,
            prior_extinction,
            prior_lens_kde,
            prior_source_kde,
            prior_lens_light_kde,
            prior_ps_kde,
            prior_special_kde,
            prior_extinction_kde,
            prior_lens_lognormal,
            prior_source_lognormal,
            prior_lens_light_lognormal,
            prior_ps_lognormal,
            prior_special_lognormal,
            prior_extinction_lognormal,
        )
        self._time_delay_likelihood = time_delay_likelihood
        if self._time_delay_likelihood is True:
            self.time_delay_likelihood = TimeDelayLikelihood(
                time_delays_measured, time_delays_uncertainties,
                lens_model_class, point_source_class)

        self._image_likelihood = image_likelihood
        if self._image_likelihood is True:
            self.image_likelihood = ImageLikelihood(
                multi_band_list,
                image_type,
                kwargs_model,
                bands_compute=bands_compute,
                likelihood_mask_list=image_likelihood_mask_list,
                source_marg=source_marg,
                linear_prior=linear_prior,
                force_minimum_source_surface_brightness=
                force_minimum_source_surface_brightness,
                flux_min=flux_min)
        self._position_likelihood = PositionLikelihood(
            point_source_class,
            astrometric_likelihood=astrometric_likelihood,
            image_position_likelihood=image_position_likelihood,
            source_position_likelihood=source_position_likelihood,
            ra_image_list=ra_image_list,
            dec_image_list=dec_image_list,
            image_position_uncertainty=image_position_uncertainty,
            check_matched_source_position=check_matched_source_position,
            source_position_tolerance=source_position_tolerance,
            source_position_sigma=source_position_sigma,
            force_no_add_image=force_no_add_image,
            restrict_image_number=restrict_image_number,
            max_num_images=max_num_images)
        self._flux_ratio_likelihood = flux_ratio_likelihood
        self._kwargs_flux_compute = kwargs_flux_compute
        if self._flux_ratio_likelihood is True:
            self.flux_ratio_likelihood = FluxRatioLikelihood(
                lens_model_class, flux_ratios, flux_ratio_errors,
                **self._kwargs_flux_compute)
        self._check_positive_flux = check_positive_flux
        self._check_bounds = check_bounds
        self._custom_logL_addition = custom_logL_addition
Esempio n. 5
0
    def __init__(self,
                 kwargs_data_joint,
                 kwargs_model,
                 param_class,
                 image_likelihood=True,
                 check_bounds=True,
                 check_solver=False,
                 point_source_likelihood=False,
                 position_uncertainty=0.004,
                 check_positive_flux=False,
                 solver_tolerance=0.001,
                 force_no_add_image=False,
                 source_marg=False,
                 restrict_image_number=False,
                 max_num_images=None,
                 bands_compute=None,
                 time_delay_likelihood=False,
                 force_minimum_source_surface_brightness=False,
                 flux_min=0,
                 image_likelihood_mask_list=None,
                 flux_ratio_likelihood=False,
                 kwargs_flux_compute={},
                 prior_lens=[],
                 prior_source=[],
                 prior_lens_light=[],
                 prior_ps=[],
                 prior_cosmo=[]):
        """
        initializing class


        :param param_class: instance of a Param() class that can cast the sorted list of parameters that are sampled into the
        conventions of the imSim_class
        :param image_likelihood: bool, option to compute the imaging likelihood
        :param check_bounds:  bool, option to punish the hard bounds in parameter space
        :param check_solver: bool, option to check whether point source position solver finds a solution to match all
         the image positions in the same source plane coordinate
        :param point_source_likelihood: bool, additional likelihood term of the predicted vs modelled point source position
        :param flaot, position_uncertainty: 1-sigma Gaussian uncertainty on the point source position
        (only used if point_source_likelihood=True)
        :param check_positive_flux: bool, option to punish models that do not have all positive linear amplitude parameters
        :param solver_tolerance: float, punishment of check_solver occures when image positions are predicted further
        away than this number
        :param image_likelihood_mask_list: list of boolean 2d arrays of size of images marking the pixels to be evaluated in the likelihood
        :param force_no_add_image: bool, if True: computes ALL image positions of the point source. If there are more
        images predicted than modelled, a punishment occures
        :param source_marg: marginalization addition on the imaging likelihood based on the covariance of the infered
        linear coefficients
        :param restrict_image_number: bool, if True: computes ALL image positions of the point source. If there are more
        images predicted than indicated in max_num_images, a punishment occures
        :param max_num_images: int, see restrict_image_number
        :param bands_compute: list of bools with same length as data objects, indicates which "band" to include in the fitting
        :param time_delay_likelihood: bool, if True computes the time-delay likelihood of the FIRST point source
        :param force_minimum_source_surface_brightness: bool, if True, evaluates the source surface brightness on a grid
        and evaluates if all positions have positive flux
        :param kwargs_flux_compute: keyword arguments of how to compute the image position fluxes (see FluxRatioLikeliood)
        """
        multi_band_list = kwargs_data_joint.get('multi_band_list', [])
        multi_band_type = kwargs_data_joint.get('image_type', 'single-band')
        time_delays_measured = kwargs_data_joint.get('time_delays_measured',
                                                     None)
        time_delays_uncertainties = kwargs_data_joint.get(
            'time_delays_uncertainties', None)

        flux_ratios = kwargs_data_joint.get('flux_ratios', None)
        flux_ratio_errors = kwargs_data_joint.get('flux_ratio_errors', None)

        self.param = param_class
        self._lower_limit, self._upper_limit = self.param.param_limits()
        lens_model_class, source_model_class, lens_light_model_class, point_source_class = class_reator.create_class_instances(
            **kwargs_model)
        self.PointSource = point_source_class

        self._prior_likelihood = PriorLikelihood(prior_lens, prior_source,
                                                 prior_lens_light, prior_ps,
                                                 prior_cosmo)
        self._time_delay_likelihood = time_delay_likelihood
        if self._time_delay_likelihood is True:
            self.time_delay_likelihood = TimeDelayLikelihood(
                time_delays_measured, time_delays_uncertainties,
                lens_model_class, point_source_class, param_class)

        self._image_likelihood = image_likelihood
        if self._image_likelihood is True:
            self.image_likelihood = ImageLikelihood(
                multi_band_list,
                multi_band_type,
                kwargs_model,
                bands_compute=bands_compute,
                likelihood_mask_list=image_likelihood_mask_list,
                source_marg=source_marg,
                force_minimum_source_surface_brightness=
                force_minimum_source_surface_brightness,
                flux_min=flux_min)
        self._position_likelihood = PositionLikelihood(
            point_source_class, param_class, point_source_likelihood,
            position_uncertainty, check_solver, solver_tolerance,
            force_no_add_image, restrict_image_number, max_num_images)
        self._flux_ratio_likelihood = flux_ratio_likelihood
        self._kwargs_flux_compute = kwargs_flux_compute
        if self._flux_ratio_likelihood is True:
            self.flux_ratio_likelihood = FluxRatioLikelihood(
                point_source_class, lens_model_class, param_class, flux_ratios,
                flux_ratio_errors, **self._kwargs_flux_compute)
        self._check_positive_flux = check_positive_flux
        self._check_bounds = check_bounds