From 4e224a03cb425bad22eb5d0e40af7d134a159e31 Mon Sep 17 00:00:00 2001 From: AlbertoPMDM Date: Wed, 18 Nov 2026 23:49:54 -0600 Subject: [PATCH] verify: diagnose failed HK1K studies step by step (file, step, message, traceback) --- src/tea_modulo/verify/hk1k.py | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/tea_modulo/verify/hk1k.py b/src/tea_modulo/verify/hk1k.py index a193b99..6aabbf2 100644 --- a/src/tea_modulo/verify/hk1k.py +++ b/src/tea_modulo/verify/hk1k.py @@ -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'\"]*", "", 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", []))