-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpatch.html
More file actions
670 lines (646 loc) · 40.7 KB
/
Copy pathpatch.html
File metadata and controls
670 lines (646 loc) · 40.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
<title>Patch — a NeuronBench interrogation bench</title>
<style>
:root{
--scope:#05100c; --scope-grid:#123a2b; --scope-edge:#0c1f18;
--phosphor:#38f5a6; --phosphor-dim:#1c8a5f;
--cyan:#5ec8ff; --amber:#ffb454; --danger:#ff6b6b;
--bg:#e9efec; --panel:#ffffff; --panel-2:#f3f6f4; --line:#d3ded8;
--ink:#14201b; --ink-2:#465852; --ink-3:#7c8d86;
--chip:#eef3f0; --chip-ink:#33463f;
--shadow:0 1px 2px rgba(20,40,32,.06),0 8px 24px rgba(20,40,32,.06);
--mono:ui-monospace,"SF Mono","JetBrains Mono","Cascadia Code",Menlo,Consolas,monospace;
--sans:system-ui,-apple-system,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;
}
@media (prefers-color-scheme:dark){
:root{ --bg:#08110e; --panel:#0e1714; --panel-2:#0b1310; --line:#1e2c26;
--ink:#e7f0ec; --ink-2:#9db1a9; --ink-3:#6c8078; --chip:#152019; --chip-ink:#b6c9c0;
--shadow:0 1px 2px rgba(0,0,0,.4),0 10px 30px rgba(0,0,0,.35); }
}
:root[data-theme="dark"]{ --bg:#08110e; --panel:#0e1714; --panel-2:#0b1310; --line:#1e2c26;
--ink:#e7f0ec; --ink-2:#9db1a9; --ink-3:#6c8078; --chip:#152019; --chip-ink:#b6c9c0;
--shadow:0 1px 2px rgba(0,0,0,.4),0 10px 30px rgba(0,0,0,.35); }
:root[data-theme="light"]{ --bg:#e9efec; --panel:#ffffff; --panel-2:#f3f6f4; --line:#d3ded8;
--ink:#14201b; --ink-2:#465852; --ink-3:#7c8d86; --chip:#eef3f0; --chip-ink:#33463f;
--shadow:0 1px 2px rgba(20,40,32,.06),0 8px 24px rgba(20,40,32,.06); }
*{box-sizing:border-box}
html{-webkit-text-size-adjust:100%}
body{margin:0;background:var(--bg);color:var(--ink);font-family:var(--sans);line-height:1.5;
-webkit-font-smoothing:antialiased;font-size:15px}
.wrap{max-width:1160px;margin:0 auto;padding:22px 20px 72px}
h1,h2,h3{margin:0;text-wrap:balance;font-weight:650;letter-spacing:-.01em}
.mono{font-family:var(--mono);font-variant-numeric:tabular-nums}
.lab{font-family:var(--mono);text-transform:uppercase;letter-spacing:.14em;font-size:11px;color:var(--ink-3);font-weight:600}
button,select{font-family:inherit;cursor:pointer}
button:focus-visible,[tabindex]:focus-visible,input:focus-visible,select:focus-visible{outline:2px solid var(--phosphor);outline-offset:2px}
header{display:flex;justify-content:space-between;align-items:flex-start;gap:20px;flex-wrap:wrap;margin-bottom:8px}
.brand{display:flex;align-items:center;gap:13px}
.glyph{width:38px;height:38px;border-radius:9px;background:var(--scope);position:relative;flex:none;
box-shadow:inset 0 0 0 1px var(--scope-edge), 0 0 18px rgba(56,245,166,.15)}
.glyph svg{position:absolute;inset:0}
h1{font-size:20px}
.brand p{margin:2px 0 0;color:var(--ink-2);font-size:13px}
.toggle{display:flex;gap:2px;background:var(--panel-2);border:1px solid var(--line);border-radius:999px;padding:3px}
.toggle button{border:0;background:transparent;color:var(--ink-3);border-radius:999px;padding:5px 11px;font-size:12px;font-family:var(--mono);letter-spacing:.03em}
.toggle button[aria-pressed="true"]{background:var(--panel);color:var(--ink);box-shadow:var(--shadow)}
.intro{color:var(--ink-2);font-size:14.5px;max-width:78ch;margin:14px 0 20px}
.intro b{color:var(--ink);font-weight:600}
.intro code{font-family:var(--mono);font-size:12.5px;background:var(--chip);color:var(--chip-ink);padding:1px 5px;border-radius:5px}
.panel{background:var(--panel);border:1px solid var(--line);border-radius:14px;box-shadow:var(--shadow)}
.panel-h{display:flex;align-items:baseline;justify-content:space-between;gap:10px;padding:13px 16px 0}
.panel-h h2{font-size:14px;letter-spacing:.01em}
.panel-h .sub{color:var(--ink-2);font-size:12px}
.transduction{display:grid;grid-template-columns:1fr 1fr;gap:16px;margin-bottom:16px}
@media(max-width:820px){.transduction{grid-template-columns:1fr}}
.cards{display:flex;flex-direction:column;gap:11px;padding:13px 16px 16px}
.exp{display:grid;grid-template-columns:118px 1fr auto;gap:13px;align-items:center;background:var(--panel-2);
border:1px solid var(--line);border-radius:11px;padding:10px;cursor:pointer;transition:border-color .12s ease}
.exp:hover{border-color:var(--phosphor-dim)}
.exp.sel{border-color:var(--phosphor);box-shadow:inset 0 0 0 1px var(--phosphor)}
.exp canvas{width:118px;height:56px;border-radius:7px;display:block;background:var(--scope)}
.exp .meta{min-width:0}
.setting{display:flex;flex-wrap:wrap;gap:5px;margin-bottom:7px}
.chip{font-family:var(--mono);font-size:11px;background:var(--chip);color:var(--chip-ink);border-radius:6px;padding:2px 7px;white-space:nowrap}
.chip.do{background:color-mix(in srgb,var(--amber) 22%,transparent);color:var(--amber)}
:root[data-theme="light"] .chip.do{color:#a5620c}
.chip.proto{white-space:normal} /* long protocol names wrap instead of overflowing */
.feats{display:flex;gap:14px;flex-wrap:wrap}
.feat{display:flex;flex-direction:column;line-height:1.25}
.feat .k{font-family:var(--mono);font-size:9.5px;letter-spacing:.06em;text-transform:uppercase;color:var(--ink-3)}
.feat .v{font-family:var(--mono);font-size:15px;font-weight:600;font-variant-numeric:tabular-nums}
.feat.score-feat .v{color:var(--phosphor-dim)}
.exp .tag{align-self:flex-start;font-family:var(--mono);font-size:10px;letter-spacing:.1em;text-transform:uppercase;color:var(--ink-3)}
.reveal{border:1px solid var(--amber);color:var(--amber);background:transparent;border-radius:8px;padding:6px 10px;font-size:11.5px;font-family:var(--mono);letter-spacing:.03em}
.reveal:hover{background:color-mix(in srgb,var(--amber) 14%,transparent)}
.bench{display:grid;grid-template-columns:1.55fr 1fr;gap:16px}
@media(max-width:820px){.bench{grid-template-columns:1fr}}
.scopewrap{padding:14px 16px 16px}
.scope{width:100%;aspect-ratio:16/7.4;background:var(--scope);border-radius:10px;display:block;
box-shadow:inset 0 0 0 1px var(--scope-edge),inset 0 0 60px rgba(0,0,0,.5)}
.scope-cap{display:flex;justify-content:space-between;align-items:center;gap:10px;margin-top:10px}
.readout{display:flex;gap:18px;flex-wrap:wrap}
.readout .feat .v{font-size:19px}
.rack{padding:14px 16px 16px;display:flex;flex-direction:column;gap:13px}
.budget{display:flex;align-items:center;justify-content:space-between;gap:10px;background:var(--panel-2);border:1px solid var(--line);border-radius:10px;padding:9px 12px}
.pips{display:flex;gap:4px}
.pip{width:14px;height:14px;border-radius:4px;background:var(--phosphor);opacity:.9;transition:opacity .2s}
.pip.spent{background:var(--line);opacity:1}
select{width:100%;background:var(--panel-2);border:1px solid var(--line);border-radius:9px;padding:9px 10px;
color:var(--ink);font-family:var(--mono);font-size:12.5px}
.ctl{display:flex;flex-direction:column;gap:6px}
.ctl .row{display:flex;justify-content:space-between;align-items:baseline}
.ctl .val{font-family:var(--mono);font-weight:600;font-variant-numeric:tabular-nums}
.ctl[hidden]{display:none}
input[type=range]{-webkit-appearance:none;appearance:none;width:100%;height:5px;border-radius:99px;background:var(--line);outline:none;margin:5px 0}
input[type=range]::-webkit-slider-thumb{-webkit-appearance:none;width:17px;height:17px;border-radius:50%;background:var(--cyan);border:2px solid var(--panel);box-shadow:0 1px 3px rgba(0,0,0,.3);cursor:pointer}
input[type=range]::-moz-range-thumb{width:15px;height:15px;border-radius:50%;background:var(--cyan);border:2px solid var(--panel);cursor:pointer}
.blockers{display:grid;grid-template-columns:1fr 1fr;gap:7px}
.blk{display:flex;align-items:center;gap:8px;background:var(--panel-2);border:1px solid var(--line);border-radius:9px;padding:8px 10px;font-family:var(--mono);font-size:12.5px;cursor:pointer;user-select:none}
.blk input{width:15px;height:15px;accent-color:var(--amber)}
.blk .t{font-weight:600}.blk .d{color:var(--ink-3);font-size:10.5px}
.run{display:flex;gap:9px}
.run button{flex:1;border:0;border-radius:10px;padding:11px;font-weight:650;font-size:13.5px;font-family:var(--mono);letter-spacing:.02em}
.run .go{background:var(--phosphor);color:#04120b}
.run .go:hover{filter:brightness(1.06)}
.run .go:disabled{background:var(--line);color:var(--ink-3);cursor:not-allowed;filter:none}
.run .ghost{flex:0 0 auto;background:transparent;color:var(--ink-2);border:1px solid var(--line)}
.run .ghost:hover{border-color:var(--ink-3);color:var(--ink)}
.hint{color:var(--ink-3);font-size:11.5px;font-family:var(--mono);line-height:1.5}
.verdict{margin-top:2px;background:var(--panel-2);border:1px dashed var(--line);border-radius:10px;padding:11px 13px}
.verdict.show{border-style:solid;border-color:var(--phosphor)}
.verdict h3{font-size:12.5px;font-family:var(--mono);letter-spacing:.02em;margin-bottom:5px}
.verdict p{margin:0;color:var(--ink-2);font-size:13px}
.verdict .em{color:var(--phosphor);font-weight:600}
.verdict .choices{display:flex;gap:8px;margin-top:9px}
.verdict .choices button{flex:1;border:1px solid var(--line);background:var(--panel);color:var(--ink);border-radius:8px;padding:8px;font-family:var(--mono);font-size:12px}
.verdict .choices button:hover{border-color:var(--phosphor-dim)}
.lockbar{display:flex;align-items:center;gap:8px;background:color-mix(in srgb,var(--amber) 12%,var(--panel-2));border:1px solid var(--amber);border-radius:9px;padding:8px 10px;font-size:11.5px;color:var(--ink-2);font-family:var(--mono)}
.lockbar[hidden]{display:none}
.lockbar b{color:var(--ink)}
.predrow{display:flex;gap:8px;margin-top:2px;align-items:center}
.pfield{display:flex;flex-direction:column;line-height:1.2}
.pfield .k{font-family:var(--mono);font-size:9.5px;letter-spacing:.06em;text-transform:uppercase;color:var(--ink)}
.pfield input{width:52px;font-family:var(--mono);font-size:14px;font-variant-numeric:tabular-nums;background:var(--panel);border:1px solid var(--line);border-radius:5px;padding:3px 5px;color:var(--ink);text-align:right}
.pfield input:focus{border-color:var(--cyan);outline:none}
.pfield input::placeholder{color:var(--ink-2);opacity:1} /* the "?" — brighter, easier to read */
.score{font-family:var(--mono);font-size:11px;margin-top:4px}
.score.good{color:var(--phosphor)}.score.mid{color:var(--amber)}.score.bad{color:var(--danger)}
.cmp{display:flex;gap:16px;align-items:baseline;font-family:var(--mono)}
.cmp .clab{font-size:10px;letter-spacing:.06em;text-transform:uppercase;color:var(--ink-3)}
.cmp .clab.you{color:var(--cyan)}
.cmp .cv{font-size:15px;font-weight:600;font-variant-numeric:tabular-nums}
.cmp .cv.you{color:var(--cyan)}
.diffs{margin-top:16px;padding:16px}
.diffs h2{font-size:14px;margin-bottom:3px}
.diffs .sub{color:var(--ink-3);font-size:12.5px;margin-bottom:14px}
.diffgrid{display:grid;grid-template-columns:repeat(3,1fr);gap:12px}
@media(max-width:820px){.diffgrid{grid-template-columns:1fr 1fr}}
@media(max-width:520px){.diffgrid{grid-template-columns:1fr}}
.diff{border-left:2px solid var(--phosphor-dim);padding:2px 0 2px 12px}
.diff .k{font-family:var(--mono);font-size:11px;letter-spacing:.05em;color:var(--phosphor-dim);margin-bottom:4px}
.diff .arc{color:var(--ink-3);font-size:12px;margin-bottom:2px}
.diff .us{color:var(--ink);font-size:12.5px}
.foot{margin-top:26px;color:var(--ink-3);font-size:12px;font-family:var(--mono);text-align:center;line-height:1.6}
.foot a{color:var(--ink-2)}
</style>
<div class="wrap">
<header>
<div class="brand">
<span class="glyph"><svg viewBox="0 0 38 38" aria-hidden="true"><polyline points="3,22 9,22 12,9 15,31 18,15 22,22 35,22" fill="none" stroke="#38f5a6" stroke-width="1.6" stroke-linejoin="round" stroke-linecap="round"/></svg></span>
<div>
<h1>Patch<span style="color:var(--phosphor-dim)"> · </span><span style="font-weight:450;color:var(--ink-2)">a NeuronBench interrogation bench</span></h1>
<p>Identify a hidden ion-channel mechanism from a few experiments — then forecast the interventions.</p>
</div>
</div>
<div class="toggle" role="group" aria-label="Theme">
<button id="t-light" aria-pressed="false">Light</button>
<button id="t-dark" aria-pressed="false">Dark</button>
</div>
</header>
<p class="intro">
A mystery neuron is patched on the bench. It is either a plain <b>Na⁺/K⁺</b> spiker or that plus
<b>one extra membrane current</b> of unknown identity. In five of the six worlds the extra channel is
<b>silent under every textbook probe</b> — it fires identically to plain under standard steps — and is
revealed only by <b>one non-textbook protocol</b>. You have an experiment <b>budget</b>: run current-clamp
protocols (the 9-protocol menu, or a custom step) and channel blockers, read the <b>test-window spike
count</b> <code>n_test</code> (the scored observable), and work out the mechanism. Then <b>forecast the
spike count</b> for each held-out test intervention — the counterfactuals the benchmark actually scores.
Running a held-out test setting on the bench is locked (that would be peeking).
</p>
<div class="transduction">
<section class="panel">
<div class="panel-h"><h2>Training set</h2><span class="sub">given · protocol → trace → n_test</span></div>
<div class="cards" id="train"></div>
</section>
<section class="panel">
<div class="panel-h"><h2>Test set</h2><span class="sub">held-out · forecast n_test</span></div>
<div class="cards" id="test"></div>
</section>
</div>
<div class="bench">
<section class="panel scopewrap">
<div class="panel-h" style="padding:0 0 10px"><h2 id="scope-title">Oscilloscope</h2><span class="sub" id="scope-sub">pick a protocol, or select a card</span></div>
<canvas class="scope" id="scope" aria-label="Oscilloscope voltage trace"></canvas>
<div class="scope-cap"><div class="readout mono" id="readout"></div></div>
</section>
<section class="panel rack">
<div class="budget">
<div><div class="lab">experiment budget</div><div class="mono" id="budget-txt" style="font-size:13px;margin-top:2px">real recordings are costly</div></div>
<div class="pips" id="pips"></div>
</div>
<div class="ctl">
<span class="lab">protocol</span>
<select id="proto"></select>
</div>
<div class="ctl" id="amp-ctl" hidden>
<div class="row"><span class="lab">stimulus current</span><span class="val"><span id="amp-v">12</span> µA</span></div>
<input type="range" id="amp" min="0" max="24" step="1" value="12" aria-label="stimulus current amplitude">
</div>
<div class="ctl" id="dur-ctl" hidden>
<div class="row"><span class="lab">step duration</span><span class="val"><span id="dur-v">300</span> ms</span></div>
<input type="range" id="dur" min="40" max="400" step="10" value="300" aria-label="step duration">
</div>
<div>
<div class="lab" style="margin-bottom:7px">channel blockers · do(g=0)</div>
<div class="blockers" id="blockers"></div>
</div>
<div class="lockbar" id="lockbar" hidden>
<span>🔒 This matches a held-out <b>test</b> protocol — running it would be peeking.</span>
</div>
<div class="run">
<button class="go" id="run">Run on the cell</button>
<button class="ghost" id="newcell" title="A new mystery cell">New cell</button>
</div>
<div class="hint" id="hint">Choose a protocol; the scope previews the current command. “Run” measures the cell and logs the spike count.</div>
<div class="verdict" id="verdict">
<h3>Your call</h3>
<p>Probe the cell, then decide: is there a hidden current?</p>
<div class="choices" id="choices"></div>
</div>
</section>
</div>
<section class="panel" style="margin-top:16px">
<div class="panel-h"><h2>Experiment log</h2><span class="sub" id="log-sub">your recordings from this cell</span></div>
<div class="cards" id="log"><p class="hint" style="padding:0 0 4px">No experiments run yet.</p></div>
</section>
<section class="panel diffs">
<h2>The six worlds</h2>
<div class="sub">Each is plain Na⁺/K⁺ + one hidden current, silent under textbook probes, revealed by one protocol.</div>
<div class="diffgrid" id="worldgrid"></div>
</section>
<p class="foot">Simulated live in your browser with the NeuronBench Hodgkin–Huxley model (deterministic port of <span class="mono">worlds.py</span>).<br>
The scored observable is the test-window spike count; the game mirrors the benchmark's discovery-then-forecast loop.
· <a href="https://github.com/murphyk/neuronbench">github.com/murphyk/neuronbench</a></p>
</div>
<script>
"use strict";
/* ==================== NeuronBench HH model — deterministic port of neuronbench/worlds.py ==================== */
const ENa=50, EK=-77, EL=-54.4;
const clip=(x,a,b)=>x<a?a:x>b?b:x;
function ab(V){
const am=0.1*(V+40)/(1-Math.exp(-(V+40)/10)+1e-9), bm=4*Math.exp(-(V+65)/18);
const ah=0.07*Math.exp(-(V+65)/20), bh=1/(1+Math.exp(-(V+35)/10));
const an=0.01*(V+55)/(1-Math.exp(-(V+55)/10)+1e-9), bn=0.125*Math.exp(-(V+65)/80);
return [am,bm,ah,bh,an,bn];
}
const minf=(V,vh,k)=>1/(1+Math.exp(-(V-vh)/k));
const hinf=(V,vh,k)=>1/(1+Math.exp((V-vh)/k));
// Chan dataclass -> object. mk<0 => hyperpolarisation-activated; hvh set => has inactivation.
const CH={
Z: {name:"Z", g:4.0, E:120, mvh:-57, mk:5, mtau:4, mpow:2, hvh:-88, hk:4, htau:130, hpow:1},
Ih: {name:"Ih",g:5.0, E:-30, mvh:-95, mk:-5, mtau:140, mpow:1, hvh:null,hk:4, htau:1e9, hpow:1},
T: {name:"T", g:3.2, E:120, mvh:-54, mk:6, mtau:2, mpow:2, hvh:-87, hk:4, htau:22, hpow:1},
D: {name:"D", g:9.0, E:-77, mvh:-30, mk:10, mtau:3, mpow:1, hvh:-80, hk:5, htau:200, hpow:1},
M: {name:"M", g:2.5, E:-77, mvh:-35, mk:10, mtau:60, mpow:1, hvh:null,hk:4, htau:1e9, hpow:1},
};
// build the piecewise current from [[dur_ms, amp], ...]; returns {I, test_start} (index of last positive run).
function buildI(segments, dt=0.01, base=20, tail=80){
const segs=[[base,0]].concat(segments).concat([[tail,0]]);
const chunks=segs.map(([d,a])=>({len:Math.max(Math.round(d/dt),1),a}));
let n=0; chunks.forEach(c=>n+=c.len);
const I=new Float32Array(n); let idx=0;
for(const c of chunks){for(let i=0;i<c.len;i++)I[idx++]=c.a;}
let start=0, cur=0, runStart=null;
for(const c of chunks){
if(c.a>0 && runStart===null) runStart=cur; else if(c.a<=0) runStart=null;
cur+=c.len;
if(c.a>0) start=runStart;
}
return {I, test_start:(start||0)};
}
// blockers: TTX->Na=0, TEA->K=0, Cd->drop extra with E>=100 (Ca-type), XE991->drop extra named "M".
function applyBlock(extra, block){
block=block||[];
const gNa=block.includes("TTX")?0:120, gK=block.includes("TEA")?0:36;
const kept=extra.filter(c=>!((block.includes("Cd")&&c.E>=100)||(block.includes("XE991")&&c.name==="M")));
return {gNa, gK, extra:kept};
}
// integrate; returns {V, cnt, dt, test_start}. mech = {extra:[Chan], slow_na:bool}.
function simulate(I, test_start, mech, block, dt=0.01, V0=-65){
const {gNa, gK, extra}=applyBlock(mech.extra||[], block);
const gL=0.3, slow_na=!!mech.slow_na, n=I.length;
let v=V0, [am,bm,ah,bh,an,bn]=ab(v);
let m=am/(am+bm), h=ah/(ah+bh), nn=an/(an+bn), s=1;
const xs=extra.map(c=>minf(v,c.mvh,c.mk));
const hs=extra.map(c=>c.hvh!=null?hinf(v,c.hvh,c.hk):1);
const V=new Float32Array(n); let cnt=0, pv=v;
for(let i=0;i<n;i++){
[am,bm,ah,bh,an,bn]=ab(v);
m+=dt*(am*(1-m)-bm*m); h+=dt*(ah*(1-h)-bh*h); nn+=dt*(an*(1-nn)-bn*nn);
m=clip(m,0,1); h=clip(h,0,1); nn=clip(nn,0,1);
if(slow_na){ const s_inf=1/(1+Math.exp((v+45)/4)), tau_s=v>-55?380:1500; s+=dt*(s_inf-s)/tau_s; s=clip(s,0,1); }
let cur=gNa*m*m*m*h*(slow_na?s:1)*(v-ENa)+gK*nn*nn*nn*nn*(v-EK)+gL*(v-EL);
for(let j=0;j<extra.length;j++){
const c=extra[j];
xs[j]+=dt*(minf(v,c.mvh,c.mk)-xs[j])/c.mtau;
if(c.hvh!=null) hs[j]+=dt*(hinf(v,c.hvh,c.hk)-hs[j])/c.htau;
xs[j]=clip(xs[j],0,1); hs[j]=clip(hs[j],0,1);
cur+=c.g*Math.pow(xs[j],c.mpow)*Math.pow(hs[j],c.hpow)*(v-c.E);
}
v+=dt*(I[i]-cur); v=clip(v,-95,80);
V[i]=v;
if(i>=test_start && pv<0 && v>=0) cnt++;
pv=v;
}
return {V, cnt, dt, test_start};
}
function runProto(mech, segments, block){ const {I,test_start}=buildI(segments); const sim=simulate(I,test_start,mech,block); sim.I=I; return sim; }
// interpretable readouts (n_test is the scored one; the rest aid insight)
function feats(sim){
const {V, test_start}=sim; let nTest=0, nPre=0, vmin=99, vpk=-99, pv=V[0];
for(let i=1;i<V.length;i++){
if(pv<0 && V[i]>=0){ if(i>=test_start) nTest++; else nPre++; }
if(V[i]<vmin) vmin=V[i]; if(V[i]>vpk) vpk=V[i]; pv=V[i];
}
return {nTest, nPre, vmin:Math.round(vmin), vpk:Math.round(vpk)};
}
/* ==================== worlds (ported from worlds.py) ==================== */
const NOVEL="Na+K + unidentified current";
const TEXTBOOK=[
["brief step (12 µA, 40 ms)", [[40,12]]],
["long step (10 µA, 300 ms)", [[300,10]]],
["strong step (18 µA, 120 ms)", [[120,18]]],
["weak step (5 µA, 120 ms)", [[120,5]]],
];
const ADVANCED=[
["hyperpol conditioning → depol test (−30 µA/250 ms, then +12 µA)", [[250,-30],[150,12]]],
["hyperpol pre-pulse, gap, weak test (−30 µA/250 ms → rest → +6 µA)", [[250,-30],[120,0],[60,6]]],
["paired long pulses (+12 µA/300 ms, gap, +12 µA/300 ms)", [[300,12],[60,0],[300,12]]],
["depol conditioning → test (+15 µA/250 ms, then +12 µA)", [[250,15],[150,12]]],
["brief hyperpol conditioning → test (−30 µA/40 ms, then +12 µA)", [[40,-30],[150,12]]],
];
const POOL=TEXTBOOK.concat(ADVANCED);
const WORLDS={
z_rebound:{blurb:"low-threshold inward current, de-inactivated by hyperpolarisation → depolarisation block",
alt:{extra:[CH.Z],slow_na:false},
disc:["conditioning pulse (−25/200, +8 test)",[[200,-25],[120,8]]],
test:[["cond −20/+10",[[200,-20],[120,10]]],["cond −30/+8",[[200,-30],[120,8]]],["cond −35/+12",[[200,-35],[120,12]]],["depol 14/200",[[200,14]]],["strong 16/150",[[150,16]]]]},
h_sag:{blurb:"hyperpolarisation-activated inward current (Iₕ) → voltage sag + post-inhibitory rebound",
alt:{extra:[CH.Ih],slow_na:false},
disc:["hyperpol step then release (−30/250 → rebound)",[[250,-30],[120,0],[60,6]]],
test:[["hyperpol −25 → rebound",[[250,-25],[120,0],[60,6]]],["hyperpol −35 → rebound",[[300,-35],[120,0],[60,5]]],["hyperpol −40 → rebound",[[350,-40],[100,0],[60,7]]],["long 10/300",[[300,10]]],["brief 12/40",[[40,12]]]]},
na_fatigue:{blurb:"slow use-dependent Na⁺ inactivation → spike-count run-down across paired pulses",
alt:{extra:[],slow_na:true},
disc:["paired pulses (12/300, 60 gap, 12/300 test)",[[300,12],[60,0],[300,12]]],
test:[["paired 10/300+300",[[300,10],[60,0],[300,10]]],["paired 14/250+250",[[250,14],[50,0],[250,14]]],["paired 8/300+300",[[300,8],[60,0],[300,8]]],["single long 12/300",[[300,12]]],["single long 10/300",[[300,10]]]]},
ca_rebound:{blurb:"fast low-threshold Ca²⁺ (T-type), de-inactivated by hyperpolarisation → rebound BURST on release",
alt:{extra:[CH.T],slow_na:false},
disc:["hyperpol then release (−30/200 → rebound burst)",[[200,-30],[150,0]]],
test:[["hyperpol −25 → release",[[200,-25],[150,0]]],["hyperpol −35 → release",[[250,-35],[150,0]]],["hyperpol −40 → release",[[250,-40],[150,0]]],["brief 12/40",[[40,12]]],["long 10/300",[[300,10]]]]},
d_type:{blurb:"D-type K⁺, de-inactivated by hyperpolarisation → delayed / suppressed firing on the test",
alt:{extra:[CH.D],slow_na:false},
disc:["conditioning pulse (−30/250, +12 test)",[[250,-30],[150,12]]],
test:[["cond −25/+12",[[250,-25],[150,12]]],["cond −35/+10",[[300,-35],[150,10]]],["cond −30/+14",[[250,-30],[150,14]]],["long 10/300",[[300,10]]],["brief 12/40",[[40,12]]]]},
textbook_M:{blurb:"M-current (Kv7): TEXTBOOK slow K⁺ → spike-frequency ADAPTATION (recallable control)",
alt:{extra:[CH.M],slow_na:false}, control:true,
disc:["sustained step (9 µA, 400 ms)",[[400,9]]],
test:[["long 8/300",[[300,8]]],["long 12/250",[[250,12]]],["long 10/400",[[400,10]]],["brief 12/40",[[40,12]]],["strong 16/200",[[200,16]]]]},
};
const WORLD_NAMES=["z_rebound","h_sag","na_fatigue","ca_rebound","d_type","textbook_M"];
const PLAIN={extra:[],slow_na:false};
const BLOCKERS=[
{c:"TTX", d:"block Na⁺"}, {c:"TEA", d:"block K⁺"},
{c:"Cd", d:"block Ca²⁺-type"}, {c:"XE991",d:"block M-current"},
];
/* ==================== app state ==================== */
let cellIdx=0, budget=8, decided=false;
const state={proto:0, amp:12, dur:300, block:[]};
const gcs=v=>getComputedStyle(document.documentElement).getPropertyValue(v).trim();
function world(){return WORLDS[WORLD_NAMES[cellIdx]];}
function truthMech(){return world().alt;} // the true cell is the world's novel alternative
function curSegments(){ return state.proto<0 ? [[state.dur, state.amp]] : POOL[state.proto][1]; }
function curLabel(){ return state.proto<0 ? `custom (${state.amp} µA, ${state.dur} ms)` : POOL[state.proto][0]; }
/* ==================== rendering ==================== */
const $=s=>document.querySelector(s), $$=s=>document.querySelectorAll(s);
function settingChips(label, block){
const chips=[`<span class="chip proto">${label}</span>`];
(block||[]).forEach(b=>chips.push(`<span class="chip do">do: ${b}</span>`));
return chips.join("");
}
// menu-style description of a protocol's segments; reuse the exact menu name when the protocol is in the pool
function describe(seg){
const hit=POOL.find(p=>JSON.stringify(p[1])===JSON.stringify(seg));
if(hit) return hit[0];
if(seg.length===1){ const [d,a]=seg[0]; return `step (${a} µA, ${d} ms)`; }
if(seg.length===2){ const [d1,a1]=seg[0], [,a2]=seg[1];
if(a1<0 && a2>0) return `hyperpol conditioning → depol test (${a1} µA/${d1} ms, then +${a2} µA)`;
if(a1<0 && a2===0) return `hyperpol step → release (${a1} µA/${d1} ms → rebound)`;
if(a1>0 && a2>0) return `depol conditioning → test (+${a1} µA/${d1} ms, then +${a2} µA)`; }
if(seg.length===3){ const [d1,a1]=seg[0], [,a2]=seg[1], [d3,a3]=seg[2];
if(a1<0 && a2===0 && a3>0) return `hyperpol pre-pulse, gap, test (${a1} µA/${d1} ms → rest → +${a3} µA)`;
if(a1>0 && a2===0 && a3>0) return `paired pulses (+${a1} µA/${d1} ms, gap, +${a3} µA/${d3} ms)`; }
return seg.map(([d,a])=>`${a>0?'+':''}${a}/${d}`).join(', ');
}
// mini icon of the injected current command (the pulse shape) for a card
function miniStim(cv, seg){
const {I,test_start}=buildI(seg);
const r=Math.min(devicePixelRatio||1,2),W=cv.clientWidth,H=cv.clientHeight;
cv.width=W*r;cv.height=H*r;const x=cv.getContext("2d");x.scale(r,r);
drawTrace(x,W,H,{I,dt:0.01,test_start},{currentOnly:true,mini:true});
}
function featCells(f, hidden){
const items=[["n_test", hidden?"?":f.nTest, true],["n_pre", hidden?"?":f.nPre,false],
["Vmin mV", hidden?"?":f.vmin,false],["Vpeak mV", hidden?"?":f.vpk,false]];
return items.map(([k,v,sc])=>`<div class="feat${sc?' score-feat':''}"><span class="k">${k}</span><span class="v">${v}</span></div>`).join("");
}
function miniTrace(cv,sim){
const r=Math.min(devicePixelRatio||1,2),W=cv.clientWidth,H=cv.clientHeight;
cv.width=W*r;cv.height=H*r;const x=cv.getContext("2d");x.scale(r,r);drawTrace(x,W,H,sim,{mini:true});
}
// draw the injected current command I(t) as an amber waveform. o.main => fill the plot (preview);
// otherwise a faint band in the lower third (overlaid under the voltage).
function drawCurrent(x,geom,I,dt,o){
o=o||{}; const {x0,x1,y0,y1}=geom, tMax=I.length*dt;
let amin=0,amax=0; for(let i=0;i<I.length;i++){if(I[i]<amin)amin=I[i];if(I[i]>amax)amax=I[i];}
const span=Math.max(amax-amin,1), padA=0.18*span, lo=amin-padA, hi=amax+padA, rng=hi-lo;
const by0=o.main?y0+6:y1-0.30*(y1-y0), by1=y1;
const sx=t=>x0+(t/tMax)*(x1-x0), cy=a=>by1-((a-lo)/rng)*(by1-by0);
const amber=gcs('--amber');
x.save();
x.strokeStyle=amber;x.globalAlpha=.32;x.setLineDash([2,3]);x.lineWidth=1;
x.beginPath();x.moveTo(x0,cy(0));x.lineTo(x1,cy(0));x.stroke();x.setLineDash([]);
x.globalAlpha=o.main?.95:.55;x.lineWidth=o.main?1.9:1.3;x.lineJoin="round";
x.beginPath();let started=false;
const step=Math.max(1,Math.floor(I.length/(x1-x0)));
for(let i=0;i<I.length;i+=step){const xx=sx(i*dt),yy=cy(I[i]);started?x.lineTo(xx,yy):(x.moveTo(xx,yy),started=true);}
x.lineTo(sx(tMax),cy(I[I.length-1]));x.stroke();
if(!o.mini){
x.globalAlpha=1;x.fillStyle=amber;x.font="9px ui-monospace,monospace";
if(o.main){
x.textAlign="right";
x.fillText(Math.round(amax)+"",x0-4,cy(amax)+3); x.fillText("0",x0-4,cy(0)+3);
if(amin<0)x.fillText(Math.round(amin)+"",x0-4,cy(amin)+3);
x.textAlign="left";x.fillText("stimulus current I (µA)",x0+2,by0-1);
} else { x.textAlign="left";x.fillText("I (µA)",x0+2,by1-3); }
}
x.restore();
}
function drawTrace(x,W,H,sim,opt){
opt=opt||{}; x.clearRect(0,0,W,H);
const pad=opt.mini?{l:4,r:4,t:4,b:4}:{l:34,r:12,t:12,b:24};
const x0=pad.l,x1=W-pad.r,y0=pad.t,y1=H-pad.b, geom={x0,x1,y0,y1};
const dt=sim.dt, n=(sim.V?sim.V.length:sim.I.length), tMax=n*dt;
const sx=t=>x0+(t/tMax)*(x1-x0);
// test-window shading
if(!opt.mini && sim.test_start!=null){
x.save();x.globalAlpha=.08;x.fillStyle=gcs('--phosphor');
x.fillRect(sx(sim.test_start*dt),y0,x1-sx(sim.test_start*dt),y1-y0);x.restore();
}
if(opt.currentOnly){ // preview: current command is the whole story
if(!opt.mini){ x.strokeStyle=gcs('--scope-grid');x.globalAlpha=.5;x.lineWidth=1;
x.beginPath();x.moveTo(x0,y1);x.lineTo(x1,y1);x.stroke();x.globalAlpha=1;
x.fillStyle=gcs('--phosphor-dim');x.font="10px ui-monospace,monospace";x.textAlign="right";x.fillText(Math.round(tMax)+" ms",x1,y1+15); }
drawCurrent(x,geom,sim.I,dt,{main:true,mini:opt.mini}); return;
}
// voltage grid + axis
const vLo=-95,vHi=55, sy=v=>y1-((v-vLo)/(vHi-vLo))*(y1-y0);
x.strokeStyle=gcs('--scope-grid');x.lineWidth=1;x.globalAlpha=opt.mini?.5:.7;
if(!opt.mini){
x.beginPath();for(const v of [-80,-40,0,40]){const yy=sy(v);x.moveTo(x0,yy);x.lineTo(x1,yy);}x.stroke();
x.globalAlpha=1;x.fillStyle=gcs('--phosphor-dim');x.font="10px ui-monospace,monospace";x.textAlign="right";
for(const v of [-80,0,40]) x.fillText(v,x0-4,sy(v)+3);
x.textAlign="left";x.fillText("mV",4,y0+6);
x.textAlign="right";x.fillText(Math.round(tMax)+" ms",x1,y1+15);
} else x.stroke();
x.globalAlpha=1;
if(sim.I) drawCurrent(x,geom,sim.I,dt,{main:false,mini:opt.mini}); // faint current overlay (shows the input pulse)
const col=opt.color||gcs('--phosphor');
x.strokeStyle=col;x.lineWidth=opt.mini?1.2:1.7;x.lineJoin="round";x.shadowColor=col;x.shadowBlur=opt.mini?3:7;
x.beginPath();
const step=Math.max(1,Math.floor(n/(x1-x0)/2));
for(let i=0;i<n;i+=step){const xx=sx(i*dt),yy=sy(sim.V[i]);i===0?x.moveTo(xx,yy):x.lineTo(xx,yy);}
x.stroke();x.shadowBlur=0;
}
function placeholder(cv){
const r=Math.min(devicePixelRatio||1,2),W=cv.clientWidth,H=cv.clientHeight;
cv.width=W*r;cv.height=H*r;const x=cv.getContext("2d");x.scale(r,r);
x.fillStyle=gcs('--scope');x.fillRect(0,0,W,H);
x.fillStyle=gcs('--phosphor');x.font="600 20px ui-monospace,monospace";x.textAlign="center";x.textBaseline="middle";
x.fillText("?",W/2,H/2);
}
/* training/test cards */
function makeTrain(spec){
const sim=runProto(truthMech(), spec.seg, []);
const el=document.createElement("div");el.className="exp";
el.innerHTML=`<canvas></canvas><div class="meta"><div class="setting">${settingChips(spec.label,[])}</div><div class="feats">${featCells(feats(sim),false)}</div></div><span class="tag">train</span>`;
requestAnimationFrame(()=>miniTrace(el.querySelector("canvas"),sim));
el.addEventListener("click",()=>selectCard(el,spec.label,sim));
return el;
}
function makeTest(spec){
const el=document.createElement("div");el.className="exp";
const truth=feats(runProto(truthMech(), spec.seg, [])).nTest;
if(spec._done){
const cls=spec._score>=75?'good':spec._score>=45?'mid':'bad';
el.innerHTML=`<canvas></canvas><div class="meta"><div class="setting">${settingChips(spec.label,[])}</div>
<div class="cmp"><span class="clab you">you</span><span class="cv you">${spec._pred}</span><span class="clab">true</span><span class="cv">${truth}</span></div>
<div class="score ${cls}">n_test forecast · score ${spec._score}/100</div></div><span class="tag">scored</span>`;
const sim=runProto(truthMech(), spec.seg, []);
requestAnimationFrame(()=>miniTrace(el.querySelector("canvas"),sim));
} else {
el.innerHTML=`<canvas></canvas><div class="meta"><div class="setting">${settingChips(spec.label,[])}</div>
<div class="predrow"><div class="pfield"><span class="k">forecast n_test</span><input inputmode="numeric" placeholder="?"></div>
<button class="reveal mini" style="padding:5px 9px">score →</button></div></div><span class="tag">predict</span>`;
requestAnimationFrame(()=>miniStim(el.querySelector("canvas"), spec.seg));
const inp=el.querySelector("input"), btn=el.querySelector("button");
const submit=()=>{ const p=parseInt(inp.value,10); if(Number.isNaN(p))return;
spec._pred=p; const err=Math.abs(p-truth); spec._score=Math.round(100*Math.max(0,1-err/6)); spec._done=true; renderTest(); };
btn.addEventListener("click",submit);
inp.addEventListener("keydown",e=>{if(e.key==="Enter")submit();});
}
return el;
}
function selectCard(el,label,sim){
$$(".exp.sel").forEach(n=>n.classList.remove("sel"));el.classList.add("sel");
$("#scope-title").textContent="Recording"; $("#scope-sub").innerHTML=settingChips(label,[]);
bigDraw(sim); $("#readout").innerHTML=featCells(feats(sim),false);
}
function bigDraw(sim,color){
const cv=$("#scope"),r=Math.min(devicePixelRatio||1,2),W=cv.clientWidth,H=cv.clientHeight;
cv.width=W*r;cv.height=H*r;const x=cv.getContext("2d");x.scale(r,r);drawTrace(x,W,H,sim,{color});
}
function renderTrain(){ const b=$("#train");b.innerHTML="";
world()._train.forEach(s=>b.appendChild(makeTrain(s))); }
function renderTest(){ const b=$("#test");b.innerHTML="";
world()._test.forEach(s=>b.appendChild(makeTest(s))); }
function renderWorldGrid(){
const g=$("#worldgrid");g.innerHTML="";
WORLD_NAMES.forEach(nm=>{const w=WORLDS[nm];const d=document.createElement("div");d.className="diff";
d.innerHTML=`<div class="k">${nm}${w.control?" · control":""}</div><div class="us">${w.blurb}</div>`;g.appendChild(d);});
}
/* bench */
function stimPreview(){
const {I,test_start}=buildI(curSegments()); // the actual injected current command
$("#scope-title").textContent="Protocol preview"; $("#scope-sub").innerHTML=settingChips(curLabel(),state.block)+' <span class="chip" style="opacity:.7">not yet run</span>';
const cv=$("#scope"),r=Math.min(devicePixelRatio||1,2),W=cv.clientWidth,H=cv.clientHeight;
cv.width=W*r;cv.height=H*r;const x=cv.getContext("2d");x.scale(r,r);
drawTrace(x,W,H,{I,dt:0.01,test_start},{currentOnly:true});
$("#readout").innerHTML='<span class="hint">This is the current you will inject. Run to measure n_test (the scored spike count).</span>';
updateLock();
}
function matchesTest(){ return world()._test.some(s=> JSON.stringify(s.seg)===JSON.stringify(curSegments()) && state.block.length===0 ); }
function updateLock(){ const locked=matchesTest(); $("#lockbar").hidden=!locked; $("#run").disabled=budget<=0||locked; }
function doRun(){
if(budget<=0 || matchesTest()) return;
budget--; updateBudget();
const label=curLabel(), block=state.block.slice();
const sim=runProto(truthMech(), curSegments(), block);
const f=feats(sim);
$("#scope-title").textContent="Measured response"; $("#scope-sub").innerHTML=settingChips(label,block);
bigDraw(sim); $("#readout").innerHTML=featCells(f,false);
const log=$("#log"); if(log.querySelector(".hint"))log.innerHTML="";
const card=document.createElement("div");card.className="exp";
card.innerHTML=`<canvas></canvas><div class="meta"><div class="setting">${settingChips(label,block)}</div><div class="feats">${featCells(f,false)}</div></div><span class="tag">#${8-budget}</span>`;
requestAnimationFrame(()=>miniTrace(card.querySelector("canvas"),sim));
card.addEventListener("click",()=>selectCard(card,label,sim));
log.prepend(card);
if(budget<=0)$("#hint").textContent="Budget spent. Make your call, or start a New cell.";
}
function updateBudget(){
const pips=$("#pips");pips.innerHTML="";
for(let i=0;i<8;i++){const p=document.createElement("span");p.className="pip"+(i>=budget?" spent":"");pips.appendChild(p);}
$("#budget-txt").innerHTML=`<span style="color:var(--phosphor)">${budget}</span> of 8 recordings left`;
updateLock();
}
/* the menu protocol that best exposes this world (plain vs hidden differ most) -- always selectable */
function menuDiscriminator(){
const alt=world().alt; let best=POOL[0][0], bd=-1;
POOL.forEach(p=>{const d=Math.abs(runProto(alt,p[1],[]).cnt-runProto(PLAIN,p[1],[]).cnt); if(d>bd){bd=d;best=p[0];}});
return {label:best, diff:bd};
}
/* verdict: identify the mechanism */
function renderChoices(){
const box=$("#choices");box.innerHTML="";
[["Na+K (plain)",false],[NOVEL,true]].forEach(([lab,isNovel])=>{
const b=document.createElement("button");b.textContent=lab;
b.addEventListener("click",()=>{
decided=true;
const correct=isNovel; // the true cell is always the world's novel alternative
const w=world(), md=menuDiscriminator();
$("#verdict").className="verdict show";
$("#verdict").innerHTML=`<h3>Revealed</h3><p>${correct?'<span class="em">Correct.</span>':'Not quite —'} this cell is <b>${WORLD_NAMES[cellIdx]}</b>: ${w.blurb}. In the menu, the protocol that exposes it is <b>“${md.label}”</b> — there the hidden current shifts the count by ${md.diff} spikes, while textbook steps look identical.</p>`;
});
box.appendChild(b);
});
}
/* protocol menu */
function initProtoMenu(){
const sel=$("#proto");sel.innerHTML="";
const og1=document.createElement("optgroup");og1.label="textbook steps";
TEXTBOOK.forEach((p,i)=>og1.appendChild(new Option(p[0], String(i))));
const og2=document.createElement("optgroup");og2.label="non-standard protocols";
ADVANCED.forEach((p,i)=>og2.appendChild(new Option(p[0], String(TEXTBOOK.length+i))));
sel.appendChild(og1);sel.appendChild(og2);
sel.appendChild(new Option("custom step…","-1"));
sel.value="0";
sel.addEventListener("change",()=>{
state.proto=parseInt(sel.value,10);
const custom=state.proto<0;
$("#amp-ctl").hidden=!custom; $("#dur-ctl").hidden=!custom;
stimPreview();
});
}
function initBlockers(){
const box=$("#blockers");box.innerHTML="";
BLOCKERS.forEach(b=>{
const l=document.createElement("label");l.className="blk";
l.innerHTML=`<input type="checkbox" data-c="${b.c}"><span><span class="t">${b.c}</span> <span class="d">${b.d}</span></span>`;
l.querySelector("input").addEventListener("change",e=>{
if(e.target.checked) state.block.push(b.c); else state.block=state.block.filter(x=>x!==b.c);
stimPreview();
});
box.appendChild(l);
});
}
function bindSlider(id,vid,key){ const el=$("#"+id);el.addEventListener("input",()=>{state[key]=parseInt(el.value,10);$("#"+vid).textContent=state[key];stimPreview();}); }
function resetCell(advance){
if(advance) cellIdx=(cellIdx+1)%WORLD_NAMES.length;
budget=8; decided=false; state.block=[]; $$("#blockers input").forEach(i=>i.checked=false);
const w=world();
// training: two TEXTBOOK steps only -- under these the novel channel is silent (plain === alt), so the
// player must DISCOVER the revealing protocol from the menu. That is the benchmark's whole thesis.
w._train=[{label:TEXTBOOK[0][0], seg:TEXTBOOK[0][1]}, {label:TEXTBOOK[1][0], seg:TEXTBOOK[1][1]}];
w._test=w.test.map(t=>({label:describe(t[1]), seg:t[1]}));
updateBudget(); renderTrain(); renderTest();
$("#log").innerHTML='<p class="hint" style="padding:0 0 4px">No experiments run yet.</p>';
$("#verdict").className="verdict"; $("#verdict").innerHTML='<h3>Your call</h3><p>Probe the cell, then decide: is there a hidden current?</p><div class="choices" id="choices"></div>';
renderChoices();
$("#hint").textContent="Choose a protocol; the scope previews the current command. “Run” measures the cell and logs the spike count.";
stimPreview();
}
/* theme */
function setTheme(t){ document.documentElement.dataset.theme=t;
$("#t-light").setAttribute("aria-pressed",String(t==="light")); $("#t-dark").setAttribute("aria-pressed",String(t==="dark")); }
function init(){
initProtoMenu(); initBlockers();
bindSlider("amp","amp-v","amp"); bindSlider("dur","dur-v","dur");
$("#run").addEventListener("click",doRun);
$("#newcell").addEventListener("click",()=>resetCell(true));
$("#t-light").addEventListener("click",()=>setTheme("light"));
$("#t-dark").addEventListener("click",()=>setTheme("dark"));
renderWorldGrid(); resetCell(false);
}
if(typeof document!=="undefined" && document.getElementById("proto")!==null) init();
else if(typeof document!=="undefined") document.addEventListener("DOMContentLoaded",init);
/* export for headless validation (node) */
if(typeof module!=="undefined"){ module.exports={simulate,runProto,buildI,feats,WORLDS,WORLD_NAMES,POOL,PLAIN,CH}; }
</script>