Skip to content

Zarr signal viewer: zarrita browser reading Zarr from cached S3 #99

Closed

Description

@neuromechanist

Tracks Stream D of the NEMAR Zarr serving-copy epic
(nemarOrg/nemar-cli#684). To be implemented in a website session.

Goal

An in-browser signal viewer on the dataset page that reads the per-recording Zarr v3
stores directly from the Cloudflare-cached zarr host with zarrita (ranged GETs,
no decode backend).

Scope

  • Add @zarrita/core (+ a small canvas/WebGL signal renderer). New "Viewer" island on
    src/pages/dataset/[id].astro.
  • Fetch <cache-host>/<id>/zarr/index.json; list recordings grouped by modality.
  • Open a .zarr store from the cached base URL; pick the view-pyramid level whose
    rate_effective is ~1 sample/pixel; ranged-GET the viewport chunks; render the
    min/max band using per-channel scale/offset. At max zoom read level 0 directly.
  • Overlay events from the store's events/ group (onset/duration/code + label_map).
  • Channel list / modality grouping; basic pan/zoom; time cursor.

Contracts

  • Store layout + reader contract: biosigIO docs/formats/zarr.md (§"Reading a store",
    §"Reader contracts: Viewer") and the Zarr PRD §7.
  • format="biosigio-zarr", format_version=2; reject newer versions.
  • Base URL is a config value (the Cloudflare cache host) — swap-able if a different CDN
    is chosen later; viewer code is base-URL agnostic.

Depends on

Stream C (cache host + CORS + index.json shape). Can prototype against a
hand-uploaded nm099999 store before C lands.

Acceptance

  • An hour-long recording opens at overview zoom within an interactive budget and bounded
    transfer (independent of length). Events overlay correctly. CORS/range work from the
    website origin.

Activity

  1. neuromechanist commented on Jun 3, 2026

    @neuromechanist
    ContributorAuthor

    Canonical first-class signal viewer — design spec

    Goal: an in-browser EEG/EMG/iEEG/MEG signal viewer on the dataset page that feels
    canonical to researchers (the MNE-Python raw browser + EEGLAB eegplot are the
    references), reads our biosigIO Zarr stores directly via zarrita, and stays
    lightweight. The producer is live (epic nemarOrg/nemar-cli#684): stores are at
    zarr.nemar.org/<id>/zarr/<bids-path>.zarr/ with an index.json manifest.

    Reference implementations (study these, don't reinvent)

    • eegdash/eegdash-viewer — the web architecture to fork: vanilla JS, Canvas 2D,
      Web Worker for I/O, client-side biquad HP/LP/notch (filtfilt, zero-phase), block
      min/max decimation, clinical aesthetic, Okabe-Ito channel colors, zero deps except an
      HDF5 reader. We replace its format readers with a Zarr chunk loader.
    • MNE-Python raw.plot() / mne-qt-browser — the research feature set + the canonical
      keyboard model + the display-vs-stored-data discipline (filters/DC/scaling are
      display-only). Overview bar, butterfly, bad-channel click, per-type scalings.
    • EEGLAB eegplot / pop_eegplot — the conventions EEG researchers expect at a glance:
      the µV scale bar, stacked traces with flush-left labels, event lines with type
      labels/colors
      , DC removal on by default, the << < > >> stepping, vertical channel slider.

    The big lightweight win

    Our biosigIO Zarr already precomputes the min/max view pyramid (<group>/view/1,2,…).
    So the viewer does no on-the-fly decimation (unlike eegdash): for the current zoom it
    picks the pyramid level whose rate_effective ≈ 1 sample/pixel, reads those small chunks,
    and renders the min/max band. At max zoom it reads level-0 directly. This is lighter and
    faster than every reference viewer.


    1. Data contract (zarrita reader)

    • GET <ZARR_BASE>/<id>/zarr/index.json -> { stores: [{ path, zarr, modalities, groups:[{name,rate,n_channels,n_samples,duration_s}] }] }. Drives the recording picker.
    • Open a store with zarrita. Root attrs: channel_groups (group names), modality_rates, format="biosigio-zarr", format_version (accept ≤ known), recording_metadata.
    • Per group <modality>_<rate>hz/ attrs: rate (served), original_rate, n_channels, n_samples, and channels[] = {label, channel_type, modality, unit, prefilter, scale, offset, row_index, usable_for_inference}.
      • 0 = (n_ch, n_time) level-0 int16 signal (sharded), with per-channel scale/offset.
      • view/L = (2, n_ch, n_time_L) min/max envelope (NOT sharded — the hot path).
      • events/ = onset (f64), duration (f64), code (i32) + label_map {code: description}.
    • Dequantize: physical = digital * scale + offset per channel (int16 store; for float32, scale=1/offset=0).
    • Address groups by channel_groups names; never reconstruct from rate. Recover native rate from original_rate.

    Technical spike to do FIRST (gates everything): confirm @zarrita/core reads our
    store — zarr v3, the view levels are non-sharded (simple chunk GETs, the rendering
    path) but level-0 is sharded + zstd/Blosc. Verify zarrita supports v3 sharding + the
    zstd codec
    for level-0; if not, the overview path (view pyramid) still works and we gate
    max-zoom level-0 reads behind that support (or add the codec). Validate against the real
    store already on S3: nm099999/zarr/sub-006/eeg/sub-006_task-P3_eeg.zarr/.


    2. Canonical feature set (the "first-class EEG viewer" tells)

    Grouped by the conventions MNE/EEGLAB users expect. P0 = MVP, P1, P2 = phasing.

    Time navigation

    • Window length selector (2/5/10/20/30 s; default 10 s). [P0]
    • Horizontal scrollbar + EEGLAB stepping << < > >> (full-window / 20%) + MNE keys (←/→ = ¼ window, Shift+←/→ = full page). [P0]
    • Time axis in seconds; toggle to clock (HH:MM:SS) with t. [P1]
    • Overview bar (MNE mne-qt-browser): a whole-recording minimap strip rendered from the
      coarsest view-pyramid level + event marks; click to jump. This is cheap because the
      coarsest pyramid level is tiny. [P1]
    • Go-to-time input. [P1]

    Amplitude / scale (the single most important tell)

    • µV scale bar (EEGLAB lower-right + MNE per-trace scalebar): shows µV/division, updates
      live. Per-modality units (µV / mV / fT). Required for P0.
    • Gain +/- (×1.1, MNE) and a gain slider (eegdash). Per-modality default scalings
      (EEG 20 µV, EOG 150 µV, ECG 500 µV, EMG 1 mV, sEEG/ECoG 100 µV, MEG-mag 1 pT — MNE defaults). [P0]
    • DC removal ON by default (d; EEGLAB submean): per-window mean subtraction, display-only. [P0]
    • Stack/Spread (EEGLAB) and Butterfly (b, MNE — overlay same-type channels; fastest way to spot a bad channel). [P1]
    • Norm/denorm (EEGLAB: per-channel z-scale so small channels are visible). [P2]
    • Clipping (MNE clipping=3: clip at 3× the slot so a hot channel doesn't smear neighbors). [P1]

    Channels

    • Stacked rows, labels flush-left (electrode names), color by channel type (Okabe-Ito / MNE type palette). [P0]
    • n_channels visible (PageUp/Down) + vertical scroll slider (EEGLAB) for 64–256-ch montages. [P1]
    • Bad-channel marking by clicking the label/trace (MNE) — viewer-only highlight for v1 (persist-back later). [P1]
    • Montage: select / reorder / hide channels; group switcher when a store has multiple (modality,rate) groups (e.g. an EEG group + a trigger group — show discrete TRIG/SYSCLOCK distinctly, usable_for_inference=false). [P2]

    Filtering (client-side DSP, display-only — MNE semantics)

    • HP / LP / notch biquad filtfilt (zero-phase), adapted from eegdash filters.js. UI:
      HP checkbox+cutoff (default off; 0.5 Hz), LP (45 Hz), notch (50/60 Hz). [P1]
    • Applied to level-0 samples (the inference signal), never the min/max envelope (it's
      nonlinear). So when filters are on, the visible window renders from a level-0 read +
      filter (the window is bounded → cheap), and the pyramid is used only for wide unfiltered
      overviews (grey the filter UI there, or fetch level-0 for the span if within budget).
    • Filters touch only the display — never the store (MNE display-vs-stored discipline). [P1]

    Events / annotations

    • Event lines from the events/ group: vertical lines at onset, type label above
      (from label_map[code]), color by type, duration shading for nonzero duration,
      type legend, show/hide (EEGLAB conventions). Events also shown in the overview bar. [P0 for
      read-only display]
    • Annotation editing (drag-to-create BAD_ spans, MNE a) — out of scope for v1 (read-only
      events first). [P2]

    Cursor / measurement

    • Chan / Time / Value readout on hover (EEGLAB status bar) — a strong professional tell. [P1]
    • Click to drop a vertical guide line (MNE green line); two-cursor time/amplitude delta. [P2]

    Keyboard model (adopt MNE's so researchers feel at home)

    ←/→ scroll ¼ · Shift+←/→ page · Home/End window size · ↑/↓ channel scroll ·
    PageUp/Down n_channels · +/- scale · b butterfly · d DC · s scalebar · t time
    format · ? help · z zen. [P1, except the P0 nav/scale keys]


    3. Architecture

    • Astro island on src/pages/dataset/[id].astro ("Viewer" tab). SSR shell + client island.
    • Config PUBLIC_ZARR_BASE_URL = https://zarr.nemar.org (swappable; the viewer is base-URL
      agnostic — direct-S3 or CDN). Reads index.json to populate the recording picker.
    • @zarrita/core opens stores + reads chunks (HTTP range to the CDN).
    • Web Worker: zarrita chunk fetch + dequant (+ optional filter on level-0) -> transfer
      Float32Array to the main thread. LRU caches (raw level-0 window + filtered window),
      so toggling a filter re-applies on cached raw (~ms) instead of refetching.
    • Main thread: Canvas 2D renderer (fork eegdash traces.js — slot layout, min/max band,
      scalebar, time axis, slot dividers, event markers). No framework needed for the canvas;
      the surrounding controls can be Astro/Preact.

    4. Performance / weight

    • Canvas 2D (WebGL optional later for 256-ch). Precomputed pyramid -> only viewport-sized
      chunks per frame. Worker off-thread. CDN caches the shared coarse levels + zarr.json
      (near-100% edge hit). Target: zarrita + ~10–15 KB viewer JS, < ~150 KB gz total.

    5. Phasing

    • P0 (credible MVP): recording picker from index.json; stacked traces from view-pyramid +
      level-0; µV scale bar; window + scroll + <<>> + nav keys; gain +/-; DC removal;
      channel labels + type colors; events overlay. (Already reads as a real EEG viewer.)
    • P1: client HP/LP/notch; n_channels + vertical scroll; butterfly; bad-channel click;
      overview bar; full keyboard model; cursor readout; clipping; time-format toggle; group switcher.
    • P2: montage reorder / stack-spread / norm; annotations; two-cursor measure; dark mode;
      WebGL renderer; multi-store compare; electrode-layout deep link (eegdash has one).

    6. Open decisions

    • Default window 10 s (MNE) vs 5 s (EEGLAB) -> 10 s.
    • Filters at overview zoom -> fetch level-0 for the bounded visible span; grey the filter UI
      only at extreme zoom-out.
    • Bad-channel marking -> viewer-only in v1.
    • Modality dependency: our current stores group as misc_<rate>hz (biosigIO falls back to
      "misc" when EEGLAB chanlocs lack channel types — known follow-up on neuromechanist/biosigio).
      Until that's fixed, derive the display modality from channels[].channel_type/modality + the
      BIDS _eeg/_emg suffix rather than trusting the group name, and don't assume the per-modality
      rate cap was applied.
    • zarrita v3 sharding/zstd support for level-0 (the spike above).

    This is the spec; the implementation lives in this repo (nemarOrg/website). Start with the
    zarrita spike against the live nm099999 store, then P0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions