Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 108 additions & 3 deletions autogalaxy/analysis/model_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def mge_model_from(
ell_comps_sigma : float = 0.3,
use_spherical: bool = False,
sigma_min: float = 1e-4,
ell_comps_limit: float = 1.0,
order_bases: bool = False,
) -> af.Collection:
"""
Construct a Multi-Gaussian Expansion (MGE) for the lens or source galaxy light.
Expand Down Expand Up @@ -87,6 +89,76 @@ def mge_model_from(
of the log-spaced sigma values. Defaults to ``1e-4``. Increase it (e.g. to a
tenth of the pixel scale) to stop the basis wasting components on scales the
data cannot resolve.
ell_comps_limit
Half-width of the box the truncated-Gaussian ell_comps priors are truncated to,
giving ``lower_limit=-ell_comps_limit`` and ``upper_limit=+ell_comps_limit``.
Must satisfy ``0.0 < ell_comps_limit <= 1.0``; the default ``1.0`` is the full
physical range. Uniform ell_comps priors are unaffected -- their width is set by
``ell_comps_uniform_width``.

Callers tighten this box when the science case bounds the isophotal ellipticity:
the Euclid strong-lens pipeline uses ``0.5`` for the lens light and ``0.7`` for
the source. Setting the box *here* rather than overwriting the priors on the
returned model keeps the prior objects the ones this function built, which
matters because ``order_bases`` attaches an assertion that references them.
order_bases
If True, require the bases' shared ``ell_comps_1`` values to be strictly
decreasing, ``basis_0 > basis_1 > ... > basis_{K-1}``, via ``K - 1`` assertions
added to the returned model. Defaults to False (off until validated on
production runs). A no-op when ``gaussian_per_basis == 1``; a ``ValueError``
when ``use_spherical=True``, which has no ``ell_comps`` to order.

**The symmetry.** With ``gaussian_per_basis=K > 1`` every basis holds the same
``total_gaussians`` linear Gaussians on the same fixed ``log10_sigma_list``, and
differs from its siblings only in the ellipticity pair it carries. The bases are
therefore *exchangeable*: permuting which basis holds which ``ell_comps`` leaves
the likelihood exactly unchanged, so the posterior has ``K!`` identical modes and
an unseeded search lands in whichever one it reaches first. Repeat fits of the
same data then report the same physical solution under swapped labels, which
breaks any downstream comparison that reads ``basis_0`` as a fixed component.
Ordering the bases picks one labelling and deletes the other ``K! - 1`` copies;
it removes no physical solution.

**Why ``ell_comps_1`` and not the magnitude.** The key must separate the modes
the search actually finds. At the maximum-likelihood point either key admits
exactly one permutation, so the choice is not about which key can order a single
point -- it is about the posterior spread. The two modes are separated in the key
by ``|key(e_A) - key(e_B)|``, and when that separation is smaller than the marginal
posterior width in the key the constraint surface passes through both modes: the
retained region mixes the two labellings and the labels are still undetermined. On
the Euclid phase-4 tiles the two bases commonly sit against opposite edges of the
ell_comps box, e.g. ``(0.007, -0.500)`` and ``(-0.023, 0.497)``. For that pair the
magnitude separation is ``0.0025`` while the ``ell_comps_1`` separation is ``1.0``,
so the ``cos 2phi`` component separates the modes by far more than any plausible
marginal width and the magnitude does not.

**Blind band.** No continuous key is exact for every configuration -- another tile
has the two bases only ``0.01`` apart in ``ell_comps_1``, a separation small
compared with a typical marginal posterior width, so there too the constraint
surface passes through both modes and ordering by ``ell_comps_1`` does not resolve
that tile. The diagnostic is a small ``|delta ell_comps_1|`` relative to the
posterior width: read such a result as undetermined labelling rather than as an
ordered answer.

**Consequences of an assertion being part of the model.** It enters the PyAutoFit
identifier, so turning ``order_bases`` on gives an otherwise identical fit a new
``unique_id`` and a fresh output directory -- with PyAutoFit at or after the
identifier fix that ships alongside this option (PyAutoFit#1581 follow-up); on
older PyAutoFit the ordered and unordered models share an identifier and an
ordered fit would load a completed unordered result from the same directory. It
also makes the model an invalid *target* for ``take_attributes``: PyAutoFit's
``assert_no_assertions`` refuses to copy attributes into a model that already
carries assertions, so build the ordered model after any such prior-passing step.
Prior passing in the other direction drops the ordering altogether: ``Result.model``
is built by ``gaussian_prior_model_for_arguments``, which clears ``_assertions``
(``PyAutoFit/autofit/mapper/prior_model/prior_model.py``, line 601), so a model
built from a result is unordered again -- recompose the ``Basis`` with
``order_bases=True`` when chaining fits rather than reusing the result's model.
Enforcement is backend-specific but has the same outcome -- NumPy raises
``af.exc.FitException`` from ``check_assertions`` and the search resamples; JAX
cannot raise inside a trace and instead evaluates the assertions as a traced
boolean and maps a violating model to the resample figure of merit (PyAutoFit
#1583).

Returns
-------
Expand All @@ -98,11 +170,25 @@ def mge_model_from(

if os.environ.get("PYAUTO_SMALL_DATASETS") == "1":
total_gaussians = 2
# A single basis leaves nothing to order, so `order_bases` degrades to a no-op
# under the small-dataset shortcut rather than changing what it asserts.
gaussian_per_basis = 1

from autogalaxy.profiles.light.linear import Gaussian, GaussianSph
from autogalaxy.profiles.basis import Basis

if not 0.0 < ell_comps_limit <= 1.0:
raise ValueError(
f"mge_model_from requires 0.0 < ell_comps_limit <= 1.0, got "
f"{ell_comps_limit}."
)

if order_bases and use_spherical:
raise ValueError(
"mge_model_from cannot order bases when use_spherical=True, because "
"spherical Gaussians have no ell_comps to order."
)

if sigma_min <= 0.0:
raise ValueError(
f"mge_model_from requires sigma_min > 0.0, got {sigma_min}."
Expand Down Expand Up @@ -151,8 +237,8 @@ def _make_ell_comps_priors():
)
else:
return (
af.TruncatedGaussianPrior(mean=0.0, sigma=ell_comps_sigma, lower_limit=-1.0, upper_limit=1.0),
af.TruncatedGaussianPrior(mean=0.0, sigma=ell_comps_sigma, lower_limit=-1.0, upper_limit=1.0),
af.TruncatedGaussianPrior(mean=0.0, sigma=ell_comps_sigma, lower_limit=-ell_comps_limit, upper_limit=ell_comps_limit),
af.TruncatedGaussianPrior(mean=0.0, sigma=ell_comps_sigma, lower_limit=-ell_comps_limit, upper_limit=ell_comps_limit),
)

# Shared centre priors (used when centre_per_basis=False).
Expand All @@ -161,6 +247,10 @@ def _make_ell_comps_priors():

bulge_gaussian_list = []

# The shared `ell_comps_1` prior of each basis, in basis order, used as the ordering
# key when `order_bases` is True.
ell_comps_1_list = []

for j in range(gaussian_per_basis):

# Per-basis centre priors when requested.
Expand All @@ -172,6 +262,7 @@ def _make_ell_comps_priors():
# Per-basis ell_comps priors (always independent across bases).
if not use_spherical:
ell_comps_0, ell_comps_1 = _make_ell_comps_priors()
ell_comps_1_list.append(ell_comps_1)

gaussian_list = af.Collection(
af.Model(model_cls) for _ in range(total_gaussians)
Expand All @@ -189,11 +280,25 @@ def _make_ell_comps_priors():

# The Basis object groups many light profiles together into a single model component.

return af.Model(
model = af.Model(
Basis,
profile_list=bulge_gaussian_list,
)

# Break the label symmetry between exchangeable bases by requiring their shared
# `ell_comps_1` values to be strictly decreasing. Attaching the assertions to the
# returned `Basis` model (rather than to a parent) means they travel with the
# component, and PyAutoFit's `gathered_assertions` finds them wherever the component
# is placed in a larger model.
if order_bases:
for j in range(len(ell_comps_1_list) - 1):
model.add_assertion(
ell_comps_1_list[j] > ell_comps_1_list[j + 1],
name=f"mge_basis_{j}_ell_comps_1_gt_basis_{j + 1}",
)

return model


def mge_point_model_from(
pixel_scales: float,
Expand Down
Loading
Loading