The r2..r3 background ring was averaged with a 10% SYMMETRIC trimmed mean. A symmetric trim is not a consistent estimator of the mean of a right-skewed (Poisson) sample: on a clean Poisson ring it sits ~0.1 ct/px BELOW the true mean at every level, and with ~50 signal pixels in the r1 disk that under-subtraction adds ~5 counts to every partial on every frame. Measured two independent ways on four rotation datasets - stored background_mean against a plain ring mean over the same pixels on reflection-free frames, and directly on apertures that provably hold no reflection. Empty-aperture pedestal, counts: plain mean -0.03..-0.20, 10% symmetric trim +5.05..+6.34, 4 sigma clip +0.02..+0.54. Replace it with a high-side-only sigma clip at mean + n*sqrt(mean), n = 4 for monochromatic data. It rejects the same one-sided contamination the trim was there for - better, in fact: a 40 px neighbour core at +100 ct shifts the trim by +10.1 ct/px, because a symmetric trim collapses once contamination exceeds ~10% of the ring, versus +0.009 ct/px at 4 sigma. False rejection on a clean ring is 0.04-0.39%. Broadband data keep their tuned 3 sigma clip unchanged. The trim stays reachable with --background-trim for back compatibility; setting either estimator clears the other, so they can never stack. --integrator boxsum does not take the clip (matching what the shipped clip already did), so it now uses the plain ring mean unless --background-trim is given. The intensities get measurably more accurate: per-shell agreement with an independent processing of the same images improves on 14 of 16 crystals (weighted -0.0347, outermost shell 12/4), the outermost-shell R_meas NUMERATOR - absolute scatter, not a denominator effect - falls 13.5% median on 16/5, and CC1/2 in the outer shell improves on 14/7. EXPECT <I/sigma> TO FALL AND EDGE R_meas TO RISE. Both are inflated by information-free counts, so both get worse when the bias is removed; neither is evidence against this change. That fingerprint is exactly how the trimmed mean was accepted in the first place. Known cost: over the 37-crystal rotation battery the de-novo space-group count goes 34 OK / 3 DIFF to 33 / 4. The single regression is a two-lattice crystal whose merge fails the absolute-sanity gate under either background (R_meas 63.5%, CC1/2 72.2%) and which carries an unresolved indexing ambiguity on the very operator being scored, so its operator CC is diluted by construction. No other crystal changes space group, and twin protection is not weakened - the H-ratio veto that refuses genuinely twinned crystals gets MORE decisive (1.63 -> 1.84, 2.83 -> 3.99). Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
142 lines
4.8 KiB
C++
142 lines
4.8 KiB
C++
// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#include <cmath>
|
|
|
|
#include "BraggIntegrationSettings.h"
|
|
#include "JFJochException.h"
|
|
|
|
#define check_max(param, val, max) if ((val) > (max)) throw JFJochException(JFJochExceptionCategory::InputParameterAboveMax, param)
|
|
#define check_min(param, val, min) if ((val) < (min)) throw JFJochException(JFJochExceptionCategory::InputParameterBelowMin, param)
|
|
#define check_finite(param, val) if (!std::isfinite(val)) throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, param)
|
|
|
|
BraggIntegrationSettings &BraggIntegrationSettings::R1(float input) {
|
|
check_finite("Integration radius R1", input);
|
|
check_min("Integration radius R1", input, 0.1);
|
|
check_max("Integration radius R1", input, 20.0);
|
|
r_1 = input;
|
|
return *this;
|
|
}
|
|
|
|
BraggIntegrationSettings &BraggIntegrationSettings::R2(float input) {
|
|
check_finite("Background inner radius R2", input);
|
|
check_min("Background inner radius R2", input, 0.1);
|
|
check_max("Background inner radius R2", input, 30.0);
|
|
|
|
if (input <= r_1)
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"Background inner radius (R2) must be larger than integration radius (R1)");
|
|
|
|
r_2 = input;
|
|
return *this;
|
|
}
|
|
|
|
BraggIntegrationSettings &BraggIntegrationSettings::R3(float input) {
|
|
check_finite("Background outer radius R3", input);
|
|
check_min("Background outer radius R3", input, 0.1);
|
|
check_max("Background outer radius R3", input, 40.0);
|
|
|
|
if (input <= r_2)
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"Background outer radius (R3) must be larger than background inner radius (R2)");
|
|
|
|
r_3 = input;
|
|
return *this;
|
|
}
|
|
|
|
BraggIntegrationSettings &BraggIntegrationSettings::DMinLimit_A(std::optional<float> input) {
|
|
if (input) {
|
|
check_finite("Minimum d-spacing", *input);
|
|
check_min("Minimum d-spacing", *input, 0.5);
|
|
check_max("Minimum d-spacing", *input, 100.0);
|
|
}
|
|
d_min_limit_A = input;
|
|
return *this;
|
|
}
|
|
|
|
BraggIntegrationSettings & BraggIntegrationSettings::FixedProfileRadius_recipA(std::optional<float> input) {
|
|
if (input) {
|
|
check_finite("Profile radius", input.value());
|
|
check_min("Profile radius [A^-1]", input.value(), 0.000001);
|
|
check_max("Profile radius [A^-1]", input.value(), 0.01);
|
|
}
|
|
fixed_profile_radius = input;
|
|
return *this;
|
|
|
|
}
|
|
|
|
std::optional<float> BraggIntegrationSettings::GetFixedProfileRadius_recipA() const {
|
|
return fixed_profile_radius;
|
|
}
|
|
|
|
BraggIntegrationSettings &BraggIntegrationSettings::Integrator(IntegratorMode input) {
|
|
integrator_mode = input;
|
|
return *this;
|
|
}
|
|
|
|
IntegratorMode BraggIntegrationSettings::GetIntegrator() const {
|
|
return integrator_mode;
|
|
}
|
|
|
|
float BraggIntegrationSettings::GetR1() const {
|
|
return r_1;
|
|
}
|
|
|
|
float BraggIntegrationSettings::GetR2() const {
|
|
return r_2;
|
|
}
|
|
|
|
float BraggIntegrationSettings::GetR3() const {
|
|
return r_3;
|
|
}
|
|
|
|
std::optional<float> BraggIntegrationSettings::GetDMinLimit_A() const {
|
|
return d_min_limit_A;
|
|
}
|
|
|
|
float BraggIntegrationSettings::GetMinimumSigmaInRegardsToI() const {
|
|
return minimum_sigma_in_regards_to_i;
|
|
}
|
|
|
|
BraggIntegrationSettings &BraggIntegrationSettings::BackgroundTrimFraction(float input) {
|
|
check_finite("Background trim fraction", input);
|
|
check_min("Background trim fraction", input, 0.0);
|
|
check_max("Background trim fraction", input, 0.49); // must leave a central majority after trimming
|
|
bkg_trim_fraction = input;
|
|
if (input > 0.0f)
|
|
bkg_clip_nsigma = 0.0f; // the two ring estimators are alternatives, not a stack
|
|
return *this;
|
|
}
|
|
|
|
float BraggIntegrationSettings::GetBackgroundTrimFraction() const {
|
|
return bkg_trim_fraction;
|
|
}
|
|
|
|
BraggIntegrationSettings &BraggIntegrationSettings::MaxHKL(std::optional<int> input) {
|
|
if (input) {
|
|
check_min("Maximum hkl index", *input, 1);
|
|
// The GPU predictor launches one thread per candidate, so the cost is (2n+1)^3: 511 is 1.1e9
|
|
// candidates per frame, already far past the point where prediction dominates a run.
|
|
check_max("Maximum hkl index", *input, 511);
|
|
}
|
|
max_hkl = input;
|
|
return *this;
|
|
}
|
|
|
|
std::optional<int> BraggIntegrationSettings::GetMaxHKL() const {
|
|
return max_hkl;
|
|
}
|
|
|
|
BraggIntegrationSettings &BraggIntegrationSettings::BackgroundClipNSigma(float input) {
|
|
check_finite("Background clip nsigma", input);
|
|
check_min("Background clip nsigma", input, 0.0);
|
|
bkg_clip_nsigma = input;
|
|
if (input > 0.0f)
|
|
bkg_trim_fraction = 0.0f; // the two ring estimators are alternatives, not a stack
|
|
return *this;
|
|
}
|
|
|
|
float BraggIntegrationSettings::GetBackgroundClipNSigma() const {
|
|
return bkg_clip_nsigma;
|
|
}
|