Skip to content

feat(stitch): add normalize_mosaic — averaging companion to the accumulators#11

Merged
Garrett Bischof (gwbischof) merged 1 commit into
mainfrom
feat/normalize-mosaic
Jun 8, 2026
Merged

feat(stitch): add normalize_mosaic — averaging companion to the accumulators#11
Garrett Bischof (gwbischof) merged 1 commit into
mainfrom
feat/normalize-mosaic

Conversation

@gwbischof

Copy link
Copy Markdown
Collaborator

Adds normalize_mosaic(canvas, counts, min_overlap=0.5) to ptychoml.stitch.

Why

The stitch_batch_* accumulators deliberately leave normalization to the caller (their docstrings say "No normalization happens here"). normalize_mosaic is the missing other half of that API — it averages a (canvas, counts) pair into a display mosaic:

  • covered pixels (counts >= min_overlap) → canvas / counts
  • under-covered pixels → NaN
  • returns (fill_value, mosaic) where fill_value is the median of covered pixels — a neutral background for renderers that treat NaN as zero

Lifted from holoptycho/vit_inference.py's MosaicWriterOp._normalise_full so holoptycho can import it instead of carrying the inline copy (a follow-up updates the H3 PR NSLS2/holoptycho#40 to use it).

Tests

tests/test_stitch.py (+9 cases): averaging, NaN masking of thin coverage, fill_value == median(covered), inclusive threshold boundary, higher-threshold exclusion, empty input → (0.0, all-NaN), float32 output, input immutability, and an end-to-end stitch_batch_livestitch_into → normalize_mosaic round-trip that recovers the patch value.

pixi run --environment ci-py312 test   # 150 passed, 5 skipped

Docs

README Stitching table + usage example updated to use normalize_mosaic; AGENTS.md note added.

…ccumulators

The stitch_batch_* accumulators deliberately leave normalization to the
caller. normalize_mosaic completes that API: given a (canvas, counts) pair it
returns (fill_value, mosaic) where covered pixels (counts >= min_overlap)
become canvas/counts, under-covered pixels become NaN, and fill_value is the
median of covered pixels (a neutral background for renderers that treat NaN
as zero).

Lifted from holoptycho/vit_inference.py MosaicWriterOp._normalise_full so
holoptycho can import it instead of carrying the inline copy.

Tested in tests/test_stitch.py (averaging, NaN masking, fill=median,
inclusive threshold, higher-threshold exclusion, empty input, float32 output,
input immutability, and an end-to-end stitch->normalize round-trip).

Co-authored-by: Himanshu Goel <4122621+himanshugoel2797@users.noreply.github.com>
@gwbischof Garrett Bischof (gwbischof) merged commit 982dea8 into main Jun 8, 2026
5 checks passed
@gwbischof Garrett Bischof (gwbischof) deleted the feat/normalize-mosaic branch June 8, 2026 17:21
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.

1 participant