One changeset, developed together in response to a review of this branch, so the files carry several of the changes at once. Full test suite passes (733 cases). Spot finding - Split ImageSpotFinder into Detect() (flag strong pixels - the expensive per-pixel pass) and ExtractSpots() (CCL + min/max-pix + resolution mask), with Run() = both. The per-image min-pix escalation now detects ONCE and repeats only the cheap extraction, instead of re-running the whole finder four times per frame as it did on the default path. It also keeps the winning attempt's spot list rather than re-extracting it, so the frame that is integrated is exactly the frame that was scored - which a GPU re-extract could not guarantee (float atomic ordering). - spot_finding_time_s no longer swallows indexing time, and indexing_time_s now sums every escalation call instead of reporting only the last. Detection limits follow the detector - The azimuthal-integration upper q and the spot-finding high-resolution limit are now std::optional, in the C++ structs AND in the OpenAPI schema, and resolve to the detector's own maximum (DiffractionExperiment::GetDetectorMaxQ_ recipA). Adaptive detection reads a pixel's ring from the azimuthal bins, so a pixel outside that q range could never be strong - the integration range silently bounded what detection could see, regardless of the requested resolution limit. Regenerated the C++ and TypeScript clients; the viewer and the web frontend each gained a "to detector edge" switch. Detection defaults are now per workflow (measured, not assumed) - Stills: adaptive detection, min-pix chosen per image, no resolution clipping. - Rotation: fixed-threshold finder, min-pix 2, 1.5 A limit. On a 33-crystal rotation battery, adaptive detection helped four hard crystals but deterministically broke three (a lost space group, a halved indexing rate, a collapsed merge), and the detector-edge limit cost indexing on a strong rotation set (100.0 -> 96.8%). Each is still overridable by its flag, and --no-adaptive-spots is new. Indexer seed escalation - Stop escalating once a seed's lattice explains >= 90% of the seed spots. Previously any frame with >= 80 spots always paid three indexer calls, online broker included. Merge-consistency filter - --min-image-cc gated on a per-image CC computed BEFORE the stills partiality post-refinement and never refreshed; the refiner now recomputes it, so the reported CC describes the data that are actually merged. - Replaced the per-call cc_mask argument with one MergeOnTheFly flag, so the merge, the error model and MergeStats can no longer disagree about which images are in (the --scale path merged unfiltered while its statistics were filtered). Per-image B-factor refinement (-B) removed - Measured on four serial-stills datasets: it is a no-op where the per-image fit is well conditioned and actively harmful where it is not (CC1/2 -8.1, R_meas +23.2 on the weakest large-cell set, whose fits hit their [-50, 200] bounds on 14-25% of images). It had also been silently DISCARDED since the partiality post-refinement landed - reported but not applied. Rather than fix and keep a knob with no demonstrated benefit, the flag and the whole image_scale_b_factor chain are gone: setting, scaling fit, message field, CBOR, HDF5 write and read-back, per-image plot, OpenAPI enum, viewer column and checkbox, docs. ScaleOnTheFly no longer needs Ceres at all - the fit is a linear IRLS. (The Wilson per-image b_factor is a different quantity and stays.) Stills partiality width now fits both of its components - sigma^2 = gamma0^2 + (gamma_e*d*)^2 instead of a purely angular gamma_e*d* with gamma0 pinned to 0. Fitted per crystal by least squares of dist_ewald^2 on d*^2. The angular-only width is fitted over a d*^2-dense population, so it was pinned by the high-resolution edge and collapsed at low d*: median partiality 0.008 beyond 13 A for reflections that were plainly recorded, 55% of them under the merge's partiality floor, and the survivors divided by those values - which inflated the merged low-resolution intensity scale 3.6x (~ +9 A^2 of apparent B). Measured on 5000 stills: the ramp flattens to 0.89x, no observation is dropped any more (701750 -> 716811), shell-mean CC1/2 and R-free improve slightly. Note CC1/2, R_meas, completeness and a B-refining R-free are all blind to that ramp, which is why it survived earlier validation; the cost is high-resolution R_meas (98.5 -> 101.9 shell-averaged). Removed dead code from add-then-remove churn - Prediction-time "still partiality" (unreachable: no setter), the phantom IndexingSettings::min_indexed_spot_fraction knob (getter, no setter - now the constant it always was), StillsPartialityRefine's caller-less Settings constructor and its reference to a long-gone env var, ProcessImage's unread bool return, an unused include, and a dead viewer overlay hook. Also - Viewer: the magnifier compared a QImage with itself, so its scene rect was set once ever and it could not pan into a larger dataset; the hover tail timer could fire after leaveEvent and resurrect the resolution readout outside the image. - update_version.sh regenerated the frontend lock file BEFORE bumping the version (every release shipped an off-by-one lock), and did git rm/git add on a path that has not existed since the client moved to src/client - with no set -e, both failed silently. - fpga/pcie_driver/postinstall.sh tested "[ ! occurrences > 0 ]", which is a redirect, not a test, so dkms add never ran. - Unit tests for the adaptive-threshold host functions, which had none. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
277 lines
12 KiB
C++
277 lines
12 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);
|
|
|
|
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);
|
|
|
|
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_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 (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);
|
|
|
|
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;
|
|
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_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 (!image_scale_factor.empty())
|
|
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 (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_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 (!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");
|
|
}
|
|
}
|