Build Packages / XDS test (JFJoch plugin) (push) Successful in 11m4s
Build Packages / Unit tests (push) Skipped
Build Packages / build:windows:nocuda (push) Successful in 17m46s
Build Packages / build:windows:cuda (push) Successful in 20m20s
Build Packages / build:viewer-tgz:cpu (push) Successful in 15m56s
Build Packages / build:viewer-tgz:cuda (push) Successful in 17m57s
Build Packages / build:rugnux-tgz (x86_64) (push) Successful in 14m10s
Build Packages / build:rugnux:windows (push) Successful in 11m12s
Build Packages / build:rugnux:aarch64 (cross) (push) Successful in 7m14s
Build Packages / build:rpm (rocky8_nocuda) (push) Successful in 22m13s
Build Packages / build:rpm (rocky9_nocuda) (push) Successful in 19m17s
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Successful in 21m21s
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Successful in 17m26s
Build Packages / build:rpm (rocky8_sls9) (push) Successful in 23m56s
Build Packages / build:rpm (rocky9_sls9) (push) Successful in 20m48s
Build Packages / build:rpm (rocky8) (push) Successful in 23m43s
Build Packages / build:rpm (rocky9) (push) Successful in 20m38s
Build Packages / build:rpm (ubuntu2204) (push) Successful in 24m57s
Build Packages / build:rpm (ubuntu2404) (push) Successful in 20m58s
Build Packages / XDS test (durin plugin) (push) Successful in 10m43s
Build Packages / Generate python client (push) Successful in 47s
Build Packages / Build documentation (push) Successful in 1m5s
Build Packages / Create release (push) Skipped
Build Packages / XDS test (neggia plugin) (push) Successful in 8m57s
Build Packages / DIALS test (push) Successful in 18m40s
* `rugnux --model` reports CC(model, data) - the correlation of the merged intensities with the placed, scaled model - by resolution shell, on the same shells as CC1/2, with the reflection count and a significance for each. * `rugnux --model` fits the model's scale, anisotropic B and bulk-solvent parameters on the working reflections only, so the R-free it reports is measured against a model no free reflection helped scale. * The bulk-solvent parameters of `rugnux --model` are searched over their physically meaningful range instead of being fitted without bounds, so a model is never scaled with a solvent term that has silently switched itself off. * The rigid-body placement of `rugnux --model` uses the same bounded bulk solvent as the reported fit, so a model is no longer placed against a target carrying a solvent term with no physical meaning. * `rugnux --model` puts the model into the data's own description of the lattice before placing it, so a model whose cell is written on other axes - I-centred where the run indexed C-centred, a different unique axis, a permuted orthorhombic cell - is placed rather than scored where it was read; `MODEL_CHANGE_OF_BASIS=` and `MODEL_SETTING_AS_READ=` report it when it happens. * The rugnux results report opens with a summary - `VERDICT=` (`OK`, `WARNINGS`, `UNUSABLE`, `FAILED`), `VERDICT_TEXT=`, `PATHOLOGY_FLAGS=` with one closed-vocabulary code per condition that warned, and the `WARNING:` lines, which used to close the file - and the sections after it are renumbered 1-5 with no gaps. * `rugnux --developer` writes the full results report - the pipeline-internal keys and the long explanations the default report now leaves out - and `--finalist-ledger` adds the evidence for every space group the search considered, not only the one it adopted. * The results report warns when the merged data carry no usable signal and when too little of reciprocal space was measured inside the fitted resolution, and omits `FITTED_RESOLUTION` where the CC1/2 curve it is fitted on never falls off. * rugnux detects translational pseudo-symmetry and reports it under the `PSEUDO_TRANSLATION` flag as `TNCS_DETECTED=` and the `TNCS_*` keys - a translation the merged data are exactly invariant under is reported as `UNDECLARED_LATTICE_TRANSLATION=` under `LATTICE_TRANSLATION` instead - and a detected pseudo-translation can no longer buy a false screw axis in the space-group search or hide a twin from the L-test (`L_TEST_VS_TNCS=`). * The space-group search determines glide planes from zonal systematic absences, so a non-Sohncke space group such as P 2_1/c or Pbca is named where the run previously stopped at its Sohncke subgroup; `SOHNCKE_SPACE_GROUP=` carries the best Sohncke group beside it on every run that searched, and a centre of symmetry is never claimed. * Where the cell metric carries more rotational symmetry than the Bravais class the indexer named, the extra rotations are put to the intensities and the space-group search is asked again on the metric's own cell - adopted only where the intensities confirm the higher symmetry - so a lattice that is nearly but not exactly hexagonal, or whose reduction landed in a sub-cell, still reaches its true point group. * Systematic-absence calls rest on the evidence rather than on counts: a screw axis whose absent class the data show extinct is no longer refused because a handful of reflections in it read as present, and `SPACE_GROUP_ALTERNATIVES=` no longer drops a candidate that differs only on a zone the sweep never measured. * A reference correlation measured on too few reflections is refused instead of scored zero, so a run given a reference MTZ is no longer reindexed on an operator that mapped almost everything outside the reference's coverage. * A frame counts as indexed from 6 spots on its lattice rather than 9, so a weakly diffracting crystal whose frames cannot carry 9 is no longer refused the lattice it fits; `--min-indexed-spots` overrides it. * `-C` accepts a known cell in any equivalent description - conventional or primitive, centred or not - instead of only the reduced primitive form, so a centred cell given the way it is published no longer makes the run report that it found no lattice. * Each reflection is corrected for the sensor's quantum efficiency at the angle it meets the detector (attenuation lengths from the NIST tables, which also fixes the spot-width parallax term on CdTe) and for the attenuation of the flight path between the sample and its pixel; `--flight-path air|helium|vacuum` declares the medium - default air, since no file states it - and the report says what was assumed and what it was worth. The unmerged MTZ records the factors in new `QE` and `FLIGHT` columns beside `LP`, so raw counts are `I / LP * QE * FLIGHT`, and `_process.h5` in new optional `qe` and `flight` datasets. * Rotation geometry post-refinement fits the crystal and the detector at once, against the observed spot positions and the observed rocking angles together, so the refined distance depends far less on how wrong the file's distance was. * A coarsely sliced sweep integrates correctly: partials are joined into one rocking event by angle rather than by frame count, so two crossings of the Ewald sphere are no longer summed into one full, and at 0.5 degrees per image or coarser the per-frame geometry refinement accepts a spot whose miss the exposure's own rotation accounts for. * `rugnux --mode scale` reports the detector tilt and direct beam of the geometry it re-scaled at, instead of zeros that read as a flat detector, and no longer warns that no image was indexed on a run whose lattice came from its input file. * Every rotation run that determined a space group and merged reports what the mounting cost: `SPINDLE_LOST_UNIQUE_FRACTION=` is the fraction (0-1) of unique reflections the mounting made unmeasurable under the measured point group, also written to the master as `/entry/MX/spindleLostUniqueFraction` and what the mounting warning fires on; `SPINDLE_SYMMETRY_AXIS_ANGLE_DEG=` / `SPINDLE_SYMMETRY_AXIS_ORDER=` describe the mounting in the `--developer` report. * Stills and grid scans carry a per-image `spindle_blind_fraction` - how much of a rotation sweep's blind cone this orientation would make unrecoverable, 0.5 and above calling for a second orientation - through the CBOR stream, HDF5 (`/entry/MX/spindleBlindFraction`), the plot and scan-result APIs, and the viewer and frontend plots; an absent value means the frame could not be assessed and is not a 0. * The results report's `REPORT_VERSION` is 7. Reviewed-on: #77 Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
329 lines
15 KiB
C++
329 lines
15 KiB
C++
// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#include "../../common/JFJochMath.h"
|
|
#include "PostIndexingRefinement.h"
|
|
|
|
#include <iostream>
|
|
#include <thread>
|
|
|
|
namespace {
|
|
struct config_ifssr final {
|
|
float threshold_contraction = .8; // contract error threshold by this value in every iteration
|
|
float max_distance = .00075; // max distance to reciprocal spots for inliers
|
|
unsigned min_spots = 8; // minimum number of spots to fit against
|
|
unsigned max_iter = 32; // max number of iterations
|
|
};
|
|
|
|
static std::pair<float, float> score_parts(float score) noexcept {
|
|
float nsp = -std::floor(score);
|
|
float s = score + nsp;
|
|
return std::make_pair(nsp - 1, s);
|
|
}
|
|
|
|
struct RefinedCandidate {
|
|
Eigen::Matrix3f cell;
|
|
float score;
|
|
float volume;
|
|
int64_t indexed_spot_count;
|
|
std::vector<uint8_t> indexed_mask;
|
|
};
|
|
|
|
static inline Eigen::MatrixX3<float> CalculateResiduals(
|
|
const Eigen::Ref<const Eigen::MatrixX3<float>> &spots,
|
|
const Eigen::Matrix3f &cell) {
|
|
Eigen::MatrixX3<float> miller = (spots * cell).array().round().matrix();
|
|
Eigen::MatrixX3<float> resid = miller * cell.inverse();
|
|
resid -= spots;
|
|
return resid;
|
|
}
|
|
|
|
static inline std::vector<uint8_t> ComputeIndexedMask(
|
|
const Eigen::Ref<const Eigen::MatrixX3<float>> &spots,
|
|
const Eigen::Matrix3f &cell,
|
|
float indexing_tolerance,
|
|
int64_t &indexed_spot_count) {
|
|
const float indexing_tolerance_sq = indexing_tolerance * indexing_tolerance;
|
|
|
|
// Compute fractional Miller indices. rint (round half to even) rather than round (round half
|
|
// away from zero): without SSE4.1 Eigen has no vector round, so each element is a libm call,
|
|
// while rint is a few inline instructions. Only the SQUARED residual is taken below and the two
|
|
// rules can differ only at an exact .5, where either leaves |frac| = 0.5 - so the mask and the
|
|
// count are the same. The refinement loop above keeps round: there the rounded value IS the
|
|
// Miller index that goes into the residual and the QR solve, so its tie rule does matter.
|
|
Eigen::MatrixX3<float> miller_frac = spots * cell;
|
|
Eigen::MatrixX3<float> miller_int = miller_frac.array().rint().matrix();
|
|
Eigen::MatrixX3<float> frac_resid = miller_frac - miller_int;
|
|
|
|
std::vector<uint8_t> mask(spots.rows(), 0);
|
|
indexed_spot_count = 0;
|
|
|
|
for (int i = 0; i < spots.rows(); ++i) {
|
|
if (frac_resid.row(i).squaredNorm() < indexing_tolerance_sq) {
|
|
mask[i] = 1;
|
|
indexed_spot_count++;
|
|
}
|
|
}
|
|
|
|
return mask;
|
|
}
|
|
|
|
template<typename MatX3, typename VecX>
|
|
static void RefineCandidateCells(const Eigen::Ref<const Eigen::MatrixX3<float>> &spots,
|
|
Eigen::DenseBase<MatX3> &cells,
|
|
Eigen::DenseBase<VecX> &scores,
|
|
const config_ifssr &cifssr,
|
|
unsigned block = 0, unsigned nblocks = 1) {
|
|
using namespace Eigen;
|
|
using Mx3 = MatrixX3<float>;
|
|
using M3 = Matrix3<float>;
|
|
|
|
const unsigned nspots = spots.rows();
|
|
const unsigned ncells = scores.rows();
|
|
VectorX<bool> below{nspots};
|
|
MatrixX3<bool> sel{nspots, 3u};
|
|
Mx3 resid{nspots, 3u};
|
|
Mx3 miller{nspots, 3u};
|
|
M3 cell;
|
|
|
|
const unsigned blocksize = (ncells + nblocks - 1u) / nblocks;
|
|
const unsigned startcell = block * blocksize;
|
|
const unsigned endcell = std::min(startcell + blocksize, ncells);
|
|
|
|
for (unsigned j = startcell; j < endcell; j++) {
|
|
if (nspots < cifssr.min_spots) {
|
|
scores(j) = float{1.};
|
|
continue;
|
|
}
|
|
|
|
cell = cells.block(3u * j, 0u, 3u, 3u).transpose(); // cell: col vectors
|
|
const float scale = cell.colwise().norm().minCoeff();
|
|
float threshold = score_parts(scores[j]).second / scale;
|
|
|
|
for (unsigned niter = 1; niter < cifssr.max_iter && threshold > cifssr.max_distance; niter++) {
|
|
miller = (spots * cell).array().round().matrix();
|
|
resid = miller * cell.inverse();
|
|
resid -= spots;
|
|
|
|
below = (resid.rowwise().norm().array() < threshold);
|
|
if (below.count() < cifssr.min_spots)
|
|
break;
|
|
|
|
threshold *= cifssr.threshold_contraction;
|
|
sel.colwise() = below;
|
|
HouseholderQR<Mx3> qr{sel.select(spots, .0f)};
|
|
cell = qr.solve(sel.select(miller, .0f));
|
|
}
|
|
|
|
resid = CalculateResiduals(spots, cell);
|
|
|
|
ArrayX<float> dist = resid.rowwise().norm();
|
|
auto nth = std::begin(dist) + (cifssr.min_spots - 1);
|
|
std::nth_element(std::begin(dist), nth, std::end(dist));
|
|
scores(j) = *nth;
|
|
|
|
cells.block(3u * j, 0u, 3u, 3u) = cell.transpose();
|
|
}
|
|
}
|
|
}
|
|
|
|
std::vector<CrystalLattice> Refine(const std::vector<Coord> &in_spots,
|
|
size_t nspots,
|
|
Eigen::MatrixX3<float> &oCell,
|
|
Eigen::VectorX<float> &scores,
|
|
RefineParameters &p) {
|
|
std::vector<CrystalLattice> ret;
|
|
|
|
Eigen::MatrixX3<float> spots(in_spots.size(), 3u);
|
|
|
|
for (int i = 0; i < in_spots.size(); i++) {
|
|
spots(i, 0u) = in_spots[i].x;
|
|
spots(i, 1u) = in_spots[i].y;
|
|
spots(i, 2u) = in_spots[i].z;
|
|
}
|
|
|
|
config_ifssr cifssr{
|
|
.min_spots = static_cast<uint32_t>(p.viable_cell_min_spots)
|
|
};
|
|
|
|
// Candidate cells refine independently - a block touches only its own scores(j) and cells rows, and
|
|
// holds its own scratch - so splitting them across threads gives the same numbers as one thread.
|
|
// Only worth it where few indexer threads run (the rotation first pass uses two, one per scheme,
|
|
// and leaves the rest of the machine idle); refine_threads stays 1 everywhere else.
|
|
const unsigned ncells = static_cast<unsigned>(scores.rows());
|
|
const unsigned nblocks = std::max(1u, std::min(p.refine_threads, ncells));
|
|
if (nblocks == 1) {
|
|
RefineCandidateCells(spots.topRows(nspots), oCell, scores, cifssr);
|
|
} else {
|
|
std::vector<std::thread> workers;
|
|
workers.reserve(nblocks - 1);
|
|
for (unsigned b = 1; b < nblocks; b++)
|
|
workers.emplace_back([&, b] {
|
|
RefineCandidateCells(spots.topRows(nspots), oCell, scores, cifssr, b, nblocks);
|
|
});
|
|
RefineCandidateCells(spots.topRows(nspots), oCell, scores, cifssr, 0, nblocks);
|
|
for (auto &w : workers)
|
|
w.join();
|
|
}
|
|
|
|
std::vector<RefinedCandidate> candidates;
|
|
|
|
// Angle bounds as cosines, once, for the per-candidate test below.
|
|
const float cos_min_angle = std::cos(p.min_angle_deg * PI / 180.0f);
|
|
const float cos_max_angle = std::cos(p.max_angle_deg * PI / 180.0f);
|
|
|
|
// A reference cell is typed the way a deposit or a paper states it, which for a centred lattice
|
|
// is the CONVENTIONAL cell - and the candidates it is compared against are Niggli-reduced
|
|
// PRIMITIVE cells, whose edges a C, I, F or R description does not have. Compared as typed, a
|
|
// centred reference therefore rejects the true candidate: it survives only where the transform
|
|
// happened to build the centred cell itself as well - a cell of a sublattice, on which the run
|
|
// then proceeds - and otherwise no lattice is returned at all, so the user who quotes the deposit
|
|
// is exactly the one the option fails. The reference is therefore expanded into the primitive
|
|
// lattices its six numbers could stand for, one per centring, each reduced the way a candidate
|
|
// is; a candidate matching any of them is kept. The cell as typed stays in the set because
|
|
// ffbidx hands back the basis it was given, unreduced.
|
|
std::vector<UnitCell> reference_cells;
|
|
if (p.reference_unit_cell) {
|
|
reference_cells.push_back(*p.reference_unit_cell);
|
|
const CrystalLattice reference(*p.reference_unit_cell);
|
|
for (const char centering: {'A', 'B', 'C', 'I', 'F', 'R'})
|
|
reference_cells.push_back(reference.ToPrimitive(centering).NiggliReduce().GetUnitCell());
|
|
reference_cells.push_back(reference.NiggliReduce().GetUnitCell());
|
|
}
|
|
|
|
for (int i = 0; i < scores.size(); i++) {
|
|
Eigen::Matrix3f cell_rows = oCell.block(3u * i, 0u, 3u, 3u);
|
|
Eigen::Matrix3f cell_cols = cell_rows.transpose();
|
|
Eigen::Vector3f row_norms = cell_rows.rowwise().norm();
|
|
|
|
if (!reference_cells.empty()) {
|
|
std::array<float, 3> obs = {row_norms(0), row_norms(1), row_norms(2)};
|
|
std::sort(obs.begin(), obs.end());
|
|
|
|
// The angles are compared as well as the lengths. Each is folded to its acute complement
|
|
// (min(x,180-x)) so the obtuse/acute setting choice is irrelevant, then the sorted
|
|
// triples are compared. Guards against a right-edges/wrong-angle cell (a pseudo-symmetric
|
|
// near-metric, e.g. a monoclinic beta refined to the wrong value) passing on lengths.
|
|
auto fold = [](float deg) { return std::min(deg, 180.0f - deg); };
|
|
auto row_angle = [&](int i, int j) {
|
|
return std::acos(std::clamp(cell_rows.row(i).normalized().dot(cell_rows.row(j).normalized()),
|
|
-1.0f, 1.0f)) * 180.0f / PI;
|
|
};
|
|
std::array<float, 3> obs_ang = {fold(row_angle(1, 2)), fold(row_angle(0, 2)), fold(row_angle(0, 1))};
|
|
std::sort(obs_ang.begin(), obs_ang.end());
|
|
|
|
auto matches = [&](const UnitCell &reference) {
|
|
std::array<float, 3> ref = {reference.a, reference.b, reference.c};
|
|
std::sort(ref.begin(), ref.end());
|
|
for (int k = 0; k < 3; ++k) {
|
|
const float denom = std::max(ref[k], REFINE_MIN_REFERENCE_LENGTH_EPSILON);
|
|
if (std::abs(obs[k] - ref[k]) / denom > p.dist_tolerance_vs_reference)
|
|
return false;
|
|
}
|
|
|
|
std::array<float, 3> ref_ang = {fold(reference.alpha), fold(reference.beta), fold(reference.gamma)};
|
|
std::sort(ref_ang.begin(), ref_ang.end());
|
|
for (int k = 0; k < 3; ++k) {
|
|
if (std::abs(obs_ang[k] - ref_ang[k]) > REFINE_ANGLE_TOLERANCE_VS_REFERENCE_DEG)
|
|
return false;
|
|
}
|
|
return true;
|
|
};
|
|
|
|
if (std::none_of(reference_cells.begin(), reference_cells.end(), matches))
|
|
continue;
|
|
} else {
|
|
if (row_norms.minCoeff() < p.min_length_A || row_norms.maxCoeff() > p.max_length_A)
|
|
continue;
|
|
}
|
|
|
|
// Filter for wrong angles. Compared as COSINES, not angles: acos is strictly decreasing on
|
|
// [-1, 1], so "angle outside [min_angle, max_angle]" is exactly "cosine outside
|
|
// [cos(max_angle), cos(min_angle)]" with the ends swapped - and the three acos calls the
|
|
// comparison needed disappear. They were not cheap: this runs per candidate cell per image,
|
|
// and on a serial-stills run acos was 41% of the whole process.
|
|
const float cos_alpha = cell_rows.row(1).normalized().dot(cell_rows.row(2).normalized());
|
|
const float cos_beta = cell_rows.row(0).normalized().dot(cell_rows.row(2).normalized());
|
|
const float cos_gamma = cell_rows.row(0).normalized().dot(cell_rows.row(1).normalized());
|
|
|
|
if (cos_alpha > cos_min_angle || cos_alpha < cos_max_angle ||
|
|
cos_beta > cos_min_angle || cos_beta < cos_max_angle ||
|
|
cos_gamma > cos_min_angle || cos_gamma < cos_max_angle)
|
|
continue;
|
|
|
|
int64_t indexed_spot_count = 0;
|
|
auto indexed_mask = ComputeIndexedMask(spots.topRows(nspots), cell_cols, p.indexing_tolerance, indexed_spot_count);
|
|
|
|
if (indexed_spot_count < p.viable_cell_min_spots)
|
|
continue;
|
|
|
|
candidates.emplace_back(RefinedCandidate{
|
|
.cell = cell_rows,
|
|
.score = scores(i),
|
|
.volume = std::abs(cell_rows.determinant()),
|
|
.indexed_spot_count = indexed_spot_count,
|
|
.indexed_mask = std::move(indexed_mask)
|
|
});
|
|
}
|
|
|
|
std::sort(candidates.begin(), candidates.end(),
|
|
[](const RefinedCandidate &a, const RefinedCandidate &b) {
|
|
const auto max_spots = std::max(a.indexed_spot_count, b.indexed_spot_count);
|
|
const auto min_spots = std::min(a.indexed_spot_count, b.indexed_spot_count);
|
|
const bool spot_counts_close = (max_spots > 0)
|
|
&& (static_cast<float>(min_spots) / static_cast<float>(max_spots)
|
|
>= REFINE_CANDIDATE_SPOT_COUNT_RATIO_THRESHOLD);
|
|
|
|
if (!spot_counts_close)
|
|
return a.indexed_spot_count > b.indexed_spot_count;
|
|
|
|
const float max_volume = std::max(a.volume, b.volume);
|
|
const float min_volume = std::max(std::min(a.volume, b.volume), REFINE_MIN_VOLUME_EPSILON);
|
|
const bool volume_differs = (max_volume / min_volume) > REFINE_CANDIDATE_VOLUME_RATIO_THRESHOLD;
|
|
|
|
if (volume_differs)
|
|
return a.volume < b.volume;
|
|
|
|
if (a.score != b.score)
|
|
return a.score < b.score;
|
|
|
|
return a.indexed_spot_count > b.indexed_spot_count;
|
|
});
|
|
|
|
std::vector<RefinedCandidate> accepted;
|
|
|
|
for (const auto &candidate: candidates) {
|
|
int64_t overlap = 0;
|
|
|
|
// Check all already selected lattices and see how many spots are already indexed for the candidate
|
|
// If the overlap is more than 40% of indexed spots - we assume the lattice doesn't bring anything new
|
|
for (const auto &selected: accepted) {
|
|
for (size_t i = 0; i < candidate.indexed_mask.size(); ++i) {
|
|
if (candidate.indexed_mask[i] && selected.indexed_mask[i])
|
|
overlap++;
|
|
}
|
|
}
|
|
|
|
if (overlap < static_cast<int64_t>(REFINE_CANDIDATE_OVERLAP_RATIO_THRESHOLD
|
|
* static_cast<float>(candidate.indexed_spot_count))) {
|
|
accepted.emplace_back(candidate);
|
|
}
|
|
}
|
|
|
|
ret.reserve(accepted.size());
|
|
|
|
for (auto &candidate: accepted) {
|
|
auto cell = candidate.cell;
|
|
if (cell.determinant() < .0f)
|
|
cell = -cell;
|
|
|
|
ret.emplace_back(
|
|
Coord(cell(0, 0), cell(0, 1), cell(0, 2)),
|
|
Coord(cell(1, 0), cell(1, 1), cell(1, 2)),
|
|
Coord(cell(2, 0), cell(2, 1), cell(2, 2))
|
|
);
|
|
}
|
|
|
|
return ret;
|
|
}
|