Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/cafaeval/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,11 +659,18 @@ def compute_metrics(pred, gt_matrix, tau_arr, toi, gt_exclude=None, ic_arr=None,
if gt_exclude is None and use_sparse:
# Sparse kernel: single-pass scatter + cumsum. No pool needed — the
# kernel is fast enough that fork overhead would dominate.
# NK/LK path passes every row through, so array row i is matrix row i.
# The NK/LK path drops the proteins with no truth in the terms of
# interest before the kernel, as the PK path does, so array row i is
# matrix row ``flatnonzero(proteins_has_gt)[i]``, not row i. Until
# 2026-11-07 this passed ``arange``, which names every row after the
# first dropped protein with its neighbour's id; PROTEA refused those
# tables as unmappable_row_numbering, one namespace per stratum.
metrics = compute_confusion_matrix_sparse(
tau_arr, g, p, toi, n_gt, ic_arr, per_protein_sink=per_protein_sink,
sink_ns=sink_ns, sink_variant=sink_variant,
sink_row_index=(np.arange(p.shape[0]) if per_protein_sink is not None else None),
sink_row_index=(
np.flatnonzero(proteins_has_gt) if per_protein_sink is not None else None
),
sink_ids=sink_ids,
)
elif gt_exclude is None:
Expand Down
45 changes: 40 additions & 5 deletions tests/test_per_protein_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,13 @@ class TestTheRowsCanBeNamed:
opaque row number cannot be joined to any of them, so the sink has to carry
enough to recover which protein each row is.

Two numberings are in play. The NK/LK kernel sees every row, so array row i
is matrix row i. The PK kernel can be handed a subset, and then the two
disagree; ``row_index`` is what reconciles them, and getting this wrong
would silently attribute one protein's score to another, which is the exact
failure this project spent a week correcting elsewhere.
Two numberings are in play. Both the NK/LK and the PK paths drop the
proteins with no truth in the terms of interest before the kernel, so the
array rows can be a subset of the matrix rows; ``row_index`` is what
reconciles them, and getting this wrong would silently attribute one
protein's score to another, which is the exact failure this project spent
a week correcting elsewhere. Until 2026-11-07 this docstring said the NK/LK
kernel sees every row, and the code believed it.
"""

def test_the_record_carries_the_id_map_and_the_row_index(self) -> None:
Expand Down Expand Up @@ -204,3 +206,36 @@ def test_identity_is_absent_rather_than_guessed_when_not_supplied(self) -> None:
compute_confusion_matrix_sparse(tau, gt, pred, toi, n_gt, per_protein_sink=sink)
assert sink.records[0]["ids"] is None
assert sink.records[0]["row_index"] is None


class TestTheNkLkPathNamesTheRowsItKeeps:
"""``compute_metrics`` drops proteins with no truth before the NK/LK kernel.

Its per-protein rows must then be named by the matrix rows they came from.
Naming them ``0..n-1`` gives every row after the first dropped protein the
id of a neighbour. PROTEA refused those tables (unmappable_row_numbering)
for one namespace in each of its NK and LK strata.
"""

@staticmethod
def _run_with_two_proteins_without_truth():
from cafaeval.evaluation import compute_metrics

tau, gt, pred, toi, _ = _toy()
gt = gt.copy()
gt[[0, 2, 4], 0] = 1.0 # these three keep truth
gt[[1, 3], :] = 0.0 # these two have none, so the path drops them
sink = PerProteinSink()
compute_metrics(pred, gt, tau, toi, n_cpu=1, per_protein_sink=sink,
sink_ids={f"P{i}": i for i in range(gt.shape[0])})
return gt, sink.records[0]

def test_row_index_names_the_matrix_rows_that_were_kept(self) -> None:
gt, rec = self._run_with_two_proteins_without_truth()
np.testing.assert_array_equal(rec["row_index"], [0, 2, 4])
assert len(rec["row_index"]) == rec["tp_at_tau"].shape[0]

def test_each_named_row_carries_its_own_proteins_truth(self) -> None:
gt, rec = self._run_with_two_proteins_without_truth()
for i, row in enumerate(rec["row_index"]):
assert rec["n_gt"][i] == gt[row].sum()
Loading