"""Cross-check the two synthetic examples with scikit-survival 0.28.0. Place beside competing_risks_example.py; use the matching reference requirements. Run: python verify_competing_risks.py No network, device access, fitting to real data or random sampling. """ from importlib.metadata import version from hashlib import sha256 import json from pathlib import Path import platform import numpy as np from sksurv.nonparametric import cumulative_incidence_competing_risks, kaplan_meier_estimator from competing_risks_example import worked_example if version("scikit-survival") != "0.28.0": raise RuntimeError("This reference check pins scikit-survival 0.28.0") data = worked_example() reports = [] for name, sample in [("complete-through-day-two", data), ("early-censoring", data["earlyCensoring"])]: for reverse in [False, True]: records = sample["records"][::-1] if reverse else sample["records"] # Explicit local labels, independent of the tutorial's clinical example. event = np.array([{"censor": 0, "failure": 1, "retirement": 2}[r["event"]] for r in records]) time = np.array([r["day"] for r in records], dtype=float) times, cif = cumulative_incidence_competing_risks(event, time) for step in sample["steps"]: index = np.flatnonzero(times == step["day"])[0] np.testing.assert_allclose( [cif[1, index], cif[2, index], 1 - cif[0, index]], [step["failureCif"], step["retirementCif"], step["eventFree"]], rtol=0, atol=1e-14, ) km_times, km = kaplan_meier_estimator(event == 1, time) day_two = np.flatnonzero(times == 2)[0] km_day_two = np.flatnonzero(km_times == 2)[0] np.testing.assert_allclose(1 - km[km_day_two], sample["failureOnlyKmComplement"]["value"], rtol=0, atol=1e-14) reports.append({"example": name, "reverseInput": reverse, "failureCifAtDayTwo": float(cif[1, day_two]), "kmComplementAtDayTwo": float(1 - km[km_day_two]), "passed": True}) root = Path(__file__).resolve().parent print(json.dumps({ "scope": "Two synthetic examples and reversed row order, not a fleet study or general library validation", "python": platform.python_version(), "packages": {name: version(name) for name in ["scikit-survival", "numpy", "scipy", "scikit-learn"]}, "generatorSha256": sha256((root / "competing_risks_example.py").read_bytes()).hexdigest(), "verificationSha256": sha256(Path(__file__).read_bytes()).hexdigest(), "checks": reports, }, indent=2))