Files
Jungfraujoch/tests/RugnuxLiveStatusTest.cpp
leonarski_f 4809c1622c
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 16m43s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 28m46s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 32m5s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 39m44s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 5m7s
Build Packages / Create release (push) Successful in 8s
Build Packages / Build documentation (push) Successful in 49s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 22m30s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 31m54s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 11m29s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 23m51s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 26m7s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 29m38s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 28m6s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 29m44s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 33m22s
Build Packages / Generate python client (push) Successful in 47s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 34m6s
Build Packages / Unit tests (push) Successful in 1h2m47s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 35m59s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 34m8s
Build Packages / Validation Battery (push) Successful in 38m31s
v1.0.0-rc.187 (#98)
* Rugnux: Refinement several times faster, one protocol on the CPU and the GPU, parts of it on the Apple GPU; its random draws are the same on every platform.
* Rugnux: Refinement follows common practice: the free set decides only the restraint weights and the displacement model (isotropic, TLS or anisotropic, by Hamilton's test), everything else is decided on the working set; lower R-free for placed and predicted models.
* Rugnux: Restraint dictionaries via `--restraints` (or among the inputs) and more built-in ligands; a model whose space group is a subgroup of the data's is refined in its own group; reflections plus a model select `--mode refine` on their own.
* Rugnux: `-C` fixes the crystal system on the axes as typed; `-S` alone fixes the space group only, the cell is still found and reduced; a space group or cell stored in the input file is ignored, with a line in the log.
* Rugnux: More reliable multi-sweep merging; `--mode scale` merges several stills runs as one; `_plot.txt` and the report name each input; a second crystal form is reported for stills given a cell.
* jfjoch_broker (experimental): Live Rugnux on FPGA detectors (JUNGFRAU) takes the same path as a DECTRIS stream, rotation and serial alike, model step included, with analysis threads per GPU; frames waiting for the analysis can be viewed; no host stage drops a frame - a source faster than the analysis is held at the card.
* jfjoch_broker: Summation of more than 256 frames per image is split between the card and the host (`summation_mode` is then `hybrid`), and live Rugnux runs on such collections; pedestal runs take a receiver of their own.
* jfjoch_broker (experimental): `stills_downsample` analyses 1 of every N serial images (chosen by a hash; the first 2000 all; the others written without analysis; reset to 1 by `/initialize`); `max_spot_count` left out means 250 for per-image indexing and up to 1000 for live stills; the live status gives the merge before the model work, the fitted resolution and the anomalous peak height of each unexplained blob.
* jfjoch_broker: Without a `pcie` entry the broker runs on a mock of the card instead of the HLS simulation; its spot, ROI and azimuthal results are zero then.
* Web frontend: Redesigned Rugnux live page; Vite 8, TypeScript 6, Mol* 5.13 and dependency security updates.
* Tools: `jfjoch_fpga_stills_test` measures live stills on the cards with a stored dataset; `jfjoch_fpga_test` loses the HLS-simulation option.
* Other: FFTW no longer fetched or linked (the CPU FFT indexer keeps the name `FFTW`); PCIe driver now GPL-2.0-or-later and third-party notices complete; several crash fixes.

Reviewed-on: #98
Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
2026-11-19 20:49:28 +01:00

603 lines
23 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 <cctype>
#include <cmath>
#include <map>
#include <sstream>
#include "../receiver/RugnuxLiveStatus.h"
#include "../receiver/RugnuxLiveStatus.h"
#include "../rugnux/Rugnux.h"
#include "../broker/OpenAPIConvert.h"
#include "../image_puller/TestImagePuller.h"
using namespace std::chrono_literals;
TEST_CASE("RugnuxLiveStatus_TimesFromFirstImage", "[RugnuxLive]") {
// 11 images at 100 Hz, the first 3 s after START, reported out of order as several forward threads do
const auto start = std::chrono::steady_clock::now() - 10s;
const auto first = start + 3s;
RugnuxLiveStatus status;
status.Start(11, 0.11, start);
for (int i: {1, 0, 2, 3, 4, 5, 6, 7, 8, 10, 9})
status.ImageReceived(first + i * 10ms);
auto s = status.Get();
CHECK(s.expected_collection_s.value() == Catch::Approx(0.11));
REQUIRE(s.images_received == 11);
REQUIRE(s.start_to_first_image_s.value() == Catch::Approx(3.0));
CHECK(!s.last_image_s);
// "now" is 10 s after START, 7 s after the first image
CHECK(s.elapsed_s == Catch::Approx(7.0).margin(0.5));
REQUIRE(status.LastImage() == first + 100ms);
s = status.Get();
REQUIRE(s.last_image_s.value() == Catch::Approx(0.1));
CHECK((s.images_received - 1) / s.last_image_s.value() == Catch::Approx(100.0));
}
TEST_CASE("RugnuxLiveStatus_BeforeFirstImage", "[RugnuxLive]") {
RugnuxLiveStatus status;
status.Start(10, std::nullopt, std::chrono::steady_clock::now() - 2s);
status.Step("waiting");
const auto s = status.Get();
CHECK(!s.start_to_first_image_s);
CHECK(!s.expected_collection_s);
CHECK(s.elapsed_s == 0.0);
REQUIRE(s.steps.size() == 1);
CHECK(s.steps[0].started_s <= 0.0);
CHECK(!status.LastImage());
}
TEST_CASE("RugnuxLiveStatus_RunLatticeOvertakesProbe", "[RugnuxLive]") {
RugnuxLiveStatus status;
status.Start(10, 1.0, std::chrono::steady_clock::now());
// The probe answers first, the run indexes later: the banner is the run's
status.Probe("no lattice on the first 60 deg");
CHECK(status.Get().probe == "no lattice on the first 60 deg");
status.Lattice("lattice on the sweep: monoclinic P: 35.00 29.00 63.00 90.0 105.0 90.0");
CHECK(status.Get().probe.starts_with("lattice on the sweep"));
// The run indexed before the probe answered: the probe's answer is not shown
status.Probe("no lattice on the first 60 deg");
CHECK(status.Get().probe.starts_with("lattice on the sweep"));
// The next collection starts afresh
status.Start(10, 1.0, std::chrono::steady_clock::now());
status.Probe("no lattice on the first 60 deg");
CHECK(status.Get().probe == "no lattice on the first 60 deg");
}
TEST_CASE("RugnuxLiveStatus_Clear", "[RugnuxLive]") {
RugnuxLiveStatus status;
status.Start(10, 1.0, std::chrono::steady_clock::now() - 2s);
status.ImageReceived(std::chrono::steady_clock::now() - 1s);
status.Step("indexing");
status.Probe("lattice found");
status.Result("OK", "fine", {{"Space group", "P 1"}}, {"a warning"});
status.Finish(RugnuxLiveState::Done);
REQUIRE(status.LastImage());
status.Clear();
const auto s = status.Get();
CHECK(s.state == RugnuxLiveState::Idle);
CHECK(s.images_expected == 0);
CHECK(s.images_received == 0);
CHECK(s.steps.empty());
CHECK(s.probe.empty());
CHECK(s.verdict.empty());
CHECK(s.summary.empty());
CHECK(s.warnings.empty());
CHECK(!s.start_to_first_image_s);
CHECK(!s.last_image_s);
CHECK(s.elapsed_s == 0.0);
CHECK(!status.LastImage());
}
TEST_CASE("TestImagePuller_SetsReceiveTime", "[RugnuxLive]") {
TestImagePuller puller;
const auto before = std::chrono::steady_clock::now();
puller.Put(ImagePullerOutput{});
const auto given = before - 5s;
puller.Put(ImagePullerOutput{.received = given});
auto out = puller.PollImage();
REQUIRE(out);
CHECK(out->received >= before);
out = puller.PollImage();
REQUIRE(out);
CHECK(out->received == given);
}
TEST_CASE("RugnuxLiveModel_MolecularReplacement", "[RugnuxLive]") {
ProcessResult r;
CHECK(!LiveModel(r));
MolecularReplacementResult mr;
mr.ok = true;
mr.model_name = "model.cif";
mr.predicted_model = true;
mr.decision = "AGREES";
mr.d_min_search = 2.5;
MRCandidate c;
c.space_group = *gemmi::find_spacegroup_by_name("P 1 21 1");
c.copies = 2;
c.tf_z = 12.5;
c.rf_z = 6.0;
c.null_sigma = 20.0;
mr.candidates = {MRCandidate{}, c};
mr.chosen = 1;
r.model_prior = mr;
ModelValidationResult v;
v.ok = true;
v.r_work = 0.38;
v.r_free = 0.40;
v.cc_model_overall = 0.71;
v.mean_atom_density_sigma = 1.6;
v.anomalous_sites = {{"SD MET A1", 5.5}};
r.model_validation = v;
r.model_placement = "MR";
const auto m = LiveModel(r);
REQUIRE(m);
CHECK(m->placement == "MR");
CHECK(m->predicted);
CHECK(m->mr_space_group == "P 1 21 1");
CHECK(m->copies.value() == 2);
CHECK(m->tf_z.value() == Catch::Approx(12.5));
CHECK(!m->llg_z);
CHECK(m->fit_sigma.value() == Catch::Approx(20.0)); // MR's null: the rigid body's was not tested
CHECK(m->r_free.value() == Catch::Approx(0.40));
CHECK(m->cc_model.value() == Catch::Approx(0.71));
CHECK(!m->rigid_body_shift_A);
CHECK(m->anomalous_site == "SD MET A1");
RugnuxLiveStatus status;
status.Start(10, 1.0, std::chrono::steady_clock::now());
status.Result("OK", "fine", {}, {}, true);
status.Model(*m);
nlohmann::json j = Convert(status.Get());
CHECK(j["anomalous_signal"] == true);
CHECK(j["model"]["placement"] == "MR");
CHECK(j["model"]["copies"] == 2);
CHECK(j["model"]["r_free"].get<double>() == Catch::Approx(0.40));
CHECK(!j["model"].contains("llg_z"));
CHECK(!j["model"].contains("rigid_body_angle_deg"));
status.Clear();
CHECK(!status.Get().model);
CHECK(!status.Get().anomalous_signal);
}
TEST_CASE("RugnuxLiveStatus_Headline", "[RugnuxLive]") {
RugnuxLiveStatus status;
status.Start(10, 1.0, std::chrono::steady_clock::now());
nlohmann::json j = Convert(status.Get());
CHECK(!j.contains("space_group"));
CHECK(!j.contains("d_min"));
CHECK(!j.contains("isa"));
CHECK(!j.contains("resolution_fit"));
status.Headline("P 1 21 1", 1.25, 24.2, 1.31);
j = Convert(status.Get());
CHECK(j["space_group"] == "P 1 21 1");
CHECK(j["d_min"].get<double>() == Catch::Approx(1.25));
CHECK(j["isa"].get<double>() == Catch::Approx(24.2));
CHECK(j["resolution_fit"].get<double>() == Catch::Approx(1.31));
// ISa undetermined, no fit
status.Headline("P 1 21 1", 1.25, std::nullopt);
j = Convert(status.Get());
CHECK(!j.contains("isa"));
CHECK(!j.contains("resolution_fit"));
status.Clear();
CHECK(status.Get().space_group.empty());
CHECK(!status.Get().d_min);
}
TEST_CASE("RugnuxLiveModel_Refinement", "[RugnuxLive]") {
ProcessResult r;
ModelValidationResult v;
v.ok = true;
v.r_work = 0.38;
v.r_free = 0.40;
r.model_validation = v;
r.model_placement = "RIGID_BODY";
CHECK(!LiveModel(r)->refinement);
RefinementResult rf;
rf.ok = true;
rf.on_gpu = true;
rf.seconds = 3.5;
rf.r_work_placement = 0.30;
rf.r_free_placement = 0.33;
rf.restrained = true;
rf.r_work = 0.19;
rf.r_free = 0.23;
rf.geometry.rmsd_bonds = 0.011;
rf.geometry.rmsd_angles = 1.6;
DensityBlob b;
b.kind = DensityBlob::Kind::Ligand;
b.centroid = gemmi::Position(1.0, 2.0, 3.0);
b.peak_sigma = 7.5;
b.volume = 40.0;
b.nearest_atom = "OG1 THR A42";
b.nearest_atom_distance = 3.1;
b.anomalous_sigma = 2.5;
rf.blobs.assign(RugnuxLiveRefinement::MAX_BLOBS + 5, b);
r.refinement = rf;
const auto m = LiveModel(r);
REQUIRE(m->refinement);
const RugnuxLiveRefinement &lr = *m->refinement;
CHECK(lr.performed);
CHECK(lr.r_free.value() == Catch::Approx(0.23));
CHECK(lr.rmsd_bonds_A.value() == Catch::Approx(0.011));
CHECK(lr.blob_count == static_cast<int64_t>(RugnuxLiveRefinement::MAX_BLOBS + 5));
CHECK(lr.blobs.size() == RugnuxLiveRefinement::MAX_BLOBS);
CHECK(lr.blobs[0].anomalous_sigma.value() == Catch::Approx(2.5));
// the placed model's R-factors are untouched
CHECK(m->r_free.value() == Catch::Approx(0.40));
RugnuxLiveStatus status;
status.Start(10, 1.0, std::chrono::steady_clock::now());
status.Model(*m);
nlohmann::json j = Convert(status.Get());
CHECK(j["refined_model"] == false);
CHECK(j["model"]["r_free"].get<double>() == Catch::Approx(0.40));
CHECK(j["model"]["refinement"]["r_free"].get<double>() == Catch::Approx(0.23));
CHECK(j["model"]["refinement"]["on_gpu"] == true);
CHECK(j["model"]["refinement"]["blob_count"] == RugnuxLiveRefinement::MAX_BLOBS + 5);
CHECK(j["model"]["refinement"]["blobs"].size() == RugnuxLiveRefinement::MAX_BLOBS);
CHECK(j["model"]["refinement"]["blobs"][0]["kind"] == "LIGAND");
CHECK(j["model"]["refinement"]["blobs"][0]["z"].get<double>() == Catch::Approx(3.0));
// Only the placement stage ran: its R-factors, no geometry
rf.restrained = false;
r.refinement = rf;
const auto p = LiveModel(r);
CHECK(p->refinement->r_free.value() == Catch::Approx(0.33));
CHECK(!p->refinement->rmsd_bonds_A);
// Failed
RefinementResult failed;
failed.failure_reason = "no dictionary";
r.refinement = failed;
status.Model(*LiveModel(r));
j = Convert(status.Get());
CHECK(j["model"]["refinement"]["performed"] == false);
CHECK(j["model"]["refinement"]["reason"] == "no dictionary");
CHECK(!j["model"]["refinement"].contains("r_free"));
}
TEST_CASE("RugnuxLiveModel_RefinementCancelled", "[RugnuxLive]") {
ProcessResult r;
ModelValidationResult v;
v.ok = true;
r.model_validation = v;
r.model_placement = "MR";
RefinementResult rf;
rf.ok = true;
rf.cancelled = true;
r.refinement = rf;
const auto m = LiveModel(r);
CHECK(m->refinement->cancelled);
RugnuxLiveStatus status;
status.Start(10, 1.0, std::chrono::steady_clock::now());
status.Model(*m);
const nlohmann::json j = Convert(status.Get());
CHECK(j["model"]["refinement"]["cancelled"] == true);
}
// A score that is not a number is left out, not written as null: the page reads every present one as a number.
TEST_CASE("RugnuxLiveModel_NonFiniteScores", "[RugnuxLive]") {
ProcessResult r;
MolecularReplacementResult mr;
mr.ok = true;
mr.d_min_search = NAN;
MRCandidate c;
c.space_group = *gemmi::find_spacegroup_by_name("P 1 21 1");
c.tf_z = NAN;
c.rf_z = INFINITY;
mr.candidates = {c};
mr.chosen = 0;
r.model_prior = mr;
ModelValidationResult v;
v.ok = true;
v.r_work = NAN;
v.r_free = NAN;
v.mean_atom_density_sigma = NAN;
r.model_validation = v;
r.model_placement = "MR";
RefinementResult rf;
rf.ok = true;
rf.restrained = true;
rf.r_work = NAN;
rf.r_free = 0.25;
rf.geometry.rmsd_bonds = NAN;
DensityBlob b;
b.nearest_atom_distance = INFINITY; // and no anomalous map: anomalous_sigma NaN
rf.blobs = {b};
r.refinement = rf;
const auto m = LiveModel(r);
REQUIRE(m);
CHECK(!m->tf_z);
CHECK(!m->rf_z);
CHECK(!m->mr_d_min);
CHECK(!m->r_free);
CHECK(!m->map_atom_sigma);
CHECK(!m->refinement->r_work);
CHECK(m->refinement->r_free.value() == Catch::Approx(0.25));
CHECK(!m->refinement->rmsd_bonds_A);
CHECK(!m->refinement->blobs.at(0).nearest_atom_distance_A);
CHECK(!m->refinement->blobs.at(0).anomalous_sigma);
RugnuxLiveStatus status;
status.Start(10, 1.0, std::chrono::steady_clock::now());
status.Model(*m);
const nlohmann::json j = Convert(status.Get());
CHECK(!j["model"].contains("tf_z"));
CHECK(!j["model"].contains("rf_z"));
CHECK(!j["model"].contains("r_free"));
CHECK(!j["model"]["refinement"].contains("r_work"));
CHECK(!j["model"]["refinement"]["blobs"][0].contains("nearest_atom_distance_A"));
CHECK(!j["model"]["refinement"]["blobs"][0].contains("anomalous_sigma"));
CHECK(j.dump().find("null") == std::string::npos);
}
TEST_CASE("RugnuxLiveStatus_StepsAfterCancel", "[RugnuxLive]") {
// A step covers the images that came: after a cancel, not the ones it was planned for
const auto start = std::chrono::steady_clock::now();
RugnuxLiveStatus status;
status.Start(1000, 10.0, start);
status.Step("Integration (geometry pre-pass), 600 images");
for (int i = 0; i < 384; i++)
status.ImageReceived(start + i * 10ms);
status.Step("Pre-scan");
status.Cut("cancel");
status.Step("Integration, 1000 images");
status.Step("Integration, 200 images");
const auto s = status.Get();
REQUIRE(s.steps.size() == 4);
CHECK(s.steps[0].title == "Integration (geometry pre-pass), 384 images");
CHECK(s.steps[1].title == "Pre-scan");
CHECK(s.steps[2].title == "Integration, 384 images");
CHECK(s.steps[3].title == "Integration, 200 images");
}
TEST_CASE("RugnuxLiveStatus_StepsAfterDeadline", "[RugnuxLive]") {
RugnuxLiveStatus status;
status.Start(1000, 10.0, std::chrono::steady_clock::now());
status.Step("Integration, 1000 images");
status.Cut("deadline");
CHECK(status.Get().steps[0].title == "Integration, 1000 images");
}
namespace {
MergeStatisticsShell TestShell(float d_max, float d_min, int n_obs, int n_unique, int n_possible) {
MergeStatisticsShell sh;
sh.d_max = d_max;
sh.d_min = d_min;
sh.total_observations = n_obs;
sh.unique_reflections = n_unique;
sh.possible_unique_reflections = n_possible;
sh.mean_i_over_sigma = 2000.0 * d_min / n_obs;
sh.cc_half = 0.5 + 0.1 * d_min;
sh.r_merge = 0.02 / d_min;
sh.r_meas = 0.03 / d_min;
sh.r_pim = 0.01 / d_min;
return sh;
}
// The rows of the report's shell table (operator<< of MergeStatistics, which the report prints) by their
// d_min label, and the rows of its serial table
std::map<std::string, std::vector<std::string>> ReportRows(const MergeStatistics &ms, bool serial) {
std::ostringstream text;
text << ms;
std::istringstream lines(text.str());
std::map<std::string, std::vector<std::string>> rows;
bool in_serial = false;
for (std::string line; std::getline(lines, line);) {
if (line.find("Serial statistics") != std::string::npos)
in_serial = true;
std::istringstream words(line);
std::vector<std::string> row;
for (std::string w; words >> w;)
row.push_back(w);
if (in_serial == serial && !row.empty() && (std::isdigit(row[0][0]) || row[0] == "Overall"))
rows[row[0]] = row;
}
return rows;
}
std::string Percent(const std::optional<double> &v) {
return v ? fmt::format("{:.1f}%", 100.0 * v.value()) : "-";
}
std::string Plain(const std::optional<double> &v) {
return v ? fmt::format("{:.1f}", v.value()) : "-";
}
}
TEST_CASE("RugnuxLiveShells_AreTheReportTable", "[RugnuxLive]") {
MergeStatistics ms;
ms.shells = {TestShell(50.0, 4.0, 4000, 1000, 1010), TestShell(4.0, 3.0, 3000, 1000, 1050),
TestShell(3.0, 2.5, 0, 0, 900), // empty: no row in the report, none here
TestShell(2.5, 2.2, 900, 600, 1000)};
ms.shells[1].cc_ref = 0.93;
ms.shells[3].mean_i_over_sigma = NAN;
ms.overall = TestShell(50.0, 2.2, 7900, 2600, 3960);
SECTION("rotation") {
const auto shells = LiveShells(ms);
const auto rows = ReportRows(ms, false);
REQUIRE(shells.size() == 3);
REQUIRE(rows.size() == 4); // three shells and the Overall row
for (const auto &sh : shells) {
const auto &row = rows.at(fmt::format("{:.2f}", sh.d_min));
// d_min N_obs N_uniq N_possib Compl Mult <I/sig> R_merge R_meas R_pim CC1/2 CC* CCref CCanom SigAno
REQUIRE(row.size() == 15);
CHECK(row[1] == std::to_string(sh.n_obs));
CHECK(row[2] == std::to_string(sh.n_unique));
CHECK(row[4] == Percent(sh.completeness));
CHECK(row[5] == Plain(sh.multiplicity));
CHECK(row[6] == Plain(sh.i_over_sigma));
CHECK(row[8] == Percent(sh.r_meas));
CHECK(row[10] == Percent(sh.cc_half));
CHECK(row[12] == Percent(sh.cc_ref));
CHECK(!sh.r_split);
CHECK(!sh.r_free);
}
CHECK(shells[0].d_max == Catch::Approx(50.0));
CHECK(shells[1].cc_ref);
CHECK(!shells[2].i_over_sigma);
// The overall row of the same table
const auto overall = LiveOverall(ms);
const auto &row = rows.at("Overall");
REQUIRE(row.size() == 15);
CHECK(row[1] == std::to_string(overall.n_obs));
CHECK(row[2] == std::to_string(overall.n_unique));
CHECK(row[4] == Percent(overall.completeness));
CHECK(row[5] == Plain(overall.multiplicity));
CHECK(row[6] == Plain(overall.i_over_sigma));
CHECK(row[8] == Percent(overall.r_meas));
CHECK(row[10] == Percent(overall.cc_half));
CHECK(overall.d_min == Catch::Approx(2.2));
}
SECTION("stills") {
ms.stills = true;
ms.shells[0].n_split_pairs = 800;
ms.shells[0].r_split = 0.12;
ms.shells[0].cc_half_split = 0.9;
ms.overall.n_split_pairs = 800;
const auto shells = LiveShells(ms);
const auto rows = ReportRows(ms, false);
const auto serial = ReportRows(ms, true);
REQUIRE(shells.size() == 3);
REQUIRE(rows.size() == 4); // three shells and the Overall row
for (const auto &sh : shells) {
const auto &row = rows.at(fmt::format("{:.2f}", sh.d_min));
// No R_merge / R_meas / R_pim on stills
REQUIRE(row.size() == 12);
CHECK(row[1] == std::to_string(sh.n_obs));
CHECK(row[6] == Plain(sh.i_over_sigma));
CHECK(row[7] == Percent(sh.cc_half));
CHECK(row[9] == Percent(sh.cc_ref));
CHECK(!sh.r_meas);
// d_min N_pairs CC1/2 CC* R_split, for the shells both halves measured
const auto it = serial.find(fmt::format("{:.2f}", sh.d_min));
CHECK((it != serial.end()) == sh.r_split.has_value());
if (it != serial.end())
CHECK(it->second[4] == Percent(sh.r_split));
}
}
SECTION("R-free of the model validated on the merge") {
ModelValidationResult v;
v.cc_model_shells = {{4.0f, 0.9, 900, NAN, NAN, NAN, 0.21, 50},
{3.0f, 0.8, 900, NAN, NAN, NAN, 0.27, 45},
{2.5f, NAN, 0, NAN, NAN, NAN, NAN, 0},
{2.2f, 0.4, 500, NAN, NAN, NAN, NAN, 0}}; // no free reflection
const auto shells = LiveShells(ms, &v);
REQUIRE(shells.size() == 3);
CHECK(shells[0].r_free.value() == Catch::Approx(0.21));
CHECK(shells[1].r_free.value() == Catch::Approx(0.27));
CHECK(!shells[2].r_free);
v.ok = true;
v.r_free = 0.25;
CHECK(LiveOverall(ms, &v).r_free.value() == Catch::Approx(0.25));
RugnuxLiveStatus status;
status.Start(10, 1.0, std::chrono::steady_clock::now());
status.Shells("50% of the images (provisional)", LiveShells(ms), LiveOverall(ms));
// A model of another merge does not label this one
status.ShellRFree("the collection", v);
CHECK(!status.Get().shells[0].r_free);
CHECK(!status.Get().overall->r_free);
status.ShellRFree("50% of the images (provisional)", v);
CHECK(status.Get().shells[0].r_free.value() == Catch::Approx(0.21));
CHECK(status.Get().overall->r_free.value() == Catch::Approx(0.25));
nlohmann::json j = Convert(status.Get());
CHECK(j["shells_merge"] == "50% of the images (provisional)");
REQUIRE(j["shells"].size() == 3);
CHECK(j["shells"][0]["d_min"].get<double>() == Catch::Approx(4.0));
CHECK(j["shells"][0]["d_max"].get<double>() == Catch::Approx(50.0));
CHECK(j["shells"][0]["r_free"].get<double>() == Catch::Approx(0.21));
CHECK(j["shells"][0]["r_meas"].get<double>() == Catch::Approx(0.03 / 4.0));
CHECK(!j["shells"][0].contains("cc_ref"));
CHECK(!j["shells"][0].contains("r_split"));
CHECK(!j["shells"][2].contains("i_over_sigma"));
REQUIRE(j.contains("overall"));
CHECK(j["overall"]["n_unique"].get<int64_t>() == 2600);
CHECK(j["overall"]["cc_half"].get<double>() == Catch::Approx(0.5 + 0.1 * 2.2f));
CHECK(j["overall"]["r_free"].get<double>() == Catch::Approx(0.25));
status.Clear();
CHECK(status.Get().shells.empty());
CHECK(!status.Get().overall);
j = Convert(status.Get());
CHECK(!j.contains("shells"));
CHECK(!j.contains("overall"));
}
}
TEST_CASE("RugnuxLiveStatus_OverallRFreeIsTheFinalModels", "[RugnuxLive]") {
MergeStatistics ms;
ms.shells = {TestShell(50.0, 4.0, 4000, 1000, 1010), TestShell(4.0, 2.2, 3000, 1000, 1050)};
ms.overall = TestShell(50.0, 2.2, 7000, 2000, 2060);
ModelValidationResult v;
v.ok = true;
v.r_work = 0.40;
v.r_free = 0.42;
v.cc_model_shells = {{4.0f, 0.9, 900, NAN, NAN, NAN, 0.35, 50}, {2.2f, 0.8, 900, NAN, NAN, NAN, 0.47, 45}};
RefinementResult rf;
rf.ok = true;
rf.r_work_placement = 0.36;
rf.r_free_placement = 0.39;
rf.restrained = true;
rf.r_work = 0.33;
rf.r_free = 0.37;
// Not refined: the placed model's, as the report's R_FREE
CHECK(LiveOverall(ms, &v).r_free.value() == Catch::Approx(0.42));
RefinementResult failed;
CHECK(LiveOverall(ms, &v, &failed).r_free.value() == Catch::Approx(0.42));
// Refined: the refined model's, as model.refinement.r_free and the report's REFINE_R_FREE
CHECK(LiveOverall(ms, &v, &rf).r_free.value() == Catch::Approx(0.37));
rf.restrained = false;
CHECK(LiveOverall(ms, &v, &rf).r_free.value() == Catch::Approx(0.39));
rf.restrained = true;
ProcessResult r;
r.model_validation = v;
r.model_placement = "RIGID_BODY";
r.refinement = rf;
RugnuxLiveStatus status;
status.Start(10, 1.0, std::chrono::steady_clock::now());
status.Model(*LiveModel(r));
status.Shells("the collection", LiveShells(ms, &v), LiveOverall(ms, &v, &rf));
nlohmann::json j = Convert(status.Get());
CHECK(j["overall"]["r_free"].get<double>() == j["model"]["refinement"]["r_free"].get<double>());
CHECK(j["model"]["r_free"].get<double>() == Catch::Approx(0.42));
// The shells stay the placed model's: the refinement measures no shell
CHECK(j["shells"][0]["r_free"].get<double>() == Catch::Approx(0.35));
// Serial: the model placed on a merge and refined after its table was shown
status.Shells("the collection", LiveShells(ms), LiveOverall(ms));
status.ShellRFree("the collection", v, &rf);
CHECK(status.Get().overall->r_free.value() == Catch::Approx(0.37));
CHECK(status.Get().shells[1].r_free.value() == Catch::Approx(0.47));
}