Skip to content

Live At cursor measure for the iEEG Brain panel - #563

Merged
neuromechanist merged 23 commits into
stagingfrom
feat/ieeg-live-activity
Nov 19, 2026
Merged

neuromechanist merged 23 commits into
stagingfrom
feat/ieeg-live-activity

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What changed

The iEEG Brain panel's Activity overlay is now live. A new measure, "At cursor" (RMS, or line length as a second choice), is the default when Activity is switched on. The contact colors follow the pointer over the scope and change at mouse speed, with no new network request.

  • The panel keeps the sample matrix of the last successful activity read (the located rows' own level-0 arrays, no copy, at most MAX_ACTIVITY_SAMPLES). It is released by every abort_activity effect: a window or option change, Activity off, a group or recording change, closing the panel, dispose.
  • SpatialPanel.setCursor(timeS | null): the viewer calls it from the scope's mousemove, with null on mouseleave and when the pointer is off the traces or over the overlay, and again after each frame (so a window that scrolls under a resting pointer moves the cursor time with it). The panel coalesces calls to one recolor per animation frame.
  • Per contact, RMS (mean removed, as the whole-window metric) or line length over a short window centered on the cursor: 50, 100, 200 (default) or 500 ms, selectable. Same unit, non-finite and mixed-unit rules as the whole-window metrics; a window under two samples at the recording's rate is refused with a sentence before any read (cursor_too_short). Band power and relative band power stay whole-window measures.
  • The color scale is derived once per read, not per cursor position: the short-window values of every located channel at up to 64 evenly spaced centers across the window, then their 2nd to 98th percentile (sequential, viridis, same as the whole-window scale). Colors are comparable across cursor positions. The legend says so.
  • Legend and accessible text say what is shown: "RMS over 200 ms centered at 12.34 s, in microvolts." plus the scale sentence. With the cursor off the scope, the panel shows the whole-window value of the same metric, labeled "Whole-window value ...". The text node is not a live region, so nothing is announced per frame.
  • Recoloring runs in requestAnimationFrame, recolors only (no layout, no geometry, no read), and the fast path allocates no per-contact data: preallocated arrays, a 256-entry viridis table, cached fill strings for the 2D fallback, and the renderer now rewrites its own color buffer in place (setColors reuses it). The one string made per frame is the legend sentence.
  • The 2D fallback (WebGL off or the 2D views chosen) updates too: the SVG circles' fill is rewritten in place, only for contacts whose color changed.
  • Exports (PNG and SVG), the contact list and the dot titles are built from the same state the screen shows (withLive), so a figure saved at the cursor states the measure, window and cursor time. The contact list and files catch up once the pointer has rested 150 ms (LIST_IDLE_MS); they are too heavy for every frame.
  • New files: live-activity.ts (allocation-free engine), cursor-activity.test.ts. The pure short-window metric and cursorScale live in activity.ts.

Deviations from the design, and why

  • The cursor lives in the reducer (cursor_changed, activity.cursorS) so it is testable, but the DOM half handles that event without a full sync() (which would rebuild the model for every contact on each move). A full paint happens only when the live state turns on or off, and when the pointer rests.
  • The contact list's values and the dot <title>s do not follow every frame (150 ms rest), see above.
  • A window cut by the edge of the read stays centered and gets shorter (not shifted); the legend says so ("cut by the edge of the read").
  • The default measure changed from band power to At cursor, so six existing unit tests and three existing e2e specs now select band power explicitly.

Measured cost (headless Chromium 148, software GL, nm000485, 136 located channels, 10 s window, data.nemar.org)

  • 60 pointer moves, one per animation frame: 59 recolor frames, median 0.10 ms, worst 0.3 to 0.4 ms (two runs) per cursorFrame callback; the budget is 16 ms.
  • The entering frame (pointer first reaches the scope, full paint of words, scale and list): 3.7 ms.
  • 0 network requests and 0 new activity reads during the moves (2D fallback and 3D runs).
  • 2D fallback: 135 of 136 contacts changed color over 5 pointer positions. 3D: canvas screenshots at the two ends of the scope differ.
  • The existing budget test "Activity recompute ... adds no long task over 200 ms" still passes (recompute 0 ms over 50 ms by both the longtask API and the heartbeat).
  • Data plane was fast (Zarr median 85 to 103 ms at preflight).

Tests

  • bunx vitest run src/lib/eeg-viewer src/lib/ieeg-spatial test/brain-template-vendor.test.ts: 1090 passed. New: cursor-activity.test.ts (19: window edges and clipping, NaN refusal, units, rate too low, the committed on006136 slice against the external reference, a sine formula, scale fixed and percentile-bounded, engine bipolar mean and no-value index, 5000 updates with a flat heap), reducer tests in panel.test.ts (default, width option, refusal sentence, cursor event, legend text, list and colors, other measures untouched), and a real-reader test in activity-reads.test.ts (matrix retained, same values as the plain plan, no extra chunk fetch).
  • bun run lint, bun run typecheck: clean.
  • E2E on demand, against a local dev server: PUBLIC_DATA_BASE_URL=https://data.nemar.org bun run test:e2e e2e/ieeg-spatial-validation.spec.ts -g "cursor": 2 passed. Also ran the existing Activity budget test and the three panel specs touched here: passed.
  • The per-frame timing wraps requestAnimationFrame and keys on the callback name cursorFrame, so it needs the dev server (unminified names).

Not run

  • Other browsers (Firefox, WebKit), a GPU-backed Chromium (the numbers above are software GL), a touch device (touch has no hover, so the live state never turns on; the whole-window value shows), the whole e2e suite, the full unit suite, CI.
  • Docs (nemarOrg/docs web/ieeg-spatial-view.md) are not edited; see the hand-off notes.

Review round 1: what changed

  1. Shell lifecycle tests. The lifecycle moved out of the DOM shell into createLiveCursor (panel.ts) with an injected clock, frame queue and timers (test infrastructure only; the state, reducer, matrix and engine are real). Tests in panel.test.ts, "the live cursor's lifecycle": many setCursor calls queue one frame and recolor once; entering and leaving live are full paints; a cursor outside the read keeps whole-window values; the list catches up only after LIST_IDLE_MS of rest; dispose with a frame queued (no throw, no recolor, timers cleared); matrix release on every abort path, one test each for window change, width change, measure change, Activity off, group change, recording change, panel close and dispose, each also checking a stale matrix is never measured (even against the old ready state); a new read replaces the old samples (values double when the samples double); position kept, not painted, while closed or off. The e2e stays the integration check.
  2. Touch and keyboard. The fallback sentence is pointer-neutral: "Whole-window value of the same measure is shown. On a device with a pointer, the value over N ms follows the pointer over the scope." cursor-source.ts (tests in cursor-source.test.ts) ends the cursor on pointerleave, pointercancel, touch pointerdown/up/cancel, touchstart/touchend/touchcancel, and ignores the compatibility mouse events after a tap; mouse behavior is unchanged. Keyboard: no cursor source, by design. The arrow keys already scroll the window and Shift-arrows page it, so spending them on a second meaning would break existing keyboard behavior; keyboard and touch users get the whole-window value, which the legend says in words (the legend paragraph is plain text in reading order, not a live region, and the measure select is labeled "At cursor: RMS").
  3. Pan flicker. Kept as a known trade-off, by design. Releasing the matrix on a window change is required (no stale samples); holding the previous live colors while the pointer keeps moving would show frozen values as current. During the 150 ms debounce plus the read, the panel shows the previous window's whole-window values with "Updating for the new window..." and returns to live when the new read lands.
  4. Resting pointer. createCursorSource.windowMoved maps the resting pixel against the current plot and clears the cursor when the plot narrowed past it or has zero width (cursor-source.test.ts: narrowing, zero width, no pointer).
  5. Edge bound. Sample i spans [i, i + 1) / rate after t0S (the left edge of sample 0, as floor(start * rate) gives it), so the read covers [t0S, t0S + nSamples / rate] and both ends are valid; code is now center < 0 || center > nSamples and the docblock says so. Test "keeps a cursor exactly at either end of the read, and refuses one a hair past it".
  6. Scale pools contact values. cursorScale takes the contacts' channel rows and pools the same per-contact means the colors use. Tests: bipolar limits follow the pair mean, not raw channels (a one-contact scale collapses to the mean); the engine colors a pair with its mean on that scale.
  7. No-value reasons. The live path now tells non-numbers (non_finite) from overflowed results (undefined_value) and takes the first channel's reason for a pair, as contactValuesOf does; band_above_nyquist cannot occur for RMS and line length. Test "why a contact has no value...".
  8. Cleanups. Real getters in live-activity.ts (no casts); docblock moved to its function; changed is now relative to an explicit commit(), so viewState/sync calls never consume it, and measure() skips re-measuring the same center (test "changed is relative to the last commit"); the animation frame is cancelled on dispose.

Also: the e2e timing probe now keys on the controller's onFrame callback. Re-measured: 59 recolor frames for 60 moves, median 0.10 ms, worst 0.4 ms, entering frame 5.7 ms, 0 requests.

Not run this round: the whole e2e suite, other browsers, GPU Chromium, real touch hardware (the touch handling is tested as logic only), CI.

Review round 2

  1. Abort-path tests through the real wiring. The activity effects now run through createActivityRunner (panel.ts), which the shell's run() and dispose() delegate to; the abort tests feed the reducer's own effects to it instead of calling release() by hand. New and changed tests in panel.test.ts: each abort path (window change, width change, measure change, Activity off, group change, recording change, panel close, dispose) goes through runner.run; "a new read releases the old samples by itself, aborts the read before it, and starts after the debounce" runs only the read_activity effect; "dispose reaches the live cursor" checks the queued frame survives the abort effect and only runner.dispose() cancels it. Mutation check done: deleting the release on abort_activity fails 5 tests, deleting the release on read_activity fails 1, deleting live.dispose() from the runner's dispose fails 1; all restored (119 passing). What stays outside unit tests: the one-line delegations in the shell (run() to the runner, dispose() to runner.dispose()), which need a DOM; the e2e recording-change and Activity specs exercise them.
  2. Cursor-source dedupe after a recording change. Verified by reading the code: a recording change remounts the viewer (navigate in viewer-session.ts calls mountEegViewer, whose first act tears the old instance down), which builds a new cursor source and a new panel; recording_changed is dispatched only from start(), once per panel. So last never outlives the panel state it mirrors. No code change, one comment in cursor-source.ts.
  3. Recolor versus viewsHtml. Accepted as not worth a change, and no test: showing it cannot happen needs a DOM. Reasoning: every full paint after live motion regenerates the SVG from the current exact colors, and the DOM's last recolor is for the latest cursor, so the two can only differ by the one-step table rounding, never by a wrong value.

Re-run: targeted unit tests 1122 passed, lint and typecheck clean, the two cursor e2e tests pass (59 recolor frames for 60 moves, median 0.10 ms, worst 0.4 ms, entering frame 8.3 ms, 0 requests). Not run: whole e2e suite, other browsers, GPU Chromium, touch hardware, CI.


Agentic AI tools were used. Workflows follow the Research Skills (doi:10.5281/zenodo.20696515). The author assumes full responsibility for this content and associated changes.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Deploying nemar-website with  Cloudflare Pages  Cloudflare Pages

Latest commit: 7750f00
Status: ✅  Deploy successful!
Preview URL: https://8403e10e.nemar-website.pages.dev
Branch Preview URL: https://feat-ieeg-live-activity.nemar-website.pages.dev

View logs

@neuromechanist
neuromechanist merged commit 2f6556a into staging Nov 19, 2026
5 checks passed
@neuromechanist
neuromechanist deleted the feat/ieeg-live-activity branch November 19, 2026 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant