The serial pre-pass runs once on the first stills_prepass_images frames (new rugnux live setting, default 1000, 100-20000, reset to 1000 by POST /initialize) instead of the doubling stages (500 -> ... -> 2000 frames, stop at 200 geometry frames). One decision: with a cell and at least RUGNUX_LIVE_GEOMETRY_MIN_FRAMES (50) strong frames among them, the beam centre, distance and cell are refined on them; otherwise the header geometry is used exactly as given - the rugnux pre-pass then neither fits nor tries a centre (ProcessConfig::stills_geometry_min_frames), and StillsGPUAnalysis applies beam/distance/cell only where the fit ran. The X-ray-only steps (saturation, shadow, defective pixels, polarization, background ring, powder rings) run as before on the start of the window; the background beam-centre estimate is reported, not applied, when the geometry is kept. Log and live-status notes say "Geometry refined from K of N frames" or "Fewer than 50 strong frames in the first N: header geometry used". Where the hold (at most half of the image buffer) fills before N frames, the pre-pass runs on those held and says so. The first N images are always analysed under stills_downsample. Rotation is unaffected. Two fixes in the stills geometry: - The refit (bundle indexed again at the fitted geometry) was skipped on every modular detector whenever the modules were not being placed, i.e. always live: placing_modules now also needs refine_modules. Offline, where the first fit places the modules and so skips its refit, the fit is run again from the placed geometry, as after a centre the trial moved. - The local beam-centre grid no longer adopts a chance maximum: it moves the centre only from one that puts spots on the lattice. OpenAPI: rugnux_settings/rugnux_status.stills_prepass_images; C++ model, frontend client and python client docs regenerated; frontend settings field. jfjoch_stills_gpu_replay and jfjoch_fpga_stills_test take -P <images>. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HVeDQrPqZ52Tp6rzzZGxf3
196 lines
7.8 KiB
C++
196 lines
7.8 KiB
C++
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#include <catch2/catch_all.hpp>
|
|
|
|
#include <atomic>
|
|
#include <functional>
|
|
#include <latch>
|
|
|
|
#include "../common/ImageBuffer.h"
|
|
#include "../common/time_utc.h"
|
|
#include "../image_analysis/structure_refinement/RugnuxModel.h"
|
|
#include "../receiver/StillsGPUAnalysis.h"
|
|
#include "../receiver/StillsSelection.h"
|
|
|
|
namespace {
|
|
struct TestSink : public StillsSink {
|
|
std::function<void(const StillsSetup &)> ready = [](const StillsSetup &) {};
|
|
std::function<void(DataMessage &)> analysed = [](DataMessage &) {};
|
|
std::function<void()> failed = [] {};
|
|
|
|
void StillsReady(const StillsSetup &setup) override { ready(setup); }
|
|
void SendAnalysedImage(DataMessage &message, const AzimuthalIntegrationProfile &) override { analysed(message); }
|
|
SpotFindingSettings AnalysisSettings() override { return DiffractionExperiment::DefaultDataProcessingSettings(); }
|
|
void StillsSetupFailed(const JFJochException &) override { failed(); }
|
|
};
|
|
|
|
DiffractionExperiment StillsExperiment() {
|
|
DiffractionExperiment x(DetJF(1));
|
|
x.ImagesPerTrigger(STILLS_PREPASS_IMAGES + 200).NumTriggers(1).DetectorDistance_mm(100).BeamX_pxl(500).BeamY_pxl(250)
|
|
.IncidentEnergy_keV(12.4);
|
|
return x;
|
|
}
|
|
|
|
// A frame after the pre-pass's sample (numbers from PREPASS_IMAGES on are never in it), in a slot of `buffer`.
|
|
std::shared_ptr<HeldImage> Frame(ImageBuffer &buffer, int64_t number) {
|
|
auto held = std::make_shared<HeldImage>();
|
|
held->slot = buffer.GetImageSlot();
|
|
REQUIRE(held->slot != nullptr);
|
|
held->message.number = STILLS_PREPASS_IMAGES + number;
|
|
held->message.image_collection_efficiency = 1.0f;
|
|
return held;
|
|
}
|
|
|
|
StartMessage Start(const DiffractionExperiment &x) {
|
|
StartMessage start;
|
|
x.FillMessage(start);
|
|
return start;
|
|
}
|
|
}
|
|
|
|
TEST_CASE("StillsGPUAnalysis_StartFailureReleasesThreads", "[StillsGPUAnalysis]") {
|
|
Logger logger(Catch::getResultCapture().getCurrentTestName());
|
|
const DiffractionExperiment x = StillsExperiment();
|
|
const PixelMask mask(x);
|
|
ImageBuffer buffer(64 * 1024 * 1024);
|
|
buffer.StartMeasurement(x);
|
|
RugnuxLiveStatus status;
|
|
|
|
std::atomic<int> failed = 0, sunk = 0;
|
|
TestSink sink;
|
|
sink.ready = [](const StillsSetup &) {
|
|
throw JFJochException(JFJochExceptionCategory::ZeroMQ, "Timeout on pushing start message");
|
|
};
|
|
sink.analysed = [&](DataMessage &) { ++sunk; };
|
|
sink.failed = [&] { ++failed; };
|
|
{
|
|
StillsGPUAnalysis gpu(x, mask, Start(x), nullptr, 4, 100, logger, sink, status);
|
|
for (int i = 0; i < 3; i++)
|
|
gpu.Hold(Frame(buffer, i));
|
|
REQUIRE_THROWS_AS(gpu.Finish(), JFJochException);
|
|
}
|
|
CHECK(failed == 1);
|
|
CHECK(sunk == 0);
|
|
CHECK(status.Get().state == RugnuxLiveState::Failed);
|
|
// Every slot the frames were held in is back.
|
|
CHECK(buffer.GetAvailSlots() == buffer.GetStatus().total_slots);
|
|
}
|
|
|
|
TEST_CASE("StillsGPUAnalysis_HoldIsBounded", "[StillsGPUAnalysis]") {
|
|
Logger logger(Catch::getResultCapture().getCurrentTestName());
|
|
const DiffractionExperiment x = StillsExperiment();
|
|
const PixelMask mask(x);
|
|
ImageBuffer buffer(64 * 1024 * 1024);
|
|
buffer.StartMeasurement(x);
|
|
REQUIRE(buffer.GetStatus().total_slots >= 4);
|
|
|
|
RugnuxLiveStatus status;
|
|
TestSink sink;
|
|
sink.ready = [](const StillsSetup &) { throw JFJochException(JFJochExceptionCategory::ZeroMQ, "no writer"); };
|
|
StillsGPUAnalysis gpu(x, mask, Start(x), nullptr, 2, 3, logger, sink, status);
|
|
// Past what the pre-pass holds, frames are still taken: only the image buffer bounds them.
|
|
for (int i = 0; i < 4; i++)
|
|
gpu.Hold(Frame(buffer, i));
|
|
CHECK(buffer.GetStatus().preparation_slots == 4);
|
|
CHECK(buffer.GetImageSlot(buffer.GetStatus().available_slots) == nullptr);
|
|
REQUIRE_THROWS(gpu.Finish());
|
|
CHECK(buffer.GetStatus().preparation_slots == 0);
|
|
}
|
|
|
|
TEST_CASE("StillsGPUAnalysis_FailingFrameDoesNotStopAnalysis", "[StillsGPUAnalysis]") {
|
|
Logger logger(Catch::getResultCapture().getCurrentTestName());
|
|
const DiffractionExperiment x = StillsExperiment();
|
|
const PixelMask mask(x);
|
|
ImageBuffer buffer(64 * 1024 * 1024);
|
|
buffer.StartMeasurement(x);
|
|
RugnuxLiveStatus status;
|
|
|
|
std::atomic<int> sunk = 0;
|
|
TestSink sink;
|
|
sink.analysed = [&](DataMessage &m) {
|
|
++sunk;
|
|
if (m.number == STILLS_PREPASS_IMAGES)
|
|
throw std::runtime_error("not a JFJochException");
|
|
};
|
|
StillsGPUAnalysis gpu(x, mask, Start(x), nullptr, 1, 100, logger, sink, status);
|
|
// The frames carry no image, so their analysis fails too - they are passed on all the same.
|
|
for (int i = 0; i < 3; i++) {
|
|
status.ImageReceived(std::chrono::steady_clock::now()); // as the receiver does before it holds a frame
|
|
gpu.Hold(Frame(buffer, i));
|
|
}
|
|
REQUIRE_NOTHROW(gpu.Finish());
|
|
CHECK(sunk == 3);
|
|
// The outcome waits for everything to be written, and comes after the latency.
|
|
CHECK(status.Get().state == RugnuxLiveState::Running);
|
|
CHECK(!status.Get().last_image_to_written_s);
|
|
status.Latency(SecondsSince(status.LastImage().value()));
|
|
gpu.ReportOutcome();
|
|
const auto s = status.Get();
|
|
CHECK(s.state == RugnuxLiveState::Failed);
|
|
CHECK(!s.message.empty());
|
|
REQUIRE(s.last_image_to_written_s);
|
|
REQUIRE(s.last_image_s);
|
|
CHECK(s.elapsed_s >= s.last_image_s.value() + s.last_image_to_written_s.value() - 1e-6);
|
|
REQUIRE(!s.steps.empty());
|
|
CHECK(s.steps.back().title == "Writing");
|
|
CHECK(s.steps.back().duration_s);
|
|
CHECK(buffer.GetStatus().preparation_slots == 0);
|
|
}
|
|
|
|
TEST_CASE("StillsGPUAnalysis_CancelCutsTheModelStep", "[StillsGPUAnalysis]") {
|
|
Logger logger(Catch::getResultCapture().getCurrentTestName());
|
|
const DiffractionExperiment x = StillsExperiment();
|
|
const PixelMask mask(x);
|
|
TestSink sink;
|
|
const auto run = [&](RugnuxLiveStatus &status, std::shared_ptr<const RugnuxModel> model) {
|
|
return std::make_unique<StillsGPUAnalysis>(x, mask, Start(x), nullptr, 1, 100, logger, sink, status, true,
|
|
std::move(model), 60.0);
|
|
};
|
|
|
|
RugnuxLiveStatus during;
|
|
auto a = run(during, std::make_shared<RugnuxModel>());
|
|
a->Cut("cancel");
|
|
CHECK(during.Get().cut == "cancel");
|
|
REQUIRE_NOTHROW(a->Finish());
|
|
|
|
// Nothing is left to cut once the model step has ended, and nothing runs without a model.
|
|
RugnuxLiveStatus after;
|
|
auto b = run(after, std::make_shared<RugnuxModel>());
|
|
b->Finish();
|
|
b->Cut("cancel");
|
|
CHECK(after.Get().cut.empty());
|
|
|
|
RugnuxLiveStatus no_model;
|
|
auto c = run(no_model, nullptr);
|
|
c->Cut("cancel");
|
|
CHECK(no_model.Get().cut.empty());
|
|
c->Finish();
|
|
}
|
|
|
|
TEST_CASE("StillsGPUAnalysis_FramesDuringThePrePassAreAnalysed", "[StillsGPUAnalysis]") {
|
|
Logger logger(Catch::getResultCapture().getCurrentTestName());
|
|
const DiffractionExperiment x = StillsExperiment();
|
|
const PixelMask mask(x);
|
|
ImageBuffer buffer(64 * 1024 * 1024);
|
|
buffer.StartMeasurement(x);
|
|
const int64_t frames = 3 + 5;
|
|
REQUIRE(buffer.GetStatus().total_slots >= frames);
|
|
|
|
std::atomic<int> sunk = 0;
|
|
std::latch offered(1);
|
|
RugnuxLiveStatus status;
|
|
TestSink sink;
|
|
// The pre-pass is not over until every frame has been offered.
|
|
sink.ready = [&](const StillsSetup &) { offered.wait(); };
|
|
sink.analysed = [&](DataMessage &) { ++sunk; };
|
|
StillsGPUAnalysis gpu(x, mask, Start(x), nullptr, 2, 3, logger, sink, status);
|
|
CHECK(status.Get().stills->images_needed == 3);
|
|
for (int i = 0; i < frames; i++)
|
|
gpu.Hold(Frame(buffer, i));
|
|
offered.count_down();
|
|
REQUIRE_NOTHROW(gpu.Finish());
|
|
CHECK(sunk == frames);
|
|
CHECK(buffer.GetStatus().preparation_slots == 0);
|
|
}
|