Lamapi commited on
Commit
298a39f
·
verified ·
1 Parent(s): 27dc113

Publish Next-Signal space

Browse files
Files changed (12) hide show
  1. README.md +56 -4
  2. app.js +64 -0
  3. core.js +63 -0
  4. evaluation.json +581 -0
  5. index.html +48 -17
  6. model.json +0 -0
  7. nlrt.js +219 -0
  8. nlui.js +123 -0
  9. parity_cases.json +1 -0
  10. preview.png +0 -0
  11. style.css +53 -26
  12. windows.json +0 -0
README.md CHANGED
@@ -1,10 +1,62 @@
1
  ---
2
  title: Next Signal
3
- emoji: 📉
4
- colorFrom: pink
5
- colorTo: blue
6
  sdk: static
 
7
  pinned: false
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  title: Next Signal
3
+ emoji: 📈
4
+ colorFrom: green
5
+ colorTo: gray
6
  sdk: static
7
+ app_file: index.html
8
  pinned: false
9
+ license: cc-by-4.0
10
+ short_description: Self-supervised sensor representations with a frozen probe
11
  ---
12
 
13
+ # Next Signal
14
+
15
+ Next Signal learns a representation of smartphone inertial-sensor windows **without labels** (SimCLR-style contrastive
16
+ learning) and checks its quality with a **frozen linear probe** on activity recognition. It is compared with a
17
+ randomly initialised encoder and with 54 simple signal statistics under the **same label budget**.
18
+
19
+ **Model:** https://huggingface.co/thelamapi/Next-Signal — pick or upload a window, add noise, scale or shift it; all three probes classify it in the browser.
20
+
21
+ ## Results on 9 unseen test subjects (2947 windows)
22
+
23
+ Accuracy (mean ± std over 5 label draws):
24
+
25
+ | Representation | 2 labels/class | 10 labels/class | 50 labels/class | all 5967 labels |
26
+ |---|---:|---:|---:|---:|
27
+ | Self-supervised encoder (Next Signal) | 0.684 ± 0.039 | 0.850 ± 0.029 | 0.901 ± 0.010 | 0.912 |
28
+ | Random encoder | 0.650 ± 0.036 | 0.779 ± 0.013 | 0.856 ± 0.013 | 0.892 |
29
+ | Signal statistics (54 features) | 0.652 ± 0.034 | 0.806 ± 0.027 | 0.862 ± 0.007 | 0.893 |
30
+
31
+ Macro-F1:
32
+
33
+ | Representation | 2/class | 10/class | 50/class |
34
+ |---|---:|---:|---:|
35
+ | Self-supervised encoder (Next Signal) | 0.688 | 0.852 | 0.903 |
36
+ | Random encoder | 0.646 | 0.776 | 0.855 |
37
+ | Signal statistics (54 features) | 0.632 | 0.802 | 0.859 |
38
+
39
+ ![Probe accuracy and confusion](preview.png)
40
+
41
+ ## Method
42
+ * Split by person: pre-training and label pool from 17 subjects, probe regularisation chosen on 4 validation subjects, scores on the official 9 test subjects.
43
+ * Encoder: 3 Conv1d blocks (32/64/64 channels, kernel 7, BatchNorm, ReLU) + global average pool → 64-d. Projection head 64→64→32 used only during pre-training.
44
+ * Pre-training: NT-Xent (τ = 0.2), batch 256, Adam 1e-3, 80 epochs on 5967 unlabeled windows; augmentations: per-channel scaling, crop-and-resize (80–128 samples), circular shift, jitter.
45
+ * Probe: standardised multinomial logistic regression; C per representation chosen on the validation subjects ({'ssl': 10.0, 'random': 1.0, 'stats': 10.0}).
46
+ * Browser: encoder runs through `nlrt.js`; Node parity max abs difference 6.106226635438361e-16.
47
+
48
+ ## Usage
49
+ ```python
50
+ from inference import load, classify
51
+ spec = load() # NumPy only
52
+ probs = classify(spec, windows, 50) # windows: (N, 9, 128) raw UCI HAR inertial signals
53
+ ```
54
+ `encoder.safetensors` contains the PyTorch encoder weights (architecture in the notebook).
55
+
56
+ ## Limitations
57
+ * One dataset and device type (waist-mounted Samsung Galaxy S II); other placements and sensors need re-validation.
58
+ * One pre-training run; probe seeds vary only the labeled samples.
59
+ * Sitting vs standing remains the main confusion (see figure). Failure examples are in `evaluation.json`.
60
+ * Trained on NVIDIA L4; profile full. Not a medical device.
61
+
62
+ Data: Anguita et al., UCI HAR Dataset, CC BY 4.0. sha256 of the downloaded archive: `c00b803081a5c797cd5e4b83700a9810b38d53d9d84e01917e090e1fdbc81031`.
app.js ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Next Signal Space: pick or upload a sensor window, perturb it, compare three frozen representations + probes. */
2
+ const { $, h, fmt, lineChart, legend, css } = NLUI;
3
+ const ACT = ["walking", "walking upstairs", "walking downstairs", "sitting", "standing", "lying"];
4
+ 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"];
5
+ const CCOL = ["#4e79a7", "#f28e2b", "#e15759", "#76b7b2", "#59a14f", "#edc948", "#b07aa1", "#ff9da7", "#9c755f"];
6
+ let ctx, uploaded = null, lastPred = null;
7
+
8
+ function current() {
9
+ const base = uploaded || ctx.w.windows[Number($("win").value)];
10
+ return Core.perturb(base, Number($("noise").value), Number($("scale").value), Number($("shift").value), 17);
11
+ }
12
+
13
+ function bars(el, p, truth) {
14
+ el.innerHTML = "";
15
+ const best = NLRT.argmax(p);
16
+ p.forEach((v, k) => el.append(h("div", { class: "small", style: "display:flex;align-items:center;gap:6px;margin:2px 0" },
17
+ h("span", { style: "width:130px;flex:none" + (k === truth ? ";font-weight:700" : "") }, ACT[k] + (k === truth ? " ✓" : "")),
18
+ h("span", { style: `height:10px;border-radius:3px;background:${k === best ? "#0b6e4f" : "var(--line)"};width:${Math.max(2, 160 * v)}px` }),
19
+ h("span", { class: "muted" }, fmt(100 * v, 1) + "%"))));
20
+ }
21
+
22
+ function update() {
23
+ ["noise", "scale", "shift"].forEach(id => $(id + "_v").textContent = $(id).value);
24
+ const x = current(), budget = Number($("budget").value);
25
+ const truth = uploaded ? -1 : ctx.w.labels[Number($("win").value)];
26
+ const series = x.map((row, c) => ({ name: CH[c], color: CCOL[c], width: 1.2, points: row.map((v, t) => [t / 50, v]) }));
27
+ const sel = $("chans").value === "acc" ? [0, 1, 2] : $("chans").value === "gyro" ? [3, 4, 5] : [6, 7, 8];
28
+ lineChart($("sig"), sel.map(i => series[i]), { xlabel: "seconds (50 Hz)", height: 200 });
29
+ legend($("sigl"), sel.map(i => series[i]));
30
+ const t0 = performance.now();
31
+ lastPred = Core.predictAll(ctx, x, budget);
32
+ $("ms").textContent = fmt(performance.now() - t0, 1);
33
+ bars($("p_ssl"), lastPred.ssl, truth); bars($("p_rand"), lastPred.random, truth); bars($("p_stats"), lastPred.stats, truth);
34
+ $("truth").textContent = uploaded ? "uploaded window (no label)" : `true activity: ${ACT[truth]} · test subject ${ctx.w.subjects[Number($("win").value)]}`;
35
+ return lastPred;
36
+ }
37
+
38
+ function parseCsv(text) {
39
+ const rows = text.trim().split(/\r?\n/).map(r => r.split(/[,;\t ]+/).map(Number)).filter(r => r.length >= 9 && r.every(isFinite));
40
+ if (rows.length < 128) throw new Error(`need 128 rows of 9 numbers, got ${rows.length}`);
41
+ return Array.from({ length: 9 }, (_, c) => rows.slice(0, 128).map(r => r[c]));
42
+ }
43
+
44
+ window.__liveTest = function () {
45
+ $("win").value = "7"; $("noise").value = "0.5"; $("scale").value = "1.3"; $("shift").value = "40"; $("budget").value = "10";
46
+ const p1 = update();
47
+ const csv = ctx.w.windows[3][0].map((_, t) => ctx.w.windows[3].map(r => r[t]).join(",")).join("\n");
48
+ uploaded = parseCsv(csv); const p2 = update(); uploaded = null;
49
+ const same = Core.predictAll(ctx, Core.perturb(ctx.w.windows[3], 0.5, 1.3, 40, 17), 10);
50
+ 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]],
51
+ stats_pred: ACT[NLRT.argmax(p1.stats)], upload_pred: ACT[NLRT.argmax(p2.ssl)] };
52
+ };
53
+
54
+ NLUI.boot(Core, async c => {
55
+ ctx = c;
56
+ ctx.w.windows.forEach((_, i) => $("win").append(h("option", { value: i }, `#${i + 1} · ${ACT[ctx.w.labels[i]]} · subject ${ctx.w.subjects[i]}`)));
57
+ for (const id of ["win", "noise", "scale", "shift", "budget", "chans"]) $(id).addEventListener("input", () => { if (id === "win") uploaded = null; update(); });
58
+ $("file").addEventListener("change", async e => {
59
+ try { uploaded = parseCsv(await e.target.files[0].text()); $("status").textContent = "Using uploaded window."; update(); }
60
+ catch (err) { $("status").textContent = "Upload failed: " + err.message; }
61
+ });
62
+ window.addEventListener("resize", update);
63
+ update();
64
+ });
core.js ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Next Signal compute core: port of signal_core.py (encoder via nlrt, stats features, probes, perturbation). */
2
+ (function (root, factory) {
3
+ if (typeof module === "object" && module.exports) module.exports = factory();
4
+ else root.Core = factory();
5
+ })(typeof self !== "undefined" ? self : this, function () {
6
+ "use strict";
7
+ const ASSETS = ["model.json", "windows.json"];
8
+ const C = 9, L = 128;
9
+
10
+ function init(assets) {
11
+ const m = NLRT.decode(assets["model.json"]);
12
+ return { m, meta: m.meta, w: assets["windows.json"] };
13
+ }
14
+
15
+ function perturb(x, noise, scale, shift, seed) {
16
+ const rng = NLRT.mulberry32(seed >>> 0), out = [];
17
+ for (let c = 0; c < C; c++) {
18
+ let mu = 0; for (let t = 0; t < L; t++) mu += x[c][t]; mu /= L;
19
+ let v = 0; for (let t = 0; t < L; t++) v += (x[c][t] - mu) * (x[c][t] - mu); const sd = Math.sqrt(v / L);
20
+ const row = new Array(L);
21
+ for (let t = 0; t < L; t++) row[t] = x[c][(((t - shift) % L) + L) % L] * scale + noise * sd * rng.normal();
22
+ out.push(row);
23
+ }
24
+ return out;
25
+ }
26
+
27
+ function embed(ctx, net, x) {
28
+ const d = new Float64Array(C * L);
29
+ for (let c = 0; c < C; c++) for (let t = 0; t < L; t++) d[c * L + t] = (x[c][t] - ctx.meta.mean[c]) / ctx.meta.std[c];
30
+ return NLRT.forward(ctx.m.nets[net], NLRT.T([C, L], d)).data;
31
+ }
32
+
33
+ function stats(x) {
34
+ const f = [];
35
+ for (let c = 0; c < C; c++) {
36
+ let mu = 0, mn = Infinity, mx = -Infinity, ad = 0, sq = 0;
37
+ for (let t = 0; t < L; t++) { const v = x[c][t]; mu += v; if (v < mn) mn = v; if (v > mx) mx = v; sq += v * v; if (t) ad += Math.abs(v - x[c][t - 1]); }
38
+ mu /= L; let vv = 0; for (let t = 0; t < L; t++) vv += (x[c][t] - mu) * (x[c][t] - mu);
39
+ f.push(mu, Math.sqrt(vv / L), mn, mx, ad / (L - 1), sq / L);
40
+ }
41
+ return f;
42
+ }
43
+
44
+ function probe(p, z) {
45
+ const K = p.W.length, out = new Array(K);
46
+ for (let k = 0; k < K; k++) { let s = p.b[k]; for (let j = 0; j < z.length; j++) s += p.W[k][j] * (z[j] - p.mu[j]) / p.sd[j]; out[k] = s; }
47
+ return NLRT.softmax(out);
48
+ }
49
+
50
+ function predictAll(ctx, x, budget) {
51
+ const P = ctx.meta.probes, b = String(budget);
52
+ return { ssl: probe(P.ssl[b], embed(ctx, "encoder", x)), random: probe(P.random[b], embed(ctx, "random_encoder", x)), stats: probe(P.stats[b], stats(x)) };
53
+ }
54
+
55
+ function runCase(ctx, input) {
56
+ const base = ctx.w.windows[input.window];
57
+ const x = perturb(base, input.noise, input.scale, input.shift, input.seed);
58
+ const p = predictAll(ctx, x, input.budget);
59
+ return { ssl: p.ssl, random: p.random, stats: p.stats, x_c0_t5: x[0][5] };
60
+ }
61
+
62
+ return { ASSETS, init, perturb, embed, stats, probe, predictAll, runCase };
63
+ });
evaluation.json ADDED
@@ -0,0 +1,581 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "project": "Next Signal",
3
+ "profile": "full",
4
+ "seed": 73,
5
+ "device": {
6
+ "python": "3.13.16",
7
+ "platform": "Linux-6.6.159+-x86_64-with-glibc2.39",
8
+ "cpu_count": 12,
9
+ "numpy": "2.4.6",
10
+ "torch": "2.14.1+cu130",
11
+ "cuda": true,
12
+ "gpu": "NVIDIA L4",
13
+ "colab": true
14
+ },
15
+ "data": {
16
+ "name": "Human Activity Recognition Using Smartphones",
17
+ "source": "https://archive.ics.uci.edu/static/public/240/human+activity+recognition+using+smartphones.zip",
18
+ "license": "CC BY 4.0",
19
+ "download": {
20
+ "url": "https://archive.ics.uci.edu/static/public/240/human+activity+recognition+using+smartphones.zip",
21
+ "file": "uci_har.zip",
22
+ "bytes": 61005872,
23
+ "sha256": "c00b803081a5c797cd5e4b83700a9810b38d53d9d84e01917e090e1fdbc81031",
24
+ "verified_against_pinned_sha256": false
25
+ },
26
+ "split": {
27
+ "pretrain_and_label_pool_subjects": [
28
+ 3,
29
+ 6,
30
+ 7,
31
+ 8,
32
+ 11,
33
+ 14,
34
+ 15,
35
+ 16,
36
+ 17,
37
+ 21,
38
+ 22,
39
+ 23,
40
+ 25,
41
+ 26,
42
+ 28,
43
+ 29,
44
+ 30
45
+ ],
46
+ "validation_subjects": [
47
+ 1,
48
+ 5,
49
+ 19,
50
+ 27
51
+ ],
52
+ "test_subjects": [
53
+ 2,
54
+ 4,
55
+ 9,
56
+ 10,
57
+ 12,
58
+ 13,
59
+ 18,
60
+ 20,
61
+ 24
62
+ ],
63
+ "windows": {
64
+ "pretrain_pool": 5967,
65
+ "validation": 1385,
66
+ "test": 2947
67
+ },
68
+ "rule": "split by person; overlapping neighbour windows never cross splits"
69
+ }
70
+ },
71
+ "pretraining": {
72
+ "epochs": 80,
73
+ "seconds": 12.5,
74
+ "final_loss": 2.184822434964387,
75
+ "loss_curve": [
76
+ 3.712400488231493,
77
+ 2.963286171788755,
78
+ 2.739344721255095,
79
+ 2.6145749817723813,
80
+ 2.5520408464514692,
81
+ 2.5070140465446142,
82
+ 2.461351944052655,
83
+ 2.4770600381104844,
84
+ 2.4195773290551226,
85
+ 2.424070461936619,
86
+ 2.4328558444976807,
87
+ 2.4071899807971455,
88
+ 2.363288568413776,
89
+ 2.3896970748901367,
90
+ 2.392222010571024,
91
+ 2.342703290607618,
92
+ 2.3041959119879682,
93
+ 2.339908081552257,
94
+ 2.316423747850501,
95
+ 2.288858061251433,
96
+ 2.3337712495223335,
97
+ 2.3336874194767163,
98
+ 2.2924146237580674,
99
+ 2.2862673324087393,
100
+ 2.2796806667162026,
101
+ 2.284140742343405,
102
+ 2.3090094068776006,
103
+ 2.2604137399922246,
104
+ 2.2845878393753716,
105
+ 2.256418549496195,
106
+ 2.284406858941783,
107
+ 2.2578139305114746,
108
+ 2.2458334694737974,
109
+ 2.2439312520234482,
110
+ 2.285909072212551,
111
+ 2.2630054536073105,
112
+ 2.2501549928084663,
113
+ 2.2446324099665103,
114
+ 2.243580144384633,
115
+ 2.2494544360948647,
116
+ 2.251435673755148,
117
+ 2.2552023141280464,
118
+ 2.23429274559021,
119
+ 2.2433379007422407,
120
+ 2.241523856702058,
121
+ 2.238611718882685,
122
+ 2.2323898750802744,
123
+ 2.215463462083236,
124
+ 2.202808970990388,
125
+ 2.2326209959776504,
126
+ 2.2435816992884097,
127
+ 2.241964412772137,
128
+ 2.2296112412991733,
129
+ 2.197518504184225,
130
+ 2.213502479636151,
131
+ 2.225979877554852,
132
+ 2.2221103440160337,
133
+ 2.2329591253529424,
134
+ 2.1921197435130244,
135
+ 2.1944941126781963,
136
+ 2.209777033847311,
137
+ 2.2089720083319624,
138
+ 2.219303970751555,
139
+ 2.2248885424240776,
140
+ 2.205455717833146,
141
+ 2.207445538562277,
142
+ 2.199899787488191,
143
+ 2.1791009488313096,
144
+ 2.215926232545272,
145
+ 2.180913614190143,
146
+ 2.1990664730901304,
147
+ 2.2125095077182935,
148
+ 2.1830043481743853,
149
+ 2.2084487002828848,
150
+ 2.2097588518391484,
151
+ 2.1778356614320176,
152
+ 2.1839393947435464,
153
+ 2.1911755748417066,
154
+ 2.1659804116124692,
155
+ 2.184822434964387
156
+ ],
157
+ "windows": 5967
158
+ },
159
+ "probe": {
160
+ "type": "standardised multinomial logistic regression",
161
+ "C_selection": {
162
+ "ssl": {
163
+ "C": 10.0,
164
+ "val_scores": {
165
+ "0.01": 0.8943441636582431,
166
+ "0.1": 0.9140794223826715,
167
+ "1.0": 0.9256317689530685,
168
+ "10.0": 0.9258724428399518
169
+ }
170
+ },
171
+ "random": {
172
+ "C": 1.0,
173
+ "val_scores": {
174
+ "0.01": 0.8016847172081829,
175
+ "0.1": 0.8245487364620939,
176
+ "1.0": 0.8250300842358604,
177
+ "10.0": 0.8204572803850781
178
+ }
179
+ },
180
+ "stats": {
181
+ "C": 10.0,
182
+ "val_scores": {
183
+ "0.01": 0.7547533092659447,
184
+ "0.1": 0.8081829121540314,
185
+ "1.0": 0.8187725631768953,
186
+ "10.0": 0.822864019253911
187
+ }
188
+ }
189
+ },
190
+ "label_seeds": [
191
+ 0,
192
+ 1,
193
+ 2,
194
+ 3,
195
+ 4
196
+ ]
197
+ },
198
+ "results_test_subjects": {
199
+ "ssl": {
200
+ "2": {
201
+ "labels_total": 12,
202
+ "acc_mean": 0.6836783169324736,
203
+ "acc_std": 0.03905040254913586,
204
+ "macro_f1_mean": 0.6880320300737847,
205
+ "macro_f1_std": 0.03318949909266294,
206
+ "seeds": 5
207
+ },
208
+ "10": {
209
+ "labels_total": 60,
210
+ "acc_mean": 0.8504920257889379,
211
+ "acc_std": 0.029252053026430767,
212
+ "macro_f1_mean": 0.852388590496618,
213
+ "macro_f1_std": 0.028747183437794182,
214
+ "seeds": 5
215
+ },
216
+ "50": {
217
+ "labels_total": 300,
218
+ "acc_mean": 0.9013912453342382,
219
+ "acc_std": 0.010230841606444484,
220
+ "macro_f1_mean": 0.9031353115767127,
221
+ "macro_f1_std": 0.009730438129879343,
222
+ "seeds": 5
223
+ },
224
+ "all": {
225
+ "labels_total": 5967,
226
+ "acc_mean": 0.9117746861214795,
227
+ "acc_std": 0.0,
228
+ "macro_f1_mean": 0.9133442911075771,
229
+ "macro_f1_std": 0.0,
230
+ "seeds": 1
231
+ }
232
+ },
233
+ "random": {
234
+ "2": {
235
+ "labels_total": 12,
236
+ "acc_mean": 0.6498133695283339,
237
+ "acc_std": 0.036250664131663314,
238
+ "macro_f1_mean": 0.645758446468177,
239
+ "macro_f1_std": 0.03552224729393867,
240
+ "seeds": 5
241
+ },
242
+ "10": {
243
+ "labels_total": 60,
244
+ "acc_mean": 0.779300984051578,
245
+ "acc_std": 0.013218300725011034,
246
+ "macro_f1_mean": 0.7763499239205346,
247
+ "macro_f1_std": 0.01271300649849152,
248
+ "seeds": 5
249
+ },
250
+ "50": {
251
+ "labels_total": 300,
252
+ "acc_mean": 0.8561927383780116,
253
+ "acc_std": 0.012688149897945202,
254
+ "macro_f1_mean": 0.8552664706192152,
255
+ "macro_f1_std": 0.012644973028308748,
256
+ "seeds": 5
257
+ },
258
+ "all": {
259
+ "labels_total": 5967,
260
+ "acc_mean": 0.8920936545639634,
261
+ "acc_std": 0.0,
262
+ "macro_f1_mean": 0.8917543014154136,
263
+ "macro_f1_std": 0.0,
264
+ "seeds": 1
265
+ }
266
+ },
267
+ "stats": {
268
+ "2": {
269
+ "labels_total": 12,
270
+ "acc_mean": 0.6524601289446895,
271
+ "acc_std": 0.03437673651432141,
272
+ "macro_f1_mean": 0.6315164863953293,
273
+ "macro_f1_std": 0.02988008478660784,
274
+ "seeds": 5
275
+ },
276
+ "10": {
277
+ "labels_total": 60,
278
+ "acc_mean": 0.8060400407193757,
279
+ "acc_std": 0.02669682260710852,
280
+ "macro_f1_mean": 0.8023744003419736,
281
+ "macro_f1_std": 0.028293967458807565,
282
+ "seeds": 5
283
+ },
284
+ "50": {
285
+ "labels_total": 300,
286
+ "acc_mean": 0.862029182219206,
287
+ "acc_std": 0.006839292941890257,
288
+ "macro_f1_mean": 0.859469912983073,
289
+ "macro_f1_std": 0.006717255422007592,
290
+ "seeds": 5
291
+ },
292
+ "all": {
293
+ "labels_total": 5967,
294
+ "acc_mean": 0.8927723108245673,
295
+ "acc_std": 0.0,
296
+ "macro_f1_mean": 0.8912415144172424,
297
+ "macro_f1_std": 0.0,
298
+ "seeds": 1
299
+ }
300
+ }
301
+ },
302
+ "confusion_ssl_10_per_class": [
303
+ [
304
+ 473,
305
+ 0,
306
+ 22,
307
+ 1,
308
+ 0,
309
+ 0
310
+ ],
311
+ [
312
+ 51,
313
+ 390,
314
+ 19,
315
+ 11,
316
+ 0,
317
+ 0
318
+ ],
319
+ [
320
+ 9,
321
+ 3,
322
+ 408,
323
+ 0,
324
+ 0,
325
+ 0
326
+ ],
327
+ [
328
+ 0,
329
+ 3,
330
+ 0,
331
+ 396,
332
+ 90,
333
+ 2
334
+ ],
335
+ [
336
+ 0,
337
+ 0,
338
+ 0,
339
+ 118,
340
+ 414,
341
+ 0
342
+ ],
343
+ [
344
+ 25,
345
+ 0,
346
+ 0,
347
+ 49,
348
+ 0,
349
+ 463
350
+ ]
351
+ ],
352
+ "failure_examples": {
353
+ "setting": "SSL probe, 10 labels/class, seed 0",
354
+ "n_wrong": 403,
355
+ "first": [
356
+ {
357
+ "test_window": 31,
358
+ "subject": 2,
359
+ "true": "sitting",
360
+ "predicted": "standing"
361
+ },
362
+ {
363
+ "test_window": 32,
364
+ "subject": 2,
365
+ "true": "sitting",
366
+ "predicted": "standing"
367
+ },
368
+ {
369
+ "test_window": 45,
370
+ "subject": 2,
371
+ "true": "sitting",
372
+ "predicted": "standing"
373
+ },
374
+ {
375
+ "test_window": 46,
376
+ "subject": 2,
377
+ "true": "sitting",
378
+ "predicted": "standing"
379
+ },
380
+ {
381
+ "test_window": 47,
382
+ "subject": 2,
383
+ "true": "sitting",
384
+ "predicted": "standing"
385
+ },
386
+ {
387
+ "test_window": 48,
388
+ "subject": 2,
389
+ "true": "sitting",
390
+ "predicted": "standing"
391
+ },
392
+ {
393
+ "test_window": 49,
394
+ "subject": 2,
395
+ "true": "sitting",
396
+ "predicted": "standing"
397
+ },
398
+ {
399
+ "test_window": 50,
400
+ "subject": 2,
401
+ "true": "sitting",
402
+ "predicted": "standing"
403
+ },
404
+ {
405
+ "test_window": 51,
406
+ "subject": 2,
407
+ "true": "sitting",
408
+ "predicted": "standing"
409
+ },
410
+ {
411
+ "test_window": 52,
412
+ "subject": 2,
413
+ "true": "sitting",
414
+ "predicted": "standing"
415
+ },
416
+ {
417
+ "test_window": 53,
418
+ "subject": 2,
419
+ "true": "sitting",
420
+ "predicted": "standing"
421
+ },
422
+ {
423
+ "test_window": 54,
424
+ "subject": 2,
425
+ "true": "sitting",
426
+ "predicted": "standing"
427
+ },
428
+ {
429
+ "test_window": 302,
430
+ "subject": 4,
431
+ "true": "standing",
432
+ "predicted": "sitting"
433
+ },
434
+ {
435
+ "test_window": 303,
436
+ "subject": 4,
437
+ "true": "standing",
438
+ "predicted": "sitting"
439
+ },
440
+ {
441
+ "test_window": 305,
442
+ "subject": 4,
443
+ "true": "standing",
444
+ "predicted": "sitting"
445
+ },
446
+ {
447
+ "test_window": 306,
448
+ "subject": 4,
449
+ "true": "standing",
450
+ "predicted": "sitting"
451
+ },
452
+ {
453
+ "test_window": 307,
454
+ "subject": 4,
455
+ "true": "standing",
456
+ "predicted": "sitting"
457
+ },
458
+ {
459
+ "test_window": 308,
460
+ "subject": 4,
461
+ "true": "standing",
462
+ "predicted": "sitting"
463
+ },
464
+ {
465
+ "test_window": 309,
466
+ "subject": 4,
467
+ "true": "standing",
468
+ "predicted": "sitting"
469
+ },
470
+ {
471
+ "test_window": 310,
472
+ "subject": 4,
473
+ "true": "standing",
474
+ "predicted": "sitting"
475
+ },
476
+ {
477
+ "test_window": 311,
478
+ "subject": 4,
479
+ "true": "standing",
480
+ "predicted": "sitting"
481
+ },
482
+ {
483
+ "test_window": 312,
484
+ "subject": 4,
485
+ "true": "standing",
486
+ "predicted": "sitting"
487
+ },
488
+ {
489
+ "test_window": 313,
490
+ "subject": 4,
491
+ "true": "standing",
492
+ "predicted": "sitting"
493
+ },
494
+ {
495
+ "test_window": 314,
496
+ "subject": 4,
497
+ "true": "standing",
498
+ "predicted": "sitting"
499
+ },
500
+ {
501
+ "test_window": 315,
502
+ "subject": 4,
503
+ "true": "standing",
504
+ "predicted": "sitting"
505
+ },
506
+ {
507
+ "test_window": 316,
508
+ "subject": 4,
509
+ "true": "standing",
510
+ "predicted": "sitting"
511
+ },
512
+ {
513
+ "test_window": 317,
514
+ "subject": 4,
515
+ "true": "standing",
516
+ "predicted": "sitting"
517
+ },
518
+ {
519
+ "test_window": 319,
520
+ "subject": 4,
521
+ "true": "standing",
522
+ "predicted": "sitting"
523
+ },
524
+ {
525
+ "test_window": 320,
526
+ "subject": 4,
527
+ "true": "standing",
528
+ "predicted": "sitting"
529
+ },
530
+ {
531
+ "test_window": 321,
532
+ "subject": 4,
533
+ "true": "standing",
534
+ "predicted": "sitting"
535
+ }
536
+ ]
537
+ },
538
+ "tests": {
539
+ "reload": {
540
+ "returncode": 0,
541
+ "seconds": 0.18,
542
+ "process": "fresh python -E subprocess, cwd = temporary copy of the model folder",
543
+ "passed": true,
544
+ "max_abs_diff": 0.0,
545
+ "n_windows": 8
546
+ },
547
+ "node_parity": {
548
+ "cases": 4,
549
+ "failed": 0,
550
+ "maxDiff": 6.106226635438361e-16,
551
+ "results": [
552
+ {
553
+ "name": "case0",
554
+ "maxDiff": 1.7780915628762273e-17
555
+ },
556
+ {
557
+ "name": "case1",
558
+ "maxDiff": 1.1102230246251565e-16
559
+ },
560
+ {
561
+ "name": "case2",
562
+ "maxDiff": 6.106226635438361e-16
563
+ },
564
+ {
565
+ "name": "case3",
566
+ "maxDiff": 6.245004513516506e-17
567
+ }
568
+ ],
569
+ "ms": 137,
570
+ "runtime": "node v22.23.4",
571
+ "status": "passed"
572
+ },
573
+ "browser": {
574
+ "status": "skipped",
575
+ "reason": "playwright not installed"
576
+ }
577
+ },
578
+ "not_run": [
579
+ "Only one pre-training seed was run; variance over pre-training seeds is not reported."
580
+ ]
581
+ }
index.html CHANGED
@@ -1,19 +1,50 @@
1
  <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  </html>
 
