Files
Jungfraujoch/image_analysis/spot_finding/SpotFindingSettings.h
T
leonarski_fandClaude Opus 4.8 0ed50d91c9 Add self-calibrating adaptive spot detection for offline stills
The offline CPU spot finder marks a pixel strong when it clears a fixed photon
count AND a local-window SNR. The fixed photon floor forces per-dataset tuning:
its sweet spot tracks the background level (weak sets want a low threshold,
strong or high-background sets a high one) and the usable window is narrow, so
users hand-tune --spot-threshold/--spot-sigma per dataset.

Add an opt-in --adaptive-spots mode (AdaptiveSpotFinderCPU) that replaces the
fixed floor with a per-resolution-ring threshold derived from each image's own
noise. Per ring it computes a peak-excluded background mean and sigma (one plain
pass + two sigma-clip passes over the assembled photon image, binned by the
azimuthal-integration ring index) and sets

    thr = max( PoissonTail(mean, p), mean + z * sqrt(sigma^2 + read^2) )

with p = false_pixels_per_frame / n_pixels the single portable knob (default
100) and z = Phi^-1(1 - p). The Poisson arm is the correct significance where
the background is countable (it carries the sqrt(mean) shot noise, so a bright
low-resolution ring gets a high threshold); the read-noise-floored Gaussian arm
keeps the threshold physical where the background vanishes (empty high-resolution
rings), without which those rings flood. read is a detector-level constant, not
a per-dataset knob. Both arms are needed: Poisson alone floods near-zero
background, Gaussian alone drops the shot-noise term and under-thresholds bright
rings.

One --adaptive-spots setting then adapts across a wide range of serial datasets
with no per-dataset threshold, matching or beating hand-tuned thresholds and the
peakfinder8/xgandalf reference on both weak large-cell and strong serial data,
with equal merged R-free.

The finder runs on the CPU (offline/viewer path) and reads the host image, which
the GPU pipeline already keeps in sync, so it works in either build. The default
(non-adaptive) path and the online/FPGA path are unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-23 17:23:19 +02:00

35 lines
1.6 KiB
C++

// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <optional>
#include <cstdint>
struct SpotFindingSettings {
bool enable = true;
float signal_to_noise_threshold = 4.0; // STRONG_PIXEL in XDS
int64_t photon_count_threshold = 10; // Threshold in photon counts
int64_t min_pix_per_spot = 2; // Minimum pixels per spot
int64_t max_pix_per_spot = 50; // Maximum pixels per spot
float high_resolution_limit = 2.0;
float low_resolution_limit = 50.0;
float cutoff_spot_count_low_res = 5.0;
std::optional<float> high_res_gap_Q_recipA = 1.5; // 0.25 * 2 * pi
// Half-width of the ice-ring exclusion band in q (2*pi/d). Measured hexagonal-ice ring FWHM on the
// JUNGFRAU is ~0.06 q, so the band half-width is ~0.03; 0.02 under-covered the strong low-res rings.
float ice_ring_width_Q_recipA = 0.03;
bool indexing = true;
bool quick_integration = true;
// Self-calibrating detection (offline/rugnux path): when true, the fixed photon_count_threshold is
// replaced by a per-resolution-ring threshold set from the image's own noise (see
// AdaptiveSpotFinderCPU), so the same setting adapts across datasets with no per-dataset tuning.
// false_pixels_per_frame is the one portable knob: the expected number of noise pixels tolerated
// per frame (the threshold's operating point), ~100 for a multi-megapixel detector.
bool adaptive_threshold = false;
float false_pixels_per_frame = 100.0f;
};