Shared interval survival for CaMa and CaCu, one jobs module for every domain, Dagster train and score by domain

This commit is contained in:
Roberto Treviño
2026-11-25 23:20:52 -06:00
parent d3d9cc3950
commit e2f7e95e83
12 changed files with 661 additions and 65 deletions
View File
+8
View File
@@ -0,0 +1,8 @@
"""CaMa's training and scoring jobs (models.jobs) over its models and their reports."""
from models import survival
from models.cama.specs import MODELS
from models.jobs import Jobs
JOBS = Jobs("cama", MODELS, survival.technical_validation, survival.interpretability)
train, score = JOBS.train, JOBS.score
+41
View File
@@ -0,0 +1,41 @@
"""The CaMa models. Survival: mortality at 6, 12 and 18 months from the first invasive biopsy
report, by interval survival (models.survival) over the age at the index and that report's
results. Training and scoring read the timelines (gold `cama_events`) of the members of
`gold.cama_cohort_survival`."""
import pandas as pd
from models.survival import Spec, age_group, band
# The index report's results (timeline types), read from a week before to RESULT_DAYS after.
RESULTS = ("histologic_type", "grade", "er", "pr", "her2")
RESULT_DAYS = 30
def covariates(events: pd.DataFrame, members: pd.DataFrame) -> pd.DataFrame:
"""Each member's age group at the index and her index report's results, in bands where
they are numbers (Ki-67 %, tumour size in mm)."""
found = pd.DataFrame(index=members.index)
found["age_group"] = age_group(events, members, (45, 60, 75))
days = (events["date"] - events["index_date"]).dt.days
near = events[days.between(-7, RESULT_DAYS)].sort_values(["date", "source_id"])
for kind in RESULTS:
found[kind] = near[near["type"] == kind].groupby("person_id")["value_code"].first()
for name, kind, cuts in (("ki67_group", "ki67_pct", (14, 20)),
("size_group", "size_mm", (20, 50))): # fmt: skip
value = near[near["type"] == kind].groupby("person_id")["value_num"].first()
found[name] = band(value.reindex(found.index), list(cuts))
return found
MODELS = {
"survival": Spec(
name="survival",
cohort="cama_cohort_survival",
features=("age_group", *RESULTS, "ki67_group", "size_group"),
covariates=covariates,
parents=("histologic_type", "age_group"),
borders=(0, 6, 12, 18),
cap_months=24,
),
}