Skip to content

ngmix masked pixels: independent neighbour and defect handling (UberSeg, noise fill, interpolation, epoch cuts) - #922

Draft
cailmdaley wants to merge 15 commits into
developfrom
feat/masked-pixels
Draft

cailmdaley wants to merge 15 commits into
developfrom
feat/masked-pixels

Conversation

@cailmdaley

@cailmdaley cailmdaley commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

This PR brings ShapePipe's approaches to masked pixels in ngmix into one design. It lands after the first Nibi production run, and changes no defaults on its own: defaults move only after the validation below (#813). It supersedes #915 and #916, and carries the UberSeg segmentation-map path split out of #897.

Design

Each pixel is asked two independent questions, and each has one key: is it a neighbour's pixel, and is it a defect? Defects are treated the same way under every blend handling. That way an A/B test on blend handling changes only the neighbour pixels. In earlier uberseg-vs-noisefill comparisons the defect treatment changed too, which confounded them.

Key Values Default
BLEND_HANDLING none | uberseg none
SEG_VIGNET_PATH path; required iff uberseg unset
DILATE_NEIGHBOUR int ≥ 0 1
DEFECT_FILL noise | interpolate noise
EPOCH_CENTRAL_DEFECT_RADIUS px, 0 disables open (1)
EPOCH_INTERPOLATED_DEFECT_RADIUS px 7
EPOCH_MASKED_FRACTION_CUT (0, 1], counts all defects 1/3
  • Neighbours. Under uberseg, neighbour pixels get zero weight and keep their raw light. This follows DES SV–Y6 practice: same-redshift neighbour light shears coherently under metacal (UberSeg neighbour masking (Erin Sheldon) — config-selectable, validated against noise-fill #776). none is develop's current behaviour. noisefill is renamed to none, because it never filled neighbours.
  • Defects. Defects get zero weight and a fill. noise is the per-pixel RMS noise fill that develop uses today. interpolate (from ngmix: opt-in DEFECT_FILL = interpolate for narrow defects, with a 7 px veto #916) uses Clough–Tocher interpolation over runs of ≤ 3 px, averaged over quarter turns, and gives the quarter-turn orbit zero weight. This takes c₁ from −1.3e-3 to 2e-6 on the ngmix: opt-in DEFECT_FILL = interpolate for narrow defects, with a 7 px veto #916 grid.
  • Epoch cuts. An epoch is dropped when a defect falls within a radius of the object. There is one radius for filled defects and one for interpolated ones. It is also dropped when its masked fraction exceeds EPOCH_MASKED_FRACTION_CUT, which now counts all defects, not only flags. Every cut is logged, so each run reports how many epochs it lost.
  • Workflow. blend_handling and defect_fill go in config.yaml and are rendered into the ngmix template. Today neither reaches ngmix. uberseg adds a third vignetmaker run for segmentation stamps, plus the matching completeness entry. In catalogue mode it also adds tile_segmentation and seg_relabel.py. It is wired in both cfis and cfis_image_sims.

Not implemented; these remain A/B options in the decision record: filling the neighbour pixels themselves, MOF neighbour subtraction, and symmetrizing the neighbour weights (#815). The symmetrized defect fill is not used here because #915's own grid rejects it as a fill (m₁₁ −2.7% vs −0.64%); its code stays on feat/symmetrized-defect-fill for the symmetrization work in #815.

Validation before any default changes

Gate Decides
#915/#916 test grids; one real tile per detection mode × blend handling Merge; seg-map alignment in practice
Epoch-loss and defect-incidence census, 20–50 real tiles (from the cut log) Veto radii, fraction cut (1/3 vs DES's 10%), noise vs interpolate
Blended-sims ladder, both arms UberSeg Δm against the 0.005 target; σₑ / N_eff cost (#813, #814)
Data A/B on matched tiles incl. FLAGS ≤ 2, pure-EB / COSEBIs Does UberSeg remove the FLAGS=2 B-modes (#814), and at what E-mode cost

Both A/B arms run on this code. The Nibi catalogue cannot serve as one of them, because this PR also changes which epochs are kept.

Open decisions

  1. Central-defect veto on by default? A flagged column through the stamp centre gives c₁ = +0.18 in that epoch under today's fill, and CFHT's fixed orientation makes such biases coherent. Against: A/B validation of off-by-default shape-measurement options #813's contract, the pinned default in the decision record, and the fact that epoch loss hasn't been measured. The census resolves the radius either way.
  2. Matching the seg map to Stephen Gwyn's objects: the footprint under each position (seg_relabel.py), or SExtractor ASSOC. The relabel's fallback count on one real tile settles it.
  3. Should UberSeg's neighbour hole get defect-style treatment? It's a one-sided zero-weight hole, the same mechanism as the defect c₁. Neither epoch cut sees it. The A/B: 4-fold weight symmetrisation — shape impact and S/N cost #815 arm on the ladder settles it.
  4. Where does UberSeg live: in ngmix (ngmix: config-selectable UberSeg neighbour masking (BLEND_HANDLING) #770), or in vignetmaker / its own module so other shape methods can reuse it (Masked pixels in ngmix: neighbour (UberSeg) and defect handling #602)?
  5. Does the coadd seg map overlaid on epochs hold up without reprojection, at DILATE_NEIGHBOUR = 1? Check the neighbour flag and the registration warnings on the ladder.

State

The seg-map branch is built on #897, so #897's content (UNIONS catalogue detection, TILE_UNIQUE_ID, ngmix row chunks) rides along here until #897 merges into develop.

Closes #602.

🤖 Generated with Claude Code

cailmdaley and others added 8 commits September 26, 2026 07:07
…defects

Defects (nonzero flag, zero weight, invalid RMS) must be zero-weighted and
noise-filled under every BLEND_HANDLING, with neighbour pixels left raw
under uberseg. The masked-fraction cut must count that same raw set, and an
epoch with a defect closer than EPOCH_CENTRAL_DEFECT_RADIUS to the stamp
centre must be dropped, at a strict boundary and at the configured radius.
The runner must pass both cut options on, the HSM centroid must ignore raw
defect values, each epoch must keep its own OFFSET, and every tile must log
its epoch-cut tally.

The science test recovers the full 2x2 response for columns, 3-px bleeds,
single pixels and edge bands at the veto radius, on 0.3" and 0.5" galaxies
through round and elliptical 0.7" PSFs, and requires |c1|, |c2| < 5e-4 and
|m11|, |m22| < 1%. Flagged pixels in the simulation hold a hot value, so an
unfilled defect shows up. A column two pixels inside the radius is the
positive control.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QAc1ywyd9Sbbra6po2iuaA
…ral ones

Metacal shears the whole stamp without reading the weights, so a defect
left raw under uberseg reached the fit. Every defect (nonzero flag, zero
weight, invalid RMS) is now zero-weighted and noise-filled whatever
BLEND_HANDLING is; uberseg only zeroes the weight of neighbour-side pixels
and keeps their light. The option that meant "no neighbour treatment" is
renamed from noisefill to none, and the retired name is rejected.

The defect set is not symmetrized: at the veto radius the one-sided fill
leaves |c| <= 3e-4, while a four-fold mask would quadruple m (-2.7% against
-0.64% for a 3-px bleed at 10 px on a 0.5" galaxy) and, through an elliptical
PSF, still leave c1 = 7.3e-4.

An epoch is dropped when a defect lies closer than
EPOCH_CENTRAL_DEFECT_RADIUS (default 10 px) to the stamp centre, the
smallest radius at which columns, 3-px bleeds, single pixels and edge bands
give |m11|, |m22| < 1% and |c1|, |c2| < 5e-4 on 0.3" and 0.5" galaxies
through a 0.7" PSF. The masked-fraction cut counts the same raw defect set;
EPOCH_MASKED_FRACTION_CUT (default 1/3) makes it configurable. Each tile
logs "epoch cuts: considered=N masked_fraction=A central_veto=B
objects_emptied=C". The HSM centroid is measured on the filled image.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QAc1ywyd9Sbbra6po2iuaA
prepare_postage_stamps already holds the object's galaxy vignet dict, so the
per-epoch OFFSET is read from it instead of a second sqlitedict lookup. The
pixel scale sets only the centroid-prior width; the comments that also
credited it with a noise window are corrected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QAc1ywyd9Sbbra6po2iuaA
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The interpolation fills only short defect runs (at most 3 px along a row or
column, with clean pixels at both ends); anything wider, and runs reaching
the stamp border, stays noise-filled. The interpolant must reproduce linear
planes without reading defect values, commute with quarter turns of the
stamp, and apply one operator to the science and metacal noise images. The
weight is zero on the defects and on the quarter-turn orbit of the
interpolated pixels, whose light stays. Each defect is vetoed at the radius
of its own fill: EPOCH_INTERPOLATED_DEFECT_RADIUS for interpolated pixels,
EPOCH_CENTRAL_DEFECT_RADIUS for noise-filled ones. The runner and Ngmix pass
DEFECT_FILL to the epoch cuts and to metacal; noise stays the default.

The science test recovers the full response matrix for interpolated columns,
full and finite 3-px bleeds and single pixels at the interpolated radius, on
0.3" and 0.5" galaxies through round and elliptical 0.7" PSFs and on a 0.7"
galaxy through a 0.9" PSF, and for noise-filled wide defects at the noise
radius. A bleed three pixels inside the radius is the positive control.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QAc1ywyd9Sbbra6po2iuaA
… veto

DEFECT_FILL = interpolate fills short defect runs (at most 3 px along a row
or column, with clean pixels at both ends: columns, 3-px bleeds, single
pixels) with a Clough-Tocher interpolant of the clean pixels within 4 px.
The interpolant is averaged over the four quarter turns of the stamp, and
each turn's triangulation serves the science image and the metacal noise
image alike. Wider holes and runs reaching the stamp border keep the noise
fill. noise stays the default and is unchanged.

Interpolation restores the object's light, so the remaining bias is the
hole in the likelihood: a one-sided zero-weight column 8 px from a 0.5"
galaxy still gives c1 = -1.3e-3. The weight is therefore also zero on the
quarter-turn orbit of the interpolated pixels (their light stays), which
brings that column to c1 = +2e-6. Symmetrizing the fill instead would trade
true light for interpolated light over four times the area.

Each defect pixel is vetoed at the radius of its own fill:
EPOCH_INTERPOLATED_DEFECT_RADIUS (default 7 px) for interpolated pixels and
EPOCH_CENTRAL_DEFECT_RADIUS (10 px) for noise-filled ones. 7 px is the
smallest radius at which interpolated columns, 3-px bleeds and single pixels
give |m11|, |m22| < 1% and |c1|, |c2| < 5e-4 on 0.3", 0.5" and 0.7" galaxies,
round and elliptical PSFs; it does not scale with galaxy size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QAc1ywyd9Sbbra6po2iuaA
Brings the decision record to what this branch does:
- central_defect_veto: committed option fixed_radii (was disabled); the
  10 px EPOCH_CENTRAL_DEFECT_RADIUS is pinned by a Values entry on its
  module constant, now tagged.
- epoch_masked_fraction_cut: the cut counts the defect set against
  EPOCH_MASKED_FRACTION_CUT (no longer [HARDCODED] on flags).
- defect_fill: defects are noise-filled under every BLEND_HANDLING; the
  docstring [LINT] this branch fixes is dropped.
- blend_handling: the no-treatment option is BLEND_HANDLING = none.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Brings develop (#875's decision record) in through the updated base, and
amends the record for this branch:
- defect_fill: the interpolate option is implemented (DEFECT_FILL =
  interpolate); noise stays the committed fill.
- central_defect_veto: EPOCH_INTERPOLATED_DEFECT_RADIUS = 7 joins the
  Values, on its now-tagged module constant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
cailmdaley added a commit that referenced this pull request Sep 28, 2026
Brings the tile segmentation map for UberSeg into #922, so the masked-pixels
work sits in one place. feat/uberseg-seg-map is built on #897
(feat/unions-catalogue-tile-detection), so this provisionally carries #897's
content too (UNIONS catalogue detection, TILE_UNIQUE_ID, ngmix row chunks)
until #897 lands on develop.

No textual conflicts: ngmix.py's row-selection chunking (#897) and the epoch
cuts, defect fill and constants (#915/#916) touch disjoint hunks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
cailmdaley and others added 7 commits September 29, 2026 16:11
Conflicts: the ngmix runner imports both the epoch-cut defaults and
write_empty_tile_output; Ngmix.process logs the epoch-cut tally and then
calls log_run_health. Develop's run-health test stubs prepare_postage_stamps
with the epoch-cut kwargs and tally and sets the epoch-cut attributes; the
defect-fill process tests give their vignettes an OFFSET and their logger
warning/error methods.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The neighbour option keeps develop's name, noisefill, in the code, the runner
config key, tests, astra.yaml and the committed universe; the name none is
dropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SExtractor's -1e30 markers in the tile VIGNET form their own per-epoch mask
(stamp.neighbours): a footprint 3 px from the centre, or covering 45% of the
stamp, drops no epoch; noisefill zero-weights and noise-fills exactly the
marked pixels, bit for bit as develop does; uberseg leaves them raw and
weighted; a flagged column near the centre is still vetoed; and
do_ngmix_metacal hands each epoch's mask to make_ngmix_observation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
prepare_postage_stamps wrote the tile VIGNET's -1e30 markers (other
detections' footprints) into each epoch's flag stamp as 2**10, so
defect_mask counted them: they fed the masked-fraction cut and the central
veto. Every epoch shares the tile VIGNET, so a neighbour within
EPOCH_CENTRAL_DEFECT_RADIUS dropped every epoch of the object.

The markers are now their own per-epoch mask, stamp.neighbours
(MegaCam-flipped with the epoch), threaded through do_ngmix_metacal and
make_ngmix_observation to prepare_ngmix_weights. The epoch cuts count
genuine defects only. Under noisefill the marked pixels get weight 0 and
the defects' noise realisation, the output noisefill gave when they arrived
as flags; under uberseg they are ignored and the seg-based weight handles
neighbours. Defects are treated identically under both. Docstrings, @sc
contracts, runner comments and the astra.yaml decisions say so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SExtractor writes -1e30 into the tile VIGNET beyond the tile's edge as well
as on other detections' footprints. Beyond the edge the epoch holds the
object's own light, cut off, so treating those pixels as neighbours exempted
edge objects from both epoch cuts and measured them with a noise-filled band
near their centre.

split_tile_markers classifies the markers geometrically on the flipped tile
VIGNET: the union of stamp rows and columns that are entirely -1e30 (the
off-image part of a rectangle clip) is off-tile, the rest is the neighbour
mask. Off-tile pixels are flagged OFF_TILE_FLAG (2**10) in the epoch's flag
stamp, so under both blend handlings they are zero-weighted, filled, and
counted by the masked-fraction cut and the central veto. Docstrings, @sc
contracts, runner comments and astra.yaml say so.

Tests: an object 3 px from the tile edge loses every epoch; a band 12 px
away passes the default cuts and is vetoed at radius 13; a corner's L-shaped
off-tile region is flagged exactly and a border-touching neighbour stays a
neighbour; off-tile pixels are filled under both blend handlings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Under DEFECT_FILL = interpolate and BLEND_HANDLING = noisefill, the
Clough-Tocher support included marked neighbour pixels that noisefill
replaces with noise, so a defect beside a bright neighbour was filled with
light the image no longer contains. The support now excludes ~clean (the
defects and the removed neighbour pixels); the NaN fallback to the noise
fill covers degenerate supports. Under uberseg the neighbour light stays in
the image and the support is unchanged. interpolate_defects names its
second argument `excluded`.

Test: a flagged pixel next to a 1e4 marked neighbour interpolates to the
kept-pixel interpolant (1.4, against 3102 with the neighbour in the
support) under noisefill, and from the raw neighbour light under uberseg.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fill_defects used np.where, which promotes a float32 stamp to float64;
develop's noisefill kept the galaxy stamp's dtype. The filled image is cast
back to the input dtype, so noisefill stays bit-identical to develop on
float32 stamps. The develop-equivalence test gains a float32 case and
checks dtypes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Masked pixels in ngmix: neighbour (UberSeg) and defect handling

1 participant