diff --git a/autoarray/dataset/imaging/simulator.py b/autoarray/dataset/imaging/simulator.py index d4ae637b9..d439c4e1e 100644 --- a/autoarray/dataset/imaging/simulator.py +++ b/autoarray/dataset/imaging/simulator.py @@ -243,12 +243,15 @@ def via_image_from( image = image + background_sky_map - image_with_poisson_noise = preprocess.data_eps_with_poisson_noise_added( - data_eps=image, - exposure_time_map=exposure_time_map, - seed=self.noise_seed, - xp=xp, - ) + # Only draw Poisson noise when it is used. The check is on the simulator's + # flags (Python-level), so it is safe under JAX tracing. + if self.add_poisson_noise_to_data or self.include_poisson_noise_in_noise_map: + image_with_poisson_noise = preprocess.data_eps_with_poisson_noise_added( + data_eps=image, + exposure_time_map=exposure_time_map, + seed=self.noise_seed, + xp=xp, + ) if self.add_poisson_noise_to_data: image = image_with_poisson_noise diff --git a/autoarray/dataset/preprocess.py b/autoarray/dataset/preprocess.py index da8d57c66..8dd991fba 100644 --- a/autoarray/dataset/preprocess.py +++ b/autoarray/dataset/preprocess.py @@ -464,6 +464,11 @@ def poisson_noise_via_data_eps_from(data_eps, exposure_time_map, seed=-1, xp=np) Values are computed from a Poisson distribution using the image's input values in units of counts. + Tiny negative values (e.g. floating-point round-off from PSF convolution) are clipped to + zero before the draw. On the NumPy path, an image that is genuinely negative (minimum below + ``-1e-10`` times its maximum) raises a ``DatasetException`` instead. The JAX path only + clips, because it cannot raise on traced values. + Parameters ---------- data_eps @@ -487,6 +492,14 @@ def poisson_noise_via_data_eps_from(data_eps, exposure_time_map, seed=-1, xp=np) image_counts = data_eps.array * exposure_time_map.array if xp is np: + if image_counts.min() < -1e-10 * abs(image_counts.max()): + raise exc.DatasetException( + "Cannot draw Poisson noise from an image with negative values " + f"(minimum {image_counts.min()} counts, maximum {image_counts.max()} " + "counts). Poisson noise needs a non-negative image; check the input " + "image and background sky level." + ) + image_counts = np.maximum(image_counts, 0) setup_random_seed(seed) noisy_eps_array = ( np.random.poisson(image_counts, data_eps.shape) / exposure_time_map.array @@ -496,6 +509,7 @@ def poisson_noise_via_data_eps_from(data_eps, exposure_time_map, seed=-1, xp=np) effective_seed = seed if seed != -1 else int(time.time() * 1e6) & 0xFFFFFFFF key = jax.random.PRNGKey(effective_seed) + image_counts = xp.maximum(image_counts, 0) noisy_eps_array = ( jax.random.poisson(key, image_counts) / exposure_time_map.array ) diff --git a/test_autoarray/dataset/imaging/test_simulator.py b/test_autoarray/dataset/imaging/test_simulator.py index 02edda895..d91798886 100644 --- a/test_autoarray/dataset/imaging/test_simulator.py +++ b/test_autoarray/dataset/imaging/test_simulator.py @@ -175,6 +175,76 @@ def test__via_image_from__psf_on__psf_and_noise_both_on(image_central_delta_3x3) ) +def test__via_image_from__poisson_off__round_off_negative_pixel_does_not_raise(): + # PSF convolution can leave round-off negatives (~-1e-19). With Poisson noise + # off, the simulator must not draw Poisson noise at all, so these never reach + # ``np.random.poisson`` (which raises ``ValueError: lam < 0``). + image = aa.Array2D.no_mask( + values=np.array([[0.1, -1.0e-19, 0.1], [0.1, 0.19, 0.1], [0.1, 0.1, 0.1]]), + pixel_scales=0.1, + ) + + simulator = aa.SimulatorImaging( + exposure_time=2000.0, + add_poisson_noise_to_data=False, + include_poisson_noise_in_noise_map=False, + noise_seed=1, + ) + + dataset = simulator.via_image_from(image=image) + + assert dataset.data.native == pytest.approx(image.native.array, abs=1e-12) + + +def test__via_image_from__poisson_off__data_is_convolved_image_plus_sky(): + image = aa.Array2D.no_mask( + values=np.array([[0.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 0.0]]), + pixel_scales=1.0, + ) + + kernel = aa.Array2D.no_mask( + values=np.array([[0.0, 1.0, 0.0], [1.0, 2.0, 1.0], [0.0, 1.0, 0.0]]), + pixel_scales=1.0, + ) + psf = aa.Convolver(kernel=kernel) + + simulator = aa.SimulatorImaging( + exposure_time=1.0, + psf=psf, + normalize_psf=False, + background_sky_level=3.0, + subtract_background_sky=False, + add_poisson_noise_to_data=False, + include_poisson_noise_in_noise_map=False, + noise_seed=1, + ) + + dataset = simulator.via_image_from(image=image) + + assert dataset.data.native == pytest.approx( + np.array([[3.0, 4.0, 3.0], [4.0, 5.0, 4.0], [3.0, 4.0, 3.0]]), 1e-8 + ) + + +def test__via_image_from__poisson_on__round_off_negative_pixel_draws_noise(): + image = aa.Array2D.no_mask( + values=np.array([[1.0, -1.0e-19, 1.0], [1.0, 2.0, 1.0], [1.0, 1.0, 1.0]]), + pixel_scales=0.1, + ) + + simulator = aa.SimulatorImaging( + exposure_time=20.0, + add_poisson_noise_to_data=True, + include_poisson_noise_in_noise_map=True, + noise_seed=1, + ) + + dataset = simulator.via_image_from(image=image) + + assert np.isfinite(dataset.data.native.array).all() + assert not np.allclose(dataset.data.native.array, image.native.array) + + def test__via_image_from__image_is_convolved__skips_psf_convolution(): # An already-convolved image (e.g. from an oversampled Convolver) must pass # through untouched by the simulator's own convolution step. diff --git a/test_autoarray/dataset/test_preprocess.py b/test_autoarray/dataset/test_preprocess.py index a8a38aa7a..8a578a3aa 100644 --- a/test_autoarray/dataset/test_preprocess.py +++ b/test_autoarray/dataset/test_preprocess.py @@ -635,6 +635,29 @@ def test__poisson_noise_from_data(): assert (poisson_noise.native == np.array([[-743, 0], [0, -3783]])).all() +def test__poisson_noise_from_data__round_off_negative_is_clipped(): + data = aa.Array2D.no_mask(values=[[10.0, -1.0e-19], [10.0, 10.0]], pixel_scales=1.0) + exposure_time_map = aa.Array2D.ones(shape_native=(2, 2), pixel_scales=1.0) + + poisson_noise = aa.preprocess.poisson_noise_via_data_eps_from( + data_eps=data, exposure_time_map=exposure_time_map, seed=1 + ) + + # The clipped pixel has zero expected counts, so its noise only removes the round-off. + assert poisson_noise.native[0, 1] == pytest.approx(1.0e-19, abs=1e-25) + assert (poisson_noise.native.array != 0.0).any() + + +def test__poisson_noise_from_data__genuinely_negative_image_raises(): + data = aa.Array2D.no_mask(values=[[1.0, -0.5], [0.5, 0.5]], pixel_scales=1.0) + exposure_time_map = aa.Array2D.ones(shape_native=(2, 2), pixel_scales=1.0) + + with pytest.raises(aa.exc.DatasetException): + aa.preprocess.poisson_noise_via_data_eps_from( + data_eps=data, exposure_time_map=exposure_time_map, seed=1 + ) + + def test__data_with_poisson_noised_added(): data = aa.Array2D.zeros(shape_native=(2, 2), pixel_scales=1.0) exposure_time_map = aa.Array2D.ones(shape_native=(2, 2), pixel_scales=1.0)