From b600319d2c1a34bb1ceaadd4ec28173e547b458b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Miguel=20P=C3=A9rez=20Canales?= Date: Tue, 1 Sep 2026 12:52:12 +0200 Subject: [PATCH] fix(eval): a protein with no scoreable truth is not a false positive The PK kernel restricted coverage to eligible rows and left every mass summing over all of them. A protein whose TOI ground truth was entirely already known in t0 has nothing left to score, so normalize() drops it from every denominator, and its predictions still landed in `fp`. Every one of those predictions is a false positive by construction: there is no term it could have emitted that would have counted. Micro precision was therefore charged for proteins the evaluation was not measuring. Restricting `pred_totals` to eligible rows is the whole fix. `tp_totals` is restricted with it, which is a no-op under the current invariant (a column can only be a TP when it is in TOI and not excluded, so an ineligible row already contributes zero) and is done so the two totals share one population rather than resting on that invariant. Measured on the fixture this file already carried: FP at the low threshold goes from 4 to 2 and every other column is unchanged, n=2, tp=2, fn=5, macro precision 1, macro recall 5/6. The new test pins all six so a later widening of the restriction cannot pass unnoticed. The docstring of test_pk_precision_recall_unchanged_by_fix said the fix touched only the `n` column. That was true of the coverage fix and is no longer true, so it now names which columns each restriction moves and why the rest cannot. Direction and size on the campaign: this RAISES PK micro precision, so published PK figures were conservative rather than optimistic. On the 2026-08-30 run the excluded mass is 3,385 of 2,648,460 in BPO and 576 of 214,491 in MFO, from ten and eight proteins, order 1e-4 on a base of 0.15. The macro columns the external board reports do not move at all. The dense fallback compute_confusion_matrix_exclude has neither this restriction nor the earlier coverage one. It is gated behind CAFAEVAL_SPARSE=0 for A/B comparison, is not reachable with a per_protein_sink, and has produced no published number, so it is left alone here and flagged rather than changed. --- src/cafaeval/evaluation.py | 27 ++++++++++++++------- tests/test_pk_coverage_bug.py | 44 +++++++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 11 deletions(-) diff --git a/src/cafaeval/evaluation.py b/src/cafaeval/evaluation.py index d8508e7..bbf157a 100644 --- a/src/cafaeval/evaluation.py +++ b/src/cafaeval/evaluation.py @@ -407,17 +407,26 @@ def compute_confusion_matrix_exclude_sparse( pred_at_tau = np.cumsum(delta_pred_w[:, ::-1], axis=1)[:, ::-1] tp_at_tau = np.cumsum(delta_tp_w[:, ::-1], axis=1)[:, ::-1] - tp_totals = tp_at_tau.sum(axis=0) - pred_totals = pred_at_tau.sum(axis=0) - - # For coverage (`n` column) to match the denominator used by normalize() - # — which is the post-exclusion protein count from _count_proteins_in_toi - # — restrict the row count to proteins that still have ≥1 GT annotation - # in TOI after the per-protein exclude mask. Without this, `n` counts - # proteins whose TOI annotations were all already known in t0, while the - # denominator drops them, producing coverage > 1 in PK. + # Restrict every aggregation to proteins that still have >= 1 GT annotation + # in TOI after the per-protein exclude mask. A protein whose TOI truth was + # entirely already known in t0 has no scoreable truth left, so normalize() + # drops it from every denominator (`n` for macro precision, `ne` elsewhere). + # Its predictions must therefore leave the numerators too. + # + # Coverage already did this. The masses did not, which put the predictions + # of those proteins into `fp` while the protein itself was not counted: + # every one of them is a false positive by construction, since nothing they + # can predict is scoreable. Micro precision was charged for them. + # + # `tp_totals` is a no-op under the current invariant, because a column can + # only be a TP when it is in TOI and not excluded, so an ineligible row + # contributes zero already. It is restricted anyway so the two totals share + # one population and the invariant is not load-bearing. eligible_rows = ((gt_sub != 0) & toi_mask[None, :] & (~excluded_mask)).any(axis=1) + tp_totals = tp_at_tau[eligible_rows].sum(axis=0) + pred_totals = pred_at_tau[eligible_rows].sum(axis=0) + metrics[:, 0] = ((pred_at_tau > 0) & eligible_rows[:, None]).sum(axis=0) metrics[:, 1] = tp_totals metrics[:, 2] = pred_totals - tp_totals diff --git a/tests/test_pk_coverage_bug.py b/tests/test_pk_coverage_bug.py index cc2846d..031f29a 100644 --- a/tests/test_pk_coverage_bug.py +++ b/tests/test_pk_coverage_bug.py @@ -83,8 +83,14 @@ def test_pk_coverage_never_exceeds_one(): def test_pk_precision_recall_unchanged_by_fix(): - """Sanity: the fix only touches the `n` column. TP/FP/FN and derived - P/R/F should be identical to pre-fix behaviour.""" + """Sanity on the columns the eligibility restriction does NOT move. + + `n` was restricted first, `fp` second (see + ``test_pk_ineligible_rows_do_not_inflate_fp``). TP, FN and the macro + P/R columns are untouched by either, because an ineligible protein + contributes exactly zero to each: nothing it predicts can be a TP, + its ``n_gt`` is zero, and its macro precision and recall are zero. + """ pred_sub, gt_sub, toi_mask, excluded_mask, n_gt, tau_arr = _make_pk_scenario() metrics = compute_confusion_matrix_exclude_sparse( @@ -97,3 +103,37 @@ def test_pk_precision_recall_unchanged_by_fix(): # So TP at low tau: protein 0 term 2 (TP, weight 1) + protein 2 term 3 (TP, weight 1) = 2 tp = metrics[0, 1] assert tp == 2.0, f"expected TP=2 at low tau, got {tp}" + + +def test_pk_ineligible_rows_do_not_inflate_fp(): + """A protein with no scoreable truth must not be charged as false positives. + + Protein 1 has its whole TOI ground truth already known in t0, so + ``normalize()`` drops it from every denominator. It still predicts terms 2 + and 3, neither of which is on its exclude list, and neither of which it has. + Every one of those predictions is a false positive BY CONSTRUCTION: there is + nothing it could have predicted that would have scored. + + Before the masses were restricted, those predictions landed in ``fp`` while + the protein itself was absent from the population, so micro precision paid + for a protein it was not measuring. FP at the low threshold is 2 (the two + eligible proteins' non-TP predictions), not 4. + """ + pred_sub, gt_sub, toi_mask, excluded_mask, n_gt, tau_arr = _make_pk_scenario() + + metrics = compute_confusion_matrix_exclude_sparse( + tau_arr, pred_sub, gt_sub, toi_mask, excluded_mask, n_gt + ) + + assert metrics[0, 2] == 2.0, ( + f"expected FP=2 at tau=0.1, got {metrics[0, 2]}. A value of 4 means the " + f"ineligible protein's two predictions are still being counted." + ) + + # And the columns that must NOT move, pinned so a future change to the + # restriction cannot quietly widen its reach. + assert metrics[0, 0] == 2.0, "n moved" + assert metrics[0, 1] == 2.0, "tp moved" + assert metrics[0, 3] == 5.0, "fn moved" + assert metrics[0, 4] == 1.0, "macro precision moved" + assert abs(metrics[0, 5] - 5.0 / 6.0) < 1e-9, f"macro recall moved: {metrics[0, 5]}"