Skip to content

IS-J1: robust, honest join of channels to contacts - #557

Merged
neuromechanist merged 8 commits into
feature/issue-537-epic-spatialfrom
feat/is-j1-robust-join
Nov 18, 2026
Merged

neuromechanist merged 8 commits into
feature/issue-537-epic-spatialfrom
feat/is-j1-robust-join

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Card IS-J1 of the iEEG spatial view plan (the intracranial track of epic #537, ADR 0033).
Part of #537.

What changed

The panel joined a recording's channels to electrode contacts by exact name, so a dataset whose Zarr store names a channel LTP1-20000 where the BIDS electrodes say LTP1 (on005415) never got a Brain toggle.
The join now runs ordered tiers, every non-exact join carries its kind and is said in the panel and the files, and anything ambiguous refuses with a cause.

  • src/lib/ieeg-spatial/channel-match.ts (new): the tiers exact, normalized, suffix, close as a table of named rules, each with its pattern, a real example, the datasets that need it and its sentence; PENDING_PATTERNS for what the survey saw and no safe rule joins; matchName, settleClaims.
    • normalized: whitespace (LENT 1 for LENT1), type_prefix (POL A3, EEG A1-Ref, and POL A1-A2 as a bipolar pair), leading_zeros (LSF1 for LSF01, either side).
    • suffix: reference_name (O1-REF1, read as a reference), average_reference (A1-AV, read as the average reference), numeric_tail (LTP1-20000: four or more digits, and never when the shaft plus those digits is itself a contact). The sentences say what the name does, not what it means: no dataset states the meaning (iEEGReference is n/a where checked).
    • close: the tier, its tag ("approximate name match"), its marker and an engine-level guard (exactly one removed shaft letter, same index) exist; the rule table is empty (see Deviations).
    • A tier with two readings refuses (ambiguous_name, listing both candidates); it never takes the first and never falls to a lower tier.
    • Claims: an exact name keeps its contact; a channel reaching it through a lower tier, or two differently written names reaching it through one non-exact tier, are refused (duplicate_contact).
  • src/lib/ieeg-spatial/geometry.ts: joinChannels uses the tiers (exact name, then the exact bipolar split, then the tiers); every join has matchKind and rule; Contact.raw carries the electrodes row's own x, y, z and size text through the parser.
  • src/lib/ieeg-spatial/export.ts, panel.ts, src/styles/eeg-viewer.css: contactTitle (the one place) now says LTP1 (channel LTP1-20000 [suffix match]); recorded (...) m, frame: ACPC; list rows show the contact as recorded, the channel in parentheses, a small tag and the rule's words as its tooltip; a selected channel reads LAF1-LAF2 (contacts LAF1, LAF2; midpoint) or LTP1-20000 (contact LTP1; suffix match); the status line adds joined by name rules: 646 suffix match; one note per rule chain says how many joined and shows three real name pairs; unlocated channels are listed by original name with their cause (a name that fits two contacts says which, a channel that lost its contact says whose it is). The files carry the same sentences because they read the same model.
  • Verbatim recorded location: the line is the file's text (-7.589096499647889e-05, -5.0, 63.150000000000006 stay as written), with the file's own unit word (m, cm, mm, pixels) and the declared frame; a size the file gives is added as written, n/a and empty are left out.
  • .context/research-ieeg-channel-names.md (new) and a pointer in .context/research.md: the survey, dataset ids and counts only.
  • scripts/ieeg-channel-survey.ts and .context/research-ieeg-channel-names-recordings.json (new, review round 1; a scope addition the review asked for): the script re-derives every count of the note from live data over the committed list of 338 recordings (--sample <file> draws a fresh list); the last two runs produced identical tables.
  • test/fixtures/ieeg-channel-names/ (new): 153 real files and Zarr label excerpts from 24 recordings of 23 datasets, with dataset, version, path, URL and SHA-256 in SOURCES.json (each excerpt also carries the hash of the full upstream response); test/ieeg-channel-fixtures.ts is the shared loader.

Survey (2026-11-18, 93 usable datasets, 328 recordings, 33,834 channels)

Row Pattern Channels Datasets Reading
S1, S2 the file's own name; an exact bipolar pair 21,835; 4,436 69; 18 exact (before this card: 77.6 percent of channels)
S3 A1-AV 1,348 5 suffix, average_reference
S4, S6 POL A3; POL A1-A2 778; 527 3; 4 normalized, type_prefix
S5 LTP1-20000 646 1 suffix, numeric_tail
S7 O1-REF1 383 1 suffix, reference_name
S8 LENT 1 240 1 normalized, whitespace
S9 LSF1 for LSF01 220 2 normalized, leading_zeros
S10, S11 POL A1-AV; EEG A1-Ref 132; 37 1; 3 suffix, a chain of two rules
P1 to P9 derived copy, position-numbered channels, case only, bare index, prime for side, doubled letter, whole lead, polarity marker, repeat counter 1,272 12 pending, with the reason
N1 to N3 not a contact (signals, scalp leads, state_*), or no row for the contact 1,980 not a naming problem

Rules S3 to S11 join 4,311 channels (12.7 percent) in 18 datasets; in 14 datasets the sampled recordings had nothing the old join could place and now have a Brain toggle (nm000379, on004551, on005007, on005415, on005545, on005670, on005931, on006107, on006233, on006910, on006914, on007118, on007119, on007120).
Each implemented rule cites its row in the note.
"Channels" is name-resolved: a channel whose contact the file leaves at n/a still counts, so the panel places 4,301 of the 4,311 rule joins (S5: 646 name-resolved, 638 located, the other 8 being EKG*-20000 channels). Reproduce with bun scripts/ieeg-channel-survey.ts.
Corroboration by each dataset's own _channels.tsv (the row at the Zarr row's index names the contact): 646 of 646 for S5 (on005415), 1,157 of 1,157 for -AV in on006107, on007118, on007119 and on007120, 209 of 209 for on004194's padding; 3,228 of the 4,311 non-exact joins (74.9 percent) have the contact in the dataset's own channel list. The check is silent where the dataset's file repeats the store's label (nm000379, on004473, on005398, on005670 sub-02, on003848) or is absent (on005670 sub-01); those joins rest on the survey reading, which the note says.

