Skip to content

fix: SimulatorImaging draws Poisson noise before checking the flag #687

Closed

Description

@Jammy2211

Overview

SimulatorImaging.via_image_from always draws Poisson noise, then checks add_poisson_noise_to_data. A simulator with Poisson noise switched off still calls np.random.poisson, which raises ValueError: lam < 0 when PSF convolution leaves round-off negatives (about 2e-17 of the peak). That killed 4 of 100 Euclid sersic resimulations. A caller cannot avoid it because the negatives are created inside the simulator.

Plan

  • Draw Poisson noise only when add_poisson_noise_to_data or include_poisson_noise_in_noise_map is set (a Python-level check on the simulator's flags, so JAX tracing is unaffected).
  • In the Poisson helper, clip round-off negatives to zero before drawing (NumPy and JAX paths), so callers who want noise get noise rather than an exception.
  • Raise a clear DatasetException for an image that is genuinely negative (below -1e-10 times its maximum) instead of clipping it silently.
  • Unit tests for the three cases.
  • Follow-up after release: remove the Euclid pipeline workaround (euclid_strong_lens_modeling_pipeline PR Feature/tracer contour #87).

Tier: judge — merge mode: human /prm

Heart RED override (development only): Heart RED at start (PyAutoGalaxy/PyAutoLens Tests failure on main, workspace validation 14 failed, worktree drift). Human approved the override in session 2026-11-10 ("override is fine, we'll fix red separately"). Release gates are unchanged.

Claim coordination: PyAutoArray is also claimed by magnification-maps-wiring (PyAutoLens#818). The files do not overlap; the human approved coordinating and shipping separately.

Detailed implementation plan

Affected Repositories

  • PyAutoArray (primary)

Branch Survey

Repository Current Branch Dirty?
./PyAutoArray main clean (2 behind origin, docs only)

Suggested branch: feature/simulator-poisson-flag-guard

Implementation Steps

  1. autoarray/dataset/imaging/simulator.py via_image_from: wrap the data_eps_with_poisson_noise_added call in if self.add_poisson_noise_to_data or self.include_poisson_noise_in_noise_map:. The noise-map branch keeps using image_with_poisson_noise (only reached when it was computed).
  2. autoarray/dataset/preprocess.py poisson_noise_via_data_eps_from: on the NumPy path, if image_counts.min() < -1e-10 * abs(image_counts.max()) raise exc.DatasetException; otherwise np.maximum(image_counts, 0) before np.random.poisson. On the JAX path use xp.maximum(image_counts, 0) with no value-level raise (tracer-safe).
  3. Tests in test_autoarray/dataset/imaging/test_simulator.py and test_autoarray/dataset/test_preprocess.py:
    • with both flags off, an image with a -1e-19 pixel simulates without raising and the data is unchanged for a non-negative image (the Witness);
    • with Poisson on, a round-off-negative image draws noise;
    • a genuinely negative image raises.

Key Files

  • autoarray/dataset/imaging/simulator.py: the unconditional draw
  • autoarray/dataset/preprocess.py: poisson_noise_via_data_eps_from

Original Prompt

Click to expand starting prompt

SimulatorImaging draws Poisson noise before checking add_poisson_noise_to_data

Type: bug
Target: autoarray
Repos:

  • PyAutoArray
    Themes:
  • simulation
  • robustness
    Difficulty: low
    Autonomy: safe
    Priority: medium
    Status: draft
    Consequence: check
    Witness: a simulator built with add_poisson_noise_to_data=False simulates an image carrying a
    -1e-19 pixel without raising, and the returned data is unchanged for a non-negative image
    Review-minutes: 10
    Filed: 2026-09-13

Defect

autoarray/dataset/imaging/simulator.py:246 (SimulatorImaging.via_image_from) calls

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

The draw is unconditional; the flag is only consulted afterwards. So a simulator
constructed with add_poisson_noise_to_data=False and
include_poisson_noise_in_noise_map=False — where the Poisson result is never used at all —
still runs np.random.poisson (preprocess.py:529 → poisson_noise_via_data_eps_from,
preprocess.py:492).

When any pixel of the post-convolution image is negative, that raises:

ValueError: lam < 0 or lam contains NaNs

The negatives need not be physical. PSF-convolving a strictly non-negative model image returns
round-off pixels where the true value is zero. Measured on tile
Tile102014322RA0376568496078DECNEG0561070209358 of the dr1_sep1_sersics sample: the input
image has minimum +5.0e-22, and its real-space convolution with the tile's real VIS PSF has
89 negative pixels at up to -3.8e-18 against a peak of 1.9e-01 — about 2e-17 of the
peak, i.e. pure floating-point round-off. A caller who has switched Poisson noise off cannot
see this coming, and cannot clip the input to avoid it, because the negatives are produced
inside via_image_from by its own convolution step.

Observed 2026-09-13 while resimulating the 100 euclid_sersics tiles through
euclid_strong_lens_modeling_pipeline/scripts/simulator.py: 4 of 100 died, indices 10, 62,
63 and 69 of the sorted sample; the other 95 went through. A 4% failure rate on round-off is
what makes it a trap rather than a visible break.

Fix

Draw only when the result is wanted:

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(...)

(or at minimum guard on add_poisson_noise_to_data and compute the noise-map branch from
whichever image it should use). That alone fixes every caller who turned Poisson noise off, and
it removes a wasted RNG draw and a wasted array allocation from every such simulation.

Independently, the draw itself should not be handed a negative lambda: np.maximum(image, 0)
(or an equivalent clip) inside poisson_noise_via_data_eps_from before the np.random.poisson
call, so a caller who does want Poisson noise on an image with convolution round-off gets
noise rather than an exception. Consider whether a genuinely negative image — one whose minimum
is a real fraction of its maximum rather than round-off — deserves a clear exc instead of a
silent clip; the pipeline-side guard chose to raise beyond -1e-10 * maximum.

Note the JAX path: the branch must stay traceable under xp is not np, so prefer a
construction-time (Python-level, on self.* flags) conditional over a value-level one.

Workaround in place

euclid_strong_lens_modeling_pipeline carries a pipeline-side guard on main, merged
2026-09-17 as PR #87 (issue #77, merge dc91d9a): simulated_image_from
runs the convolution itself, clips the round-off, and passes the result back with
image_is_convolved=True. That workaround should be removed once this lands.

Out of scope

The simulator's other noise components, the use_real_space_convolution choice, and anything
about how the round-off arises in the convolution (it is ordinary floating-point behaviour, not
a defect).

Activity

  1. Jammy2211 commented on Nov 10, 2026

    @Jammy2211
    CollaboratorAuthor

    Library PR created — Heart RED override (development only)

    PR: #688 (pending-release)

    Authorization: live human, this session, 2026-11-10: "Do a, override is fine well fix red separate". Claim coordination with magnification-maps-wiring approved: "Coordinate and ship separate".

    Current Heart RED reasons (verbatim): "PyAutoGalaxy/Tests: failure"; "PyAutoLens/Tests: failure"; "PyAutoHands/workflows/Python Version Matrix: failure"; "PyAutoHeart/workflows/Workspace Smoke: failure"; "Workspace validation: NOT ready — 1399p / 14f / 125s @ cloud#42607741369"; "Worktree drift: 0 orphan / 0 missing / 3 dirty".

    Passed branch gates: unit tests, full workspace smoke (154 passed / 0 failed; autocti environments not buildable locally), independent review CLEAN. Details in the PR body.

    Commit, push and PR-open only. Merge needs a separate human /prm with every required check green. No release is authorized, and this PR does not fix Heart.

    🤖 Generated with Claude Code

  2. Jammy2211 commented on Nov 10, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped

    Merged via /prm: #688 (merge 2e494f94, pending-release). All 3 CI legs green (3.12, 3.13, nojax).

    • SimulatorImaging.via_image_from draws Poisson noise only when add_poisson_noise_to_data or include_poisson_noise_in_noise_map is set.
    • poisson_noise_via_data_eps_from clips round-off negatives before drawing. On NumPy, a genuinely negative image (min < -1e-10 × max) raises DatasetException.

    Follow-up (Mind draft, after release): draft/maintenance/euclid/remove_simulator_poisson_round_off_workaround.md removes the Euclid pipeline workaround (PR #87).

    🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions