diff --git a/autolens/lens/substructure_util.py b/autolens/lens/substructure_util.py index e27045c71..daff078bf 100644 --- a/autolens/lens/substructure_util.py +++ b/autolens/lens/substructure_util.py @@ -78,6 +78,24 @@ def extract(prof): raise ValueError(f"Unsupported halo profile class: {profile_cls}") +def _padding_params_from(profile_cls, n_params): + """ + The parameter row put into an empty halo slot: a zero centre followed by the profile's + ``vmapped_padding_params`` (ones when it has none). + + The mask zeroes an empty slot's deflection, but zero padding would put a zero scale + radius into the profile, whose infinite partial derivatives turn every ``jax.grad`` + through the plane into NaN (zero cotangent times infinity). A row inside the profile's + domain keeps the gradients finite. ``vmapped_deflections_from`` swaps in the same row + itself; padding here as well keeps the gradients finite with an autogalaxy whose hook + predates that swap. + """ + radial = getattr(profile_cls, "vmapped_padding_params", None) + if radial is None: + radial = np.ones(n_params - 2) + return np.concatenate([np.zeros(2), np.asarray(radial, dtype=float)]) + + def precompute_scaling_matrix(plane_redshifts, cosmology=None): import jax.numpy as jnp @@ -168,7 +186,8 @@ def galaxies_to_halo_arrays(galaxies, plane_redshifts, max_n, profile_cls): Returns ------- The halo parameters ``(n_planes, max_n, n_params)``, the slot mask ``(n_planes, max_n)`` - and the sheet convergence of each plane ``(n_planes,)``. + and the sheet convergence of each plane ``(n_planes,)``. Slots the mask leaves empty hold + the profile's benign padding parameters (see ``_padding_params_from``), not zeros. """ import jax.numpy as jnp @@ -183,7 +202,10 @@ def galaxies_to_halo_arrays(galaxies, plane_redshifts, max_n, profile_cls): else: _check_max_n(counts=counts, plane_redshifts=plane_redshifts, max_n=max_n) - params = np.zeros((n_planes, max_n, n_params)) + params = np.tile( + _padding_params_from(profile_cls=profile_cls, n_params=n_params), + (n_planes, max_n, 1), + ) mask = np.zeros((n_planes, max_n), dtype=bool) sheet_kappas = np.zeros(n_planes) diff --git a/test_autolens/lens/test_substructure_util.py b/test_autolens/lens/test_substructure_util.py index 3cfcc38ec..ab86278b8 100644 --- a/test_autolens/lens/test_substructure_util.py +++ b/test_autolens/lens/test_substructure_util.py @@ -237,3 +237,116 @@ def test__los_realizations_to_arrays__overflow_names_the_realisation(): max_n=3, profile_cls=al.mp.NFWTruncatedSph, ) + + +def _multi_plane_summed_deflections(grid, plane_params, plane_masks, scaling_matrix): + """ + Trace ``grid`` through the halo planes one at a time and return the sum of every plane's + deflections, each plane taking its own (possibly unpadded) parameter array. + """ + import jax.numpy as jnp + + deflections = [] + total = 0.0 + for i, (params, mask) in enumerate(zip(plane_params, plane_masks)): + traced = grid + for j in range(i): + traced = traced - scaling_matrix[i, j] * deflections[j] + plane_deflections = al.mp.NFWTruncatedSph.vmapped_deflections_from( + traced, params, mask + ) + deflections.append(plane_deflections) + total = total + jnp.sum(plane_deflections) + return total + + +def _nfw_truncated_halos_scattered(redshift, total, seed): + rng = np.random.default_rng(seed) + return [ + al.Galaxy( + redshift=redshift, + mass=al.mp.NFWTruncatedSph( + centre=tuple(rng.uniform(-1.0, 1.0, size=2)), + kappa_s=float(rng.uniform(0.01, 0.05)), + scale_radius=float(rng.uniform(0.5, 1.5)), + truncation_radius=float(rng.uniform(2.0, 4.0)), + ), + ) + for _ in range(total) + ] + + +@requires_jax +def test__galaxies_to_halo_arrays__padded_slots_keep_multi_plane_gradients_finite(): + import jax + import jax.numpy as jnp + + plane_redshifts = [0.3, 0.6] + counts = [2, 5] + galaxies = _nfw_truncated_halos_scattered( + redshift=0.3, total=2, seed=1 + ) + _nfw_truncated_halos_scattered(redshift=0.6, total=5, seed=2) + + with jax.enable_x64(True): + grid = jnp.array(np.random.default_rng(3).uniform(-2.0, 2.0, size=(30, 2))) + scaling_matrix = substructure_util.precompute_scaling_matrix(plane_redshifts) + + padded_params, padded_mask, _ = substructure_util.galaxies_to_halo_arrays( + galaxies=galaxies, + plane_redshifts=plane_redshifts, + max_n=8, + profile_cls=al.mp.NFWTruncatedSph, + ) + assert not bool(padded_mask.all()) + + unpadded = [ + substructure_util.galaxies_to_halo_arrays( + galaxies=[g for g in galaxies if g.redshift == z], + plane_redshifts=[z], + max_n=count, + profile_cls=al.mp.NFWTruncatedSph, + ) + for z, count in zip(plane_redshifts, counts) + ] + unpadded_params = [params[0] for params, _, _ in unpadded] + unpadded_masks = [mask[0] for _, mask, _ in unpadded] + assert all(bool(mask.all()) for mask in unpadded_masks) + + def padded(grid, params): + return _multi_plane_summed_deflections( + grid, list(params), list(padded_mask), scaling_matrix + ) + + def no_padding(grid, params): + return _multi_plane_summed_deflections( + grid, params, unpadded_masks, scaling_matrix + ) + + d_grid, d_params = jax.grad(padded, argnums=(0, 1))(grid, padded_params) + d_grid_ref, d_params_ref = jax.grad(no_padding, argnums=(0, 1))( + grid, unpadded_params + ) + + assert np.isfinite(np.asarray(d_grid)).all() + assert np.isfinite(np.asarray(d_params)).all() + np.testing.assert_allclose(d_grid, d_grid_ref, rtol=0.0, atol=1e-10) + for plane, count in enumerate(counts): + np.testing.assert_allclose( + d_params[plane, :count], d_params_ref[plane], rtol=0.0, atol=1e-10 + ) + assert np.all(np.asarray(d_params[plane, count:]) == 0.0) + + +@requires_jax +def test__galaxies_to_halo_arrays__empty_slots_hold_benign_padding(): + params, mask, _ = substructure_util.galaxies_to_halo_arrays( + galaxies=_nfw_truncated_halos_on_plane(redshift=0.7, total=1), + plane_redshifts=[0.7], + max_n=3, + profile_cls=al.mp.NFWTruncatedSph, + ) + + assert mask.tolist() == [[True, False, False]] + np.testing.assert_allclose(np.asarray(params[0, 0]), [0.0, 0.0, 0.01, 1.0, 2.0]) + radial_padding = np.asarray(params[0, 1:, 2:]) + assert np.all(radial_padding[:, 1] > 0.0)