File size: 5,305 Bytes
32f5a65 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | from __future__ import annotations
import pandas as pd
import pytest
from sepsis_mcp.tabular_summary import build_summary_tables, main
def test_build_summary_tables_merges_run_level_and_subgroup_metrics(tmp_path) -> None:
sweep_dir = tmp_path / "sweep"
runs_dir = sweep_dir / "runs" / "cal64__xgboost__alpha0p1__none"
runs_dir.mkdir(parents=True)
pd.DataFrame(
[
{
"run_id": "cal64__xgboost__alpha0p1__none",
"experiment": "A_to_B",
"method": "standard",
"model_type": "xgboost",
"calibration_patients": 64,
"alpha": 0.1,
"mask_strategy": "none",
"auroc": 0.76,
"auprc": 0.05,
"empirical_coverage": 0.92,
"average_set_size": 0.94,
"abstention_rate": 0.0,
"max_group_coverage_gap": 0.04,
"target_coverage": 0.9,
},
{
"run_id": "cal64__xgboost__alpha0p1__none",
"experiment": "A_to_B",
"method": "missingness_aware",
"model_type": "xgboost",
"calibration_patients": 64,
"alpha": 0.1,
"mask_strategy": "none",
"auroc": 0.76,
"auprc": 0.05,
"empirical_coverage": 0.89,
"average_set_size": 0.97,
"abstention_rate": 0.0,
"max_group_coverage_gap": 0.06,
"target_coverage": 0.9,
},
]
).to_csv(sweep_dir / "sweep_results.csv", index=False)
pd.DataFrame(
[
{
"experiment": "A_to_B",
"method": "standard",
"group": 0,
"group_label": "low",
"count": 20,
"coverage": 0.95,
},
{
"experiment": "A_to_B",
"method": "standard",
"group": 2,
"group_label": "high",
"count": 12,
"coverage": 0.87,
},
{
"experiment": "A_to_B",
"method": "missingness_aware",
"group": 0,
"group_label": "low",
"count": 20,
"coverage": 0.91,
},
{
"experiment": "A_to_B",
"method": "missingness_aware",
"group": 2,
"group_label": "high",
"count": 12,
"coverage": 0.9,
},
]
).to_csv(runs_dir / "subgroup_coverage.csv", index=False)
overall_summary, subgroup_summary = build_summary_tables(sweep_dir)
assert set(overall_summary["method"]) == {"standard", "missingness_aware"}
assert {"coverage_gap", "absolute_coverage_gap"} <= set(overall_summary.columns)
assert overall_summary.loc[overall_summary["method"] == "standard", "coverage_gap"].item() == pytest.approx(
0.02
)
assert set(subgroup_summary["group_label"]) == {"low", "high"}
assert {"model_type", "calibration_patients", "auroc", "coverage_gap", "is_high_missing_group"} <= set(
subgroup_summary.columns
)
high_row = subgroup_summary[
(subgroup_summary["method"] == "standard") & (subgroup_summary["group_label"] == "high")
].iloc[0]
assert high_row["coverage_gap"] == pytest.approx(-0.03)
assert bool(high_row["is_high_missing_group"]) is True
def test_tabular_summary_main_writes_overall_and_subgroup_csvs(tmp_path) -> None:
sweep_dir = tmp_path / "sweep"
runs_dir = sweep_dir / "runs" / "cal64__xgboost__alpha0p1__none"
runs_dir.mkdir(parents=True)
pd.DataFrame(
[
{
"run_id": "cal64__xgboost__alpha0p1__none",
"experiment": "A_to_A",
"method": "standard",
"model_type": "xgboost",
"calibration_patients": 64,
"alpha": 0.1,
"mask_strategy": "none",
"auroc": 0.77,
"auprc": 0.06,
"empirical_coverage": 0.91,
"average_set_size": 0.95,
"abstention_rate": 0.0,
"max_group_coverage_gap": 0.03,
"target_coverage": 0.9,
}
]
).to_csv(sweep_dir / "sweep_results.csv", index=False)
pd.DataFrame(
[
{
"experiment": "A_to_A",
"method": "standard",
"group": 1,
"group_label": "medium",
"count": 10,
"coverage": 0.92,
}
]
).to_csv(runs_dir / "subgroup_coverage.csv", index=False)
output_dir = tmp_path / "summary"
main(
[
"--sweep-dir",
str(sweep_dir),
"--output-dir",
str(output_dir),
]
)
overall_path = output_dir / "overall_summary.csv"
subgroup_path = output_dir / "subgroup_summary.csv"
assert overall_path.exists()
assert subgroup_path.exists()
assert pd.read_csv(overall_path).shape[0] == 1
assert pd.read_csv(subgroup_path).shape[0] == 1
|