1
  <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <link rel="icon" href="data:,">
7
+ <title>Next Signal</title>
8
+ <link rel="stylesheet" href="style.css">
9
+ </head>
10
+ <body>
11
+ <main>
12
+ <header>
13
+ <h1>Next Signal</h1>
14
+ <p>An encoder learned from <em>unlabeled</em> smartphone sensor windows, tested with a frozen linear probe. Pick a window from people the model never saw, distort it, or upload your own; three representations classify it in your browser.</p>
15
+ <div class="badges"><span class="badge">Live in-browser inference</span><span class="badge">UCI HAR (CC BY 4.0)</span><span class="badge">Test subjects held out</span><a class="badge" href="https://huggingface.co/thelamapi/Next-Signal">Model repo</a></div>
16
+ </header>
17
+ <div class="grid">
18
+ <section class="card">
19
+ <h2>Input window (2.56 s, 9 channels)</h2>
20
+ <label for="win">Held-out test window</label><select id="win"></select>
21
+ <label for="file">…or upload a CSV: 128 rows × 9 columns in the UCI HAR channel order</label><input type="file" id="file" accept=".csv,.txt">
22
+ <label for="noise">Added noise (× channel std): <b id="noise_v"></b></label><input type="range" id="noise" min="0" max="1.5" step="0.1" value="0">
23
+ <label for="scale">Amplitude scale: <b id="scale_v"></b></label><input type="range" id="scale" min="0.5" max="1.5" step="0.05" value="1">
24
+ <label for="shift">Time shift (samples): <b id="shift_v"></b></label><input type="range" id="shift" min="0" max="127" step="1" value="0">
25
+ <label for="budget">Probe trained with labels per class</label>
26
+ <select id="budget"><option value="2">2</option><option value="10" selected>10</option><option value="50">50</option></select>
27
+ <p class="status" id="status"></p>
28
+ </section>
29
+ <section class="card">
30
+ <h2>Signal</h2>
31
+ <select id="chans" style="max-width:220px"><option value="acc">body acceleration</option><option value="gyro">gyroscope</option><option value="total">total acceleration</option></select>
32
+ <canvas id="sig" style="margin-top:6px"></canvas><div id="sigl"></div>
33
+ <p class="small muted" id="truth"></p>
34
+ </section>
35
+ </div>
36
+ <div class="grid" style="margin-top:12px">
37
+ <section class="card"><h2>Self-supervised encoder + probe</h2><div id="p_ssl"></div></section>
38
+ <section class="card"><h2>Random encoder + probe</h2><div id="p_rand"></div></section>
39
+ <section class="card"><h2>Hand-made statistics + probe</h2><div id="p_stats"></div></section>
40
+ </div>
41
+ <p class="small muted">Inference took <span id="ms">–</span> ms for all three. Every probe saw the same labeled windows.</p>
42
+ <section class="card" style="margin-top:12px"><h2>Recorded benchmark</h2><img src="preview.png" alt="Probe accuracy vs labels" style="width:100%;border-radius:8px"></section>
43
+ <footer>Research demo on public smartphone data from 30 volunteers; not a medical or safety device.</footer>
44
+ </main>
45
+ <script src="nlrt.js"></script>
46
+ <script src="nlui.js"></script>
47
+ <script src="core.js"></script>
48
+ <script src="app.js"></script>
49
+ </body>
50
  </html>
