Files
leonarski_f a9d5367013
Build Packages / Create release (push) Successful in 26s
Build Packages / Build documentation (push) Successful in 1m43s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 5m7s
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 11m48s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 7m22s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 12m24s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 25m30s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 25m29s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 29m41s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 21m44s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 24m41s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 26m9s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 26m3s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 28m28s
Build Packages / Generate python client (push) Successful in 27s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 31m16s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 20m44s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 26m27s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 22m52s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 27m44s
Build Packages / Unit tests (push) Successful in 1h46m55s
Build Packages / Validation Battery (push) Successful in 52m50s
v1.0.0-rc.181 (#92)
* Rugnux: Several rotation sweeps of one crystal are merged as one dataset (`rugnux a_master.h5 b_master.h5`), on a common indexing and cell, with a non-isomorphous sweep left out and reported; `--sweeps separate` processes each sweep on its own. Bruker `.sfrm` frames and imgCIF files with a full goniometer axis chain are read.
* Rugnux: More robust rotation processing - a retry with a more sensitive spot finder when no lattice is found, a self-consistent detector distance, long axes and doubled cells tested as hypotheses, a `-C` supercell multiple stepped down, and each ice ring merged, down-weighted or rejected on its own.
* Rugnux: Serial stills are merged with a partiality-aware error model, reported as `ERROR_MODEL_A/B/C` with no ISa or R_merge/R_meas/R_pim; runs without `-S` have an overall intensity scale about 0.86-0.88 times that of earlier versions, with unchanged statistics. A second crystal form is left out of the merge and reported, the indexing ambiguity is resolved more reliably, and `--model` no longer influences processing.
* Rugnux: The reported CC1/2 is the standard unweighted one, which can be lower than before on sweeps with weak frames; ISa comes with a 95% interval (`ISA_LOW`/`ISA_HIGH`).
* Rugnux: Model refinement adds anisotropic/TLS, local NCS and twin refinement where R-free supports them; building waters and relabelling ions is now off by default (`--refine-solvent` turns it on).
* NXmx: A detector of several modules is written as `module_0` ... `module_N` only, so DIALS reads each module as its own panel.
* Other: JPEG previews use a bundled encoder (libjpeg-turbo no longer needed), Rugnux prints how to cite it, and the release archives contain the HTML documentation.

Reviewed-on: #92
Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
2026-11-02 12:05:20 +01:00

198 lines
8.6 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include "CellPopulation.h"
#include <algorithm>
#include <array>
#include <cmath>
#include <limits>
#include <Eigen/Dense>
std::vector<CellPopulationRange> CellPopulations(const std::vector<double> &relative_volume) {
constexpr double everything = std::numeric_limits<double>::infinity();
// Below this many frames a histogram has no valleys to speak of.
constexpr size_t MIN_FRAMES = 50;
// A valley separates two populations only where it dips below this fraction of the kept peak; a
// shallower dip is noise in one population's tail.
constexpr double VALLEY_FRACTION = 0.5;
// A population holding less than this share of the frames is a stray group, not a crystal form.
constexpr double MIN_SHARE = 0.1;
const size_t n = relative_volume.size();
if (n < MIN_FRAMES)
return {};
std::vector<double> v = relative_volume;
std::sort(v.begin(), v.end());
// The bin: twice a robust standard deviation over n^(1/3) (Freedman-Diaconis with the median
// absolute deviation in place of the interquartile range, which a second population inflates).
const double median = v[n / 2];
std::vector<double> dev(n);
for (size_t i = 0; i < n; i++)
dev[i] = std::fabs(v[i] - median);
std::nth_element(dev.begin(), dev.begin() + n / 2, dev.end());
const double bin = 2.0 * 1.4826 * dev[n / 2] / std::cbrt(static_cast<double>(n));
if (!(bin > 0))
return {};
const double lo = v.front();
const int nbins = static_cast<int>((v.back() - lo) / bin) + 1;
std::vector<double> count(nbins, 0.0);
for (double x : v)
count[std::min(nbins - 1, static_cast<int>((x - lo) / bin))] += 1.0;
// Three-bin running mean, so a single empty or full bin is not a valley or a peak.
std::vector<double> smooth(nbins, 0.0);
for (int i = 0; i < nbins; i++) {
const int a = std::max(0, i - 1), b = std::min(nbins - 1, i + 1);
double s = 0;
for (int j = a; j <= b; j++)
s += count[j];
smooth[i] = s / (b - a + 1);
}
// Out from a peak on each side: the valley is the lowest point, below VALLEY_FRACTION of the peak,
// before the histogram rises again to twice its height by more than counting noise - the next
// population, however small. None: the population runs out to the end of the data.
const auto valley = [&](int peak, int step) {
int lowest = peak;
for (int i = peak + step; i >= 0 && i < nbins; i += step) {
if (smooth[i] < smooth[lowest])
lowest = i;
const double low = smooth[lowest], here = smooth[i];
// A three-bin mean of Poisson counts has a variance of a third of its value.
const bool rises = here >= 2.0 * low && here - low > 3.0 * std::sqrt((here + low) / 3.0);
if (low < VALLEY_FRACTION * smooth[peak] && rises)
return lowest;
}
return -1;
};
// Every population: a local maximum of the histogram and the valleys about it, if it holds a real share
// of the frames.
std::vector<CellPopulationRange> populations;
for (int i = 0; i < nbins; i++) {
if (smooth[i] <= 0 || (i > 0 && smooth[i - 1] > smooth[i]) || (i + 1 < nbins && smooth[i + 1] >= smooth[i]))
continue;
const int left = valley(i, -1), right = valley(i, +1);
// A population is its highest peak: a stray frame or two in its tail is a local maximum too, and
// with no valley of its own its range is the whole population - or, where the tail runs out
// before any valley, everything, two forms included.
bool highest = true;
for (int j = left < 0 ? 0 : left; j <= (right < 0 ? nbins - 1 : right); j++)
if (smooth[j] > smooth[i])
highest = false;
if (!highest)
continue;
const double low = left < 0 ? -everything : lo + (left + 0.5) * bin;
const double high = right < 0 ? everything : lo + (right + 0.5) * bin;
size_t count = 0;
double sum = 0;
for (double x : v)
if (x >= low && x <= high) {
count++;
sum += x;
}
if (count < MIN_SHARE * static_cast<double>(n))
continue;
populations.push_back({low, high, count, sum / static_cast<double>(count)});
}
return populations;
}
std::pair<double, double> GivenCellPopulation(const std::vector<double> &relative_volume) {
constexpr double everything = std::numeric_limits<double>::infinity();
// The population centred nearest the given cell is kept - not merely the peak nearest it, which can be
// a few stray frames when the given cell is a percent off the crystal.
double best_low = -everything, best_high = everything, best_distance = everything;
for (const auto &p : CellPopulations(relative_volume))
if (std::fabs(p.mean) < best_distance) {
best_distance = std::fabs(p.mean);
best_low = p.low;
best_high = p.high;
}
return {best_low, best_high};
}
namespace {
// A cell's shape, for telling forms apart: its edges as logarithms, so a percent weighs the same on every
// edge. Not its angles: a still's refined angles carry the indexing more than the crystal - the same form
// comes out with its monoclinic angle on one axis or another, obtuse or acute, or held at 90 degrees - and
// those spread more than two forms' angles differ.
Eigen::Vector3d Shape(const UnitCell &c) {
return {std::log(c.a), std::log(c.b), std::log(c.c)};
}
}
UnitCell AlignedCell(const UnitCell &cell, const UnitCell &reference) {
const std::array<float, 3> edge = {cell.a, cell.b, cell.c}, angle = {cell.alpha, cell.beta, cell.gamma};
const Eigen::Vector3d ref = Shape(reference);
std::array<int, 3> p = {0, 1, 2};
UnitCell best = cell;
double best_distance = std::numeric_limits<double>::infinity();
do {
const UnitCell c{edge[p[0]], edge[p[1]], edge[p[2]], angle[p[0]], angle[p[1]], angle[p[2]]};
const double distance = (Shape(c) - ref).squaredNorm();
if (distance < best_distance) {
best_distance = distance;
best = c;
}
} while (std::next_permutation(p.begin(), p.end()));
return best;
}
CellForms SeparateCellForms(const std::vector<UnitCell> &cells, const UnitCell &reference) {
CellForms out;
out.form.assign(cells.size(), 0);
const size_t n = cells.size();
if (n == 0)
return out;
std::vector<Eigen::Vector3d> f(n);
Eigen::Vector3d mean = Eigen::Vector3d::Zero();
for (size_t i = 0; i < n; i++) {
f[i] = Shape(AlignedCell(cells[i], reference));
mean += f[i];
}
mean /= static_cast<double>(n);
Eigen::Matrix3d cov = Eigen::Matrix3d::Zero();
for (const auto &x : f)
cov += (x - mean) * (x - mean).transpose();
// The direction the cells spread along most: between two forms, where there are two, as the spread
// between them adds to the spread of each.
const Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> eig(cov);
const Eigen::Vector3d axis = eig.eigenvectors().col(2);
out.coordinate.resize(n);
for (size_t i = 0; i < n; i++)
out.coordinate[i] = (f[i] - mean).dot(axis);
auto populations = CellPopulations(out.coordinate);
if (populations.size() < 2)
return out;
std::sort(populations.begin(), populations.end(),
[](const CellPopulationRange &x, const CellPopulationRange &y) { return x.count > y.count; });
const auto &major = populations[0], &minor = populations[1];
const auto in = [](const CellPopulationRange &p, double x) { return x >= p.low && x <= p.high; };
Eigen::Vector3d mean_major = Eigen::Vector3d::Zero(), mean_minor = mean_major;
std::vector<UnitCell> minor_cells;
for (size_t i = 0; i < n; i++) {
if (in(major, out.coordinate[i]))
mean_major += f[i];
else if (in(minor, out.coordinate[i])) {
mean_minor += f[i];
minor_cells.push_back(AlignedCell(cells[i], reference));
}
}
mean_major /= static_cast<double>(major.count);
mean_minor /= static_cast<double>(minor.count);
out.separation = (mean_minor - mean_major).norm();
if (out.separation < MIN_FORM_SEPARATION)
return out;
for (size_t i = 0; i < n; i++)
if (in(minor, out.coordinate[i]))
out.form[i] = 1;
out.minor_frames = minor_cells.size();
out.minor_cell = MeanUnitCell(minor_cells);
return out;
}