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]}"