beam stop: a bright ring is not a beam stop

Inside the stop a whole ring is blocked, so its own median is blocked too and the per-pixel
comparison has nothing to work with. The walk that covers that case declared a ring to be inside the
stop when its background fell below a third of the LARGEST background of any ring further out - and
on a sample whose background peaks in a strong ring away from the beam, the ordinary background
inside that ring is legitimately below a third of the peak. The walk then runs out to the ring and
returns a filled disk of good detector: on one corpus dataset 16 % of the area, with diffraction
rings plainly visible inside the disk it masked, and on another 2.5 %.

The defect predates the branch; what changed is that the flood from the beam centre used to discard
the disk whenever its seeds landed on invalid pixels, which is how the first of those two datasets
came back with an empty mask instead of a wrong one. Dropping that anchor was right, and it made
this visible.

A ring is now compared against what this detector's background typically is - the median over the
rings the walk is willing to judge - which is robust to a bright ring and to a corner ring of a
handful of pixels alike, and is less code. Evaluated over 151 corpus datasets: the number returning
a disk larger than 0.2 % of the detector falls from 12 to 3, the two pathological cases collapse
(radius 974 -> 68 px and 188 -> 12 px), and genuine stops move by a few pixels at most (152 -> 132,
122 -> 120, 70 -> 64). The new test fails on the old walk and passes on this one.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
leonarski_fandClaude Opus 5 committed 2026-09-13 07:36:57 +02:00
1 parent 99910e187e
commit a7ed4e2d34
3 files changed
+65 -19

No files matched your search

