PULSE-code / pulse /tasks /datacheck.py
velvet-pine-22's picture
PULSE-code: benchmark code for the PULSE dataset (camera-ready release)
dffbc1f verified
Raw History Blame Contribute Delete
3.55 kB
"""Infrastructure smoke test (not a paper result).
Loads every recording the manifest expects for one fold with the strict
loaders, checks the manifest, and fits a nearest-centroid scenario classifier
on per-recording mean/std features (train volunteers), reports it on the
inner-validation volunteers and evaluates it once on the test volunteers. It
exercises the whole pipeline (paths, TSV fallback, manifest, provenance,
results.json, aggregation) on CPU in minutes::
python -m pulse.tasks.datacheck --root $PULSE_ROOT --split splits/pool33.json \
--fold 0 --seed 42 --config configs/datacheck/emg_pressure.yaml
"""
from __future__ import annotations
import sys
import time
import numpy as np
from ..data.io import load_recording
from ..metrics import accuracy, confusion_stat, macro_f1
from ..paths import split_rid
from .common import TaskRun, build_parser
N_CLASSES = 8 # scenarios S1-S8
def features(arrays, order, downsample):
feats = []
for m in order:
a = arrays[m][::downsample].astype(np.float64)
feats.append(a.mean(0))
feats.append(a.std(0))
return np.concatenate(feats)
def main(argv=None) -> int:
args = build_parser("datacheck", __doc__).parse_args(argv)
run = TaskRun("datacheck", args)
cfg = run.config
mods = list(cfg["modalities"])
man = run.manifest(mods, cfg.get("gate_modalities", []), cfg.get("scenarios", None) or
[f"s{i}" for i in range(1, 9)], bool(cfg.get("require_annotation", False)))
if args.print_manifest:
return 0
ds = int(cfg.get("downsample", 5))
X, y, loaded, frames = {}, {}, {}, {}
t0 = time.time()
for part in ("train", "inner_val", "test"):
rids = man.partitions[part]
xs, ys = [], []
for rid in rids:
rec = load_recording(run.dr, rid, mods, cfg.get("gate_modalities", []))
xs.append(features(rec.arrays, mods, ds))
ys.append(int(split_rid(rid)[1][1:]) - 1)
frames[rid] = rec.n_frames
X[part], y[part], loaded[part] = np.stack(xs), np.asarray(ys), list(rids)
print(f" loaded {part}: {len(rids)} recordings ({time.time() - t0:.0f}s)", flush=True)
mu, sd = X["train"].mean(0), X["train"].std(0)
sd[sd < 1e-8] = 1.0
z = {p: (X[p] - mu) / sd for p in X}
cents = np.stack([z["train"][y["train"] == c].mean(0) if np.any(y["train"] == c)
else np.full(z["train"].shape[1], np.inf) for c in range(N_CLASSES)])
def predict(a):
d = ((a[:, None, :] - cents[None]) ** 2).sum(-1)
return np.argmin(d, axis=1)
p_iv = predict(z["inner_val"]) if len(z["inner_val"]) else np.zeros(0, int)
cm_iv = confusion_stat(y["inner_val"], p_iv, N_CLASSES)
p_te = predict(z["test"])
per_rec = {rid: {"confusion": confusion_stat([yt], [yp], N_CLASSES)}
for rid, yt, yp in zip(loaded["test"], y["test"], p_te)}
defs = {"macro_f1": {"fn": "macro_f1", "stat": "confusion"},
"accuracy": {"fn": "accuracy", "stat": "confusion"}}
run.finish(loaded=loaded, metric_defs=defs, per_recording={"test": per_rec},
metrics={"inner_val": {"macro_f1": macro_f1(cm_iv), "accuracy": accuracy(cm_iv)}},
manifest=man, selection={"criterion": "none (closed-form classifier)"},
extra={"note": "infrastructure smoke test, not a paper result",
"n_frames": frames, "feature_dim": int(X["train"].shape[1])})
return 0
if __name__ == "__main__":
sys.exit(main())