Skip to content

Commit 341aba9

Browse files
Jammy2211Jammy2211
authored andcommitted
position lieklihood list now used in analysis
1 parent ca5023c commit 341aba9

18 files changed

Lines changed: 146 additions & 380 deletions

File tree

autolens/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@
8989
from .lens.tracer import Tracer
9090
from .lens.sensitivity import SubhaloSensitivityResult
9191
from .lens.to_inversion import TracerToInversion
92-
from .analysis.positions import PositionsLHResample
93-
from .analysis.positions import PositionsLHPenalty
92+
from .analysis.positions import PositionsLH
9493
from .imaging.simulator import SimulatorImaging
9594
from .imaging.fit_imaging import FitImaging
9695
from .imaging.model.analysis import AnalysisImaging

autolens/analysis/analysis/dataset.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313

1414
from autolens.analysis.analysis.lens import AnalysisLens
1515
from autolens.analysis.result import ResultDataset
16-
from autolens.analysis.positions import PositionsLHResample
17-
from autolens.analysis.positions import PositionsLHPenalty
16+
from autolens.analysis.positions import PositionsLH
1817

1918
from autolens import exc
2019

@@ -28,7 +27,7 @@ def __init__(
2827
self,
2928
dataset,
3029
positions_likelihood_list: Optional[
31-
List[Union[PositionsLHResample, PositionsLHPenalty]]
30+
List[PositionsLH]
3231
] = None,
3332
adapt_image_maker: Optional[ag.AdaptImageMaker] = None,
3433
cosmology: ag.cosmo.LensingCosmology = ag.cosmo.Planck15(),
@@ -52,7 +51,8 @@ def __init__(
5251
positions_likelihood_list
5352
Alters the likelihood function to include a term which accounts for whether image-pixel coordinates in
5453
arc-seconds corresponding to the multiple images of each lensed source galaxy trace close to one another in
55-
their source-plane.
54+
their source-plane. This is a list, as it may support multiple planes, where a positions likelihood object
55+
is input for each plane (e.g. double source plane lensing).
5656
adapt_images
5757
Contains the adapt-images which are used to make a pixelization's mesh and regularization adapt to the
5858
reconstructed galaxy's morphology.
@@ -62,7 +62,7 @@ def __init__(
6262
Settings controlling how an inversion is fitted during the model-fit, for example which linear algebra
6363
formalism is used.
6464
raise_inversion_positions_likelihood_exception
65-
If an inversion is used without the `positions_likelihood` it is likely a systematic solution will
65+
If an inversion is used without the `positions_likelihood_list` it is likely a systematic solution will
6666
be inferred, in which case an Exception is raised before the model-fit begins to inform the user
6767
of this. This exception is not raised if this input is False, allowing the user to perform the model-fit
6868
anyway.
@@ -124,7 +124,7 @@ def raise_exceptions(self, model):
124124

125125
if has_pix:
126126
if (
127-
self.positions_likelihood is None
127+
self.positions_likelihood_list is None
128128
and self.raise_inversion_positions_likelihood_exception
129129
and not conf.instance["general"]["test"][
130130
"disable_positions_lh_inversion_check"
@@ -133,7 +133,7 @@ def raise_exceptions(self, model):
133133
raise exc.AnalysisException(
134134
"""
135135
You have begun a model-fit which reconstructs the source using a pixelization.
136-
However, you have not input a `positions_likelihood` object.
136+
However, you have not input a `positions_likelihood_list` object.
137137
It is likely your model-fit will infer an inaccurate solution.
138138
139139
Please read the following readthedocs page for a description of why this is, and how to set up

autolens/analysis/analysis/lens.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
import autoarray as aa
77
import autogalaxy as ag
88

9-
from autolens.analysis.positions import PositionsLHResample
10-
from autolens.analysis.positions import PositionsLHPenalty
9+
from autolens.analysis.positions import PositionsLH
1110
from autolens.lens.tracer import Tracer
1211

1312
from autolens.lens import tracer_util
@@ -23,7 +22,7 @@ class AnalysisLens:
2322
def __init__(
2423
self,
2524
positions_likelihood_list: Optional[
26-
List[Union[PositionsLHResample, PositionsLHPenalty]]
25+
List[PositionsLH]
2726
] = None,
2827
cosmology: ag.cosmo.LensingCosmology = ag.cosmo.Planck15(),
2928
):

autolens/analysis/positions.py

Lines changed: 22 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
from autolens import exc
2222

2323

24-
class AbstractPositionsLH:
24+
class PositionsLH:
2525
def __init__(
2626
self,
27+
positions: aa.Grid2DIrregular,
2728
threshold: float,
28-
positions: Optional[aa.Grid2DIrregular] = None,
29+
log_likelihood_penalty_factor: float = 1e8,
2930
plane_redshift: Optional[float] = None,
3031
):
3132
"""
@@ -37,9 +38,17 @@ def __init__(
3738
on the basis that this indicates an unphysical or inaccurate mass model. If they trace within the
3839
threshold the penalty term is not applied.
3940
41+
For the `PositionsLH` object, if the multiple image coordinates do not trace within the source-plane
42+
threshold of one another a penalty to the likelihood is applied:
43+
44+
`log_Likelihood_penalty_base - log_likelihood_penalty_factor * (max_source_plane_separation - threshold)`
45+
46+
The penalty term reduces as the source-plane coordinates trace closer to one another, meaning that the
47+
initial stages of the non-linear search can sample mass models that reduce the threshold.
48+
4049
For example, for one penalty term, if the multiple image coordinates are defined
4150
via `positions=aa.Grid2DIrregular([(1.0, 0.0), (-1.0, 0.0)]` and they do not trace within `threshold=0.3` of
42-
one another, the mass model will be rejected and a new model sampled.
51+
one another, the mass model will receive a large likelihood penalty.
4352
4453
The default behaviour assumes a single lens plane and single source plane, meaning the input `positions`
4554
are the image-plane coordinates of one source galaxy at a specifc redshift.
@@ -55,11 +64,13 @@ def __init__(
5564
threshold
5665
If the maximum separation of any two source plane coordinates is above the threshold the penalty term
5766
is applied.
67+
log_likelihood_penalty_factor
68+
A factor which multiplies how far source pixels do not trace within the threshold of one another, with a
69+
larger factor producing a larger penalty making the non-linear parameter space gradient steeper.
5870
plane_redshift
5971
The plane redshift of the lensed source multiple images, which is only required if position threshold
6072
for a double source plane lens system is being used where the specific plane is required.
6173
"""
62-
6374
self.positions = positions
6475
self.threshold = threshold
6576
self.plane_redshift = plane_redshift
@@ -71,10 +82,7 @@ def __init__(
7182
"Please input more positions into the Positions."
7283
)
7384

74-
def log_likelihood_function_positions_overwrite(
75-
self, instance: af.ModelInstance, analysis: AnalysisDataset
76-
) -> Optional[float]:
77-
raise NotImplementedError
85+
self.log_likelihood_penalty_factor = log_likelihood_penalty_factor
7886

7987
def output_positions_info(self, output_path: str, tracer: Tracer):
8088
"""
@@ -94,7 +102,7 @@ def output_positions_info(self, output_path: str, tracer: Tracer):
94102
-------
95103
96104
"""
97-
with open_(path.join(output_path, "positions.info"), "w+") as f:
105+
with open_(path.join(output_path, "positions.info"), "a+") as f:
98106

99107
positions_fit = SourceMaxSeparation(
100108
data=self.positions, noise_map=None, tracer=tracer, plane_redshift=self.plane_redshift
@@ -104,7 +112,11 @@ def output_positions_info(self, output_path: str, tracer: Tracer):
104112
coordinate=(0.0, 0.0)
105113
)
106114

107-
f.write(f"Plane Redshift: {self.plane_redshift} \n")
115+
if self.plane_redshift is None:
116+
f.write(f"Plane Index: -1 \n")
117+
else:
118+
f.write(f"Plane Redshift: {self.plane_redshift} \n")
119+
108120
f.write(f"Positions: \n {self.positions} \n\n")
109121
f.write(f"Radial Distance from (0.0, 0.0): \n {distances} \n\n")
110122
f.write(f"Threshold = {self.threshold} \n")
@@ -113,116 +125,6 @@ def output_positions_info(self, output_path: str, tracer: Tracer):
113125
)
114126
f.write("")
115127

116-
117-
class PositionsLHResample(AbstractPositionsLH):
118-
"""
119-
The `PositionsLH` objects add a penalty term to the likelihood of the **PyAutoLens** `log_likelihood_function`
120-
defined in the `Analysis` classes.
121-
122-
The penalty term inspects the distance that the locations of the multiple images of the lensed source galaxy
123-
trace within one another in the source-plane and penalizes solutions where they trace far from one another,
124-
on the basis that this indicates an unphysical or inaccurate mass model. If they trace within the
125-
threshold the penalty term is not applied.
126-
127-
For the `PositionsLHResample` object, if the multiple image coordinates do not trace within the source-plane
128-
threshold of one another the mass model is rejected and a new model is sampled.
129-
130-
The penalty term rejects any model where the source-plane coordinates do not trace within the threshold, meaning
131-
that the initial stages of the non-linear search may need to sample many mass models randomly in order to sample
132-
an initial set that that trace within the threshold.
133-
134-
Parameters
135-
----------
136-
positions
137-
The arcsecond coordinates of the lensed source multiple images which are used to compute the likelihood
138-
penalty.
139-
threshold
140-
If the maximum separation of any two source plane coordinates is above the threshold the penalty term
141-
is applied.
142-
"""
143-
144-
def log_likelihood_function_positions_overwrite(
145-
self, instance: af.ModelInstance, analysis: AnalysisDataset
146-
) -> Optional[float]:
147-
"""
148-
This is called in the `log_likelihood_function` of certain `Analysis` classes to add the penalty term of
149-
this class, which rejects and resamples mass models which do not trace within the threshold of one another
150-
in the source-plane.
151-
152-
Parameters
153-
----------
154-
instance
155-
The instance of the lens model that is being fitted for this iteration of the non-linear search.
156-
analysis
157-
The analysis class from which the log likliehood function is called.
158-
"""
159-
tracer = analysis.tracer_via_instance_from(instance=instance)
160-
161-
if not tracer.has(cls=ag.mp.MassProfile) or len(tracer.planes) == 1:
162-
return
163-
164-
positions_fit = SourceMaxSeparation(
165-
data=self.positions,
166-
noise_map=None,
167-
tracer=tracer,
168-
plane_redshift=self.plane_redshift,
169-
)
170-
171-
if not positions_fit.max_separation_within_threshold(self.threshold):
172-
if os.environ.get("PYAUTOFIT_TEST_MODE") == "1":
173-
return
174-
175-
raise exc.RayTracingException
176-
177-
178-
class PositionsLHPenalty(AbstractPositionsLH):
179-
def __init__(
180-
self,
181-
threshold: float,
182-
log_likelihood_penalty_factor: float = 1e8,
183-
positions: Optional[aa.Grid2DIrregular] = None,
184-
plane_redshift: Optional[float] = None,
185-
):
186-
"""
187-
The `PositionsLH` objects add a penalty term to the likelihood of the **PyAutoLens** `log_likelihood_function`
188-
defined in the `Analysis` classes.
189-
190-
The penalty term inspects the distance that the locations of the multiple images of the lensed source galaxy
191-
trace within one another in the source-plane and penalizes solutions where they trace far from one another,
192-
on the basis that this indicates an unphysical or inaccurate mass model. If they trace within the
193-
threshold the penalty term is not applied.
194-
195-
For the `PositionsLHPenalty` object, if the multiple image coordinates do not trace within the source-plane
196-
threshold of one another a penalty to the likelihood is applied:
197-
198-
`log_Likelihood_penalty_base - log_likelihood_penalty_factor * (max_source_plane_separation - threshold)`
199-
200-
The penalty term reduces as the source-plane coordinates trace closer to one another, meaning that the
201-
initial stages of the non-linear search can sample mass models that reduce the threshold.
202-
203-
Parameters
204-
----------
205-
positions
206-
The arcsecond coordinates of the lensed source multiple images which are used to compute the likelihood
207-
penalty.
208-
threshold
209-
If the maximum separation of any two source plane coordinates is above the threshold the penalty term
210-
is applied.
211-
log_likelihood_penalty_factor
212-
A factor which multiplies how far source pixels do not trace within the threshold of one another, with a
213-
larger factor producing a larger penalty making the non-linear parameter space gradient steeper.
214-
plane_redshift
215-
The plane redshift of the lensed source multiple images, which is only required if position threshold
216-
for a double source plane lens system is being used where the specific plane is required.
217-
"""
218-
super().__init__(
219-
positions=positions,
220-
threshold=threshold,
221-
plane_redshift=plane_redshift
222-
)
223-
224-
self.log_likelihood_penalty_factor = log_likelihood_penalty_factor
225-
226128
def log_likelihood_penalty_base_from(
227129
self, dataset: Union[aa.Imaging, aa.Interferometer]
228130
) -> float:

0 commit comments

Comments
 (0)