Quick start¶
This example monitors the sensitivity of a classifier in three hospital sites, inspects the results after every daily batch and writes an Article 72 report.
1. Data¶
Each case needs the reference label (y_true), the model prediction
(y_pred) and, optionally, a subgroup label. Here a simulated stream is
used in which site C has a sensitivity of 0.82, below the declared 0.90.
import anywatch as aw
from anywatch.datasets import make_classification_stream
stream = make_classification_stream(
6000,
groups=["site_a", "site_b", "site_c"],
prevalence=0.3,
sensitivity={"site_a": 0.95, "site_b": 0.94, "site_c": 0.82},
seed=1,
)
2. Monitor¶
monitor = aw.SubgroupMonitor(
metric="sensitivity",
target=0.90,
groups=["site_a", "site_b", "site_c"],
alpha=0.05,
)
for day, start in enumerate(range(0, len(stream), 500), start=1):
batch = slice(start, start + 500)
for alarm in monitor.update(stream.y_true[batch], stream.y_pred[batch], stream.group[batch]):
print(f"day {day}: {alarm.message}")
With alpha=0.05 and the default Bonferroni correction, the probability
that any site raises a false alarm on any day is at most 5 %, no matter
how many days you monitor.
3. Inspect¶
for row in monitor.status():
print(row.group, round(row.estimate, 3), (round(row.ci_lower, 3), round(row.ci_upper, 3)))
The intervals are confidence sequences: they cover the true sensitivity at all times simultaneously with probability at least 95 %.
4. Report¶
report = aw.build_art72_report(
system=aw.SystemInfo(
name="Chest X-ray triage",
version="2.1.0",
provider="Example Health Ltd",
intended_purpose="Prioritise suspected pneumothorax for radiologist review",
),
monitors=[monitor],
plan=aw.PostMarketMonitoringPlan(
data_sources=["Radiologist confirmed reads"],
collection_frequency="Daily",
responsible_person="Head of Clinical Safety",
),
)
report.save("art72_report.html")
5. Command line¶
The same analysis is available without writing code:
anywatch report cases.csv --metric sensitivity --target 0.9 --group-col site \
--framework art72 --format markdown --output report.md