Distance test: the seven other halvings' pairs of fits run side by side
The paired free-minus-held test of the distance (33d089cfa) makes its pair of joint solves on eight
halvings of the reflections and reads the change's standard error off how the change varies between
them. The seven pairs after the first ran one after another, each pair's two solves beside each other,
so a test cost seven rounds of solves: 0.42 s per test on the battery, in pass 1 and again in pass 2
at the header - about 200 s over the open and in-house arm outside the distance pre-passes.
The halving's seed was a variable the fits read through the split predicate; it is now an argument of
the predicate and of the three fit lambdas, and the seven pairs run concurrently, their changes read
back in seed order. Same seeds, same solves, same order of the changes: the mean and the standard
error are the ones the sequential loop computed, and the first halving's fits, which the decision is
made on, are untouched.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HVeDQrPqZ52Tp6rzzZGxf3
This commit is contained in:
1 parent
03eb81bf21
commit
3044fdc6f7
1 file changed
+42
-33
@@ -12,6 +12,7 @@
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <numeric>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
@@ -446,17 +447,17 @@ PostRefineResult PostRefineRotationGeometry(PostRefineObservations observations,
|
||||
// reflection, so a parity split would leave the validation half empty). A reflection's rocking
|
||||
// event and its spot positions carry the same hkl, so both residual families split together.
|
||||
// The seed picks one of several such halvings: 0 is the split every fit below is made on, the
|
||||
// others are the distance test's repeated splits.
|
||||
unsigned split_seed = 0;
|
||||
auto is_val = [&split_seed](int h, int k, int l) {
|
||||
// others are the distance test's repeated splits, which run side by side, so the seed is an
|
||||
// argument of each fit rather than a variable the fits read.
|
||||
auto is_val = [](int h, int k, int l, unsigned seed) {
|
||||
unsigned u = static_cast<unsigned>(h) * 2654435761u + static_cast<unsigned>(k) * 2246822519u
|
||||
+ static_cast<unsigned>(l) * 3266489917u + split_seed * 2654435769u;
|
||||
+ static_cast<unsigned>(l) * 3266489917u + seed * 2654435769u;
|
||||
u ^= u >> 15; u *= 2246822519u; u ^= u >> 13;
|
||||
return (u & 1u) != 0u;
|
||||
};
|
||||
enum Subset { FIT, VAL, ALL };
|
||||
auto in = [&](int h, int k, int l, Subset s) {
|
||||
return s == ALL || (is_val(h, k, l) == (s == VAL)); };
|
||||
auto in = [&](int h, int k, int l, Subset s, unsigned seed) {
|
||||
return s == ALL || (is_val(h, k, l, seed) == (s == VAL)); };
|
||||
|
||||
// The crystal as XtalResidual's parameter blocks - orientation (angle-axis), cell lengths,
|
||||
// cell angles - in the per-system parameterisation that residual's B matrix reads. Seeded from
|
||||
@@ -553,12 +554,12 @@ PostRefineResult PostRefineRotationGeometry(PostRefineObservations observations,
|
||||
auto joint_cost = [&](Subset s, const double bm[2], const double ds[1], const double rv[3],
|
||||
const double q0[3], const double q1[3], const double q2[3],
|
||||
double &pos_out, double &exc_out, double *se_out = nullptr,
|
||||
double *exc_se_out = nullptr) {
|
||||
double *exc_se_out = nullptr, unsigned seed = 0) {
|
||||
double cp = 0.0, ce = 0.0, c4 = 0.0, c4e = 0.0;
|
||||
size_t np = 0, ne = 0;
|
||||
for (size_t oi = 0; oi < n_obs; ++oi) {
|
||||
const Partial *pp = obs[oi];
|
||||
if (!in(pp->h, pp->k, pp->l, s)) continue;
|
||||
if (!in(pp->h, pp->k, pp->l, s, seed)) continue;
|
||||
XtalResidual r(pp->obs_x, pp->obs_y, lambda_l, pixel_mm, cos_rot3, sin_rot3,
|
||||
angle_rad(pp->img), pp->h, pp->k, pp->l, sys, orientation);
|
||||
double res[3] = {0, 0, 0};
|
||||
@@ -571,7 +572,7 @@ PostRefineResult PostRefineRotationGeometry(PostRefineObservations observations,
|
||||
}
|
||||
for (const int32_t i : selected) {
|
||||
const Event &ev = events[i];
|
||||
if (!in(ev.h, ev.k, ev.l, s)) continue;
|
||||
if (!in(ev.h, ev.k, ev.l, s, seed)) continue;
|
||||
JointExcitationResidual r(lambda_l, ev.phi_obs, settings.excitation_weight,
|
||||
ev.h, ev.k, ev.l, sys);
|
||||
double res = 0.0;
|
||||
@@ -604,11 +605,12 @@ PostRefineResult PostRefineRotationGeometry(PostRefineObservations observations,
|
||||
const double bm_a[2], const double ds_a[1], const double rv_a[3],
|
||||
const double q0_a[3], const double q1_a[3], const double q2_a[3],
|
||||
const double bm_b[2], const double ds_b[1], const double rv_b[3],
|
||||
const double q0_b[3], const double q1_b[3], const double q2_b[3]) {
|
||||
const double q0_b[3], const double q1_b[3], const double q2_b[3],
|
||||
unsigned seed = 0) {
|
||||
std::vector<double> dp, de;
|
||||
for (size_t oi = 0; oi < n_obs; ++oi) {
|
||||
const Partial *pp = obs[oi];
|
||||
if (!in(pp->h, pp->k, pp->l, s)) continue;
|
||||
if (!in(pp->h, pp->k, pp->l, s, seed)) continue;
|
||||
XtalResidual r(pp->obs_x, pp->obs_y, lambda_l, pixel_mm, cos_rot3, sin_rot3,
|
||||
angle_rad(pp->img), pp->h, pp->k, pp->l, sys, orientation);
|
||||
double ra[3] = {0, 0, 0}, rb[3] = {0, 0, 0};
|
||||
@@ -620,7 +622,7 @@ PostRefineResult PostRefineRotationGeometry(PostRefineObservations observations,
|
||||
}
|
||||
for (const int32_t i : selected) {
|
||||
const Event &ev = events[i];
|
||||
if (!in(ev.h, ev.k, ev.l, s)) continue;
|
||||
if (!in(ev.h, ev.k, ev.l, s, seed)) continue;
|
||||
JointExcitationResidual r(lambda_l, ev.phi_obs, settings.excitation_weight,
|
||||
ev.h, ev.k, ev.l, sys);
|
||||
double ra = 0.0, rb = 0.0;
|
||||
@@ -649,7 +651,7 @@ PostRefineResult PostRefineRotationGeometry(PostRefineObservations observations,
|
||||
// of the tilt block and its own loss object, which every residual of the problem shares.
|
||||
auto solve_joint = [&](Subset s, bool wide, bool fix_distance, double bm[2], double ds[1],
|
||||
double rv[3], double q0[3], double q1[3], double q2[3],
|
||||
double *distance_corr = nullptr) {
|
||||
double *distance_corr = nullptr, unsigned seed = 0) {
|
||||
const double ds_seed = ds[0];
|
||||
const double q1_seed[3] = {q1[0], q1[1], q1[2]};
|
||||
double tilt[2] = {det_rot[0], det_rot[1]};
|
||||
@@ -659,7 +661,7 @@ PostRefineResult PostRefineRotationGeometry(PostRefineObservations observations,
|
||||
ceres::Problem p(po);
|
||||
for (size_t oi = 0; oi < n_obs; ++oi) {
|
||||
const Partial *pp = obs[oi];
|
||||
if (!in(pp->h, pp->k, pp->l, s)) continue;
|
||||
if (!in(pp->h, pp->k, pp->l, s, seed)) continue;
|
||||
p.AddResidualBlock(new ceres::AutoDiffCostFunction<XtalResidual, 3, 2, 1, 2, 3, 3, 3, 3>(
|
||||
new XtalResidual(pp->obs_x, pp->obs_y, lambda_l, pixel_mm, cos_rot3, sin_rot3,
|
||||
angle_rad(pp->img), pp->h, pp->k, pp->l, sys, orientation)),
|
||||
@@ -667,7 +669,7 @@ PostRefineResult PostRefineRotationGeometry(PostRefineObservations observations,
|
||||
}
|
||||
for (const int32_t i : selected) {
|
||||
const Event &ev = events[i];
|
||||
if (!in(ev.h, ev.k, ev.l, s)) continue;
|
||||
if (!in(ev.h, ev.k, ev.l, s, seed)) continue;
|
||||
p.AddResidualBlock(new ceres::AutoDiffCostFunction<JointExcitationResidual, 1, 3, 3, 3, 3>(
|
||||
new JointExcitationResidual(lambda_l, ev.phi_obs, settings.excitation_weight,
|
||||
ev.h, ev.k, ev.l, sys)),
|
||||
@@ -863,24 +865,31 @@ PostRefineResult PostRefineRotationGeometry(PostRefineObservations observations,
|
||||
if (convH && !settings.distance_decided) {
|
||||
std::vector<double> changes{paired_change(VAL, bm_h, ds_h, rv_h, q0_h, q1_h, q2_h,
|
||||
bm_f, ds_f, rv_f, q0_f, q1_f, q2_f).exc};
|
||||
for (split_seed = 1; split_seed < SPLITS; ++split_seed) {
|
||||
double bm_a[2] = {beam_x0, beam_y0}, ds_a[1] = {dist0}, rv_a[3], q0_a[3], q1_a[3], q2_a[3];
|
||||
double bm_b[2] = {beam_x0, beam_y0}, ds_b[1] = {dist0}, rv_b[3], q0_b[3], q1_b[3], q2_b[3];
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
rv_a[j] = rv_b[j] = ax0[j]; q0_a[j] = q0_b[j] = p0_0[j];
|
||||
q1_a[j] = q1_b[j] = p1_0[j]; q2_a[j] = q2_b[j] = p2_0[j];
|
||||
}
|
||||
auto held_a = std::async(std::launch::async, [&] {
|
||||
return solve_joint(FIT, /*wide=*/true, /*fix_distance=*/true, bm_a, ds_a,
|
||||
rv_a, q0_a, q1_a, q2_a);
|
||||
});
|
||||
const bool conv_b = solve_joint(FIT, /*wide=*/true, /*fix_distance=*/false, bm_b, ds_b,
|
||||
rv_b, q0_b, q1_b, q2_b);
|
||||
if (held_a.get() && conv_b)
|
||||
changes.push_back(paired_change(VAL, bm_a, ds_a, rv_a, q0_a, q1_a, q2_a,
|
||||
bm_b, ds_b, rv_b, q0_b, q1_b, q2_b).exc);
|
||||
}
|
||||
split_seed = 0;
|
||||
// The other halvings' pairs are independent of each other and of the pair above,
|
||||
// so all run side by side; their changes are read in seed order.
|
||||
std::vector<std::future<std::optional<double>>> pairs;
|
||||
for (unsigned seed = 1; seed < SPLITS; ++seed)
|
||||
pairs.push_back(std::async(std::launch::async, [&, seed]() -> std::optional<double> {
|
||||
double bm_a[2] = {beam_x0, beam_y0}, ds_a[1] = {dist0}, rv_a[3], q0_a[3], q1_a[3], q2_a[3];
|
||||
double bm_b[2] = {beam_x0, beam_y0}, ds_b[1] = {dist0}, rv_b[3], q0_b[3], q1_b[3], q2_b[3];
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
rv_a[j] = rv_b[j] = ax0[j]; q0_a[j] = q0_b[j] = p0_0[j];
|
||||
q1_a[j] = q1_b[j] = p1_0[j]; q2_a[j] = q2_b[j] = p2_0[j];
|
||||
}
|
||||
auto held_a = std::async(std::launch::async, [&] {
|
||||
return solve_joint(FIT, /*wide=*/true, /*fix_distance=*/true, bm_a, ds_a,
|
||||
rv_a, q0_a, q1_a, q2_a, nullptr, seed);
|
||||
});
|
||||
const bool conv_b = solve_joint(FIT, /*wide=*/true, /*fix_distance=*/false, bm_b, ds_b,
|
||||
rv_b, q0_b, q1_b, q2_b, nullptr, seed);
|
||||
if (!held_a.get() || !conv_b)
|
||||
return std::nullopt;
|
||||
return paired_change(VAL, bm_a, ds_a, rv_a, q0_a, q1_a, q2_a,
|
||||
bm_b, ds_b, rv_b, q0_b, q1_b, q2_b, seed).exc;
|
||||
}));
|
||||
for (auto &pair : pairs)
|
||||
if (const auto change = pair.get())
|
||||
changes.push_back(*change);
|
||||
double se;
|
||||
MeanAndStandardError(changes, free_vs_held, se);
|
||||
const double k = static_cast<double>(changes.size());
|
||||
|
||||
Reference in new issue
Block a user