verify: diagnose failed HK1K studies step by step (file, step, message, traceback)
This commit is contained in:
@@ -73,6 +73,39 @@ def sequential(fn, argsets):
|
|||||||
return [fn(*a) for a in 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:
|
def group_stats(values) -> dict | None:
|
||||||
import numpy as np
|
import numpy as np
|
||||||
v = np.asarray([x for x in values if x is not None and math.isfinite(x)], float)
|
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_",
|
if c.startswith(("posterior_", "spindle_", "gradient_", "slow_wave_",
|
||||||
"aperiodic_", "mse_"))},
|
"aperiodic_", "mse_"))},
|
||||||
"by_age_group": {}}
|
"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)):
|
for lo, hi in ((12, 24), (24, 36)):
|
||||||
g = ok[(ok.age_months >= lo) & (ok.age_months < hi)]
|
g = ok[(ok.age_months >= lo) & (ok.age_months < hi)]
|
||||||
info["by_age_group"][f"{lo}-{hi}"] = group_stats(g.get("posterior_rhythm_hz", []))
|
info["by_age_group"][f"{lo}-{hi}"] = group_stats(g.get("posterior_rhythm_hz", []))
|
||||||
|
|||||||
Reference in New Issue
Block a user