Files
Jungfraujoch/image_analysis/scale_merge/IceRingIntensity.cpp
leonarski_f aecd48b9d3
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
v1.0.0-rc.186 (#97)
* 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>
2026-11-13 20:13:43 +01:00

131 lines
5.8 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include "IceRingIntensity.h"
#include <algorithm>
#include <cmath>
#include "../../common/Definitions.h"
#include "../../common/JFJochMath.h"
namespace {
// Hexagonal ice's line strengths at the rings a merge commonly reaches; the rest of ICE_RING_RES_A
// are weak. Following phenix.xtriage's ice-ring table (Zwart, Grosse-Kunstleve & Adams, CCP4
// Newsletter 43, 2005).
constexpr std::array<float, 11> ICE_REL = {1.000f, 0.750f, 0.530f, 0.170f, 0.390f, 0.300f,
0.040f, 0.180f, 0.030f, 0.020f, 0.020f};
struct Point { double s2, I; };
}
bool WilsonLineThrough(const std::vector<MergedReflection> &reflections, double &intercept, double &slope) {
std::vector<Point> base;
base.reserve(reflections.size());
for (const auto &r : reflections)
if (std::isfinite(r.I) && r.d > 0.0f)
base.push_back({1.0 / (static_cast<double>(r.d) * r.d), r.I});
std::sort(base.begin(), base.end(), [](const Point &p, const Point &q) { return p.s2 < q.s2; });
constexpr int NSHELL = 8;
std::vector<double> x, y, w;
for (int i = 0; i < NSHELL; ++i) {
const size_t lo = base.size() * i / NSHELL, hi = base.size() * (i + 1) / NSHELL;
if (hi - lo < 5) continue;
double sx = 0, sI = 0, sI2 = 0;
for (size_t j = lo; j < hi; ++j) { sx += base[j].s2; sI += base[j].I; sI2 += base[j].I * base[j].I; }
const double n = static_cast<double>(hi - lo), m = sI / n, var = std::max(sI2 / n - m * m, 1e-30);
if (m <= 0.0) continue;
x.push_back(sx / n);
y.push_back(std::log(m));
w.push_back(n * m * m / var); // 1 / var(ln mean)
}
if (x.size() < 3) return false;
// Weighted least squares on the shell points.
double sw = 0, swx = 0, swy = 0, swxx = 0, swxy = 0;
for (size_t i = 0; i < x.size(); ++i) {
sw += w[i]; swx += w[i] * x[i]; swy += w[i] * y[i]; swxx += w[i] * x[i] * x[i]; swxy += w[i] * x[i] * y[i];
}
const double det = sw * swxx - swx * swx;
if (!(std::fabs(det) > 0.0)) return false;
slope = (sw * swxy - swx * swy) / det;
intercept = (swy - slope * swx) / sw;
return true;
}
IceRingIntensity MeasureIceRingIntensity(const std::vector<MergedReflection> &merged) {
IceRingIntensity out;
std::vector<double> q;
q.reserve(merged.size());
for (const auto &r : merged)
q.push_back(std::isfinite(r.I) && r.d > 0.0f ? 2.0 * PI / r.d : NAN);
std::vector<char> on_band(merged.size(), 0);
for (size_t i = 0; i < merged.size(); ++i)
if (std::isfinite(q[i]))
for (const float d : ICE_RING_RES_A)
if (std::fabs(q[i] - 2.0 * PI / d) < ICE_INTENSITY_BAND_Q) { on_band[i] = 1; break; }
// Across-ring fits: y_k against x_k = rel_k / <I>_k, weighted by 1 / se_k^2, y_k the core's excess
// over the line and then the core's excess over the flanks.
double sxx = 0.0, sxy = 0.0, cxx = 0.0, cxy = 0.0;
for (size_t k = 0; k < ICE_REL.size(); ++k) {
const double qk = 2.0 * PI / ICE_RING_RES_A[k];
std::vector<MergedReflection> base;
int below = 0, above = 0;
for (size_t i = 0; i < merged.size(); ++i) {
if (!std::isfinite(q[i]) || on_band[i] || std::fabs(q[i] - qk) >= ICE_INTENSITY_WINDOW_Q) continue;
base.push_back(merged[i]);
(q[i] < qk ? below : above)++;
}
if (static_cast<int>(base.size()) < ICE_INTENSITY_MIN_BASE || below < 20 || above < 20) continue;
double a, b;
if (!WilsonLineThrough(base, a, b)) continue;
// The core and the flanks, each reflection as its ratio to the line.
struct Band { double sum = 0.0, sum2 = 0.0, sum_exp = 0.0; int n = 0; } core, flank;
for (size_t i = 0; i < merged.size(); ++i) {
if (!std::isfinite(q[i]) || std::fabs(q[i] - qk) >= ICE_INTENSITY_BAND_Q) continue;
const double I_exp = std::exp(a + b / (static_cast<double>(merged[i].d) * merged[i].d));
const double ratio = merged[i].I / I_exp;
Band &band = std::fabs(q[i] - qk) < ICE_INTENSITY_CORE_Q ? core : flank;
band.sum += ratio; band.sum2 += ratio * ratio; band.sum_exp += I_exp; ++band.n;
}
if (core.n < ICE_INTENSITY_MIN_CORE || flank.n < ICE_INTENSITY_MIN_CORE) continue;
auto mean_se = [](const Band &band, double &mean, double &se) {
mean = band.sum / band.n;
const double var = (band.sum2 - band.sum * band.sum / band.n) / (band.n - 1);
se = std::sqrt(std::max(var, 0.0) / band.n);
};
double mean, se, mean_f, se_f;
mean_se(core, mean, se);
mean_se(flank, mean_f, se_f);
if (!(se > 0.0) || !(se_f > 0.0)) continue;
IceRingIntensityRing ring;
ring.ring_d = ICE_RING_RES_A[k];
ring.rel = ICE_REL[k];
ring.n_core = core.n;
ring.n_flank = flank.n;
ring.n_base = static_cast<int>(base.size());
ring.ratio = mean;
ring.z = (mean - 1.0) / se;
ring.ratio_flank = mean_f;
ring.z_flank = (mean_f - 1.0) / se_f;
ring.mean_expected = core.sum_exp / core.n;
out.rings.push_back(ring);
if (ring.mean_expected > 0.0) {
const double x = ring.rel / ring.mean_expected;
sxx += x * x / (se * se);
sxy += x * (mean - 1.0) / (se * se);
cxx += x * x / (se * se + se_f * se_f);
cxy += x * (mean - mean_f) / (se * se + se_f * se_f);
++out.n_rings;
}
}
if (sxx > 0.0) {
out.amplitude = sxy / sxx;
out.amplitude_z = out.amplitude * std::sqrt(sxx);
out.contrast = cxy / cxx;
out.contrast_z = out.contrast * std::sqrt(cxx);
}
return out;
}