v1.0.0-rc.179 (#90)
Build Packages / Create release (push) Successful in 8s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 5m11s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 10m32s
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 18m26s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 22m11s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 23m32s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 25m36s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 27m28s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 28m10s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 28m10s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 29m24s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 21m45s
Build Packages / Generate python client (push) Successful in 31s
Build Packages / Build documentation (push) Successful in 1m21s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 24m2s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 29m25s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 30m27s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 27m12s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 32m20s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 33m36s
Build Packages / Unit tests (push) Successful in 2h7m18s
Build Packages / Validation Battery (push) Successful in 33m19s

* Rugnux: Stills processing needs only the unit cell and space group beyond what the file records - detector distance, beam centre, tilt, panel placement, saturation level, gain and polarisation plane are found from the images - and is up to twice as fast.
* Rugnux: Stills can be indexed without a GPU, with a CPU implementation of the fast-feedback indexer (macOS, computers without a GPU).
* Rugnux: Rotation and stills data are integrated in the detector's own units with one gain model, improving data from CCD detectors.
* Web frontend: The Rugnux live page shows the placed model with its electron density and anomalous difference map in a Mol* viewer.

Reviewed-on: #90
Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
This commit was merged in pull request #90.
This commit is contained in:
leonarski_f authored and leonarski_f committed 2026-10-24 14:52:29 +02:00
1 parent afbe844b20
commit b00c668e3a
394 files changed
+57778 -2178

No files matched your search

+135
View File
@@ -7,6 +7,7 @@
#include <climits>
#include <cmath>
#include <cstring>
#include <numbers>
#include <optional>
#include <thread>
#include <vector>
@@ -16,6 +17,7 @@
#include "../common/JFJochMessages.h"
#include "../common/PixelMask.h"
#include "../image_analysis/beam_stop/ShadowFinder.h"
#include "../image_analysis/beam_stop/ShadowFinderInternal.h"
namespace {
// Odd and square, so the beam sits on a pixel and a cross-shaped scene is exactly 4-fold
@@ -110,6 +112,37 @@ TEST_CASE("ShadowFinder_FindsAnInjectedBeamStop", "[ShadowFinder]") {
CHECK(std::count(mask.begin(), mask.end(), 1u) == 2612);
}
// A detector read in ADU is the photon scene times its gain, and with the gain given the mask is the
// photon scene's. The stop here lets three photons a frame through: at nine ADU a photon that is 27
// counts, which a photon threshold would take for a recorded reflection and give the whole stop back.
TEST_CASE("ShadowFinder_ADetectorReadInADUGivesThePhotonMask", "[ShadowFinder]") {
constexpr int32_t gain = 9;
const auto mask_of = [](int32_t scale, float pixel_gain) {
DiffractionExperiment x = TestExperiment();
BraggIntegrationSettings bis = x.GetBraggIntegrationSettings();
bis.PixelGain(pixel_gain);
x.ImportBraggIntegrationSettings(bis);
const PixelMask pixel_mask(x);
ShadowFinder finder(x, pixel_mask);
std::vector<std::vector<int32_t>> frames;
std::vector<uint8_t> buffer;
for (int f = 0; f < NFRAMES; f++) {
std::vector<int32_t> frame = Scene(/*cross=*/false, /*reflection=*/false);
for (auto &v : frame)
v = scale * (v == 0 ? 3 : 12);
frames.push_back(std::move(frame));
DataMessage msg{};
msg.image = CompressedImage(frames.back(), W, H);
finder.AddImage(msg, buffer);
}
return finder.GetMask();
};
const auto photons = mask_of(1, 1.0f);
REQUIRE(photons[I(C, C)] == 1);
CHECK(mask_of(gain, static_cast<float>(gain)) == photons);
CHECK(mask_of(gain, 1.0f)[I(C, C)] == 0);
}
// A holder arm that lets part of the beam through, on a background bright enough that every pixel
// reaches the reflection guard's count, crossing a module gap wider than the bridge. Each of the
// three hid the arm on its own: the partly transmitting stretch never reaches SHADOW_RATIO, the gap
@@ -410,6 +443,7 @@ TEST_CASE("ShadowFinder_APolarizedBackgroundIsNotAShadow", "[ShadowFinder]") {
frame[I(x, y)] = static_cast<int32_t>(std::lround(LEVEL * factor / at_peak));
}
std::optional<float> applied;
auto mask_for = [&](const std::optional<float> &polarization) {
DiffractionExperiment x = TestExperiment();
x.DetectorDistance_mm(DISTANCE_MM).PolarizationFactor(polarization);
@@ -422,13 +456,97 @@ TEST_CASE("ShadowFinder_APolarizedBackgroundIsNotAShadow", "[ShadowFinder]") {
finder.AddImage(msg, buffer);
}
const auto mask = finder.GetMask();
applied = finder.GetAppliedPolarization();
return std::count(mask.begin(), mask.end(), 1u);
};
CHECK(mask_for(POLARIZATION) == 0);
CHECK(applied == POLARIZATION); // the plane the background shows is the stated one: kept as stated
CHECK(mask_for(std::nullopt) > static_cast<long>(W) * H / 20);
}
// A beam stop with four arms on a background polarized in the plane along the image's y, at a short
// distance, in an experiment that states the usual horizontal plane. Divided by the factor of the
// stated plane the background's lobes read as shadow, and how much of them did followed the third
// decimal of the stated degree. The plane is taken from the background, so the stop and its arms come
// back, the lobes do not, and the mask is the same for any stated degree and either stated plane.
TEST_CASE("ShadowFinder_ThePolarizationPlaneIsTheOneTheBackgroundShows", "[ShadowFinder]") {
constexpr float DISTANCE_MM = 8.0f;
constexpr double TRUE_POLARIZATION = -0.998; // the plane along the image's y
constexpr int LEVEL = 24;
const float pixel_mm = TestExperiment().GetPixelSize_mm();
const auto stop = Scene(/*cross=*/true, false);
std::vector<int32_t> frame(static_cast<size_t>(W) * H);
for (int y = 0; y < H; y++)
for (int xi = 0; xi < W; xi++) {
const double dx = xi - C, dy = y - C;
const double rho = std::hypot(dx, dy) * pixel_mm;
const double cos_2theta_sq = DISTANCE_MM * DISTANCE_MM / (DISTANCE_MM * DISTANCE_MM + rho * rho);
const double rr = dx * dx + dy * dy;
const double cos_2phi = rr > 0.0 ? (dx * dx - dy * dy) / rr : 0.0;
const double factor = 0.5 * (1.0 + cos_2theta_sq - TRUE_POLARIZATION * cos_2phi * (1.0 - cos_2theta_sq));
frame[I(xi, y)] = stop[I(xi, y)] == 0 ? 0 : static_cast<int32_t>(std::lround(LEVEL * factor));
}
std::optional<float> applied; // what the run's other corrections take (GetAppliedPolarization)
auto mask_for = [&](const std::optional<float> &polarization) {
DiffractionExperiment x = TestExperiment();
x.DetectorDistance_mm(DISTANCE_MM).PolarizationFactor(polarization);
const PixelMask pixel_mask(x);
ShadowFinder finder(x, pixel_mask);
std::vector<uint8_t> buffer;
for (int f = 0; f < NFRAMES; f++) {
DataMessage msg{};
msg.image = CompressedImage(frame, W, H);
finder.AddImage(msg, buffer);
}
const auto out = finder.GetMask();
applied = finder.GetAppliedPolarization();
return out;
};
const auto mask = mask_for(0.99f);
CHECK(applied == -0.99f); // the stated degree, in the plane shown
CHECK(mask[I(C, C)] == ShadowFinder::SHADOW); // the stop
CHECK(mask[I(W - 3, C)] == ShadowFinder::SHADOW); // and its arms, along x
CHECK(mask[I(C, 3)] == ShadowFinder::SHADOW); // and along y, inside the suppressed lobe
CHECK(mask[I(C + 20, 3)] == 0); // but not the lobe itself
CHECK(mask[I(C - 20, H - 4)] == 0);
CHECK(mask_for(0.998f) == mask);
CHECK(mask_for(-0.998f) == mask);
CHECK(applied == -0.998f);
mask_for(std::nullopt);
CHECK(!applied); // no stated degree, nothing to apply
}
// The plane is decided from the sector medians of each band: the cos 2phi harmonic has to follow the
// profile a polarized source gives it, with a one-sided shadow over a third of the ring left out by
// comparing opposite sectors. A background without modulation decides nothing.
TEST_CASE("ShadowFinder_PolarizationPlane", "[ShadowFinder]") {
using namespace shadow_finder;
constexpr int N_BANDS = 8;
std::vector<float> amplitude(N_BANDS);
for (int band = 0; band < N_BANDS; band++)
amplitude[band] = 0.05f * static_cast<float>(band + 1);
auto medians = [&](double sign, bool shadow) {
std::vector<double> m(static_cast<size_t>(N_BANDS) * HARMONIC_SECTORS);
for (int band = 0; band < N_BANDS; band++)
for (int k = 0; k < HARMONIC_SECTORS; k++) {
const double phi = (k + 0.5) * 2.0 * std::numbers::pi / HARMONIC_SECTORS - std::numbers::pi;
double v = 1.0 + sign * amplitude[band] * std::cos(2.0 * phi);
if (shadow && k >= 8 && k < 16)
v *= 0.7; // a third of the ring around phi = 0
m[static_cast<size_t>(band) * HARMONIC_SECTORS + k] = v;
}
return m;
};
CHECK(PolarizationPlane(medians(-1.0, false), amplitude) == 1.0f); // dim along x: horizontal
CHECK(PolarizationPlane(medians(+1.0, false), amplitude) == -1.0f); // dim along y: vertical
CHECK(PolarizationPlane(medians(+1.0, true), amplitude) == -1.0f); // the shadow along x does not hide it
CHECK(PolarizationPlane(medians(0.0, false), amplitude) == 0.0f); // no modulation: no decision
}
// The other side of that correction: a polarization factor that is not the beam's. Here the
// background is unpolarized and the experiment states 0.99, so after the division every outer ring is
// bright along x and dim along y - a smooth lobe below PENUMBRA_RATIO across the top and bottom of the
@@ -593,6 +711,23 @@ TEST_CASE("ShadowFinder_DeviceMaskMatchesHost", "[ShadowFinder]") {
CHECK(std::count(r.host_mask.begin(), r.host_mask.end(), ShadowFinder::TRANSMITTING) > 0);
CHECK(r.device_mask == r.host_mask);
}
SECTION("a stop that lets a little through, read in ADU at the gain") {
constexpr int32_t gain = 9;
DiffractionExperiment x = TestExperiment();
BraggIntegrationSettings bis = x.GetBraggIntegrationSettings();
bis.PixelGain(static_cast<float>(gain));
x.ImportBraggIntegrationSettings(bis);
std::vector<std::vector<int32_t>> frames;
for (int f = 0; f < NFRAMES; f++) {
frames.push_back(Scene(false, false));
for (auto &v : frames.back())
v = gain * (v == 0 ? 3 : 12);
}
const auto r = MaskBothWays(x, frames);
CHECK(r.host_mask[I(C, C)] == 1);
CHECK(r.device_mask == r.host_mask);
}
}
// The pre-scan keeps one accumulator per card and folds them together when the mask is read. Two