Files
Jungfraujoch/writer/HDF5DataFilePluginMX.cpp
T
leonarski_fandClaude Opus 5.5 0a515bb086 rc179: everything on rc178 after its merge into main (#88), except the CHANGELOG
The rc178 branch continued after #88 was squash-merged; this is its content from b9ada7914 (the tree
#88 squashed) to 094eee31a, as one change on main. The individual commits stay on the rc178 branch:

- still battery: --cold evicts each set's input from the page cache; Alvra pixel masks; manifest fixes
- AGIPD: converter to one rigid panel per module, int16 photons, mask folded in; still battery reads the converted sets
- DetectorModules: panel look-up by id, plane -> panel, plane bounds and a per-pixel walk; mask size checked
- tests: [PanelLayout] equivalence harness, stacked vs assembled panel layout
- Dense per-pixel maps through the panel table (panel layout stage 1)
- Panel id on spots and the panel table in the start message
- Viewer and preview show a panel detector as it lies on the detector plane
- Panel layout stage 3: per-panel spot finding (CPU+GPU, CCL) and raster algorithms on the plane raster
- Panel layout stage 2: prediction and Bragg integration through the panel table
- XTC reader: frames handed up as stored, streams indexed in parallel
- XFEL reader: integer frames stay integer, one gain, placement by panel
- EXTERNAL_TEST_DATA_STILLS.md: images used vs the entry for every set; drop "Not in the battery"
- still battery: EuXFEL sets read the depositors' Cheetah CXI files directly; AGIPD converter removed
- float32_to_int16.py: a non-finite pixel becomes -32768, the int16 error marker, not a zero count
- CrystFEL geometry: read the pixel masks and bad regions instead of ignoring them
- Panel layout stage 5: the XFEL reader hands up the stored frame and its panel table
- Panel table persisted in NXmx; modulation grid and pre-scan memo key on the plane
- still battery: Alvra sets take their pixel mask from the CrystFEL geometry, not --mask-good
- still battery: take the deposited model as mmCIF where the RCSB has no PDB-format file
- still battery: dimple gets PDB-format coordinates converted from an mmCIF-only deposition
- stills fixed cost: geometry refinement and pre-scan do the same work in less wall time
- stills geometry sample and trial spot finding run as many workers as the image pass
- Stills integration: background ring reach measured on empty apertures, radial curvature from the whole frame
- StillsPartialityRefine: the partiality width is the run's, measured on the crystals, not the prediction window's
- MergeOnTheFly: capture term and multiplicative outlier band for the stills merge (both opt-in)
- Stills given a cell: the written cell is the mean over the frames that are merged
- Stills: the radial curvature correction decided per run on the same empty apertures
- StillsPartialityRefine: measure the run width only on shells with signal
- Stills pre-scan: report the paired z of the radial-correction comparison
- module placement: the five concurrent fits each keep every thread
- still battery: score R-free against the deposited data through the same dimple protocol
- still battery: the R-free target is the lower of the deposition's dimple and published values
- Stills: background clip follows the radial curve; signal disk from the lattice spacing
- Stills keep an overloaded observation as a lower bound instead of dropping it
- Stills: drop the lattice-spacing signal disk; the correction decision back on the plain clip
- battery: R-free against the deposition without refinement only, one protocol for rotation and stills
- still battery: EuXFEL sets run on a 2,000-image subset for now
- stills geometry refinement: fit the detector distance and the cell together
- stills geometry refinement: a distance given with --detector-distance is held in the joint fit
- Metal integration engine: back to its panel-s5 kernels; Mac stills keep the old clip, no saturation count
- Metal integration test: the CPU comparison runs on a rotation experiment
- Stills: kept overloads go back to the drop where they are not concentrated at low resolution
- stills distance search: an absolute 30-3000 mm scan, a sample found without d-limits, the fit iterated
- battery results: XFEL still battery 20261022-1358 on rc178 0cf62452c - 12 sets, cold, R_free unrefined
- battery results: still summary gains the resolution of our merge and of the deposition
- Pre-scan masks a pixel at the saturation limit on most frames, as one holding the error value
- Stills keep an overload as a lower bound only where it has pixels at the limit; run-level gate removed
- XFEL reader: a per-frame mask is read the way the frames are, its chunk inflated outside the HDF5 lock
- stills distance search: every centre scanned with a wider acceptance, a larger sample where nothing indexes
- rugnux usage: --beam-center-check describes the absolute 30-3000 mm stills distance scan, not the old 60-160 % grid
- stills: detector tilt refined in the geometry pre-pass, kept where held-out frames agree
- CLAUDE.md: the changelog is written once, just before the merge into main - condensed one-liners of what users get
- broker: serve the placed model and its maps of the last rugnux live run (/rugnux/model.cif, /rugnux/map.ccp4)
- frontend: Mol* viewer of the placed model and its density on the Rugnux live page
- writer: _model.cif from rugnux live keeps the input model's metadata, as the served model.cif does
- rugnux live maps: one unit cell centred on the model; the viewer opens on all of it
- Anomalous difference map: ANOM/PHANOM in _maps.mtz, /rugnux/map.ccp4?type=anom, a magenta mesh in the viewer
- Mol* viewer: open with the maps drawn within 10 A of the centre of view
- Mol* viewer: aaredb's structure-viewer setup, on a black stage
- rugnux: the gain of a stills file that states none is read off its single-photon peak where the background resolves photons
- Mol* viewer: in line with AareDB's StructureView at its current head (8ece2c2)
- ACKNOWLEDGEMENT: the web frontend's Mol* viewer follows AareDB's setup (G. Gotthard, PSI)
- stills tilt decision: the modules stay where the run placed them
- still battery: no distance pins and no per-set tuning flags; gain from the data except where a file's baseline hides it
- battery results: rc178 6f6f69 - full still battery and full rotation battery with REFMAC
- Bragg integration: a CCD rotation sweep's sigmas at the measured gain, intensities in photons
- BackgroundGain: the gain of a frame that resolves photons is the spacing of its photon peaks, so a baseline left in the frame no longer hides it
- ShadowFinder: measure the background's azimuthal modulation instead of predicting it from the polarization
- BackgroundGain: one gain estimate for rotation and stills; the still battery no longer gives EuXFEL 152 its gain
- ShadowFinder: measure the modulation robustly under a wide one-sided shadow
- ShadowFinderTest: a one-sided shadow is not fitted as part of the background's modulation
- ShadowFinder: back to the predicted polarization factor, in the plane the background shows
- MXAnalysisAfterFPGA: the Bragg engine's edge mask in the image's layout, so raw / untransformed FPGA runs start again
- battery results: still battery on rc178 e54f55 replaces the earlier rc178 still tables
- XFELReader: per-frame mask and photon conversion without a library call or a branch per pixel
- stills geometry trial: the hypotheses a search asks are indexed side by side
- stills tilt decision: the fits start beside the pre-scan and the geometry trial
- XFELReader: a deflated frame is inflated and converted a piece at a time, while it is in the cache
- XFELReader: a byte-shuffled frame is unshuffled a piece at a time, a whole pixel per step
- GeometryRefiner: residuals carry only the blocks a fit moves; beam and distance one block
- GeometryRefiner: a spot's residual differentiates its observed and predicted halves apart
- docs: EXTERNAL_TEST_DATA lists every open-arm battery set, with corrected values and counts
- battery: doc_check.py cross-checks the manifests against the dataset pages
- stills module placement: the placement fit gets half the threads, each held-out fit an eighth
- still battery: the EuXFEL sets run on all their frames, as the published still runs did
- battery results: still battery on rc178 a0ce6c, cold on a quiet machine, replaces the earlier rc178 table
- macOS build: explicit size_t in the XFEL reader's row bounds; parentheses in the CPU preprocessor's select

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015CeEGgpKdfkCTJb3YNMJcu
2026-10-23 17:56:42 +02:00

293 lines
13 KiB
C++

// SPDX-FileCopyrightText: 2024 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include "HDF5DataFilePluginMX.h"
inline std::string to_bravais_code(const std::optional<LatticeMessage> &lm_opt) {
if (!lm_opt.has_value()) return "";
const auto &lm = lm_opt.value();
const char C = lm.centering;
switch (lm.crystal_system) {
case gemmi::CrystalSystem::Triclinic:
if (C == 'P') return "aP";
break;
case gemmi::CrystalSystem::Monoclinic:
if (C == 'P') return "mP";
if (C == 'A') return "mA";
if (C == 'B') return "mB";
if (C == 'C') return "mC";
break;
case gemmi::CrystalSystem::Orthorhombic:
if (C == 'P') return "oP";
if (C == 'A') return "oA";
if (C == 'B') return "oB";
if (C == 'C') return "oC";
if (C == 'F') return "oF";
if (C == 'I') return "oI";
break;
case gemmi::CrystalSystem::Tetragonal:
if (C == 'P') return "tP";
if (C == 'I') return "tI";
break;
case gemmi::CrystalSystem::Trigonal:
if (C == 'R') return "hR"; // rhombohedral in hex setting
break;
case gemmi::CrystalSystem::Hexagonal:
if (C == 'P') return "hP";
break;
case gemmi::CrystalSystem::Cubic:
if (C == 'P') return "cP";
if (C == 'F') return "cF";
if (C == 'I') return "cI";
break;
}
return "";
}
HDF5DataFilePluginMX::HDF5DataFilePluginMX(const StartMessage &msg)
: max_spots(msg.max_spot_count),
max_extra_lattices(msg.max_extra_lattices),
indexing(msg.indexing_algorithm != IndexingAlgorithmEnum::None) {
}
void HDF5DataFilePluginMX::OpenFile(HDF5File &data_file, const DataMessage &msg, size_t images_per_file) {
bkg_estimate.reserve(images_per_file);
ice_ring_score.reserve(images_per_file);
spindle_blind_fraction.reserve(images_per_file);
if (max_spots == 0)
return;
spot_x.reserve(max_spots * images_per_file);
spot_y.reserve(max_spots * images_per_file);
spot_int.reserve(max_spots * images_per_file);
spot_ice_ring.reserve(max_spots * images_per_file);
spot_h.reserve(max_spots * images_per_file);
spot_k.reserve(max_spots * images_per_file);
spot_l.reserve(max_spots * images_per_file);
spot_dist_ewald.reserve(max_spots * images_per_file);
spot_panel.reserve(max_spots * images_per_file);
if (indexing) {
spot_indexed.reserve(max_spots * images_per_file);
spot_lattice.reserve(max_spots * images_per_file);
indexed_lattice.reserve(images_per_file * 9);
extra_lattices.reserve(images_per_file * max_extra_lattices * 9);
}
beam_corr_x.reserve(images_per_file);
beam_corr_y.reserve(images_per_file);
npeaks.reserve(images_per_file);
strong_pixel_count.reserve(images_per_file);
indexed.reserve(images_per_file);
profile_radius.reserve(images_per_file);
mosaicity_deg.reserve(images_per_file);
b_factor.reserve(images_per_file);
resolution_estimate.reserve(images_per_file);
spot_count_total.reserve(images_per_file);
spot_count_ice.reserve(images_per_file);
spot_count_ice_control.reserve(images_per_file);
spot_count_indexed.reserve(images_per_file);
spot_count_low_res.reserve(images_per_file);
integrated_reflections.reserve(images_per_file);
image_scale_factor.reserve(images_per_file);
image_scale_cc.reserve(images_per_file);
image_scale_mosaicity.reserve(images_per_file);
}
void HDF5DataFilePluginMX::Write(const DataMessage &msg, uint64_t image_number) {
if (msg.bkg_estimate.has_value())
bkg_estimate[image_number] = msg.bkg_estimate.value();
if (msg.ice_ring_score.has_value())
ice_ring_score[image_number] = msg.ice_ring_score.value();
if (msg.spindle_blind_fraction.has_value())
spindle_blind_fraction[image_number] = msg.spindle_blind_fraction.value();
if (max_spots == 0)
return;
if (image_number >= max_image_number) {
max_image_number = image_number;
spot_x.resize(max_spots * (max_image_number + 1));
spot_y.resize(max_spots * (max_image_number + 1));
spot_int.resize(max_spots * (max_image_number + 1));
spot_ice_ring.resize(max_spots * (max_image_number + 1));
spot_h.resize(max_spots * (max_image_number + 1));
spot_k.resize(max_spots * (max_image_number + 1));
spot_l.resize(max_spots * (max_image_number + 1));
spot_lattice.resize(max_spots * (max_image_number + 1), -1);
spot_dist_ewald.resize(max_spots * (max_image_number + 1), NAN);
spot_panel.resize(max_spots * (max_image_number + 1), -1);
if (indexing) {
spot_indexed.resize(max_spots * (max_image_number + 1));
indexed_lattice.resize((max_image_number + 1) * 9, NAN);
extra_lattices.resize((max_image_number + 1) * max_extra_lattices * 9, NAN);
}
}
uint32_t spot_cnt = std::min(msg.spots.size(), max_spots);
for (int i = 0; i < spot_cnt; i++) {
spot_x[max_spots * image_number + i] = msg.spots[i].x;
spot_y[max_spots * image_number + i] = msg.spots[i].y;
spot_int[max_spots * image_number + i] = msg.spots[i].intensity;
spot_panel[max_spots * image_number + i] = msg.spots[i].panel;
if (msg.spots[i].panel >= 0)
spot_panel_present = true;
if (indexing) {
spot_indexed[max_spots * image_number + i] = msg.spots[i].indexed;
spot_lattice[max_spots * image_number + i] = msg.spots[i].lattice;
spot_h[max_spots * image_number + i] = msg.spots[i].h;
spot_k[max_spots * image_number + i] = msg.spots[i].k;
spot_l[max_spots * image_number + i] = msg.spots[i].l;
spot_dist_ewald[max_spots * image_number + i] = msg.spots[i].dist_ewald_sphere;
}
spot_ice_ring[max_spots * image_number + i] = msg.spots[i].ice_ring;
}
npeaks[image_number] = spot_cnt;
strong_pixel_count[image_number] = msg.strong_pixel_count.value_or(0);
spot_count_total[image_number] = msg.spot_count.value_or(0);
spot_count_ice[image_number] = msg.spot_count_ice_rings.value_or(0);
spot_count_ice_control[image_number] = msg.spot_count_ice_control.value_or(NAN);
spot_count_low_res[image_number] = msg.spot_count_low_res.value_or(0);
if (indexing) {
indexed[image_number] = msg.indexing_result.value_or(0);
// Fallback value is indexing result (1 or 0 lattices)
indexing_lattice_count[image_number] = msg.indexing_lattice_count.value_or(indexed[image_number]);
profile_radius[image_number] = msg.profile_radius.value_or(NAN);
mosaicity_deg[image_number] = msg.mosaicity_deg.value_or(NAN);
b_factor[image_number] = msg.b_factor.value_or(NAN);
resolution_estimate[image_number] = msg.resolution_estimate.value_or(NAN);
beam_corr_x[image_number] = msg.beam_corr_x.value_or(NAN);
beam_corr_y[image_number] = msg.beam_corr_y.value_or(NAN);
spot_count_indexed[image_number] = msg.spot_count_indexed.value_or(0);
integrated_reflections[image_number] = msg.integrated_reflections.value_or(0);
if (msg.indexing_lattice) {
auto tmp = msg.indexing_lattice->GetVector();
for (int i = 0; i < 9; i++)
indexed_lattice[image_number * 9 + i] = tmp[i];
} else {
for (int i = 0; i < 9; i++)
indexed_lattice[image_number * 9 + i] = NAN;
}
for (size_t li = 0; li < max_extra_lattices; li++) {
const size_t base = (image_number * max_extra_lattices + li) * 9;
if (li < msg.indexing_extra_lattices.size()) {
auto tmp = msg.indexing_extra_lattices[li].GetVector();
for (int i = 0; i < 9; i++)
extra_lattices[base + i] = tmp[i];
} else {
for (int i = 0; i < 9; i++)
extra_lattices[base + i] = NAN;
}
}
if (msg.lattice_type) {
bravais_lattice[image_number] = to_bravais_code(msg.lattice_type);
niggli_class[image_number] = msg.lattice_type->niggli_class;
} else {
bravais_lattice[image_number] = "";
niggli_class[image_number] = 0;
}
if (msg.image_scale_factor.has_value())
image_scale_present = true;
image_scale_factor[image_number] = msg.image_scale_factor.value_or(NAN);
image_scale_cc[image_number] = msg.image_scale_cc.value_or(NAN);
image_scale_mosaicity[image_number] = msg.image_scale_mosaicity.value_or(NAN);
}
}
void HDF5DataFilePluginMX::WriteFinal(HDF5File &data_file) {
HDF5Group(data_file, "/entry/MX").NXClass("NXcollection");
if (!spot_x.empty()) {
data_file.SaveVector("/entry/MX/peakXPosRaw", spot_x, {(hsize_t) (max_image_number + 1), max_spots});
data_file.SaveVector("/entry/MX/peakYPosRaw", spot_y, {(hsize_t) (max_image_number + 1), max_spots});
data_file.SaveVector("/entry/MX/peakTotalIntensity", spot_int, {(hsize_t) (max_image_number + 1), max_spots});
data_file.SaveVector("/entry/MX/peakIceRingRes", spot_ice_ring, {(hsize_t) (max_image_number + 1), max_spots});
data_file.SaveVector("/entry/MX/nPeaks", npeaks.vec());
if (spot_panel_present)
data_file.SaveVector("/entry/MX/peakPanel", spot_panel, {(hsize_t) (max_image_number + 1), max_spots});
if (indexing) {
data_file.SaveVector("/entry/MX/peakH", spot_h, {(hsize_t) (max_image_number + 1), max_spots});
data_file.SaveVector("/entry/MX/peakK", spot_k, {(hsize_t) (max_image_number + 1), max_spots});
data_file.SaveVector("/entry/MX/peakL", spot_l, {(hsize_t) (max_image_number + 1), max_spots});
data_file.SaveVector("/entry/MX/peakDistEwaldSphere", spot_dist_ewald,
{(hsize_t) (max_image_number + 1), max_spots});
data_file.SaveVector("/entry/MX/peakIndexed", spot_indexed, {(hsize_t) (max_image_number + 1), max_spots});
data_file.SaveVector("/entry/MX/peakLattice", spot_lattice, {(hsize_t) (max_image_number + 1), max_spots});
}
}
if (!strong_pixel_count.empty())
data_file.SaveVector("/entry/MX/strongPixels", strong_pixel_count.vec());
if (!spot_count_ice.empty())
data_file.SaveVector("/entry/MX/peakCountIceRingRes", spot_count_ice.vec());
if (!spot_count_ice_control.empty())
data_file.SaveVector("/entry/MX/peakCountIceRingControl", spot_count_ice_control.vec());
if (!spot_count_indexed.empty())
data_file.SaveVector("/entry/MX/peakCountIndexed", spot_count_indexed.vec());
if (!spot_count_low_res.empty())
data_file.SaveVector("/entry/MX/peakCountLowRes", spot_count_low_res.vec());
if (!spot_count_total.empty())
data_file.SaveVector("/entry/MX/peakCountUnfiltered", spot_count_total.vec());
if (!indexed.empty())
data_file.SaveVector("/entry/MX/imageIndexed", indexed.vec());
if (!indexing_lattice_count.empty())
data_file.SaveVector("/entry/MX/indexingLatticeCount", indexing_lattice_count.vec());
if (!indexed_lattice.empty())
data_file.SaveVector("/entry/MX/latticeIndexed", indexed_lattice, {(hsize_t) (max_image_number + 1), 9})
->Units("Angstrom");
if (!extra_lattices.empty())
data_file.SaveVector("/entry/MX/latticeIndexedExtra", extra_lattices,
{(hsize_t) (max_image_number + 1), (hsize_t) max_extra_lattices, 9})
->Units("Angstrom");
if (!bkg_estimate.empty())
data_file.SaveVector("/entry/MX/bkgEstimate", bkg_estimate.vec());
if (!ice_ring_score.empty())
data_file.SaveVector("/entry/MX/iceRingScore", ice_ring_score.vec());
if (!spindle_blind_fraction.empty())
data_file.SaveVector("/entry/MX/spindleBlindFraction", spindle_blind_fraction.vec());
if (!profile_radius.empty())
data_file.SaveVector("/entry/MX/profileRadius", profile_radius.vec())->Units("Angstrom^-1");
if (!mosaicity_deg.empty())
data_file.SaveVector("/entry/MX/mosaicity", mosaicity_deg.vec())->Units("deg");
if (!b_factor.empty())
data_file.SaveVector("/entry/MX/bFactor", b_factor.vec())->Units("Angstrom^2");
if (!beam_corr_x.empty())
data_file.SaveVector("/entry/MX/beam_corr_x", beam_corr_x.vec())->Units("pixel");
if (!beam_corr_y.empty())
data_file.SaveVector("/entry/MX/beam_corr_y", beam_corr_y.vec())->Units("pixel");
if (!niggli_class.empty())
data_file.SaveVector("/entry/MX/niggliClass", niggli_class.vec());
if (!bravais_lattice.empty())
data_file.SaveVector("/entry/MX/bravaisLattice", bravais_lattice.vec());
if (!resolution_estimate.empty())
data_file.SaveVector("/entry/MX/resolutionEstimate", resolution_estimate.vec())->Units("Angstrom");
if (!integrated_reflections.empty())
data_file.SaveVector("/entry/MX/integratedReflections", integrated_reflections.vec());
if (image_scale_present) {
data_file.SaveVector("/entry/MX/imageScaleFactor", image_scale_factor.vec());
data_file.SaveVector("/entry/MX/imageScaleCC", image_scale_cc.vec());
data_file.SaveVector("/entry/MX/imageScaleMosaicity", image_scale_mosaicity.vec())->Units("deg");
}
}