lenstronomy.LensModel.Solver package¶
Submodules¶
lenstronomy.LensModel.Solver.epl_shear_solver module¶
- solvelenseq_majoraxis(args, Nmeas=200, Nmeas_extra=50)[source]¶
Solve the lens equation, where the arguments have been properly rotated to the major-axis.
- solve_lenseq_pemd(pos_, kwargs_lens, Nmeas=400, Nmeas_extra=80, **kwargs)[source]¶
Solves the lens equation using a semi-analytical recipe.
- Parameters:
pos – The source plane position (shape (2,)), or the source plane positions (shape (2,N)) for which to solve the lens equation
kwargs_lens – List of kwargs in lenstronomy style, following [‘EPL’, ‘SHEAR’] format
Nmeas – resolution with which to sample the angular grid, higher means more reliable lens equation solving. For solving many positions at once, you may want to set this higher.
Nmeas_extra – resolution with which to additionally sample the angular grid at the low-shear end, higher means more reliable lens equation solving. For solving many positions at once, you may want to set this higher.
- Returns:
The lens plane positions.
Note: generally the (demagnified) central image will also be included.
- caustics_epl_shear(kwargs_lens, num_th=500, maginf=0, sourceplane=True, return_which=None)[source]¶
Analytically calculates the caustics of an EPL+shear lens model. Since for gamma>2, the outer critical curve does not exist, the option to find the curves for a set, finite magnification exists, by supplying maginf, so that the routine finds the curve of this magnification, rather than the true caustic.
- Parameters:
kwargs_lens – List of kwargs in lenstronomy style, following [‘EPL’, ‘SHEAR’] format
num_th – resolution.
maginf – the outer critical curve for t>1 will be replaced with the curve where the inverse magnification is maginf
sourceplane – if True (default), ray-shoot the calculated critical curves to the source plane
return_which – options ‘quad’ (boundary of area within which there are 4 images), ‘double’ (boundary of area within which there are 2 images), ‘caustic’ (the diamond caustic) and ‘cut’ (the cut, if it exists, that is if t<2, else, if t>2, returns the caustic) and None (in that case: return quad, caustic, cut)
- Returns:
(2,N) array if return_which set, else a tuple of (caustic, cut, quad)
lenstronomy.LensModel.Solver.lens_equation_solver module¶
- class LensEquationSolver(lensModel)[source]¶
Bases:
objectClass to solve for image positions given lens model and source position.
- __init__(lensModel)[source]¶
This class must contain the following definitions (with same syntax as the standard LensModel() class: def ray_shooting() def hessian() def magnification()
- Parameters:
lensModel – instance of a class according to lenstronomy.LensModel.lens_model
- change_source_redshift(z_source=None)[source]¶
Change source redshift in solver.
- Parameters:
z_source (float or None)
- Returns:
updated lens model instance
- image_position_stochastic(source_x, source_y, kwargs_lens, search_window=10, precision_limit=1e-10, arrival_time_sort=True, x_center=0, y_center=0, num_random=1000)[source]¶
Solves the lens equation stochastic with the scipy minimization routine on the quadratic distance between the backwards ray-shooted proposed image position and the source position. Credits to Giulia Pagano.
- Parameters:
source_x – source position
source_y – source position
kwargs_lens – lens model list of keyword arguments
search_window – angular size of search window
precision_limit – limit required on the precision in the source plane
arrival_time_sort – bool, if True sorts according to arrival time
x_center – center of search window
y_center – center of search window
num_random – number of random starting points of the non-linear solver in the search window
- Returns:
x_image, y_image
- candidate_solutions(sourcePos_x, sourcePos_y, kwargs_lens, min_distance=0.1, search_window=10, verbose=False, x_center=0, y_center=0)[source]¶
Finds pixels in the image plane possibly hosting a solution of the lens equation, for the given source position and lens model.
- Parameters:
sourcePos_x – source position in units of angle
sourcePos_y – source position in units of angle
kwargs_lens – lens model parameters as keyword arguments
min_distance – minimum separation to consider for two images in units of angle
search_window – window size to be considered by the solver. Will not find image position outside this window
verbose – bool, if True, prints some useful information for the user
x_center – float, center of the window to search for point sources
y_center – float, center of the window to search for point sources
- Returns:
(approximate) angular position of (multiple) images ra_pos, dec_pos in units of angles, related ray-traced source displacements and pixel width
- Raises:
AttributeError, KeyError
- image_position_analytical(x, y, kwargs_lens, arrival_time_sort=True, magnification_limit=None, **kwargs_solver)[source]¶
Solves the lens equation. Only supports EPL-like (plus shear) models. Uses a specialized recipe that solves a one-dimensional lens equation that is easier and more reliable to solve than the usual two-dimensional lens equation.
- Parameters:
x – source position in units of angle, an array of positions is also supported.
y – source position in units of angle, an array of positions is also supported.
kwargs_lens – lens model parameters as keyword arguments
arrival_time_sort – bool, if True, sorts image position in arrival time (first arrival photon first listed)
magnification_limit – None or float, if set will only return image positions that have an abs(magnification) larger than this number
kwargs_solver – additional kwargs to be supplied to the solver. Particularly relevant are Nmeas and Nmeas_extra
- Returns:
(exact) angular position of (multiple) images ra_pos, dec_pos in units of angle Note: in contrast to the other solvers, generally the (heavily demagnified) central image will also be included, so setting a proper magnification_limit is more important. To get similar behaviour, a limit of 1e-1 is acceptable
- image_position_from_source(sourcePos_x, sourcePos_y, kwargs_lens, solver='lenstronomy', **kwargs)[source]¶
Solves the lens equation, i.e. finds the image positions in the lens plane that are mapped to a given source position.
- Parameters:
sourcePos_x – source position in units of angle
sourcePos_y – source position in units of angle
kwargs_lens – lens model parameters as keyword arguments
solver – which solver to use, can be ‘lenstronomy’ (default), ‘analytical’ or ‘stochastic’.
kwargs – Any additional kwargs are passed to the chosen solver, see the documentation of image_position_lenstronomy, image_position_analytical and image_position_stochastic
- Returns:
(exact) angular position of (multiple) images ra_pos, dec_pos in units of angle
- image_position_lenstronomy(sourcePos_x, sourcePos_y, kwargs_lens, min_distance=0.1, search_window=10, precision_limit=1e-10, num_iter_max=100, arrival_time_sort=True, initial_guess_cut=True, verbose=False, x_center=0, y_center=0, num_random=0, non_linear=False, magnification_limit=None)[source]¶
Finds image position given source position and lens model. The solver first samples does a grid search in the lens plane, and the grid points that are closest to the supplied source position are fed to a specialized gradient-based root finder that finds the exact solutions. Works with all lens models.
- Parameters:
sourcePos_x – source position in units of angle
sourcePos_y – source position in units of angle
kwargs_lens – lens model parameters as keyword arguments
min_distance – minimum separation to consider for two images in units of angle
search_window – window size to be considered by the solver. Will not find image position outside this window
precision_limit – required precision in the lens equation solver (in units of angle in the source plane).
num_iter_max – maximum iteration of lens-source mapping conducted by solver to match the required precision
arrival_time_sort – bool, if True, sorts image position in arrival time (first arrival photon first listed)
initial_guess_cut – bool, if True, cuts initial local minima selected by the grid search based on distance criteria from the source position
verbose – bool, if True, prints some useful information for the user
x_center – float, center of the window to search for point sources
y_center – float, center of the window to search for point sources
num_random – int, number of random positions within the search window to be added to be starting positions for the gradient decent solver
non_linear – bool, if True applies a non-linear solver not dependent on Hessian computation
magnification_limit – None or float, if set will only return image positions that have an abs(magnification) larger than this number
- Returns:
(exact) angular position of (multiple) images ra_pos, dec_pos in units of angle
- Raises:
AttributeError, KeyError
- find_bright_image(sourcePos_x, sourcePos_y, kwargs_lens, numImages=4, min_distance=0.01, search_window=5, precision_limit=1e-10, num_iter_max=10, arrival_time_sort=True, x_center=0, y_center=0, num_random=0, non_linear=False, magnification_limit=None, initial_guess_cut=True, verbose=False)[source]¶
- Parameters:
sourcePos_x – source position in units of angle
sourcePos_y – source position in units of angle
kwargs_lens – lens model parameters as keyword arguments
min_distance – minimum separation to consider for two images in units of angle
search_window – window size to be considered by the solver. Will not find image position outside this window
precision_limit – required precision in the lens equation solver (in units of angle in the source plane).
num_iter_max – maximum iteration of lens-source mapping conducted by solver to match the required precision
arrival_time_sort – bool, if True, sorts image position in arrival time (first arrival photon first listed)
initial_guess_cut – bool, if True, cuts initial local minima selected by the grid search based on distance criteria from the source position
verbose – bool, if True, prints some useful information for the user
x_center – float, center of the window to search for point sources
y_center – float, center of the window to search for point sources
num_random – int, number of random positions within the search window to be added to be starting positions for the gradient decent solver
non_linear – bool, if True applies a non-linear solver not dependent on Hessian computation
magnification_limit – None or float, if set will only return image positions that have an abs(magnification) larger than this number
- Returns:
(exact) angular position of (multiple) images ra_pos, dec_pos in units of angle
- sort_arrival_times(x_mins, y_mins, kwargs_lens)[source]¶
Sort arrival times (fermat potential) of image positions in increasing order of light travel time.
- Parameters:
x_mins – ra position of images
y_mins – dec position of images
kwargs_lens – keyword arguments of lens model
- Returns:
sorted lists of x_mins and y_mins
lenstronomy.LensModel.Solver.solver module¶
- class Solver(solver_type, lensModel, num_images, parameter_module=None)[source]¶
Bases:
objectJoint solve class to manage with type of solver to be executed and checks whether the requirements are fulfilled.
- __init__(solver_type, lensModel, num_images, parameter_module=None)[source]¶
- Parameters:
solver_type – string, option for specific solver type see detailed instruction of the Solver4Point and Solver2Point classes
lensModel – instance of a LensModel() class
num_images – int, number of images to be solved for
parameter_module – a class to be used with solver_type that has routines to be used in the Solver4Point module for parameter handling: extract_array, update_kwargs, and add_fixed_lens
- constraint_lensmodel(x_pos, y_pos, kwargs_list, xtol=1.49012e-12)[source]¶
- Parameters:
x_pos – x-position constraints on images
y_pos – y-position constraints on images
kwargs_list – lens model keyword argument list
xtol – tolerance level of solution when to stop the non-linear solver
- Returns:
updated lens model that satisfies the lens equation for the point sources, accuracy
- update_solver(kwargs_lens, x_pos, y_pos)[source]¶
- Parameters:
kwargs_lens – lens model keyword argument list
x_pos – x-position constraints on images
y_pos – y-position constraints on images
- Returns:
updated lens model that satisfies the lens equation for the point sources
- check_solver(image_x, image_y, kwargs_lens)[source]¶
Returns the precision of the solver to match the image position.
- Parameters:
kwargs_lens – full lens model (including solved parameters)
image_x – point source in image
image_y – point source in image
- Returns:
precision of Euclidean distances between the different rays arriving at the image positions
- add_fixed_lens(kwargs_fixed_lens, kwargs_lens_init)[source]¶
Returns kwargs that are kept fixed during run, depending on options.
- Parameters:
kwargs_fixed_lens – keyword argument list of fixed parameters (indicated by fitting argument of the user)
kwargs_lens_init – Initial values of the full lens model keyword arguments
- Returns:
updated kwargs_fixed_lens, added fixed parameters being added (and replaced later on) by the non-linear solver.
lenstronomy.LensModel.Solver.solver2point module¶
- class Solver2Point(lensModel, solver_type='CENTER', decoupling=True)[source]¶
Bases:
objectClass to solve a constraint lens model with two point source positions.
options are: ‘CENTER’: solves for ‘center_x’, ‘center_y’ parameters of the first lens model ‘ELLIPSE’: solves for ‘e1’, ‘e2’ of the first lens (can also be shear) ‘SHAPELETS’: solves for shapelet coefficients c01, c10 ‘THETA_E_PHI: solves for Einstein radius of first lens model and shear angle of second model
- __init__(lensModel, solver_type='CENTER', decoupling=True)[source]¶
- Parameters:
lensModel – instance of LensModel class
solver_type – string
decoupling – bool
- constraint_lensmodel(x_pos, y_pos, kwargs_list, xtol=1.49012e-12)[source]¶
Constrains lens model parameters by demanding the solution to match the image positions to a single source position.
- Parameters:
x_pos – list of image positions (x-axis)
y_pos – list of image position (y-axis)
kwargs_list – list of lens model kwargs
xtol – tolerance level of solution when to stop the non-linear solver
- Returns:
updated lens model that satisfies the lens equation for the point sources
lenstronomy.LensModel.Solver.solver4point module¶
- class Solver4Point(lensModel, solver_type='PROFILE', parameter_module=None)[source]¶
Bases:
objectClass to make the constraints for the solver.
- __init__(lensModel, solver_type='PROFILE', parameter_module=None)[source]¶
- Parameters:
lensModel – An instance of the LensModel class
solver_type – a string that specifies the possible solver routines; current implementations are ‘PROFILE’,’PROFILE_SHEAR’, ‘CUSTOM’
parameter_module – a class to be used with routines “extract_array”, “update_kwargs”, and “add_fixed_lens” with the same call signatures as the methods in this class
- constraint_lensmodel(x_pos, y_pos, kwargs_list, xtol=1.49012e-12)[source]¶
- Parameters:
x_pos – list of image positions (x-axis)
y_pos – list of image position (y-axis)
xtol – numerical tolerance level
kwargs_list – list of lens model kwargs
- Returns:
updated lens model that satisfies the lens equation for the point sources, accuracy