A cancelled collection showed verdict FAILED, "No crystal lattice was found in these frames" and "cancelled after 0 images", with step titles carrying the planned image counts. The live status now has verdict CANCELLED, says how many images came before the cancel, and a step title's ", N images" is capped at the images received once the run is cut by a cancel. The report of a rugnux CLI run is unchanged. The frontend shows CANCELLED as info, not as an error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HVeDQsU5a8AJUdJPgWevAK
266 lines
9.8 KiB
C++
266 lines
9.8 KiB
C++
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#include "RugnuxLiveStatus.h"
|
|
|
|
#include <algorithm>
|
|
#include <cctype>
|
|
#include <cmath>
|
|
|
|
#include "../rugnux/Rugnux.h"
|
|
|
|
double RugnuxLiveStatus::Since(std::chrono::steady_clock::time_point t) const {
|
|
return std::chrono::duration<double>(t - start).count();
|
|
}
|
|
|
|
double RugnuxLiveStatus::Now() const {
|
|
return Since(std::chrono::steady_clock::now());
|
|
}
|
|
|
|
// A step of the run, "..., N images", covers no more images than came: after a cancel the run goes on over those.
|
|
void RugnuxLiveStatus::CapImages(std::string &title) const {
|
|
const std::string suffix = " images";
|
|
const size_t pos = title.rfind(", ");
|
|
if (snapshot.cut != "cancel" || pos == std::string::npos || !title.ends_with(suffix))
|
|
return;
|
|
const std::string count = title.substr(pos + 2, title.size() - pos - 2 - suffix.size());
|
|
if (count.empty() || !std::all_of(count.begin(), count.end(), ::isdigit))
|
|
return;
|
|
if (std::stoll(count) > snapshot.images_received)
|
|
title = title.substr(0, pos + 2) + std::to_string(snapshot.images_received) + suffix;
|
|
}
|
|
|
|
void RugnuxLiveStatus::CloseStep() {
|
|
if (!snapshot.steps.empty() && !snapshot.steps.back().duration_s)
|
|
snapshot.steps.back().duration_s = Now() - snapshot.steps.back().started_s;
|
|
}
|
|
|
|
void RugnuxLiveStatus::Start(int64_t images_expected, std::optional<double> expected_collection_s,
|
|
std::chrono::steady_clock::time_point start_received) {
|
|
std::unique_lock ul(status_mutex);
|
|
snapshot = RugnuxLiveSnapshot{};
|
|
snapshot.state = RugnuxLiveState::Running;
|
|
snapshot.images_expected = images_expected;
|
|
snapshot.expected_collection_s = expected_collection_s;
|
|
start = start_received;
|
|
last_image.reset();
|
|
}
|
|
|
|
void RugnuxLiveStatus::ImageReceived(std::chrono::steady_clock::time_point received) {
|
|
std::unique_lock ul(status_mutex);
|
|
++snapshot.images_received;
|
|
// Several forward threads report, so the images need not come here in the order they arrived.
|
|
const double t = Since(received);
|
|
if (!snapshot.start_to_first_image_s || t < snapshot.start_to_first_image_s.value())
|
|
snapshot.start_to_first_image_s = t;
|
|
if (!last_image || received > last_image.value())
|
|
last_image = received;
|
|
}
|
|
|
|
void RugnuxLiveStatus::Step(const std::string &title) {
|
|
std::unique_lock ul(status_mutex);
|
|
CloseStep();
|
|
snapshot.steps.push_back(RugnuxLiveStep{.title = title, .started_s = Now()});
|
|
CapImages(snapshot.steps.back().title);
|
|
snapshot.step_progress.reset();
|
|
}
|
|
|
|
void RugnuxLiveStatus::Note(const std::string &text) {
|
|
std::unique_lock ul(status_mutex);
|
|
if (snapshot.steps.empty())
|
|
snapshot.steps.push_back(RugnuxLiveStep{.title = "", .started_s = Now()});
|
|
snapshot.steps.back().notes.push_back(text);
|
|
}
|
|
|
|
void RugnuxLiveStatus::Progress(uint64_t done, uint64_t total) {
|
|
std::unique_lock ul(status_mutex);
|
|
if (total > 0)
|
|
snapshot.step_progress = static_cast<float>(done) / static_cast<float>(total);
|
|
}
|
|
|
|
void RugnuxLiveStatus::Finish(RugnuxLiveState state, const std::string &message) {
|
|
std::unique_lock ul(status_mutex);
|
|
CloseStep();
|
|
snapshot.elapsed_s = Now();
|
|
snapshot.state = state;
|
|
snapshot.message = message;
|
|
snapshot.step_progress.reset();
|
|
}
|
|
|
|
void RugnuxLiveStatus::Result(const std::string &verdict, const std::string &verdict_text,
|
|
const std::vector<std::pair<std::string, std::string>> &summary,
|
|
const std::vector<std::string> &warnings, bool anomalous_signal) {
|
|
std::unique_lock ul(status_mutex);
|
|
snapshot.verdict = verdict;
|
|
snapshot.verdict_text = verdict_text;
|
|
snapshot.summary = summary;
|
|
snapshot.warnings = warnings;
|
|
snapshot.anomalous_signal = anomalous_signal;
|
|
}
|
|
|
|
void RugnuxLiveStatus::Model(const RugnuxLiveModel &model) {
|
|
std::unique_lock ul(status_mutex);
|
|
snapshot.model = model;
|
|
}
|
|
|
|
void RugnuxLiveStatus::Headline(const std::string &space_group, std::optional<double> d_min,
|
|
std::optional<double> isa) {
|
|
std::unique_lock ul(status_mutex);
|
|
snapshot.space_group = space_group;
|
|
snapshot.d_min = d_min;
|
|
snapshot.isa = isa;
|
|
}
|
|
|
|
void RugnuxLiveStatus::Stills(const RugnuxLiveStills &stills) {
|
|
std::unique_lock ul(status_mutex);
|
|
snapshot.stills = stills;
|
|
}
|
|
|
|
void RugnuxLiveStatus::Latency(double last_image_to_written_s) {
|
|
std::unique_lock ul(status_mutex);
|
|
snapshot.last_image_to_written_s = last_image_to_written_s;
|
|
}
|
|
|
|
std::optional<std::chrono::steady_clock::time_point> RugnuxLiveStatus::LastImage() {
|
|
std::unique_lock ul(status_mutex);
|
|
if (last_image)
|
|
snapshot.last_image_s = Since(last_image.value());
|
|
return last_image;
|
|
}
|
|
|
|
void RugnuxLiveStatus::Cut(const std::string &reason) {
|
|
std::unique_lock ul(status_mutex);
|
|
snapshot.cut = reason;
|
|
snapshot.cut_s = Now();
|
|
for (auto &step: snapshot.steps)
|
|
CapImages(step.title);
|
|
}
|
|
|
|
void RugnuxLiveStatus::Probe(const std::string &text) {
|
|
std::unique_lock ul(status_mutex);
|
|
snapshot.probe = text;
|
|
snapshot.probe_s = Now();
|
|
}
|
|
|
|
void RugnuxLiveStatus::Warning(const std::string &text) {
|
|
std::unique_lock ul(status_mutex);
|
|
snapshot.warnings.push_back(text);
|
|
}
|
|
|
|
void RugnuxLiveStatus::Structure(std::shared_ptr<const RugnuxStructure> structure,
|
|
std::shared_ptr<const RugnuxStructure> refined_structure) {
|
|
std::unique_lock ul(status_mutex);
|
|
snapshot.structure = std::move(structure);
|
|
snapshot.refined_structure = std::move(refined_structure);
|
|
}
|
|
|
|
void RugnuxLiveStatus::Clear() {
|
|
std::unique_lock ul(status_mutex);
|
|
snapshot = RugnuxLiveSnapshot{};
|
|
last_image.reset();
|
|
}
|
|
|
|
RugnuxLiveSnapshot RugnuxLiveStatus::Get() const {
|
|
std::unique_lock ul(status_mutex);
|
|
RugnuxLiveSnapshot ret = snapshot;
|
|
if (snapshot.state == RugnuxLiveState::Running)
|
|
ret.elapsed_s = Now();
|
|
|
|
// Held since START, reported since the first image - or, before it has come, since now.
|
|
const double zero = snapshot.start_to_first_image_s.value_or(snapshot.state == RugnuxLiveState::Running ? ret.elapsed_s : 0.0);
|
|
ret.elapsed_s -= zero;
|
|
for (auto &step: ret.steps)
|
|
step.started_s -= zero;
|
|
if (ret.probe_s)
|
|
*ret.probe_s -= zero;
|
|
if (ret.last_image_s)
|
|
*ret.last_image_s -= zero;
|
|
if (ret.cut_s)
|
|
*ret.cut_s -= zero;
|
|
return ret;
|
|
}
|
|
|
|
namespace {
|
|
// A score the status carries only where it is a number: JSON has no NaN or infinity (nlohmann writes null)
|
|
void SetIfFinite(std::optional<double> &out, double v) {
|
|
if (std::isfinite(v))
|
|
out = v;
|
|
}
|
|
}
|
|
|
|
std::optional<RugnuxLiveModel> LiveModel(const ProcessResult &result) {
|
|
if (!result.model_validation && !result.model_prior)
|
|
return std::nullopt;
|
|
RugnuxLiveModel m;
|
|
m.placement = result.model_placement;
|
|
if (result.model_prior) {
|
|
const MolecularReplacementResult &mr = *result.model_prior;
|
|
m.model_name = mr.model_name;
|
|
m.predicted = mr.predicted_model;
|
|
m.mr_decision = mr.decision;
|
|
if (!mr.ok)
|
|
m.reason = mr.failure_reason;
|
|
if (mr.chosen >= 0) {
|
|
const MRCandidate &c = mr.candidates[mr.chosen];
|
|
m.mr_space_group = c.space_group.xhm();
|
|
m.copies = c.copies;
|
|
SetIfFinite(m.tf_z, c.tf_z);
|
|
SetIfFinite(m.rf_z, c.rf_z);
|
|
SetIfFinite(m.llg_z, c.llg_z);
|
|
SetIfFinite(m.fit_sigma, c.null_sigma);
|
|
SetIfFinite(m.mr_d_min, mr.d_min_search);
|
|
}
|
|
}
|
|
if (result.model_validation) {
|
|
const ModelValidationResult &v = *result.model_validation;
|
|
if (!v.model_name.empty())
|
|
m.model_name = v.model_name;
|
|
if (!v.ok) {
|
|
if (m.reason.empty())
|
|
m.reason = v.failure_reason;
|
|
} else {
|
|
SetIfFinite(m.r_work, v.r_work);
|
|
SetIfFinite(m.r_free, v.r_free);
|
|
SetIfFinite(m.cc_model, v.cc_model_overall);
|
|
if (v.fit_tested)
|
|
SetIfFinite(m.fit_sigma, v.r_work_sigma);
|
|
SetIfFinite(m.map_atom_sigma, v.mean_atom_density_sigma);
|
|
if (v.rigid_body_applied) {
|
|
SetIfFinite(m.rigid_body_angle_deg, v.rigid_body_angle_deg);
|
|
SetIfFinite(m.rigid_body_shift_A, v.rigid_body_shift_A);
|
|
}
|
|
if (!v.anomalous_sites.empty()) {
|
|
m.anomalous_site = v.anomalous_sites.front().label;
|
|
SetIfFinite(m.anomalous_site_sigma, v.anomalous_sites.front().sigma);
|
|
}
|
|
}
|
|
}
|
|
if (result.refinement) {
|
|
const RefinementResult &rf = *result.refinement;
|
|
RugnuxLiveRefinement r;
|
|
r.performed = rf.ok;
|
|
r.cancelled = rf.cancelled;
|
|
r.reason = rf.failure_reason;
|
|
r.on_gpu = rf.on_gpu;
|
|
r.seconds = rf.seconds;
|
|
if (rf.ok) {
|
|
SetIfFinite(r.r_work, rf.restrained ? rf.r_work : rf.r_work_placement);
|
|
SetIfFinite(r.r_free, rf.restrained ? rf.r_free : rf.r_free_placement);
|
|
if (rf.restrained) {
|
|
SetIfFinite(r.rmsd_bonds_A, rf.geometry.rmsd_bonds);
|
|
SetIfFinite(r.rmsd_angles_deg, rf.geometry.rmsd_angles);
|
|
}
|
|
}
|
|
r.blob_count = static_cast<int64_t>(rf.blobs.size());
|
|
for (size_t i = 0; i < std::min(rf.blobs.size(), RugnuxLiveRefinement::MAX_BLOBS); i++) {
|
|
const DensityBlob &b = rf.blobs[i];
|
|
RugnuxLiveBlob blob{DensityBlobKindName(b.kind), b.centroid.x, b.centroid.y, b.centroid.z, b.peak_sigma,
|
|
b.volume, b.nearest_atom, std::nullopt};
|
|
SetIfFinite(blob.nearest_atom_distance_A, b.nearest_atom_distance); // infinite: no atom near
|
|
r.blobs.push_back(blob);
|
|
}
|
|
m.refinement = std::move(r);
|
|
}
|
|
return m;
|
|
}
|