+7 -6
View File
@@ -81,12 +81,13 @@ own radius**, not to a global threshold.
7. **Reflection guard.** A pixel whose **max**-projection reached `min_reflection` recorded a real
reflection — a beam stop cannot block a reflection that was measured — so it is **never masked**
(a small-cluster test drops single-pixel zingers).
8. **Central disk (low-res guard).** From the azimuthal **blocked fraction** about the beam centre,
mask a full disk out to the radius where nearly every azimuth is blocked (a disk blocks all
directions; a thin arm/gap does not). Returns nothing when the centre is not behind a disk, and
is **capped just inside the innermost reflection** so a real edge spot is never swallowed. This
guards the low-resolution zone the stop makes un-measurable, including a disk the raw data has
already voided.
8. **Central disk (low-res guard).** Inside the stop a whole ring is blocked, so its own median is
blocked too and step 4 has nothing to compare against. Walking outward, every ring whose
background is below `blocked_ring_ratio` of **what this detector's background typically is** -
the median over the rings the walk is willing to judge - lies wholly inside the stop and is
shadow in its entirety. The comparison is deliberately **not** against the largest background
further out: a sample whose background peaks in a strong ring away from the beam (a powder
standard, a strong solvent ring) then reads as a stop the size of that ring.
9. **Soft boundary.** Grow the core+disk into partially-shadowed pixels (`ratio < penumbra_ratio`)
within `penumbra_max_px`, then a morphological close rounds the contour and hole-filling closes
the disk interior.
+21 -13
View File
@@ -646,24 +646,32 @@ std::vector<uint32_t> ShadowFinder::GetMask(size_t nthreads) const {
if (valid[i])
ring_pixels[radius[i]]++;
// A ring lies inside the stop when its background is a fraction of the background further out.
// Counting statistics cannot decide this: on a bright dataset the shadow is still well counted.
// The comparison is only ever used to answer "is this whole ring inside the stop", never to
// judge an individual pixel, so taking the largest background over an outward window is safe
// here in a way it would not be per pixel. It is taken only over the rings this same walk is
// willing to judge, though: at the corner of the detector a ring holds a handful of pixels and
// its median is one pixel's mean, so one recorded reflection out there would otherwise become
// the background every ring inside it is compared against.
std::vector<float> outward_max(max_radius + 2, 0.0f);
for (int rad = max_radius; rad >= 0; rad--)
outward_max[rad] = std::max(ring_pixels[rad] >= MIN_RING_PIXELS ? baseline[rad] : 0.0f,
outward_max[rad + 1]);
// A ring lies inside the stop when its background is a fraction of what this detector's
// background typically is. Counting statistics cannot decide this: on a bright dataset the
// shadow is still well counted. The comparison used to be against the LARGEST background of any
// ring further out, and that reads a sample whose background peaks in a strong ring away from
// the beam - a powder standard, a strong solvent ring - as a beam stop the size of that ring:
// the ordinary background inside it is legitimately below a third of the peak. On one corpus
// dataset it declared 16 % of the detector to be stop, with diffraction rings visible inside the
// disk it masked. The median over the rings this walk is willing to judge is what "typically"
// means, and it is robust from both sides - to a bright ring, and to a corner ring of a handful
// of pixels whose median is one pixel's mean.
std::vector<float> judgeable;
for (int rad = 0; rad <= max_radius; rad++)
if (ring_pixels[rad] >= MIN_RING_PIXELS)
judgeable.push_back(baseline[rad]);
float typical_background = 0.0f;
if (!judgeable.empty()) {
const auto middle = judgeable.begin() + judgeable.size() / 2;
std::nth_element(judgeable.begin(), middle, judgeable.end());
typical_background = *middle;
}
int blocked_out_to = -1;
for (int rad = 0; rad <= max_radius; rad++) {
if (ring_pixels[rad] < MIN_RING_PIXELS)
continue;
if (baseline[rad] >= BLOCKED_RING_RATIO * outward_max[rad])
if (baseline[rad] >= BLOCKED_RING_RATIO * typical_background)
break;
blocked_out_to = rad;
}
+37
View File
@@ -338,3 +338,40 @@ TEST_CASE("ShadowFinder_APolarizedBackgroundIsNotAShadow", "[ShadowFinder]") {
CHECK(mask_for(std::nullopt) > static_cast<long>(W) * H / 20);
}
// Inside the stop a whole ring is blocked, so its own median is blocked too and the per-pixel
// comparison has nothing to work with; the walk that finds those rings is what covers the disk.
// It decides by comparing a ring against what this detector's background typically is - and NOT
// against the brightest ring anywhere further out, which a sample whose background peaks well away
// from the beam turns into a beam stop the size of that peak. The scene here is that: an ordinary
// stop, and a strong ring at four times the background far outside it. What comes back has to be
// the stop, not a disk reaching the ring.
TEST_CASE("ShadowFinder_ABrightRingIsNotABeamStop", "[ShadowFinder]") {
constexpr int RING_R = 100, RING_HALF = 2, RING_LEVEL = 8 * BACKGROUND;
const DiffractionExperiment x = TestExperiment();
const PixelMask pixel_mask(x);
ShadowFinder finder(x, pixel_mask);
std::vector<std::vector<int32_t>> frames;
std::vector<uint8_t> buffer;
for (int f = 0; f < NFRAMES; f++) {
frames.push_back(Scene(/*cross=*/false, /*reflection=*/false));
for (int y = 0; y < H; y++)
for (int xi = 0; xi < W; xi++) {
const double r = std::hypot(xi - C, y - C);
if (std::abs(r - RING_R) <= RING_HALF)
frames.back()[I(xi, y)] = RING_LEVEL;
}
DataMessage msg{};
msg.image = CompressedImage(frames.back(), W, H);
finder.AddImage(msg, buffer);
}
const auto mask = finder.GetMask();
CHECK(mask[I(C, C)] == 1); // the stop is still covered
CHECK(mask[I(C, C - STOP_R - 20)] == 0); // lit detector between the stop and the ring
CHECK(mask[I(C, C - RING_R + 5)] == 0);
CHECK(mask[I(C, C - RING_R)] == 0); // and the ring itself
// Anything much beyond the stop, its arm and their penumbra means the walk ran away.
CHECK(std::count(mask.begin(), mask.end(), 1u) < 6000);
}