Files
Jungfraujoch/image_analysis/pixel_refinement/PixelRefine.cpp
T
leonarski_f 47dc19dd03
Build Packages / build:rpm (rocky8_nocuda) (push) Successful in 21m4s
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Successful in 17m57s
Build Packages / build:rpm (rocky9_nocuda) (push) Successful in 20m42s
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Successful in 17m29s
Build Packages / build:rpm (rocky8_sls9) (push) Successful in 19m56s
Build Packages / build:rpm (rocky9_sls9) (push) Successful in 23m29s
Build Packages / build:rpm (rocky8) (push) Successful in 20m41s
Build Packages / build:rpm (ubuntu2204) (push) Successful in 20m30s
Build Packages / build:rpm (rocky9) (push) Successful in 24m48s
Build Packages / Generate python client (push) Successful in 29s
Build Packages / Build documentation (push) Successful in 1m27s
Build Packages / Create release (push) Skipped
Build Packages / build:rpm (ubuntu2404) (push) Successful in 20m52s
Build Packages / XDS test (durin plugin) (push) Successful in 17m17s
Build Packages / XDS test (JFJoch plugin) (push) Successful in 16m57s
Build Packages / XDS test (neggia plugin) (push) Successful in 14m40s
Build Packages / DIALS test (push) Successful in 27m3s
Build Packages / Unit tests (push) Successful in 2h20m37s
PixelRefine: Improvements to accept more reasonable count of reflections
2026-06-12 10:12:23 +02:00

1250 lines
57 KiB
C++

// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include "PixelRefine.h"
#include <algorithm>
#include <cmath>
#include <limits>
#include <vector>
#include <Eigen/Dense>
#include <ceres/ceres.h>
#include <ceres/rotation.h>
#include "../geom_refinement/LatticeReduction.h"
namespace {
// Per-pixel observation, in *raw* detector counts (no per-pixel solid-angle or
// polarization correction - same units the "normal" integrator works in; the
// per-reflection polarization correction is applied via ReflGroup::pol).
struct PixelObs {
double x, y; // detector pixel coordinate
double Iobs; // raw pixel value (signal + background)
double Ibkg; // local background estimate (per-shoebox level, raw counts)
double weight; // 1 / sigma_pixel
};
// One reflection together with the pixels of its shoebox.
struct ReflGroup {
int h, k, l;
double d;
double Itrue; // reference intensity (held fixed)
double R_bw_sq; // bandwidth radial-width^2 contribution (0 = monochromatic)
double pol; // per-reflection polarization correction (raw = true * pol)
double Ibkg; // local flat background (raw counts, constant over the shoebox)
double predicted_x, predicted_y;
std::vector<PixelObs> pixels;
};
double SafeInv(double x, double fallback) {
if (!std::isfinite(x) || std::fabs(x) < 1e-30)
return fallback;
return 1.0 / x;
}
// Median of a vector (in place, partially reorders it).
double MedianInPlace(std::vector<double> &v) {
if (v.empty())
return 0.0;
const size_t mid = v.size() / 2;
std::nth_element(v.begin(), v.begin() + mid, v.end());
if (v.size() % 2 == 1)
return v[mid];
const double hi = v[mid];
std::nth_element(v.begin(), v.begin() + mid - 1, v.begin() + mid);
return 0.5 * (v[mid - 1] + hi);
}
// Mask marking the *core* (radius `radius`) of every predicted spot, so that the
// local-background sampling of one reflection never picks up a neighbouring
// reflection's signal. Same idea as BraggIntegrate2D::BuildReflectionMask.
std::vector<uint8_t> BuildSpotMask(const std::vector<Reflection> &predicted, int nrefl,
size_t xpixel, size_t ypixel, int radius) {
std::vector<uint8_t> mask(xpixel * ypixel, 0);
const double r_sq = static_cast<double>(radius) * radius;
for (int i = 0; i < nrefl; ++i) {
const auto &r = predicted[i];
const int cx = static_cast<int>(std::lround(r.predicted_x));
const int cy = static_cast<int>(std::lround(r.predicted_y));
const int x0 = std::max(0, cx - radius);
const int x1 = std::min<int>(static_cast<int>(xpixel) - 1, cx + radius);
const int y0 = std::max(0, cy - radius);
const int y1 = std::min<int>(static_cast<int>(ypixel) - 1, cy + radius);
for (int y = y0; y <= y1; ++y) {
for (int x = x0; x <= x1; ++x) {
const double dx = x - r.predicted_x;
const double dy = y - r.predicted_y;
if (dx * dx + dy * dy <= r_sq)
mask[static_cast<size_t>(xpixel) * y + x] = 1;
}
}
}
return mask;
}
// Square shoebox bounds (inclusive) around a predicted spot, clamped to the
// detector. The centre is rounded to the nearest pixel with std::lround so the
// signal box is centred identically to the spot-core mask (BuildSpotMask) and
// the local-background ring (EstimateLocalBackground), which also lround. Used by
// Run and the diagnostic renderers so all three share one shoebox definition.
struct ShoeboxBox { int min_x, max_x, min_y, max_y; };
ShoeboxBox ShoeboxBounds(double px, double py, int radius, size_t xpixel, size_t ypixel) {
const int cx = static_cast<int>(std::lround(px));
const int cy = static_cast<int>(std::lround(py));
return {
std::max(cx - radius, 0),
std::min<int>(cx + radius, static_cast<int>(xpixel) - 1),
std::max(cy - radius, 0),
std::min<int>(cy + radius, static_cast<int>(ypixel) - 1)
};
}
// Local flat background around one shoebox, in raw detector counts. Samples the
// square ring shoebox_radius < max(|dx|,|dy|) <= bkg_outer_radius centred on the
// spot, dropping pixels that belong to any spot core (spot_mask) or carry a
// masked/saturated sentinel, and returns the median (robust to residual spot
// tails / zingers). Mirrors the local-background of BraggIntegrate2D, replacing
// the azimuthal-bin mean that proved a poor proxy for reflection background.
template<class T>
bool EstimateLocalBackground(const T *image,
const std::vector<uint8_t> &spot_mask,
size_t xpixel, size_t ypixel,
double cx, double cy,
int shoebox_radius, int bkg_outer_radius,
double &bkg_mean) {
const int icx = static_cast<int>(std::lround(cx));
const int icy = static_cast<int>(std::lround(cy));
const int x0 = std::max(0, icx - bkg_outer_radius);
const int x1 = std::min<int>(static_cast<int>(xpixel) - 1, icx + bkg_outer_radius);
const int y0 = std::max(0, icy - bkg_outer_radius);
const int y1 = std::min<int>(static_cast<int>(ypixel) - 1, icy + bkg_outer_radius);
std::vector<double> vals;
vals.reserve(static_cast<size_t>((x1 - x0 + 1) * (y1 - y0 + 1)));
for (int y = y0; y <= y1; ++y) {
for (int x = x0; x <= x1; ++x) {
// Skip the square shoebox core: that is signal, not background.
if (std::abs(x - icx) <= shoebox_radius && std::abs(y - icy) <= shoebox_radius)
continue;
const size_t np = static_cast<size_t>(xpixel) * y + x;
if (spot_mask[np])
continue;
const T raw = image[np];
if (raw == std::numeric_limits<T>::max())
continue;
if (std::is_signed_v<T> && raw == std::numeric_limits<T>::min())
continue;
vals.push_back(static_cast<double>(raw));
}
}
if (vals.size() < 5)
return false;
bkg_mean = MedianInPlace(vals);
return true;
}
// Per-pixel: map a detector pixel through the current geometry into the
// reference reciprocal frame. Cheap (a few trig + one rotation); depends on the
// pixel and the detector geometry, not on the lattice.
template<typename T>
void ObservedRecip(const T *beam, const T *distance_mm, const T *detector_rot,
double obs_x, double obs_y,
double pixel_size, double inv_lambda,
Eigen::Matrix<T, 3, 1> &e_obs_recip) {
// PyFAI convention (left-handed for rot1/rot2): rot3 = 0 assumed.
const T c1 = ceres::cos(detector_rot[0]);
const T s1 = ceres::sin(detector_rot[0]);
const T c2 = ceres::cos(detector_rot[1]);
const T s2 = ceres::sin(detector_rot[1]);
const T det_x = (T(obs_x) - beam[0]) * T(pixel_size);
const T det_y = (T(obs_y) - beam[1]) * T(pixel_size);
const T det_z = T(distance_mm[0]);
const T t1_x = c1 * det_x + s1 * det_z;
const T t1_y = det_y;
const T t1_z = -s1 * det_x + c1 * det_z;
const T x = t1_x;
const T y = c2 * t1_y + s2 * t1_z;
const T z = -s2 * t1_y + c2 * t1_z;
const T inv_norm = T(1) / ceres::sqrt(x * x + y * y + z * z);
T recip_raw[3] = {
x * inv_norm * T(inv_lambda),
y * inv_norm * T(inv_lambda),
(z * inv_norm - T(1.0)) * T(inv_lambda)
};
e_obs_recip = Eigen::Matrix<T, 3, 1>(recip_raw[0], recip_raw[1], recip_raw[2]);
}
// Per-reflection: predicted node g_hkl, |g_hkl|^2, and the Ewald-sphere normal.
// This is the expensive part (symmetry-aware B matrix, three rotations, cross
// products) - it depends only on the lattice (p0,p1,p2) and hkl, so for a whole
// shoebox it can be computed once. Convention identical to XtalOptimizer.
template<typename T>
bool PredictedNode(const T *p0, const T *p1, const T *p2,
double exp_h, double exp_k, double exp_l,
gemmi::CrystalSystem symmetry, double inv_lambda,
Eigen::Matrix<T, 3, 1> &e_pred_recip,
Eigen::Matrix<T, 3, 1> &n_radial, T &q_sq) {
Eigen::Matrix<T, 3, 1> e_uc_len = Eigen::Matrix<T, 3, 1>::Zero();
Eigen::Matrix<T, 3, 3> Bmat = Eigen::Matrix<T, 3, 3>::Identity();
if (symmetry == gemmi::CrystalSystem::Hexagonal) {
e_uc_len << p1[0], p1[0], p1[2];
Bmat(0, 1) = T(-0.5);
Bmat(1, 1) = T(sqrt(3.0) / 2.0);
} else if (symmetry == gemmi::CrystalSystem::Orthorhombic) {
e_uc_len << p1[0], p1[1], p1[2];
} else if (symmetry == gemmi::CrystalSystem::Tetragonal) {
e_uc_len << p1[0], p1[0], p1[2];
} else if (symmetry == gemmi::CrystalSystem::Cubic) {
e_uc_len << p1[0], p1[0], p1[0];
} else if (symmetry == gemmi::CrystalSystem::Monoclinic) {
e_uc_len << p1[0], p1[1], p1[2];
Bmat(0, 2) = ceres::cos(p2[0]);
Bmat(2, 2) = ceres::sin(p2[0]);
} else {
const T ca = ceres::cos(p2[0]);
const T cb = ceres::cos(p2[1]);
const T cg = ceres::cos(p2[2]);
const T sg = ceres::sin(p2[2]);
e_uc_len << p1[0], p1[1], p1[2];
Bmat(0, 1) = cg;
Bmat(1, 1) = sg;
const T cx = cb;
const T cy = (ca - cb * cg) / sg;
const T v = T(1) - cx * cx - cy * cy;
const T cz = (v >= T(0)) ? ceres::sqrt(v) : T(0);
Bmat(0, 2) = cx;
Bmat(1, 2) = cy;
Bmat(2, 2) = cz;
}
const T L0 = e_uc_len[0];
const T L1 = e_uc_len[1];
const T L2 = e_uc_len[2];
T col0_unrot[3] = {Bmat(0, 0) * L0, Bmat(1, 0) * L0, Bmat(2, 0) * L0};
T col1_unrot[3] = {Bmat(0, 1) * L1, Bmat(1, 1) * L1, Bmat(2, 1) * L1};
T col2_unrot[3] = {Bmat(0, 2) * L2, Bmat(1, 2) * L2, Bmat(2, 2) * L2};
T col0_rot[3], col1_rot[3], col2_rot[3];
ceres::AngleAxisRotatePoint(p0, col0_unrot, col0_rot);
ceres::AngleAxisRotatePoint(p0, col1_unrot, col1_rot);
ceres::AngleAxisRotatePoint(p0, col2_unrot, col2_rot);
const Eigen::Matrix<T, 3, 1> A(col0_rot[0], col0_rot[1], col0_rot[2]);
const Eigen::Matrix<T, 3, 1> Bv(col1_rot[0], col1_rot[1], col1_rot[2]);
const Eigen::Matrix<T, 3, 1> C(col2_rot[0], col2_rot[1], col2_rot[2]);
const Eigen::Matrix<T, 3, 1> BxC = Bv.cross(C);
const Eigen::Matrix<T, 3, 1> CxA = C.cross(A);
const Eigen::Matrix<T, 3, 1> AxB = A.cross(Bv);
const T Vol = A.dot(BxC);
if (ceres::abs(Vol) < T(1e-12))
return false;
const T invV = T(1) / Vol;
e_pred_recip = (BxC * T(exp_h) + CxA * T(exp_k) + AxB * T(exp_l)) * invV;
q_sq = e_pred_recip.squaredNorm();
// Ewald sphere centre at -k_i = (0,0,-inv_lambda); radial normal at g_hkl.
const Eigen::Matrix<T, 3, 1> S_pred(
e_pred_recip[0],
e_pred_recip[1],
e_pred_recip[2] + T(inv_lambda));
const T S_pred_norm = S_pred.norm();
if (S_pred_norm < T(1e-10))
return false;
n_radial = S_pred / S_pred_norm;
return true;
}
} // namespace
// ---------------------------------------------------------------------------
// Cost functor
//
// I_pred(pixel) = G * Itrue * B_term * P_radial * P_tangential * pol + I_bkg
//
// B_term = exp(-B |q|^2 / 4) (Debye-Waller)
// P_radial = exp(-eps_r^2 / R0_eff^2) (partiality: fraction of
// the mosaic blob on the
// Ewald sphere; <= 1)
// P_tangential = exp(-eps_t^2/R1^2) / (pi R1^2) (Gaussian spatial profile
// in the Ewald tangent plane)
// pol = per-reflection polarization correction (raw = true * pol),
// evaluated once at the predicted spot position (as in
// BraggIntegrate2D). 1 if polarization is disabled.
//
// Everything is in *raw* detector counts: there is no per-pixel solid-angle or
// area (Lorentz/Jacobian) weighting - each pixel counts equally, like the normal
// integrator. The tangential factor is what makes this "profile fitting"; the
// 1/(pi R1^2) normalization keeps the profile width R1 from soaking up the
// overall scale G.
//
// X-ray bandwidth: a spread in lambda is a spread in the Ewald-sphere radius,
// i.e. a purely *radial* thickening of the shell. It adds (in quadrature) a
// resolution-dependent term to the radial width:
// R0_eff^2 = R0^2 + R_bw^2 , R_bw^2 = (b*lambda)^2 / (2 d^4)
// where b = relative bandwidth (sigma of dlambda/lambda). R_bw grows like 1/d^2,
// so bandwidth leaves low-resolution spots sharp and smears high-resolution ones
// radially - the pink-beam/DMM signature. R_bw_sq is a fixed per-reflection
// constant (b is known), so R0 keeps meaning "intrinsic" width (mosaic +
// divergence + beam). b = 0 makes R_bw = 0: a monochromatic no-op.
// ---------------------------------------------------------------------------
struct PixelResidual {
PixelResidual(const PixelObs &obs, double Itrue,
double lambda, double pixel_size,
double exp_h, double exp_k, double exp_l,
double R_bw_sq, double pol,
gemmi::CrystalSystem symmetry)
: Itrue(Itrue), Iobs(obs.Iobs), Ibkg(obs.Ibkg), weight(obs.weight),
obs_x(obs.x), obs_y(obs.y),
inv_lambda(1.0 / lambda), pixel_size(pixel_size),
exp_h(exp_h), exp_k(exp_k), exp_l(exp_l),
R_bw_sq(R_bw_sq), pol(pol), symmetry(symmetry) {
if (std::fabs(lambda) < 1e-6)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"Lambda cannot be close to zero");
}
// Maps a detector pixel through the current geometry + lattice into the
// reference reciprocal frame and returns:
// q_sq = |g_hkl|^2 (predicted node, for B-factor)
// eps_radial = deviation along Ewald normal (partiality direction)
// eps_tang_sq = squared deviation in the detector-tangential plane (profile)
template<typename T>
bool GeometryTerms(const T *const beam,
const T *const distance_mm,
const T *const detector_rot,
const T *const p0,
const T *const p1,
const T *const p2,
T &q_sq, T &eps_radial, T &eps_tang_sq) const {
Eigen::Matrix<T, 3, 1> e_obs_recip;
ObservedRecip(beam, distance_mm, detector_rot,
obs_x, obs_y, pixel_size, inv_lambda,
e_obs_recip);
Eigen::Matrix<T, 3, 1> e_pred_recip, n_radial;
if (!PredictedNode(p0, p1, p2, exp_h, exp_k, exp_l, symmetry, inv_lambda,
e_pred_recip, n_radial, q_sq))
return false;
const Eigen::Matrix<T, 3, 1> delta_q = e_obs_recip - e_pred_recip;
eps_radial = delta_q.dot(n_radial);
eps_tang_sq = (delta_q - eps_radial * n_radial).squaredNorm();
return true;
}
// Assembles the full model intensity for the pixel from the geometry terms.
template<typename T>
bool Model(const T *const beam, const T *const distance_mm,
const T *const detector_rot,
const T *const p0, const T *const p1, const T *const p2,
const T *const scale_factor, const T *const B, const T *const R,
T &Ipred) const {
T q_sq, eps_radial, eps_tang_sq;
if (!GeometryTerms(beam, distance_mm, detector_rot,
p0, p1, p2, q_sq, eps_radial, eps_tang_sq))
return false;
if (R[0] < T(1e-10) || R[1] < T(1e-10))
return false;
const T B_term = ceres::exp(-B[0] * q_sq / T(4.0));
// Separable Gaussian spot model:
// radial P_r(e) = exp(-e^2/R0_eff^2) (peak-normalized, in (0,1])
// tangent g_t(e) = exp(-|e|^2/R1^2) / (pi R1^2) [1/A^-2]
// Every pixel counts equally (no area/Lorentz weighting); the radial factor
// is the still-image partiality (how far the reflection sits from the Ewald
// sphere); the overall scale is carried by the free G.
//
// IMPORTANT: the radial factor MUST use the same convention here as the
// extraction's `partiality` (peak-normalized), otherwise image_scale_corr
// = 1/(partiality*G*B) does not invert the model and a leftover, R0_eff-
// dependent (hence resolution-dependent) factor biases the intensities.
// R0_eff folds in the energy-bandwidth broadening via R_bw_sq.
const T R0_eff_sq = R[0] * R[0] + T(R_bw_sq);
const T P_radial = ceres::exp(-eps_radial * eps_radial / R0_eff_sq);
const T P_tang = ceres::exp(-eps_tang_sq / (R[1] * R[1]))
/ (T(M_PI) * R[1] * R[1]);
const T signal = scale_factor[0] * T(Itrue) * B_term * P_radial * P_tang * T(pol);
Ipred = signal + T(Ibkg);
return true;
}
template<typename T>
bool operator()(const T *const beam,
const T *const distance_mm,
const T *const detector_rot,
const T *const p0,
const T *const p1,
const T *const p2,
const T *const scale_factor,
const T *const B,
const T *const R,
T *residual) const {
T Ipred;
if (!Model(beam, distance_mm, detector_rot, p0, p1, p2, scale_factor, B, R, Ipred))
return false;
residual[0] = (Ipred - T(Iobs)) * T(weight);
return true;
}
const double Itrue, Iobs, Ibkg, weight;
const double obs_x, obs_y;
const double inv_lambda;
const double pixel_size;
const double exp_h, exp_k, exp_l;
const double R_bw_sq; // bandwidth radial-width^2 contribution (0 = monochromatic)
const double pol; // per-reflection polarization correction
gemmi::CrystalSystem symmetry;
};
// ---------------------------------------------------------------------------
// Per-shoebox cost functor
//
// One residual block per reflection emitting N residuals (one per shoebox pixel).
// The expensive per-reflection geometry (PredictedNode: symmetry-aware B matrix,
// three rotations, cross products) is computed ONCE; only the cheap per-pixel
// ObservedRecip + Gaussian profile run in the pixel loop. This is identical in
// value to the old one-block-per-pixel formulation but ~(pixels-per-shoebox)x
// fewer evaluations of the costly node computation. Uses the same shared helpers
// (and hence the same conventions) as PixelResidual.
// ---------------------------------------------------------------------------
struct ShoeboxResidual {
ShoeboxResidual(const ReflGroup &g, double lambda, double pixel_size,
gemmi::CrystalSystem symmetry)
: pixels(g.pixels), Itrue(g.Itrue), R_bw_sq(g.R_bw_sq), pol(g.pol),
exp_h(g.h), exp_k(g.k), exp_l(g.l),
inv_lambda(1.0 / lambda), pixel_size(pixel_size),
symmetry(symmetry) {}
template<typename T>
bool operator()(const T *const *params, T *residual) const {
// Parameter blocks (order matches AddParameterBlock in Run):
// 0 beam[2] 1 distance[1] 2 detector_rot[2]
// 3 p0[3] 4 p1[3] 5 p2[3] 6 scale[1] 7 B[1] 8 R[2]
const T *beam = params[0];
const T *distance_mm = params[1];
const T *detector_rot = params[2];
const T *p0 = params[3];
const T *p1 = params[4];
const T *p2 = params[5];
const T *scale_factor = params[6];
const T *B = params[7];
const T *R = params[8];
if (R[0] < T(1e-10) || R[1] < T(1e-10))
return false;
// --- per-reflection: computed once ---------------------------------
Eigen::Matrix<T, 3, 1> e_pred_recip, n_radial;
T q_sq;
if (!PredictedNode(p0, p1, p2, exp_h, exp_k, exp_l, symmetry, inv_lambda,
e_pred_recip, n_radial, q_sq))
return false;
const T B_term = ceres::exp(-B[0] * q_sq / T(4.0));
const T R0_eff_sq = R[0] * R[0] + T(R_bw_sq);
// --- per-pixel loop -------------------------------------------------
for (size_t i = 0; i < pixels.size(); ++i) {
const PixelObs &obs = pixels[i];
Eigen::Matrix<T, 3, 1> e_obs_recip;
ObservedRecip(beam, distance_mm, detector_rot,
obs.x, obs.y, pixel_size, inv_lambda, e_obs_recip);
const Eigen::Matrix<T, 3, 1> delta_q = e_obs_recip - e_pred_recip;
const T eps_radial = delta_q.dot(n_radial);
const T eps_tang_sq = (delta_q - eps_radial * n_radial).squaredNorm();
const T P_radial = ceres::exp(-eps_radial * eps_radial / R0_eff_sq);
const T P_tang = ceres::exp(-eps_tang_sq / (R[1] * R[1]))
/ (T(M_PI) * R[1] * R[1]);
const T signal = scale_factor[0] * T(Itrue) * B_term * P_radial * P_tang * T(pol);
const T Ipred = signal + T(obs.Ibkg);
residual[i] = (Ipred - T(obs.Iobs)) * T(obs.weight);
}
return true;
}
std::vector<PixelObs> pixels;
const double Itrue, R_bw_sq, pol;
const double exp_h, exp_k, exp_l;
const double inv_lambda, pixel_size;
gemmi::CrystalSystem symmetry;
};
PixelRefine::PixelRefine(const DiffractionExperiment &experiment,
const std::vector<MergedReflection> &reference)
: xpixel(experiment.GetXPixelsNum()),
ypixel(experiment.GetYPixelsNum()),
experiment(experiment),
hkl_key_generator(experiment.GetScalingSettings().GetMergeFriedel(),
experiment.GetSpaceGroupNumber().value_or(1)) {
for (const auto &ref: reference)
reference_data[hkl_key_generator(ref)] = ref.I;
}
void PixelRefine::BuildParameterBlocks(const PixelRefineData &data,
double beam[2], double &dist_mm,
double detector_rot[2],
double latt_vec0[3], double latt_vec1[3], double latt_vec2[3]) const {
beam[0] = data.geom.GetBeamX_pxl();
beam[1] = data.geom.GetBeamY_pxl();
dist_mm = data.geom.GetDetectorDistance_mm();
detector_rot[0] = data.geom.GetPoniRot1_rad();
detector_rot[1] = data.geom.GetPoniRot2_rad();
for (int i = 0; i < 3; ++i)
latt_vec0[i] = latt_vec1[i] = latt_vec2[i] = 0.0;
double beta = data.latt.GetUnitCell().beta;
switch (data.crystal_system) {
case gemmi::CrystalSystem::Orthorhombic:
LatticeToRodriguesAndLengths_GS(data.latt, latt_vec0, latt_vec1);
break;
case gemmi::CrystalSystem::Tetragonal:
LatticeToRodriguesAndLengths_GS(data.latt, latt_vec0, latt_vec1);
latt_vec1[0] = (latt_vec1[0] + latt_vec1[1]) / 2.0;
break;
case gemmi::CrystalSystem::Cubic:
LatticeToRodriguesAndLengths_GS(data.latt, latt_vec0, latt_vec1);
latt_vec1[0] = (latt_vec1[0] + latt_vec1[1] + latt_vec1[2]) / 3.0;
break;
case gemmi::CrystalSystem::Hexagonal:
LatticeToRodriguesAndLengths_Hex(data.latt, latt_vec0, latt_vec1);
break;
case gemmi::CrystalSystem::Monoclinic:
LatticeToRodriguesLengthsBeta_Mono(data.latt, latt_vec0, latt_vec1, beta);
latt_vec2[0] = beta;
break;
default: {
LatticeToRodriguesAndLengths_GS(data.latt, latt_vec0, latt_vec1);
const auto uc = data.latt.GetUnitCell();
latt_vec2[0] = uc.alpha * M_PI / 180.0;
latt_vec2[1] = uc.beta * M_PI / 180.0;
latt_vec2[2] = uc.gamma * M_PI / 180.0;
break;
}
}
}
BraggPredictionSettings PixelRefine::BuildPredictionSettings(const PixelRefineData &data) const {
// Radial Ewald-acceptance band: predict exactly the reflections the merge's
// partiality floor would still keep, and no tighter. A reflection's partiality
// is exp(-s^2 / R0_eff^2) (s = excitation error), so it survives min_partiality
// when |s| <= R0 * sqrt(-ln(min_partiality)). Using that as the cutoff keeps
// prediction and the partiality cut consistent. The struct default (0.0005) is
// far narrower than R0 (~0.005), so previously most keepable reflections were
// never predicted and per-reflection multiplicity collapsed ~4x.
const double min_part = std::clamp(experiment.GetScalingSettings().GetMinPartiality(), 1e-6, 0.999);
const double r0 = std::max(data.R[0], 1e-4);
const double cutoff = r0 * std::sqrt(-std::log(min_part));
// Relative bandwidth (sigma of dlambda/lambda): the explicit PixelRefine model
// value if set, else the experiment's nominal bandwidth (FWHM -> sigma). >0
// thickens the band radially at high resolution (the 1/d^2 pink-beam smear),
// matching the integrator and preventing the outer shells from being clipped.
float bw_sigma = static_cast<float>(data.bandwidth);
if (bw_sigma <= 0.0f)
bw_sigma = experiment.GetBandwidthFWHM().value_or(0.0f) / 2.3548f;
return BraggPredictionSettings{
.high_res_A = experiment.GetBraggIntegrationSettings().GetDMinLimit_A(),
.ewald_dist_cutoff = static_cast<float>(cutoff),
.max_hkl = 100,
.centering = data.centering,
.bandwidth_sigma = bw_sigma
};
}
template<class T>
void PixelRefine::Run(const T *image,
BraggPrediction &prediction,
PixelRefineData &data) {
data.solved = false;
data.reflections.clear();
const double lambda = data.geom.GetWavelength_A();
const double pixel_size = data.geom.GetPixelSize_mm();
const BraggPredictionSettings settings_prediction = BuildPredictionSettings(data);
const int radius = data.shoebox_radius;
const int bkg_outer_radius = std::max(radius + 1, data.bkg_outer_radius);
// Per-reflection polarization correction (raw = true * pol), evaluated once at
// the predicted spot - same handling as BraggIntegrate2D. Identity if disabled.
const auto pol_factor = experiment.GetPolarizationFactor();
auto polarization = [&](double x, double y) -> double {
if (!pol_factor)
return 1.0;
return data.geom.CalcAzIntPolarizationCorr(static_cast<float>(x), static_cast<float>(y),
pol_factor.value());
};
// Bandwidth radial-width^2 (in the code's R = sqrt(2)*sigma convention):
// R_bw^2 = (b*lambda)^2 / (2 d^4), b = relative bandwidth (sigma).
// A fixed per-reflection constant; data.bandwidth == 0 -> monochromatic no-op.
const double bw = data.bandwidth;
auto bandwidth_radial_sq = [&](double d) -> double {
if (bw <= 0.0 || d <= 0.0)
return 0.0;
const double bl = bw * lambda;
return bl * bl / (2.0 * d * d * d * d);
};
// Mutable experiment whose geometry is re-synced from the refined data.geom
// before each prediction, so shoeboxes track the refined geometry/cell.
DiffractionExperiment exp_iter = experiment;
// State retained after the loop for the final reflection extraction.
std::vector<ReflGroup> groups;
double beam[2] = {0, 0};
double dist_mm = data.geom.GetDetectorDistance_mm();
double detector_rot[2] = {0, 0};
double latt_vec0[3] = {0, 0, 0}; // orientation (Rodrigues)
double latt_vec1[3] = {0, 0, 0}; // lengths
double latt_vec2[3] = {0, 0, 0}; // angles (rad)
const bool eval_only = (data.max_iterations <= 0);
const int n_iter = std::max(1, data.max_iterations);
for (int iter = 0; iter < n_iter; ++iter) {
// ---- 1. Re-sync prediction geometry from the (refined) model ----------
exp_iter.BeamX_pxl(data.geom.GetBeamX_pxl())
.BeamY_pxl(data.geom.GetBeamY_pxl())
.DetectorDistance_mm(data.geom.GetDetectorDistance_mm())
.PoniRot1_rad(data.geom.GetPoniRot1_rad())
.PoniRot2_rad(data.geom.GetPoniRot2_rad());
const int nrefl = prediction.Calc(exp_iter, data.latt, settings_prediction);
// ---- 2. Collect per-reflection shoebox pixels -------------------------
// GetReflections() returns the full pre-sized buffer; only the first
// nrefl entries are valid for this image (the rest are stale/zeroed).
groups.clear();
const auto &predicted = prediction.GetReflections();
// Spot-core mask over ALL predicted reflections, so each reflection's
// local background ignores pixels that belong to a neighbouring spot.
const auto spot_mask = BuildSpotMask(predicted, nrefl, xpixel, ypixel, radius);
for (int ri = 0; ri < nrefl; ++ri) {
const auto &refl = predicted[ri];
const auto hkl = hkl_key_generator(refl);
if (!reference_data.contains(hkl))
continue;
// Local flat background from the ring around the shoebox (raw counts).
// No azimuthal fallback: if we cannot estimate a clean local background
// the reflection is dropped, exactly as BraggIntegrate2D marks it
// unobserved when fewer than a handful of background pixels survive.
double Ibkg = 0.0;
if (!EstimateLocalBackground(image, spot_mask, xpixel, ypixel,
refl.predicted_x, refl.predicted_y,
radius, bkg_outer_radius, Ibkg))
continue;
ReflGroup g;
g.h = refl.h;
g.k = refl.k;
g.l = refl.l;
g.d = refl.d;
g.Itrue = reference_data[hkl];
g.R_bw_sq = bandwidth_radial_sq(refl.d);
g.pol = polarization(refl.predicted_x, refl.predicted_y);
g.Ibkg = Ibkg;
g.predicted_x = refl.predicted_x;
g.predicted_y = refl.predicted_y;
const auto box = ShoeboxBounds(refl.predicted_x, refl.predicted_y, radius, xpixel, ypixel);
for (int y = box.min_y; y <= box.max_y; ++y) {
for (int x = box.min_x; x <= box.max_x; ++x) {
const size_t npixel = xpixel * y + x;
// Skip sentinel (masked / saturated) pixels. We assume the pixel
// mask is already applied upstream (encoded as the sentinel).
if (image[npixel] == std::numeric_limits<T>::max())
continue;
if (std::is_signed_v<T> && (image[npixel] == std::numeric_limits<T>::min()))
continue;
const double Iobs = static_cast<double>(image[npixel]); // raw counts
// Per-pixel variance: Poisson noise of the raw counts.
double var = std::max(Iobs, 0.0);
if (!(var > 1.0))
var = 1.0;
PixelObs obs{
.x = static_cast<double>(x),
.y = static_cast<double>(y),
.Iobs = Iobs,
.Ibkg = Ibkg,
.weight = 1.0 / std::sqrt(var)
};
g.pixels.push_back(obs);
}
}
if (!g.pixels.empty())
groups.push_back(std::move(g));
}
if (groups.empty())
return;
// ---- 3. Set up parameter blocks (geometry part mirrors XtalOptimizer) -
BuildParameterBlocks(data, beam, dist_mm, detector_rot,
latt_vec0, latt_vec1, latt_vec2);
// ---- 4. Build the problem ---------------------------------------------
// One residual block per shoebox (N residuals), so the expensive
// per-reflection node geometry is evaluated once per reflection instead
// of once per pixel.
ceres::Problem problem;
size_t residual_pixels = 0;
for (const auto &g : groups) {
auto *cost = new ceres::DynamicAutoDiffCostFunction<ShoeboxResidual>(
new ShoeboxResidual(g, lambda, pixel_size, data.crystal_system));
cost->AddParameterBlock(2); // beam
cost->AddParameterBlock(1); // distance
cost->AddParameterBlock(2); // detector_rot
cost->AddParameterBlock(3); // p0 (orientation)
cost->AddParameterBlock(3); // p1 (lengths)
cost->AddParameterBlock(3); // p2 (angles)
cost->AddParameterBlock(1); // scale G
cost->AddParameterBlock(1); // B
cost->AddParameterBlock(2); // R
cost->SetNumResiduals(static_cast<int>(g.pixels.size()));
// No robust loss here: a per-block (whole-shoebox) Huber would act on
// the sum of ~N squared residuals and mis-scale, unlike the previous
// per-pixel Huber. Per-pixel sigma weighting is retained; per-pixel
// outlier rejection (zingers) is a TODO if needed.
problem.AddResidualBlock(cost, nullptr,
beam, &dist_mm, detector_rot,
latt_vec0, latt_vec1, latt_vec2,
&data.scale_factor, &data.B_factor, data.R);
residual_pixels += g.pixels.size();
}
data.residual_count = residual_pixels;
// ---- 5. Constrain / bound parameter blocks ----------------------------
if (!data.refine_orientation)
problem.SetParameterBlockConstant(latt_vec0);
if (!data.refine_unit_cell) {
problem.SetParameterBlockConstant(latt_vec1);
problem.SetParameterBlockConstant(latt_vec2);
} else {
for (int i = 0; i < 3; ++i) {
problem.SetParameterLowerBound(latt_vec1, i, 5.0);
problem.SetParameterUpperBound(latt_vec1, i, 1000.0);
}
if (data.crystal_system != gemmi::CrystalSystem::Monoclinic &&
data.crystal_system != gemmi::CrystalSystem::Triclinic)
problem.SetParameterBlockConstant(latt_vec2);
}
if (!data.refine_beam_center)
problem.SetParameterBlockConstant(beam);
if (!data.refine_distance) {
problem.SetParameterBlockConstant(&dist_mm);
} else {
problem.SetParameterLowerBound(&dist_mm, 0, dist_mm * 0.9);
problem.SetParameterUpperBound(&dist_mm, 0, dist_mm * 1.1);
}
if (!data.refine_detector_angles) {
problem.SetParameterBlockConstant(detector_rot);
} else {
const double rng = 3.0 / 180.0 * M_PI;
for (int i = 0; i < 2; ++i) {
problem.SetParameterLowerBound(detector_rot, i, detector_rot[i] - rng);
problem.SetParameterUpperBound(detector_rot, i, detector_rot[i] + rng);
}
}
if (data.refine_scale)
problem.SetParameterLowerBound(&data.scale_factor, 0, 0.0);
else
problem.SetParameterBlockConstant(&data.scale_factor);
if (!data.refine_B)
problem.SetParameterBlockConstant(&data.B_factor);
if (data.refine_R) {
problem.SetParameterLowerBound(data.R, 0, 1e-5);
problem.SetParameterLowerBound(data.R, 1, 1e-5);
} else {
problem.SetParameterBlockConstant(data.R);
}
// ---- 6. Solve (or, for max_iterations<=0, just evaluate the cost) -----
// Evaluate-only is the live-residual path: it reports the current cost
// without moving any parameter, so a UI can show how good the present
// R0/R1/bandwidth/geometry are as the user drags sliders.
if (eval_only) {
double cost = 0.0;
problem.Evaluate(ceres::Problem::EvaluateOptions(), &cost, nullptr, nullptr, nullptr);
data.final_cost = cost;
data.solved = true;
} else {
ceres::Solver::Options options;
options.linear_solver_type = ceres::DENSE_QR;
options.minimizer_progress_to_stdout = false;
options.logging_type = ceres::LoggingType::SILENT;
options.max_solver_time_in_seconds = data.max_time_s;
options.num_threads = 1;
ceres::Solver::Summary summary;
ceres::Solve(options, &problem, &summary);
data.final_cost = summary.final_cost;
data.solved = summary.IsSolutionUsable();
}
// ---- 7. Write refined geometry + lattice back into data ---------------
if (data.refine_beam_center)
data.geom.BeamX_pxl(beam[0]).BeamY_pxl(beam[1]);
if (data.refine_distance)
data.geom.DetectorDistance_mm(dist_mm);
if (data.refine_detector_angles)
data.geom.PoniRot1_rad(detector_rot[0]).PoniRot2_rad(detector_rot[1]);
if (data.refine_orientation || data.refine_unit_cell) {
switch (data.crystal_system) {
case gemmi::CrystalSystem::Orthorhombic:
data.latt = AngleAxisAndCellToLattice(latt_vec0, latt_vec1, M_PI/2, M_PI/2, M_PI/2);
break;
case gemmi::CrystalSystem::Tetragonal:
latt_vec1[1] = latt_vec1[0];
data.latt = AngleAxisAndCellToLattice(latt_vec0, latt_vec1, M_PI/2, M_PI/2, M_PI/2);
break;
case gemmi::CrystalSystem::Cubic:
latt_vec1[1] = latt_vec1[0];
latt_vec1[2] = latt_vec1[0];
data.latt = AngleAxisAndCellToLattice(latt_vec0, latt_vec1, M_PI/2, M_PI/2, M_PI/2);
break;
case gemmi::CrystalSystem::Hexagonal:
latt_vec1[1] = latt_vec1[0];
data.latt = AngleAxisAndCellToLattice(latt_vec0, latt_vec1, M_PI/2, M_PI/2, 2.0*M_PI/3.0);
break;
case gemmi::CrystalSystem::Monoclinic:
data.latt = AngleAxisAndCellToLattice(latt_vec0, latt_vec1, M_PI/2, latt_vec2[0], M_PI/2);
break;
default:
data.latt = AngleAxisAndCellToLattice(latt_vec0, latt_vec1,
latt_vec2[0], latt_vec2[1], latt_vec2[2]);
break;
}
}
} // predict<->refine iterations
// ---- Extract integrated reflections ---------------------------------------
// Profile fitting gives the recorded amplitude (fitting the tangential profile
// P_t against the background-subtracted pixels):
// J = sum_p[ P_t,p (Iobs_p - Ibkg)/v_p ] / sum_p[ P_t,p^2 / v_p ]
// ~ G * Itrue * B_term * partiality * pol (recorded raw counts)
// var(J) = 1 / sum_p[ P_t,p^2 / v_p ]
//
// Two SEPARATE fractions reduce the full intensity to what these pixels record:
//
// partiality - the radial / rocking dimension that a still does NOT sample.
// Only the slice of the reflection that crosses the Ewald
// sphere on this shot is recorded; <= 1. We DIVIDE it out to
// recover the full intensity. = profile-weighted P_radial.
//
// completeness - the fraction of the spot's detector footprint that landed on
// live pixels (= profile captured by live pixels / profile over
// the whole shoebox). 1.0 when the spot sits fully on the
// detector; < 1.0 only when a detector edge, gap or mask clips
// it. Profile fitting already extrapolates over the missing
// pixels, so this is NOT applied to r.I - it is a quality flag.
//
// Output split (Merge multiplies r.I * image_scale_corr and weights by
// 1/(sigma*image_scale_corr)^2 - see Merge.cpp):
// r.I = J / (B_term * partiality * pol) = G * Itrue
// r.sigma = sqrt(var(J)) / (B_term * partiality * pol)
// r.partiality = profile-weighted P_radial in (0,1] (the rocking fraction)
// r.completeness = live/total tangential profile in (0,1] (detector clipping)
// r.image_scale_corr = 1/G (per-image scale ONLY)
// so r.I * image_scale_corr = Itrue. B, partiality and polarization live on the
// intensity, G lives on image_scale_corr - one clean meaning per field.
//
// We walk the full (unclamped) shoebox once: every grid point feeds the total
// tangential profile (completeness denominator); points that are real, live
// detector pixels also feed the profile fit and the captured profile.
data.reflections.reserve(groups.size());
for (const auto &g : groups) {
const int cx = static_cast<int>(std::lround(g.predicted_x));
const int cy = static_cast<int>(std::lround(g.predicted_y));
// Debye-Waller factor for this reflection (constant over its shoebox).
const double B_term = std::exp(-data.B_factor / (4.0 * g.d * g.d));
double num = 0.0, den = 0.0, bkg_sum = 0.0, radial_sum = 0.0;
double prof_live = 0.0, prof_full = 0.0; // tangential profile: captured / total
size_t n = 0;
for (int y = cy - radius; y <= cy + radius; ++y) {
for (int x = cx - radius; x <= cx + radius; ++x) {
// Geometry/profile for this grid point (valid even off the detector).
PixelObs probe{static_cast<double>(x), static_cast<double>(y), 0.0, g.Ibkg, 1.0};
PixelResidual pr(probe, 1.0, lambda, pixel_size, g.h, g.k, g.l,
g.R_bw_sq, g.pol, data.crystal_system);
double q_sq, eps_r, eps_t_sq;
if (!pr.GeometryTerms(beam, &dist_mm, detector_rot,
latt_vec0, latt_vec1, latt_vec2, q_sq, eps_r, eps_t_sq))
continue;
if (!(data.R[0] > 0.0) || !(data.R[1] > 0.0))
continue;
// Tangential profile shape (area-normalized) -> the fit template.
const double P_t = std::exp(-eps_t_sq / (data.R[1] * data.R[1]))
/ (M_PI * data.R[1] * data.R[1]);
prof_full += P_t; // whole shoebox, on- or off-detector
// Only real, unmasked detector pixels carry signal.
if (x < 0 || x >= static_cast<int>(xpixel) || y < 0 || y >= static_cast<int>(ypixel))
continue;
const size_t np = static_cast<size_t>(xpixel) * y + x;
if (image[np] == std::numeric_limits<T>::max())
continue;
if (std::is_signed_v<T> && image[np] == std::numeric_limits<T>::min())
continue;
const double Iobs = static_cast<double>(image[np]); // raw counts
double v = std::max(Iobs, 0.0); // Poisson variance
if (!(v > 1.0))
v = 1.0;
// Peak-normalized radial factor (the partiality), in (0,1]. The
// bandwidth-broadened radial width matches the model in Model().
const double R0_eff_sq = data.R[0] * data.R[0] + g.R_bw_sq;
const double P_radial = std::exp(-eps_r * eps_r / R0_eff_sq);
// Profile-fit accumulators. The amplitude estimator weights pixels by
// P_t^2/v, so the partiality (which de-scales that amplitude) MUST use
// the SAME weights - otherwise an R0_eff-dependent (resolution-
// dependent) factor is left behind in r.I.
const double w = P_t * P_t / v;
num += P_t * (Iobs - g.Ibkg) / v;
den += w;
radial_sum += P_radial * w; // partiality weighted exactly like num/den
prof_live += P_t; // captured tangential profile
bkg_sum += g.Ibkg;
++n;
}
}
Reflection r{};
r.h = g.h;
r.k = g.k;
r.l = g.l;
r.d = static_cast<float>(g.d);
r.predicted_x = static_cast<float>(g.predicted_x);
r.predicted_y = static_cast<float>(g.predicted_y);
r.observed_x = NAN;
r.observed_y = NAN;
r.rlp = 1.0f;
r.partiality = (den > 0.0) ? static_cast<float>(radial_sum / den) : 1.0f;
r.completeness = (prof_full > 0.0) ? static_cast<float>(prof_live / prof_full) : 1.0f;
if (den > 0.0 && n > 0) {
const double I_amp = num / den; // ~ G*Itrue*B_term*partiality*pol
const double sigma_amp = std::sqrt(1.0 / den);
const double corr = static_cast<double>(r.partiality) * B_term * g.pol; // B, partiality & pol
r.bkg = static_cast<float>(bkg_sum / static_cast<double>(n));
r.observed = true;
if (corr > 0.0 && data.scale_factor > 0.0) {
r.I = static_cast<float>(I_amp / corr); // = G*Itrue
r.sigma = static_cast<float>(sigma_amp / corr);
r.image_scale_corr = static_cast<float>(1.0 / data.scale_factor); // G only
} else {
r.I = static_cast<float>(I_amp);
r.sigma = static_cast<float>(sigma_amp);
r.image_scale_corr = NAN;
}
} else {
r.I = 0.0f;
r.sigma = NAN;
r.bkg = 0.0f;
r.observed = false;
}
data.reflections.push_back(r);
}
// ---- Per-image CC vs reference (the half/ref correlation diagnostic) -------
// Pearson CC of the scaled estimate (r.I * image_scale_corr = Itrue_est)
// against the reference intensities, over the matched reflections.
{
double sx = 0, sy = 0, sxx = 0, syy = 0, sxy = 0;
size_t cn = 0;
for (const auto &r : data.reflections) {
if (!r.observed || !std::isfinite(r.I) || !std::isfinite(r.image_scale_corr))
continue;
const auto it = reference_data.find(hkl_key_generator(r));
if (it == reference_data.end())
continue;
const double x = static_cast<double>(r.I) * static_cast<double>(r.image_scale_corr);
const double y = it->second;
if (!std::isfinite(x) || !std::isfinite(y))
continue;
sx += x; sy += y; sxx += x * x; syy += y * y; sxy += x * y; ++cn;
}
data.cc = NAN;
data.cc_n = static_cast<int64_t>(cn);
if (cn >= 3) {
const double nd = static_cast<double>(cn);
const double cov = sxy - sx * sy / nd;
const double vx = sxx - sx * sx / nd;
const double vy = syy - sy * sy / nd;
if (vx > 0.0 && vy > 0.0)
data.cc = cov / std::sqrt(vx * vy);
}
}
}
template<class T>
std::vector<float> PixelRefine::PredictImage(const T *image,
BraggPrediction &prediction,
const PixelRefineData &data,
bool include_background) const {
std::vector<float> img(xpixel * ypixel, 0.0f);
const double lambda = data.geom.GetWavelength_A();
const double pixel_size = data.geom.GetPixelSize_mm();
const int radius = data.shoebox_radius;
const int bkg_outer_radius = std::max(radius + 1, data.bkg_outer_radius);
const double bw = data.bandwidth;
const auto pol_factor = experiment.GetPolarizationFactor();
auto polarization = [&](double x, double y) -> double {
if (!pol_factor)
return 1.0;
return data.geom.CalcAzIntPolarizationCorr(static_cast<float>(x), static_cast<float>(y),
pol_factor.value());
};
auto bandwidth_radial_sq = [&](double d) -> double {
if (bw <= 0.0 || d <= 0.0)
return 0.0;
const double bl = bw * lambda;
return bl * bl / (2.0 * d * d * d * d);
};
double beam[2], dist_mm, detector_rot[2];
double latt_vec0[3], latt_vec1[3], latt_vec2[3];
BuildParameterBlocks(data, beam, dist_mm, detector_rot, latt_vec0, latt_vec1, latt_vec2);
DiffractionExperiment exp_iter = experiment;
exp_iter.BeamX_pxl(data.geom.GetBeamX_pxl())
.BeamY_pxl(data.geom.GetBeamY_pxl())
.DetectorDistance_mm(data.geom.GetDetectorDistance_mm())
.PoniRot1_rad(data.geom.GetPoniRot1_rad())
.PoniRot2_rad(data.geom.GetPoniRot2_rad());
const BraggPredictionSettings settings_prediction = BuildPredictionSettings(data);
const int nrefl = prediction.Calc(exp_iter, data.latt, settings_prediction);
const auto &predicted = prediction.GetReflections();
const auto spot_mask = BuildSpotMask(predicted, nrefl, xpixel, ypixel, radius);
for (int ri = 0; ri < nrefl; ++ri) {
const auto &refl = predicted[ri];
const auto it = reference_data.find(hkl_key_generator(refl));
if (it == reference_data.end())
continue;
const double Itrue = it->second;
const double R_bw_sq = bandwidth_radial_sq(refl.d);
const double pol = polarization(refl.predicted_x, refl.predicted_y);
// Local background straight from the actual image (flat per shoebox), laid
// into the box so the prediction overlays the real frame - the same model
// path Run() fits, now reproduced faithfully because we have the image.
double Ibkg = 0.0;
const bool have_bkg = include_background &&
EstimateLocalBackground(image, spot_mask, xpixel, ypixel,
refl.predicted_x, refl.predicted_y,
radius, bkg_outer_radius, Ibkg);
const auto box = ShoeboxBounds(refl.predicted_x, refl.predicted_y, radius, xpixel, ypixel);
for (int y = box.min_y; y <= box.max_y; ++y) {
for (int x = box.min_x; x <= box.max_x; ++x) {
const size_t npixel = xpixel * y + x;
PixelObs obs{
.x = static_cast<double>(x),
.y = static_cast<double>(y),
.Iobs = 0.0,
.Ibkg = have_bkg ? Ibkg : 0.0,
.weight = 1.0
};
PixelResidual pr(obs, Itrue, lambda, pixel_size,
refl.h, refl.k, refl.l, R_bw_sq, pol, data.crystal_system);
double Ipred = 0.0; // raw counts: signal (+ local background)
if (pr.Model(beam, &dist_mm, detector_rot,
latt_vec0, latt_vec1, latt_vec2,
&data.scale_factor, &data.B_factor, data.R, Ipred))
img[npixel] += static_cast<float>(Ipred);
}
}
}
return img;
}
template<class T>
std::vector<float> PixelRefine::ChiSquaredImage(const T *image,
BraggPrediction &prediction,
const PixelRefineData &data) const {
std::vector<float> img(xpixel * ypixel, 0.0f);
const double lambda = data.geom.GetWavelength_A();
const double pixel_size = data.geom.GetPixelSize_mm();
const int radius = data.shoebox_radius;
const int bkg_outer_radius = std::max(radius + 1, data.bkg_outer_radius);
const double bw = data.bandwidth;
const auto pol_factor = experiment.GetPolarizationFactor();
auto polarization = [&](double x, double y) -> double {
if (!pol_factor)
return 1.0;
return data.geom.CalcAzIntPolarizationCorr(static_cast<float>(x), static_cast<float>(y),
pol_factor.value());
};
auto bandwidth_radial_sq = [&](double d) -> double {
if (bw <= 0.0 || d <= 0.0)
return 0.0;
const double bl = bw * lambda;
return bl * bl / (2.0 * d * d * d * d);
};
double beam[2], dist_mm, detector_rot[2];
double latt_vec0[3], latt_vec1[3], latt_vec2[3];
BuildParameterBlocks(data, beam, dist_mm, detector_rot, latt_vec0, latt_vec1, latt_vec2);
DiffractionExperiment exp_iter = experiment;
exp_iter.BeamX_pxl(data.geom.GetBeamX_pxl())
.BeamY_pxl(data.geom.GetBeamY_pxl())
.DetectorDistance_mm(data.geom.GetDetectorDistance_mm())
.PoniRot1_rad(data.geom.GetPoniRot1_rad())
.PoniRot2_rad(data.geom.GetPoniRot2_rad());
const BraggPredictionSettings settings_prediction = BuildPredictionSettings(data);
const int nrefl = prediction.Calc(exp_iter, data.latt, settings_prediction);
const auto &predicted = prediction.GetReflections();
const auto spot_mask = BuildSpotMask(predicted, nrefl, xpixel, ypixel, radius);
for (int ri = 0; ri < nrefl; ++ri) {
const auto &refl = predicted[ri];
const auto it = reference_data.find(hkl_key_generator(refl));
if (it == reference_data.end())
continue;
const double Itrue = it->second;
const double R_bw_sq = bandwidth_radial_sq(refl.d);
const double pol = polarization(refl.predicted_x, refl.predicted_y);
// Local flat background, identical to Run(); skip the reflection if it
// cannot be estimated (matches Run() dropping the reflection).
double Ibkg = 0.0;
if (!EstimateLocalBackground(image, spot_mask, xpixel, ypixel,
refl.predicted_x, refl.predicted_y,
radius, bkg_outer_radius, Ibkg))
continue;
const auto box = ShoeboxBounds(refl.predicted_x, refl.predicted_y, radius, xpixel, ypixel);
for (int y = box.min_y; y <= box.max_y; ++y) {
for (int x = box.min_x; x <= box.max_x; ++x) {
const size_t npixel = xpixel * y + x;
// Same gating as Run(): only pixels that actually enter the fit.
if (image[npixel] == std::numeric_limits<T>::max())
continue;
if (std::is_signed_v<T> && (image[npixel] == std::numeric_limits<T>::min()))
continue;
const double Iobs = static_cast<double>(image[npixel]); // raw counts
double var = std::max(Iobs, 0.0);
if (!(var > 1.0))
var = 1.0;
const double weight = 1.0 / std::sqrt(var);
PixelObs obs{
.x = static_cast<double>(x),
.y = static_cast<double>(y),
.Iobs = Iobs,
.Ibkg = Ibkg,
.weight = weight
};
PixelResidual pr(obs, Itrue, lambda, pixel_size,
refl.h, refl.k, refl.l, R_bw_sq, pol, data.crystal_system);
double Ipred = 0.0;
if (pr.Model(beam, &dist_mm, detector_rot,
latt_vec0, latt_vec1, latt_vec2,
&data.scale_factor, &data.B_factor, data.R, Ipred)) {
// residual_i = (I_pred - I_obs) * weight (== Ceres residual);
// its square is this pixel's contribution to the cost.
const double rw = (Ipred - Iobs) * weight;
img[npixel] += static_cast<float>(rw * rw);
}
}
}
}
return img;
}
// Explicit instantiations for the supported (uncompressed) image pixel types.
template void PixelRefine::Run<int8_t>(const int8_t *, BraggPrediction &, PixelRefineData &);
template void PixelRefine::Run<int16_t>(const int16_t *, BraggPrediction &, PixelRefineData &);
template void PixelRefine::Run<int32_t>(const int32_t *, BraggPrediction &, PixelRefineData &);
template void PixelRefine::Run<uint8_t>(const uint8_t *, BraggPrediction &, PixelRefineData &);
template void PixelRefine::Run<uint16_t>(const uint16_t *, BraggPrediction &, PixelRefineData &);
template void PixelRefine::Run<uint32_t>(const uint32_t *, BraggPrediction &, PixelRefineData &);
template std::vector<float> PixelRefine::PredictImage<int8_t>(const int8_t *, BraggPrediction &, const PixelRefineData &, bool) const;
template std::vector<float> PixelRefine::PredictImage<int16_t>(const int16_t *, BraggPrediction &, const PixelRefineData &, bool) const;
template std::vector<float> PixelRefine::PredictImage<int32_t>(const int32_t *, BraggPrediction &, const PixelRefineData &, bool) const;
template std::vector<float> PixelRefine::PredictImage<uint8_t>(const uint8_t *, BraggPrediction &, const PixelRefineData &, bool) const;
template std::vector<float> PixelRefine::PredictImage<uint16_t>(const uint16_t *, BraggPrediction &, const PixelRefineData &, bool) const;
template std::vector<float> PixelRefine::PredictImage<uint32_t>(const uint32_t *, BraggPrediction &, const PixelRefineData &, bool) const;
template std::vector<float> PixelRefine::ChiSquaredImage<int8_t>(const int8_t *, BraggPrediction &, const PixelRefineData &) const;
template std::vector<float> PixelRefine::ChiSquaredImage<int16_t>(const int16_t *, BraggPrediction &, const PixelRefineData &) const;
template std::vector<float> PixelRefine::ChiSquaredImage<int32_t>(const int32_t *, BraggPrediction &, const PixelRefineData &) const;
template std::vector<float> PixelRefine::ChiSquaredImage<uint8_t>(const uint8_t *, BraggPrediction &, const PixelRefineData &) const;
template std::vector<float> PixelRefine::ChiSquaredImage<uint16_t>(const uint16_t *, BraggPrediction &, const PixelRefineData &) const;
template std::vector<float> PixelRefine::ChiSquaredImage<uint32_t>(const uint32_t *, BraggPrediction &, const PixelRefineData &) const;