37 lines
1.7 KiB
Python
37 lines
1.7 KiB
Python
"""A hospital patient in the recurrence analysis' terms: her covariates as the external cohorts name them, and the scenarios
|
|
(subgroups defined by covariates) she belongs to. What the hospital does not report (nodes, subtype, surgery...) is not given, so
|
|
a scenario that needs it does not include her."""
|
|
|
|
import numpy as np
|
|
import pandas as pd
|
|
|
|
from models.cama.recurrence import prep
|
|
|
|
POSITIVE = {"POSITIVO": "Positive", "NEGATIVO": "Negative"}
|
|
HER2 = {"H3": "Positive", "H0": "Negative", "H1": "Negative"} # 2+ is equivocal: not given
|
|
|
|
|
|
def _bin(value, cuts: list, labels: list) -> str | None:
|
|
if value is None or pd.isna(value):
|
|
return None
|
|
return str(pd.cut([float(value)], cuts, labels=labels, right=False)[0])
|
|
|
|
|
|
def covariates_of(row: pd.Series) -> dict[str, str]:
|
|
"""The covariates of one labelled member (`models.cama.survival.live.members`) that the cohorts also have."""
|
|
found = {
|
|
"Edad": _bin(row.get("age_years"), prep.AGE_CUTS, prep.AGE_LABELS),
|
|
"Tamano_tumor": _bin(row.get("size_mm"), prep.SIZE_CUTS, prep.SIZE_LABELS),
|
|
"Grado_histologico": {"G1": "1", "G2": "2", "G3": "3"}.get(row.get("grade")),
|
|
"Receptor_estrogeno": POSITIVE.get(row.get("er")),
|
|
"Receptor_progesterona": POSITIVE.get(row.get("pr")),
|
|
"HER2": HER2.get(row.get("her2")),
|
|
}
|
|
return {k: v for k, v in found.items() if v is not None}
|
|
|
|
|
|
def matching(scenarios: list[dict], covariates: dict[str, str]) -> list[dict]:
|
|
"""The scenarios whose every condition her covariates meet, the larger first within each cohort."""
|
|
met = [s for s in scenarios if all(var in covariates and covariates[var] in values for var, values in s["condiciones"].items())]
|
|
return sorted(met, key=lambda s: (s["cohorte"], -s["n"]))
|