autofit.Analysis#
- class autofit.Analysis(use_jax: bool = False, use_jax_for_visualization: bool = False, **kwargs)[source]#
Bases:
ABCProtocol for an analysis. Defines methods that can or must be implemented to define a class that compute the likelihood that some instance fits some data.
Methods
Compute latent variables from a model instance.
Override to compute latent variables from the instance.
Build the fit used by the visualizer.
log_likelihood_functionReturns the Result of the non-linear search after it is completed.
Overwrite this method to modify the attributes of the Analysis class before the non-linear search begins.
Overwrite this method to modify the attributes of the Analysis class before the non-linear search begins.
modify_modelperform_quick_updatePrint JAX VRAM use for a given batch size.
save_attributessave_resultssave_results_combinedAssociate an explicit model with this analysis.
Attributes
LATENT_KEYSWhether this analysis supports background quick updates.
Whether the visualizer can work directly with JAX arrays.
- class Result(samples_summary: SamplesSummary, paths: AbstractPaths | None = None, samples: Samples | None = None, search_internal: object | None = None, analysis: Analysis | None = None)#
Bases:
AbstractResultThe result of a non-linear search.
The default behaviour is for all key results to be in the samples_summary attribute, which is a concise summary of the results of the non-linear search. The reasons for this to be the main attribute are:
It is concise and therefore has minimal I/O overhead, which is important because when runs are resumed
the results are loaded often, which can become very slow for large results via a samples.csv.
The output.yaml config files can be used to disable the output of the samples.csv file
and search_internal.dill files. This means in order for results to be loaded in a way that allows a run to resume, the samples_summary must contain all results necessary to resume the run.
For this reason, the samples and search_internal attributes are optional. On the first run of a model-fit, they will always contain values as they are passed in via memory from the results of the search. However, if a run is resumed they are no longer available in memory, and they will only be available if their corresponding samples.csv and search_internal.dill files are output on disk and available to load.
This object includes:
The samples_summary attribute, which is a summary of the results of the non-linear search.
The paths attribute, which contains the path structure to the results of the search on the hard-disk and
is used to load the samples and search internal attributes if they are required and not available in memory.
The samples of the non-linear search (E.g. MCMC chains, nested sampling samples) which are used to compute
the maximum likelihood model, posteriors and other properties.
The non-linear search used to perform the model fit in its internal format (e.g. the Dynesty sampler used
by dynesty itself as opposed to PyAutoFit abstract classes).
- Parameters:
samples_summary – A summary of the most important samples of the non-linear search (e.g. maximum log likelihood, median PDF).
paths – The paths to the results of the search, used to load the samples and search internal attributes if they are required and not available in memory.
samples – The samples of the non-linear search, for example the MCMC chains.
search_internal – The non-linear search used to perform the model fit in its internal format.
analysis – The Analysis object that was used to perform the model-fit from which this result is inferred.
- property projected_model: AbstractPriorModel#
Create a new model with the same structure as the previous model, replacing each prior with a new prior created by calculating sufficient statistics from samples and corresponding weights for that prior.
- property samples: Samples | None#
Returns the samples of the non-linear search, for example the MCMC chains or nested sampling samples.
When a model-fit is run the first time, the samples are passed into the result via memory and therefore always available.
However, if a model-fit is resumed the samples are not available in memory and they only way to load them is via the samples.csv file output on the hard-disk. This property handles the loading of the samples from the samples.csv file if they are not available in memory.
- Return type:
The samples of the non-linear search.
- property search_internal#
Returns the non-linear search used to perform the model fit in its internal sampler format.
When a model-fit is run the first time, the search internal is passed into the result via memory and therefore always available.
However, if a model-fit is resumed the search internal is not available in memory and they only way to load it is via the search_internal.dill file output on the hard-disk. This property handles the loading of the search internal from the search_internal.dill file if it is not available in memory.
- Return type:
The non-linear search used to perform the model fit in its internal sampler format.
- fit_for_visualization(instance)[source]#
Build the fit used by the visualizer.
Dispatch over
self.fit_fromwith an opt-injax.jitfast path:use_jax_for_visualization=False(default) — plainself.fit_from(instance). Untouched by JAX.use_jax_for_visualization=True— lazily constructjax.jit(self.fit_from)on the first call and cache it on the instance as_jitted_fit_from, then call that for every subsequent visualization. The first call pays the compile cost; subsequent calls reuse the cached compiled function.
Caching is per-
Analysisinstance so each analysis gets its own compiled function keyed off that instance’s closed-over state (self.dataset,self.settings, etc. — these ride as pytree aux data viaregister_instance_pytree(FitImaging, no_flatten=...)in PyAutoLens).fit_fromis defined by Analysis subclasses (e.g.AnalysisImaging), not the base class — this method is only callable on subclasses that provide it. Downstream visualizers should prefer this over callingfit_fromdirectly so the JIT seam stays in one place.For the JIT path to succeed, the
Fit*return type (and every nested autoarray / galaxy / lens type it carries) must be pytree- registered. That wiring lives in each analysis subclass (seeAnalysisImaging._register_fit_imaging_pytreesin PyAutoLens). Variants that have not yet been pytree-audited must leaveuse_jax_for_visualizationat its default ofFalse.
- compute_latent_samples(samples: Samples, batch_size: int | None = None) Samples | None[source]#
Compute latent variables from a model instance.
A latent variable is not itself a free parameter of the model but can be derived from it. Latent variables may provide physically meaningful quantities that help interpret a model fit, and their values (with errors) are stored in latent.csv in parallel with samples.csv.
This implementation is designed to be compatible with both NumPy and JAX:
It is written to be side-effect free, so it can be JIT-compiled with jax.jit.
It can be vectorized over many parameter sets at once using jax.vmap, enabling efficient batched evaluation of latent variables for multiple samples.
Returned values should be simple JAX/NumPy scalars or arrays (no Python objects), so they can be stacked into arrays of shape (n_samples, n_latents) for batching.
Any NaNs introduced (e.g. from invalid model states) can be masked or replaced downstream.
- Parameters:
parameters (array-like) – The parameter vector of the model sample. This will typically come from the non-linear search. Inside this method it is mapped back to a model instance via model.instance_from_vector.
model (Model) – The model object defining how the parameter vector is mapped to an instance. Passed explicitly so that this function can be used inside JAX transforms (vmap, jit) with functools.partial.
- Returns:
A tuple containing the latent variables in a fixed order: (intensity_total, magnitude, angle). Each entry may be NaN if the corresponding component of the model is not present.
- Return type:
- compute_latent_variables(parameters, model) Dict[str, float][source]#
Override to compute latent variables from the instance.
Latent variables are expressed as a dictionary: {“name”: value}
More complex models can be expressed by separating variables names by ‘.’ {“name.attribute”: value}
- Parameters:
instance – An instance of the model.
- Return type:
The computed latent variables.
- with_model(model)[source]#
Associate an explicit model with this analysis. Instances of the model will be used to compute log likelihood in place of the model passed from the search.
- Parameters:
model – A model to associate with this analysis
- Return type:
An analysis for that model
- modify_before_fit(paths: AbstractPaths, model: AbstractPriorModel)[source]#
Overwrite this method to modify the attributes of the Analysis class before the non-linear search begins.
An example use-case is using properties of the model to alter the Analysis class in ways that can speed up the fitting performed in the log_likelihood_function.
- modify_after_fit(paths: AbstractPaths, model: AbstractPriorModel, result: Result)[source]#
Overwrite this method to modify the attributes of the Analysis class before the non-linear search begins.
An example use-case is using properties of the model to alter the Analysis class in ways that can speed up the fitting performed in the log_likelihood_function.
- make_result(samples_summary: SamplesSummary, paths: AbstractPaths, samples: SamplesPDF | None = None, search_internal: object | None = None, analysis: object | None = None) Result[source]#
Returns the Result of the non-linear search after it is completed.
The result type is defined as a class variable in the Analysis class. It can be manually overwritten by a user to return a user-defined result object, which can be extended with additional methods and attributes specific to the model-fit.
The standard Result object may include:
The samples summary, which contains the maximum log likelihood instance and median PDF model.
The paths of the search, which are used for loading the samples and search internal below when a search
is resumed.
The samples of the non-linear search (e.g. MCMC chains) also stored in samples.csv.
The non-linear search used for the fit in its internal representation, which is used for resuming a search
and making bespoke visualization using the search’s internal results.
The analysis used to fit the model (default disabled to save memory, but option may be useful for certain
projects).
- Parameters:
samples_summary – The summary of the samples of the non-linear search, which include the maximum log likelihood instance and median PDF model.
paths – An object describing the paths for saving data (e.g. hard-disk directories or entries in sqlite database).
samples – The samples of the non-linear search, for example the chains of an MCMC run.
search_internal – The internal representation of the non-linear search used to perform the model-fit.
analysis – The analysis used to fit the model.
- Returns:
The result of the non-linear search, which is defined as a class variable in the Analysis class.
- Return type:
- property supports_jax_visualization: bool#
Whether the visualizer can work directly with JAX arrays.
Derived from the
use_jax_for_visualizationflag passed at construction time. Subclasses may override to force a specific answer (e.g. an Analysis that has been audited to support JAX visualization unconditionally).