TEA EEG baselines trained on a local cohort and moved between installations as a bundle of group numbers

This commit is contained in:
AlbertoPMDM
2026-11-20 09:36:41 -06:00
parent 7b8a4e8499
commit 70646e64b6
8 changed files with 553 additions and 17 deletions
+26 -15
View File
@@ -52,10 +52,15 @@ class Listing:
def list_reference(s: Settings) -> list[tuple[str, float]]:
"""In a worker: (s3 path, age in months) of each reference EDF with an age inside `[reference] ages`."""
"""In a worker: (path, age in months) of each reference EDF with an age inside `[reference] ages`, from the
cluster's S3 or, for a cohort on this machine, from its folder (`reference.local_studies`)."""
from tea.pipeline.storage import s3
from models.tea.eeg.reference import LOCAL, local_studies
r = s.reference
if r.storage == LOCAL:
return local_studies(s)
fs = s3()
paths = fs.find(r.prefix)
tables = [p for p in paths if p.endswith(r.ages_table)]
@@ -153,11 +158,6 @@ def train_scenario(scenario: str, studies: list[ReferenceStudy], found: Listing,
tags = {"domain": DOMAIN, "scenario": scenario, MODEL_SHA_TAG: sha,
COHORT_URL_TAG: f"{DOMAIN}/{REFERENCE_SOURCE}/{s.reference.name}", COHORT_SOURCE_TAG: REFERENCE_SOURCE,
**expected_tags(scenario, found, s, environment)}
for key, value in {**tags, **code_version()}.items():
client.set_tag(run_id, key, value)
for key, value in tags.items():
client.set_model_version_tag(name, version, key, value)
features = [c for c in data.columns if c != "age_months"]
present = {f: int(data[f].notna().sum()) for f in features}
params = getattr(module, "baseline_params", {}) or {}
@@ -179,21 +179,32 @@ def train_scenario(scenario: str, studies: list[ReferenceStudy], found: Listing,
"edades_cubiertas_meses": list(covered) if covered else None,
"n_efectivo_minimo": n_min},
}
for kind, record in routines.items():
client.log_text(run_id, json.dumps(record, ensure_ascii=False, indent=1, default=str), routine_path(kind))
client.log_metric(run_id, "n_referencia", len(data))
for feature, n in present.items():
client.log_metric(run_id, f"n.{feature}", n)
metrics = {"n_referencia": len(data), **{f"n.{feature}": n for feature, n in present.items()}}
if covered:
client.log_metric(run_id, "edad_min_cubierta", covered[0])
client.log_metric(run_id, "edad_max_cubierta", covered[1])
client.update_model_version(name, version, description=(
metrics.update(edad_min_cubierta=covered[0], edad_max_cubierta=covered[1])
description = (
f"Curva de referencia por edad de {biomarker}{f' en sueño {phase}' if phase else ''}, entrenada en "
f"{s.reference.name} ({len(data)} estudios de {s.reference.ages[0]:g} a {s.reference.ages[1]:g} meses). "
"Pendiente de validación clínica."))
"Pendiente de validación clínica.")
record(client, name, version, run_id, tags=tags, routines=routines, metrics=metrics, description=description)
return version
def record(client, name: str, version: str, run_id: str, *, tags: dict[str, str], routines: dict[str, dict],
metrics: dict[str, float], description: str) -> None:
"""What the registry's promotion checks read, on a version and its run: the tags (on both, with the code
version on the run), the routine records, the metrics and the description."""
for key, value in {**tags, **code_version()}.items():
client.set_tag(run_id, key, value)
for key, value in tags.items():
client.set_model_version_tag(name, version, key, value)
for kind, routine in routines.items():
client.log_text(run_id, json.dumps(routine, ensure_ascii=False, indent=1, default=str), routine_path(kind))
for key, value in metrics.items():
client.log_metric(run_id, key, value)
client.update_model_version(name, version, description=description)
def train_all(s: Settings, environment: str, *, cluster: str = "auto", found: Listing | None = None,
studies: list[ReferenceStudy] | None = None) -> dict[str, tuple[str, bool]]:
"""Every scenario's version, by scenario: (version, whether it's new). A scenario already trained on the same