78 lines
3.1 KiB
Python
78 lines
3.1 KiB
Python
"""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 `gold.cama_cohort_survival`.
|
|
- Recurrence: risk of recurrence at 12, 36 and 60 months, trained on two public reference
|
|
cohorts (METABRIC and SCAN-B, models.cama.recurrence) read from a configured folder
|
|
(CAMA_REFERENCE_DIR), not on the hospital's patients; slow (its bootstraps take over an
|
|
hour), so it trains only when asked for by name."""
|
|
|
|
import os
|
|
from dataclasses import dataclass, field
|
|
from pathlib import Path
|
|
|
|
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
|
|
|
|
|
|
# The reference cohorts' files, as their publishers distribute them.
|
|
REFERENCE_FILES = {"metabric": "brca_metabric_clinical_data.tsv",
|
|
"scanb": "scanb_clinical_metadata.txt"} # fmt: skip
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ReferenceRecurrence:
|
|
"""The recurrence model over the reference cohorts of `CAMA_REFERENCE_DIR`."""
|
|
|
|
name: str
|
|
cohort: None = None # trains on no gold cohort, and scores no patient
|
|
slow: bool = True
|
|
method: str = field(default="stepwise logistic regression + Bayesian network", init=False)
|
|
|
|
def train(self, events, as_of=None) -> tuple:
|
|
from models.cama import recurrence
|
|
|
|
folder = Path(os.environ.get("CAMA_REFERENCE_DIR", "reference/cama"))
|
|
raws = {s: recurrence.read(s, folder / f) for s, f in REFERENCE_FILES.items()}
|
|
model, metrics, reports = recurrence.train(raws)
|
|
return model, metrics, reports
|
|
|
|
def predict(self, model, events) -> pd.DataFrame:
|
|
return model.predict(events)
|
|
|
|
|
|
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,
|
|
),
|
|
"recurrence": ReferenceRecurrence("recurrence"),
|
|
}
|