Skip to content

Commit

Permalink
modified source position likelihood
Browse files Browse the repository at this point in the history
  • Loading branch information
sibirrer committed Aug 1, 2024
1 parent cbac00c commit 5614837
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 38 deletions.
34 changes: 13 additions & 21 deletions lenstronomy/Sampling/Likelihoods/position_likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ def __init__(
ra_image_list=None,
dec_image_list=None,
source_position_likelihood=False,
check_matched_source_position=False,
source_position_tolerance=0.001,
source_position_tolerance=None,
source_position_sigma=0.001,
force_no_add_image=False,
restrict_image_number=False,
Expand All @@ -36,11 +35,12 @@ def __init__(
:param dec_image_list: list or DEC image positions per model component
: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 (image_position_uncertainty)
:param check_matched_source_position: bool, if True, checks whether multiple images are a solution of the same
source
:param source_position_tolerance: tolerance level (in arc seconds in the source plane) of the different images
:param source_position_tolerance: tolerance level (in arc seconds in the source plane) of the different images.
If set =! None, then the backwards ray tracing is performed on the images and demand on the same position of
the source is meant to match the requirements, otherwise a punishing likelihood term is introduced
:type source_position_tolerance: None or float
:param source_position_sigma: r.m.s. value corresponding to a 1-sigma Gaussian likelihood accepted by the model
precision in matching the source position
precision in matching the source position transformed from the image plane
:param force_no_add_image: bool, if True, will punish additional images appearing in the frame of the modelled
image(first calculate them)
:param restrict_image_number: bool, if True, searches for all appearing images in the frame of the data and
Expand All @@ -54,8 +54,7 @@ def __init__(
self._astrometric_likelihood = astrometric_likelihood
self._image_position_sigma = image_position_uncertainty
self._source_position_sigma = source_position_sigma
self._check_matched_source_position = check_matched_source_position
self._bound_source_position_scatter = source_position_tolerance
self._bound_source_position_tolerance = source_position_tolerance
self._force_no_add_image = force_no_add_image
self._restrict_number_images = restrict_image_number
self._source_position_likelihood = source_position_likelihood
Expand Down Expand Up @@ -89,17 +88,6 @@ def logL(self, kwargs_lens, kwargs_ps, kwargs_special, verbose=False):
logL += logL_astrometry
if verbose is True:
print("Astrometric likelihood = %s" % logL_astrometry)
if self._check_matched_source_position is True:
logL_source_scatter = self.source_position_likelihood(
kwargs_lens,
kwargs_ps,
self._source_position_sigma,
hard_bound_rms=self._bound_source_position_scatter,
verbose=verbose,
)
logL += logL_source_scatter
if verbose is True:
print("Source scatter punishing likelihood = %s" % logL_source_scatter)
if self._force_no_add_image:
additional_image_bool = self.check_additional_images(kwargs_ps, kwargs_lens)
if additional_image_bool is True:
Expand All @@ -119,9 +107,13 @@ def logL(self, kwargs_lens, kwargs_ps, kwargs_special, verbose=False):
"Number of images found %s exceeded the limited number allowed %s"
% (len(ra_image_list[0]), self._max_num_images)
)
if self._source_position_likelihood is True:
if self._source_position_likelihood is True or self._bound_source_position_tolerance is not None:
logL_source_pos = self.source_position_likelihood(
kwargs_lens, kwargs_ps, sigma=self._image_position_sigma
kwargs_lens,
kwargs_ps,
self._source_position_sigma,
hard_bound_rms=self._bound_source_position_tolerance,
verbose=verbose,
)
logL += logL_source_pos
if verbose is True:
Expand Down
5 changes: 0 additions & 5 deletions lenstronomy/Sampling/likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def __init__(
param_class,
image_likelihood=True,
check_bounds=True,
check_matched_source_position=False,
astrometric_likelihood=False,
image_position_likelihood=False,
source_position_likelihood=False,
Expand Down Expand Up @@ -85,9 +84,6 @@ def __init__(
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 of 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 image_position_uncertainty: float, 1-sigma Gaussian uncertainty on the
Expand Down Expand Up @@ -197,7 +193,6 @@ def __init__(
"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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def setup_method(self):
ra_image_list=[x_pos],
dec_image_list=[y_pos],
source_position_likelihood=True,
check_matched_source_position=False,
source_position_tolerance=0.001,
force_no_add_image=False,
restrict_image_number=False,
Expand All @@ -51,7 +50,6 @@ def setup_method(self):
ra_image_list=[x_pos],
dec_image_list=[y_pos],
source_position_likelihood=True,
check_matched_source_position=True,
source_position_tolerance=0.001,
force_no_add_image=True,
restrict_image_number=True,
Expand Down
8 changes: 4 additions & 4 deletions test/test_Sampling/test_Samplers/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def simple_einstein_ring_likelihood():
kwargs_likelihood = {
"source_marg": False,
"image_position_uncertainty": 0.004,
"check_matched_source_position": False,
"source_position_tolerance": 0.001,
"source_position_likelihood": False,
"source_position_tolerance": None,
"source_position_sigma": 0.001,
}

Expand Down Expand Up @@ -229,8 +229,8 @@ def simple_einstein_ring_likelihood_2d():
kwargs_likelihood = {
"source_marg": False,
"image_position_uncertainty": 0.004,
"check_matched_source_position": False,
"source_position_tolerance": 0.001,
"source_position_likelihood": False,
"source_position_tolerance": None,
"source_position_sigma": 0.001,
}

Expand Down
3 changes: 1 addition & 2 deletions test/test_Sampling/test_likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,9 @@ def condition_definition(
"source_marg": True,
"astrometric_likelihood": True,
"image_position_uncertainty": 0.004,
"check_matched_source_position": False,
"source_position_tolerance": 0.001,
"source_position_likelihood": True,
"source_position_sigma": 0.001,
"check_positive_flux": True,
"flux_ratio_likelihood": True,
"prior_lens": [[0, "theta_E", 1, 0.1]],
"custom_logL_addition": condition_definition,
Expand Down
3 changes: 1 addition & 2 deletions test/test_Sampling/test_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ def setup_method(self):
kwargs_likelihood = {
"source_marg": False,
"image_position_uncertainty": 0.004,
"check_matched_source_position": False,
"source_position_tolerance": 0.001,
"source_position_tolerance": None,
"source_position_sigma": 0.001,
}
self.param_class = Param(kwargs_model, **kwargs_constraints)
Expand Down
3 changes: 1 addition & 2 deletions test/test_Workflow/test_fitting_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ def setup_method(self):
"source_marg": True,
"linear_prior": [1],
"image_position_uncertainty": 0.004,
"check_matched_source_position": False,
"source_position_tolerance": 0.001,
"source_position_tolerance": None,
"source_position_sigma": 0.001,
"check_positive_flux": True,
}
Expand Down

0 comments on commit 5614837

Please sign in to comment.