Files
Jungfraujoch/tests/FrenchWilsonTest.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

100 lines
4.4 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include <catch2/catch_all.hpp>
#include <cmath>
#include <vector>
#include "../image_analysis/scale_merge/FrenchWilson.h"
namespace {
const gemmi::SpaceGroup &SG(int number) { return *gemmi::find_spacegroup_by_number(number); }
MergedReflection Refl(int h, int k, int l, float d, float I, float sigma) {
MergedReflection r;
r.h = h; r.k = k; r.l = l; r.d = d; r.I = I; r.sigma = sigma;
return r;
}
// A resolution-spread of ordinary reflections so a Wilson mean can be formed per shell.
std::vector<MergedReflection> Background() {
std::vector<MergedReflection> v;
for (int h = 1; h <= 12; ++h)
for (int k = 0; k <= 12; ++k)
for (int l = 0; l <= 12; ++l)
v.push_back(Refl(h, k, l, 40.0f / (1 + h * h + k * k + l * l), 800.0f, 20.0f));
return v;
}
}
TEST_CASE("French-Wilson: strong reflections reduce to sqrt(I)", "[french_wilson]") {
auto v = Background();
v.push_back(Refl(1, 0, 0, 25.0f, 40000.0f, 50.0f)); // I/sigma = 800, clearly strong
ApplyFrenchWilson(v, SG(1));
CHECK(v.back().F == Catch::Approx(std::sqrt(40000.0)).epsilon(0.02)); // ~200
CHECK(v.back().sigmaF >= 0.0f);
CHECK(std::isfinite(v.back().sigmaF));
}
TEST_CASE("French-Wilson: weak and negative intensities get a positive amplitude", "[french_wilson]") {
auto v = Background();
v.push_back(Refl(2, 0, 0, 20.0f, -40.0f, 50.0f)); // negative measured intensity
v.push_back(Refl(3, 0, 0, 15.0f, 10.0f, 50.0f)); // weak, I < sigma
ApplyFrenchWilson(v, SG(1));
const auto& neg = v[v.size() - 2];
const auto& weak = v.back();
CHECK(std::isfinite(neg.F));
CHECK(neg.F > 0.0f); // Bayesian estimate is positive (naive sqrt would give 0)
CHECK(std::isfinite(weak.F));
CHECK(weak.F > 0.0f);
}
TEST_CASE("French-Wilson: amplitudes are always finite and non-negative", "[french_wilson]") {
std::vector<MergedReflection> v;
// A deliberate mix: strong, weak, negative, tiny sigma, across resolution.
for (int i = 0; i < 300; ++i) {
const float d = 20.0f / (1 + 0.05f * i);
const float I = (i % 7 == 0) ? -30.0f : static_cast<float>((i % 50) * 40);
v.push_back(Refl(1 + i, 2, 3, d, I, 25.0f));
}
ApplyFrenchWilson(v, SG(96)); // P4(3)2(1)2 (has centric reflections + epsilon>1 axes)
for (const auto& r : v) {
CHECK(std::isfinite(r.F));
CHECK(r.F >= 0.0f);
CHECK(std::isfinite(r.sigmaF));
CHECK(r.sigmaF >= 0.0f);
}
}
TEST_CASE("French-Wilson: centric weak reflection gets a smaller amplitude than acentric", "[french_wilson]") {
// At the same resolution (same Wilson mean Sigma) and the same near-zero intensity, the centric
// prior puts more weight near |F|=0, so the posterior <|F|> is smaller than for an acentric
// reflection (0.80*sqrt(Sigma) vs 0.89*sqrt(Sigma) at I=0). This pins the centric/acentric prior
// the right way round: if the two priors were swapped the inequality below would flip.
std::vector<MergedReflection> v;
// Uniform strong background across resolution, so every shell has ~the same Wilson mean.
for (int h = 1; h <= 10; ++h)
for (int k = 1; k <= 10; ++k)
for (int l = 1; l <= 6; ++l)
v.push_back(Refl(h, k, l, 20.0f / (0.5f + 0.1f * (h + k + l)), 1000.0f, 30.0f));
// Two weak (I=0) probes at the SAME resolution: (3,1,0) is centric in P4 (l=0 zone), (3,1,4) is
// acentric. d is set directly, so both share a shell (hence Sigma) regardless of the cell.
v.push_back(Refl(3, 1, 0, 5.0f, 0.0f, 10.0f));
v.push_back(Refl(3, 1, 4, 5.0f, 0.0f, 10.0f));
ApplyFrenchWilson(v, SG(75)); // P4
const auto& centric = v[v.size() - 2];
const auto& acentric = v.back();
CHECK(centric.F > 0.0f);
CHECK(acentric.F > 0.0f);
CHECK(centric.F < acentric.F);
}
TEST_CASE("French-Wilson: unusable sigma falls back to sqrt(max(I,0))", "[french_wilson]") {
auto v = Background();
v.push_back(Refl(4, 0, 0, 12.0f, 144.0f, NAN)); // no sigma
v.push_back(Refl(5, 0, 0, 11.0f, -5.0f, NAN)); // no sigma, negative I
ApplyFrenchWilson(v, SG(1));
CHECK(v[v.size() - 2].F == Catch::Approx(12.0f)); // sqrt(144)
CHECK(v.back().F == Catch::Approx(0.0f)); // sqrt(max(-5,0))
}