model.json ADDED
The diff for this file is too large to render. See raw diff
 
nlrt.js ADDED
@@ -0,0 +1,219 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* nlrt.js: browser and Node twin of nlrt.py. Batch size 1, float64 math. */
2
+ (function (root, factory) {
3
+ if (typeof module === "object" && module.exports) module.exports = factory();
4
+ else root.NLRT = factory();
5
+ })(typeof self !== "undefined" ? self : this, function () {
6
+ "use strict";
7
+
8
+ function b64d(s) {
9
+ let bin;
10
+ if (typeof atob === "function") bin = atob(s);
11
+ else bin = Buffer.from(s, "base64").toString("binary");
12
+ const bytes = new Uint8Array(bin.length);
13
+ for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
14
+ return new Float32Array(bytes.buffer);
15
+ }
16
+
17
+ function decode(spec) {
18
+ for (const name in spec.nets || {}) {
19
+ for (const L of spec.nets[name]) {
20
+ if (typeof L.w === "string") L.w = b64d(L.w);
21
+ if (typeof L.b === "string") L.b = b64d(L.b);
22
+ }
23
+ }
24
+ for (const name in spec.tensors || {}) {
25
+ const t = spec.tensors[name];
26
+ if (typeof t.data === "string") t.data = b64d(t.data);
27
+ }
28
+ return spec;
29
+ }
30
+
31
+ async function loadModel(url) {
32
+ const r = await fetch(url);
33
+ if (!r.ok) throw new Error("cannot load " + url);
34
+ return decode(await r.json());
35
+ }
36
+
37
+ function pair(v) { return Array.isArray(v) ? v : [v, v]; }
38
+
39
+ // Tensor: {shape: [D]} or {shape: [C, H, W]} or {shape: [C, L]}, data: Float64Array
40
+ function T(shape, data) {
41
+ const n = shape.reduce((a, b) => a * b, 1);
42
+ return { shape: shape.slice(), data: data || new Float64Array(n) };
43
+ }
44
+
45
+ function as3(x) {
46
+ if (x.shape.length === 2) return { t: T([x.shape[0], 1, x.shape[1]], x.data), sq: true };
47
+ return { t: x, sq: false };
48
+ }
49
+
50
+ function conv2d(x0, L) {
51
+ const { t: x, sq } = as3(x0);
52
+ const [C, H, W] = x.shape;
53
+ const [O, I, KH, KW] = L.w_shape;
54
+ const [sh, sw] = pair(L.stride), [ph, pw] = pair(L.pad);
55
+ const OH = Math.floor((H + 2 * ph - KH) / sh) + 1;
56
+ const OW = Math.floor((W + 2 * pw - KW) / sw) + 1;
57
+ const out = new Float64Array(O * OH * OW);
58
+ const w = L.w, b = L.b, xd = x.data;
59
+ for (let o = 0; o < O; o++) {
60
+ for (let oy = 0; oy < OH; oy++) {
61
+ for (let ox = 0; ox < OW; ox++) {
62
+ let s = b[o];
63
+ for (let c = 0; c < I; c++) {
64
+ for (let ky = 0; ky < KH; ky++) {
65
+ const iy = oy * sh + ky - ph;
66
+ if (iy < 0 || iy >= H) continue;
67
+ const wb = ((o * I + c) * KH + ky) * KW, xb = (c * H + iy) * W;
68
+ for (let kx = 0; kx < KW; kx++) {
69
+ const ix = ox * sw + kx - pw;
70
+ if (ix < 0 || ix >= W) continue;
71
+ s += w[wb + kx] * xd[xb + ix];
72
+ }
73
+ }
74
+ }
75
+ out[(o * OH + oy) * OW + ox] = s;
76
+ }
77
+ }
78
+ }
79
+ return sq ? T([O, OW], out) : T([O, OH, OW], out);
80
+ }
81
+
82
+ function maxpool(x0, L) {
83
+ const { t: x, sq } = as3(x0);
84
+ const [C, H, W] = x.shape, [kh, kw] = pair(L.k);
85
+ const OH = Math.floor(H / kh), OW = Math.floor(W / kw);
86
+ const out = new Float64Array(C * OH * OW);
87
+ for (let c = 0; c < C; c++)
88
+ for (let oy = 0; oy < OH; oy++)
89
+ for (let ox = 0; ox < OW; ox++) {
90
+ let m = -Infinity;
91
+ for (let i = 0; i < kh; i++)
92
+ for (let j = 0; j < kw; j++) {
93
+ const v = x.data[(c * H + oy * kh + i) * W + ox * kw + j];
94
+ if (v > m) m = v;
95
+ }
96
+ out[(c * OH + oy) * OW + ox] = m;
97
+ }
98
+ return sq ? T([C, OW], out) : T([C, OH, OW], out);
99
+ }
100
+
101
+ function linear(x, L) {
102
+ const [O, I] = L.w_shape;
103
+ const out = new Float64Array(O);
104
+ for (let o = 0; o < O; o++) {
105
+ let s = L.b[o];
106
+ const base = o * I;
107
+ for (let i = 0; i < I; i++) s += L.w[base + i] * x.data[i];
108
+ out[o] = s;
109
+ }
110
+ return T([O], out);
111
+ }
112
+
113
+ function map(x, f) {
114
+ const d = new Float64Array(x.data.length);
115
+ for (let i = 0; i < d.length; i++) d[i] = f(x.data[i]);
116
+ return T(x.shape, d);
117
+ }
118
+
119
+ function forward(layers, x) {
120
+ if (!(x.data instanceof Float64Array)) x = T(x.shape, Float64Array.from(x.data));
121
+ for (const L of layers) {
122
+ switch (L.type) {
123
+ case "linear": x = linear(x, L); break;
124
+ case "conv2d": x = conv2d(x, L); break;
125
+ case "relu": x = map(x, v => (v > 0 ? v : 0)); break;
126
+ case "tanh": x = map(x, Math.tanh); break;
127
+ case "sigmoid": x = map(x, v => 1 / (1 + Math.exp(-v))); break;
128
+ case "maxpool": x = maxpool(x, L); break;
129
+ case "gap": {
130
+ const C = x.shape[0], n = x.data.length / C, d = new Float64Array(C);
131
+ for (let c = 0; c < C; c++) { let s = 0; for (let i = 0; i < n; i++) s += x.data[c * n + i]; d[c] = s / n; }
132
+ x = T([C], d); break;
133
+ }
134
+ case "flatten": x = T([x.data.length], x.data); break;
135
+ case "reshape": x = T(L.shape, x.data); break;
136
+ case "upsample": {
137
+ const s = L.s;
138
+ if (x.shape.length === 3) {
139
+ const [C, H, W] = x.shape, d = new Float64Array(C * H * s * W * s);
140
+ for (let c = 0; c < C; c++)
141
+ for (let y = 0; y < H * s; y++)
142
+ for (let xx = 0; xx < W * s; xx++)
143
+ d[(c * H * s + y) * W * s + xx] = x.data[(c * H + Math.floor(y / s)) * W + Math.floor(xx / s)];
144
+ x = T([C, H * s, W * s], d);
145
+ } else {
146
+ const [C, Ln] = x.shape, d = new Float64Array(C * Ln * s);
147
+ for (let c = 0; c < C; c++) for (let i = 0; i < Ln * s; i++) d[c * Ln * s + i] = x.data[c * Ln + Math.floor(i / s)];
148
+ x = T([C, Ln * s], d);
149
+ }
150
+ break;
151
+ }
152
+ case "l2norm": {
153
+ let s = 0; for (const v of x.data) s += v * v;
154
+ const n = Math.max(Math.sqrt(s), 1e-12);
155
+ x = map(x, v => v / n); break;
156
+ }
157
+ default: throw new Error("unknown layer " + L.type);
158
+ }
159
+ }
160
+ return x;
161
+ }
162
+
163
+ function mulberry32(seed) {
164
+ let a = seed >>> 0;
165
+ const random = function () {
166
+ a = (a + 0x6D2B79F5) >>> 0;
167
+ let t = a;
168
+ t = Math.imul(t ^ (t >>> 15), t | 1) >>> 0;
169
+ t = (t ^ ((t + (Math.imul(t ^ (t >>> 7), t | 61) >>> 0)) >>> 0)) >>> 0;
170
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
171
+ };
172
+ return {
173
+ random,
174
+ uniform: (lo, hi) => lo + (hi - lo) * random(),
175
+ randint: n => Math.floor(random() * n),
176
+ normal: () => { const u = Math.max(random(), 1e-12), v = random(); return Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v); },
177
+ };
178
+ }
179
+
180
+ function softmax(arr) {
181
+ let m = -Infinity; for (const v of arr) if (v > m) m = v;
182
+ const e = Array.from(arr, v => Math.exp(v - m)); const s = e.reduce((a, b) => a + b, 0);
183
+ return e.map(v => v / s);
184
+ }
185
+
186
+ function argmax(arr) { let k = 0; for (let i = 1; i < arr.length; i++) if (arr[i] > arr[k]) k = i; return k; }
187
+
188
+ // Deep comparison used by parity tests: numbers within tol, everything else exact.
189
+ function compare(got, want, tol, path, acc) {
190
+ acc = acc || { maxDiff: 0, mismatches: [] };
191
+ path = path || "$";
192
+ if (typeof want === "number") {
193
+ const d = Math.abs(Number(got) - want);
194
+ if (!(d <= tol * Math.max(1, Math.abs(want)))) acc.mismatches.push(path + ": got " + got + " want " + want);
195
+ if (isFinite(d)) acc.maxDiff = Math.max(acc.maxDiff, d);
196
+ } else if (Array.isArray(want)) {
197
+ if (!got || got.length !== want.length) acc.mismatches.push(path + ": length " + (got && got.length) + " vs " + want.length);
198
+ else for (let i = 0; i < want.length; i++) compare(got[i], want[i], tol, path + "[" + i + "]", acc);
199
+ } else if (want && typeof want === "object") {
200
+ for (const k in want) compare(got ? got[k] : undefined, want[k], tol, path + "." + k, acc);
201
+ } else if (got !== want) acc.mismatches.push(path + ": got " + got + " want " + want);
202
+ return acc;
203
+ }
204
+
205
+ function runParity(core, ctx, parity) {
206
+ const results = [];
207
+ let maxDiff = 0, failed = 0;
208
+ for (const c of parity.cases) {
209
+ const out = core.runCase(ctx, c.input);
210
+ const r = compare(out, c.expected, c.tol != null ? c.tol : parity.tol);
211
+ maxDiff = Math.max(maxDiff, r.maxDiff);
212
+ if (r.mismatches.length) failed++;
213
+ results.push({ name: c.name, maxDiff: r.maxDiff, mismatches: r.mismatches.slice(0, 5) });
214
+ }
215
+ return { cases: parity.cases.length, failed, maxDiff, results };
216
+ }
217
+
218
+ return { b64d, decode, loadModel, T, forward, mulberry32, softmax, argmax, compare, runParity };
219
+ });
nlui.js ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* nlui.js: small DOM and canvas helpers shared by Next Lab Spaces. */
2
+ (function (root) {
3
+ "use strict";
4
+ const $ = id => document.getElementById(id);
5
+ function h(tag, attrs, ...kids) {
6
+ const e = document.createElement(tag);
7
+ for (const k in attrs || {}) {
8
+ if (k === "class") e.className = attrs[k];
9
+ else if (k.startsWith("on")) e.addEventListener(k.slice(2), attrs[k]);
10
+ else if (k === "html") e.innerHTML = attrs[k];
11
+ else e.setAttribute(k, attrs[k]);
12
+ }
13
+ for (const c of kids.flat()) if (c != null) e.append(c.nodeType ? c : document.createTextNode(String(c)));
14
+ return e;
15
+ }
16
+ const css = name => getComputedStyle(document.documentElement).getPropertyValue(name).trim();
17
+ const fmt = (v, d = 3) => (v == null || !isFinite(v) ? "–" : Number(v).toFixed(d));
18
+
19
+ function grayImage(canvas, data, w, h, scale, opts) {
20
+ opts = opts || {};
21
+ canvas.width = w * scale; canvas.height = h * scale;
22
+ const ctx = canvas.getContext("2d");
23
+ const img = ctx.createImageData(w, h);
24
+ let lo = opts.lo != null ? opts.lo : 0, hi = opts.hi != null ? opts.hi : 1;
25
+ for (let i = 0; i < w * h; i++) {
26
+ const v = Math.max(0, Math.min(255, Math.round(255 * (data[i] - lo) / (hi - lo || 1))));
27
+ img.data[4 * i] = img.data[4 * i + 1] = img.data[4 * i + 2] = v; img.data[4 * i + 3] = 255;
28
+ }
29
+ const tmp = document.createElement("canvas"); tmp.width = w; tmp.height = h;
30
+ tmp.getContext("2d").putImageData(img, 0, 0);
31
+ ctx.imageSmoothingEnabled = false;
32
+ ctx.drawImage(tmp, 0, 0, w * scale, h * scale);
33
+ return ctx;
34
+ }
35
+
36
+ function rgbImage(canvas, data, w, h, scale) { // data: CHW in [0,1]
37
+ canvas.width = w * scale; canvas.height = h * scale;
38
+ const ctx = canvas.getContext("2d");
39
+ const img = ctx.createImageData(w, h), n = w * h;
40
+ for (let i = 0; i < n; i++) {
41
+ for (let c = 0; c < 3; c++) img.data[4 * i + c] = Math.max(0, Math.min(255, Math.round(255 * data[c * n + i])));
42
+ img.data[4 * i + 3] = 255;
43
+ }
44
+ const tmp = document.createElement("canvas"); tmp.width = w; tmp.height = h;
45
+ tmp.getContext("2d").putImageData(img, 0, 0);
46
+ ctx.imageSmoothingEnabled = false;
47
+ ctx.drawImage(tmp, 0, 0, w * scale, h * scale);
48
+ return ctx;
49
+ }
50
+
51
+ // series: [{name, color, points: [[x,y],...], dash}]
52
+ function lineChart(canvas, series, opts) {
53
+ opts = opts || {};
54
+ const W = canvas.clientWidth || 600, H = opts.height || 240, dpr = window.devicePixelRatio || 1;
55
+ canvas.width = W * dpr; canvas.height = H * dpr; canvas.style.height = H + "px";
56
+ const ctx = canvas.getContext("2d"); ctx.scale(dpr, dpr);
57
+ const m = { l: 46, r: 10, t: 10, b: 34 };
58
+ let xs = [], ys = [];
59
+ for (const s of series) for (const [x, y] of s.points) { if (isFinite(y)) { xs.push(x); ys.push(y); } }
60
+ if (opts.hlines) for (const hl of opts.hlines) ys.push(hl.y);
61
+ let x0 = opts.xmin != null ? opts.xmin : Math.min(...xs), x1 = opts.xmax != null ? opts.xmax : Math.max(...xs);
62
+ let y0 = opts.ymin != null ? opts.ymin : Math.min(...ys), y1 = opts.ymax != null ? opts.ymax : Math.max(...ys);
63
+ if (y1 - y0 < 1e-9) { y0 -= 1; y1 += 1; }
64
+ if (x1 - x0 < 1e-9) x1 = x0 + 1;
65
+ const X = x => m.l + (x - x0) / (x1 - x0) * (W - m.l - m.r);
66
+ const Y = y => H - m.b - (y - y0) / (y1 - y0) * (H - m.t - m.b);
67
+ ctx.font = "11px system-ui"; ctx.fillStyle = css("--muted"); ctx.strokeStyle = css("--line"); ctx.lineWidth = 1;
68
+ for (let i = 0; i <= 4; i++) {
69
+ const y = y0 + (y1 - y0) * i / 4;
70
+ ctx.beginPath(); ctx.moveTo(m.l, Y(y)); ctx.lineTo(W - m.r, Y(y)); ctx.stroke();
71
+ ctx.fillText(Math.abs(y) >= 100 ? y.toFixed(0) : y.toFixed(2), 4, Y(y) + 4);
72
+ }
73
+ for (let i = 0; i <= 4; i++) {
74
+ const x = x0 + (x1 - x0) * i / 4;
75
+ ctx.fillText(Math.abs(x) >= 100 || Number.isInteger(x) ? x.toFixed(0) : x.toFixed(1), X(x) - 8, H - m.b + 14);
76
+ }
77
+ if (opts.xlabel) ctx.fillText(opts.xlabel, W / 2 - 30, H - 4);
78
+ if (opts.hlines) for (const hl of opts.hlines) {
79
+ ctx.setLineDash([4, 4]); ctx.strokeStyle = hl.color || css("--muted");
80
+ ctx.beginPath(); ctx.moveTo(m.l, Y(hl.y)); ctx.lineTo(W - m.r, Y(hl.y)); ctx.stroke(); ctx.setLineDash([]);
81
+ }
82
+ for (const s of series) {
83
+ ctx.strokeStyle = s.color; ctx.lineWidth = s.width || 2; ctx.setLineDash(s.dash || []);
84
+ ctx.beginPath(); let first = true;
85
+ for (const [x, y] of s.points) { if (!isFinite(y)) continue; if (first) ctx.moveTo(X(x), Y(y)); else ctx.lineTo(X(x), Y(y)); first = false; }
86
+ ctx.stroke(); ctx.setLineDash([]);
87
+ }
88
+ return { X, Y, ctx };
89
+ }
90
+
91
+ function legend(el, series) {
92
+ el.innerHTML = "";
93
+ for (const s of series) el.append(h("span", { class: "small", style: "margin-right:12px;white-space:nowrap" },
94
+ h("span", { style: `display:inline-block;width:14px;height:3px;background:${s.color};vertical-align:middle;margin-right:4px` }), s.name));
95
+ }
96
+
97
+ async function loadAssets(core) {
98
+ const assets = {};
99
+ for (const name of core.ASSETS) {
100
+ const r = await fetch(name);
101
+ if (!r.ok) throw new Error("cannot load " + name);
102
+ assets[name] = await r.json();
103
+ }
104
+ return assets;
105
+ }
106
+
107
+ // Standard bootstrap: load assets, init core, expose __parity for automated tests.
108
+ async function boot(core, start) {
109
+ try {
110
+ const assets = await loadAssets(core);
111
+ const ctx = core.init(assets);
112
+ const parity = await (await fetch("parity_cases.json")).json();
113
+ root.__parity = () => NLRT.runParity(core, ctx, parity);
114
+ await start(ctx, assets);
115
+ root.__ready = true;
116
+ } catch (e) {
117
+ console.error(e); root.__error = String(e && e.stack || e);
118
+ const s = document.getElementById("status"); if (s) s.textContent = "Failed to start: " + e;
119
+ }
120
+ }
121
+
122
+ root.NLUI = { $, h, css, fmt, grayImage, rgbImage, lineChart, legend, boot };
123
+ })(typeof self !== "undefined" ? self : this);
parity_cases.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"tol": 1e-06, "cases": [{"name": "case0", "input": {"window": 0, "noise": 0, "scale": 1, "shift": 0, "seed": 17, "budget": 10}, "expected": {"ssl": [0.9991489973740404, 9.91264997043895e-05, 5.409943946579194e-05, 0.00014131738313891208, 0.0005175318808271934, 3.892742282316641e-05], "random": [0.9894982789334876, 0.0024839295440291283, 0.0031167586445251167, 0.001785525913275588, 0.002320396668245648, 0.0007951102964371029], "stats": [0.9520927746756169, 0.03580613121858015, 0.002221163100050602, 3.0437421221261982e-05, 0.009352311858938409, 0.000497181725592683], "x_c0_t5": -0.2409}}, {"name": "case1", "input": {"window": 5, "noise": 0.5, "scale": 1.3, "shift": 40, "seed": 17, "budget": 2}, "expected": {"ssl": [0.17026187749522584, 0.7033762600449321, 0.10181295172397017, 0.013966553240775632, 0.00443905832180822, 0.006143299173288118], "random": [0.3566920240727889, 0.37989140579757025, 0.24062432153659724, 0.008713668457262399, 0.0024711947294342204, 0.011607385406346999], "stats": [0.9799609395424483, 0.013362566172893161, 0.006601504769300604, 6.080124801237526e-05, 1.0030172852003693e-05, 4.158094493426839e-06], "x_c0_t5": -0.03215677550252982}}, {"name": "case2", "input": {"window": 11, "noise": 1.0, "scale": 0.7, "shift": 100, "seed": 17, "budget": 50}, "expected": {"ssl": [0.22349453272009193, 0.031400601016802324, 0.002522191569925669, 0.00027370340283973263, 0.7213289983971857, 0.02097997289315481], "random": [0.29008991749647517, 0.4527040827418937, 0.0038635006254615504, 0.14023705349050053, 0.05913060728499002, 0.053974838360679074], "stats": [0.999934365292505, 4.240528341396469e-09, 2.9578191770136953e-09, 5.49052976161859e-05, 1.3953403803975671e-06, 9.326871150988586e-06], "x_c0_t5": 0.006735880919457645}}, {"name": "case3", "input": {"window": 17, "noise": 0.2, "scale": 1.0, "shift": 5, "seed": 17, "budget": 10}, "expected": {"ssl": [0.008871277530342581, 3.875283376430546e-05, 0.0002794351845043425, 0.003519615348484858, 0.0010574985655816445, 0.9862334205373223], "random": [0.02690391514329467, 0.0049322330784053806, 0.029017162114984247, 0.003373567922606025, 0.0005641995676908536, 0.9352089221730189], "stats": [1.0601806668936615e-05, 0.00042761203652521624, 3.0803424822087406e-09, 8.307056088453093e-07, 0.002271458648126763, 0.9972894937227277], "x_c0_t5": -0.0011843398917051901}}]}
preview.png ADDED
style.css CHANGED
@@ -1,28 +1,55 @@
1
- body {
2
- padding: 2rem;
3
- font-family: -apple-system, BlinkMacSystemFont, "Arial", sans-serif;
 
4
  }
