From 001c61b3bd36c95390ce655d0ce5cd805c3a9bc8 Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Thu, 26 Nov 2026 14:02:14 +0000 Subject: [PATCH] Export galaxy_snr_maps_from and soft_snr_maps_from from autolens al.galaxy_snr_maps_from and al.soft_snr_maps_from (PyAutoGalaxy#763), next to galaxy_name_image_dict_via_result_from, plus a test fitting a simulated lens with a HilbertSNR mesh, soft S/N maps and AdaptSplit, with full and short ("source") galaxy keys giving the same likelihood. Co-Authored-By: Claude Opus 5.5 --- autolens/__init__.py | 2 + test_autolens/analysis/test_adapt_snr_maps.py | 77 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 test_autolens/analysis/test_adapt_snr_maps.py diff --git a/autolens/__init__.py b/autolens/__init__.py index 1978fb27b..c917473c3 100644 --- a/autolens/__init__.py +++ b/autolens/__init__.py @@ -55,6 +55,8 @@ from autogalaxy.analysis.adapt_images.adapt_images import AdaptImages from autogalaxy.analysis.adapt_images.adapt_images import ( galaxy_name_image_dict_via_result_from, + galaxy_snr_maps_from, + soft_snr_maps_from, ) from autogalaxy.gui.clicker import Clicker from autogalaxy.gui.scribbler import Scribbler diff --git a/test_autolens/analysis/test_adapt_snr_maps.py b/test_autolens/analysis/test_adapt_snr_maps.py new file mode 100644 index 000000000..0086d9f4b --- /dev/null +++ b/test_autolens/analysis/test_adapt_snr_maps.py @@ -0,0 +1,77 @@ +import numpy as np +import pytest + +import autofit as af +import autolens as al + + +def test__exports(): + assert al.galaxy_snr_maps_from is not None + assert al.soft_snr_maps_from is not None + assert hasattr(al.AdaptImages, "from_result") + assert al.image_mesh.HilbertSNR is not None + + +def _simulated_lens(): + grid = al.Grid2D.uniform(shape_native=(31, 31), pixel_scales=0.1, over_sample_size=1) + psf = al.Convolver.from_gaussian(shape_native=(3, 3), pixel_scales=0.1, sigma=0.1, normalize=True) + + lens = al.Galaxy(redshift=0.5, mass=al.mp.Isothermal(centre=(0.0, 0.0), einstein_radius=0.8)) + source = al.Galaxy(redshift=1.0, bulge=al.lp.Sersic(intensity=0.5, effective_radius=0.2, sersic_index=1.0)) + tracer = al.Tracer(galaxies=[lens, source]) + + simulator = al.SimulatorImaging( + exposure_time=300.0, psf=psf, add_poisson_noise_to_data=False, noise_seed=1 + ) + dataset = simulator.via_tracer_from(tracer=tracer, grid=grid) + dataset = al.Imaging( + data=dataset.data, + psf=dataset.psf, + noise_map=al.Array2D.full(fill_value=0.05, shape_native=(31, 31), pixel_scales=0.1), + over_sample_size_lp=1, + over_sample_size_pixelization=1, + ) + mask = al.Mask2D.circular(shape_native=(31, 31), pixel_scales=0.1, radius=1.3) + return dataset.apply_mask(mask=mask) + + +def test__hilbert_snr_soft_maps_and_short_keys__fit_with_adapt_split(): + dataset = _simulated_lens() + mask = dataset.mask + + # The lens has no light, so the source S/N map is the data over the noise map. + snr = al.Array2D(values=np.asarray(dataset.data.array) / 0.05, mask=mask) + full_key = "('galaxies', 'source')" + snr_maps = {full_key: snr} + + image_mesh = al.image_mesh.HilbertSNR(pixels=60, edge_pixels=12) + mesh_grid = image_mesh.image_plane_mesh_grid_from(mask=mask, adapt_data=snr) + soft_maps = al.soft_snr_maps_from(snr_maps) + + assert np.asarray(mesh_grid).shape == (72, 2) + + galaxies = af.ModelInstance() + galaxies.lens = al.Galaxy(redshift=0.5, mass=al.mp.Isothermal(centre=(0.0, 0.0), einstein_radius=0.8)) + galaxies.source = al.Galaxy( + redshift=1.0, + pixelization=al.Pixelization( + mesh=al.mesh.Delaunay(pixels=image_mesh.pixels, zeroed_pixels=image_mesh.edge_pixels), + regularization=al.reg.AdaptSplit( + inner_coefficient=0.01, outer_coefficient=1.0, signal_scale=1.0 + ), + ), + ) + instance = af.ModelInstance() + instance.galaxies = galaxies + + log_likelihoods = [] + for key in (full_key, "source"): + adapt_images = al.AdaptImages( + galaxy_name_image_dict={key: soft_maps[full_key]}, + galaxy_name_image_plane_mesh_grid_dict={key: mesh_grid}, + ) + analysis = al.AnalysisImaging(dataset=dataset, adapt_images=adapt_images, use_jax=False) + log_likelihoods.append(analysis.log_likelihood_function(instance=instance)) + + assert np.isfinite(log_likelihoods[0]) + assert log_likelihoods[1] == pytest.approx(log_likelihoods[0], 1.0e-10)