Bragg integration: drop the 2% sigma floor and carry the background variance
Two changes to the same variance chain; they are in one commit because the second exists to remove an assumption the first was breaking, and separating them leaves a tree that is correct only by luck. The reported sigma was floored at 2% of the intensity, a per-partial I/sigma cap of 50. It applied only to the box-sum seed, never to the profile fit, so the shipped default was unaffected - but the combine back-derives each partial's non-signal variance as sigma^2 - I, and a floored sigma makes that quantity mean nothing. It then read corr^2 * (0.0004 I^2 - I), which is not a background variance. Measured on --integrator boxsum: the reported sigma understated the true scatter by up to 16x at I ~ 21000 counts per partial, and pooled_I amplified a 1 ct/px background drift into an 11.5% intensity error on the strongest reflections. What the floor stood in for - that at high intensity the error is systematic rather than counting - is already carried downstream, twice: the fitted b in v = a*sigma^2 + (b*I)^2, measured from the data rather than assumed, and SigmaWithSystematicFloor on the merged sigma. The floor was that idea applied one level too early with a hardcoded b of 0.02. It arrived without a test or a setter and was unreachable from the CLI, the API and the config. The merge now takes the non-signal variance the integrator actually measured instead of inverting sigma^2 = I + N. That identity is exact for a box sum once the floor is gone and was never exact for a profile fit, whose sigma^2 = 1/den + (wsum/den)^2 * bkg_var is formed against a fitted intensity. The value is carried through BraggFitResult, Reflection and Obs, both engines, both merges, and the process-file round trip; files written before this change are read with the term absent, which is what they had. Battery, 37 crystals, paired: space groups unchanged, reflection sets unchanged, median delta zero on R_meas and CC1/2. --integrator boxsum on the reference crystal goes ISa 8.9 -> 20.2 with a 0.947 -> 1.032. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -16,7 +16,6 @@ inline void cuda_err(cudaError_t val) {
|
||||
struct BraggGpuParams {
|
||||
int W, H;
|
||||
float r1_sq, r2, r2_sq, r3, r3_sq;
|
||||
float min_sigma_ratio;
|
||||
int R, G, GG;
|
||||
float bkg_clip_nsigma; // high-side background sigma-clip multiplier (0 = no clip)
|
||||
int empirical; // ProfileEmpirical vs ProfileGaussian
|
||||
@@ -56,7 +55,7 @@ __global__ void mark_mask(const float *px_x, const float *px_y, uint8_t *mask, B
|
||||
__global__ void boxsum(const float *px_x, const float *px_y, const float *dd,
|
||||
const int32_t *img, const uint8_t *mask, BraggGpuParams p, int n,
|
||||
int *cx_o, int *cy_o, float *I_o, float *sigma_o, float *bkg_o,
|
||||
float *bkgvar_o, float *obsx_o, float *obsy_o, uint8_t *ok_o, uint8_t *strong_o,
|
||||
float *bkgvar_o, float *varbkg_o, float *obsx_o, float *obsy_o, uint8_t *ok_o, uint8_t *strong_o,
|
||||
uint8_t *hasobs_o, unsigned long long *invd2mm,
|
||||
float *isum_o, int *ninner_o, int *rbin_o,
|
||||
float *rad_sum, int *rad_cnt, int n_rad) {
|
||||
@@ -235,7 +234,7 @@ __global__ void boxsum(const float *px_x, const float *px_y, const float *dd,
|
||||
// var(I) = Isum + n_inner^2 * bkg/n_bkg_used. Both engines must agree.
|
||||
const double bkg_var = bkg / (double) n_bkg_used;
|
||||
const double var_bkg_term = (double) s_ninner * (double) s_ninner * bkg_var;
|
||||
double sigma = fmax(1.0, I * (double) p.min_sigma_ratio);
|
||||
double sigma = 1.0;
|
||||
uint8_t hasobs = 0; double ox = 0.0, oy = 0.0;
|
||||
if (Isum > 0) {
|
||||
sigma = fmax(sigma, sqrt((double) Isum + var_bkg_term));
|
||||
@@ -247,6 +246,7 @@ __global__ void boxsum(const float *px_x, const float *px_y, const float *dd,
|
||||
cy_o[i] = (int) lroundf(cy);
|
||||
I_o[i] = (float) I; sigma_o[i] = (float) sigma; bkg_o[i] = (float) bkg;
|
||||
bkgvar_o[i] = (float) bkg_var;
|
||||
varbkg_o[i] = (float) ((double) s_ninner * bkg + var_bkg_term);
|
||||
isum_o[i] = (float) Isum;
|
||||
ninner_o[i] = s_ninner;
|
||||
rbin_o[i] = min(max((int) lroundf(s_r0), 0), n_rad > 0 ? n_rad - 1 : 0);
|
||||
@@ -383,10 +383,10 @@ __global__ void radial_correct(const float *rad_sum, const int *rad_cnt, int n_r
|
||||
__global__ void fit(const int32_t *img, const float *px_x, const float *px_y,
|
||||
const int *cx_a, const int *cy_a, const float *dd, const unsigned long long *invd2mm,
|
||||
const float *I_seed, const float *sigma_seed, const float *bkg_a,
|
||||
const float *bkgvar_a, const uint8_t *ok_a,
|
||||
const float *bkgvar_a, const float *varbkg_seed, const uint8_t *ok_a,
|
||||
const float *shell_P, const float *global_P,
|
||||
const float *shell_sigma2, const float *global_sigma2, const int *shell_n,
|
||||
float *I_o, float *sigma_o, uint8_t *ok_o, BraggGpuParams p, int n) {
|
||||
float *I_o, float *sigma_o, float *varbkg_o, uint8_t *ok_o, BraggGpuParams p, int n) {
|
||||
const int i = blockIdx.x;
|
||||
if (i >= n) return;
|
||||
extern __shared__ float Pbuf[];
|
||||
@@ -471,13 +471,15 @@ __global__ void fit(const int32_t *img, const float *px_x, const float *px_y,
|
||||
// estimate's own error (see the CPU engine).
|
||||
const float wr = s_wsum / s_den;
|
||||
float I = s_I, sigma = sqrtf(1.0f / s_den + wr * wr * bkgvar_a[i]);
|
||||
float var_bkg = fmaxf(0.0f, 1.0f / s_den - fmaxf(0.0f, I) + wr * wr * bkgvar_a[i]);
|
||||
// Guard against profile-fit runaways (see the CPU engine): fall back to the summation seed
|
||||
// when the profile result diverges from it.
|
||||
if (fabsf(I - I_seed[i]) > (float) PROFILE_SUMMATION_MAX_NSIGMA * sigma_seed[i]) {
|
||||
I = I_seed[i];
|
||||
sigma = sigma_seed[i];
|
||||
var_bkg = varbkg_seed[i];
|
||||
}
|
||||
I_o[i] = I; sigma_o[i] = sigma; ok_o[i] = 1;
|
||||
I_o[i] = I; sigma_o[i] = sigma; varbkg_o[i] = var_bkg; ok_o[i] = 1;
|
||||
} else ok_o[i] = 0;
|
||||
}
|
||||
}
|
||||
@@ -546,6 +548,7 @@ void BraggIntegrationEngineGPU::EnsureCapacity(size_t n) {
|
||||
d_sigma = CudaDevicePtr<float>(new_capacity);
|
||||
d_bkg = CudaDevicePtr<float>(new_capacity);
|
||||
d_bkg_var = CudaDevicePtr<float>(new_capacity);
|
||||
d_var_bkg = CudaDevicePtr<float>(new_capacity);
|
||||
d_isum = CudaDevicePtr<float>(new_capacity);
|
||||
d_ninner = CudaDevicePtr<int>(new_capacity);
|
||||
d_rbin = CudaDevicePtr<int>(new_capacity);
|
||||
@@ -557,6 +560,7 @@ void BraggIntegrationEngineGPU::EnsureCapacity(size_t n) {
|
||||
|
||||
h_px_x.resize(new_capacity); h_px_y.resize(new_capacity); h_d.resize(new_capacity);
|
||||
h_I.resize(new_capacity); h_sigma.resize(new_capacity); h_bkg.resize(new_capacity);
|
||||
h_var_bkg.resize(new_capacity);
|
||||
h_obs_x.resize(new_capacity); h_obs_y.resize(new_capacity);
|
||||
h_ok.resize(new_capacity); h_has_obs.resize(new_capacity);
|
||||
capacity = new_capacity;
|
||||
@@ -588,7 +592,6 @@ std::vector<Reflection> BraggIntegrationEngineGPU::Run(const ImagePreprocessorBu
|
||||
BraggGpuParams p{
|
||||
.W = static_cast<int>(xpixel), .H = static_cast<int>(ypixel),
|
||||
.r1_sq = r1_sq, .r2 = r2, .r2_sq = r2_sq, .r3 = r3, .r3_sq = r3_sq,
|
||||
.min_sigma_ratio = min_sigma_ratio,
|
||||
.R = R, .G = G, .GG = GG,
|
||||
.bkg_clip_nsigma = mode != IntegratorMode::BoxSum ? bkg_clip_nsigma : 0.0f,
|
||||
.empirical = empirical ? 1 : 0,
|
||||
@@ -613,7 +616,7 @@ std::vector<Reflection> BraggIntegrationEngineGPU::Run(const ImagePreprocessorBu
|
||||
reset<<<32, 256, 0, *stream>>>(d_shell_grid, d_global_grid, d_shell_n, d_global_n, d_invd2, GG);
|
||||
mark_mask<<<n, threads, 0, *stream>>>(d_px_x, d_px_y, d_mask, p, n);
|
||||
boxsum<<<n, threads, 0, *stream>>>(d_px_x, d_px_y, d_d, img, d_mask, p, n,
|
||||
d_cx, d_cy, d_I, d_sigma, d_bkg, d_bkg_var, d_obs_x, d_obs_y,
|
||||
d_cx, d_cy, d_I, d_sigma, d_bkg, d_bkg_var, d_var_bkg, d_obs_x, d_obs_y,
|
||||
d_ok, d_strong, d_has_obs, d_invd2,
|
||||
d_isum, d_ninner, d_rbin,
|
||||
d_rad_sum, d_rad_cnt, rad_n);
|
||||
@@ -633,14 +636,15 @@ std::vector<Reflection> BraggIntegrationEngineGPU::Run(const ImagePreprocessorBu
|
||||
d_shell_grid, d_global_grid, d_shell_n, d_global_n,
|
||||
d_shell_P, d_global_P, d_shell_sigma2, d_global_sigma2, p);
|
||||
fit<<<n, threads, fit_shared_bytes, *stream>>>(img, d_px_x, d_px_y, d_cx, d_cy, d_d, d_invd2,
|
||||
d_I, d_sigma, d_bkg, d_bkg_var, d_ok, d_shell_P, d_global_P,
|
||||
d_I, d_sigma, d_bkg, d_bkg_var, d_var_bkg, d_ok, d_shell_P, d_global_P,
|
||||
d_shell_sigma2, d_global_sigma2, d_shell_n,
|
||||
d_I, d_sigma, d_ok, p, n);
|
||||
d_I, d_sigma, d_var_bkg, d_ok, p, n);
|
||||
}
|
||||
|
||||
cuda_err(cudaMemcpyAsync(h_I.data(), d_I, sizeof(float) * npredicted, cudaMemcpyDeviceToHost, *stream));
|
||||
cuda_err(cudaMemcpyAsync(h_sigma.data(), d_sigma, sizeof(float) * npredicted, cudaMemcpyDeviceToHost, *stream));
|
||||
cuda_err(cudaMemcpyAsync(h_bkg.data(), d_bkg, sizeof(float) * npredicted, cudaMemcpyDeviceToHost, *stream));
|
||||
cuda_err(cudaMemcpyAsync(h_var_bkg.data(), d_var_bkg, sizeof(float) * npredicted, cudaMemcpyDeviceToHost, *stream));
|
||||
cuda_err(cudaMemcpyAsync(h_ok.data(), d_ok, sizeof(uint8_t) * npredicted, cudaMemcpyDeviceToHost, *stream));
|
||||
// Pass A always fills the box-sum centroid (observed spot position), so copy it back in all modes -
|
||||
// post-refinement uses it as the observed position (beam-centre / distance).
|
||||
@@ -654,6 +658,7 @@ std::vector<Reflection> BraggIntegrationEngineGPU::Run(const ImagePreprocessorBu
|
||||
results[i].I = h_I[i];
|
||||
results[i].sigma = h_sigma[i];
|
||||
results[i].bkg = h_bkg[i];
|
||||
results[i].var_bkg = h_var_bkg[i];
|
||||
results[i].ok = true;
|
||||
if (h_has_obs[i]) {
|
||||
results[i].observed_x = h_obs_x[i];
|
||||
|
||||
Reference in New Issue
Block a user