-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathanalysis.py
More file actions
163 lines (131 loc) · 5.72 KB
/
analysis.py
File metadata and controls
163 lines (131 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import logging
import numpy as np
from typing import List, Optional
import autofit as af
import autoarray as aa
from autogalaxy.galaxy.galaxy import Galaxy
from autogalaxy.galaxy.galaxies import Galaxies
from autogalaxy.cosmology.model import LensingCosmology
logger = logging.getLogger(__name__)
logger.setLevel(level="INFO")
class Analysis(af.Analysis):
def __init__(
self,
cosmology: LensingCosmology = None,
use_jax: bool = True,
**kwargs,
):
"""
Fits a model to a dataset via a non-linear search.
This abstract Analysis class for all model-fits which fit galaxies, but does not perform a model-fit by
itself (and is therefore only inherited from).
This class stores the Cosmology used for the analysis and adapt images used for certain model classes.
Parameters
----------
cosmology
The Cosmology assumed for this analysis.
"""
from autogalaxy.cosmology.model import Planck15
self.cosmology = cosmology or Planck15()
super().__init__(use_jax=use_jax, **kwargs)
def galaxies_via_instance_from(
self,
instance: af.ModelInstance,
) -> List[Galaxy]:
"""
Create a list of galaxies from a model instance, which is used to fit the dataset.
The instance may only contain galaxies, in which case this function is redundant. However, if extra galaxies
are included, the instance will contain both galaxies and extra galaxies, and they should be added to create
the single list of galaxies used to fit the dataset.
Parameters
----------
instance
An instance of the model that is fitted to the data by this analysis (whose parameters may have been set
via a non-linear search).
Returns
-------
A list of galaxies that is used to then fit the dataset.
"""
if hasattr(instance, "extra_galaxies"):
if getattr(instance, "extra_galaxies", None) is not None:
return Galaxies(
galaxies=instance.galaxies + instance.extra_galaxies,
)
return Galaxies(galaxies=instance.galaxies)
def dataset_model_via_instance_from(
self, instance: af.ModelInstance
) -> aa.DatasetModel:
"""
Create a dataset model from a model instance, which is used to fit the dataset.
Parameters
----------
instance
An instance of the model that is fitted to the data by this analysis (whose parameters may have been set
via a non-linear search).
Returns
-------
A dataset_model that is used to then fit the dataset.
"""
if hasattr(instance, "dataset_model"):
return instance.dataset_model
def make_result(
self,
samples_summary: af.SamplesSummary,
paths: af.AbstractPaths,
samples: Optional[af.SamplesPDF] = None,
search_internal: Optional[object] = None,
analysis: Optional[af.Analysis] = None,
) -> af.Result:
"""
After the non-linear search is complete create its `Result`, which includes:
- 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 model used to fit the data, which uses the samples to create specific instances of the model (e.g.
an instance of the maximum log likelihood model).
- The non-linear search used to perform the model fit.
The `ResultImaging` object contains a number of methods which use the above objects to create the max
log likelihood galaxies `FitImaging`, adapt-galaxy images,etc.
Parameters
----------
samples
A PyAutoFit object which contains the samples of the non-linear search, for example the chains of an MCMC
run of samples of the nested sampler.
search
The non-linear search used to perform this model-fit.
Returns
-------
ResultImaging
The result of fitting the model to the imaging dataset, via a non-linear search.
"""
return self.Result(
samples_summary=samples_summary,
paths=paths,
samples=samples,
search_internal=search_internal,
analysis=self,
)
def perform_quick_update(self, paths, instance):
"""
Perform a quick visualization update during non-linear search fitting.
This method is called intermittently while the sampler is running to produce
the `subplot+fit` plots of the current maximum-likelihood model fit. The intent
is to provide fast feedback (without waiting for the full run to complete) so that
users can monitor whether the fit is behaving sensibly.
The plot appears both in a matplotlib window (if running locally) and is also saved to the
`output` folder of the output path.
Parameters
----------
paths : af.DirectoryPaths
Object describing the output folder structure where visualization files
should be written.
instance : model instance
The current maximum-likelihood instance of the model, used to generate
the visualization plots.
"""
self.Visualizer().visualize(
analysis=self,
paths=paths,
instance=instance,
during_analysis=True,
quick_update=True,
)