| from __future__ import annotations |
|
|
| import json |
| from pathlib import Path |
|
|
| import pandas as pd |
|
|
|
|
| def test_run_multigroup_ablation_analysis_writes_expected_outputs(tmp_path: Path) -> None: |
| from sepsis_mcp.appendix_multigroup_ablation import ( |
| build_multigroup_summary, |
| write_multigroup_outputs, |
| ) |
|
|
| overall = pd.DataFrame( |
| [ |
| { |
| "run_id": "seed0-bin", |
| "grouping_variant": "binary_selected", |
| "group_count": 2, |
| "method": "missingness_aware", |
| "empirical_coverage": 0.90, |
| "max_group_coverage_gap": 0.02, |
| "average_set_size": 0.95, |
| }, |
| { |
| "run_id": "seed0-k4", |
| "grouping_variant": "mask_cluster_k4", |
| "group_count": 4, |
| "method": "missingness_aware", |
| "empirical_coverage": 0.89, |
| "max_group_coverage_gap": 0.04, |
| "average_set_size": 0.96, |
| }, |
| ] |
| ) |
| subgroup = pd.DataFrame( |
| [ |
| {"run_id": "seed0-bin", "grouping_variant": "binary_selected", "method": "missingness_aware", "group": 0, "count": 120}, |
| {"run_id": "seed0-bin", "grouping_variant": "binary_selected", "method": "missingness_aware", "group": 1, "count": 130}, |
| {"run_id": "seed0-k4", "grouping_variant": "mask_cluster_k4", "method": "missingness_aware", "group": 0, "count": 40}, |
| {"run_id": "seed0-k4", "grouping_variant": "mask_cluster_k4", "method": "missingness_aware", "group": 1, "count": 50}, |
| {"run_id": "seed0-k4", "grouping_variant": "mask_cluster_k4", "method": "missingness_aware", "group": 2, "count": 55}, |
| {"run_id": "seed0-k4", "grouping_variant": "mask_cluster_k4", "method": "missingness_aware", "group": 3, "count": 60}, |
| ] |
| ) |
|
|
| summary = build_multigroup_summary(overall_summary=overall, subgroup_summary=subgroup) |
| paths = write_multigroup_outputs(summary=summary, output_dir=tmp_path / "multigroup") |
| manifest = json.loads(paths["manifest"].read_text(encoding="utf-8")) |
| written = pd.read_csv(paths["summary"]) |
|
|
| assert "smallest_group_size_mean" in written.columns |
| assert written.loc[written["grouping_variant"] == "binary_selected", "smallest_group_size_mean"].iloc[0] == 120 |
| assert Path(manifest["summary"]).exists() |
|
|
|
|
| def test_build_negative_case_summary_flags_neutral_or_worse_mcar_rows() -> None: |
| from sepsis_mcp.appendix_negative_case_analysis import build_negative_case_summary |
|
|
| combined = pd.DataFrame( |
| [ |
| {"delta_m": 0.0, "mechanism": "mcar", "method": "standard", "max_group_coverage_gap_mean": 0.010, "empirical_coverage_mean": 0.902, "average_set_size_mean": 1.01}, |
| {"delta_m": 0.0, "mechanism": "mcar", "method": "mondrian_tilted", "max_group_coverage_gap_mean": 0.012, "empirical_coverage_mean": 0.901, "average_set_size_mean": 1.02}, |
| {"delta_m": 0.1, "mechanism": "mcar", "method": "standard", "max_group_coverage_gap_mean": 0.020, "empirical_coverage_mean": 0.903, "average_set_size_mean": 1.03}, |
| {"delta_m": 0.1, "mechanism": "mcar", "method": "mondrian_tilted", "max_group_coverage_gap_mean": 0.019, "empirical_coverage_mean": 0.902, "average_set_size_mean": 1.04}, |
| ] |
| ) |
|
|
| summary = build_negative_case_summary(combined) |
|
|
| assert set(summary["mechanism"]) == {"mcar"} |
| assert "gap_advantage_vs_standard" in summary.columns |
| assert bool(summary.loc[summary["delta_m"] == 0.0, "is_neutral_or_worse"].iloc[0]) is True |
|
|
|
|
| def test_summarize_runtime_records_computes_relative_overhead() -> None: |
| from sepsis_mcp.appendix_runtime_analysis import summarize_runtime_records |
|
|
| records = pd.DataFrame( |
| [ |
| {"method": "standard", "stage": "calibration", "seconds": 0.50}, |
| {"method": "standard", "stage": "test", "seconds": 0.10}, |
| {"method": "missingness_aware", "stage": "calibration", "seconds": 0.55}, |
| {"method": "missingness_aware", "stage": "test", "seconds": 0.12}, |
| {"method": "cp_mda_exact", "stage": "calibration", "seconds": 1.50}, |
| {"method": "cp_mda_exact", "stage": "test", "seconds": 0.30}, |
| ] |
| ) |
|
|
| summary = summarize_runtime_records(records) |
|
|
| assert "relative_to_standard" in summary.columns |
| assert summary.loc[(summary["method"] == "missingness_aware") & (summary["stage"] == "calibration"), "relative_to_standard"].iloc[0] == 1.10 |
| assert summary.loc[(summary["method"] == "cp_mda_exact") & (summary["stage"] == "test"), "relative_to_standard"].iloc[0] == 3.00 |
|
|
|
|
| def test_write_implementation_details_emits_expected_sections(tmp_path: Path) -> None: |
| from sepsis_mcp.appendix_implementation_details import write_implementation_details |
|
|
| output_path = tmp_path / "IMPLEMENTATION_DETAILS.md" |
| manifest_path = tmp_path / "manifest.json" |
| write_implementation_details(output_path=output_path, manifest_path=manifest_path) |
|
|
| text = output_path.read_text(encoding="utf-8") |
| manifest = json.loads(manifest_path.read_text(encoding="utf-8")) |
|
|
| assert "Implementation Details" in text |
| assert "GOSSIS Classification Defaults" in text |
| assert "MIMIC-IV Validation Defaults" in text |
| assert "Conformity Scores" in text |
| assert Path(manifest["implementation_details"]).exists() |
|
|