rugnux: an R that compares between runs, beside the one that makes the maps

R_WORK and R_FREE score the data against a model scaled by k_overall, a
symmetry-constrained anisotropic B and a flat bulk solvent. That scale is
deliberate - a free per-shell rescale would reshape each map's radial
amplitude profile and a batch of maps would no longer be comparable - but it
can only bend one way with resolution, so whatever a dataset's own radial
profile does that k*exp(-B s^2) cannot follow is reported as R. Two reductions
of one crystal whose amplitudes differ radially therefore differ in R_FREE for
a reason that has nothing to do with either fitting the model: measured on one
such pair of runs, R_FREE moved a median +0.042 over eleven sets where
phenix.model_vs_data, which scales per shell, moved +0.002.

Report a second reading of the same fit: R_MODEL_SHELL_SCALED, one free scale
per resolution shell of the merge table, with R_MODEL as the same sum without
it and MODEL_RADIAL_MISFIT (RMS of ln k_shell) as the size of the rescale, so
the reason a comparison is or is not readable is on the page. Over all the
reflections, not the free 5%: nothing here is refined, so work and free
estimate the same quantity and the split only costs precision.

Nothing is written from it. On two sets, every output file - both maps, the
anomalous map, the map coefficients, the MTZ, the HKL and the CIF - is byte
for byte what the same commit produced without this change, and R_WORK and
R_FREE are unchanged.

On the pair above, the new R moves a median +0.0005 and holds every set
inside 0.0065 except the one that is a real regression, which it puts first
at +0.0174 where the old metric ranked it seventh. The battery reports it
beside R_free, and scores neither.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013nW6FNRP1bBJJ8pfHiByAT
This commit is contained in:
leonarski_fandClaude Opus 5 committed 2026-09-21 14:09:39 +02:00
1 parent 29b5d7cce1
commit 8364cfbc40
10 files changed
+235 -4

No files matched your search

+66
View File
@@ -916,6 +916,72 @@ ModelValidationResult ValidateAgainstModel(const std::vector<MergedReflection> &
result.cc_model_overall, result.cc_model_n,
result.cc_model_shells.back().d_min, result.cc_model_shells.back().cc,
result.cc_model_shells.back().n, result.cc_model_shells.back().sigma);
// --- the same R with the radial profile taken out, and how much of it there was ---
// R above is what the maps are made of and is read WITHIN a run; this is the one to read
// BETWEEN two. The scale the maps use can only bend as k*exp(-B s^2) (see the note in
// fit_model, and the reason it stays that way), so where two reductions of one crystal differ
// in the radial profile of their amplitudes, the part of the difference that shape cannot
// follow lands in R - measured on this corpus, enough to move R by more than a real change in
// the data does. Giving the scale one free factor per shell removes the radial profile and
// only the radial profile: what is left is the agreement inside each shell, which is the
// question "do these data fit this model better or worse than those did".
//
// On ALL the reflections, not the free set. Nothing here is refined - the coordinates, the B
// factors and the occupancies are the model's own - so work and free estimate the same
// quantity (measured: the two differ by a median 0.001 over the corpus), and the 5% split
// therefore buys no cross-validation while costing a factor of sqrt(20) in precision. Read on
// all of them, the number is worth about +-0.002 per set instead of +-0.010, which is the
// difference between seeing a real change and seeing the sampling of a free set.
//
// Nothing is written from any of this. fmodel, the maps, the MTZ and the R-factors above are
// untouched; it is a second reading of the fit that has already happened.
double num = 0, den = 0, plain_num = 0, plain_den = 0;
int n_scored = 0;
std::vector<double> sum_oc(report_shell_d_min.size(), 0), sum_cc(report_shell_d_min.size(), 0);
for (const auto &hv : fmodel.v) {
const auto it = obs_by_hkl.find(hkl_key(hv.hkl));
if (it == obs_by_hkl.end()) continue;
const size_t bin = shell_of(it->second.d);
if (bin >= sum_oc.size()) continue;
sum_oc[bin] += it->second.F * std::abs(hv.value);
sum_cc[bin] += static_cast<double>(std::abs(hv.value)) * std::abs(hv.value);
}
// The shell's scale by least squares on its own reflections. One parameter per shell against
// the thousands of reflections in it, so scoring the same reflections it was fitted on lowers
// R by far less than the difference it is there to expose.
std::vector<double> ln_k;
ln_k.reserve(report_shell_d_min.size());
for (size_t i = 0; i < sum_oc.size(); i++)
if (sum_cc[i] > 0 && sum_oc[i] > 0)
ln_k.push_back(std::log(sum_oc[i] / sum_cc[i]));
for (const auto &hv : fmodel.v) {
const auto it = obs_by_hkl.find(hkl_key(hv.hkl));
if (it == obs_by_hkl.end()) continue;
const size_t bin = shell_of(it->second.d);
if (bin >= sum_oc.size()) continue;
const double Fo = it->second.F, Fc = std::abs(hv.value);
const double k = (sum_cc[bin] > 0 && sum_oc[bin] > 0) ? sum_oc[bin] / sum_cc[bin] : 1.0;
num += std::fabs(Fo - k * Fc);
den += Fo;
plain_num += std::fabs(Fo - Fc);
plain_den += Fo;
++n_scored;
}
if (den > 0 && ln_k.size() >= 2) {
result.r_model = plain_num / plain_den;
result.r_model_shell_scaled = num / den;
result.n_model = n_scored;
result.shell_scale_bins = static_cast<int>(ln_k.size());
const double mean_ln_k = std::accumulate(ln_k.begin(), ln_k.end(), 0.0) / ln_k.size();
double var = 0;
for (double v : ln_k) var += (v - mean_ln_k) * (v - mean_ln_k);
result.radial_misfit = std::sqrt(var / ln_k.size());
logger.Info("Model validation: R(all reflections) {:.4f}, with a per-shell scale {:.4f} "
"over {} shells; radial misfit {:.3f}",
result.r_model, result.r_model_shell_scaled, result.shell_scale_bins,
result.radial_misfit);
}
}
// --- sigma_A weighting: the maps are 2mFo-DFc and mFo-DFc, not 2Fo-Fc and Fo-Fc ---