Scikit-learn
human-activity-recognition
wearable
wrist
time-series
cpu
scikit-learn
WISP / benchmarks /funnel_cells.py
Zipeng365's picture
wisp-har 0.4.0: WISP-Funnel search and the adaptive HMM switch (default decoder)
93a9282 verified
Raw History Blame Contribute Delete
4.11 kB
"""WISP-Funnel on one WristHARBench cell through the public API (the package's 150-cell benchmark).
Fit on the training split with the paper's validation split (selection uses training and
validation data only); both controllers come from one run (``funnel_estimators_``); the test
split is scored once with each model's decoder (``groups`` = participant, ``time_index``).
``--decoder recipe`` is the protocol of the funnel benchmark record (each winner's own HMM
flag); ``--decoder adaptive`` is the 0.4.0 default. Run with two cores and one BLAS thread per
process, as the record did (``OMP_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1``; ``--n-jobs 2`` runs
the two controllers in parallel).
python benchmarks/funnel_cells.py --data-root <fold exports> --dataset adl_wrist_accel_v1 \\
--fold 0 --seed 42 --decoder recipe --out <dir>
"""
from __future__ import annotations
import argparse
import json
import os
import resource
import sys
import time
from pathlib import Path
import numpy as np
sys.path.insert(0, str(Path(__file__).resolve().parent))
from _frozen import paper_scores # noqa: E402
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--data-root", required=True)
ap.add_argument("--dataset", required=True)
ap.add_argument("--fold", type=int, required=True)
ap.add_argument("--seed", type=int, required=True)
ap.add_argument("--decoder", default="recipe")
ap.add_argument("--n-jobs", type=int, default=2)
ap.add_argument("--out", required=True)
a = ap.parse_args()
import wisp
dest = Path(a.out) / a.dataset / f"fold{a.fold}_seed{a.seed}"
dest.mkdir(parents=True, exist_ok=True)
d = wisp.datasets.load_exported_fold(a.data_root, a.dataset, a.fold)
tr = {"X": d.train.X, "y": d.train.y, "subject": d.train.groups, "time_index": d.train.time_index}
va = {"X": d.val.X, "y": d.val.y, "subject": d.val.groups, "time_index": d.val.time_index}
te = {"X": d.test.X, "y": d.test.y, "subject": d.test.groups, "time_index": d.test.time_index}
K = len(d.label_names)
search = wisp.WISPSearch(strategy="funnel", decoder=a.decoder, classes=np.arange(K), n_jobs=a.n_jobs,
random_state=a.seed)
t0 = time.perf_counter()
# the export's sampling rate and study setting feed the search controllers' dataset fingerprint
search.fit(tr["X"], tr["y"], groups=tr["subject"], time_index=tr["time_index"],
validation=(va["X"], va["y"], va["subject"], va["time_index"]),
sampling_rate_hz=d.sampling_rate_hz, study_setting=d.study_setting)
out = {"dataset": a.dataset, "fold": a.fold, "seed": a.seed, "decoder": a.decoder,
"fit_seconds": time.perf_counter() - t0, "funnel_seconds": search.funnel_seconds_,
"wisp_version": wisp.__version__, "methods": {}}
for c, est in search.funnel_estimators_.items():
P = est.predict_proba(te["X"])
pred = est.predict(te["X"], groups=te["subject"], time_index=te["time_index"])
s = search.funnel_summary_[c]
np.savez_compressed(dest / f"funnel_{c}_test.npz", y_true=te["y"], y_pred=pred, proba=P)
out["methods"][f"funnel_{c}"] = {
"winner_id": s["winner_id"], "recipe": s["recipe"].to_dict(), "seed": s["seed"],
"decoder": est.decoder_, "decoder_reason": est.decoder_reason_, "decoder_scores": est.decoder_scores_,
"decision_seconds": est.decision_seconds_, "proxy_seconds": s["proxy_seconds"],
"verification_seconds": s["verification_seconds"], "refit_seconds": s["refit_seconds"],
"decoded": paper_scores(te["y"], pred, K), "argmax": paper_scores(te["y"], P.argmax(1), K)}
usage = resource.getrusage(resource.RUSAGE_SELF)
out.update(cpu_seconds=usage.ru_utime + usage.ru_stime, peak_rss_gib=usage.ru_maxrss / 1024 ** 2,
slurm_job_id=os.environ.get("SLURM_JOB_ID"))
(dest / "result.json").write_text(json.dumps(out, indent=1, default=str))
print(json.dumps({k: v for k, v in out.items() if k != "methods"}, default=str))
if __name__ == "__main__":
main()