diff --git a/image_analysis/scale_merge/OutlierBand.h b/image_analysis/scale_merge/OutlierBand.h new file mode 100644 index 000000000..c8cfb0610 --- /dev/null +++ b/image_analysis/scale_merge/OutlierBand.h @@ -0,0 +1,37 @@ +// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute +// SPDX-License-Identifier: GPL-3.0-only + +#pragma once + +// The merge's outlier test (MergeAndStats and its device twin MergeAccumKernel): is a full outside the +// band its reflection's median and the error model allow? Written once for both, so they read the same +// formula. +// +// The error model's systematic term b* is a MULTIPLICATIVE error - absorption, the illuminated +// volume, a scale that is off - and a factor is as likely to be 1/f as f. A band of +-n*sigma about the +// median is symmetric in I instead: with b of a few percent the two agree, but where b is large the +// linear band reaches far below the median and only a little above it. On a strongly absorbing crystal +// (b = 0.34) every full the test removed read 2-7x its median - the least absorbed fulls, the ones +// nearest the true intensity - and none read low, so the merged intensities were cut from above only. +// Here the multiplicative part of the band is exp(+-n*b) about the expected intensity, which is the +// linear band to first order in n*b, and the counting part stays linear. +// up = exp(n*b) - 1 and down = 1 - exp(-n*b) are per-merge constants, taken on the host and handed to +// the device, so that both sides compute them from the same library call. + +#include + +#ifdef __CUDACC__ +#define OUTLIER_BAND_HD __host__ __device__ inline +#else +#define OUTLIER_BAND_HD inline +#endif + +// I: the full's intensity; median: its reflection's outlier-test median; var: the full's variance under +// the error model plus any added variance; b_I: the multiplicative part of that sigma, b * I_expected. +OUTLIER_BAND_HD bool OutsideOutlierBand(double I, double median, double nsigma, double var, double I_expected, + double b_I, double up, double down) { + const double count_var = var - b_I * b_I > 0.0 ? var - b_I * b_I : 0.0; + const double n2c = nsigma * nsigma * count_var; + const double hi = I_expected * up, lo = I_expected * down; + return I - median > sqrt(n2c + hi * hi) || median - I > sqrt(n2c + lo * lo); +} diff --git a/image_analysis/scale_merge/RotationScaleMerge.cpp b/image_analysis/scale_merge/RotationScaleMerge.cpp index 1c9e003a6..12613a3f2 100644 --- a/image_analysis/scale_merge/RotationScaleMerge.cpp +++ b/image_analysis/scale_merge/RotationScaleMerge.cpp @@ -25,6 +25,7 @@ #include "FrenchWilson.h" #include "ResolutionCutoff.h" #include "WilsonOutliers.h" +#include "OutlierBand.h" #include "../../common/CorrelationCoefficient.h" #include "../../common/CrystalLattice.h" #include "../../common/Definitions.h" @@ -4981,6 +4982,9 @@ RotationScaleMerge::Result RotationScaleMerge::MergeAndStats(int n_groups, bool did_gpu_acc = true; } #endif + // The multiplicative part of the outlier band (OutlierBand.h), from the error model as it stands. + const double reject_up = std::expm1(reject_nsigma * error_model_b); + const double reject_down = -std::expm1(-reject_nsigma * error_model_b); if (!did_gpu_acc) for (const auto &o : fulls) { if (!usable_merge(o)) continue; @@ -4994,9 +4998,10 @@ RotationScaleMerge::Result RotationScaleMerge::MergeAndStats(int n_groups, bool if (reject_outliers && error_model_active && std::isfinite(reject_median[o.group])) { // The pooled pair, widened by the shell's measured anomalous variance. const double v_add = reject_var_add.empty() ? 0.0 : reject_var_add[o.group]; - const double tol = reject_nsigma - * std::sqrt(static_cast(sigma_corr) * sigma_corr + v_add); - if (std::fabs(I_corr - reject_median[o.group]) > tol) { + const double I_b = std::isfinite(em_mean[o.group]) ? em_mean[o.group] : I_corr; + if (OutsideOutlierBand(I_corr, reject_median[o.group], reject_nsigma, + static_cast(sigma_corr) * sigma_corr + v_add, I_b, + error_model_b * I_b, reject_up, reject_down)) { ++reject_count; rejected_obs[&o - fulls.data()] = 1; continue; diff --git a/image_analysis/scale_merge/RotationScaleMergeGPU.cu b/image_analysis/scale_merge/RotationScaleMergeGPU.cu index 68536ff9b..4681bbd0f 100644 --- a/image_analysis/scale_merge/RotationScaleMergeGPU.cu +++ b/image_analysis/scale_merge/RotationScaleMergeGPU.cu @@ -2,6 +2,7 @@ // SPDX-License-Identifier: GPL-3.0-only #include "RotationScaleMergeGPU.h" +#include "OutlierBand.h" #include #include @@ -461,6 +462,7 @@ namespace { struct MergeParams { int n_groups; double min_partiality, error_model_a, error_model_b, reject_nsigma; + double reject_up, reject_down; // the multiplicative part of the outlier band (OutlierBand.h) int for_search, error_model_active, reject_outliers; const float *I, *sigma, *corr, *partiality, *d, *reject_median, *var_bkg, *var_per_I, *capture; const float *reject_var_add; // per group: the shell's measured share of dI^2/4 @@ -593,8 +595,9 @@ namespace { ? ModelSigma(i, p, isfinite(p.em_mean[g]) ? p.em_mean[g] : double(I_corr), sigma_raw) : sigma_raw; if (p.reject_outliers && p.error_model_active && isfinite(rmed)) { - const double tol = p.reject_nsigma * sqrt(double(sigma_corr) * sigma_corr + v_add); - if (fabs(double(I_corr) - rmed) > tol) { + const double I_b = isfinite(p.em_mean[g]) ? p.em_mean[g] : double(I_corr); + if (OutsideOutlierBand(I_corr, rmed, p.reject_nsigma, double(sigma_corr) * sigma_corr + v_add, + I_b, p.error_model_b * I_b, p.reject_up, p.reject_down)) { ++rejected; p.rejected_obs[i] = 1; continue; } } @@ -1222,6 +1225,8 @@ void RotationScaleMergeGPU::MergeAccum(double error_model_a, double error_model_ p.error_model_a = error_model_a; p.error_model_b = error_model_b; p.error_model_active = error_model_active ? 1 : 0; p.reject_outliers = reject_outliers ? 1 : 0; p.reject_nsigma = reject_nsigma; + p.reject_up = std::expm1(reject_nsigma * error_model_b); + p.reject_down = -std::expm1(-reject_nsigma * error_model_b); p.reject_median = d.reject_median.get(); p.reject_var_add = d.reject_var_add.get(); p.I = d.f_I.get(); p.sigma = d.f_sigma.get(); p.corr = d.f_corr.get(); p.partiality = d.f_partiality.get();