From 797f679141506121b0efc53542bca7f268699f68 Mon Sep 17 00:00:00 2001 From: kert Date: Sat, 31 Oct 2026 20:32:04 -0400 Subject: [PATCH] feat(prisma): screening calibration gate --- src/cli/prisma.py | 8 ++++++ src/prisma/calibrate.py | 50 ++++++++++++++++++++++++++++++++++ tests/prisma/test_calibrate.py | 43 +++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 src/prisma/calibrate.py create mode 100644 tests/prisma/test_calibrate.py diff --git a/src/cli/prisma.py b/src/cli/prisma.py index 84e72d9..20f7b0b 100644 --- a/src/cli/prisma.py +++ b/src/cli/prisma.py @@ -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 ─────────────────────────────────────── diff --git a/src/prisma/calibrate.py b/src/prisma/calibrate.py new file mode 100644 index 0000000..6dba936 --- /dev/null +++ b/src/prisma/calibrate.py @@ -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") + ) diff --git a/tests/prisma/test_calibrate.py b/tests/prisma/test_calibrate.py new file mode 100644 index 0000000..fe428aa --- /dev/null +++ b/tests/prisma/test_calibrate.py @@ -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