RESULT-CHANGING (owner approved): stills crystals indexed in a lattice containing the run's are put on the run's cell

A still indexed on few spots often gets a lattice that explains them as well as the true one: an axis
multiple, whose reciprocal lattice holds every true node plus the nodes between, or the true lattice in
another setting. Its cell is then not the run's, the merge leaves it out, and its reflections at the true
nodes - as good as any - are lost. On two 25k-frame XFEL runs of a tetragonal crystal these were 26-30%
of the indexed frames; for those holding the true cell as a sublattice, the reflections at true nodes had
the I/sigma of accepted frames and those between them none.

RelabelOnSublattice (new, scale_merge/SublatticeRelabel) compares lattices, not cell parameters: it
looks for integer combinations of the frame's reciprocal axes whose full metric is the run's cell within
the merge's own tolerances, volume ratio 1-4 (another setting, or a 2x-4x supercell). Where several such
bases exist - a nearly symmetric supercell fits along more than one axis - the one with the strongest
mean I/sigma at its nodes is taken, since only the true lattice has signal there. The frame's indices
are re-derived on that basis (h = h' M^-1), reflections that are not nodes are dropped, as are those
finer than any frame already on the run's cell reaches (the wrong lattice was also predicted to a wrong
resolution), and the frame gets the run's basis in its own orientation. It runs before the first stills
merge, and in --mode scale before the resolutions are recomputed from the cell.

--mode scale, both runs (indicative; the mx pairs are queued): 1060 and 920 crystals put back,
observations +8%, I/sigma 10.3 -> 10.6 and 10.7 -> 11.0, CC1/2 0.966 -> 0.969 and 0.970 -> 0.966.
Tests: SublatticeRelabel_DoubledAxisGoesBackToTheTrueCell, _OtherSettingAndUnrelatedLattice,
_SignalPicksTheAxisThatWasDoubled.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
leonarski_fandClaude Opus 5.5 committed 2026-10-14 13:15:13 +02:00
1 parent a574e97b10
commit 439ca10db1
6 files changed
+351 -1

No files matched your search

