Files
Jungfraujoch/image_analysis/geom_refinement/RingsFromProfile.cpp
T
leonarski_f 538f3504d3
Build Packages / build:windows:nocuda (push) Successful in 20m4s
Build Packages / Unit tests (push) Skipped
Build Packages / build:viewer-tgz:cpu (push) Successful in 16m5s
Build Packages / build:viewer-tgz:cuda (push) Successful in 17m26s
Build Packages / build:rpm (rocky8_nocuda) (push) Successful in 27m46s
Build Packages / build:rpm (rocky9_nocuda) (push) Successful in 20m17s
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Successful in 26m13s
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Successful in 23m17s
Build Packages / build:rpm (rocky8_sls9) (push) Successful in 28m11s
Build Packages / build:rpm (rocky9_sls9) (push) Successful in 19m30s
Build Packages / build:rpm (rocky8) (push) Successful in 24m34s
Build Packages / build:rpm (rocky9) (push) Successful in 21m30s
Build Packages / build:rpm (ubuntu2204) (push) Successful in 23m33s
Build Packages / build:rpm (ubuntu2404) (push) Successful in 20m18s
Build Packages / DIALS test (push) Successful in 18m23s
Build Packages / XDS test (durin plugin) (push) Successful in 11m30s
Build Packages / XDS test (JFJoch plugin) (push) Successful in 10m16s
Build Packages / XDS test (neggia plugin) (push) Successful in 8m2s
Build Packages / Generate python client (push) Successful in 49s
Build Packages / Build documentation (push) Successful in 1m21s
Build Packages / Create release (push) Skipped
Build Packages / build:windows:cuda (push) Successful in 29m45s
v1.0.0.rc-161 (#71)
This is an UNSTABLE release. It includes many experimental features, as well as many AI generated fixes. We recommend using rc.152 for production use.

* **rugnux: significantly better quality of results, and faster.** A large rework of integration, scaling, merging, geometry refinement and space-group determination, together with measurements the program previously made no attempt at - the direct beam before indexing, the beam stop, the goniometer rotation scale, and the stretches of a sweep the crystal did not deliver. A rotation dataset typically gains observations at better <I/sigma> and R_meas, and every `mx` and `scale` run writes a `<prefix>_report.txt` results report modelled on XDS's `CORRECT.LP`. Many defaults moved with it: spot detection is self-calibrating, beam-stop detection and rotation geometry post-refinement are on, resolution limits default to as far as the detector reaches, and ice-ring handling engages only where the crystal is measured to have ice.
* **jfjoch_viewer:** the beam-stop shadow, the detector calibration and the beam-centre measurement are reachable from "Analyze dataset"; the settings panel reports how the sample moved and how polarized the beam was; image rendering and interaction are faster.
* **Performance:** bitshuffle+LZ4 images are decoded on the GPU rather than on the host, with the bitshuffle inverse fused into preprocessing so the decompressed frame is never held in device memory.
* **Broker, writer, packaging and build:** image-slot lifetime and locking fixes, per-image datasets sized by the images actually written, the Debian/Ubuntu broker package renamed to `jfjoch`, and `image_analysis` compiling under MSVC again.

**Breaking change to the rugnux command line:**
* `--azint-only` and `--scale` are **removed**, replaced by `--mode azint` and `--mode scale`; the full pipeline is `--mode mx` and remains the default. A script passing the old flags now fails with the list of valid modes rather than silently running the wrong one.
* `-t`/`--stride` is **refused on rotation data**: skipping frames cuts every reflection's rocking curve, so the combined fulls and their partiality would be measured over frames the sweep never recorded. Select a contiguous range with `-s`/`-e` instead. `--mode azint` and `--force-still` still take a stride.

**Breaking changes to OpenAPI** - regenerate the client (`jfjoch-client` 1.0.0-rc.161, `frontend/src/client`) or read the affected fields as optional:
* `image_scale_b` is removed from the `plot_type` enum, so a client requesting that plot now gets an error rather than a curve.
* `azim_int_settings.high_q_recipA`, `spot_finding_settings.high_resolution_limit` and `spot_finding_settings.low_resolution_limit` are no longer `required`. All three mean "no limit at that end" when unset and are omitted from the response instead of carrying a placeholder value, which raises in a client generated from an rc.160-or-earlier spec. A value of 0 is still accepted and means the same thing.

**Breaking changes to the stored formats** - a consumer reading these fields must treat them as optional:
* The per-image image-scale B factor is no longer computed, so `/entry/MX/imageScaleBFactor` is absent from newly written HDF5 files and the corresponding key is absent from the CBOR DataMessage and END blocks. Files written by rc.160 and earlier still contain it and still open; nothing in the pipeline reads it any more.
* `_reflns.jfjoch_diffrn_ISa` now carries the whole-range `1/sqrt(a*b)` that XDS's ISa denotes, and the error-model `a` and `b` are reported in XDS's convention; the strong-reflection asymptote moves to `_reflns.jfjoch_diffrn_ISa_asymptotic`. **A file written by an earlier version carries the asymptote under the plain `ISa` name.**

Reviewed-on: #71
Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
2026-08-13 17:03:10 +02:00

138 lines
6.8 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include <algorithm>
#include <cmath>
#include "RingsFromProfile.h"
#include "../../common/JFJochMath.h"
namespace {
// Peak position of one ring in one azimuthal sector, in q, or NaN if there is no peak worth using.
//
// The window is narrow and centred on where the ring is expected, so the background under it is close
// to a straight line: take it from the two bins at each end and interpolate. The position itself is the
// intensity-weighted centroid of everything above half the peak height, which is insensitive to the
// exact half-maximum crossing and needs no line-shape assumption - a powder ring is not Gaussian, it is
// the instrumental profile convolved with whatever strain and size broadening the standard has.
float SectorPeakQ(const std::vector<float> &profile, int32_t q_bins, int phi_bin,
int lo_bin, int hi_bin, float low_q, float q_spacing, float min_peak_over_noise) {
const size_t row = static_cast<size_t>(phi_bin) * static_cast<size_t>(q_bins);
const auto value = [&](int i) { return profile[row + static_cast<size_t>(i)]; };
const auto q_of = [&](int i) { return low_q + (static_cast<float>(i) + 0.5f) * q_spacing; };
const float bkg_lo = 0.5f * (value(lo_bin) + value(lo_bin + 1));
const float bkg_hi = 0.5f * (value(hi_bin) + value(hi_bin - 1));
const auto bkg_at = [&](int i) {
const float t = static_cast<float>(i - lo_bin) / static_cast<float>(hi_bin - lo_bin);
return bkg_lo + t * (bkg_hi - bkg_lo);
};
int peak = -1;
float peak_height = 0.0f;
for (int i = lo_bin + 2; i <= hi_bin - 2; ++i) {
const float h = value(i) - bkg_at(i);
if (h > peak_height) { peak_height = h; peak = i; }
}
if (peak < 0)
return NAN;
// Scatter of the background shoulders, as the noise this peak has to stand clear of. A sector with
// no ring in it has a "peak" that is just the largest background fluctuation, and this is what
// rejects it - the alternative, an absolute intensity cut, would need a value per detector and beam.
float s = 0.0f;
int n = 0;
for (int i : {lo_bin, lo_bin + 1, hi_bin - 1, hi_bin}) {
const float r = value(i) - bkg_at(i);
s += r * r;
++n;
}
const float noise = std::sqrt(s / static_cast<float>(n));
if (!(peak_height > min_peak_over_noise * noise))
return NAN;
const float half = 0.5f * peak_height;
double sum_wq = 0.0, sum_w = 0.0;
for (int i = peak; i >= lo_bin && value(i) - bkg_at(i) >= half; --i) {
const double w = value(i) - bkg_at(i);
sum_wq += w * q_of(i);
sum_w += w;
}
for (int i = peak + 1; i <= hi_bin && value(i) - bkg_at(i) >= half; ++i) {
const double w = value(i) - bkg_at(i);
sum_wq += w * q_of(i);
sum_w += w;
}
if (!(sum_w > 0.0))
return NAN;
return static_cast<float>(sum_wq / sum_w);
}
} // namespace
std::vector<RingOptimizerInput> RingsFromAzimuthalProfile(const std::vector<float> &profile,
const AzimuthalIntegrationMapping &mapping,
const DiffractionGeometry &geom,
const std::vector<float> &calibrant_ring_q,
float q_window_recipA,
float min_peak_over_noise) {
std::vector<RingOptimizerInput> out;
const int32_t q_bins = mapping.GetQBinCount();
const int32_t azim_bins = mapping.GetAzimuthalBinCount();
// One azimuthal bin is a plain radial profile: the ring is averaged over every direction at once, so
// nothing remains to say where its centre is. This needs the run to have been integrated with
// azimuthal bins (jfjoch_broker azim_int_settings.azimuthal_bins, rugnux --azim-phi-bins).
if (azim_bins < 4 || q_bins < 8
|| profile.size() != static_cast<size_t>(q_bins) * static_cast<size_t>(azim_bins))
return out;
const auto &settings = mapping.Settings();
const float low_q = settings.GetLowQ_recipA();
const float q_spacing = settings.GetQSpacing_recipA();
const float high_q = low_q + static_cast<float>(q_bins) * q_spacing;
for (size_t i = 0; i < calibrant_ring_q.size(); ++i) {
const float q_ring = calibrant_ring_q[i];
// Never let the window reach into the neighbouring ring. SectorPeakQ takes the background under
// the peak from the two bins at each end of the window, so a window wider than half the gap to
// the next ring measures that ring's flank as this one's background. Hexagonal ice has three
// rings within 0.06 1/A of one another, which the fixed window merges into a single peak.
float window = q_window_recipA;
if (i > 0)
window = std::min(window, 0.5f * (q_ring - calibrant_ring_q[i - 1]));
if (i + 1 < calibrant_ring_q.size())
window = std::min(window, 0.5f * (calibrant_ring_q[i + 1] - q_ring));
if (!(q_ring - window > low_q) || !(q_ring + window < high_q))
continue;
const int window_bins = static_cast<int>(std::lround(window / q_spacing));
const int centre_bin = static_cast<int>((q_ring - low_q) / q_spacing);
const int lo_bin = std::max(0, centre_bin - window_bins);
const int hi_bin = std::min(q_bins - 1, centre_bin + window_bins);
// Two background bins at each end and a peak between them is the least this can work with; a
// ring whose window is narrower than that is not resolved at this q spacing.
if (hi_bin - lo_bin < 6)
continue;
for (int phi_bin = 0; phi_bin < azim_bins; ++phi_bin) {
const float q_obs = SectorPeakQ(profile, q_bins, phi_bin, lo_bin, hi_bin,
low_q, q_spacing, min_peak_over_noise);
if (!std::isfinite(q_obs))
continue;
// The sector's CENTRE, not its lower edge: GetBin() floors phi into the sector, so a bin
// stands for [j, j+1) and taking its edge would rotate every ring point by half a sector -
// which is exactly the cos(phi) signal the beam centre is read from.
const float phi_rad = static_cast<float>((static_cast<double>(phi_bin) + 0.5)
* 2.0 * PI / static_cast<double>(azim_bins));
const auto [x, y] = geom.ResPhiToPxl(static_cast<float>(2.0 * PI) / q_obs, phi_rad);
if (!std::isfinite(x) || !std::isfinite(y))
continue;
out.push_back({x, y, q_ring});
}
}
return out;
}