Files
Jungfraujoch/image_analysis/scale_merge/RotationScaleMerge.h
T
leonarski_f 54c0100e8e
Build Packages / Unit tests (push) Successful in 1h28m28s
Build Packages / build:windows:nocuda (push) Successful in 14m45s
Build Packages / build:windows:cuda (push) Successful in 13m13s
Build Packages / build:viewer-tgz:cpu (push) Successful in 6m47s
Build Packages / build:viewer-tgz:cuda (push) Successful in 7m22s
Build Packages / build:rpm (rocky9_nocuda) (push) Successful in 13m52s
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Successful in 14m16s
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Successful in 13m19s
Build Packages / build:rpm (rocky8_sls9) (push) Successful in 12m50s
Build Packages / build:rpm (rocky9_sls9) (push) Successful in 14m40s
Build Packages / build:rpm (rocky8) (push) Successful in 11m18s
Build Packages / build:rpm (rocky9) (push) Successful in 12m4s
Build Packages / build:rpm (ubuntu2204) (push) Successful in 11m55s
Build Packages / build:rpm (ubuntu2404) (push) Successful in 11m22s
Build Packages / DIALS test (push) Successful in 13m37s
Build Packages / XDS test (durin plugin) (push) Successful in 8m47s
Build Packages / XDS test (JFJoch plugin) (push) Successful in 9m4s
Build Packages / XDS test (neggia plugin) (push) Successful in 7m45s
Build Packages / Generate python client (push) Successful in 34s
Build Packages / Build documentation (push) Successful in 1m4s
Build Packages / Create release (push) Skipped
Build Packages / build:rpm (rocky8_nocuda) (push) Successful in 7m16s
v1.0.0-rc.157 (#67)
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: Rebrand the offline data-processing subsystem as `rugnux` and consolidate all offline analysis into the single `rugnux` binary - `jfjoch_process` is now `rugnux`, the former `jfjoch_azint` is now `rugnux --azint-only`, and `jfjoch_scale` is now `rugnux --scale` (see the new docs/NAMING.md and docs/RUGNUX.md). Scaling and merging are on by default for rotation and stills (`--no-merge` disables them), replacing the previous opt-in `-M, --scale-merge`.
* rugnux: CLI fixes - default `-N` to all hardware threads, parse numeric option arguments strictly (reject non-numeric or trailing input instead of silently yielding 0), require `--wavelength > 0`, and correct the reproduced command line and `--scale` reference-cell handling.
* rugnux: De-novo space-group improvements - recover genuine high symmetry and centred Bravais lattices from intensities, add an automatic CC1/2 high-resolution cutoff, and report L-test twinning statistics.
* rugnux: Index weakly-diffracting low-resolution rotation data that previously failed (e.g. F-cubic crystals that diffract only to ~4 A on a detector reaching ~1.5 A). The per-frame indexing gate now measures the indexed fraction only within the resolution range the lattice actually diffracts to, so the many sub-diffraction ice/noise spots no longer make the fraction floor unreachable; the two-pass first pass tries several image-sampling schemes (spread across the whole rotation vs a consecutive wedge whose native stride keeps a reflection's rocking curve continuous, letting the FFT resolve a long axis) and keeps the one that indexes the most frames; and the de-novo space-group search no longer discards all reflections (and crashes) when every resolution shell falls below <I/sigma> = 1.
* rugnux: Lower the low-resolution R-meas for strongly-diffracting rotation data - drop edge-of-sweep truncated fulls whose rocking curve was captured below `--min-captured-fraction` (default 0.7 for rotation), and report R-meas only over the observations kept by outlier rejection (matching XDS). The 0.7 default also strips the partiality-extrapolated fulls that dominate the intensity second moment on weakly-diffracting crystals, so the de-novo space-group search is no longer starved by the error-model I/sigma floor and recovers the correct symmetry (e.g. the F-cubic Benas crystals: Benas_3 -> F432, Benas_7 -> P6122, instead of P4/P1); on the reference battery every other crystal keeps its space group.
* rugnux: Write the refined geometry (beam, tilt, axis) to _process.h5 and place non-standard mmCIF items under a reserved `jfjoch` prefix.
* jfjoch_broker: Ordinary acquisition failures (receiver/writer/analysis problems, missed packets, writer disconnect) now return to the Idle state with an Error-severity message, so a run can be retried without an expensive re-initialisation; only failures that leave the detector in an undefined state (new JFJochCriticalException, e.g. PCIe/FPGA faults) go to the Error state and force re-initialisation.
* jfjoch_broker: A synchronous /start now reports its failure to the HTTP caller instead of returning HTTP 200, and an incomplete or truncated dataset (missing packets, writer disconnect) is reported as an error rather than a "reduce frame rate" warning.
* jfjoch_broker: Drop uncollected placeholder rows (number = -1) from the scan_result REST endpoint.
* jfjoch_broker: Fix the inverted per-image compression ratio reported by the Lite receiver (was compressed/uncompressed instead of uncompressed/compressed).
* jfjoch_broker: Bragg integration adds a quantization-noise variance floor with a box-sum fallback, and treats the type-maximum marker as an invalid pixel for unsigned image types.
* jfjoch_writer: Detect file-overwrite conflicts at start for back-channel transports, and reset the writer when end-of-collection finalisation fails.
* jfjoch_viewer: Preview overlays follow the geometry (resolution/ROI arcs, true beam centre, predictions, coral secondary-lattice spots, legend), add save-as-JPEG, and fix an HTTP live-follow memory leak.
* Frontend: Improved aesthetics and usability, and added in-browser pixel-mask and JUNGFRAU-pedestal visualisation.
* CI: Name the Windows installer jfjoch-viewer-* instead of jfjoch-*.Reviewed-on: #67

Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
2026-07-11 07:19:11 +02:00

206 lines
12 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <cstdint>
#include <optional>
#include <vector>
#include "../../common/DiffractionExperiment.h"
#include "../../common/Logger.h"
#include "../../common/Reflection.h"
#include "../../common/UnitCell.h"
#include "../IntegrationOutcome.h"
#include "Merge.h" // MergedReflection, MergeStatistics
#ifdef JFJOCH_USE_CUDA
#include <memory>
#include "RotationScaleMergeGPU.h"
#endif
// Dedicated, allocate-once scale+combine+merge for rotation data (the -P rot3d path): recompute the
// per-frame partiality from the (smoothed) mosaicity, robustly fit a per-image scale G, 3D-combine each
// rocking event's partials into fulls, refit a per-frame scale on the fulls (XDS order), and merge with
// a global error model.
//
// The per-frame partial observations are ingested ONCE into flat vectors; the hkl->ASU grouping is
// computed once per space group (by a sort, not a map) and reused across all scaling iterations; every
// hot step is a flat loop over those vectors, so the whole pipeline maps onto CUDA kernels (segmented
// reduction + per-frame solve) and runs GPU-resident when a GPU is present, with the CPU loops as the
// bit-parity fallback. CC1/2 and the per-image CC are computed once at the end, not every iteration.
//
// Used only for the self-scaling rotation case with per-image G (Rotation partiality, a fixed/forced
// mosaicity is honoured by the recompute). It does NOT support B-factor refinement, external-reference
// scaling, an absorption surface or wedge refinement - the caller rejects those combinations. Stills use
// the per-image ScaleOnTheFly (fixed partiality) instead.
class RotationScaleMerge {
public:
struct Result {
std::vector<MergedReflection> merged;
MergeStatistics statistics;
double isa = 0.0; // 1/b of the fitted error model (0 if the model stayed at identity)
};
// experiment: read live (its space group is changed by the caller between Run() calls).
// partial_outcomes: the per-frame partials; the final per-frame scale (G, CC, mosaicity) is written
// back onto them so the offline per-image scaling table is still exported.
// reference_cell: the consensus cell (for the completeness count and the cell-consistency mask).
RotationScaleMerge(const DiffractionExperiment &experiment,
std::vector<IntegrationOutcome> &partial_outcomes,
std::optional<UnitCell> reference_cell,
int scaling_iterations,
float ice_ring_half_width_q,
size_t nthreads,
Logger &logger,
std::string observation_dump_path = {});
// Copy the per-frame partials into the flat buffers. Call once before the first Run().
void Ingest();
// Scale (per-frame G) -> smooth G -> 3D combine -> scale fulls -> merge -> error model -> statistics
// for the space group currently set on the experiment, reusing the ingested buffers.
// for_search: the de-novo P1 pass whose merged intensities feed the space-group search - ice-ring
// reflections are dropped from the merge and the error model (kept otherwise, for completeness).
// masked_ice_rings: rings (indices into ICE_RING_RES_A) to drop from the final merge; empty = none.
Result Run(bool for_search, const std::vector<char> &masked_ice_rings = {});
// Override the high-resolution cut for the next Run() - used to gate the de-novo P1 search pass at
// <I/sigma> >= 1 without cutting the final in-symmetry merge. Reset to the manual limit afterwards.
void SetDMinLimit(std::optional<double> d_min_A) { d_min_limit = d_min_A; }
private:
// One integrated observation - a per-frame partial during scaling/combine, or a combined full during
// scale-fulls/merge. Flat (not nested per image); a POD so the arrays translate straight to CUDA.
struct Obs {
int32_t h, k, l;
float I, sigma, d, rlp, partiality, zeta, delta_phi, bkg;
float image_number; // fractional frame position (for 3D-combine contiguity)
int32_t frame; // index of the outcome whose per-frame scale G applies to this obs
uint8_t on_ice;
float corr; // image_scale_corr (working; updated by scaling)
int32_t group; // dense ASU-group id for the current space group; <0 = never mergeable
};
const DiffractionExperiment &x;
std::vector<IntegrationOutcome> &partials_out; // written back at the end of scaling
std::optional<UnitCell> reference_cell;
size_t nthreads;
Logger &logger;
std::string observation_dump_path;
// Fixed settings snapshot (read once in the ctor).
int n_frames = 0;
double min_partiality = 0.02;
std::optional<double> d_min_limit;
bool merge_friedel = true;
double capture_uncertainty_coeff = 0.0;
double min_captured_fraction = 0.0;
double reject_nsigma = 0.0;
bool reject_outliers = false;
double rfree_fraction = 0.0;
int scaling_iter = 3;
bool scale_fulls = true;
double mosaicity_deg = 0.1;
float ice_half_width_q = 0.0f;
// Automatic high-resolution cutoff for the written reflections + reported shells (post-merge; the
// scaling, combine and error model always run over the full range). Manual d_min_limit wins.
ResolutionCutoffMethod resolution_cutoff_method = ResolutionCutoffMethod::Off;
double resolution_cc_target = 0.30;
int report_shell_count = 10;
// Flat buffers, allocated once by Ingest() and reused across Run() calls.
std::vector<Obs> partials; // all per-frame partials, grouped by frame
std::vector<int32_t> frame_start, frame_count; // CSR ranges of `partials` per frame
std::vector<uint8_t> frame_cell_ok; // per-frame cell-consistency mask (1 = kept)
std::vector<uint8_t> finite_ok; // per-obs AcceptReflection finiteness (immutable; 1 = kept)
std::vector<double> g_partial; // per-frame partial scale G
// Raw-hkl ordering, built ONCE by Ingest and reused: `perm` lists partial indices sorted by
// (raw h,k,l, image_number); each distinct raw hkl is a contiguous run [rawrun_start, +count) of it.
// The expensive sort happens once here, so per-pass combine (event split) and ASU grouping are linear.
std::vector<int32_t> perm;
std::vector<int32_t> rawrun_start, rawrun_count;
std::vector<int32_t> rawrun_h, rawrun_k, rawrun_l;
std::vector<float> rawrun_d; // representative resolution per raw hkl
std::vector<int32_t> rawrun_group; // dense ASU-group id per raw hkl (<0 = absent/out of range)
std::vector<Obs> fulls; // combined fulls (rebuilt each Run), sorted by frame
std::vector<int32_t> fulls_frame_start, fulls_frame_count; // CSR ranges of `fulls` per frame
std::vector<double> g_full; // per-frame scale on the fulls
// Set by FitPerFrameG: which frames were fitted this call (so corr/G is updated only there).
std::vector<uint8_t> frame_scaled_scratch;
// Per-frame mosaicity smoothed in frame order (deterministic); used to recompute partiality and
// written back for the per-image scaling table. Empty if there is no per-frame mosaicity.
std::vector<float> mos_smooth;
// Working per-group arrays (sized to the current group count; reused).
std::vector<int32_t> group_h, group_k, group_l;
#ifdef JFJOCH_USE_CUDA
// GPU engine: the whole hot path (scaling, combine, scale-fulls, per-frame CC, smooth-G, merge +
// error model) runs on the device, resident, when a GPU is present. Null / inactive otherwise, with
// the CPU loops as the bit-parity fallback. Built in Ingest.
std::unique_ptr<RotationScaleMergeGPU> gpu_;
bool gpu_active_ = false;
#endif
// --- helpers (each a flat pass; see the .cpp) ---
// Compute the dense ASU-group id for the current space group by grouping the (pre-sorted) raw-hkl
// runs by their ASU key - one gemmi ASU reduction per distinct raw hkl, not per observation. Fills
// rawrun_group, the group_h/k/l representative tables, and partials[].group; returns the group count.
int ComputeAsuGroups(const HKLKeyGenerator &key_generator);
// Inverse-variance per-group mean of I*corr over `obs` (the merge reference). exclude_ice/masked drop
// those reflections (used for the error-model/merge means, not the scaling reference).
void ReduceGroupMeans(const std::vector<Obs> &obs, int n_groups,
bool exclude_ice, const std::vector<char> &masked_ice_rings,
std::vector<double> &out_mean) const;
// Robust per-frame G fit (IRLS, Cauchy k=3), unity=false uses the rotation partiality, unity=true the
// scale-fulls (partiality already folded in). Reads out_mean[group] as the reference intensity.
void FitPerFrameG(std::vector<Obs> &obs, const std::vector<int32_t> &fstart,
const std::vector<int32_t> &fcount, const std::vector<double> &group_mean_in,
bool unity, std::vector<double> &g);
// corr = rlp / (partiality * G[frame]); leaves corr unchanged for frames that could not be fit.
void UpdateCorr(std::vector<Obs> &obs, const std::vector<double> &g,
const std::vector<uint8_t> &frame_scaled) const;
void SmoothG(std::vector<Obs> &obs, std::vector<double> &g, int window) const;
// The windowed geometric mean of G over frames (the shared first half of SmoothG); the GPU path
// applies the resulting ratio to the resident corr in a kernel instead of the host obs loop.
void ComputeSmoothGWindow(const std::vector<double> &g, int window,
std::vector<double> &g_smooth) const;
// Smooth per-frame mosaicity in frame order and recompute each partial's partiality from it, so the
// per-frame partials of one rocking event tile the curve consistently (they sum toward 1) before the
// 3D combine. Deterministic (frame order); replaces the old arrival-order mosaicity moving average
// that prediction applied. SG-independent, so done once in Ingest.
void SmoothMosaicityAndPartiality();
void Combine(); // partials -> fulls (CPU)
// Sort `fulls` by peak frame and (re)build fulls_frame_start/count (the per-frame CSR the scale-fulls
// step slices). Shared by the CPU Combine tail and the GPU combine path.
void SortFullsByFrame();
// Per-frame CC vs the partial merge reference (CPU; the GPU equivalent is gpu_->ComputePartialCC).
void ComputePerFrameCC(const std::vector<double> &partial_group_mean,
std::vector<double> &cc, std::vector<int64_t> &cc_n) const;
// Write G/CC/mosaicity back onto the partials (once, at the end of partial scaling) from the given
// per-frame cc/cc_n, so the offline per-image scaling table is still exported.
void FinalizePerFrameScale(const std::vector<double> &cc, const std::vector<int64_t> &cc_n,
const std::vector<uint8_t> &frame_scaled);
// Error model + merge + statistics over the fulls (the last stage). n_groups is the fulls group count.
// fulls_resident: the (scaled) fulls + their group CSR are still on the GPU, so the em-stats / samples
// / merge-accumulate / R_meas reductions run there (only per-group + samples come back).
Result MergeAndStats(int n_groups, bool for_search, const std::vector<char> &masked_ice_rings,
bool fulls_resident);
};