verify: diagnose failed HK1K studies step by step (file, step, message, traceback)

This commit is contained in:
AlbertoPMDM
2026-11-18 23:49:54 -06:00
parent 5da1921c69
commit 4e224a03cb
+36
View File
@@ -73,6 +73,39 @@ def sequential(fn, argsets):
return [fn(*a) for a in argsets]
def diagnose(path, age_months, plan, modules, order, biomarkers, line_hz) -> dict:
"""In a task: re-run one failed HK1K study step by step, without catching, and report
the step that raised, the message and the traceback (paths removed). HK1K only."""
import re
import traceback
from tea_modulo.contracts import Study
from tea_modulo.pipeline import storage
path = "s3://" + guard(path)
out = {"file": Path(path).name, "step": None, "error": "", "traceback": ""}
clean = lambda s: re.sub(r"(/|[A-Za-z]:\\)[^\s'\"]*", "<path>", s) # noqa: E731
step = "read"
try:
with storage.local_copy(path) as p:
step = "validation"
summary = modules["validation"].summarize(p, line_hz=line_hz)
step = "preprocessing"
prep = modules["preprocessing"].run(p, modules["preprocessing"].plan_for(summary, plan),
age_months=age_months)
study = Study(prep)
for name in order:
step = name
study = study.with_labels(modules[name].predict(study))
for name in biomarkers:
step = name
modules[name].measure(study)
out["step"] = "none (did not fail again)"
except Exception as e: # noqa: BLE001
out.update(step=step, error=clean(f"{type(e).__name__}: {e}")[:500],
traceback=clean("".join(traceback.format_exception(e)[-12:]))[-4000:])
return out
def group_stats(values) -> dict | None:
import numpy as np
v = np.asarray([x for x in values if x is not None and math.isfinite(x)], float)
@@ -186,6 +219,9 @@ def main(argv=None) -> int:
if c.startswith(("posterior_", "spindle_", "gradient_", "slow_wave_",
"aperiodic_", "mse_"))},
"by_age_group": {}}
failed = remote.loc[remote.error != "", "key"].tolist()[:10]
info["diagnosis"] = [client.submit(diagnose, paths[i], ages[i], plan, mods, order, bios, 50.0,
pure=False, priority=-10).result() for i in failed]
for lo, hi in ((12, 24), (24, 36)):
g = ok[(ok.age_months >= lo) & (ok.age_months < hi)]
info["by_age_group"][f"{lo}-{hi}"] = group_stats(g.get("posterior_rhythm_hz", []))