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
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>
299 lines
11 KiB
C++
299 lines
11 KiB
C++
// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#include "AzimuthalIntegrationProfile.h"
|
|
#include "JFJochException.h"
|
|
#include "Definitions.h"
|
|
|
|
#include <algorithm>
|
|
|
|
inline float sum_to_count(float sum, uint64_t count) {
|
|
if (count == 0)
|
|
return NAN;
|
|
return sum / (static_cast<float>(count));
|
|
}
|
|
|
|
inline float calc_std(float sum, float sum2, uint64_t count) {
|
|
if (count == 0 || count == 1)
|
|
return NAN;
|
|
const auto fp_count = static_cast<float>(count);
|
|
const float variance = (sum2 - sum * sum / fp_count) / (fp_count - 1);
|
|
// The two sums are floats accumulated over millions of pixels, so on a near-constant ring the
|
|
// difference of two nearly equal large numbers lands either side of zero; clamp before the root
|
|
// rather than emit a NaN standard deviation. Both spot-finder ring accumulators do the same.
|
|
return std::sqrt(std::max(0.0f, variance));
|
|
}
|
|
|
|
AzimuthalIntegrationProfile::AzimuthalIntegrationProfile(const AzimuthalIntegrationMapping &mapping)
|
|
: sum(mapping.GetBinNumber(), 0),
|
|
sum2(mapping.GetBinNumber(), 0),
|
|
count(mapping.GetBinNumber(), 0),
|
|
bin_to_q(mapping.GetBinToQ()),
|
|
bin_to_d(mapping.GetBinToD()),
|
|
bin_to_2theta(mapping.GetBinToTwoTheta()),
|
|
bin_to_phi(mapping.GetBinToPhi()),
|
|
q_bins(mapping.GetQBinCount()),
|
|
azim_bins(mapping.GetAzimuthalBinCount()) {
|
|
}
|
|
|
|
void AzimuthalIntegrationProfile::Clear(const AzimuthalIntegrationMapping &mapping) {
|
|
std::unique_lock ul(m);
|
|
|
|
bin_to_d = mapping.GetBinToD();
|
|
bin_to_q = mapping.GetBinToQ();
|
|
bin_to_2theta = mapping.GetBinToTwoTheta();
|
|
bin_to_phi = mapping.GetBinToPhi();
|
|
q_bins = mapping.GetQBinCount();
|
|
azim_bins = mapping.GetAzimuthalBinCount();
|
|
|
|
sum = std::vector<float>(mapping.GetBinNumber(), 0);
|
|
sum2 = std::vector<float>(mapping.GetBinNumber(), 0);
|
|
count = std::vector<uint64_t>(mapping.GetBinNumber(), 0);
|
|
}
|
|
|
|
void AzimuthalIntegrationProfile::Add(int64_t bin, int64_t value) {
|
|
if (bin < 0 || bin >= sum.size())
|
|
return;
|
|
std::unique_lock ul(m);
|
|
sum[bin] += static_cast<float>(value);
|
|
sum2[bin] += static_cast<float>(value * value);
|
|
count[bin]++;
|
|
}
|
|
|
|
void AzimuthalIntegrationProfile::Add(const std::vector<float> &in_sum,
|
|
const std::vector<float> &in_sum2,
|
|
const std::vector<uint32_t> &in_count) {
|
|
std::unique_lock ul(m);
|
|
if ((in_sum.size() == sum.size()) && (in_count.size() == count.size())) {
|
|
for (int i = 0; i < sum.size(); i++) {
|
|
sum[i] += in_sum[i];
|
|
count[i] += in_count[i];
|
|
}
|
|
if (in_sum2.size() == sum2.size()) {
|
|
for (int i = 0; i < sum.size(); i++) {
|
|
sum2[i] += in_sum2[i];
|
|
}
|
|
}
|
|
} else if (!in_sum.empty() && !in_count.empty())
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Mismatch in size of sum/count datasets");
|
|
}
|
|
|
|
std::vector<float> AzimuthalIntegrationProfile::GetResult() const {
|
|
std::unique_lock ul(m);
|
|
|
|
std::vector<float> rad_int_profile(sum.size(), 0);
|
|
for (int i = 0; i < sum.size(); i++)
|
|
rad_int_profile[i] = sum_to_count(sum[i], count[i]);
|
|
return rad_int_profile;
|
|
}
|
|
|
|
std::vector<float> AzimuthalIntegrationProfile::GetStd() const {
|
|
std::unique_lock ul(m);
|
|
|
|
std::vector<float> rad_int_profile(sum.size(), 0);
|
|
for (int i = 0; i < sum.size(); i++)
|
|
rad_int_profile[i] = calc_std(sum[i], sum2[i], count[i]);
|
|
return rad_int_profile;
|
|
}
|
|
|
|
std::vector<uint64_t> AzimuthalIntegrationProfile::GetPixelCount() const {
|
|
std::unique_lock ul(m);
|
|
return count;
|
|
}
|
|
|
|
std::vector<float> AzimuthalIntegrationProfile::GetResult1D() const {
|
|
std::unique_lock ul(m);
|
|
|
|
std::vector<float> sum_q(q_bins, 0.0f);
|
|
std::vector<uint64_t> count_q(q_bins, 0);
|
|
|
|
for (int i = 0; i < sum.size(); i++) {
|
|
const int q_bin = i % q_bins;
|
|
sum_q[q_bin] += sum[i];
|
|
count_q[q_bin] += count[i];
|
|
}
|
|
|
|
std::vector<float> rad_int_profile(q_bins, 0.0f);
|
|
for (int q = 0; q < q_bins; q++)
|
|
rad_int_profile[q] = sum_to_count(sum_q[q], count_q[q]);
|
|
|
|
return rad_int_profile;
|
|
}
|
|
void AzimuthalIntegrationProfile::SetTitle(const std::string &input) {
|
|
title = input;
|
|
}
|
|
|
|
const std::vector<float> &AzimuthalIntegrationProfile::GetXAxis(PlotAzintUnit unit) const {
|
|
switch (unit) {
|
|
case PlotAzintUnit::TwoTheta_deg:
|
|
return bin_to_2theta;
|
|
case PlotAzintUnit::d_A:
|
|
return bin_to_d;
|
|
default:
|
|
case PlotAzintUnit::Q_recipA:
|
|
return bin_to_q;
|
|
}
|
|
}
|
|
|
|
MultiLinePlot AzimuthalIntegrationProfile::GetPlot(bool force_1d, PlotAzintUnit plot_unit) const {
|
|
MultiLinePlot ret;
|
|
|
|
const std::vector<float> &x_coord = GetXAxis(plot_unit);
|
|
|
|
if (azim_bins == 1)
|
|
ret.AddPlot(MultiLinePlotStruct{.title = title, .x = x_coord, .y = GetResult()});
|
|
else {
|
|
if (force_1d) {
|
|
std::vector<float> x_shortened(q_bins);
|
|
for (int i = 0; i < q_bins; i++)
|
|
x_shortened[i] = x_coord[i];
|
|
ret.AddPlot(MultiLinePlotStruct{.title = title, .x = x_shortened, .y = GetResult1D()});
|
|
} else {
|
|
ret.AddPlot(MultiLinePlotStruct{.title = title, .x = x_coord, .y= bin_to_phi, .z = GetResult()});
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
float AzimuthalIntegrationProfile::GetMeanValueOfBins(uint16_t min_bin, uint16_t max_bin) const {
|
|
std::unique_lock ul(m);
|
|
|
|
float ret_sum = 0;
|
|
uint64_t ret_count = 0;
|
|
|
|
for (int i = 0; i < sum.size(); i++) {
|
|
uint16_t q_bin = i % q_bins;
|
|
if (q_bin >= min_bin && q_bin <= max_bin) {
|
|
ret_sum += sum[i];
|
|
ret_count += count[i];
|
|
}
|
|
}
|
|
|
|
return sum_to_count(ret_sum, ret_count);
|
|
}
|
|
|
|
float AzimuthalIntegrationProfile::GetBkgEstimate(const AzimuthalIntegrationSettings &settings) const {
|
|
auto min_bin = settings.QToBin(settings.GetBkgEstimateLowQ_recipA());
|
|
auto max_bin = settings.QToBin(settings.GetBkgEstimateHighQ_recipA());
|
|
return GetMeanValueOfBins(min_bin, max_bin);
|
|
}
|
|
|
|
float AzimuthalIntegrationProfile::GetIceRingScore(const AzimuthalIntegrationSettings &settings,
|
|
float half_width_q) const {
|
|
return IceRingScore(GetResult1D(), q_bins, settings, half_width_q);
|
|
}
|
|
|
|
float AzimuthalIntegrationProfile::IceRingScore(const std::vector<float> &profile, int32_t q_bins,
|
|
const AzimuthalIntegrationSettings &settings,
|
|
float half_width_q) {
|
|
// Strongest hexagonal-ice ring's intensity relative to the background *under* it (1 = no ice). The
|
|
// background is a smooth whole-profile estimate: a running median of the NON-ice bins, interpolated to
|
|
// each ring position - not a couple of adjacent shoulder bins (the azint binning is coarser than the
|
|
// ring width, so a local shoulder is only ~1 bin and a narrow ratio is noisy and can double-count the
|
|
// ring's own edge). Clean profiles then sit at ~1 at every ring; ice makes the ring bin stand out.
|
|
constexpr float two_pi = 6.283185307f;
|
|
// Average over the azimuthal bins, if there are any; a profile that is already 1-D passes through.
|
|
std::vector<float> prof(std::max(q_bins, 0), 0.0f);
|
|
std::vector<int> nbin(prof.size(), 0);
|
|
for (size_t i = 0; i < profile.size() && !prof.empty(); i++) {
|
|
const size_t q_bin = i % prof.size();
|
|
if (std::isfinite(profile[i])) {
|
|
prof[q_bin] += profile[i];
|
|
nbin[q_bin]++;
|
|
}
|
|
}
|
|
for (size_t i = 0; i < prof.size(); i++)
|
|
prof[i] = nbin[i] ? prof[i] / static_cast<float>(nbin[i]) : NAN;
|
|
const int nq = static_cast<int>(prof.size());
|
|
const float low_q = settings.GetLowQ_recipA();
|
|
const float dq = settings.GetQSpacing_recipA();
|
|
if (nq < 12 || !(dq > 0.0f))
|
|
return 1.0f;
|
|
|
|
auto q_of = [&](int i) { return low_q + (static_cast<float>(i) + 0.5f) * dq; };
|
|
auto on_ice = [&](float q) {
|
|
for (const float d : ICE_RING_RES_A)
|
|
if (std::fabs(q - two_pi / d) < 1.5f * half_width_q)
|
|
return true;
|
|
return false;
|
|
};
|
|
|
|
// Non-ice, finite, positive bins (ascending q) carry the background.
|
|
std::vector<int> base;
|
|
for (int i = 0; i < nq; ++i)
|
|
if (std::isfinite(prof[i]) && prof[i] > 0.0f && !on_ice(q_of(i)))
|
|
base.push_back(i);
|
|
if (base.size() < 8)
|
|
return 1.0f;
|
|
|
|
// Running median over the base bins => a smooth background robust to the ice peaks.
|
|
constexpr int K = 4;
|
|
std::vector<float> base_bg(base.size());
|
|
std::vector<float> window;
|
|
for (int j = 0; j < static_cast<int>(base.size()); ++j) {
|
|
const int lo = std::max(0, j - K);
|
|
const int hi = std::min(static_cast<int>(base.size()), j + K + 1);
|
|
window.clear();
|
|
for (int m = lo; m < hi; ++m)
|
|
window.push_back(prof[base[m]]);
|
|
std::sort(window.begin(), window.end());
|
|
base_bg[j] = window[window.size() / 2];
|
|
}
|
|
|
|
float score = 1.0f;
|
|
for (const float d : ICE_RING_RES_A) {
|
|
const float qr = two_pi / d;
|
|
const int b = static_cast<int>(std::lround((qr - low_q) / dq - 0.5f));
|
|
if (b < 0 || b >= nq || !std::isfinite(prof[b]) || prof[b] <= 0.0f)
|
|
continue;
|
|
// Linear-interpolate the smooth background to the ring position.
|
|
float bg;
|
|
if (qr <= q_of(base.front()))
|
|
bg = base_bg.front();
|
|
else if (qr >= q_of(base.back()))
|
|
bg = base_bg.back();
|
|
else {
|
|
bg = NAN;
|
|
for (int j = 0; j + 1 < static_cast<int>(base.size()); ++j) {
|
|
const float qa = q_of(base[j]), qb = q_of(base[j + 1]);
|
|
if (qa <= qr && qr <= qb) {
|
|
const float t = (qb > qa) ? (qr - qa) / (qb - qa) : 0.0f;
|
|
bg = base_bg[j] + t * (base_bg[j + 1] - base_bg[j]);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (std::isfinite(bg) && bg > 0.0f)
|
|
score = std::max(score, prof[b] / bg);
|
|
}
|
|
return score;
|
|
}
|
|
|
|
AzimuthalIntegrationProfile &AzimuthalIntegrationProfile::operator+=(const AzimuthalIntegrationProfile &other) {
|
|
if ((other.bin_to_q != bin_to_q) || (sum.size() != other.sum.size())) {
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"Error combining two radial integration profiles");
|
|
}
|
|
|
|
for (int i = 0; i < sum.size(); i++) {
|
|
sum[i] += other.sum[i];
|
|
sum2[i] += other.sum2[i];
|
|
count[i] += other.count[i];
|
|
}
|
|
|
|
return *this;
|
|
}
|
|
|
|
void AzimuthalIntegrationProfile::Add(const DeviceOutput &result) {
|
|
std::unique_lock ul(m);
|
|
|
|
if (sum.size() > FPGA_INTEGRATION_BIN_COUNT )
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
|
"Error in getting result from FPGA");
|
|
|
|
for (int i = 0; i < sum.size(); i++) {
|
|
sum[i] += result.integration_result[i].sum;
|
|
count[i] += result.integration_result[i].count;
|
|
}
|
|
}
|