Gates (measured at head aaf2fc4 after review round 1, round 2 below; the epic tip d162257 is the base; nothing newer to merge)

  • bun install --frozen-lockfile: Checked 416 installs across 568 packages (no changes)
  • bun run typecheck: Result (517 files): 0 errors, 0 warnings, 19 hints (the base's 19)
  • bun run lint: Checked 445 files in 216ms. No fixes applied. (and biome check scripts/ieeg-channel-survey.ts: Checked 1 file in 5ms. No fixes applied.)
  • bun run test src/lib/ieeg-spatial: Test Files 12 passed (12), Tests 447 passed (447)
  • bun run test: Test Files 189 passed | 3 skipped (192), Tests 5473 passed | 14 skipped (5487)
  • E2E_PORT=4417 PUBLIC_DATA_BASE_URL=https://data.nemar.org bun run test:e2e e2e/ieeg-spatial-panel.spec.ts (headless, dev server): 28 passed (2.5m) (the 26 earlier tests, two of whose patterns now say size -999 because the recorded line is verbatim, and 2 new). Two earlier runs stopped before the end (serial mode skips the rest): one on a transient data-plane 500 (a directory listing past the quiet retries, in the failed-read test, while the embed's own landing fetch also logged an upstream error), one on two patterns of mine that needed size -999; the third run passed 28 of 28.
  • Leak check on every written file: only the two XML namespace identifiers the earlier cards already document.
  • Bundle, bunx astro build and gzip -9c <chunk> | wc -c, base and head built the same way: panel chunk 90,024 raw and 33,013 gzipped at the base, 99,124 raw and 36,109 gzipped here (+3,096 gzipped; the owner's budget is 64 KB, so 35.3 KB used); viewer chunk 166,137 raw and 58,277 gzipped at the base, 58,280 here (a file name hash). The pending table is only imported by tests, so none of its text is in the chunk.

Revert checks

Each is one line mutated, the named tests fail, the line is reverted, git status --porcelain clean afterwards (run twice, the second time on the final code).

  • Card 1 to 6: suffix tier off (15 fail); suffix tried before the bipolar split (1); two candidates take the first (4); the close guard removed (1); the claim check skipped (3); a pending reason emptied (1).
  • Display and verbatim: row shows only the matched name (4); title drops the channel (5); tag dropped (4); status omits the counts (2); recorded line from the parsed number (2); parser keeps String(x) (2); unit word rewritten to mm (2); no approximate marker (1); bipolar channel line dropped (2); match notes dropped (2); ambiguous candidates hidden (1); size cell dropped (1).

Deviations from the card

  1. close has no rule. The card's stop condition applies: the one surveyed candidate (a doubled shaft letter) is right in on003374 (mmAL1 for mAL1) and wrong in nm000454, where DHAA is a real shaft the file lacks and the edit would put four channels on shaft DHA. A general edit distance would join left and right shafts (AL, AR). The tier, its tag, its marker, the status wording and a guard that allows only one removed shaft letter with the same index are in place and tested with rule tables of the tests' own (matchName, joinChannels and placeContacts take an optional rule table), on real contact names. nm000454 is pinned as the counterexample.
  2. The close marker is opacity (0.55), through the colors the panel already hands to the 3D renderer and the 2D views, because the card keeps the renderer out of scope; the tag, the status line and the "[approximate name match]" in titles say it in words. Known limit, documented at APPROXIMATE_ALPHA: while anything is selected the other contacts are dimmed to 0.3 and the selected one is drawn in full, so the opacity cue is gone then. Nothing is affected today (no shipped rule joins by close); the first close rule must add a cue that is not opacity (a ring or marker in the renderer and the 2D views).
  3. normalized has three rules, not the card's five. Case, Unicode lookalikes and the SEEG prefix are not in the survey (no name outside ASCII among 69,806; no label starts with SEEG ; case differences occur only in on004624, where the labels run CH0 to CH35 against contacts Ch1 to Ch32, so a case-folded join could be off by one). They are in PENDING_PATTERNS with that evidence. type_prefix also reads a bipolar pair after the word, and rules chain (EEG A1-Ref is type_prefix+reference_name).
  4. numeric_tail needs four or more digits, so A1-2 (a bipolar pair in short form) and T7-1 are never read as a tail. The tail's meaning is not stated by any dataset (iEEGReference is n/a in all six datasets checked); the rule rests on the survey and is corroborated by the dataset's own _channels.tsv, which names the joined contact at the same row for 646 of 646 channels in on005415. -AV is the same kind of reading, corroborated the same way in four of its five datasets (1,157 of 1,157). Both sentences say what the name does, not what it means.
  5. The recorded line adds , size X when the file gives a size other than n/a or empty, as written and with no unit of ours (on005522 writes -999).
  6. A new "Selected channel:" label sits before "Selected:" in the files, and the live panel has a matching line; list rows now show (channel X) in parentheses.
  7. The survey script is not committed (scratch); the committed evidence is the fixtures with their hashes and the note, and the counts are the run's.
  8. The parser test was written first; the rest were written alongside and each is proved by mutation.
  9. scripts/ieeg-geometry-census.ts is not edited: the census classifies geometry, and the join is not part of its classes.

Review round 1 (head aaf2fc4)

No Critical and no false join were found; the deviations were accepted (no close rule, the engine kept, -AV kept with rewording, opacity deferred). Fixed:

  • I1: near-miss names are pinned unlocated beside the real forms, with contacts A1 and A3 copied from real rows (A1-2, A1-9, A1-999, pol A3, Eeg A3, EEG A3, XEEG A3, A1-AVG, A1-AV2, A1-av, A1-Reference, A1-Ref+3; contacts A1 and A3 because a refusal of pol A3 shows nothing unless A3 exists), so a loosened anchor fails; a repeated identical label at the winning tier is tested (the redundant first guard of settleClaims, which only shielded an equivalent mutation, is removed).
  • I2: the -AV and -REF sentences and patterns now say what the name does, "read as the average reference" and "read as a reference"; no dataset states either meaning.
  • I3: the P3 evidence is CH0 to CH31 plus four state_* channels (36 in all) against contacts Ch1 to Ch32; the four state_* channels are signals (N1), so P3 is 192 channels, N1 891.
  • I4: scripts/ieeg-channel-survey.ts and the list of recordings are committed; the note says how to re-derive the counts, that "channels" is name-resolved including channels with no position (the "Located" column is what the panel draws), and gives the _channels.tsv corroboration by rule and dataset. Where the file is silent (it repeats the store's label, or is absent) it says so; the measured agreement is stronger than the survey reading for S5, S3 (four of five datasets) and on004194, and silent for S7 and S8.
  • S1: zero stripping refuses when the stripped zeros could be a digit of another shaft's name (contacts L1 and L0: L01 does not join L1; both directions), and the surveyed padding is untouched (S9 counts unchanged).
  • S2: bipolar pairs are claimed like contacts. Cause: duplicate_contact, the same word as for one contact (the pair is listed as contacts). POL A1-A2 beside A1-A2 is refused; A1-A2 and A2-A1, both exact, both stay as today; two differently written non-exact names for one pair both lose it; a contact and a pair never compete.
  • S3: the opacity marker is overridden while anything is selected (other contacts dimmed to 0.3, the selected one drawn in full), so the cue is gone then; documented at APPROXIMATE_ALPHA and contactColors (and in deviation 2). No shipped rule joins by close, so nothing is affected today; the first close rule must add a cue that is not opacity. The text cues (the tag, the status line, the title) stay.
  • S5: the alpha test compares with a literal 0.55, and the constant is pinned.

Revert checks (one line mutated, the named test fails, reverted, tree clean afterwards), one per new test: {3,} to * in the numeric tail (the near-miss test fails); the type prefix to any word, and with /i (the same test); -AV$ to -AV.*$ (same); -ref\d*$ to -ref.*$ (same); topLabels.size === 1 to top.length === 1 (4 tests, among them the repeated-label ones); the -AV sentence and the -REF sentence back to the old words (1 test each); P3 back to CH35 (1); the shaft guard removed (2); pair claims skipped (2); exact pairs refusing each other (1); APPROXIMATE_ALPHA 0.55 to 0.5 (1). The earlier 18 mutations (the card's six and the display and verbatim twelve) were rerun on the final code, head aaf2fc4, and each still fails its tests (suffix off 17, suffix first 1, take first 4, close guard 1, claims skipped 6, pending reason 2, then 4, 5, 4, 2, 2, 2, 2, 1, 2, 2, 1, 1); the tree was clean after every one.

e2e, whole spec once on the final source: 28 passed (2.5m). Survey script: three runs the same day, the last two with identical tables; 33,834 channels, 2,437 requests, 0 exhausted.

Review round 2

Merge-ready, no false refusal on real data; one optional finding fixed without changing behavior: the pair claim key sorts the two contacts, so non-exact POL A1-A2 and POL A2-A1 (opposite polarity, plausibly two real channels) both refuse as duplicate_contact, while exact A1-A2 and A2-A1 both stay. A refusal is the safe side: two non-exact spellings cannot be told apart from a duplicate, while exact names are unambiguous; no surveyed recording has it. The reason is documented at settleClaims, and a known-answer test row on real contacts pins both halves (plus an exact pair keeping the pair from a non-exact opposite spelling, and either opposite spelling joining alone). Revert check: the sorted key turned off (c.contacts.join for the sorted join) fails that test (1 failure). Gates on the head after round 2: lint Checked 445 files in 244ms. No fixes applied.; typecheck Result (517 files): 0 errors, 0 warnings, 19 hints; bun run test src/lib/ieeg-spatial 12 files, 448 passed; bun run test Test Files 189 passed | 3 skipped (192), Tests 5474 passed | 14 skipped (5488); leak check on the two changed files prints nothing. Only a test and a comment changed, so the e2e (28 passed (2.5m)) and the bundle figures above stand.

Not run, would check

  • CI: every job green on the final head, because the manager reads gh pr checks.
  • Census rerun: it takes about two hours; expected result is every class and reason count unchanged.
  • Recordings beyond the survey's 328, because the sample is not the catalog; a dataset that shows another pattern is one MATCH_RULES entry and a test row.
  • Safari and Firefox (only Chromium was available); the new tag uses existing theme tokens and was not looked at in light and dark screenshots.

Patch notes for other owners

  • For nemar-cli and biosigIO: in on005415, on004551, on005007, on004473 and on006136 the Zarr store's labels differ from the dataset's own channels.tsv names (the Zarr index already counts them as units_report.unmatched_channels). Writing the BIDS channel name into the store's label would remove the need for these rules for those datasets.
  • For dataset maintainers (on004624, nm000377, nm000436): the channel names carry no usable link to the contact names (P2, P3, P7), so their Brain toggle stays closed; naming the channels as the contacts would open it.
  • For IS-QA1: the e2e adds on005415 (toggle, tag, status, notes, a saved SVG) and the exact-only check of on005522 and nm000376; the panel chunk is at 35,996 gzipped.
  • For IS-D1 (the guide): the tag and the status line's "joined by name rules" are the reader's cue that a name was read by a rule, and the recorded line is the file's text.
  • The AGENTS.md architecture map still has no src/lib/ieeg-spatial/ entry (outside this card).

Card: IS-J1.

153 public files and Zarr label excerpts from 24 recordings of 23 datasets, with dataset, version, path, URL and SHA-256 in SOURCES.json, and the loader the next tests share.
Ordered tiers (exact, normalized, suffix, close) as a table of named rules read from the survey, ambiguous_name for two readings, claims so an exact name keeps its contact, and the file's own coordinate cells carried through the parser. close has no rule: the one candidate is right in one dataset and wrong in another. Tests read the committed real recordings; each of the card's six mutations fails its test.
Rows, titles, the Selected lines, notes and the status line name the contact as recorded, the channel beside it and the kind of match; the recorded line is the file's own text with its unit word and frame; an approximate match is drawn half transparent. Unit, provenance and headless e2e tests; on005415 now has a Brain toggle.
Counts per pattern over 328 recordings of 93 datasets, each rule's rows, and the pending patterns with the reason.
@cloudflare-workers-and-pages

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

Copy link
Copy Markdown

Deploying nemar-website with  Cloudflare Pages  Cloudflare Pages

Latest commit: 474cb44
Status: ✅  Deploy successful!
Preview URL: https://61e0c147.nemar-website.pages.dev
Branch Preview URL: https://feat-is-j1-robust-join.nemar-website.pages.dev

View logs

Near-miss names (A1-2, A1-AVG, pol A3, A1-Reference and more) are pinned unlocated so a loosened rule anchor fails a test; zero padding never carries a name across a shaft (L01 does not join L1 beside L0); bipolar pairs are claimed like contacts (POL A1-A2 does not take A1-A2); the -AV and -REF sentences say what the name does, not what it means; P3 evidence corrected (CH0 to CH31 plus four state_* channels); the close marker's opacity limit is documented; the alpha test uses a literal. Each new test fails under its mutation.
scripts/ieeg-channel-survey.ts re-derives the note's counts (name-resolved and located, the _channels.tsv corroboration, per dataset) over the committed list of 338 recordings; --sample draws a fresh list. Three runs the same day agree.
How to re-derive the counts, what channels and located mean, the _channels.tsv corroboration by rule and dataset, and the corrected P3 evidence.
Non-exact POL A1-A2 and POL A2-A1 both refuse (duplicate_contact) while exact A1-A2 and A2-A1 both stay; the reason is documented at settleClaims. Behavior unchanged. The sorted-key mutation now fails this test.
@neuromechanist
neuromechanist merged commit 6cba53d into feature/issue-537-epic-spatial Nov 18, 2026
5 checks passed
@neuromechanist
neuromechanist deleted the feat/is-j1-robust-join branch November 18, 2026 13:38
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