Files
Jungfraujoch/image_analysis/scale_merge/ReferenceFreeReindex.h
leonarski_f 7f2ebd8209
Build Packages / Build documentation (push) Successful in 46s
Build Packages / Create release (push) Successful in 8s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 5m25s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 9m46s
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 13m25s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 16m41s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 23m46s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 25m37s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 24m31s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 28m9s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 20m47s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 26m21s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 27m36s
Build Packages / Generate python client (push) Successful in 31s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 29m27s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 31m46s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 25m22s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 30m36s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 31m49s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 23m57s
Build Packages / Unit tests (push) Successful in 1h47m52s
Build Packages / Validation Battery (push) Successful in 44m55s
v1.0.0-rc.184 (#95)
* Rugnux: Rotation processing recovers most of the speed lost since 1.0.0-rc.180; live processing in jfjoch_broker finishes sooner.
* Rugnux: More reliable rotation processing and indexing, including small molecules and supercells.
* Rugnux: More robust serial data and model refinement; molecular replacement handles multi-chain models.
* jfjoch_broker (experimental): Live serial processing keeps the detector geometry fixed for the whole run.
* jfjoch_broker: Pixel sum without an FPGA, a mask bit for inactive pixels, and several stability fixes.
* jfjoch_broker: Rotation indexing and quick integration moved to rugnux live mode; their API settings are deprecated and ignored.
* jfjoch_broker, Rugnux: Azimuthal integration can use logarithmic (log10) Q bins or a given number of bins, set from the API, the web interface and the Rugnux command line.
* jfjoch_viewer: Build and display fixes; the README is rewritten.

Reviewed-on: #95
Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
2026-11-10 22:06:51 +01:00

109 lines
7.4 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <cmath>
#include <array>
#include <cstdint>
#include <set>
#include <vector>
#include "gemmi/symmetry.hpp"
#include "../../common/DiffractionExperiment.h"
#include "../IntegrationOutcome.h"
class ReindexAmbiguityResolver;
// Serial stills: every crystal is indexed in one of the indexings its lattice allows (the identity and
// the twin-law operators of ReindexAmbiguityOperators), at random. Which one each crystal is in is decided
// from its intensities against all the other crystals and its own refined cell (CellMetric), added as
// log-likelihoods; neither locks a crystal: where one cannot tell two indexings apart it adds nothing, and
// where it can, it is weighed against the other. The run's indexing as a whole - which the crystals'
// agreement with each other leaves open - is chosen by the cells and, where one is given, a reference.
// What a crystal's own refined cell says about the indexing it is in. A crystal truly in indexing g has,
// as indexed, the run's cell after the inverse of g, up to the scatter of the refined cells about it. That
// scatter is fitted on the run itself (expectation-maximisation over the indexings, each equally likely),
// so an operator is told apart by the cells only as far as the run's cells differ under it by more than
// one crystal's cell scatters. A cell is compared by its shape - each length after the overall size is
// taken out, since the operators keep the volume and the size carries the distance and wavelength errors
// - and by its angles' distance from 90 degrees, since the monoclinic convention of an obtuse angle is
// taken after indexing, whatever the true indexing. An angle a crystal's refinement held at 90 or 120
// degrees was not measured, and a cell shared exactly by several crystals is the prior that the
// refinement did not move: neither says anything.
struct CellMetric {
UnitCell cell; // the run's
std::array<double, 6> variance{}; // of a crystal's cell about the prediction (log lengths, angles
// in rad), 0 where no crystal measured it
std::set<std::array<int64_t, 6>> priors; // cells more than one crystal has to the last digit
size_t crystals = 0; // crystals the fit used
// Per indexing (as indexed, then ops[g - 1]), the log-likelihood of the crystal's cell; all zero where
// it says nothing.
[[nodiscard]] std::vector<double> LogLikelihood(const CrystalLattice &latt,
const std::vector<gemmi::Op> &ops) const;
};
CellMetric FitCellMetric(const std::vector<IntegrationOutcome> &outcomes, const UnitCell &cell,
const std::vector<gemmi::Op> &ops);
// A crystal's intensities as a log-likelihood of each indexing, from its CC with the others (or with a
// reference) in it, on n reflections: (n - 3) * weight * atanh(cc). Fisher's z of a CC on n reflections has
// a variance of 1 / (n - 3); with z near zeta_right in the right indexing and near zeta_wrong in a wrong
// one, the log-odds of two indexings is (n - 3) (zeta_right - zeta_wrong) (z_g - z_h), so the weight is the
// run's zeta_right - zeta_wrong: the median z of the chosen indexing less the median of the mean z of the others.
double IntensityLogLikelihood(double cc, double n, double weight);
// What a run's resolution leaves for the crystals that come after it (StillsLiveMerge).
struct AmbiguityModel {
CellMetric metric;
double intensity_weight = 0.0; // as in IntensityLogLikelihood
};
struct ReferenceFreeReindexResult {
size_t crystals = 0; // crystals that took part (enough reflections to say anything)
size_t reference_overruled = 0; // with a reference: crystals put into another indexing than their CC
// with the reference alone would
size_t reindexed = 0; // of them, put into an indexing other than the one they came in
double median_sigma = NAN; // per crystal, how far its chosen indexing beats the best other one
// in agreement with the rest of the merge, in sigma; the median
double decided_fraction = NAN; // share of the crystals for which that margin is over 3 sigma
double cc_chosen = NAN; // median CC of a crystal with the merge of all the others, as chosen
double cc_other = NAN; // ... and in the best other indexing
size_t cell_moved = 0; // crystals their cell put into another indexing than their intensities alone
std::vector<double> metric_log_odds; // per operator: median over the crystals of how much better their
// cell fits as indexed than after it (log-likelihood)
AmbiguityModel model;
int sweeps = 0; // passes over the crystals until (almost) none changed (the start kept)
std::vector<int> choice; // per outcome: -1 did not take part, 0 kept, k reindexed by ops[k - 1]
};
// `cell` is the run's cell, `ops` its twin-law operators (identity excluded). The intensities first make the
// crystals consistent with each other (Brehm & Diederichs); the run's indexing as a whole is then the one the
// reference and the cells favour together (where neither says anything, the one that moves the fewest
// crystals); then each crystal takes the indexing its intensities and cell favour, crystal by crystal, until
// (almost) none moves. `reference`: merged intensities in the run's cell and group, or empty. The crystals are
// reindexed in place.
ReferenceFreeReindexResult ResolveIndexingAmbiguity(std::vector<IntegrationOutcome> &outcomes,
const DiffractionExperiment &x, const UnitCell &cell,
const std::vector<gemmi::Op> &ops,
const std::vector<MergedReflection> &reference, size_t nthreads);
// A crystal that comes after a run has been resolved (ResolveIndexingAmbiguity, which gave `model`): put
// into the indexing its CC with the merge of that run (`resolver`) and its own cell together favour. Returns
// the indexing it is put in (0 as indexed, k after ops[k - 1]).
int ReindexOnReference(IntegrationOutcome &outcome, const ReindexAmbiguityResolver &resolver,
const std::vector<gemmi::Op> &ops, const AmbiguityModel &model);
// The run's indexing as a whole, once its crystals were put into theirs in steps (a checkpoint's
// ResolveIndexingAmbiguity, then crystal by crystal with ReindexOnReference): relabelled by the indexing
// the crystals' cells favour together, or, where they say nothing, by the one that leaves the most crystals
// as they were indexed, as ResolveIndexingAmbiguity takes it on the whole run. choice: per outcome, the
// indexing it is in (0 as indexed, k after ops[k - 1], -1 not taking part), updated. Returns the crystals
// moved. Relabelling every crystal by one indexing changes no intensity nor partiality, only the frame the
// merge is reported in.
size_t RelabelRunIndexing(std::vector<IntegrationOutcome> &outcomes, std::vector<int> &choice,
const DiffractionExperiment &x, const std::vector<gemmi::Op> &ops,
const CellMetric &metric);