Survival scores name the report's clinical scenarios each patient falls in
This commit is contained in:
+9
-2
@@ -220,7 +220,9 @@ class IntervalSurvival(BaseEstimator):
|
||||
# ---------------------------------------------------------------- a model over gold timelines
|
||||
|
||||
TRAIN_FRACTION = 0.7
|
||||
OUTPUT = ["person_id", "index_date", "horizon_years", "probability", "ci_low", "ci_high"]
|
||||
OUTPUT = ["person_id", "index_date", "horizon_years", "probability", "ci_low", "ci_high",
|
||||
"scenarios"] # fmt: skip
|
||||
SCENARIO_SEPARATOR = " | " # between the names of a member's clinical scenarios
|
||||
|
||||
|
||||
def split(members: pd.DataFrame, train_fraction: float) -> tuple[pd.DataFrame, pd.DataFrame]:
|
||||
@@ -324,13 +326,17 @@ class Spec:
|
||||
return model, metrics, {"train": train, "test": test}
|
||||
|
||||
def predict(self, model: IntervalSurvival, events: pd.DataFrame) -> pd.DataFrame:
|
||||
"""One row per member and horizon: her mortality by it, and its interval when kept."""
|
||||
"""One row per member and horizon: her mortality by it, its interval when kept, and
|
||||
the clinical scenarios of the report she falls in (the curves her record draws)."""
|
||||
members = self.members(events)
|
||||
mortality = 1.0 - model.survival(members)
|
||||
n, h = len(members), len(self.horizons)
|
||||
empty = ([np.nan] * h, [np.nan] * h)
|
||||
ends = [model.intervals_.get(tuple(c), empty)
|
||||
for c in self._combinations(members).itertuples(index=False)] # fmt: skip
|
||||
masks = scenario_masks(members, self.scenarios)
|
||||
scenarios = [SCENARIO_SEPARATOR.join(name for name, m in masks.items() if m[i]) or None
|
||||
for i in range(n)] # fmt: skip
|
||||
return pd.DataFrame({
|
||||
"person_id": np.repeat(members["person_id"].to_numpy(), h),
|
||||
"index_date": np.repeat(members["index_date"].dt.date.to_numpy(), h),
|
||||
@@ -338,6 +344,7 @@ class Spec:
|
||||
"probability": mortality.ravel(),
|
||||
"ci_low": np.array([e[0] for e in ends], dtype=float).ravel(),
|
||||
"ci_high": np.array([e[1] for e in ends], dtype=float).ravel(),
|
||||
"scenarios": np.repeat(np.asarray(scenarios, dtype=object), h),
|
||||
})[OUTPUT] # fmt: skip
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user