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
4 changes: 3 additions & 1 deletion autogalaxy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
mapper_from as Mapper,
) # noqa
from autoarray.inversion.pixelization.border_relocator import BorderRelocator
from autoarray.preloads import Preloads
from autoarray.preloads import mapper_indices_from
from autoarray.mask.mask_1d import Mask1D # noqa
from autoarray.mask.mask_2d import Mask2D # noqa
from autoarray.mask.derive.zoom_2d import Zoom2D
Expand All @@ -51,6 +53,7 @@
from autoarray.structures.visibilities import Visibilities # noqa
from autoarray.structures.visibilities import VisibilitiesNoiseMap # noqa

from .analysis import model_util
from .analysis.adapt_images.adapt_images import AdaptImages
from .analysis.adapt_images.adapt_image_maker import AdaptImageMaker
from . import aggregator as agg
Expand Down Expand Up @@ -80,7 +83,6 @@
from .galaxy.galaxy import Galaxy
from .galaxy.galaxies import Galaxies
from .galaxy.redshift import Redshift
from .galaxy.stellar_dark_decomp import StellarDarkDecomp
from .galaxy.to_inversion import AbstractToInversion
from .galaxy.to_inversion import GalaxiesToInversion
from .profiles.geometry_profiles import EllProfile
Expand Down
3 changes: 1 addition & 2 deletions autogalaxy/analysis/adapt_images/adapt_images.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from __future__ import annotations
import jax.numpy as jnp
import numpy as np
from typing import TYPE_CHECKING, Dict, Optional, Tuple

Expand Down Expand Up @@ -132,7 +131,7 @@ def from_result(cls, result, use_model_images: bool = False) -> "AdaptImages":
else:
galaxy_image = result.subtracted_signal_to_noise_map_galaxy_dict[path]

minimum_galaxy_value = adapt_minimum_percent * jnp.max(galaxy_image.array)
minimum_galaxy_value = adapt_minimum_percent * xp.max(galaxy_image.array)
galaxy_image[galaxy_image < minimum_galaxy_value] = minimum_galaxy_value

galaxy_name_image_dict[path] = galaxy_image
Expand Down
129 changes: 129 additions & 0 deletions autogalaxy/analysis/model_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import numpy as np
from typing import Optional, Tuple

import autofit as af


def mge_model_from(
mask_radius: float,
total_gaussians: int = 30,
gaussian_per_basis: int = 1,
centre_prior_is_uniform: bool = True,
centre: Tuple[float, float] = (0.0, 0.0),
centre_fixed: Optional[Tuple[float, float]] = None,
use_spherical: bool = False,
) -> af.Collection:
"""
Construct a Multi-Gaussian Expansion (MGE) for the lens or source galaxy light

This model is designed as a "start here" configuration for lens modeling:

- The lens and source light are represented by a Basis object composed of many
Gaussian light profiles with fixed logarithmically spaced widths (`sigma`).
- All Gaussians within each basis share common centres and ellipticity
components, reducing degeneracy while retaining flexibility.

- Users can combine with a lens mass model of their choiuce.

The resulting model provides a good balance of speed, flexibility, and accuracy
for fitting most galaxy-scale strong lenses.

This code is mostly to make the API simple for new users, hiding the technical
details of setting up an MGE. More advanced users may wish to customize the
model further.

Parameters
----------
mask_radius
The outer radius (in arcseconds) of the circular mask applied to the data.
This determines the maximum Gaussian width (`sigma`) used in the lens MGE.
lens_total_gaussians
Total number of Gaussian light profiles used in the lens MGE basis.
source_total_gaussians
Total number of Gaussian light profiles used in the source MGE basis.
lens_gaussian_per_basis
Number of separate Gaussian bases to include for the lens light profile.
Each basis has `lens_total_gaussians` components.
source_gaussian_per_basis
Number of separate Gaussian bases to include for the source light profile.
Each basis has `source_total_gaussians` components.

Returns
-------
model : af.Collection
An `autofit.Collection` containing:
- A lens galaxy at redshift 0.5, with:
* bulge light profile: MGE basis of Gaussians
* mass profile: Isothermal ellipsoid
* external shear
- A source galaxy at redshift 1.0, with:
* bulge light profile: MGE basis of Gaussians

Notes
-----
- Lens light Gaussians have widths (sigma) logarithmically spaced between 0.01"
and the mask radius.
- Source light Gaussians have widths logarithmically spaced between 0.01" and 1.0".
- Gaussian centres are free parameters but tied across all components in each
basis to reduce dimensionality.
- This function is a convenience utility: it hides the technical setup of MGE
composition and provides a ready-to-use lens model for quick experimentation.
"""

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

