Build Packages / Build documentation (push) Successful in 48s
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 15m47s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 23m19s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 18m47s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 5m54s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 11m27s
Build Packages / Unit tests (push) Successful in 1h50m25s
Build Packages / Validation Battery (push) Successful in 46m38s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 32m52s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 32m32s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 28m34s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 29m19s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 30m11s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 33m58s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 31m26s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 34m4s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 26m59s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 31m50s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 32m17s
Build Packages / Generate python client (push) Successful in 32s
Build Packages / Create release (push) Successful in 9s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 19m38s
* jfjoch_broker (experimental): Live Rugnux covers serial collections from DECTRIS detectors; a rotation collection too large for the image buffer falls back to standard per-image processing with a warning, and a stream that ends without its end message finishes the collection instead of hanging it. * jfjoch_broker (experimental): Live Rugnux settings to build the solvent during refinement and to process a rotation collection as stills, a Clear action for the last result, a `_report.txt` for live rotation runs, R-free reported for the refined model, and ice-ring detection on by default for rotation as in the command line. * jfjoch_broker (experimental): More complete and reproducible live serial merges, analysed for twinning, pseudo-translation and anisotropy, with merging statistics by resolution shell in the live status. * jfjoch_writer: Live results with large models are written reliably; update jfjoch_writer together with jfjoch_broker. * Rugnux: Several masters can be processed together with `--force-still`, including grid scans; more robust and faster merging of several sweeps; improved twinning, pseudo-translation, anisotropy, anomalous-signal and space-group decisions. * Rugnux: Molecular replacement places the copies of a multi-copy solution together; the pseudo-translation warning quotes the Patterson peak and is not raised once a model is placed; the report advises on extending the rotation or reorienting the crystal where the coverage would gain from it. * Web frontend: Light and dark modes, a phone layout, a hideable page list, a redesigned Rugnux live page with step-through of the Fo-Fc peaks in the structure view. * jfjoch_viewer: A broker URL given on the command line follows the running collection. Reviewed-on: #97 Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
166 lines
6.9 KiB
C++
166 lines
6.9 KiB
C++
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#include "IceRingVerdict.h"
|
|
|
|
#include <cmath>
|
|
|
|
#include "../../common/Definitions.h"
|
|
#include "../../common/JFJochMath.h"
|
|
#include "IceRingIntensity.h"
|
|
|
|
namespace {
|
|
struct Moments {
|
|
int n = 0;
|
|
double sum = 0.0, sum2 = 0.0, sum3 = 0.0, sum4 = 0.0;
|
|
void Add(double t) { ++n; sum += t; sum2 += t * t; sum3 += t * t * t; sum4 += t * t * t * t; }
|
|
[[nodiscard]] double Mean() const { return sum / n; }
|
|
[[nodiscard]] double Var() const { return (sum2 - sum * sum / n) / (n - 1); }
|
|
// The variance of ln(Var()): (kurtosis - 1) / n. Wilson intensities are far from Gaussian (an
|
|
// acentric E^2 has kurtosis 9, not 3), so the Gaussian 2 / n would call ordinary scatter excess.
|
|
[[nodiscard]] double VarLogVar() const {
|
|
const double m = Mean();
|
|
const double m2 = sum2 / n - m * m;
|
|
const double m4 = sum4 / n - 4.0 * m * sum3 / n + 6.0 * m * m * sum2 / n - 3.0 * m * m * m * m;
|
|
return (m4 / (m2 * m2) - 1.0) / n;
|
|
}
|
|
};
|
|
|
|
double OneOverD2(float d) { return 1.0 / (static_cast<double>(d) * d); }
|
|
}
|
|
|
|
const char *IceRingZoneName(int zone) {
|
|
switch (zone) {
|
|
case -2: return "low-q outer flank";
|
|
case -1: return "low-q flank";
|
|
case 1: return "high-q flank";
|
|
case 2: return "high-q outer flank";
|
|
default: return "core";
|
|
}
|
|
}
|
|
|
|
const char *IceRingActionName(IceRingAction a) {
|
|
switch (a) {
|
|
case IceRingAction::DownWeight: return "down-weight";
|
|
case IceRingAction::Reject: return "reject";
|
|
default: return "merge";
|
|
}
|
|
}
|
|
|
|
IceRingDecision DecideIceRings(const std::vector<MergedReflection> &merged) {
|
|
IceRingDecision out;
|
|
out.weight_factor.assign(merged.size(), 1.0);
|
|
|
|
std::vector<MergedReflection> off_ring;
|
|
for (const auto &r : merged)
|
|
if (!r.on_ice_ring)
|
|
off_ring.push_back(r);
|
|
|
|
// The ring each flagged reflection belongs to: the nearest in q.
|
|
std::vector<int> ring_of(merged.size(), -1);
|
|
std::vector<double> dq_of(merged.size(), 0.0);
|
|
for (size_t i = 0; i < merged.size(); ++i) {
|
|
if (!merged[i].on_ice_ring || !(merged[i].d > 0.0f)) continue;
|
|
const double q = 2.0 * PI / merged[i].d;
|
|
for (int k = 0; k < static_cast<int>(ICE_RING_RES_A.size()); ++k) {
|
|
const double dq = q - 2.0 * PI / ICE_RING_RES_A[k];
|
|
if (ring_of[i] < 0 || std::fabs(dq) < std::fabs(dq_of[i])) {
|
|
ring_of[i] = k;
|
|
dq_of[i] = dq;
|
|
}
|
|
}
|
|
}
|
|
|
|
for (int k = 0; k < static_cast<int>(ICE_RING_RES_A.size()); ++k) {
|
|
const double s2_ring = OneOverD2(ICE_RING_RES_A[k]);
|
|
bool any = false;
|
|
for (int r : ring_of) any |= r == k;
|
|
if (!any) continue;
|
|
|
|
std::vector<MergedReflection> window;
|
|
for (const auto &r : off_ring)
|
|
if (r.d > 0.0f && std::fabs(OneOverD2(r.d) - s2_ring) < ICE_VERDICT_WINDOW_S2)
|
|
window.push_back(r);
|
|
// The line the bands are read against: through the window's own shells, with no I/sigma cut
|
|
// (a weak low-resolution dataset has no shell that would pass one, and its rings - the ones
|
|
// that reach it - still need judging); flat at the window's mean where even that cannot be
|
|
// fitted.
|
|
double intercept = 0.0, slope = 0.0;
|
|
bool have_line = WilsonLineThrough(window, intercept, slope);
|
|
if (!have_line && !window.empty()) {
|
|
double sum = 0.0;
|
|
for (const auto &r : window) sum += r.I;
|
|
if (sum > 0.0) {
|
|
intercept = std::log(sum / static_cast<double>(window.size()));
|
|
have_line = true;
|
|
}
|
|
}
|
|
|
|
auto t_of = [&](const MergedReflection &r) {
|
|
const double I_exp = std::exp(intercept + slope * OneOverD2(r.d));
|
|
return (r.I - I_exp) / std::sqrt(static_cast<double>(r.sigma) * r.sigma + I_exp * I_exp);
|
|
};
|
|
|
|
Moments nb;
|
|
if (have_line)
|
|
for (const auto &r : window)
|
|
nb.Add(t_of(r));
|
|
|
|
// Each band's reflections are counted whether or not there is a baseline to judge them
|
|
// against, so a band merged for want of one is still reported as such.
|
|
// Five bands: the core and, on each side, an inner and an outer flank. A ring's shoulder reaches
|
|
// past the core into the inner flank; judged together with the outer flank it was diluted into a
|
|
// merge (a 5x shoulder was kept on one crystal), so each flank is judged on its own.
|
|
auto zone_of = [](double dq) {
|
|
return dq < -ICE_VERDICT_FLANK_Q ? 0 : dq < -ICE_VERDICT_CORE_Q ? 1
|
|
: dq <= ICE_VERDICT_CORE_Q ? 2 : dq <= ICE_VERDICT_FLANK_Q ? 3 : 4;
|
|
};
|
|
Moments zone[5];
|
|
int in_zone[5] = {0, 0, 0, 0, 0};
|
|
for (size_t i = 0; i < merged.size(); ++i) {
|
|
if (ring_of[i] != k) continue;
|
|
const int z = zone_of(dq_of[i]);
|
|
++in_zone[z];
|
|
if (nb.n > 0)
|
|
zone[z].Add(t_of(merged[i]));
|
|
}
|
|
|
|
IceRingZoneVerdict v[5];
|
|
for (int z = 0; z < 5; ++z) {
|
|
v[z].ring_d = ICE_RING_RES_A[k];
|
|
v[z].zone = z - 2;
|
|
v[z].n = in_zone[z];
|
|
v[z].n_neighbours = nb.n;
|
|
if (zone[z].n < ICE_VERDICT_MIN_ZONE || nb.n < ICE_VERDICT_MIN_NEIGHBOURS)
|
|
continue;
|
|
v[z].bias_z = (zone[z].Mean() - nb.Mean())
|
|
/ std::sqrt(zone[z].Var() / zone[z].n + nb.Var() / nb.n);
|
|
v[z].var_ratio = zone[z].Var() / nb.Var();
|
|
v[z].var_ratio_z = std::log(v[z].var_ratio) / std::sqrt(zone[z].VarLogVar() + nb.VarLogVar());
|
|
if (std::fabs(v[z].bias_z) > ICE_VERDICT_SIGMA)
|
|
v[z].action = IceRingAction::Reject;
|
|
else if (v[z].var_ratio_z > ICE_VERDICT_SIGMA)
|
|
v[z].action = IceRingAction::DownWeight;
|
|
}
|
|
|
|
for (size_t i = 0; i < merged.size(); ++i) {
|
|
if (ring_of[i] != k) continue;
|
|
const IceRingZoneVerdict &zv = v[zone_of(dq_of[i])];
|
|
if (zv.action == IceRingAction::Reject)
|
|
out.weight_factor[i] = 0.0;
|
|
else if (zv.action == IceRingAction::DownWeight) {
|
|
// The band's excess variance of t, in the reflection's own units.
|
|
const auto &r = merged[i];
|
|
const double I_exp = std::exp(intercept + slope * OneOverD2(r.d));
|
|
const double var = static_cast<double>(r.sigma) * r.sigma;
|
|
const double excess = (zone[zv.zone + 2].Var() - nb.Var()) * (var + I_exp * I_exp);
|
|
out.weight_factor[i] = var / (var + excess);
|
|
}
|
|
}
|
|
for (const auto &zv : v)
|
|
if (zv.n > 0)
|
|
out.zones.push_back(zv);
|
|
}
|
|
return out;
|
|
}
|