acc8c371bc
models/cama/seer_survival is the SEER survival pipeline as Hamilton dataflows, with the interval LR and network keeping their per-interval fits and surviving an interval without deaths. models/cama/survival is the scenario: it indexes the total cohort at the first invasive biopsy report (steps/indexing.py), derives the covariates and the outcome from the cohort's events, fits the interval survival, writes the five routines and publishes the population curves (analisis/curvas.json) with the version. An estimate may carry the model's own index date.
66 lines
3.8 KiB
Python
66 lines
3.8 KiB
Python
"""Ejecuta las etapas del pipeline en orden (cada una en su propio proceso).
|
|
|
|
Uso (desde la carpeta que contiene los scripts y los datos):
|
|
python ejecutar_pipeline.py # todas las etapas, saltando las que estan vigentes
|
|
python ejecutar_pipeline.py --solo 05 # solo esa etapa
|
|
python ejecutar_pipeline.py --desde 02 # desde esa etapa en adelante
|
|
python ejecutar_pipeline.py --forzar 02 04 # corre esas aunque esten vigentes
|
|
python ejecutar_pipeline.py --todo # corre todas, sin saltar ninguna
|
|
|
|
Una etapa se salta solo si sus salidas existen y son mas recientes que su script y que sus entradas.
|
|
Escribe bitacora_pipeline.csv (etapa, estado, segundos). Sin mensajes en pantalla salvo errores.
|
|
"""
|
|
import argparse, csv, subprocess, sys, time
|
|
from pathlib import Path
|
|
|
|
BALANCEADO = "dataframe_balanceado_con_edad_y_tumor_agrupados.csv"
|
|
ETAPAS = [
|
|
dict(id="01", script="01_depuracion.py", requiere=["dataframe_recodificado.csv", "mapeo_columnas_recodificadas.json"],
|
|
produce=["dataframe_depurado.csv"]),
|
|
dict(id="02", script="02_balanceo_categorizacion.py", requiere=["dataframe_depurado.csv"],
|
|
produce=[BALANCEADO, "propuestas_categorizacion.csv", "dataframe_poblacion_natural.csv"]),
|
|
dict(id="03", script="03_bootstrapping.py", requiere=[BALANCEADO], produce=["bootstrap_indices.npy", "bootstrap_metadata.json"]),
|
|
dict(id="04", script="04_modelado.py",
|
|
requiere=[BALANCEADO, "propuestas_categorizacion.csv", "bootstrap_indices.npy", "bootstrap_metadata.json", "dataframe_poblacion_natural.csv"],
|
|
produce=[]), # sin salida unica verificable: siempre se corre
|
|
dict(id="05", script="05_curvas_supervivencia.py", requiere=["dataframe_depurado.csv"],
|
|
produce=["curva_supervivencia_KM_KS_LR_HBN.png", "curva_supervivencia_puntos_LR_HBN.csv",
|
|
"subgrupos_puntos_LR_HBN.csv", "subgrupos_KM_Weibull_fraccional.csv"]),
|
|
dict(id="06", script="06_factores_riesgo.py",
|
|
requiere=["dataframe_poblacion_natural.csv", "seleccion_bootstrap.csv", "hbn_arcos.csv"],
|
|
produce=["factores_riesgo.csv", "factores_riesgo_resumen_variables.csv", "asociaciones_tratamiento.csv"]),
|
|
]
|
|
|
|
def mtime(p): return Path(p).stat().st_mtime
|
|
|
|
def vigente(e):
|
|
if not e["produce"] or not all(Path(p).exists() and Path(p).stat().st_size > 0 for p in e["produce"]): return False
|
|
ref = max([mtime(e["script"])] + [mtime(p) for p in e["requiere"] if Path(p).exists()])
|
|
return min(mtime(p) for p in e["produce"]) >= ref
|
|
|
|
def guardar(bit):
|
|
with open("bitacora_pipeline.csv", "w", newline="", encoding="utf8") as f:
|
|
w = csv.writer(f); w.writerow(["etapa", "estado", "segundos"]); w.writerows(bit)
|
|
|
|
def main():
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument("--solo", nargs="+"); ap.add_argument("--desde")
|
|
ap.add_argument("--forzar", nargs="+", default=[]); ap.add_argument("--todo", action="store_true")
|
|
a = ap.parse_args()
|
|
sel = [e for e in ETAPAS if (not a.solo or e["id"] in a.solo) and (not a.desde or e["id"] >= a.desde)]
|
|
bit = []; rehacer = False
|
|
for e in sel:
|
|
faltan = [p for p in e["requiere"] if not Path(p).exists()]
|
|
if faltan: sys.exit(f"Etapa {e['id']}: faltan entradas {faltan}")
|
|
if not (a.todo or e["id"] in a.forzar or rehacer) and vigente(e):
|
|
bit.append((e["id"], "saltada", 0)); continue
|
|
t0 = time.time(); r = subprocess.run([sys.executable, e["script"]])
|
|
bit.append((e["id"], "ok" if r.returncode == 0 else f"error({r.returncode})", round(time.time() - t0)))
|
|
guardar(bit)
|
|
if r.returncode != 0: sys.exit(r.returncode)
|
|
rehacer = rehacer or bool(e["produce"]) # si una etapa con salidas se rehizo, las siguientes tambien
|
|
guardar(bit)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|