LensModel

The LensModel module is the interface between the user and

LensFactory.LensModel.read_inputFunction
read_input(filename::AbstractString) -> ModelConfig

Reads the input YAML file and constructs a ModelConfig struct containing all the necessary information for lens modeling and sampling. For details on the expected structure of the input YAML file, please refer to Example - 2.

Arguments

  • filename::AbstractString: Path to the input YAML file.

Returns

  • ModelConfig::ModelConfig: A struct containing the observation details, cosmology, lens configuration, source configuration, parameter definitions, and sampling configuration.
source
LensFactory.LensModel.fit_modelFunction
fit_model(model::ModelConfig; save::Bool=true, file_name::Union{String, Nothing}=nothing)

Performs lens model fitting using the given model.

Arguments:

  • model::ModelConfig: The lens model configuration containing the observation, lens, source, and sampler details.
  • save::Bool=true: Whether to save the MCMC results to a JLD2 file (default: true)
  • file_name::Union{String, Nothing}=nothing: The name of the JLD2 file to save results to. If no name is provided then a name will be generated based on the lens name and current date (i.e., LensName_DDMMYYYY.jld2).

Returns:

  • chains::Array{Float64, 3}: The MCMC chains containing sampled parameter values
  • chi2::Matrix{Float64}: The chi-squared values corresponding to each sample in the chains
source
LensFactory.LensModel.get_best_modelFunction

getbestmodel(model::ModelConfig, chains::Array{Float64, 3}, chi2::Matrix{Float64}) Get the best-fit lens model based on the MCMC results stored in chains and chi2. The best-fit parameters are determined by the minimum chi2 in chi2.

Arguments

  • model::ModelConfig: The lens model configuration used for the MCMC fit.
  • chains::Array{Float64, 3}: The MCMC chains containing the sampled parameter values. The dimensions should be (nsteps, nchains, n_parameters).
  • chi2::Matrix{Float64}: The chi2 values corresponding to each sample in the MCMC chains. The dimensions should be (nsteps, nsteps).

Returns

  • best_model: The best-fit lens model constructed using the best-fit parameters.
source
LensFactory.LensModel.get_potentialFunction
get_potential(file_name::String, θx::T, θy::T; unit::Symbol=:RA_DEC) where T <: Union{ROA, Vector{Int64}}

Calculate the lensing potential at the given coordinates (θx, θy) for the best-fit model based on the MCMC results stored in file_name. The user can specify the unit of the input coordinates as either RA/DEC or arcseconds.

Arguments

  • file_name::String: Path to the JLD2 file containing the MCMC results
  • θx: x-coordinates
  • θy: y-coordinates

Keyword Arguments

  • unit::Symbol=:RA_DEC: Unit of the input coordinates. Supported units are :RA_DEC and :arcsec. If :RA_DEC then (θx, θy) = (RA, DEC) is assumed and will be converted to arcseconds relative to the reference position in the model.

Returns

  • ψ: Lensing potential at the input coordinates for the best-fit model.
source
LensFactory.LensModel.get_deflectionFunction
get_deflection(file_name::String, θx::T, θy::T; unit::Symbol=:RA_DEC) where T <: Union{ROA, Vector{Int64}}

Calculate the lensing deflection at the given coordinates (θx, θy) for the best-fit model based on the MCMC results stored in file_name. The user can specify the unit of the input coordinates as either RA/DEC or arcseconds.

Arguments

  • file_name::String: Path to the JLD2 file containing the MCMC results
  • θx: x-coordinates
  • θy: y-coordinates

Keyword Arguments

  • unit::Symbol=:RA_DEC: Unit of the input coordinates. Supported units are :RA_DEC and :arcsec. If :RA_DEC then (θx, θy) = (RA, DEC) is assumed and will be converted to arcseconds relative to the reference position in the model.

Returns

  • αx: x-component of the deflection angle (in arcseconds).
  • αy: y-component of the deflection angle (in arcseconds).
source
LensFactory.LensModel.get_jacobianFunction
get_jacobian(file_name::String, θx::T, θy::T; unit::Symbol=:RA_DEC) where T <: Union{ROA, Vector{Int64}}

Calculate the Jacobian (i.e., deformation tensor) of the lens mapping at the given coordinates (θx, θy) for the best-fit model based on the MCMC results stored in file_name. The user can specify the unit of the input coordinates as either RA/DEC or arcseconds.

Arguments

  • file_name::String: Path to the JLD2 file containing the MCMC results
  • θx: x-coordinates
  • θy: y-coordinates

Keyword Arguments

  • unit::Symbol=:RA_DEC: Unit of the input coordinates. Supported units are :RA_DEC and :arcsec. If :RA_DEC then (θx, θy) = (RA, DEC) is assumed and will be converted to arcseconds relative to the reference position in the model.

Returns

  • ψxx: xx-component of the jacobian.
  • ψyy: yy-component of the jacobian.
  • ψxy: xy-component of the jacobian.
source
LensFactory.LensModel.save_best_fitFunction
save_best_fit(file_name::String; save_potential::Bool=true, save_deflection::Bool=true, save_kappa::Bool=true, save_gamma::Bool=true)

Save fits files for the best-fit model based on the MCMC results stored in file_name. The user can choose which lensing quantities to save by setting the corresponding boolean flags.

Arguments

  • file_name::String: Path to the JLD2 file containing the MCMC results

Keyword Arguments

  • save_potential::Bool=true: Whether to save the lensing potential as potential.fits
  • save_deflection::Bool=true: Whether to save the deflection angles as alpha_x.fits and alpha_y.fits
  • save_kappa::Bool=true: Whether to save the convergence map as kappa.fits
  • save_gamma::Bool=true: Whether to save the shear maps as gamma1.fits and gamma2.fits

Returns

  • Nothing (saves FITS files to disk)
source
LensFactory.LensModel.check_parityFunction
check_parity(model::ModelConfig, chains::Array{Float64, 3}, chi2::Matrix{Float64})

Check the parity of the best-fit model against the input parities for each knot image. This function assumes that parity was enforced during the modelling (i.e., model.source_config.use_parity = true). If parity was not enforced, an error is thrown. The function prints a table comparing the input parity and the best-fit model parity for each knot image, along with a status indicating whether the parity matches or not.

Arguments

  • model::ModelConfig: The lens model configuration used for the MCMC fit.
  • chains::Array{Float64, 3}: The MCMC chains containing the sampled parameter values. The dimensions should be (nsteps, nchains, n_parameters).
  • chi2::Matrix{Float64}: The chi2 values corresponding to each sample in the MCMC chains. The dimensions should be (nsteps, nsteps).

Returns

  • Nothing (prints a table to the console)
source
LensFactory.LensModel.get_best_fit_rmsFunction
get_best_fit_rms(model::ModelConfig, chains::Array{Float64, 3}, chi2::Matrix{Float64}; check_parity::Bool=false)

Calculate the RMS of the best-fit model based on the MCMC results stored in chains and chi2. The function prints a table showing the RMS for each knot image, as well as a global total RMS. If check_parity is set to true, the function will also check the parity of each knot image and print a warning if any parity does not match.

Arguments

  • model::ModelConfig: The lens model configuration used for the MCMC fit.
  • chains::Array{Float64, 3}: The MCMC chains containing the sampled parameter values. The dimensions should be (nsteps, nchains, n_parameters).
  • chi2::Matrix{Float64}: The chi2 values corresponding to each sample in the MCMC chains. The dimensions should be (nsteps, nsteps).

Keyword Arguments

  • check_parity::Bool=false: Whether to check the parity of each knot image against the input parity.

Returns

  • Nothing (prints a table to the console)
source