diff --git a/rugnux/RugnuxFarEnd.cpp b/rugnux/RugnuxFarEnd.cpp index 720c616f9..f379d5539 100644 --- a/rugnux/RugnuxFarEnd.cpp +++ b/rugnux/RugnuxFarEnd.cpp @@ -18,9 +18,9 @@ // and what it measured on the rest is evidence those decisions never saw. Here the sweep is cut // into blocks of FAR_END_BLOCK_DEG, a handful of per-frame quantities pass 2 already has in memory // are averaged per block, and every block past the first part is compared with the blocks inside -// it. A late block is measured against the early blocks' mean in units of the spread of the early -// blocks themselves, as a prediction for one more block (Student's t on the early blocks' degrees -// of freedom). A block is out of line when its two-sided p, times the number of late-block +// it, and every block inside it with those past it. A block is measured against the other part's +// mean in units of the spread of that part's blocks themselves, as a prediction for one more block +// (Student's t on that part's degrees of freedom). A block is out of line when its two-sided p, times the number of late-block // comparisons the run made over all the quantities, is below the significance the run tests // everything else at (SPOT_BUDGET_SIGNIFICANCE_Z, 0.1 %): an unchanged sweep then says "hold" in all // but one run in a thousand, however many blocks and quantities there are. No number in it is set @@ -32,7 +32,9 @@ // - the cell scale the strong reflections ask for, d at the observed centroid over d predicted: // the cell; // - the residual rotation between each frame's refined orientation and the sweep's lattice turned -// to that frame, about the spindle and off it: the rotation scale and the orientation; +// to that frame, about the spindle and off it: the rotation scale and the orientation - and for +// the rotation scale also its rate, the slope of the residual about the spindle against the +// angle, late blocks against early ones; // - the background: the shadow mask (hardware that shadows more, or less, as the sample turns). namespace { @@ -106,15 +108,17 @@ void Rugnux::FarEndCheck(PipelineLocals &p, const RotationIndexerResult &rot) { return i < static_cast(v.size()) ? static_cast(v[i]) : NAN; }; - enum { INDEXED, ON_LATTICE, CELL, ABOUT_SPINDLE, OFF_SPINDLE, BACKGROUND, N_STATS }; - struct Stat { const char *what; const char *decision; double unit; const char *suffix; }; - const Stat stats[N_STATS] = { - {"frames indexing on their own", "the lattice", 100.0, "%"}, - {"spots on the lattice", "the lattice", 100.0, "%"}, - {"cell scale of the strong reflections (d observed / d predicted - 1)", "the cell", 100.0, "%"}, - {"orientation residual about the spindle", "the rotation scale", 1.0, " deg"}, - {"orientation residual off the spindle", "the orientation", 1.0, " deg"}, - {"background", "the shadow mask", 1.0, ""}, + enum { INDEXED, ON_LATTICE, CELL, ABOUT_SPINDLE, OFF_SPINDLE, BACKGROUND, N_STATS, RATE = N_STATS }; + struct Stat { const char *what; const char *decision; double unit; const char *suffix; const char *tag; }; + const Stat stats[N_STATS + 1] = { + {"frames indexing on their own", "the lattice", 100.0, "%", "indexed"}, + {"spots on the lattice", "the lattice", 100.0, "%", "on-lattice"}, + {"cell scale of the strong reflections (d observed / d predicted - 1)", "the cell", 100.0, "%", "cell"}, + {"orientation residual about the spindle", "the rotation scale", 1.0, " deg", "about-spindle"}, + {"orientation residual off the spindle", "the orientation", 1.0, " deg", "off-spindle"}, + {"background", "the shadow mask", 1.0, "", "background"}, + {"rate of the residual about the spindle (per 100 deg of rotation)", "the rotation scale", 100.0, "%", + "rate"}, }; std::vector> frame(N_STATS, std::vector(n, NAN)); const Coord spindle = gon.GetAxis().Normalize(); @@ -184,52 +188,108 @@ void Rugnux::FarEndCheck(PipelineLocals &p, const RotationIndexerResult &rot) { } const auto deg = [&](int ordinal) { return gon.GetAngle_deg(static_cast(ordinal)); }; - // Every late block of every quantity against the early blocks of the same quantity. - struct Comparison { int stat; Range range; double value, mean, noise, t, p; }; + // Every late block of every quantity against the early blocks of the same quantity - and every + // early block against the late ones, because the part that is out of line can be the first: a + // crystal that settles, or an orientation that drifts, over the opening degrees makes the first + // part the odd one, and its spread then hides nothing the late blocks show. + struct Comparison { int stat; Range range; double value, mean, noise, t, p; bool first_part; }; std::vector comparisons; - for (int s = 0; s < N_STATS; s++) { + const auto compare = [&](int s, const std::vector &reference, const std::vector &tested, + bool first_part) { std::vector means; - for (const auto &r : early_blocks) + for (const auto &r : reference) if (const Block b = Summarise(frame[s], r.first, r.last); std::isfinite(b.mean)) means.push_back(b.mean); - const int ne = static_cast(means.size()); - if (ne < 3) - continue; - double me = 0.0; + const int nr = static_cast(means.size()); + if (nr < 3) + return; + double mr = 0.0; for (const double m : means) - me += m; - me /= ne; - double ve = 0.0; + mr += m; + mr /= nr; + double vr = 0.0; for (const double m : means) - ve += (m - me) * (m - me); - ve /= ne - 1; - for (const auto &r : late_blocks) { + vr += (m - mr) * (m - mr); + vr /= nr - 1; + for (const auto &r : tested) { const Block b = Summarise(frame[s], r.first, r.last); if (!std::isfinite(b.mean)) continue; - const double noise = std::sqrt(ve * (1.0 + 1.0 / ne) + b.se * b.se); - const double t = noise > 0.0 ? (b.mean - me) / noise : (b.mean == me ? 0.0 : INFINITY); - comparisons.push_back({s, r, b.mean, me, noise, t, TwoSidedP(t, ne - 1)}); + const double noise = std::sqrt(vr * (1.0 + 1.0 / nr) + b.se * b.se); + const double t = noise > 0.0 ? (b.mean - mr) / noise : (b.mean == mr ? 0.0 : INFINITY); + comparisons.push_back({s, r, b.mean, mr, noise, t, TwoSidedP(t, nr - 1), first_part}); + } + }; + for (int s = 0; s < N_STATS; s++) { + compare(s, early_blocks, late_blocks, false); + compare(s, late_blocks, early_blocks, true); + } + // The rotation scale is a RATE: a stage turning at the wrong rate leaves a residual about the + // spindle that grows with the angle, and the scale decided on the first part zeroes its slope + // there. So the slope of the late blocks is compared with that of the early ones, each a + // straight-line fit to its blocks' means with its own standard error. + { + struct Fit { double slope = NAN, se = NAN; int blocks = 0; }; + const auto fit = [&](const std::vector &ranges) { + std::vector x, y; + for (const auto &r : ranges) + if (const Block b = Summarise(frame[ABOUT_SPINDLE], r.first, r.last); std::isfinite(b.mean)) { + x.push_back(0.5 * (deg(r.first) + deg(r.last))); + y.push_back(b.mean); + } + Fit f; + f.blocks = static_cast(x.size()); + if (f.blocks < 3) + return f; + double mx = 0.0, my = 0.0; + for (int i = 0; i < f.blocks; i++) { + mx += x[i]; + my += y[i]; + } + mx /= f.blocks; + my /= f.blocks; + double sxx = 0.0, sxy = 0.0; + for (int i = 0; i < f.blocks; i++) { + sxx += (x[i] - mx) * (x[i] - mx); + sxy += (x[i] - mx) * (y[i] - my); + } + f.slope = sxy / sxx; + double rss = 0.0; + for (int i = 0; i < f.blocks; i++) { + const double e = y[i] - my - f.slope * (x[i] - mx); + rss += e * e; + } + f.se = std::sqrt(rss / (f.blocks - 2) / sxx); + return f; + }; + const Fit fe = fit(early_blocks), fl = fit(late_blocks); + if (fe.blocks >= 3 && fl.blocks >= 3) { + const double noise = std::sqrt(fe.se * fe.se + fl.se * fl.se); + const double t = noise > 0.0 ? (fl.slope - fe.slope) / noise : 0.0; + comparisons.push_back({RATE, {late_blocks.front().first, late_blocks.back().last}, fl.slope, + fe.slope, noise, t, TwoSidedP(t, fe.blocks + fl.blocks - 4), false}); } } const double alpha = std::erfc(SPOT_BUDGET_SIGNIFICANCE_Z / std::sqrt(2.0)); const double family = static_cast(comparisons.size()); const auto describe = [&](const Comparison &c, int out_of_line, int late) { const Stat &st = stats[c.stat]; - return fmt::format("{} over {:.0f}-{:.0f} deg is {:.3g}{} against {:.3g}{} +- {:.2g} on the first part " - "(t = {:.1f}, p = {:.2g} in a family of {}; {} of {} late blocks out of line)", + return fmt::format("{} over {:.0f}-{:.0f} deg is {:.3g}{} against {:.3g}{} +- {:.2g} on {} " + "(t = {:.1f}, p = {:.2g} in a family of {}; {} of {} blocks out of line)", st.what, deg(c.range.first), deg(c.range.last), c.value * st.unit, st.suffix, - c.mean * st.unit, st.suffix, c.noise * st.unit, c.t, c.p, comparisons.size(), - out_of_line, late); + c.mean * st.unit, st.suffix, c.noise * st.unit, + c.first_part ? "the rest of the sweep" : "the first part", c.t, c.p, + comparisons.size(), out_of_line, late); }; std::vector revisits; + std::string by_quantity; // each quantity's smallest p times the family, for the record const Comparison *closest = nullptr; - for (int s = 0; s < N_STATS; s++) { + for (int s = 0; s <= RATE; s++) { const Comparison *worst = nullptr; - int out_of_line = 0, late = 0; + int out_of_line = 0, tested = 0; for (const auto &c : comparisons) if (c.stat == s) { - late++; + tested++; if (c.p * family < alpha) out_of_line++; if (!worst || c.p < worst->p) @@ -237,9 +297,11 @@ void Rugnux::FarEndCheck(PipelineLocals &p, const RotationIndexerResult &rot) { } if (!worst) continue; + by_quantity += fmt::format("{}{} {:.2g}", by_quantity.empty() ? "" : ", ", stats[s].tag, + std::min(1.0, worst->p * family)); if (out_of_line > 0) revisits.push_back(fmt::format("{} would be revisited because {}", stats[s].decision, - describe(*worst, out_of_line, late))); + describe(*worst, out_of_line, tested))); if (!closest || worst->p < closest->p) closest = worst; } @@ -254,12 +316,12 @@ void Rugnux::FarEndCheck(PipelineLocals &p, const RotationIndexerResult &rot) { logger.Info("far-end check: nothing to compare ({}; the first part ends at {:.0f} deg)", blocks, deg(early)); else if (revisits.empty()) - logger.Info("far-end check: early decisions hold ({}; closest: {})", blocks, - describe(*closest, 0, late_of(closest->stat))); + logger.Info("far-end check: early decisions hold ({}; closest: {}; p x family by quantity: {})", + blocks, describe(*closest, 0, late_of(closest->stat)), by_quantity); else { std::string line; for (const auto &r : revisits) line += (line.empty() ? "" : "; ") + r; - logger.Info("far-end check: {} ({})", line, blocks); + logger.Info("far-end check: {} ({}; p x family by quantity: {})", line, blocks, by_quantity); } }