|
| 1 | +""" |
| 2 | +Analysis class for fitting a ``Tracer`` model to a weak-lensing shear catalogue. |
| 3 | +
|
| 4 | +``AnalysisWeak`` implements the ``log_likelihood_function`` called by a ``PyAutoFit`` |
| 5 | +non-linear search at each iteration. It: |
| 6 | +
|
| 7 | +1. Constructs a ``Tracer`` from the current model instance. |
| 8 | +2. Calls ``FitWeak`` to compare the tracer's model shear field (evaluated at the |
| 9 | + catalogue's galaxy positions via ``LensCalc.shear_yx_2d_via_hessian_from``) against |
| 10 | + the observed ``WeakDataset``. |
| 11 | +3. Returns the fit's log likelihood as the figure of merit. |
| 12 | +
|
| 13 | +It also manages result output (``ResultWeak``) and on-the-fly visualisation |
| 14 | +(``VisualizerWeak``). |
| 15 | +""" |
| 16 | +import autofit as af |
| 17 | +import autogalaxy as ag |
| 18 | + |
| 19 | +from autogalaxy.analysis.analysis.analysis import Analysis as AgAnalysis |
| 20 | + |
| 21 | +from autolens.analysis.analysis.lens import AnalysisLens |
| 22 | +from autolens.weak.dataset import WeakDataset |
| 23 | +from autolens.weak.fit import FitWeak |
| 24 | +from autolens.weak.model.result import ResultWeak |
| 25 | +from autolens.weak.model.visualizer import VisualizerWeak |
| 26 | + |
| 27 | + |
| 28 | +class AnalysisWeak(AgAnalysis, AnalysisLens): |
| 29 | + Visualizer = VisualizerWeak |
| 30 | + Result = ResultWeak |
| 31 | + |
| 32 | + def __init__( |
| 33 | + self, |
| 34 | + dataset: WeakDataset, |
| 35 | + cosmology: ag.cosmo.LensingCosmology = None, |
| 36 | + title_prefix: str = None, |
| 37 | + use_jax: bool = False, |
| 38 | + **kwargs, |
| 39 | + ): |
| 40 | + """ |
| 41 | + Fits a lens model to a weak-lensing shear catalogue via a non-linear search. |
| 42 | +
|
| 43 | + The `Analysis` class defines the `log_likelihood_function` which fits the model to the dataset and returns the |
| 44 | + log likelihood value defining how well the model fitted the data. |
| 45 | +
|
| 46 | + It handles many other tasks, such as visualization, outputting results to hard-disk and storing results in |
| 47 | + a format that can be loaded after the model-fit is complete. |
| 48 | +
|
| 49 | + This class is used for model-fits which fit lens mass models to `WeakDataset` shear catalogues — the |
| 50 | + weak-lensing analogue of `AnalysisImaging` / `AnalysisPoint`. Each background galaxy in the catalogue |
| 51 | + contributes two independent shear measurements (gamma_1 and gamma_2), which `FitWeak` compares against |
| 52 | + the model shear field of the `Tracer`. |
| 53 | +
|
| 54 | + `use_jax` defaults to `False` because `FitWeak` is a NumPy-only fit (its `model_shear` is cached via |
| 55 | + `functools.cached_property` and its statistics use `np.asarray`); JAX support requires pytree |
| 56 | + registration of `FitWeak` and an `xp`-threaded fit path, which is deliberate future work. |
| 57 | +
|
| 58 | + Parameters |
| 59 | + ---------- |
| 60 | + dataset |
| 61 | + The `WeakDataset` that is fitted by the model, containing the observed per-galaxy shear |
| 62 | + measurements, their positions and the per-galaxy noise. |
| 63 | + cosmology |
| 64 | + The Cosmology assumed for this analysis. |
| 65 | + title_prefix |
| 66 | + A string that is added before the title of all figures output by visualization, for example to |
| 67 | + put the name of the dataset and galaxy in the title. |
| 68 | + """ |
| 69 | + super().__init__(cosmology=cosmology, use_jax=use_jax, **kwargs) |
| 70 | + |
| 71 | + AnalysisLens.__init__(self=self, cosmology=cosmology, use_jax=use_jax) |
| 72 | + |
| 73 | + self.dataset = dataset |
| 74 | + |
| 75 | + self.title_prefix = title_prefix |
| 76 | + |
| 77 | + def log_likelihood_function(self, instance): |
| 78 | + """ |
| 79 | + Given an instance of the model, where the model parameters are set via a non-linear search, fit the model |
| 80 | + instance to the weak-lensing shear catalogue. |
| 81 | +
|
| 82 | + This function returns a log likelihood which is used by the non-linear search to guide the model-fit. |
| 83 | +
|
| 84 | + For this analysis class, this function performs the following steps: |
| 85 | +
|
| 86 | + 1) Extracts all galaxies from the model instance and sets up a `Tracer`, which includes ordering the galaxies |
| 87 | + by redshift to set up each `Plane`. |
| 88 | +
|
| 89 | + 2) Uses the `Tracer` to create a `FitWeak` object, which evaluates the tracer's shear field at the |
| 90 | + catalogue's galaxy positions (via the same `LensCalc.shear_yx_2d_via_hessian_from` primitive the |
| 91 | + `SimulatorShearYX` uses) and compares it to the observed shears. |
| 92 | +
|
| 93 | + 3) Returns the fit's log likelihood — a Gaussian likelihood over the N x 2 independent shear components. |
| 94 | +
|
| 95 | + Parameters |
| 96 | + ---------- |
| 97 | + instance |
| 98 | + An instance of the model that is being fitted to the data by this analysis (whose parameters have been set |
| 99 | + via a non-linear search). |
| 100 | +
|
| 101 | + Returns |
| 102 | + ------- |
| 103 | + float |
| 104 | + The log likelihood indicating how well this model instance fitted the weak-lensing data. |
| 105 | + """ |
| 106 | + return self.fit_from(instance=instance).log_likelihood |
| 107 | + |
| 108 | + def fit_from(self, instance) -> FitWeak: |
| 109 | + """ |
| 110 | + Given a model instance create a `FitWeak` object. |
| 111 | +
|
| 112 | + This function is used in the `log_likelihood_function` to fit the model to the weak-lensing data and |
| 113 | + compute the log likelihood. |
| 114 | +
|
| 115 | + Parameters |
| 116 | + ---------- |
| 117 | + instance |
| 118 | + An instance of the model that is being fitted to the data by this analysis (whose parameters have been set |
| 119 | + via a non-linear search). |
| 120 | +
|
| 121 | + Returns |
| 122 | + ------- |
| 123 | + The fit of the lens model to the weak-lensing shear catalogue. |
| 124 | + """ |
| 125 | + tracer = self.tracer_via_instance_from( |
| 126 | + instance=instance, |
| 127 | + ) |
| 128 | + |
| 129 | + return FitWeak( |
| 130 | + dataset=self.dataset, |
| 131 | + tracer=tracer, |
| 132 | + ) |
| 133 | + |
| 134 | + def save_attributes(self, paths: af.DirectoryPaths): |
| 135 | + """ |
| 136 | + Before the non-linear search begins, this routine saves attributes of the `Analysis` object to the `files` |
| 137 | + folder such that they can be loaded after the analysis using PyAutoFit's database and aggregator tools. |
| 138 | +
|
| 139 | + For this analysis, it outputs the following: |
| 140 | +
|
| 141 | + - The weak-lensing shear catalogue as a readable .json file. |
| 142 | +
|
| 143 | + It is common for these attributes to be loaded by many of the template aggregator functions given in the |
| 144 | + `aggregator` modules. For example, when using the database tools to perform a fit, the default behaviour is for |
| 145 | + the dataset, settings and other attributes necessary to perform the fit to be loaded via the pickle files |
| 146 | + output by this function. |
| 147 | +
|
| 148 | + Parameters |
| 149 | + ---------- |
| 150 | + paths |
| 151 | + The paths object which manages all paths, e.g. where the non-linear search outputs are stored, |
| 152 | + visualization, and the pickled objects used by the aggregator output by this function. |
| 153 | + """ |
| 154 | + ag.output_to_json( |
| 155 | + obj=self.dataset, |
| 156 | + file_path=paths._files_path / "dataset.json", |
| 157 | + ) |
0 commit comments