# The sigma values of the Gaussians will be fixed to values spanning 0.01 to the mask radius, 3.0".
log10_sigma_list = np.linspace(-4, np.log10(mask_radius), total_gaussians)

# By defining the centre here, it creates two free parameters that are assigned below to all Gaussians.

if centre_fixed is not None:
centre_0 = centre[0]
centre_1 = centre[1]
elif centre_prior_is_uniform:
centre_0 = af.UniformPrior(
lower_limit=centre[0] - 0.1, upper_limit=centre[0] + 0.1
)
centre_1 = af.UniformPrior(
lower_limit=centre[1] - 0.1, upper_limit=centre[1] + 0.1
)
else:
centre_0 = af.GaussianPrior(mean=centre[0], sigma=0.3)
centre_1 = af.GaussianPrior(mean=centre[1], sigma=0.3)

if use_spherical:
model_cls = GaussianSph
else:
model_cls = Gaussian

bulge_gaussian_list = []

for j in range(gaussian_per_basis):
# A list of Gaussian model components whose parameters are customized belows.

gaussian_list = af.Collection(
af.Model(model_cls) for _ in range(total_gaussians)
)

# Iterate over every Gaussian and customize its parameters.

for i, gaussian in enumerate(gaussian_list):
gaussian.centre.centre_0 = centre_0 # All Gaussians have same y centre.
gaussian.centre.centre_1 = centre_1 # All Gaussians have same x centre.
if not use_spherical:
gaussian.ell_comps = gaussian_list[
0
].ell_comps # All Gaussians have same elliptical components.
gaussian.sigma = (
10 ** log10_sigma_list[i]
) # All Gaussian sigmas are fixed to values above.

bulge_gaussian_list += gaussian_list

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

return af.Model(
Basis,
profile_list=bulge_gaussian_list,
)
12 changes: 0 additions & 12 deletions autogalaxy/analysis/plotter_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,6 @@ def should_plot(name):
mat_plot_1d=mat_plot_1d,
)

try:
if should_plot("subplot_galaxies_1d"):
galaxies_plotter.subplot_galaxies_1d()
except OverflowError:
pass

try:
if should_plot("subplot_galaxies_1d_decomposed"):
galaxies_plotter.subplot_galaxies_1d_decomposed()
except OverflowError:
pass

if should_plot("fits_galaxy_images"):

image_list = [
Expand Down
2 changes: 0 additions & 2 deletions autogalaxy/config/visualize/plots.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ fit_imaging: {} # Settings for plots of fits to imagi
galaxies: # Settings for plots of galaxies (e.g. GalaxiesPlotter).
subplot_galaxies: true # Plot subplot of all quantities in each galaxies group (e.g. images, convergence)?
subplot_galaxy_images: false # Plot subplot of the image of each galaxy in the model?
subplot_galaxies_1d: false # Plot subplot of all quantities in 1D of each galaxies group (e.g. images, convergence)?
subplot_galaxies_1d_decomposed: false # Plot subplot of all quantities in 1D decomposed of each galaxies group (e.g. images, convergence)?
fits_galaxy_images: false # Output a .fits file containing images of every galaxy?

inversion: # Settings for plots of inversions (e.g. InversionPlotter).
Expand Down
Loading
Loading