|
| 1 | +from functools import cached_property |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +import autoarray as aa |
| 6 | + |
| 7 | +from autogalaxy.profiles.mass.abstract.abstract import MassProfile |
| 8 | +from autogalaxy.profiles.mass.input.input_potential import InputPotential |
| 9 | + |
| 10 | + |
| 11 | +def gaussian_random_field_from( |
| 12 | + shape_native, pixel_scale: float, power_amplitude: float, power_slope: float, seed: int |
| 13 | +) -> np.ndarray: |
| 14 | + """ |
| 15 | + A real Gaussian random field realization with isotropic power-law power |
| 16 | + spectrum P(k) = power_amplitude * k^(-power_slope). |
| 17 | +
|
| 18 | + The field is generated by filtering white Gaussian noise in Fourier space |
| 19 | + (multiplying its transform by sqrt(P(k)) and inverse transforming), which |
| 20 | + guarantees a real field whose power spectrum follows P(k); the k = 0 mode |
| 21 | + is zeroed so the field has zero mean. Note this normalization convention |
| 22 | + differs from the ``powerbox`` package used by the original |
| 23 | + implementation — the spectrum's shape and seed-reproducibility are what |
| 24 | + potential-correction validation relies on. |
| 25 | +
|
| 26 | + Parameters |
| 27 | + ---------- |
| 28 | + shape_native |
| 29 | + The 2D shape of the realization. |
| 30 | + pixel_scale |
| 31 | + The pixel size, setting the physical frequencies k. |
| 32 | + power_amplitude |
| 33 | + The amplitude of the power spectrum. |
| 34 | + power_slope |
| 35 | + The (positive) slope of the power-law spectrum, P(k) ~ k^-slope. |
| 36 | + seed |
| 37 | + The random seed, making the realization reproducible. |
| 38 | + """ |
| 39 | + rng = np.random.default_rng(seed) |
| 40 | + white_noise = rng.normal(size=shape_native) |
| 41 | + |
| 42 | + ky = np.fft.fftfreq(shape_native[0], d=pixel_scale) * 2.0 * np.pi |
| 43 | + kx = np.fft.fftfreq(shape_native[1], d=pixel_scale) * 2.0 * np.pi |
| 44 | + k_grid = np.sqrt(ky[:, None] ** 2 + kx[None, :] ** 2) |
| 45 | + |
| 46 | + power = np.zeros_like(k_grid) |
| 47 | + nonzero = k_grid > 0 |
| 48 | + power[nonzero] = power_amplitude * k_grid[nonzero] ** (-power_slope) |
| 49 | + |
| 50 | + field_ft = np.fft.fft2(white_noise) * np.sqrt(power) |
| 51 | + return np.fft.ifft2(field_ft).real |
| 52 | + |
| 53 | + |
| 54 | +class GaussianRandomField(MassProfile): |
| 55 | + def __init__( |
| 56 | + self, |
| 57 | + mask: aa.Mask2D, |
| 58 | + power_amplitude: float = 1.0, |
| 59 | + power_slope: float = 1.0, |
| 60 | + seed: int = 1, |
| 61 | + ): |
| 62 | + """ |
| 63 | + A mass profile whose lensing potential is a Gaussian random field |
| 64 | + realization with power spectrum P(k) = power_amplitude * k^(-power_slope), |
| 65 | + used to simulate extended perturbations of a smooth lens-mass model |
| 66 | + (e.g. for validating potential-correction reconstructions). |
| 67 | +
|
| 68 | + The realization is evaluated on the unmasked pixels of the input mask |
| 69 | + and wrapped in an ``InputPotential``, from which the deflection angles |
| 70 | + and convergence are derived via the mask's sparse derivative |
| 71 | + operators. |
| 72 | +
|
| 73 | + Ported from the ``potential_correction`` package of Cao et al. 2025 |
| 74 | + (https://github.com/caoxiaoyue/lensing_potential_correction). If you |
| 75 | + use this profile in your research, please cite Cao et al. 2025; |
| 76 | + citation materials are provided at |
| 77 | + https://github.com/caoxiaoyue/potential_correction_paper. The |
| 78 | + realization here uses a plain-numpy Fourier filter rather than the |
| 79 | + original's ``powerbox`` dependency (see |
| 80 | + ``gaussian_random_field_from``). |
| 81 | +
|
| 82 | + Parameters |
| 83 | + ---------- |
| 84 | + mask |
| 85 | + The cleaned 2D mask (an ``aa.Mask2D`` carrying the pixel scale) |
| 86 | + on whose unmasked pixels the potential is defined (see |
| 87 | + ``aa.util.derivative.cleaned_mask_from``). |
| 88 | + power_amplitude |
| 89 | + The amplitude of the potential's power spectrum. |
| 90 | + power_slope |
| 91 | + The (positive) slope of the power-law spectrum, P(k) ~ k^-slope. |
| 92 | + seed |
| 93 | + The random seed, making the realization reproducible. |
| 94 | + """ |
| 95 | + self.mask = mask |
| 96 | + self.power_amplitude = power_amplitude |
| 97 | + self.power_slope = power_slope |
| 98 | + self.seed = seed |
| 99 | + super().__init__() |
| 100 | + |
| 101 | + @cached_property |
| 102 | + def lensing_potential_native(self) -> np.ndarray: |
| 103 | + return gaussian_random_field_from( |
| 104 | + shape_native=self.mask.shape_native, |
| 105 | + pixel_scale=self.mask.pixel_scale, |
| 106 | + power_amplitude=self.power_amplitude, |
| 107 | + power_slope=self.power_slope, |
| 108 | + seed=self.seed, |
| 109 | + ) |
| 110 | + |
| 111 | + @cached_property |
| 112 | + def input_potential(self) -> InputPotential: |
| 113 | + grid = aa.Grid2D.from_mask(mask=self.mask) |
| 114 | + return InputPotential( |
| 115 | + lensing_potential=self.lensing_potential_native[~np.asarray(self.mask)], |
| 116 | + image_plane_grid=np.asarray(grid), |
| 117 | + mask=self.mask, |
| 118 | + ) |
| 119 | + |
| 120 | + def convergence_2d_from(self, grid: aa.type.Grid2DLike, xp=np, **kwargs): |
| 121 | + return self.input_potential.convergence_2d_from(grid=grid, xp=xp, **kwargs) |
| 122 | + |
| 123 | + def potential_2d_from(self, grid: aa.type.Grid2DLike, xp=np, **kwargs): |
| 124 | + return self.input_potential.potential_2d_from(grid=grid, xp=xp, **kwargs) |
| 125 | + |
| 126 | + def deflections_yx_2d_from(self, grid: aa.type.Grid2DLike, xp=np, **kwargs): |
| 127 | + return self.input_potential.deflections_yx_2d_from(grid=grid, xp=xp, **kwargs) |
0 commit comments