"""Synthetic miniature PULSE tree with the public layout and its edge cases.""" from __future__ import annotations import csv import json import os import sys from pathlib import Path import numpy as np import pytest REPO = Path(__file__).resolve().parents[1] sys.path.insert(0, str(REPO)) from pulse.data import modalities as M # noqa: E402 T = 240 # frames per synthetic recording (100 Hz) IMU_FIELDS = ["acc_x", "acc_y", "acc_z", "gyr_x", "gyr_y", "gyr_z", "ang_x", "ang_y", "ang_z", "mag_x", "mag_y", "mag_z", "q0", "q1", "q2", "q3", "temp", "battery"] SKELETON_JOINTS = ["Hips", "Spine", "RightHand", "RightInHandIndex", "LeftHand"] def imu_columns(n_sensors: int): return [f"WT{s}_{f}" for s in range(n_sensors) for f in IMU_FIELDS] def write_csv(path: Path, header, data, extra_cols=None): path.parent.mkdir(parents=True, exist_ok=True) with open(path, "w", newline="", encoding="utf-8") as f: w = csv.writer(f) w.writerow(["time"] + list(header)) for i, row in enumerate(data): w.writerow([f"{i / 100:.2f}"] + [f"{x:.5f}" if isinstance(x, float) else x for x in row]) def write_tsv(path: Path, n: int, rng): path.parent.mkdir(parents=True, exist_ok=True) cols = ["Frame", "Time"] for j in SKELETON_JOINTS: cols += [f"{j}_X", f"{j}_Y", f"{j}_Z", f"{j}_QX", f"{j}_QY", f"{j}_QZ", f"{j}_QW"] with open(path, "w", encoding="utf-8") as f: f.write("\t".join(cols) + "\n") for i in range(n): vals = [str(i), f"{i / 100:.2f}"] + [f"{v:.4f}" for v in rng.normal(500, 50, len(cols) - 2)] f.write("\t".join(vals) + "\n") # volunteer -> scenario -> spec LAYOUT = { "v1": {"s1": {}, "s2": {"tsv_at_root": True}, "s9": {"no_annotation": True}}, "v2": {"s1": {}, "s2": {"eyetrack_one_eye": True}}, "v3": {"s1": {"no_pressure": True}, "s2": {"no_pressure": True}}, "v5": {"s1": {"imu_sensors": 4, "no_mocap": True}, "s2": {"imu_sensors": 4, "no_mocap": True}}, "v6": {"s1": {}, "s2": {}}, } def build_tree(root: Path, seed: int = 0) -> Path: rng = np.random.default_rng(seed) rows = [] for vol, scens in LAYOUT.items(): for scen, spec in scens.items(): rid = f"{vol}{scen}" d = root / "data" / vol / scen d.mkdir(parents=True, exist_ok=True) mods = ["emg", "imu"] k = int(scen[1:]) write_csv(d / "aligned_emg_100hz.csv", M.EMG_COLS, (rng.normal(k, 1, (T, 8))).tolist()) write_csv(d / "aligned_imu_100hz.csv", imu_columns(spec.get("imu_sensors", 10)), rng.normal(k, 1, (T, 18 * spec.get("imu_sensors", 10))).tolist()) write_csv(d / "aligned_myo_quat_100hz.csv", M.MYO_QUAT_COLS, rng.normal(0, 1, (T, 4)).tolist()) ecols = list(M.EYETRACK_CORE_COLS) if spec.get("eyetrack_one_eye"): ecols = [c for c in ecols if "Right Eye" not in c] with open(d / "aligned_eyetrack_100hz.csv", "w", newline="", encoding="utf-8") as f: w = csv.writer(f) w.writerow(["time", "UTC"] + ecols) for i in range(T): w.writerow([i / 100, 1.7e12 + i] + list(rng.normal(100, 10, len(ecols)))) mods.append("eyetrack") if not spec.get("no_pressure"): p = np.abs(rng.normal(0, 1, (T, 50))) p[: T // 2] = 0.0 write_csv(d / "aligned_pressure_100hz.csv", M.PRESSURE_COLS, p.tolist()) mods.append("pressure") if not spec.get("no_mocap"): tsv = (d if spec.get("tsv_at_root") else d / "raw") / f"aligned_{rid}_s_Q.tsv" write_tsv(tsv, T, rng) write_csv(d / "aligned_mocap_100hz.csv", ["Q_RThumbTip X", "Q_RThumbTip Y", "Q_RThumbTip Z", "Q_RThumbTip Type"], [[*rng.normal(0, 1, 3).tolist(), "Measured"] for _ in range(T)]) mods.append("mocap") align = ["emg", "imu", "eyetrack", "myo_quat"] align += ["pressure"] if "pressure" in mods else [] align += ["mocap", "mocap_sq"] if "mocap" in mods else [] with open(d / "alignment_metadata.json", "w", encoding="utf-8") as f: json.dump({"volunteer": vol, "scenario": scen, "n_samples": T, "aligned_modalities": align, "modalities": {m: {} for m in align}}, f) has_ann = scen != "s9" and not spec.get("no_annotation") if has_ann: segs = [] verbs = ["grasp", "move", "place", "support", "grasp"] nouns = ["cup", "box", "cup", "cup", "mouse"] for i in range(5): segs.append({"timestamp": f"00:00:0{i}-00:00:0{i + 1}", "action_annotation": {"action_name": verbs[i], "object_name": nouns[i], "hand_type": "right"}}) (root / "annotations" / vol).mkdir(parents=True, exist_ok=True) with open(root / "annotations" / vol / f"{scen}.json", "w", encoding="utf-8") as f: json.dump({"segments": segs}, f) all_mods = ["scene_video", "eyetrack", "pressure", "emg", "imu", "mocap"] rows.append({"recording_id": rid, "volunteer_id": vol, "scenario_id": scen, "duration_sec": T / 100, "n_samples_100hz": T, "aligned_modalities": "|".join(m for m in all_mods if m in mods), "missing_modalities": "|".join(m for m in all_mods if m not in mods and m != "scene_video"), "has_dense_annotations": int(has_ann), "split": "train"}) (root / "metadata").mkdir(parents=True, exist_ok=True) with open(root / "metadata" / "recordings.csv", "w", newline="", encoding="utf-8") as f: w = csv.DictWriter(f, fieldnames=list(rows[0])) w.writeheader() w.writerows(rows) return root @pytest.fixture() def mini_root(tmp_path) -> Path: return build_tree(tmp_path / "PULSE") def mini_split(path: Path) -> Path: """A 3-fold split over the synthetic volunteers.""" d = {"schema": "pulse-split/1", "name": "mini", "volunteers": ["v1", "v2", "v3", "v5", "v6"], "folds": [ {"fold": 0, "test": ["v1"], "inner_val": ["v2"], "train": ["v3", "v5", "v6"]}, {"fold": 1, "test": ["v2", "v5"], "inner_val": ["v6"], "train": ["v1", "v3"]}, {"fold": 2, "test": ["v3", "v6"], "inner_val": ["v1"], "train": ["v2", "v5"]}, ]} path.write_text(json.dumps(d), encoding="utf-8") return path def real_root(): r = os.environ.get("PULSE_ROOT") return Path(r) if r and Path(r, "data").is_dir() else None realdata = pytest.mark.skipif(real_root() is None, reason="PULSE_ROOT not set to a dataset copy")