PyAutoFit
PyAutoFit copied to clipboard
as_model() broken at some point
trafficstars
In PyAutoGalaxy, the following unit test has broken:
def test__stochastic_model_from():
model = af.Collection(
galaxies=af.Collection(
lens=af.Model(
ag.Galaxy,
redshift=0.5,
light=ag.lp.SphSersic(),
mass=ag.mp.SphIsothermal(),
),
source=af.Model(
ag.Galaxy,
redshift=1.0,
pixelization=ag.pix.VoronoiBrightnessImage(),
regularization=ag.reg.AdaptiveBrightness(),
),
)
)
instance = model.instance_from_prior_medians()
model = af.Collection(
galaxies=af.Collection(lens=af.Model(ag.Galaxy, redshift=0.5))
)
result = mock.MockResult(instance=instance, model=model)
model = ag.util.model.stochastic_model_from(
result=result, include_pixelization=True
)
assert isinstance(model.galaxies.lens.mass.centre, af.TuplePrior)
assert isinstance(model.galaxies.lens.light.intensity, float)
assert isinstance(model.galaxies.source.pixelization.pixels, af.UniformPrior)
assert not isinstance(
model.galaxies.source.regularization.inner_coefficient, af.UniformPrior
)
The issue is that the following line now longer receives a UniformPrior (it is an int instead):
assert isinstance(model.galaxies.source.pixelization.pixels, af.UniformPrior)
The code stochastic_model_from() is as follows (I've reduced it for clairty):
def stochastic_model_from(
result,
include_pixelization=False,
):
"""
Make a stochastic model from the `Result` of a model-fit, where the stochastic model uses the same model
components as the original model but may switch certain components (e.g. the lens light, source pixelization)
to free parameters.
The stochastic model is used to perform a stochastic model-fit, which refits a model but introduces a log
likelihood cap whereby all model-samples with a likelihood above this cap are rounded down to the value of the cap.
This `log_likelihood_cap` is determined by sampling ~250 log likeilhood values from the original model's, but where
each model evaluation uses a different KMeans seed of the pixelization to derive a unique pixelization with which
to reconstruct the source galaxy (therefore a pixelization which uses the KMeans method, like the
`VoronoiBrightnessImage` must be used to perform a stochastic fit).
The cap is computed as the mean of these ~250 values and it is introduced o avoid underestimated errors due
to artificial likelihood boosts.
Parameters
----------
result : af.Result
The result of a previous `Analysis` search whose maximum log likelihood model forms the basis of the hyper model.
include_pixelization : bool
If `True` the `VoronoiBrightnessImage` pixelization in the model is fitted for.
Returns
-------
af.Collection
The stochastic model, which is the same model as the input model but may fit for or fix additional parameters.
"""
if not hasattr(result.model.galaxies, "lens"):
raise exc.PriorException(
"Cannot extend a search with a stochastic search if the lens galaxy `Model` "
"is not named `lens`. "
)
model_classes = [MassProfile]
if include_pixelization:
model_classes.append(AbstractPixelization)
model = result.instance.as_model(model_classes)
I guess a change in the model mapper has caused this?
This test passes for me