"""F0 onset-timing analysis: event detection, onset finding, end to end on crafted signals.""" import csv import json import numpy as np from conftest import REPO, SKELETON_JOINTS, write_csv from pulse.data import modalities as M from pulse.data.availability import build_availability from pulse.paths import DataRoot from pulse.tasks import f0 T = 1500 ONSETS = (400, 800, 1200) def test_detect_onsets_hysteresis_and_gap(): p = np.zeros(1000) p[100:300] = 20.0 p[303:400] = 20.0 # 3-frame dip: no release, same contact p[600:603] = 20.0 # too short to persist p[800:820] = 20.0 p[840:900] = 20.0 # released, but re-contact within min_gap: not a new onset on = f0.detect_onsets(p, 5.0, 10, 50, 0.7, 5, 0.3) assert on == [100, 800] def test_signal_onset_finds_rise(): s = np.zeros(400) s[250:] = 1.0 # rises 50 frames before ref=300 assert f0.signal_onset(s, 300, 200, 0.3, 0.3, 25) == -50 assert f0.signal_onset(np.zeros(400), 300, 200, 0.3, 0.3, 25) is None def _craft(rng, shift_emg=60, shift_motion=80, gap_at=None): pres = np.zeros((T, 50)) emg = rng.normal(0, 0.05, (T, 8)) pos = np.zeros((T, 3)) x = 100.0 # a real hand is never at the lab origin moving = np.zeros(T, bool) for on in ONSETS: pres[on:on + 150, :25] = 2.0 # right hand only emg[on - shift_emg:on + 100] += rng.normal(0, 1.0, (shift_emg + 100, 8)) moving[on - shift_motion:on] = True for i in range(T): x += 5.0 if moving[i] else 0.0 pos[i] = (x, 0.0, 0.0) if gap_at is not None: pos[gap_at] = 0.0 # untracked frame (Qualisys writes 0, 0, 0) return pres, emg, pos def _write_recording(d, rid, rng, **kw): pres, emg, pos = _craft(rng, **kw) write_csv(d / "aligned_pressure_100hz.csv", M.PRESSURE_COLS, pres.tolist()) write_csv(d / "aligned_emg_100hz.csv", M.EMG_COLS, emg.tolist()) 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"] tsv = d / "raw" / f"aligned_{rid}_s_Q.tsv" for p in (d / f"aligned_{rid}_s_Q.tsv", tsv): if p.exists(): p.unlink() tsv.parent.mkdir(parents=True, exist_ok=True) with open(tsv, "w", encoding="utf-8") as f: f.write("\t".join(cols) + "\n") for i in range(T): row = [str(i), f"{i / 100:.2f}"] for j in SKELETON_JOINTS: xyz = pos[i] if j == "RightHand" else (500.0, 500.0, 500.0) row += [f"{v:.3f}" for v in xyz] + ["0.1", "0.2", "0.3", "0.9"] f.write("\t".join(row) + "\n") # other streams must keep the same length for name in ("aligned_imu_100hz.csv", "aligned_eyetrack_100hz.csv", "aligned_myo_quat_100hz.csv", "aligned_mocap_100hz.csv"): p = d / name if p.exists(): p.unlink() meta = json.loads((d / "alignment_metadata.json").read_text(encoding="utf-8")) meta.update(n_samples=T, aligned_modalities=["emg", "pressure", "mocap_sq"]) (d / "alignment_metadata.json").write_text(json.dumps(meta), encoding="utf-8") def test_f0_end_to_end(mini_root, tmp_path): rng = np.random.default_rng(3) for rid, gap in (("v6s1", None), ("v6s2", 700)): # v6s2: one untracked frame before contact 800 _write_recording(mini_root / "data" / "v6" / rid[2:], rid, rng, gap_at=gap) rows = list(csv.DictReader(open(mini_root / "metadata" / "recordings.csv", encoding="utf-8"))) for r in rows: if r["recording_id"] in ("v6s1", "v6s2"): r.update(n_samples_100hz=T, aligned_modalities="pressure|emg") with open(mini_root / "metadata" / "recordings.csv", "w", newline="", encoding="utf-8") as f: w = csv.DictWriter(f, fieldnames=list(rows[0])) w.writeheader() w.writerows(rows) av = tmp_path / "av.json" av.write_text(json.dumps(build_availability(DataRoot.from_arg(mini_root))), encoding="utf-8") out = tmp_path / "f0" rc = f0.main(["--root", str(mini_root), "--availability", str(av), "--out", str(out), "--workers", "1", "--set", "volunteers=[v6]", "--set", "n_bootstrap=200"]) assert rc == 0 r = json.loads((out / "f0_timing.json").read_text(encoding="utf-8")) assert r["provenance"]["loaded_recordings"] == ["v6s1", "v6s2"] assert r["counts"]["onsets"] == 6 and r["counts"]["used"] == 5 and r["counts"]["mocap_gap"] == 1 assert r["per_recording_counts"]["v6s2"]["mocap_gap"] == 1 st = r["stats"] assert -800 <= st["emg_delay_ms"]["median"] <= -400 # burst starts 600 ms before contact assert -900 <= st["motion_delay_ms"]["median"] <= -700 # motion starts 800 ms before ev = st["paired_motion_minus_emg"]["event_level"] assert ev["mean_diff_ms"] < 0 and ev["motion_first_fraction"] == 1.0 assert st["paired_motion_minus_emg"]["volunteer_level"]["n_volunteers"] == 1 z = np.load(out / "f0_traces.npz") assert z["traces"].shape == (5, 3, 250) and z["time_ms"][0] == -2000 assert str(mini_root) not in (out / "f0_timing.json").read_text(encoding="utf-8") # figures + numbers import subprocess import sys res = subprocess.run([sys.executable, str(REPO / "figures" / "f0" / "plot_f0.py"), "--in", str(out), "--out", str(tmp_path / "fig"), "--tables", str(tmp_path / "tables")], capture_output=True, text=True) assert res.returncode == 0, res.stderr assert (tmp_path / "fig" / "f0_grasp_phase_timing.pdf").is_file() assert (tmp_path / "fig" / "f0_delay_distributions.pdf").is_file() nums = (tmp_path / "tables" / "numbers_f0.tex").read_text(encoding="utf-8") assert r"\csname pulse@f0/events\endcsname{5}" in nums def test_edge_filled_parsing(): w = {"warnings": ["mocap edge-filled to preserve original duration (front=4.092s, back=6.408s)", "something else"]} assert f0.edge_filled(w) == {"mocap": (4.092, 6.408)} def test_place_on_alignment_timeline(): from pulse.errors import DataQualityError span = {"start": 3, "n": 5, "n_total": 12, "offsets": {"emg": 2, "mocap": 0}, "lengths": {"emg": 9, "mocap": 12}} emg = np.arange(9) # re-trimmed stream: timeline frames 2..10 mocap = np.arange(12) # full timeline assert f0.place(emg, "emg", span, "v0s1").tolist() == [1, 2, 3, 4, 5] # timeline 3..7 assert f0.place(mocap, "mocap", span, "v0s1").tolist() == [3, 4, 5, 6, 7] try: f0.place(np.arange(8), "emg", span, "v0s1") except DataQualityError: pass else: raise AssertionError("a stream with an unexpected length must raise") def test_volunteer_bootstrap_ci_matches_its_estimate(): rng = np.random.default_rng(0) vols, emg, mot = [], [], [] for i, shift in enumerate((-120, -100, -90, -80, -60, -40, 10, 30)): n = 5 + 10 * (i % 2) # unequal event counts: pooled median != median of medians e = rng.normal(-600, 50, n) vols += [f"v{i + 1}"] * n emg += list(e) mot += list(e + shift + rng.normal(0, 5, n)) st = f0.paired_stats(np.array(emg), np.array(mot), vols, 500, 0)["volunteer_level"] lo, hi = st["bootstrap_median_of_volunteer_medians_ci95_ms"] assert lo <= st["median_of_volunteer_median_diffs_ms"] <= hi lo, hi = st["cluster_bootstrap_pooled_median_diff_ci95_ms"] assert lo <= st["pooled_median_diff_ms"] <= hi def test_fill_gaps_interpolates_untracked_frames(): pos = np.array([[1.0, 2.0, 3.0], [0.0, 0.0, 0.0], [np.nan, np.nan, np.nan], [4.0, 5.0, 6.0]]) gap = ~np.isfinite(pos).all(axis=1) | (pos == 0).all(axis=1) assert gap.tolist() == [False, True, True, False] out = f0.fill_gaps(pos, gap) assert np.allclose(out[1], [2.0, 3.0, 4.0]) and np.allclose(out[2], [3.0, 4.0, 5.0])