From 2ad460b9580bfc5892549f6bac4070517cec0162 Mon Sep 17 00:00:00 2001 From: frapercan Date: Wed, 19 Aug 2026 10:19:02 +0200 Subject: [PATCH] feat(eval): the sink carries which protein each row is The arrays were anonymous. Every axis they exist to support, sequence length, identity to the nearest donor, taxonomic relation, is keyed by protein, so arrays indexed by an opaque row number cannot be joined to any of them and the feature does not deliver what it was built for. Records now carry the ground truth's protein-to-index map and a row_index mapping array row to that numbering. Both are needed because two numberings are in play: the NK/LK kernel sees every row, so array row i is matrix row i, while the PK kernel can be handed a subset via pred[proteins_has_gt] and the two then disagree. compute_metrics knows which subsetting it performed, so it is what records the mapping rather than leaving the caller to reconstruct it. Getting this wrong would attribute one protein's score to another, which is precisely the failure the consuming project spent a week correcting in its neighbour search. It is worth a test rather than a comment, so there is one asserting row_index has exactly one entry per array row. Identity is absent rather than guessed when not supplied, and the id map is copied rather than aliased. --- src/cafaeval/evaluation.py | 37 ++++++++++++++++++---- tests/test_per_protein_sink.py | 56 ++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 6 deletions(-) diff --git a/src/cafaeval/evaluation.py b/src/cafaeval/evaluation.py index 9370539..d8508e7 100644 --- a/src/cafaeval/evaluation.py +++ b/src/cafaeval/evaluation.py @@ -46,7 +46,7 @@ def __init__(self): self.records = [] def record(self, *, tp_at_tau, pred_at_tau, n_gt, protein_rows=None, - ns=None, variant=None): + ns=None, variant=None, row_index=None, ids=None): """Store one kernel's per-protein arrays. Copies are taken: the kernels reuse and mutate their buffers, and a sink @@ -65,6 +65,15 @@ def record(self, *, tp_at_tau, pred_at_tau, n_gt, protein_rows=None, # headline metric. "ns": ns, "variant": variant, + # Which protein each array row is. Without these the arrays are + # anonymous and cannot be joined to anything, which defeats the + # purpose: the axes they exist to support (length, identity to + # the nearest donor, taxonomic relation) are all keyed by + # protein. ``row_index`` maps array row to the index used in + # ``ids``, because the PK path hands the kernel a subset of rows + # and the two numberings then differ. + "row_index": None if row_index is None else np.asarray(row_index).copy(), + "ids": None if ids is None else dict(ids), } ) @@ -200,7 +209,8 @@ def compute_confusion_matrix_exclude(tau_arr, g_perprotein, pred_matrix, toi_per def compute_confusion_matrix_sparse(tau_arr, g, pred_matrix, toi, n_gt, ic_arr=None, per_protein_sink=None, sink_ns=None, - sink_variant=None): + sink_variant=None, sink_row_index=None, + sink_ids=None): """Sparse alternative to :func:`compute_confusion_matrix`. Scatters each non-zero prediction into the highest tau bin at which it is @@ -301,7 +311,8 @@ def compute_confusion_matrix_sparse(tau_arr, g, pred_matrix, toi, n_gt, ic_arr=N # from these same arrays, so a caller can check the two agree. if per_protein_sink is not None: per_protein_sink.record(tp_at_tau=tp_at_tau, pred_at_tau=pred_at_tau, n_gt=n_gt, - ns=sink_ns, variant=sink_variant) + ns=sink_ns, variant=sink_variant, + row_index=sink_row_index, ids=sink_ids) return metrics @@ -309,6 +320,7 @@ def compute_confusion_matrix_sparse(tau_arr, g, pred_matrix, toi, n_gt, ic_arr=N def compute_confusion_matrix_exclude_sparse( tau_arr, pred_sub, gt_sub, toi_mask, excluded_mask, n_gt, ic_arr=None, per_protein_sink=None, sink_ns=None, sink_variant=None, + sink_row_index=None, sink_ids=None, ): """Sparse alternative to :func:`compute_confusion_matrix_exclude`. @@ -428,6 +440,7 @@ def compute_confusion_matrix_exclude_sparse( per_protein_sink.record( tp_at_tau=tp_at_tau, pred_at_tau=pred_at_tau, n_gt=n_gt, protein_rows=eligible_rows, ns=sink_ns, variant=sink_variant, + row_index=sink_row_index, ids=sink_ids, ) return metrics @@ -477,7 +490,8 @@ def _cme_worker(tau_chunk): ) def compute_metrics(pred, gt_matrix, tau_arr, toi, gt_exclude=None, ic_arr=None, n_cpu=0, - per_protein_sink=None, sink_ns=None, sink_variant=None): + per_protein_sink=None, sink_ns=None, sink_variant=None, + sink_ids=None): """ Takes the prediction and the ground truth and for each threshold in tau_arr calculates the confusion matrix and returns the coverage, @@ -636,9 +650,12 @@ 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. 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_ids=sink_ids, ) elif gt_exclude is None: # Dense fallback kernel (opt-in, CAFAEVAL_SPARSE=0) needs a dense @@ -657,9 +674,15 @@ def compute_metrics(pred, gt_matrix, tau_arr, toi, gt_exclude=None, ic_arr=None, pred_sub = pred else: pred_sub = pred[proteins_has_gt, :] + # PK path may hand the kernel a subset, so the array row numbering and + # the ``ids`` numbering differ. Record which original rows went in. metrics = compute_confusion_matrix_exclude_sparse( tau_arr, pred_sub, gt_with_annots, toi_mask, excluded_mask, n_gt, ic_arr, per_protein_sink=per_protein_sink, sink_ns=sink_ns, sink_variant=sink_variant, + sink_row_index=( + np.flatnonzero(proteins_has_gt) if per_protein_sink is not None else None + ), + sink_ids=sink_ids, ) else: # Dense fallback: fan out tau chunks over a fork pool using the @@ -795,7 +818,8 @@ def evaluate_prediction(prediction, gt, ontologies, tau_arr, gt_exclude=None, no t_metrics = time.time() metrics_df = compute_metrics( prediction[ns].matrix, gt[ns].matrix, tau_arr, ontologies[ns].toi, exclude, None, n_cpu, - per_protein_sink=per_protein_sink, sink_ns=ns, sink_variant="unweighted") + per_protein_sink=per_protein_sink, sink_ns=ns, sink_variant="unweighted", + sink_ids=getattr(gt[ns], "ids", None)) t_norm = time.time() dfs.append(normalize(metrics_df, ns, tau_arr, ne, normalization)) _eval_logger.info( @@ -820,7 +844,8 @@ def evaluate_prediction(prediction, gt, ontologies, tau_arr, gt_exclude=None, no t_metrics_w = time.time() metrics_df_w = compute_metrics( prediction[ns].matrix, gt[ns].matrix, tau_arr, ontologies[ns].toi_ia, exclude, ontologies[ns].ia, n_cpu, - per_protein_sink=per_protein_sink, sink_ns=ns, sink_variant="weighted") + per_protein_sink=per_protein_sink, sink_ns=ns, sink_variant="weighted", + sink_ids=getattr(gt[ns], "ids", None)) t_norm_w = time.time() dfs_w.append(normalize(metrics_df_w, ns, tau_arr, ne, normalization)) _eval_logger.info( diff --git a/tests/test_per_protein_sink.py b/tests/test_per_protein_sink.py index 11788fe..90d262e 100644 --- a/tests/test_per_protein_sink.py +++ b/tests/test_per_protein_sink.py @@ -148,3 +148,59 @@ def test_context_defaults_to_none_rather_than_to_a_guess(self) -> None: compute_confusion_matrix_sparse(tau, gt, pred, toi, n_gt, per_protein_sink=sink) assert sink.records[0]["ns"] is None assert sink.records[0]["variant"] is None + + +class TestTheRowsCanBeNamed: + """Anonymous arrays are useless for the purpose the sink exists for. + + Every axis the sink is meant to support, sequence length, identity to the + nearest donor, taxonomic relation, is keyed by protein. Arrays indexed by an + 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. + """ + + def test_the_record_carries_the_id_map_and_the_row_index(self) -> None: + tau, gt, pred, toi, n_gt = _toy() + ids = {f"P{i:05d}": i for i in range(gt.shape[0])} + sink = PerProteinSink() + compute_confusion_matrix_sparse( + tau, gt, pred, toi, n_gt, per_protein_sink=sink, + sink_row_index=np.arange(gt.shape[0]), sink_ids=ids, + ) + rec = sink.records[0] + assert rec["ids"] == ids + np.testing.assert_array_equal(rec["row_index"], np.arange(gt.shape[0])) + + def test_row_index_has_one_entry_per_array_row(self) -> None: + """The property that makes the join sound rather than plausible.""" + tau, gt, pred, toi, n_gt = _toy() + sink = PerProteinSink() + compute_confusion_matrix_sparse( + tau, gt, pred, toi, n_gt, per_protein_sink=sink, + sink_row_index=np.arange(gt.shape[0]), + ) + rec = sink.records[0] + assert len(rec["row_index"]) == rec["tp_at_tau"].shape[0] + + def test_the_id_map_is_copied_not_aliased(self) -> None: + tau, gt, pred, toi, n_gt = _toy() + ids = {"P1": 0} + sink = PerProteinSink() + compute_confusion_matrix_sparse( + tau, gt, pred, toi, n_gt, per_protein_sink=sink, sink_ids=ids, + ) + ids["P2"] = 1 + assert "P2" not in sink.records[0]["ids"] + + def test_identity_is_absent_rather_than_guessed_when_not_supplied(self) -> None: + tau, gt, pred, toi, n_gt = _toy() + sink = PerProteinSink() + 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