Files
Jungfraujoch/image_analysis/spot_finding/ImageSpotFinder.cpp
T
leonarski_f 680c36c20d
Build Packages / Unit tests (push) Successful in 1h22m15s
Build Packages / build:windows:nocuda (push) Successful in 18m0s
Build Packages / build:windows:cuda (push) Successful in 20m30s
Build Packages / build:viewer-tgz:cpu (push) Successful in 10m32s
Build Packages / build:viewer-tgz:cuda (push) Successful in 11m39s
Build Packages / build:rugnux-tgz (x86_64) (push) Successful in 8m55s
Build Packages / build:rugnux:windows (push) Successful in 11m25s
Build Packages / build:rpm (rocky8_nocuda) (push) Successful in 20m6s
Build Packages / build:rpm (rocky9_nocuda) (push) Successful in 16m27s
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Successful in 20m19s
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Successful in 15m34s
Build Packages / build:rpm (rocky8_sls9) (push) Successful in 20m25s
Build Packages / build:rpm (rocky9_sls9) (push) Successful in 19m36s
Build Packages / build:rpm (rocky8) (push) Successful in 17m43s
Build Packages / build:rpm (rocky9) (push) Successful in 13m34s
Build Packages / build:rpm (ubuntu2204) (push) Successful in 21m28s
Build Packages / build:rpm (ubuntu2404) (push) Successful in 18m19s
Build Packages / DIALS test (push) Successful in 12m36s
Build Packages / XDS test (durin plugin) (push) Successful in 6m56s
Build Packages / XDS test (JFJoch plugin) (push) Successful in 6m48s
Build Packages / XDS test (neggia plugin) (push) Successful in 6m7s
Build Packages / Generate python client (push) Successful in 11s
Build Packages / Build documentation (push) Successful in 36s
Build Packages / Create release (push) Skipped
Build Packages / build:rugnux:aarch64 (cross) (push) Successful in 5m11s
v1.0.0-rc.166 (#76)
* `rugnux --mode calibration` writes `<prefix>.json` beside the `.poni`, whose `dataset_settings` member is a `jfjoch_broker` `dataset_settings` body as it stands.
* `rugnux` and `jfjoch_viewer` read PILATUS miniCBF sweeps natively, without conversion.
* Masters written by other facilities open, including Eiger 1.x and third-party NXmx variants.
* `rugnux` measures the beam centre on every run, and indexes with it when the file's value indexes nothing.
* A detector swung out on a 2theta arm is placed where the file says it stands, and the calibration can hold the tilt fixed.
* `rugnux` writes the unmerged MTZ by default, and a P1 merge beside it, so a wrong space group can be re-merged without reprocessing.
* Significant improvements to symmetry handling in `rugnux`: the lattice, the point group, the setting and the systematic absences.
* The `rugnux` report gives the resolution the CC1/2 fit reached, beside the range the reflections were written to.
* The `rugnux` report gives the twinning statistics measured before the space group was decided, beside the ones measured after.
* The `rugnux` report gives the strong-direction diffraction limit, and warns when CC1/2 is not monotone with resolution.
* `rugnux` ranks screw axes on the evidence their absences carry, rather than on how many control reflections a candidate happens to have.
* Twinning is no longer reported when the L-test contradicts it.
* The `rugnux` report gives the detector tilt, the measured tilt and the direct beam beside the beam centre, and a post-refined beam centre is judged against the run's own measurement rather than the file's.
* `--no-refine-tilt` holds the detector tilt at the value in the file, instead of zeroing it, when the calibration starts from the spots.
* The `jfjoch_viewer` grid scan view draws the cells in the proportion of the scan steps, so the map has the shape of the scanned area.

Reviewed-on: #76
Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
2026-09-02 21:17:31 +02:00

114 lines
4.9 KiB
C++

// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include <bit>
#include "../../common/JFJochException.h"
#include "ImageSpotFinder.h"
#include "StrongPixelSet.h"
ImageSpotFinder::ImageSpotFinder(int32_t width, int32_t height, bool host_bit_buffer)
: width(width),
height(height),
output_buffer(host_bit_buffer ? width * height / 32 + 1 : 0),
res_mask_bits(OutputSize(), 0) {
// Exclude the padding bits of the last word up front, so neither the host scan nor the GPU
// compaction needs a separate "is this bit still inside the image?" test.
const size_t npixel = static_cast<size_t>(width) * height;
if (npixel % 32 != 0)
res_mask_bits.back() = ~((1u << (npixel % 32)) - 1u);
}
size_t ImageSpotFinder::OutputSize() const {
return (width * height) / 32 + ((width * height % 32 != 0) ? 1 : 0);
}
size_t ImageSpotFinder::OutputByteSize() const {
return OutputSize() * sizeof(uint32_t);
}
void ImageSpotFinder::SetResolutionMask(const std::vector<bool> &mask) {
const size_t npixel = static_cast<size_t>(width) * height;
if (mask.size() != npixel)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"ImageSpotFinder::SetResolutionMask: mask size mismatch");
std::vector<uint32_t> packed(OutputSize(), 0);
for (size_t i = 0; i < npixel; i++)
if (mask[i])
packed[i / 32] |= 1u << (i % 32);
SetResolutionMaskBits(packed);
}
void ImageSpotFinder::SetResolutionMaskBits(const std::vector<uint32_t> &packed_mask) {
if (packed_mask.size() != OutputSize())
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"ImageSpotFinder::SetResolutionMaskBits: mask size mismatch");
res_mask_bits = packed_mask;
const size_t npixel = static_cast<size_t>(width) * height;
if (npixel % 32 != 0)
res_mask_bits.back() |= ~((1u << (npixel % 32)) - 1u);
}
const std::vector<float> &ImageSpotFinder::GetRingBackground() const {
static const std::vector<float> none;
return none;
}
void ImageSpotFinder::ExtractComponentsHost(const ImagePreprocessorBuffer &image,
const SpotFindingSettings &settings) {
// Collect the strong pixels first and read their values afterwards, instead of reading the image
// pixel by pixel: on the GPU that read is a device gather, which is what lets the preprocessed
// image stay on the device instead of being copied back in full for every frame.
strong_pixel.clear();
for (size_t i = 0; i < OutputSize(); i++) {
// The resolution mask is packed like the bit buffer, so a whole word of it is excluded at
// once instead of testing 32 bits one at a time.
uint32_t word = output_buffer[i] & ~res_mask_bits[i];
while (word != 0) {
strong_pixel.push_back(static_cast<uint32_t>(i * 32 + std::countr_zero(word)));
word &= word - 1;
}
}
strong_pixel_count = static_cast<uint32_t>(strong_pixel.size());
components.clear();
// The connected-component search gives up on a frame with this many strong pixels, so their values
// are of no use - not even worth gathering off the device.
if (strong_pixel.size() >= StrongPixelLimit(static_cast<size_t>(width) * height))
return;
image.Gather(strong_pixel, strong_pixel_value);
StrongPixelSet pixel_set;
for (size_t i = 0; i < strong_pixel.size(); i++)
pixel_set.AddStrongPixel(strong_pixel[i] % width, strong_pixel[i] / width, strong_pixel_value[i]);
pixel_set.FindComponentsImage(settings, components);
}
const std::vector<DiffractionSpot> &ImageSpotFinder::ExtractComponents(const ImagePreprocessorBuffer &image,
const SpotFindingSettings &settings) {
ExtractComponentsHost(image, settings);
return components;
}
std::vector<DiffractionSpot> ImageSpotFinder::Filter(const std::vector<DiffractionSpot> &in,
const SpotFindingSettings &settings) {
std::vector<DiffractionSpot> out;
const int64_t min_pix = settings.min_pix_per_spot.value_or(2);
for (const auto &spot: in)
if (spot.PixelCount() >= min_pix)
out.push_back(spot);
return out;
}
std::vector<DiffractionSpot> ImageSpotFinder::ExtractSpots(const ImagePreprocessorBuffer &image,
const SpotFindingSettings &settings) {
return Filter(ExtractComponents(image, settings), settings);
}
std::vector<DiffractionSpot> ImageSpotFinder::Run(const ImagePreprocessorBuffer &image,
const SpotFindingSettings &settings) {
Detect(image, settings);
return ExtractSpots(image, settings);
}