bio-agent-trajectories / code /vera /competence.py
ryan-superman's picture
Publish trajectories and metrics (BMB content gated)
df22bef verified
Raw History Blame Contribute Delete
6.71 kB
"""One vocabulary for what a tool can and cannot establish.
Every tool failure measured in this project has the same shape: a **confident, well-formed
answer from outside the tool's competence**, indistinguishable by the caller from a good
one.
* CellTypist, given a mouse lung sample and a human immune-only reference, called 91% of
cells "Double-positive thymocytes". Concentrated, decisive, wrong.
* BLAST, given 40 reads from an influenza sample, returned *Homo sapiens*. The host
dominates and the virus is invisible at that depth; the answer was about the wrong
organism.
* Cell2Sentence, given genes averaged over a heterogeneous sample, named a pseudo-cell
belonging to no population.
None raised. Each was fixed separately, in its own vocabulary -- `abstained`,
`reference_is_conclusive`, `can_see_minority` -- which is three spellings of one idea and
leaves the next tool free to invent a fourth. This module makes the idea explicit and
shared, so a new tool must state its competence to be usable at all.
The distinction that matters is between **negative** and **uninformative**. "No influenza
found in 40 host-dominated reads" is not evidence of absence, and a caller that treats it
as such reaches a confident wrong conclusion faster than one with no tool at all.
"""
from __future__ import annotations
from dataclasses import dataclass, field
@dataclass(frozen=True)
class Competence:
"""What a tool result establishes, and what it cannot.
Attributes:
applicable: Whether the tool was in-domain for this input at all. False means the
result carries no information about the question, however confident it looks.
confidence: Strength of the finding in [0, 1] when applicable, else 0.
margin: Separation from the runner-up interpretation, where one exists. A high
confidence with a negligible margin is the out-of-domain signature: the tool
had to choose between options that all fit badly.
ties: Candidates the evidence cannot order below the leader, at the same relative
threshold `margin` uses. Strictly stronger than `margin`, which only looks at
the immediate runner-up. Reported alongside `alternatives` because the two
criteria fail in opposite directions and neither dominates: measured at the
real Cas9 geometry, `alternatives` abstains on 200/200 answers that were all
correct, while at the invented one-distractor geometry `ties` commits to 240
answers that were all wrong. Which to gate on is a policy choice with a
measured cost on each side, and resolving it needs information from outside the
tool's ranking axis.
alternatives: Candidates besides the top one that also cleared the tool's evidence
floor. Measured on 2,400 simulated libraries: margin is large both when the
true answer dominates and when a distractor dominates, so the gap between the
top two is not the same as the absence of a rival. A ranking tool that leaves
this at 0 is asserting its answer was unique, not merely ahead.
blind_spots: What this result cannot rule out, in the caller's terms. Empty only
when the tool genuinely could have seen anything relevant.
remedy: What would let the question be answered, when the tool cannot.
"""
applicable: bool
confidence: float = 0.0
margin: float = 0.0
alternatives: int = 0
ties: int = 0
blind_spots: tuple[str, ...] = ()
remedy: str = ""
@property
def conclusive(self) -> bool:
"""Whether a caller may act on this result as an answer.
Requires four things: the tool was in-domain, it is confident, it separated its
answer from the runner-up, and no rival cleared the evidence floor. Confidence
alone is what made every failure look like a success -- CellTypist reported 0.885
while choosing between two brain references for a lung sample, on a margin of
0.013. Uniqueness is the condition added after measurement: with exactly one
saturated distractor present, the margin-only rule accepted 40% of libraries and
was right on none of them, because the distractor cleanly outranked the truth.
Requiring uniqueness cut confidently wrong answers from 240 to 28 and raised
accuracy on what is accepted from 0.583 to 0.923.
"""
return (self.applicable and self.confidence >= 0.5 and self.margin >= 0.05
and self.alternatives == 0)
@property
def informative(self) -> bool:
"""Whether the result says anything at all, even if not conclusive.
A non-applicable result is not a negative result. Distinguishing the two is the
point of this class: absence of evidence from a tool that could not have seen the
thing is not evidence of absence.
"""
return self.applicable and not self.blind_spots
def explain(self) -> str:
"""One line a caller can act on, or show to a model."""
if not self.applicable:
return f"NOT APPLICABLE: {self.remedy or 'this tool cannot address the input'}"
if self.conclusive:
return f"conclusive (confidence {self.confidence:.2f}, margin {self.margin:.2f})"
parts = [f"INCONCLUSIVE (confidence {self.confidence:.2f}, margin {self.margin:.2f})"]
if self.alternatives:
parts.append(f"{self.alternatives} other candidate(s) cleared the evidence "
f"floor; this is a candidate SET, and the top of the ranking is "
f"not the answer")
if self.blind_spots:
parts.append("cannot rule out: " + "; ".join(self.blind_spots))
if self.remedy:
parts.append(f"to resolve: {self.remedy}")
return " -- ".join(parts)
def not_applicable(remedy: str = "") -> Competence:
"""A result carrying no information about the question."""
return Competence(applicable=False, remedy=remedy)
def bounded(confidence: float, margin: float, blind_spots: tuple[str, ...] = (),
remedy: str = "", alternatives: int = 0, ties: int = 0) -> Competence:
"""An in-domain result that may still be unable to rule things out.
`alternatives` defaults to 0, which asserts the answer was unique. A ranking tool must
pass the real count; leaving it silent is a claim, not an abstention.
"""
return Competence(applicable=True, confidence=confidence, margin=margin,
blind_spots=blind_spots, remedy=remedy, alternatives=alternatives,
ties=ties)