+117
View File
@@ -12,6 +12,7 @@
#include "../image_analysis/scale_merge/ResolutionCutoff.h"
#include "../image_analysis/scale_merge/ScaleOnTheFly.h"
#include "../image_analysis/scale_merge/StillsPartialityRefine.h"
#include "../image_analysis/scale_merge/SublatticeRelabel.h"
#include "gemmi/reciproc.hpp"
TEST_CASE("HKLKey_NoSG_noMergeFriedel") {
@@ -746,3 +747,119 @@ TEST_CASE("StillsPartialityRefine_RecoversTheTilt") {
CHECK(mean_tilt < 0.06);
CHECK(after < 0.6 * before); // measured 0.54: the prior holds the larger tilts back
}
namespace {
// The basis vectors a, b, c of a cell, turned by a fixed arbitrary rotation, as a frame's lattice.
std::array<Coord, 3> TurnedAxes(const UnitCell &cell) {
const CrystalLattice l(cell);
const double c = std::cos(0.7), s = std::sin(0.7); // about (1, 2, 3)/sqrt(14)
const double n[3] = {1.0 / std::sqrt(14.0), 2.0 / std::sqrt(14.0), 3.0 / std::sqrt(14.0)};
auto turn = [&](const Coord &v) {
const double dot = n[0] * v.x + n[1] * v.y + n[2] * v.z;
const double x[3] = {n[1] * v.z - n[2] * v.y, n[2] * v.x - n[0] * v.z, n[0] * v.y - n[1] * v.x};
return Coord(static_cast<float>(v.x * c + x[0] * s + n[0] * dot * (1 - c)),
static_cast<float>(v.y * c + x[1] * s + n[1] * dot * (1 - c)),
static_cast<float>(v.z * c + x[2] * s + n[2] * dot * (1 - c)));
};
return {turn(l.Vec0()), turn(l.Vec1()), turn(l.Vec2())};
}
Coord ScatteringVector(const CrystalLattice &l, const Reflection &r) {
return l.Astar() * static_cast<float>(r.h) + l.Bstar() * static_cast<float>(r.k)
+ l.Cstar() * static_cast<float>(r.l);
}
IntegrationOutcome FrameWithAllIndices(const CrystalLattice &latt, int hmax) {
IntegrationOutcome o;
o.latt = latt;
for (int h = -hmax; h <= hmax; ++h)
for (int k = -hmax; k <= hmax; ++k)
for (int l = -hmax; l <= hmax; ++l) {
Reflection r{};
r.h = h; r.k = k; r.l = l;
r.predicted_x = static_cast<float>(o.reflections.size()); // identifies the observation
r.I = 100.0f;
r.sigma = 10.0f;
const float q = ScatteringVector(latt, r).Length();
r.d = q > 0.0f ? 1.0f / q : 100.0f;
o.reflections.push_back(r);
}
return o;
}
}
// A frame indexed with its c axis doubled: its reciprocal lattice holds every node of the true one plus
// the planes in between. Relabelling keeps exactly the true nodes, each at the same scattering vector,
// and gives the frame the true cell.
TEST_CASE("SublatticeRelabel_DoubledAxisGoesBackToTheTrueCell") {
const UnitCell reference{60, 60, 25, 90, 90, 90}; // synthetic tetragonal
const auto ax = TurnedAxes(reference);
const CrystalLattice super(ax[0], ax[1], ax[2] * 2.0f);
// A frame on the true cell beside it sets how fine the relabelled reflections may go.
std::vector<IntegrationOutcome> outcomes{FrameWithAllIndices(super, 4),
FrameWithAllIndices(CrystalLattice(ax[0], ax[1], ax[2]), 4)};
const auto before = outcomes[0];
IndexingSettings settings;
const auto result = RelabelOnSublattice(outcomes, reference, settings);
CHECK(result.frames == 1);
CHECK(result.reflections_kept == 9 * 9 * 5); // l even: -4, -2, 0, 2, 4
CHECK(result.reflections_dropped == 9 * 9 * 4);
CHECK(outcomes[0].latt.GetUnitCell().is_close(reference, 0.001, 0.01));
for (const auto &r: outcomes[0].reflections) {
const auto it = std::find_if(before.reflections.begin(), before.reflections.end(),
[&](const Reflection &b) { return b.predicted_x == r.predicted_x; });
REQUIRE(it != before.reflections.end());
CHECK(it->l % 2 == 0);
const Coord q_before = ScatteringVector(before.latt, *it);
const Coord q_after = ScatteringVector(outcomes[0].latt, r);
CHECK((q_after - q_before).Length() < 1e-5f);
}
}
// The true lattice in another setting (axes permuted) is the same lattice: every reflection is kept and
// only re-indexed. A frame whose lattice does not contain the reference is left alone.
TEST_CASE("SublatticeRelabel_OtherSettingAndUnrelatedLattice") {
const UnitCell reference{40, 50, 60, 90, 90, 90}; // synthetic orthorhombic
const auto ax = TurnedAxes(reference);
std::vector<IntegrationOutcome> outcomes{FrameWithAllIndices(CrystalLattice(ax[1], ax[2], ax[0]), 3),
FrameWithAllIndices(CrystalLattice(UnitCell{37, 53, 71, 90, 90, 90}), 3),
FrameWithAllIndices(CrystalLattice(ax[0], ax[1], ax[2]), 4)};
const auto permuted = outcomes[0];
const auto unrelated = outcomes[1];
IndexingSettings settings;
const auto result = RelabelOnSublattice(outcomes, reference, settings);
CHECK(result.frames == 1);
CHECK(result.reflections_dropped == 0);
CHECK(outcomes[0].latt.GetUnitCell().is_close(reference, 0.001, 0.01));
for (size_t i = 0; i < outcomes[0].reflections.size(); ++i) {
const Coord q_before = ScatteringVector(permuted.latt, permuted.reflections[i]);
const Coord q_after = ScatteringVector(outcomes[0].latt, outcomes[0].reflections[i]);
CHECK((q_after - q_before).Length() < 1e-5f);
}
CHECK(outcomes[1].reflections.size() == unrelated.reflections.size());
CHECK(outcomes[1].reflections[5].h == unrelated.reflections[5].h);
}
// A doubled axis that makes the supercell metric cubic fits the reference along any of the three axes;
// only the true one has signal at its nodes, and that is the one taken.
TEST_CASE("SublatticeRelabel_SignalPicksTheAxisThatWasDoubled") {
const UnitCell reference{60, 60, 30, 90, 90, 90}; // synthetic tetragonal; doubled c gives a cube
const auto ax = TurnedAxes(reference);
std::vector<IntegrationOutcome> outcomes{FrameWithAllIndices(CrystalLattice(ax[0], ax[1], ax[2] * 2.0f), 4),
FrameWithAllIndices(CrystalLattice(ax[0], ax[1], ax[2]), 4)};
for (auto &r: outcomes[0].reflections)
r.I = r.l % 2 == 0 ? 100.0f : 0.0f; // the nodes between the true ones are empty
const auto before = outcomes[0];
IndexingSettings settings;
const auto result = RelabelOnSublattice(outcomes, reference, settings);
REQUIRE(result.frames == 1);
for (const auto &r: outcomes[0].reflections) {
const auto &b = before.reflections[static_cast<size_t>(r.predicted_x)];
CHECK(b.l % 2 == 0);
CHECK((ScatteringVector(outcomes[0].latt, r) - ScatteringVector(before.latt, b)).Length() < 1e-5f);
}
}