feat(prisma): screening calibration gate
This commit is contained in:
@@ -437,6 +437,14 @@ def search_protocols() -> None:
|
||||
typer.echo(f"{name}\t{query}")
|
||||
|
||||
|
||||
@app.command("calibrate")
|
||||
def calibrate_cmd() -> None:
|
||||
"""Calibration gate: sensitivity ≥ 0.95 on 200 title/abstract and 50 full text."""
|
||||
typer.echo("title_abstract\t200")
|
||||
typer.echo("full_text\t50")
|
||||
typer.echo("sensitivity\t0.95")
|
||||
|
||||
|
||||
# ── VPN droplet lifecycle ───────────────────────────────────────
|
||||
|
||||
|
||||
|
||||
50
src/prisma/calibrate.py
Normal file
50
src/prisma/calibrate.py
Normal file
@@ -0,0 +1,50 @@
|
||||
"""Whether a screening prompt is calibrated enough to run on the corpus.
|
||||
|
||||
The positive class is ``include`` and ``uncertain``. An item is caught
|
||||
when the prediction is in that class too, so ``include`` predicted as
|
||||
``uncertain`` still counts.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
_POSITIVE = frozenset({"include", "uncertain"})
|
||||
_SIZES = {"title_abstract": 200, "full_text": 50}
|
||||
_MIN_SENSITIVITY = 0.95
|
||||
|
||||
|
||||
def sensitivity(gold: Sequence[str], pred: Sequence[str]) -> float:
|
||||
"""True positives over gold positives.
|
||||
|
||||
Raises:
|
||||
ValueError: the two sequences differ in length, or gold has no
|
||||
positive.
|
||||
"""
|
||||
if len(gold) != len(pred):
|
||||
raise ValueError("gold and pred lengths differ")
|
||||
positives = [label for label in gold if label in _POSITIVE]
|
||||
if not positives:
|
||||
raise ValueError("no gold positives")
|
||||
caught = sum(
|
||||
1
|
||||
for label, guess in zip(gold, pred, strict=True)
|
||||
if label in _POSITIVE and guess in _POSITIVE
|
||||
)
|
||||
return caught / len(positives)
|
||||
|
||||
|
||||
def sized(n: int, stage: str) -> bool:
|
||||
"""True when ``n`` meets the calibration sample for ``stage``."""
|
||||
if stage not in _SIZES:
|
||||
raise ValueError(stage)
|
||||
return n >= _SIZES[stage]
|
||||
|
||||
|
||||
def cleared(sens: float, n_ta: int, n_ft: int) -> bool:
|
||||
"""True at sensitivity ≥ 0.95 with both sample sizes met."""
|
||||
return (
|
||||
sens >= _MIN_SENSITIVITY
|
||||
and sized(n_ta, "title_abstract")
|
||||
and sized(n_ft, "full_text")
|
||||
)
|
||||
43
tests/prisma/test_calibrate.py
Normal file
43
tests/prisma/test_calibrate.py
Normal file
@@ -0,0 +1,43 @@
|
||||
"""Screening calibration: sensitivity and the two sample-size gates."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from typer.testing import CliRunner
|
||||
|
||||
from cli.prisma import app
|
||||
from prisma.calibrate import cleared, sensitivity, sized
|
||||
|
||||
runner = CliRunner()
|
||||
|
||||
|
||||
def test_sensitivity_counts_include_and_uncertain():
|
||||
assert (
|
||||
sensitivity(
|
||||
["include", "exclude", "uncertain"],
|
||||
["uncertain", "exclude", "exclude"],
|
||||
)
|
||||
== 0.5
|
||||
)
|
||||
|
||||
|
||||
def test_sensitivity_rejects_a_bad_comparison():
|
||||
with pytest.raises(ValueError):
|
||||
sensitivity(["include"], ["include", "exclude"])
|
||||
with pytest.raises(ValueError):
|
||||
sensitivity(["exclude"], ["exclude"])
|
||||
|
||||
|
||||
def test_size_and_clearance_gates():
|
||||
assert sized(200, "title_abstract") is True
|
||||
assert sized(199, "title_abstract") is False
|
||||
assert sized(50, "full_text") is True
|
||||
assert sized(49, "full_text") is False
|
||||
assert cleared(0.95, 200, 50) is True
|
||||
assert cleared(0.949, 200, 50) is False
|
||||
assert cleared(1.0, 199, 50) is False
|
||||
|
||||
|
||||
def test_calibrate_command_is_registered():
|
||||
result = runner.invoke(app, ["calibrate", "--help"])
|
||||
assert result.exit_code == 0
|
||||
Reference in New Issue
Block a user