autolens.AnalysisImaging#

class AnalysisImaging[source]#

Bases: AnalysisDataset

Fits a lens model to an imaging dataset via a non-linear search (see AnalysisDataset for the full docstring of the shared parameters).

Parameters:

shared_preloads (bool) – Opts this analysis into the cross-factor shared-state mechanism of a FactorGraphModel (see shared_state_from). Set this to True only when this analysis is one of many exposures of the same lens (e.g. multi-exposure imaging with per-exposure pixel offsets) sharing an identical lens model, so the exposure-invariant source-plane mesh geometry can be computed once and reused by every exposure. False by default, leaving the standard per-analysis behaviour unchanged.

Methods

adapt_images_via_instance_from

compute_latent_samples

Compute latent-variable samples for every posterior sample.

compute_latent_variables

Override to compute latent variables from the instance.

dataset_model_via_instance_from

Create a dataset model from a model instance, which is used to fit the dataset.

fit_for_visualization

Build the fit used by the visualizer.

fit_from

Given a model instance create a FitImaging object.

galaxies_via_instance_from

Create a list of galaxies from a model instance, which is used to fit the dataset.

log_likelihood_function

Given an instance of the model, where the model parameters are set via a non-linear search, fit the model instance to the imaging dataset.

log_likelihood_penalty_from

Call the positions overwrite log likelihood function, which add a penalty term to the likelihood if the positions of the multiple images of the lensed source do not trace close to one another in the source plane.

make_result

After the non-linear search is complete create its Result, which includes:

modify_after_fit

Overwrite this method to modify the attributes of the Analysis class before the non-linear search begins.

modify_before_fit

This function is called immediately before the non-linear search begins and performs final tasks and checks before it begins.

modify_model

perform_quick_update

Perform a quick visualization update during non-linear search fitting.

print_vram_use

Print JAX VRAM use for a given batch size.

raise_exceptions

save_attributes

Before the non-linear search begins, output the imaging dataset.fits to the files folder so the aggregator loaders (e.g. ImagingAgg, agg_util.mask_header_from) can always reload the dataset via fit.value(name="dataset"), independently of whether the visualization fits_dataset output ran.

save_results

At the end of a model-fit, this routine saves attributes of the Analysis object to the files folder such that they can be loaded after the analysis using PyAutoFit's database and aggregator tools.

save_results_combined

shared_state_from

Compute the exposure-invariant source-plane mesh geometry once so it can be shared across the factors of a multi-exposure FactorGraphModel (see autofit.Analysis.shared_state_from).

tracer_via_instance_from

Create a Tracer from the galaxies contained in a model instance.

with_model

Associate an explicit model with this analysis.

Attributes

LATENT_BATCH_MODE

LATENT_KEYS

supports_background_update

Whether this analysis supports background quick updates.

supports_jax_visualization

Whether the visualizer can work directly with JAX arrays.

Result#

alias of ResultImaging

Visualizer#

alias of VisualizerImaging

Latent#

alias of LatentLens

log_likelihood_function(instance, shared=None)[source]#

Given an instance of the model, where the model parameters are set via a non-linear search, fit the model instance to the imaging dataset.

This function returns a log likelihood which is used by the non-linear search to guide the model-fit.

For this analysis class, this function performs the following steps:

  1. If the analysis has a adapt image, associated the model galaxy images of this dataset to the galaxies in the model instance.

  2. Extract attributes which model aspects of the data reductions, like the scaling the background sky and background noise.

  3. Extracts all galaxies from the model instance and set up a Tracer, which includes ordering the galaxies by redshift to set up each Plane.

  4. Use the Tracer and other attributes to create a FitImaging object, which performs steps such as creating model images of every galaxy in the tracer, blurring them with the imaging dataset’s PSF and computing residuals, a chi-squared statistic and the log likelihood.

Certain models will fail to fit the dataset and raise an exception. For example if an Inversion is used, the linear algebra calculation may be invalid and raise an Exception. In such circumstances the model is discarded and its likelihood value is passed to the non-linear search in a way that it ignores it (for example, using a value of -1.0e99).

Parameters:
  • instance (ModelInstance) – An instance of the model that is being fitted to the data by this analysis (whose parameters have been set via a non-linear search).

  • shared – The cross-factor shared state of a FactorGraphModel, computed once per evaluation by the lead factor’s shared_state_from (see that method). For this analysis it is a PreloadsImaging carrying the exposure-invariant source-plane mesh geometry; when provided it is reused by the fit instead of being recomputed. None (the default, e.g. a standalone fit) leaves behaviour unchanged.

Returns:

The log likelihood indicating how well this model instance fitted the imaging data.

Return type:

float

shared_state_from(instance)[source]#

Compute the exposure-invariant source-plane mesh geometry once so it can be shared across the factors of a multi-exposure FactorGraphModel (see autofit.Analysis.shared_state_from).

When shared_preloads is set, every factor of the graph is an exposure of the same lens sharing an identical lens model, so the source-plane mesh (the image-mesh centres of this lead exposure, ray-traced through the shared lens model) is built once here and returned inside a PreloadsImaging, which FactorGraphModel forwards as the shared argument to every factor’s log_likelihood_function. Each exposure then maps its own (offset) data grid onto the shared mesh instead of computing its own image-mesh and mesh ray-trace, so every exposure reconstructs on an identical source-pixel grid.

Unlike the interferometer datacube case, the mapper, mapping matrix, curvature matrix and regularization matrix are NOT shared — per-exposure PSFs and pixel offsets make the first three per-dataset, and regularization may adapt to per-exposure data.

Returns None when the analysis has not opted in (shared_preloads=False) or when the model performs no inversion, in which case no state is shared and every factor fits as normal.

The caller is responsible for the invariance contract: only enable shared_preloads when the factors genuinely share the lens model, so the source-plane mesh really is exposure-invariant. The lead factor’s own DatasetModel offset (if any) is applied when the mesh is traced, so the mesh is defined in the lead exposure’s frame.

fit_from(instance, preloads=None)[source]#

Given a model instance create a FitImaging object.

This function is used in the log_likelihood_function to fit the model to the imaging data and compute the log likelihood.

Parameters:
  • instance (ModelInstance) – An instance of the model that is being fitted to the data by this analysis (whose parameters have been set via a non-linear search).

  • preloads – An optional PreloadsImaging carrying the exposure-invariant source-plane mesh geometry, computed once and reused by the fit instead of being rebuilt. Supplied by the multi-exposure shared-state path (see shared_state_from); None (the default) fits as normal.

Returns:

The fit of the plane to the imaging dataset, which includes the log likelihood.

Return type:

FitImaging

save_attributes(paths)[source]#

Before the non-linear search begins, output the imaging dataset.fits to the files folder so the aggregator loaders (e.g. ImagingAgg, agg_util.mask_header_from) can always reload the dataset via fit.value(name="dataset"), independently of whether the visualization fits_dataset output ran. The plotter interface also writes this file to the image folder for inspection, but that write is gated on visualization settings and is not guaranteed for every fit.