Files
Jungfraujoch/writer/RugnuxModelFile.cpp
T
leonarski_f 8261b7354e
Build Packages / Create release (push) Successful in 9s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 5m8s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 11m12s
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 16m43s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 20m56s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 22m4s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 26m57s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 28m24s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 27m58s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 29m29s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 18m23s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 19m55s
Build Packages / Generate python client (push) Successful in 32s
Build Packages / Build documentation (push) Successful in 1m32s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 27m20s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 29m27s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 32m41s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 34m46s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 37m25s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 35m56s
Build Packages / Unit tests (push) Successful in 2h8m44s
Build Packages / Validation Battery (push) Successful in 33m35s
v1.0.0-rc.176 (#86)
* jfjoch_broker: Optional live Rugnux analysis of DECTRIS rotation collections inside the broker, with merged data, maps and the placed model written beside the master file, the rotation lattice in the scan result, and a "Rugnux live" tab and dashboard row in the web frontend.
* Rugnux: Faster processing, with molecular replacement and model validation spread over several GPUs and run on the Apple GPU on macOS.
* Rugnux: The polarization stated in miniCBF and d*TREK headers is used, twinned crystals' space groups are decided more reliably, and a run that finds no lattice still reports every image.

Reviewed-on: #86
Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
2026-10-19 07:03:28 +02:00

100 lines
3.8 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include "RugnuxModelFile.h"
#include <fstream>
#include <iterator>
#include <stdexcept>
#include <zlib.h>
#include <nlohmann/json.hpp>
#include <gemmi/mmread.hpp>
#include <gemmi/calculate.hpp> // count_atom_sites
namespace {
// The bytes of a gzip stream inflated, every member of it.
std::string Gunzip(const std::string &bytes) {
z_stream s{};
if (inflateInit2(&s, 15 + 32) != Z_OK)
throw std::runtime_error("cannot start zlib");
std::string out;
char buf[1 << 16];
s.next_in = reinterpret_cast<Bytef *>(const_cast<char *>(bytes.data()));
s.avail_in = static_cast<uInt>(bytes.size());
int ret = Z_OK;
while (ret != Z_STREAM_END || s.avail_in > 0) {
if (ret == Z_STREAM_END)
inflateReset(&s);
s.next_out = reinterpret_cast<Bytef *>(buf);
s.avail_out = sizeof buf;
ret = inflate(&s, Z_NO_FLUSH);
if (ret != Z_OK && ret != Z_STREAM_END) {
inflateEnd(&s);
throw std::runtime_error("the gzip stream is damaged");
}
out.append(buf, sizeof buf - s.avail_out);
if (ret == Z_OK && s.avail_in == 0 && s.avail_out != 0) {
inflateEnd(&s);
throw std::runtime_error("the gzip stream is cut short");
}
}
inflateEnd(&s);
return out;
}
std::string ReadFile(const std::string &path) {
std::ifstream f(path, std::ios::binary);
if (!f)
throw std::runtime_error("cannot open " + path);
return {std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>()};
}
} // namespace
RugnuxModel ParseRugnuxModel(const std::string &bytes, const std::string &name, const std::string &pae_json) {
RugnuxModel m;
m.name = name;
std::string text = bytes.size() >= 2 && bytes[0] == '\x1f' && bytes[1] == '\x8b' ? Gunzip(bytes) : bytes;
gemmi::cif::Document doc;
m.structure = gemmi::read_structure_from_memory(text.data(), text.size(), name, gemmi::CoorFormat::Detect, &doc);
if (gemmi::count_atom_sites(m.structure) == 0)
throw std::runtime_error(name + " holds no atoms");
m.modelcif = m.structure.input_format == gemmi::CoorFormat::Mmcif && !doc.blocks.empty()
&& doc.blocks[0].has_mmcif_category("_ma_qa_metric");
if (!pae_json.empty()) {
try {
const nlohmann::json j = nlohmann::json::parse(pae_json);
const nlohmann::json &pae = j.is_array() ? j.at(0).at("predicted_aligned_error")
: j.at("predicted_aligned_error");
std::vector<std::vector<double>> rows;
for (const auto &row : pae)
rows.push_back(row.get<std::vector<double>>());
m.pae = std::move(rows);
} catch (const std::exception &) {
}
}
return m;
}
RugnuxModel ReadRugnuxModelFile(const std::string &path) {
const std::string pae_path = PaePathFor(path);
std::string pae_json;
if (!pae_path.empty() && std::ifstream(pae_path))
pae_json = ReadFile(pae_path);
return ParseRugnuxModel(ReadFile(path), path, pae_json);
}
std::string PaePathFor(const std::string &model_path) {
const size_t slash = model_path.find_last_of("/\\");
const std::string dir = slash == std::string::npos ? std::string() : model_path.substr(0, slash + 1);
const std::string name = model_path.substr(dir.size());
const size_t at = name.rfind("-model_v");
if (at == std::string::npos)
return {};
const size_t dot = name.find('.', at);
const std::string version = name.substr(at + 8, dot == std::string::npos ? std::string::npos : dot - at - 8);
return dir + name.substr(0, at) + "-predicted_aligned_error_v" + version + ".json";
}