5
-
6
- h1 {
7
- font-size: 16px;
8
- margin-top: 0;
9
- }
10
-
11
- p {
12
- color: rgb(107, 114, 128);
13
- font-size: 15px;
14
- margin-bottom: 10px;
15
- margin-top: 5px;
16
- }
17
-
18
- .card {
19
- max-width: 620px;
20
- margin: 0 auto;
21
- padding: 16px;
22
- border: 1px solid lightgray;
23
- border-radius: 16px;
24
- }
25
-
26
- .card p:last-child {
27
- margin-bottom: 0;
28
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ :root {
2
+ --bg: #f7f7f5; --panel: #ffffff; --ink: #1d1f22; --muted: #5f6670; --line: #dcdfe3;
3
+ --accent: #0b6e4f; --accent-soft: #e3f1ec; --warn: #a4471b; --warn-soft: #fbeee6;
4
+ --blue: #2f5fb3; --gold: #b07d12; --red: #c0392b;
5
  }
6
+ @media (prefers-color-scheme: dark) {
7
+ :root {
8
+ --bg: #15171a; --panel: #1e2125; --ink: #eceef0; --muted: #a5acb5; --line: #33383e;
9
+ --accent: #4cc39a; --accent-soft: #1b3229; --warn: #f0a070; --warn-soft: #3a2519;
10
+ --blue: #7ea6f0; --gold: #e0b44c; --red: #f07a6c;
11
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  }
13
+ * { box-sizing: border-box; }
14
+ html, body { margin: 0; padding: 0; background: var(--bg); color: var(--ink); }
15
+ body { font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
16
+ main { max-width: 1040px; margin: 0 auto; padding: 16px; }
17
+ header h1 { font-size: 1.6rem; margin: 4px 0 2px; letter-spacing: -0.01em; }
18
+ header p { margin: 0 0 8px; color: var(--muted); max-width: 70ch; }
19
+ .badges { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0 12px; }
20
+ .badge { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); border: 1px solid var(--line); }
21
+ .badge.warn { background: var(--warn-soft); color: var(--warn); }
22
+ .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 12px; }
23
+ .card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; min-width: 0; }
24
+ .card h2 { font-size: 1.02rem; margin: 0 0 8px; }
25
+ .card h3 { font-size: .95rem; margin: 10px 0 6px; }
26
+ label { display: block; font-size: 13px; color: var(--muted); margin: 8px 0 3px; }
27
+ input[type=text], textarea, select { width: 100%; font: inherit; padding: 7px 9px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); }
28
+ textarea { min-height: 110px; resize: vertical; }
29
+ input[type=range] { width: 100%; }
30
+ button { font: inherit; padding: 7px 12px; border-radius: 8px; border: 1px solid var(--accent); background: var(--accent); color: #fff; cursor: pointer; }
31
+ button.ghost { background: transparent; color: var(--accent); }
32
+ button:disabled { opacity: .5; cursor: default; }
33
+ .row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
34
+ canvas { max-width: 100%; height: auto; display: block; border-radius: 6px; }
35
+ .card > canvas { width: 100%; }
36
+ .muted { color: var(--muted); }
37
+ .small { font-size: 13px; }
38
+ .status { font-size: 13px; color: var(--muted); min-height: 1.4em; }
39
+ table { border-collapse: collapse; width: 100%; font-size: 13px; }
40
+ th, td { text-align: left; padding: 4px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
41
+ th { color: var(--muted); font-weight: 600; }
42
+ td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
43
+ .scroll { overflow-x: auto; }
44
+ mark { background: #ffe58a; color: #1d1f22; padding: 0 2px; border-radius: 3px; }
45
+ mark.weak { background: #fff3c4; }
46
+ video { width: 100%; border-radius: 8px; }
47
+ footer { margin: 18px 0 8px; font-size: 13px; color: var(--muted); }
48
+ a { color: var(--blue); }
49
+ .chips { display: flex; flex-wrap: wrap; gap: 6px; }
50
+ .chip { border: 1px solid var(--line); border-radius: 999px; padding: 3px 10px; background: var(--bg); font-size: 13px; cursor: pointer; color: var(--ink); }
51
+ .chip.on { border-color: var(--accent); background: var(--accent-soft); }
52
+ .kpi { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 8px; }
53
+ .kpi div { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; }
54
+ .kpi b { display: block; font-size: 1.15rem; }
55
+ .kpi span { font-size: 12px; color: var(--muted); }
windows.json ADDED
The diff for this file is too large to render. See raw diff