#!/usr/bin/env python3 import argparse import json from pathlib import Path import numpy as np def main(): p = argparse.ArgumentParser(description="Create a tiny subset of ds16k dataset for smoke testing") p.add_argument("--dataset-root", required=True) p.add_argument("--out-root", required=True) p.add_argument("--per-class", type=int, default=64) args = p.parse_args() src = Path(args.dataset_root).expanduser().resolve() out = Path(args.out_root).expanduser().resolve() out.mkdir(parents=True, exist_ok=True) classes = sorted([d for d in src.iterdir() if d.is_dir() and d.name.startswith("expert_")]) if not classes: raise SystemExit("no expert_* dirs found") summary = { "source": str(src), "out": str(out), "per_class": int(args.per_class), "classes": [], } for c in classes: t = np.load(c / "traces.npy", mmap_mode="r") ids = np.load(c / "trial_ids.npy") k = min(int(args.per_class), int(t.shape[0])) c_out = out / c.name c_out.mkdir(parents=True, exist_ok=True) np.save(c_out / "traces.npy", np.array(t[:k], dtype=np.float32)) np.save(c_out / "trial_ids.npy", np.array(ids[:k], dtype=np.int32)) meta = { "class": c.name, "rows": int(k), "trace_len": int(t.shape[1]), "trial_id_min": int(ids[:k].min()), "trial_id_max": int(ids[:k].max()), } with open(c_out / "meta.json", "w") as f: json.dump(meta, f, indent=2) summary["classes"].append(meta) for fn in ["README.md", "capture_meta.json", "downsample_summary.json"]: pth = src / fn if pth.exists(): (out / fn).write_bytes(pth.read_bytes()) with open(out / "subset_summary.json", "w") as f: json.dump(summary, f, indent=2) print("[ok] wrote subset", out) print("[ok] class_count", len(classes), "per_class", int(args.per_class), "total", len(classes) * int(args.per_class)) if __name__ == "__main__": main()