Skip to content

feat(eval): optional side channel for the per-protein vectors - #23

Merged
frapercan merged 1 commit into
mainfrom
feat/per-protein-sink
Aug 19, 2026
Merged

frapercan merged 1 commit into
mainfrom
feat/per-protein-sink

Conversation

@frapercan

Copy link
Copy Markdown
Owner

Why

The kernels build tp_at_tau and pred_at_tau with one row per protein and then
collapse them:

metrics[:, 4] = precision.sum(axis=0)
metrics[:, 5] = recall.sum(axis=0)

axis=0 is the protein axis. Everything downstream sees only the collapsed
number, so a caller wanting to report a score stratified by a per-protein
property has nothing to stratify. Sequence length, identity to the nearest donor
and taxonomic distance are all per protein, and all three are the axes the
consuming project needs.

The shape of the change

PerProteinSink is an optional keyword defaulting to None, on the two sparse
kernels and on compute_metrics.

A sink rather than a return value, deliberately: changing the return shape would
ripple through compute_metrics, evaluate_prediction and cafa_eval, and every
existing caller of each. With no sink passed, every signature, every return type
and every number this library produces is unchanged
, and there is a test
asserting exactly that.

The property the feature rests on

The arrays handed over are the ones the aggregate was computed from, not a
recomputation. So the column sums of the emitted arrays must reproduce the
published tp, fp and coverage columns, and the tests check it:

np.testing.assert_allclose(metrics[:, 1], rec["tp_at_tau"].sum(axis=0), atol=1e-12)

Two routes to one quantity drift unless something checks. A stratified table whose
cells do not sum to the headline is worse than no stratified table.

Two decisions worth flagging

The dense fallback raises rather than leaving the sink empty. It fans tau
chunks over a fork pool, so the per-protein arrays live in worker processes and
never return. A caller that asked for stratification and silently got none would
publish a stratified table with nothing behind it, which is the failure this
whole feature exists to make impossible.

The arrays are copied. The kernels reuse their buffers, so a sink holding a
view would change under its owner between one call and the next. There is a test
for that.

Checks

pytest tests -q: 22 passed, 6 xfailed. ruff check . and mypy clean.

The confusion-matrix kernels build tp_at_tau and pred_at_tau with one row
per protein and then collapse them with .sum(axis=0). Everything
downstream sees only the collapsed number, so a caller wanting a score
stratified by a per-protein property, sequence length, homology to the
donor bank, taxonomic distance, has nothing to stratify.

PerProteinSink is an optional keyword that defaults to None. It is a sink
rather than a return value on purpose: changing the return shape of the
kernels would ripple through compute_metrics, evaluate_prediction and
cafa_eval and every existing caller of each. With no sink passed, all
four signatures, all four return types and every number this library
produces are exactly what they were.

The arrays handed over are the ones the aggregate was computed from
rather than a recomputation, so a caller can verify the two agree. The
tests do exactly that: the column sums of the emitted arrays must
reproduce the published tp, fp and coverage columns. Two routes to one
quantity drift unless something checks, and a stratified table whose
cells do not sum to the headline is worse than no stratified table.

The dense fallback raises NotImplementedError rather than leaving the
sink empty. It fans tau chunks over a fork pool, so the per-protein
arrays live in worker processes and never come back; a caller that asked
for stratification and silently received none would publish a stratified
table with nothing behind it.

