CaMa survival: the members' stage, surgery and adjuvant treatments, read by the clinical scenarios and the development stages

This commit is contained in:
Roberto Treviño Cervantes
2026-11-22 11:36:52 -06:00
parent 94c7cf93dc
commit d91ac7c14b
4 changed files with 54 additions and 10 deletions
+29 -2
View File
@@ -300,10 +300,11 @@ def development_record(cohort_frame):
def test_a_stage_that_cannot_run_on_the_cohort_is_recorded_and_does_not_stop_the_others(development_record):
"""Pins down: on a cohort whose covariates barely vary (the 240 members here) the SEER stages that need variation
publish their error in the record and the run still produces it, with every stage listed. Fails if one stage's
exception escapes the node, which would stop a version from training."""
exception escapes the node, which would stop a version from training. The stage and the treatments come from the
members' timeline, so none of them is listed as unavailable; fails if the record stops reading them."""
assert [e["etapa"] for e in development_record["etapas"]] == ["01", "02", "03", "04", "05", "06"]
assert set(development_record["errores"]) <= {"01", "02", "03", "04", "05", "06"}
assert "Estadio_cat" in development_record["variables_no_disponibles"]
assert development_record["variables_no_disponibles"] == []
def test_the_development_stages_publish_their_tables_and_figures_on_a_varied_cohort():
@@ -513,3 +514,29 @@ def test_a_saved_model_whose_class_is_older_still_scores_with_the_current_code(m
for name, expected in production_model.component_survival(members_frame[FEATURES]).items():
assert again[name] == pytest.approx(expected)
def test_the_members_carry_their_stage_and_treatments_and_the_scenarios_read_them():
"""Pins down: a member's stage is the one reported with her diagnosis and her surgery, radiotherapy and chemotherapy are
those of the year after it, and the clinical scenarios that need a stage or a conservative surgery find her. Fails if
`member_table` stops reading them or `live.scenario_masks` goes back to age and subtype only."""
from models.cama.survival import estimator, live
rows = []
for pac, (stage, surgery, her2) in enumerate([("EI", "SEGMENTECTOMIA", "H0"), ("EIV", "MASTECTOMIA_TOTAL", "H3")], start=1):
rows += [
{"pac_id": pac, "tipo": "nacimiento", "fecha": "1950-01-01", "valor_codigo": None, "valor_numerico": None},
{"pac_id": pac, "tipo": "biopsia", "fecha": "2025-01-10", "valor_codigo": "CARCINOMA_INVASOR", "valor_numerico": None},
{"pac_id": pac, "tipo": "estadio_clinico", "fecha": "2025-01-10", "valor_codigo": stage, "valor_numerico": None},
{"pac_id": pac, "tipo": "her2", "fecha": "2025-01-10", "valor_codigo": her2, "valor_numerico": None},
{"pac_id": pac, "tipo": "receptor_estrogeno", "fecha": "2025-01-10", "valor_codigo": "NEGATIVO", "valor_numerico": None},
{"pac_id": pac, "tipo": "receptor_progesterona", "fecha": "2025-01-10", "valor_codigo": "NEGATIVO", "valor_numerico": None},
{"pac_id": pac, "tipo": "cirugia", "fecha": "2025-03-01", "valor_codigo": surgery, "valor_numerico": None},
]
rows.append({"pac_id": 1, "tipo": "radioterapia", "fecha": "2025-05-01", "valor_codigo": "ADYUVANTE", "valor_numerico": None})
cohort = pd.DataFrame(rows).assign(fecha_indice="2025-01-10", fuente_id="x")
members = estimator.member_table(cohort, [45, 60, 75], [14, 20], 30)
assert members.loc[1, ["stage", "surgery", "radiotherapy", "chemotherapy"]].tolist() == ["I", "Cirugía conservadora", "Sí", "No"]
assert members.loc[2, ["stage", "surgery", "radiotherapy"]].tolist() == ["IV", "Mastectomía", "No"]
masks = live.scenario_masks(members.reset_index())
assert masks["Estadio I"].tolist() == [True, False] and masks["Estadio IV"].tolist() == [False, True]