Publicar Weibull y puntos del modelo en las curvas por combinación
This commit is contained in:
+21
-13
@@ -435,13 +435,14 @@ def _scenario_curves(months: np.ndarray, event: np.ndarray, cap_months: int) ->
|
|||||||
} # fmt: skip
|
} # fmt: skip
|
||||||
|
|
||||||
|
|
||||||
def _combination_curves(spec: Spec, members: pd.DataFrame) -> list[dict]:
|
def _combination_curves(spec: Spec, model: IntervalSurvival, members: pd.DataFrame) -> list[dict]:
|
||||||
"""Kaplan-Meier of the members who share every covariate value: the one curve a patient with
|
"""The curves of the members who share every covariate value, the ones a patient with those
|
||||||
those values is shown. Fewer than MIN_SCENARIO_EVENTS deaths: counts and why, no curve."""
|
values is shown: Kaplan-Meier, Weibull, fractional Weibull and the model's points for those
|
||||||
from models import curves
|
values (its bootstrap band where training saw them). Fewer than MIN_SCENARIO_EVENTS deaths:
|
||||||
|
counts and why, no curve."""
|
||||||
months, event = members["months"].to_numpy(), members["event"].to_numpy(bool)
|
months, event = members["months"].to_numpy(), members["event"].to_numpy(bool)
|
||||||
keys = spec._combinations(members)
|
keys = spec._combinations(members)
|
||||||
|
intervals = getattr(model, "intervals_", {})
|
||||||
found = []
|
found = []
|
||||||
for values, rows in keys.groupby(list(spec.features), sort=True).indices.items():
|
for values, rows in keys.groupby(list(spec.features), sort=True).indices.items():
|
||||||
values = values if isinstance(values, tuple) else (values,)
|
values = values if isinstance(values, tuple) else (values,)
|
||||||
@@ -450,13 +451,20 @@ def _combination_curves(spec: Spec, members: pd.DataFrame) -> list[dict]:
|
|||||||
if entry["fallecidos"] < MIN_SCENARIO_EVENTS:
|
if entry["fallecidos"] < MIN_SCENARIO_EVENTS:
|
||||||
entry["reason"] = "insufficient_deaths"
|
entry["reason"] = "insufficient_deaths"
|
||||||
else:
|
else:
|
||||||
until = int(months[rows].max())
|
entry |= _scenario_curves(months[rows], event[rows], spec.cap_months)
|
||||||
km = curves.kaplan_meier(months[rows], event[rows], until)
|
point = {"S": model.survival(members.iloc[rows[:1]])[0].tolist()}
|
||||||
entry["km"] = {
|
band = intervals.get(values)
|
||||||
"months": list(range(until + 1)),
|
if band: # kept as mortality: survival's limits are its complements, swapped
|
||||||
**{n: v.tolist() for n, v in zip(("S", "low", "high"), km, strict=True)},
|
point |= {"low": [1 - v for v in band[1]], "high": [1 - v for v in band[0]]}
|
||||||
"interval": {"available": True, "level": 0.95, "method": "greenwood_log_log"},
|
point["interval"] = {"available": bool(band),
|
||||||
}
|
"level": spec.interval_level if band else None,
|
||||||
|
"method": "bootstrap_percentile" if band else None} # fmt: skip
|
||||||
|
entry["points"] = {"COMB": point}
|
||||||
|
entry["horizons_months"] = list(spec.borders[1:])
|
||||||
|
entry["km"]["interval"] = {
|
||||||
|
"available": True, "level": 0.95, "method": "greenwood_log_log"}
|
||||||
|
for name in ("weibull", "weibull_fractional"):
|
||||||
|
entry[name]["interval"] = {"available": False, "level": None, "method": None}
|
||||||
found.append(entry)
|
found.append(entry)
|
||||||
return found
|
return found
|
||||||
|
|
||||||
@@ -558,7 +566,7 @@ def interpretability(spec: Spec, model: IntervalSurvival, data: dict) -> dict:
|
|||||||
"available": True, "level": 0.95, "method": "bootstrap_percentile"}
|
"available": True, "level": 0.95, "method": "bootstrap_percentile"}
|
||||||
for scenario in report["escenarios_sin_curvas"]:
|
for scenario in report["escenarios_sin_curvas"]:
|
||||||
scenario["reason"] = "insufficient_deaths"
|
scenario["reason"] = "insufficient_deaths"
|
||||||
report["km_combinaciones"] = _combination_curves(spec, members)
|
report["km_combinaciones"] = _combination_curves(spec, model, members)
|
||||||
return report
|
return report
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -130,6 +130,10 @@ def test_survival_matches_the_current_model_and_publishes_patient_scenarios(monk
|
|||||||
for c in combined:
|
for c in combined:
|
||||||
assert set(c["valores"]) == set(spec.features)
|
assert set(c["valores"]) == set(spec.features)
|
||||||
assert ("km" in c) == (c["fallecidos"] >= survival.MIN_SCENARIO_EVENTS)
|
assert ("km" in c) == (c["fallecidos"] >= survival.MIN_SCENARIO_EVENTS)
|
||||||
|
if "km" in c:
|
||||||
|
assert {"weibull", "weibull_fractional", "points"} <= set(c)
|
||||||
|
point = c["points"]["COMB"]
|
||||||
|
assert len(point["S"]) == len(c["horizons_months"]) == len(spec.borders) - 1
|
||||||
(scenario,) = report["escenarios"]
|
(scenario,) = report["escenarios"]
|
||||||
name = scenario["escenario"]
|
name = scenario["escenario"]
|
||||||
assert name == "Citología · HSIL+"
|
assert name == "Citología · HSIL+"
|
||||||
|
|||||||
Reference in New Issue
Block a user