Build Packages / Create release (push) Successful in 17s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 4m41s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 20m49s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 13m6s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 23m44s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 7m28s
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 11m50s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 14m9s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 16m25s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 15m42s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 20m50s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 20m36s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 22m18s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 23m29s
Build Packages / Generate python client (push) Successful in 30s
Build Packages / Build documentation (push) Successful in 1m10s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 21m38s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 17m8s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 23m10s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 19m16s
Build Packages / Unit tests (push) Successful in 2h8m4s
Build Packages / Validation Battery (push) Successful in 40m53s
* Rugnux: Processes serial crystallography data from XFELs - SACLA (MPCCD), PAL-XFEL and European XFEL (CXI), LCLS (XTC) and SwissFEL Alvra (JUNGFRAU with a CrystFEL module geometry) - with several files or Jungfraujoch master files merged in one run (`rugnux *_master.h5`). * Rugnux: Stills processing takes a known unit cell and space group as a starting point, finds the beam centre (and, with a cell, the detector distance) from the images, detects the saturation level in other facilities' files and refines the placement of individual detector panels. * Rugnux: The stills report gives R_split and CC1/2 from frame half-sets, comparable with CrystFEL. * jfjoch_viewer: Opens the same XFEL file formats. * Validation: A stills battery checks Rugnux against deposited structures from SACLA, PAL-XFEL, LCLS, European XFEL and SwissFEL. * Rugnux: More reliable convergence of rotation scaling, and live molecular replacement starts as soon as the model is uploaded. * jfjoch_broker: Initialize clears the previous dataset's bearer tokens, image buffer and live Rugnux status, and a slow CUDA JIT cache is reported at start. Reviewed-on: #88 Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
97 lines
4.3 KiB
C++
97 lines
4.3 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 <cmath>
|
|
#include <limits>
|
|
|
|
std::pair<double, double> GivenCellPopulation(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 {-everything, everything};
|
|
|
|
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 {-everything, everything};
|
|
|
|
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. Of those that hold
|
|
// a real share of the frames, the one 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 (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);
|
|
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;
|
|
const double distance = std::fabs(sum / static_cast<double>(count));
|
|
if (distance < best_distance) {
|
|
best_distance = distance;
|
|
best_low = low;
|
|
best_high = high;
|
|
}
|
|
}
|
|
return {best_low, best_high};
|
|
}
|