Skip to content

feat(preprocess): add inner_crop_from_probe — ViT-patch crop from probe geometry#12

Merged
Garrett Bischof (gwbischof) merged 1 commit into
mainfrom
feat/inner-crop-from-probe
Jun 8, 2026
Merged

feat(preprocess): add inner_crop_from_probe — ViT-patch crop from probe geometry#12
Garrett Bischof (gwbischof) merged 1 commit into
mainfrom
feat/inner-crop-from-probe

Conversation

@gwbischof

Copy link
Copy Markdown
Collaborator

Adds inner_crop_from_probe(probe, threshold=0.5) to ptychoml.preprocess.

Why

The reconstruction probe's amplitude defines which pixels of each ViT output patch carry meaningful signal. For a roughly circular probe of radius R (measured at threshold × peak amplitude), the largest inscribed axis-aligned square gives:

inner_crop = floor(patch/2 − R/√2)     # clamped to [0, patch//4]

Extracted from the geometry half of holoptycho's inner_crop_from_onnx so holoptycho can import it — the ONNX-loading / probe-tensor extraction glue stays local (filename convention, onnx lib). Accepts a complex or real 2-D probe, so it works on both a model's ONNX-baked probe and PtychoViTInference.baked_probe.

Pure numpy, array-in/scalar-out — same boundary as compute_sample_pixel_size / normalize_mosaic.

Note

The original inner_crop_from_onnx docstring formula (patch//2 − floor(R/√2)) didn't match its own code (floor of the whole expression — they differ by ±1). I kept the code's behavior (faithful to #37) and corrected the formula in the docstring.

Tests

tests/test_preprocess.py (+8): inscribed-square value, quarter-patch clamp, complex-amplitude handling, real==complex, threshold-controls-support, empty→None, 2-D requirement, and a lock against the original geometry on a gaussian probe.

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

First of the two H4 PRs (holoptycho inner_crop_from_onnx will call this); the other is true vit-only mode.

…be geometry

Derive a ViT-patch inner_crop from a reconstruction probe: for a roughly
circular probe of radius R (at threshold x peak amplitude), the largest
inscribed axis-aligned square gives floor(patch/2 - R/sqrt(2)), clamped to
[0, patch//4]. Accepts a complex or real 2-D probe (a model's ONNX-baked
probe, or PtychoViTInference.baked_probe).

Extracted from the geometry half of holoptycho/vit_inference.py
inner_crop_from_onnx so holoptycho can import it (the ONNX-loading / probe
extraction glue stays in holoptycho). Note the original docstring formula
(patch//2 - floor(R/sqrt2)) didn't match its own code (floor of the whole
expression); kept the code's behavior and corrected the formula.

Tested in tests/test_preprocess.py: inscribed-square value, quarter-patch
clamp, complex-amplitude handling, real==complex, threshold-controls-support,
empty->None, 2-D requirement, and a lock against the original geometry on a
gaussian probe.

Co-authored-by: Himanshu Goel <4122621+himanshugoel2797@users.noreply.github.com>
@gwbischof Garrett Bischof (gwbischof) merged commit b6e4f7f into main Jun 8, 2026
5 checks passed
@gwbischof Garrett Bischof (gwbischof) deleted the feat/inner-crop-from-probe branch June 8, 2026 18:03
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