// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute // SPDX-License-Identifier: GPL-3.0-only #include "IceRingIntensity.h" #include #include #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 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 &reflections, double &intercept, double &slope) { std::vector 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(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 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(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 &merged) { IceRingIntensity out; std::vector 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 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 / _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 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(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(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(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; }