Refinement: R-free compared paired; the model as given kept only where better
R-free in FreeSetPrefers is now compared as a paired difference: each trial keeps R-free as one term per free twin group (FreeRTerms, the grouping TwinNull's paired z uses, now shared as TwinGroups), and a candidate may be higher than the incumbent by no more than the standard error of the sum of the groups' differences. The single-model standard error R_free / sqrt(2 N_free) it replaces was several times wider than the scatter of two trials from one start on the same reflections, which left -LL free deciding almost alone. The as-given guard now asks whether the model as given displaces the refinement on the free set (rather than whether the refinement displaces it), and never keeps it where the refinement is better on both R-work and R-free: on one guard set the refinement lowered R-free 0.2408 -> 0.2373 and R-work 0.242 -> 0.215 but raised -LL free by 7, and the model as given came out. That -LL free is higher there however D and the variance are fitted (on the free set, on the work set, on both, or crosswise), so it is not a consequence of fitting the variance on the free set. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UqXYdHHZ4cogS1mrGUtfN7
This commit is contained in:
1 parent
ce608b5e18
commit
adde6d5a5c
2 files changed
+72
-26
No files matched your search
@@ -93,11 +93,21 @@ constexpr double MAX_RMSD_BONDS = 0.02, MAX_RMSD_ANGLES = 2.0;
|
||||
constexpr int SOLVENT_ITERATIONS = 10;
|
||||
// The free set decides the weights and the displacement model alone, and only where both of its measures agree: a
|
||||
// candidate is preferred to the incumbent where its -ln L of the free reflections is lower and its R-free not higher
|
||||
// by more than the standard error of the incumbent's, R_free / sqrt(2 N_free).
|
||||
// Following Tickle, Laskowski & Moss (2000) Acta Cryst. D56, 442-450 (the standard error of R-free), as PDB-REDO
|
||||
// decides on more than one free measure, Joosten, Long, Murshudov & Perrakis (2014) IUCrJ 1, 213-220
|
||||
bool FreeSetPrefers(double r_free, double nll_free, double r_free_b, double nll_free_b, int n_free) {
|
||||
return nll_free < nll_free_b && r_free < r_free_b + r_free_b / std::sqrt(2.0 * n_free);
|
||||
// than the incumbent's by more than the standard error of the difference. The two are scored on the same free
|
||||
// reflections, so the difference is paired: R-free is a sum of one term per free twin group (FreeRTerms), and the
|
||||
// standard error is that of the sum of the groups' differences.
|
||||
// As PDB-REDO decides on more than one free measure, Joosten, Long, Murshudov & Perrakis (2014) IUCrJ 1, 213-220
|
||||
bool FreeSetPrefers(const std::vector<float> &r_terms, double nll_free, const std::vector<float> &r_terms_b,
|
||||
double nll_free_b) {
|
||||
const size_t n = r_terms.size();
|
||||
double sum = 0, sum2 = 0;
|
||||
for (size_t k = 0; k < n; k++) {
|
||||
const double d = r_terms[k] - r_terms_b[k];
|
||||
sum += d;
|
||||
sum2 += d * d;
|
||||
}
|
||||
const double se = n > 1 ? std::sqrt(std::max(sum2 - sum * sum / n, 0.0) * n / (n - 1)) : 0.0;
|
||||
return nll_free < nll_free_b && sum < se;
|
||||
}
|
||||
// Every other step is taken on the work-set target (WorkTarget) where it lowers it by more than this, relative.
|
||||
constexpr double TARGET_TOLERANCE = 1e-4;
|
||||
@@ -1909,6 +1919,39 @@ std::string DomainText(const RefinementReflections &refl, const XrayRowModel &ro
|
||||
// As REFMAC5 drops twin domains of small fraction, Murshudov et al. (2011) Acta Cryst. D67, 355-367
|
||||
constexpr double TWIN_DOMAIN_MIN = 0.05;
|
||||
|
||||
// Each observed reflection's twin group, named by its lowest observed row (itself where there is no twin law).
|
||||
std::vector<int> TwinGroups(const RefinementReflections &refl) {
|
||||
const size_t n = refl.Size();
|
||||
std::vector<int> group(n);
|
||||
for (size_t m = 0; m < n; m++) {
|
||||
group[m] = static_cast<int>(m);
|
||||
for (size_t d = 0; d < refl.Laws(); d++)
|
||||
if (refl.mates[d][m] < static_cast<int>(n))
|
||||
group[m] = std::min(group[m], refl.mates[d][m]);
|
||||
}
|
||||
return group;
|
||||
}
|
||||
|
||||
// R-free of the model as the last Fcalc left it, as one term per free twin group, in the order of the groups (the
|
||||
// members of a group share their F_calc, so their terms are summed into one, as TwinNull sums them): the terms
|
||||
// FreeSetPrefers pairs.
|
||||
std::vector<float> FreeRTerms(const Context &ctx) {
|
||||
const std::vector<std::complex<float>> fmodel = TwinnedModel(ctx.refl, ctx.rows, ctx.fcalc);
|
||||
const std::vector<int> group = TwinGroups(ctx.refl);
|
||||
double fo = 0;
|
||||
std::vector<double> sum(ctx.refl.Size(), 0.0);
|
||||
for (size_t m = 0; m < ctx.refl.Size(); m++)
|
||||
if (ctx.refl.free[m]) {
|
||||
fo += ctx.refl.fo[m];
|
||||
sum[group[m]] += std::fabs(ctx.refl.fo[m] - std::abs(fmodel[m]));
|
||||
}
|
||||
std::vector<float> terms;
|
||||
for (size_t m = 0; m < ctx.refl.Size(); m++)
|
||||
if (ctx.refl.free[m] && group[m] == static_cast<int>(m))
|
||||
terms.push_back(static_cast<float>(sum[m] / fo));
|
||||
return terms;
|
||||
}
|
||||
|
||||
// The null of a twinned refinement, for the model as the last Refit() left it: its R-free at the same fraction with
|
||||
// every reflection's mate replaced by an unrelated observed one of the same resolution and the same set (free with
|
||||
// free, work with work, as a law's twin pairs are). The reflections of each set are put in order of 1/d^2 with the two
|
||||
@@ -1932,14 +1975,7 @@ constexpr double TWIN_STOP_Z = 0.5;
|
||||
void TwinNull(const Context &ctx, RefinementResult &result, size_t first = 0) {
|
||||
RefinementReflections null = ctx.refl;
|
||||
const size_t n = null.Size();
|
||||
// each observed reflection's twin group, named by its lowest observed row
|
||||
std::vector<int> group(n);
|
||||
for (size_t m = 0; m < n; m++) {
|
||||
group[m] = static_cast<int>(m);
|
||||
for (size_t d = 0; d < ctx.refl.Laws(); d++)
|
||||
if (ctx.refl.mates[d][m] < static_cast<int>(n))
|
||||
group[m] = std::min(group[m], ctx.refl.mates[d][m]);
|
||||
}
|
||||
const std::vector<int> group = TwinGroups(ctx.refl);
|
||||
for (size_t d = first; d < null.Laws(); d++)
|
||||
std::iota(null.mates[d].begin(), null.mates[d].end(), 0);
|
||||
for (const bool free : {false, true}) {
|
||||
@@ -2265,6 +2301,7 @@ void MeasureWorkTarget(Context &ctx, const RefinementTopology &topo, const NcsCo
|
||||
const RefinementScore sc = ctx.Score();
|
||||
t.nll_work = sc.nll_work;
|
||||
t.nll_free = sc.nll_free;
|
||||
t.free_r_terms = FreeRTerms(ctx);
|
||||
const std::vector<std::array<double, 4>> xyzb = ctx.XyzB();
|
||||
t.e_geometry = EvaluateRestraints(RestraintsNow(ctx, topo, ncs, ncs_sigma), xyzb, nullptr);
|
||||
t.e_b = EvaluateRestraints(BRestraintsNow(ctx, topo), xyzb, nullptr);
|
||||
@@ -2938,6 +2975,7 @@ RFactors ReadInputDisplacements(Context &ctx, const std::vector<TlsBody> &tls, L
|
||||
int best = -1;
|
||||
std::vector<RFactors> rf;
|
||||
std::vector<double> nll_free;
|
||||
std::vector<std::vector<float>> r_terms;
|
||||
for (size_t k = 0; k < readings.size(); k++) {
|
||||
ctx.SetB(readings[k].b);
|
||||
ctx.SetAniso(readings[k].aniso);
|
||||
@@ -2947,9 +2985,10 @@ RFactors ReadInputDisplacements(Context &ctx, const std::vector<TlsBody> &tls, L
|
||||
ctx.ForgetStarts();
|
||||
rf.push_back(ctx.Refit());
|
||||
nll_free.push_back(ctx.Score().nll_free);
|
||||
r_terms.push_back(FreeRTerms(ctx));
|
||||
logger.Info("Refinement: the model's {} TLS groups ({} atoms) read as {}: R-work {:.4f}, R-free {:.4f}, -LL free "
|
||||
"{:.1f}", tls.size(), tls_u.size(), readings[k].name, rf[k].r_work, rf[k].r_free, nll_free[k]);
|
||||
if (best < 0 || FreeSetPrefers(rf[k].r_free, nll_free[k], rf[best].r_free, nll_free[best], rf[k].n_free))
|
||||
if (best < 0 || FreeSetPrefers(r_terms[k], nll_free[k], r_terms[best], nll_free[best]))
|
||||
best = static_cast<int>(k);
|
||||
}
|
||||
ctx.SetB(readings[best].b);
|
||||
@@ -3194,6 +3233,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
|
||||
result.r_work_start = r.r_work;
|
||||
result.r_free_start = r.r_free;
|
||||
result.score_start = ctx.Score();
|
||||
const std::vector<float> start_r_terms = FreeRTerms(ctx);
|
||||
logger.Info("Refinement{}: {} atoms ({} riding hydrogens), {} reflections to {:.2f} A ({} free), on the {}; as "
|
||||
"given R-work {:.4f}, R-free {:.4f}; k_sol {:.3f}, b_sol {:.1f}, overall B {:.1f}, Wilson B {:.0f}, B "
|
||||
"ceiling {:.0f}, mask grid {:.2f} A",
|
||||
@@ -3211,6 +3251,7 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
|
||||
given.trial.r_work = r.r_work;
|
||||
given.trial.r_free = r.r_free;
|
||||
given.trial.nll_free = result.score_start.nll_free;
|
||||
given.trial.free_r_terms = start_r_terms;
|
||||
given.pos = ctx.Positions();
|
||||
given.b = ctx.B();
|
||||
given.aniso = ctx.aniso;
|
||||
@@ -3461,10 +3502,9 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
|
||||
|
||||
// A trial at other weights (or the model as given) displaces another only where its geometry is within
|
||||
// bounds and the free set prefers it (FreeSetPrefers).
|
||||
const int n_free = result.n_free;
|
||||
auto displaces = [n_free](const RefinementWeightTrial &t, const RefinementWeightTrial &b) {
|
||||
auto displaces = [](const RefinementWeightTrial &t, const RefinementWeightTrial &b) {
|
||||
return t.within_bounds &&
|
||||
(!b.within_bounds || FreeSetPrefers(t.r_free, t.nll_free, b.r_free, b.nll_free, n_free));
|
||||
(!b.within_bounds || FreeSetPrefers(t.free_r_terms, t.nll_free, b.free_r_terms, b.nll_free));
|
||||
};
|
||||
// A trial taken on from `s`: at the weights of s, where the work-set target (WorkTarget) is lower than that of s
|
||||
// by more than TARGET_TOLERANCE, and the geometry within bounds where that of s is. A trial at other weights
|
||||
@@ -4377,10 +4417,17 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
|
||||
// NCS; the second chose the tightest rung without, and from the B factors the first left, its TLS bodies
|
||||
// 0.02 lower in R-free. Where the ladder and the trajectory leave the model where it was, the hypotheses
|
||||
// would run the same trials from the same model again, so the round ends there: from a model refined
|
||||
// before, a round is its ladder and one step. No round where the refinement does not displace the model
|
||||
// as given (5.).
|
||||
// before, a round is its ladder and one step. No round where the model as given is kept over the
|
||||
// refinement (5.).
|
||||
// The model as given kept over the refinement: where the free set prefers it, as one weight is preferred to
|
||||
// another (displaces), and the refinement is not better on both R-work and R-free. The work-set target cannot
|
||||
// choose here, the refinement having minimised it from the model as given.
|
||||
auto given_kept = [&]() {
|
||||
return displaces(given.trial, state.trial) &&
|
||||
!(state.trial.r_free < given.trial.r_free && state.trial.r_work < given.trial.r_work);
|
||||
};
|
||||
for (int round = 2; round <= settings.rounds && settings.adp_hypotheses && !stopped && state.ran &&
|
||||
!ctx.Cancelled() && (!settings.as_given_competes || displaces(state.trial, given.trial));
|
||||
!ctx.Cancelled() && (!settings.as_given_competes || !given_kept());
|
||||
round++) {
|
||||
const TrialOutcome before = state;
|
||||
const std::vector<TlsBody> own_tls = state.tls;
|
||||
@@ -4451,12 +4498,10 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector<Me
|
||||
break;
|
||||
}
|
||||
}
|
||||
// 5. The model as given, first in order of trust: the refinement is kept where it displaces it on the free set
|
||||
// as a weight does (displaces), else the model is given back as it came (the solvent build's edits undone),
|
||||
// so that what comes out is never worse on the free set than what went in - where the free set is free to
|
||||
// the model as given (RefinementSettings::as_given_competes). The work-set target cannot make this choice:
|
||||
// the refinement minimised it from the model as given.
|
||||
if (settings.as_given_competes && state.ran && !ctx.Cancelled() && !displaces(state.trial, given.trial)) {
|
||||
// 5. The model as given: where it is kept over the refinement (given_kept), it is given back as it came (the
|
||||
// solvent build's edits undone), so that what comes out is never worse on both free measures than what went
|
||||
// in - where the free set is free to the model as given (RefinementSettings::as_given_competes).
|
||||
if (settings.as_given_competes && state.ran && !ctx.Cancelled() && given_kept()) {
|
||||
logger.Info("Refinement: the model as given kept (R-work {:.4f}, R-free {:.4f}, -LL free {:.1f}), the "
|
||||
"refinement did not displace it (R-work {:.4f}, R-free {:.4f}, -LL free {:.1f})",
|
||||
given.trial.r_work, given.trial.r_free, given.trial.nll_free, state.trial.r_work,
|
||||
|
||||
@@ -163,6 +163,7 @@ struct RefinementWeightTrial {
|
||||
// The work-set target's terms where the trial ended (RefineOnce's WorkTarget): -ln L of the working reflections,
|
||||
// the restraints' sum of the geometry (with the NCS restraints at ncs_sigma) and that of the B similarity.
|
||||
double nll_work = NAN, e_geometry = NAN, e_b = NAN;
|
||||
std::vector<float> free_r_terms; // R-free as one term per free twin group (FreeRTerms), for paired comparisons
|
||||
GeometryStatistics geometry;
|
||||
bool within_bounds = false; // the geometry bounds the choice is made inside
|
||||
std::string adp; // the displacement model: "isotropic", "TLS ..." or "anisotropic"
|
||||
|
||||
Reference in new issue
Block a user