/* Next Signal Space: pick or upload a sensor window, perturb it, compare three frozen representations + probes. */ const { $, h, fmt, lineChart, legend, css } = NLUI; const ACT = ["walking", "walking upstairs", "walking downstairs", "sitting", "standing", "lying"]; const CH = ["body_acc_x", "body_acc_y", "body_acc_z", "body_gyro_x", "body_gyro_y", "body_gyro_z", "total_acc_x", "total_acc_y", "total_acc_z"]; const CCOL = ["#4e79a7", "#f28e2b", "#e15759", "#76b7b2", "#59a14f", "#edc948", "#b07aa1", "#ff9da7", "#9c755f"]; let ctx, uploaded = null, lastPred = null; function current() { const base = uploaded || ctx.w.windows[Number($("win").value)]; return Core.perturb(base, Number($("noise").value), Number($("scale").value), Number($("shift").value), 17); } function bars(el, p, truth) { el.innerHTML = ""; const best = NLRT.argmax(p); p.forEach((v, k) => el.append(h("div", { class: "small", style: "display:flex;align-items:center;gap:6px;margin:2px 0" }, h("span", { style: "width:130px;flex:none" + (k === truth ? ";font-weight:700" : "") }, ACT[k] + (k === truth ? " ✓" : "")), h("span", { style: `height:10px;border-radius:3px;background:${k === best ? "#0b6e4f" : "var(--line)"};width:${Math.max(2, 160 * v)}px` }), h("span", { class: "muted" }, fmt(100 * v, 1) + "%")))); } function update() { ["noise", "scale", "shift"].forEach(id => $(id + "_v").textContent = $(id).value); const x = current(), budget = Number($("budget").value); const truth = uploaded ? -1 : ctx.w.labels[Number($("win").value)]; const series = x.map((row, c) => ({ name: CH[c], color: CCOL[c], width: 1.2, points: row.map((v, t) => [t / 50, v]) })); const sel = $("chans").value === "acc" ? [0, 1, 2] : $("chans").value === "gyro" ? [3, 4, 5] : [6, 7, 8]; lineChart($("sig"), sel.map(i => series[i]), { xlabel: "seconds (50 Hz)", height: 200 }); legend($("sigl"), sel.map(i => series[i])); const t0 = performance.now(); lastPred = Core.predictAll(ctx, x, budget); $("ms").textContent = fmt(performance.now() - t0, 1); bars($("p_ssl"), lastPred.ssl, truth); bars($("p_rand"), lastPred.random, truth); bars($("p_stats"), lastPred.stats, truth); $("truth").textContent = uploaded ? "uploaded window (no label)" : `true activity: ${ACT[truth]} · test subject ${ctx.w.subjects[Number($("win").value)]}`; return lastPred; } function parseCsv(text) { const rows = text.trim().split(/\r?\n/).map(r => r.split(/[,;\t ]+/).map(Number)).filter(r => r.length >= 9 && r.every(isFinite)); if (rows.length < 128) throw new Error(`need 128 rows of 9 numbers, got ${rows.length}`); return Array.from({ length: 9 }, (_, c) => rows.slice(0, 128).map(r => r[c])); } window.__liveTest = function () { $("win").value = "7"; $("noise").value = "0.5"; $("scale").value = "1.3"; $("shift").value = "40"; $("budget").value = "10"; const p1 = update(); const csv = ctx.w.windows[3][0].map((_, t) => ctx.w.windows[3].map(r => r[t]).join(",")).join("\n"); uploaded = parseCsv(csv); const p2 = update(); uploaded = null; const same = Core.predictAll(ctx, Core.perturb(ctx.w.windows[3], 0.5, 1.3, 40, 17), 10); return { ok: p1.ssl.length === 6 && Math.abs(p2.ssl[0] - same.ssl[0]) < 1e-12, ssl_pred: ACT[NLRT.argmax(p1.ssl)], truth: ACT[ctx.w.labels[7]], stats_pred: ACT[NLRT.argmax(p1.stats)], upload_pred: ACT[NLRT.argmax(p2.ssl)] }; }; NLUI.boot(Core, async c => { ctx = c; ctx.w.windows.forEach((_, i) => $("win").append(h("option", { value: i }, `#${i + 1} · ${ACT[ctx.w.labels[i]]} · subject ${ctx.w.subjects[i]}`))); for (const id of ["win", "noise", "scale", "shift", "budget", "chans"]) $(id).addEventListener("input", () => { if (id === "win") uploaded = null; update(); }); $("file").addEventListener("change", async e => { try { uploaded = parseCsv(await e.target.files[0].text()); $("status").textContent = "Using uploaded window."; update(); } catch (err) { $("status").textContent = "Upload failed: " + err.message; } }); window.addEventListener("resize", update); update(); });