Files
Jungfraujoch/tests/LePageLatticeTest.cpp
T
leonarski_f a39fd29f77
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
v1.0.0-rc.167 (#77)
* `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>
2026-09-09 07:25:13 +02:00

210 lines
11 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include <catch2/catch_all.hpp>
#include "../common/CrystalLattice.h"
#include "../common/Coord.h"
#include "../common/UnitCell.h"
#include "../image_analysis/lattice_search/LePageLattice.h"
#include "gemmi/symmetry.hpp"
#include <array>
#include <cmath>
#include <random>
namespace {
int CentringMultiplicity(char c) {
switch (c) {
case 'A': case 'B': case 'C': case 'I': return 2;
case 'R': return 3;
case 'F': return 4;
default: return 1;
}
}
// The lattice is what has to come back, not the axes: a conventional cell of the right class whose
// PRIMITIVE volume is the one we started from. Comparing lengths element-wise would fail a correct
// answer given in another setting.
void CheckLattice(const std::optional<LePageResult> &r, gemmi::CrystalSystem system, char centering,
float primitive_volume) {
REQUIRE(r.has_value());
CHECK(r->system == system);
CHECK(r->centering == centering);
const float v = std::fabs(r->conventional.CalcVolume()) / CentringMultiplicity(r->centering);
CHECK(v == Catch::Approx(primitive_volume).epsilon(0.02));
}
// The Miller-index matrix a filter is handed, against the integer matrix expected there.
bool SameMatrix(const gemmi::Mat33 &m, const std::array<int, 9> &expected) {
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (std::fabs(m[i][j] - expected[3 * i + j]) > 1e-6)
return false;
return true;
}
// The lattice as an indexer hands it over: some basis of it, in some orientation, with noise.
CrystalLattice Present(const CrystalLattice &L, std::mt19937 &rng, float noise_A) {
std::uniform_int_distribution<int> pick(0, 2), amount(-1, 1);
std::uniform_real_distribution<float> uni(0, 1);
std::normal_distribution<float> gauss(0, noise_A);
gemmi::Mat33 m(1, 0, 0, 0, 1, 0, 0, 0, 1);
for (int n = 0; n < 4; n++) {
const int i = pick(rng), j = pick(rng);
if (i == j)
continue;
gemmi::Mat33 shear(1, 0, 0, 0, 1, 0, 0, 0, 1);
shear.a[i][j] = amount(rng);
m = shear.multiply(m);
}
const float theta = 2 * (float)M_PI * uni(rng), phi = std::acos(2 * uni(rng) - 1);
const Coord axis(std::sin(phi) * std::cos(theta), std::sin(phi) * std::sin(theta), std::cos(phi));
CrystalLattice out = L.Multiply(m).Multiply(RotMatrix(2 * (float)M_PI * uni(rng), axis));
Coord v[3] = {out.Vec0(), out.Vec1(), out.Vec2()};
for (auto &k : v) { k.x += gauss(rng); k.y += gauss(rng); k.z += gauss(rng); }
return CrystalLattice(v[0], v[1], v[2]);
}
} // namespace
TEST_CASE("LePageLattice - the fourteen Bravais lattices") {
struct Case { gemmi::CrystalSystem system; char centering; float a, b, c, al, be, ga; };
const Case cases[] = {
{gemmi::CrystalSystem::Triclinic, 'P', 23, 31, 41, 81, 95, 71},
{gemmi::CrystalSystem::Monoclinic, 'P', 31, 43, 57, 90, 103, 90},
{gemmi::CrystalSystem::Monoclinic, 'C', 91, 43, 57, 90, 103, 90},
{gemmi::CrystalSystem::Orthorhombic, 'P', 31, 43, 57, 90, 90, 90},
{gemmi::CrystalSystem::Orthorhombic, 'C', 31, 43, 57, 90, 90, 90},
{gemmi::CrystalSystem::Orthorhombic, 'I', 31, 43, 57, 90, 90, 90},
{gemmi::CrystalSystem::Orthorhombic, 'F', 31, 43, 57, 90, 90, 90},
{gemmi::CrystalSystem::Tetragonal, 'P', 47, 47, 71, 90, 90, 90},
{gemmi::CrystalSystem::Tetragonal, 'I', 47, 47, 71, 90, 90, 90},
{gemmi::CrystalSystem::Trigonal, 'R', 61, 61, 133, 90, 90, 120},
{gemmi::CrystalSystem::Hexagonal, 'P', 61, 61, 97, 90, 90, 120},
{gemmi::CrystalSystem::Cubic, 'P', 71, 71, 71, 90, 90, 90},
{gemmi::CrystalSystem::Cubic, 'I', 71, 71, 71, 90, 90, 90},
{gemmi::CrystalSystem::Cubic, 'F', 71, 71, 71, 90, 90, 90},
};
for (const Case &c : cases) {
const CrystalLattice conventional(c.a, c.b, c.c, c.al, c.be, c.ga);
const CrystalLattice primitive = conventional.ToPrimitive(c.centering);
const float primitive_volume = std::fabs(primitive.CalcVolume());
std::mt19937 rng(20260831);
for (int i = 0; i < 20; i++) {
INFO("class " << (int)c.system << c.centering << " presentation " << i);
CheckLattice(LePageLattice(Present(primitive, rng, 0.02f)), c.system, c.centering,
primitive_volume);
}
}
}
TEST_CASE("LePageLattice - a cubic F lattice on the Niggli type boundary") {
// An fcc lattice has both a 60/60/60 and a ~120/90/120 shortest-vector basis, so it sits ON the
// boundary between the two Niggli types by construction and the reduction lands on either side
// according to the last bits of the cell it is given. Reading the symmetry off the metric has no
// forms to fall between, so the answer does not depend on which side it landed on.
const CrystalLattice conventional(121.0f * std::sqrt(2.0f), 121.0f * std::sqrt(2.0f),
121.0f * std::sqrt(2.0f), 90, 90, 90);
const CrystalLattice primitive = conventional.ToPrimitive('F');
const float primitive_volume = std::fabs(primitive.CalcVolume());
std::mt19937 rng(7);
for (int i = 0; i < 40; i++) {
INFO("presentation " << i);
CheckLattice(LePageLattice(Present(primitive, rng, 0.1f)), gemmi::CrystalSystem::Cubic, 'F',
primitive_volume);
}
}
TEST_CASE("LePageLattice - a tetragonal I description of a cubic F lattice") {
// Same lattice as above, handed over in the setting a, a, a*sqrt(2) that describes it as body-
// centred tetragonal. Both descriptions are the same lattice, and the answer has to be the same.
const float a = 120.5f;
const CrystalLattice tetragonal(a, a, a * std::sqrt(2.0f), 90, 90, 90);
const CrystalLattice primitive = tetragonal.ToPrimitive('I');
auto r = LePageLattice(primitive);
REQUIRE(r.has_value());
CHECK(r->system == gemmi::CrystalSystem::Cubic);
CHECK(r->centering == 'F');
CHECK(std::fabs(r->conventional.CalcVolume()) / 4 ==
Catch::Approx(std::fabs(primitive.CalcVolume())).epsilon(0.01));
}
TEST_CASE("LePageLattice - a monoclinic lattice is named C even where I is less oblique") {
// The least oblique naming of this lattice's (a, c) plane is body-centred, beta a few degrees
// from 90. The answer must still be the C-centred reference setting - the space-group search
// enumerates reference settings only, so a promotion earned by a lattice named I can never be
// confirmed. And it must be the least oblique C the plane offers, not a relabel: on this
// lattice a -> a + c puts beta past the 150-degree bound of the constrained refinement.
const CrystalLattice conventional(40, 54, 76, 90, 93, 90); // the I naming of the lattice
const CrystalLattice primitive = conventional.ToPrimitive('I');
const float primitive_volume = std::fabs(primitive.CalcVolume());
std::mt19937 rng(20260908);
for (int i = 0; i < 20; i++) {
INFO("presentation " << i);
const auto r = LePageLattice(Present(primitive, rng, 0.02f));
CheckLattice(r, gemmi::CrystalSystem::Monoclinic, 'C', primitive_volume);
const UnitCell uc = r->conventional.GetUnitCell();
CHECK(uc.beta > 90.0f);
CHECK(uc.beta < 150.0f);
}
}
TEST_CASE("LePageLattice - the change of basis is integral and right-handed") {
const CrystalLattice conventional(47, 47, 71, 90, 90, 90);
const CrystalLattice primitive = conventional.ToPrimitive('I');
auto r = LePageLattice(primitive);
REQUIRE(r.has_value());
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
CHECK(r->reindex[i][j] == Catch::Approx(std::round(r->reindex[i][j])).margin(1e-9));
CHECK(r->reindex.determinant() > 0);
CHECK(r->conventional.CalcVolume() > 0);
}
TEST_CASE("LePageLattice - a pseudo-symmetric metric is not promoted") {
// A monoclinic cell whose beta sits a few degrees from 90 is not orthorhombic, however close the
// reduced form is to an orthorhombic character.
const CrystalLattice L(31, 43, 57, 90, 93, 90);
auto r = LePageLattice(L);
REQUIRE(r.has_value());
CHECK(r->system == gemmi::CrystalSystem::Monoclinic);
CHECK(r->centering == 'P');
}
TEST_CASE("LePageLattice - an operator filter picks out a sub-lattice of the metric") {
// A hexagonal metric carries twelve rotations, and asked as it stands that is the answer. Keeping
// only the three two-folds that close into 222 - the one along c and the two in the plane, along
// a+b and a-b - leaves the orthorhombic sub-lattice, whose conventional cell is (a+b, a-b, c) on
// twice the volume and so C-centred.
const CrystalLattice L(100, 100, 70, 90, 90, 120);
const auto whole_metric = LePageLattice(L);
REQUIRE(whole_metric.has_value());
CHECK(whole_metric->system == gemmi::CrystalSystem::Hexagonal);
const std::array<int, 9> along_c = {-1, 0, 0, 0, -1, 0, 0, 0, 1}; // -h,-k,l
const std::array<int, 9> along_a_plus_b = {0, 1, 0, 1, 0, 0, 0, 0, -1}; // k,h,-l
const std::array<int, 9> along_a_minus_b = {0, -1, 0, -1, 0, 0, 0, 0, -1}; // -k,-h,-l
const auto r = LePageLattice(L, LATTICE_MAX_OBLIQUITY_DEG, [&](const gemmi::Mat33 &m) {
return SameMatrix(m, along_c) || SameMatrix(m, along_a_plus_b) ||
SameMatrix(m, along_a_minus_b);
});
REQUIRE(r.has_value());
CHECK(r->system == gemmi::CrystalSystem::Orthorhombic);
CHECK(r->centering == 'C');
const UnitCell uc = r->conventional.GetUnitCell();
CHECK(uc.a == Catch::Approx(100).epsilon(0.01));
CHECK(uc.b == Catch::Approx(100 * std::sqrt(3.0)).epsilon(0.01));
CHECK(uc.c == Catch::Approx(70).epsilon(0.01));
}
TEST_CASE("LePageLattice - a filtered group may not rest on an operator the filter refused") {
// Two two-folds sixty degrees apart generate a three-fold, so the pair closes into the whole
// hexagonal group - four of whose two-folds the filter rejected. A sub-lattice whose own operators
// were offered and refused is not one the filter supports, so there is no answer to give.
const CrystalLattice L(100, 100, 70, 90, 90, 120);
const std::array<int, 9> along_a_plus_b = {0, 1, 0, 1, 0, 0, 0, 0, -1}; // k,h,-l
const std::array<int, 9> along_a = {1, 0, 0, -1, -1, 0, 0, 0, -1}; // h,-h-k,-l
const auto r = LePageLattice(L, LATTICE_MAX_OBLIQUITY_DEG, [&](const gemmi::Mat33 &m) {
return SameMatrix(m, along_a_plus_b) || SameMatrix(m, along_a);
});
CHECK(!r.has_value());
}