Files
Jungfraujoch/image_analysis/scale_merge/HKLKey.cpp
T
leonarski_fandClaude Opus 5.5 37a8c8e24e Rotation merge: drop rocking events with an overloaded pixel; capture uncertainty in the merge variance
A saturated pixel in a spot means the brightest part of the reflection was
not measured. The integration used to drop the peak frame's partial (its
peak pixel is unreadable) and keep the flanks, so the combine extrapolated
the event from its tails by the partiality model: on a strongly
diffracting small-molecule crystal the strongest low-order reflections
read 2-3x low and were the largest SHELXL misfits. XDS drops such a
reflection (OVERLOAD); so does rugnux now.

- Integration (CPU + GPU engines): a reflection is `overloaded` when a
  signal-disk pixel is saturated, or unreadable on this frame but not in
  the run's pixel mask - EIGER/PILATUS write their error value for a
  pixel they could not count, which the preprocessor turns into a masked
  pixel like a gap's. The engines now receive the PixelMask to tell the
  two apart (an earlier attempt that re-classified the marker as
  saturation in the preprocessor broke a dataset whose gaps are not in
  the file's mask). An overloaded reflection is kept with its box sum,
  unfitted, only so its event can be recognised.
- Rotation combine (CPU + GPU): an event with any overloaded partial is
  dropped whole; counted in the log and the report
  (OBSERVATIONS_REJECTED_OVERLOAD=). The unmerged MTZ export drops it too.
- Everything else that reads reflections leaves an overloaded one out:
  AcceptReflection (stills merge, per-image scaling), the post-refinement
  gather, the axial-row sums.
- Capture uncertainty: the merge rebuilds each full's variance at the
  reflection's mean (counting_variance / ModelSigma) and dropped the
  capture term the combine had put into sigma, so a full extrapolated
  from part of its rocking curve merged at the weight of a whole one.
  Fulls now carry it (Obs::capture) and the rebuilt variance adds
  (capture * <I>)^2, host and device.

SHELXL R1 on rugnux's own integration (harness), median fix -> this:
citric acid .0648 -> .0420 (XDS .051; 221 events dropped, EXTI 1.02 -> 0.29),
HEPES .0396 -> .0381 (184), aspirin 20 keV .0387 -> .0385 (6),
aspirin 25 keV .0376 -> .0375 (5); metformin/nidppe/dnba/lalanine/cytidine
no overloads, unchanged. YAG .116 -> .128 (87 dropped; its scale loop does
not settle either way). Proteins and private subset: see the branch report.
Tests: BraggIntegrationEngineCPU_SaturatedPeakIsFlaggedNotDropped (new),
BraggIntegrationEngineGPU_MatchesCPU (overloaded flag compared),
AcceptReflection_ResolutionLimits, [write_reflections], [large].

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K5K8jvPPbmCrbqnWkddTuB
2026-10-04 21:01:40 +02:00

109 lines
3.5 KiB
C++

// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include <cmath>
#include "HKLKey.h"
#include "gemmi/symmetry.hpp"
uint64_t HKLKey::pack() const {
constexpr int bits = 21;
constexpr int bias = 1 << (bits - 1); // 1,048,576
constexpr int max_value = bias - 1;
constexpr int min_value = -bias;
constexpr std::uint64_t mask = (1ULL << bits) - 1ULL;
if (h < min_value || h > max_value ||
k < min_value || k > max_value ||
l < min_value || l > max_value) {
throw std::out_of_range("HKL index outside packable range");
}
const std::uint64_t hh = static_cast<std::uint64_t>(h + bias) & mask;
const std::uint64_t kk = static_cast<std::uint64_t>(k + bias) & mask;
const std::uint64_t ll = static_cast<std::uint64_t>(l + bias) & mask;
return (hh << 1) | (kk << (bits + 1)) | (ll << (2 * bits + 1)) | (plus ? 1ULL : 0ULL);
}
HKLKeyGenerator::HKLKeyGenerator(bool merge_friedel, const gemmi::SpaceGroup &sg)
: merge_friedel(merge_friedel),
sg(sg),
ops(sg.operations()),
asu(&sg) {
}
HKLKey HKLKeyGenerator::operator()(const MergedReflection &r) const {
return operator()(r.h, r.k, r.l);
}
HKLKey HKLKeyGenerator::operator()(const Reflection &r) const {
return operator()(r.h, r.k, r.l);
}
HKLKey HKLKeyGenerator::operator()(int32_t h, int32_t k, int32_t l) const {
HKLKey key{h, k, l, true};
if (sg.number == 1) {
const HKLKey neg{-h, -k, -l, true};
if (std::tie(key.h, key.k, key.l) < std::tie(neg.h, neg.k, neg.l)) {
key.h = -key.h;
key.k = -key.k;
key.l = -key.l;
key.plus = merge_friedel;
}
} else {
const gemmi::Op::Miller in{h, k, l};
const auto [hkl, sign_plus] = asu.to_asu_sign(in, ops);
key.h = hkl[0];
key.k = hkl[1];
key.l = hkl[2];
key.plus = merge_friedel ? true : sign_plus;
}
return key;
}
bool HKLKeyGenerator::IsSystematicallyAbsent(int32_t h, int32_t k, int32_t l) const {
return ops.is_systematically_absent(gemmi::Op::Miller{h, k, l});
}
bool HKLKeyGenerator::IsSystematicallyAbsent(const Reflection &r) const {
return IsSystematicallyAbsent(r.h, r.k, r.l);
}
bool AcceptReflection(const Reflection &r, std::optional<double> d_min_limit, std::optional<double> d_max_limit) {
if (!std::isfinite(r.I) || r.overloaded) // a saturated spot is not a measurement
return false;
if (!std::isfinite(r.d) || r.d <= 0.0f)
return false;
if (d_min_limit && r.d < d_min_limit)
return false;
if (d_max_limit && r.d > d_max_limit)
return false;
const float corr = r.prescaling_corr * r.qe_corr * r.flight_corr;
if (!std::isfinite(corr) || corr == 0.0f)
return false;
if (!std::isfinite(r.sigma) || r.sigma <= 0.0)
return false;
return true;
}
bool AcceptReflection(const Reflection &r, double d_min_limit, double d_max_limit) {
if (!std::isfinite(r.I) || r.overloaded) // a saturated spot is not a measurement
return false;
if (!std::isfinite(r.d) || r.d <= 0.0f)
return false;
if (d_min_limit > 0.0 && r.d < d_min_limit)
return false;
if (d_max_limit > 0.0 && r.d > d_max_limit)
return false;
const float corr = r.prescaling_corr * r.qe_corr * r.flight_corr;
if (!std::isfinite(corr) || corr == 0.0f)
return false;
if (!std::isfinite(r.sigma) || r.sigma <= 0.0)
return false;
return true;
}