diff --git a/image_analysis/geom_refinement/PostRefine.cpp b/image_analysis/geom_refinement/PostRefine.cpp index f1b5d3e9e..992b417d9 100644 --- a/image_analysis/geom_refinement/PostRefine.cpp +++ b/image_analysis/geom_refinement/PostRefine.cpp @@ -445,9 +445,12 @@ PostRefineResult PostRefineRotationGeometry(PostRefineObservations observations, // lattice centering condition (e.g. an I-centred lattice has h+k+l even for EVERY present // 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. - auto is_val = [](int h, int k, int l) { + // 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) { unsigned u = static_cast(h) * 2654435761u + static_cast(k) * 2246822519u - + static_cast(l) * 3266489917u; + + static_cast(l) * 3266489917u + split_seed * 2654435769u; u ^= u >> 15; u *= 2246822519u; u ^= u >> 13; return (u & 1u) != 0u; }; @@ -821,20 +824,32 @@ PostRefineResult PostRefineRotationGeometry(PostRefineObservations observations, // excitation residual seventeen times the free fit's). Where freeing the distance // lowers the held-out excitation residual below the held fit's by more than the // standard error of that change, the free fit is committed exactly as before. The - // error is the PAIRED one (paired_change): both fits are read on the same held-out - // rocking events, so what a change has to beat is the scatter of what each event sees - // change, not the scatter of either fit's residuals, which is mostly the centroids' own - // and common to both. Read unpaired, a header 0.5 % short whose free fit lowered the - // excitation residual by 4.7 paired standard errors was held. A move this lets through - // is still only a candidate: re-integrated at, it has to show the fixed point the - // distance check looks for (FixedPointDistance in rugnux) before the run keeps it. Where - // it does not, the held fit is the candidate from here on: what the walk, the gate - // and the commit below see. The positional residual is deliberately not consulted - // for this. Its in-fit gain along the degenerate direction is the one re-integration - // erases, and pooled with the excitation family it either drowns a decisive - // excitation gain in its own noise (measured: a 54 % excitation gain read as 9 % - // pooled against a 9 % noise) or lends the degenerate direction a gain that is not - // there. + // change is read PAIRED (paired_change): both fits on the same held-out rocking + // events, so the centroids' own scatter, common to both, drops out of it. Read + // unpaired, a header 0.5 % short whose free fit lowered the excitation residual by + // 4.7 paired standard errors was held. Its standard error, though, is NOT the scatter + // of that one split's events: the change each event sees is the free fit's move times + // that event's sensitivity to the cell scale, so a spread taken over the events is + // proportional to the move and says nothing about whether the move itself is more than + // the fit's noise. Where it is not, the sign of the change goes with the half the fit + // happened to be made on - measured, a crystal whose free fit moved 0.02 % read the + // change at -6 to +9 of that spread across nine halvings of the same reflections, and + // the spread of the change across the halvings was seven times the one any of them + // reported. So the pair of fits is made on SPLITS halvings of the reflections and the + // standard error is taken from how the change varies between them, which carries the + // fit's own noise and whatever the events share. The halvings overlap, so the variance + // of their mean is not that of independent repeats: following Nadeau & Bengio (2003) + // Machine Learning 52, 239-281 it is s^2 (1/K + n_val/n_fit), with n_val/n_fit = 1 for a + // halving. Where the move is decisive the two errors agree and nothing changes. + // A move this lets through is still only a candidate: re-integrated at, it has to show + // the fixed point the distance check looks for (FixedPointDistance in rugnux) before + // the run keeps it. Where it does not, the held fit is the candidate from here on: + // what the walk, the gate and the commit below see. The positional residual is + // deliberately not consulted for this. Its in-fit gain along the degenerate direction + // is the one re-integration erases, and pooled with the excitation family it either + // drowns a decisive excitation gain in its own noise (measured: a 54 % excitation gain + // read as 9 % pooled against a 9 % noise) or lends the degenerate direction a gain + // that is not there. // A run that has already walked off the header has refuted that hypothesis by // re-integrating - the rounds of a walk are judged on what they realise - so the fit // is not asked to hold a distance that is no longer the header's. @@ -843,21 +858,46 @@ PostRefineResult PostRefineRotationGeometry(PostRefineObservations observations, if (convH) cv_h = joint_cost(VAL, bm_h, ds_h, rv_h, q0_h, q1_h, q2_h, pos_h, exc_h, nullptr, &exc_h_se); - const PairedChange free_vs_held = convH - ? 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) - : PairedChange{}; - const double noise = free_vs_held.exc_se; + constexpr unsigned SPLITS = 8; + double free_vs_held = NAN, noise = NAN; + if (convH && !settings.distance_decided) { + std::vector 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; + double se; + MeanAndStandardError(changes, free_vs_held, se); + const double k = static_cast(changes.size()); + const double s = se * std::sqrt(k); // the sample sd of the k changes + noise = s * std::sqrt(1.0 / k + 1.0); + } const bool hold_distance = convH && (settings.distance_decided - || !(std::isfinite(noise) && free_vs_held.exc < -noise)); + || !(std::isfinite(noise) && free_vs_held < -noise)); result.rocking_holds_distance = hold_distance; if (settings.distance_at_header) logger.Info("Post-refine GEOM distance: free {:.3f} mm gives held-out excitation {:.3e} +- " "{:.1e} (positional {:.3e}); held at the header {:.3f} mm gives {:.3e} +- " "{:.1e} (positional {:.3e}); free minus held {:+.3e} against the noise {:.1e} " - "=> {}; the free fit's formal sd(distance) {:.4f} %, corr(distance, cell " - "a/b/c) {:.4f} {:.4f} {:.4f}", + "over {} splits => {}; the free fit's formal sd(distance) {:.4f} %, corr(distance, " + "cell a/b/c) {:.4f} {:.4f} {:.4f}", ds_f[0], exc_ref, exc_ref_se, pos_ref, dist0, exc_h, exc_h_se, pos_h, - free_vs_held.exc, noise, + free_vs_held, noise, SPLITS, hold_distance ? "the rocking angles do not ask for it, the header distance is held" : convH ? "the free distance pays" : "the held fit did not converge, the free distance stands",