Files
Jungfraujoch/image_analysis/scale_merge/CellPopulation.cpp
T
leonarski_fandClaude Opus 5.5 380cdf3dc7 rugnux stills with a given cell: one crystal form merged, distance and centre searched together, a stated distance kept
- A given cell keeps only the frames whose refined cell belongs to its population (GivenCellPopulation):
  the peak of the refined-volume histogram nearest the given cell, out to the valley that separates it
  from another population, with the bin set by the data's own spread. A sample of two crystal forms
  indexed against one form's cell otherwise merges both; a run with one population keeps every frame.
- The distance grid is run from every centre hypothesis, not only the one decided at the file's
  distance, and a local grid of centres follows at the distance decided. A wrong header distance and a
  centre several pixels off (both background estimators misread it on a Rayonix set) otherwise leave
  each other flat; now the cell alone finds both.
- A distance given with --detector-distance is not searched.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 18:02:10 +02:00

78 lines
3.4 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;
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);
}
// The peak nearest the given cell: from its bin, uphill.
int peak = std::clamp(static_cast<int>(std::floor(-lo / bin)), 0, nbins - 1);
while (true) {
if (peak + 1 < nbins && smooth[peak + 1] > smooth[peak]) peak++;
else if (peak > 0 && smooth[peak - 1] > smooth[peak]) peak--;
else break;
}
// Out from the 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 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;
};
const int left = valley(-1), right = valley(+1);
return {left < 0 ? -everything : lo + (left + 0.5) * bin,
right < 0 ? everything : lo + (right + 0.5) * bin};
}