Arrays are copied into the sink because the kernels reuse their buffers,
and a sink holding a view would change under its owner. There is a test
for that too.
@frapercan
frapercan merged commit 7335b5b into main Aug 19, 2026
6 checks passed
@frapercan
frapercan deleted the feat/per-protein-sink branch August 19, 2026 00:37
github-actions Bot pushed a commit to frapercan/PROTEA that referenced this pull request Aug 19, 2026
…in scores (#782)

Moves the `cafaeval-protea` pin from `80d705a` to `7335b5b`
(frapercan/cafaeval-protea#23).

`7335b5b` is `80d705a` plus the optional `PerProteinSink` and nothing
else:

```
$ git diff --stat 80d705a 7335b5b -- src/
 src/cafaeval/evaluation.py | 84 +++++++++++++++++++++++++++++++-----
```

The lock delta is the reference alone; no other package resolves
differently.

## Why

The confusion-matrix kernels build `tp_at_tau` and `pred_at_tau` with
one row per
protein and collapse them with `.sum(axis=0)` before anything downstream
sees
them. So a score cannot be stratified by any **per-protein** property,
and
sequence length, identity to the nearest donor and taxonomic relation
are all per
protein. They are also the three axes the evaluation is supposed to
report, and
the reason the current board can only stratify by category and aspect.

## Nothing changes until something asks

The sink is an optional keyword defaulting to `None`. With no sink
passed, every
signature, every return type and every number the library produces are
byte-identical, and the upstream tests assert exactly that rather than
leaving it
to inspection. **The 224 rows already on the board stay valid.**

## One incidental improvement

The previous lock entry read `reference = "main"` while resolving to a
commit, so
the recorded intent was a moving branch. This one pins the commit in
both fields,
so the lock no longer depends on where a branch happens to point.

## What this unblocks

Emitting per-protein scores costs no new predictions: the 32 prediction
sets are
done and clean, and the stratification is an offline pass over them. It
also
gives the paired comparisons for free, since a paired test needs exactly
that
per-protein object.
frapercan added a commit that referenced this pull request Aug 19, 2026
… kernels (#24)

Follow-up to #23, which stopped one level short.

## What was wrong

#23 wired `PerProteinSink` into the two sparse kernels and
`compute_metrics`.
Nobody calls those. The consuming project calls `cafa_eval`:

```
cafa_eval -> evaluate_prediction -> compute_metrics -> kernel
```

So the feature existed and could not be reached from the only entry
point that
uses this library. That is worse than not shipping it, because from
outside it
reads as done.

I caught it while wiring the consumer, at the exact moment of writing
the call
that would have silently collected nothing.

## What this adds

`per_protein_sink` on `evaluate_prediction` and `cafa_eval`, and a
namespace and
variant tag on every record.

The tags are not cosmetic. `evaluate_prediction` calls `compute_metrics`
twice
per namespace, once unweighted and once weighted, so a full run hands
the sink
six records. Untagged, a caller cannot tell the weighted BPO arrays from
the
unweighted CCO ones, and the **weighted** variant is the one carrying
`f_micro_w`. An untagged record cannot serve the purpose the sink exists
for.

## Unchanged

Every default is still `None`. With no sink passed, no signature, no
return type
and no number this library produces is different, and the existing tests
still
assert exactly that.

## Tests

Three added: that both public entry points expose the parameter, that a
record
reports its namespace and variant, and that the context defaults to
`None` rather
than to a guess.

`pytest tests -q`: 25 passed, 6 xfailed. `ruff` and `mypy` clean.
frapercan added a commit that referenced this pull request Aug 19, 2026
Third and final piece of the per-protein sink. #23 emitted the arrays,
#24 made
them reachable from `cafa_eval`, and this makes them **usable**.

## The gap

The arrays were anonymous, indexed by an opaque row number. Every axis
the sink
exists to support is keyed by protein:

- sequence length
- identity to the nearest donor
- taxonomic relation to the nearest donor

None can be joined to a row number, so the feature as shipped could not
do the
one thing it was built for. I found this while writing the consumer, at
the point
of asking which protein row 0 was.

## Two numberings, which is why this is not one line

The NK/LK kernel receives every row, so array row `i` is matrix row `i`.
The PK
kernel can be handed a subset:

```python
pred_sub = pred[proteins_has_gt, :]
```

after which the array numbering and the `ids` numbering disagree.
`compute_metrics`
is what performs that subsetting, so it is what records the mapping,
rather than
leaving each caller to reconstruct it from state it does not have.

Getting this wrong would attribute one protein's score to another. That
is
exactly the failure the consuming project spent a week correcting in its
neighbour search, where a positional misalignment produced plausible
numbers for
a fortnight. So there is a test asserting `row_index` has one entry per
array
row, rather than a comment saying it should.

## Also

Identity is `None` when not supplied rather than guessed, and the id map
is
copied rather than aliased, with tests for both.

`pytest tests -q`: 29 passed, 6 xfailed. `ruff` and `mypy` clean.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant