Skip to content

IS-V2: template anatomy for template frames - #556

Merged
neuromechanist merged 13 commits into
feature/issue-537-epic-spatialfrom
feat/is-v2-anatomy
Nov 18, 2026
Merged

neuromechanist merged 13 commits into
feature/issue-537-epic-spatialfrom
feat/is-v2-anatomy

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

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

What changed

When the shown frame is a standard template identifier with a cited row, the Brain panel draws the vendored MNI152NLin2009cAsym surface (IS-T1) behind the contacts, after a cited affine where the frame is not the surface's own, and always shows the words "Template brain (MNI152NLin2009cAsym), not this subject's anatomy", plus "approximate: ..." whenever a transform or another MNI variant is involved. Every other frame keeps the contacts-only view and says No template placement for FRAME: positions are shown in the dataset's own frame.

  • src/lib/ieeg-spatial/transforms.ts (new): the registry, the two cited affines, the label and sentence words, the surface's file names, mergeMeshes (the two hemispheres as one mesh for setMesh, 16-bit indices) and loadTemplateSurface.
    • MNI152NLin2009cAsym: the surface's own frame, no transform, no "approximate".
    • every other MNI152* identifier and IXI549Space: identity, "approximate: MNI template variants differ by a few millimeters".
    • MNI305, fsaverage and fsaverageSym: FreeSurfer's published MNI305 to MNI152 affine, "approximate: MNI305 to MNI152 affine". fsaverageSym rests on the BIDS appendix sentence it shares word for word with fsaverage ("the volumetric template corresponds to a FreeSurfer variant of MNI305 space"); the surface is a symmetric variant of fsaverage, so the relation is approximate (driver ruling, round 2).
    • Talairach: the inverse of Lancaster et al.'s Eq. (3), "approximate: Talairach to MNI affine".
    • Every other identifier of the pinned BIDS appendix is pending, not silently absent: MNIColin27, ICBM452AirSpace, ICBM452Warp5Space, fsLR, NIHPD, OASIS30AntsOASISAnts, OASIS30Atropos and UNCInfant. Each has an entry in src/lib/ieeg-spatial/pending-templates.ts with the reason it has no row and the exact evidence that would place it (source, relation, worked example); the panel reads No template placement yet for X: positions are shown in the dataset's own frame for them, distinct from No template placement for X: ... (ACPC, ScanRAS, Other, Pixels, non-standard labels, and a placed identifier whose units are not millimeters). None of the eight appears in the census. fsLR stays pending by ruling: an fsLR-labeled position may be a surface vertex coordinate, which no source covers, even though BIDS names MNI152NLin6Asym as its volumetric template. See "Findings per family".
    • IXI549Space keeps its identity row as the one documented exception to the policy that a placed row needs a stated relation to the MNI152 frame: BIDS says it is "linearly transformed to ICBM MNI 452", a relation to ICBM452 (itself pending), not to an MNI152 template. The row's comment and source say so, a test pins it, and a source stating its relation to an MNI152 template with a worked pair would settle it (or the row moves to pending).
    • A docblock at the top of the registry says how to add a family (source, relation or matrix, worked example, fixture, row, label wording, tests, and deleting the pending entry), so a future card is mechanical.
    • The Talairach row is approximate by construction, and its source comment says so: Eq. (3) is a group-level bias model fitted on SPM2 and FSL normalizations. Table V's landmark disparities, at best 0.5 to 2.7 mm (8 landmarks, 10 brains), are for the per-package best-fit transforms (Eqs. 1 and 2); the pooled Eq. (3) is not tabulated, so its residuals are likely somewhat larger. A dataset's own Talairach frame may also come from another pipeline such as FreeSurfer's talairach.xfm. The excerpt fixture now carries Table V's values, and a test pins the range to them.
  • src/lib/ieeg-spatial/pending-templates.ts (new): the pending table. Only tests import it (a test pins that no runtime module does), so none of its text ships in the panel's chunk; the panel derives "pending" from the registry (isPendingTemplate: a template frame the registry has no row for), and transforms.test.ts fails unless every such identifier has an entry.
  • src/lib/ieeg-spatial/panel.ts: a template status in the state machine (idle, loading, ready, failed) and a load_template effect, emitted only while the panel is open on a registered frame with a canvas to draw on (never for any other frame, never without WebGL2); the template model with the label, the Fonov et al. reference link (doi) and the link to the MNI notice (LICENSE.txt); the no-placement line in frameNotes; drawnPositions (the contacts through the row's affine only while the surface is drawn, index for index with Placement.contacts); the DOM half fetches the two files once per page, sets the mesh, and re-sets the contacts. A failed fetch leaves the contacts as declared with a sentence and no label; reopening the panel, a frame change or a recording change tries again.
  • src/styles/eeg-viewer.css (one rule, see Deviations): the panel's items no longer shrink.
  • e2e/ieeg-spatial-panel.spec.ts: 7 new tests, see Gates.
  • test/fixtures/ieeg-transforms/: excerpts of the two sources (FreeSurfer's item 8 and 8b, the matrix rows of Lancaster's Eq. 3), with their sources and SHA-256 in SOURCES.json.

Gates (head fc30ca5, round 2 delta review fixes; the epic tip 221321e is in the branch)

  • bun install --frozen-lockfile: Checked 416 installs across 568 packages (no changes)
  • bun run typecheck: Result (507 files): 0 errors, 0 warnings, 19 hints (the base's 19)
  • bun run lint: Checked 438 files in 198ms. No fixes applied.
  • bun run test src/lib/ieeg-spatial/transforms.test.ts src/lib/ieeg-spatial/panel.test.ts: Test Files 2 passed (2), Tests 114 passed (114) (32 and 82)
  • bun run test src/lib/ieeg-spatial: Test Files 8 passed (8), Tests 282 passed (282)
  • bun run test: Test Files 185 passed | 3 skipped (188), Tests 5308 passed | 14 skipped (5322)
  • bun run test:e2e e2e/ieeg-spatial-panel.spec.ts against bun run dev, PUBLIC_DATA_BASE_URL=https://data.nemar.org, headless: 17 passed (1.3m) on this head (10 existing, 7 new). New: on005522 draws the surface with the label and "approximate: MNI template variants differ by a few millimeters", links the notice and the reference, requests nothing before the panel opens and exactly lh.mesh.bin and rh.mesh.bin after, and does not refetch when the panel is closed and reopened; nm000355 (ACPC) draws no surface, requests no mesh and says No template placement for ACPC: positions are shown in the dataset's own frame; nm000454 (own frame), nm000485 (Talairach), nm000376 (IXI549Space) and on004100 (fsaverage) carry the label each row owes them; a failed mesh fetch leaves the contacts, says so without a label, and a reopened panel recovers; the EEG case requests no mesh. A run on the previous head had one transient failure of an unrelated embed load (on004100 never showed its viewer within 90 s; no panel code was involved); the run on this head passed 17 of 17, as did the one before it. Measured ink share of the panel canvas (the e2e's own evidence that a brain is drawn): 0.166 to 0.170 with the surface on four datasets, 0.045 for 152 contacts alone (nm000355); the bounds are 0.12 and 0.09.
  • Bundle, measured with bunx astro build and gzip -9c <chunk> | wc -c. Panel chunk: 58,830 raw and 21,645 gzipped at the epic tip; 66,990 raw and 24,736 gzipped on this head (+8,160 raw, +3,091 gzipped; the previous push measured 66,433 and 24,497, so this round added +557 raw and +239 gzipped: two longer row sources and the fsaverageSym row). A first version with the pending text inside transforms.ts measured 69,812 raw and 25,763 gzipped; moving the table to a module only tests import saved 1,266 gzipped bytes, and a grep of the build finds none of its text in any chunk. Viewer chunk: 166,137 raw and 58,277 gzipped at the epic tip, 166,137 and 58,278 now (the panel import's file name only). The two hemisphere files (266,170 and 266,092 bytes; 214,437 and 215,120 gzipped) are in no chunk and are fetched only on a placed frame.
  • Leak check on every written file, the new pending-templates.ts included, and on this body: all three commands print nothing (the four source sites whose hosts are not on the check's list are named in words below, not linked).

Revert checks

Round 2 (this push), each a one-line mutation that fails the named test, reverted, tree clean after each: the Talairach wording back to claiming disparities "left" after Eq. 3, the range changed to 0.5 to 3.1 mm, the "per-package fits; pooled Eq. 3 not tabulated" words dropped, and a Table V value in the fixture changed (each fails the Talairach wording test, which reads Table V's values from the excerpt); the fsaverageSym row with the identity (3 failing, the worked example through the registry and the panel test), removed (7 failing), with its "so approximate" words dropped, with the Talairach label, or as a variant row (4 failing); the fsaverageSym entry put back in the pending table (the exhaustiveness test fails: both); IXI549Space given the plain variant source, or its "documented exception" or "not to an MNI152 template" words dropped, or the word "exception" added to another row's source (each fails the IXI549Space test).

Round 1 of the registry request (earlier push): this round's new tests (registry exhaustiveness, pending entries, the pending sentence, the import guard, the panel test), each failing under a one-line mutation and reverted, tree clean after each: a pending entry deleted (the appendix test fails: neither placed nor pending), a placed row deleted (the same test and the example tests), a family put in both tables, a pending entry with an empty needed, with a needed lacking the worked example, with a one-word reason, noPlacementSentence without the yet (the transforms test and the panel test), isPendingTemplate always false (4 failing), a pending family given a placing fallback in placementRuleFor (3 failing), and panel.ts importing the pending table (the import guard fails).

Earlier rounds:

Each test was shown to fail under a one-line mutation, then reverted (git status --porcelain clean after each). Unit: a transposed MNI305_TO_MNI152 (8 failing), a transposed MNI_TO_TALAIRACH (4), TALAIRACH_TO_MNI set to the forward matrix (6), the fsaverage and Talairach rows with the wrong matrix (4, 6), units.unit ignored (2), the variants list shortened (6), a variant row with a real transform (killed); load for every frame (templateRuleOf dropped from wantsTemplate: 3 failing), open not required (4), canvas not required (1), drawnTemplate without ready (2), contacts not transformed (2), failed retry removed (2), WebGL2 withdrawal removed (2), the no-placement line removed (6), a report accepted when not loading (2), the label dropped for identity rows (4 in templateLabel, 4 for the approximate-only variant, 2 in the model), the loading state claiming a drawing (2). The redundant kind branch in placeOnTemplate survived as an equivalent mutant and was removed. e2e: load for every frame (nm000355 fails), label dropped (on005522 fails), mesh never set (on005522 fails), the CSS rule removed (the toggle test fails, as it did before the rule existed).

Findings per family (researched 2026-11-18, GET only, primary sources)

Every identifier of the pinned appendix table is accounted for: 14 placed (the surface's own frame, nine variants including IXI549Space, MNI305, fsaverage, fsaverageSym, Talairach) and eight pending. A row is placed only where a primary source states the relation and a worked example (or an identity relation) can be tested; every other family stays pending, with what would settle it in pending-templates.ts. Four source sites (the LONI atlas page, the NITRC atlas page, the figshare record and the Mindboggle page) have hosts that the repository leak check does not list, so they are named here and not linked: each is the URL the BIDS appendix table lists for its family.

Family What was found Source Status
MNI152NLin2009cAsym The surface's own frame public/vendor/brain-templates provenance placed, own
other MNI152* The BIC 2009 page: "All templates are describing the same anatomy, but sampling is different"; the Lin page: each individual linearly registered to MNI305 https://www.bic.mni.mcgill.ca/ServicesAtlases/ICBM152NLin2009, https://www.bic.mni.mcgill.ca/ServicesAtlases/ICBM152Lin placed, identity, approximate (unchanged)
IXI549Space BIDS: the average of 549 IXI subjects "linearly transformed to ICBM MNI 452", a relation to ICBM452 (pending), not to an MNI152 template BIDS appendix, https://github.com/bids-standard/bids-specification/blob/074ae6ffcdcfa27f07e131f42a545574f20675c8/src/appendices/coordinate-systems.md placed, identity, approximate: the card's row and the one documented exception to the policy; settled by a source stating its relation to an MNI152 template with a worked pair
MNI305, fsaverage FreeSurfer's matrix with a worked example, both directions https://surfer.nmr.mgh.harvard.edu/fswiki/CoordinateSystems placed, affine, approximate (unchanged)
fsaverageSym BIDS: its volumetric template "corresponds to a FreeSurfer variant of MNI305 space", word for word the clause the fsaverage row rests on; FreeSurfer's Xhemi page adds nothing on fsaverage_sym coordinates; the surface is a symmetric variant of fsaverage BIDS appendix (above), https://surfer.nmr.mgh.harvard.edu/fswiki/Xhemi placed, affine (the MNI305 matrix and its worked example), approximate: driver ruling, round 2
Talairach Lancaster et al.'s Eq. (3); no worked pair https://doi.org/10.1002/hbm.20345 placed, affine, approximate (unchanged; owner may veto)
ICBM452AirSpace LONI atlas page: an average space built from the average position, orientation, scale and shear of 452 brains; no matrix to any MNI152 template the LONI atlas page the appendix lists pending: needs the affine to an ICBM152 variant and a worked pair
ICBM452Warp5Space BIDS: a 5th-order polynomial warp, so not an affine; nothing prints its relation to MNI152 BIDS appendix (above) pending: needs the polynomial, a new registry kind and a worked pair
MNIColin27 TemplateFlow README: "This average was linearly registered to the average 305"; the BIC page: stereotaxic space on a 0.5 mm grid; no matrix printed https://github.com/templateflow/tpl-MNIColin27, https://www.bic.mni.mcgill.ca/ServicesAtlases/Colin27Highres pending: the relation is qualitative; needs the Colin27 to MNI305 matrix and a pair
NIHPD BIC: six age ranges in two flavors under one identifier, each nonlinearly averaged in "Talairach-like MNI stereotaxic space"; no transform to MNI152, and the identifier does not say which template https://www.bic.mni.mcgill.ca/ServicesAtlases/NIHPD-obj1, https://doi.org/10.1016/j.neuroimage.2010.07.033 pending: needs the age range and flavor (SpatialReference), a transform per template and a pair
OASIS30AntsOASISAnts, OASIS30Atropos TemplateFlow tpl-OASIS30ANTs: its own grid, no space statement; the figshare record and the Mindboggle page describe the ANTs and Atropos templates with none either https://github.com/templateflow/tpl-OASIS30ANTs, plus the two pages the appendix lists pending: needs a statement of alignment to MNI152 and a pair, per template
UNCInfant TemplateFlow tpl-UNCInfant: three age cohorts (neonate, 1 year, 2 years) on their own grids; the NITRC page relates none to adult MNI space https://github.com/templateflow/tpl-UNCInfant, plus the NITRC page the appendix lists pending: needs the cohort, a transform to an adult template and a pair; an infant surface may be needed
fsLR BIDS: a surface template whose volumetric template is MNI152NLin6Asym; TemplateFlow tpl-fsLR holds surfaces only and cites Van Essen et al. 2012 https://github.com/templateflow/tpl-fsLR, https://doi.org/10.1093/cercor/bhr291 pending by ruling: an fsLR-labeled position may be a surface vertex coordinate, which no source covers, even though BIDS names MNI152NLin6Asym as its volumetric template; candidate is an identity variant row once a source prints how vertex and volume coordinates relate, with a worked pair

Rulings (driver, after the delta review): fsaverageSym is placed because its BIDS clause is the one fsaverage already rests on, together with FreeSurfer's MNI305 to MNI152 worked example; fsLR stays pending for the reason in its row. The owner may veto either; placing or unplacing a family is a one-line change in the registry and its pending entry.

Stop condition reached, for the lead (Talairach)

The card's first stop condition: a cited source gives no worked example to test against. FreeSurfer's page does (MNI305, both directions, pinned to 0.01 mm, and read from the committed excerpt). Lancaster et al. 2007, BrainMap's m-files and NiMARE give no worked point pair for Talairach to MNI: the paper prints the matrix (Eq. 3) only, the m-files carry code only, and NiMARE's test says its own "true" values are a TODO ("Get converted coords from official site"). The run could not ask, so this PR carries the Talairach row with the strongest evidence available and marks the acceptance open:

  • Driver ruling after review round 1: keep the Talairach row, labeled approximate (the reviewer re-verified every coefficient against the paper's full text, the BrainMap m-files and NiMARE, and recomputed the direction: the origin lands 0.842 mm from the paper's mean FSL anterior commissure, the wrong direction misses by 8.6 mm). The owner may veto; the row is one row("Talairach", ...) line in TEMPLATE_PLACEMENTS to delete, and the panel then shows No template placement for Talairach.
  • The matrix agrees digit for digit across three copies: the paper's Eq. 3 (read from the equation image of the open-access full text), BrainMap's icbm_other2tal.m and tal2icbm_other.m, and NiMARE's tal2mni and mni2tal (so the 0.5 mm stop condition did not trigger).
  • The direction: Eq. 3 is named "MNI to Talairach" and the m-files apply it that way; the paper says the MNI origin lies about half a centimeter from the Talairach origin (translation 3.65 mm in z) and its Table IV text puts FSL-fitted MNI brains' mean anterior commissure at (0.3, 1.4, -4.4). The test pins that the Talairach origin (the anterior commissure) lands within 1 mm of that mean (it lands 0.84 mm away) and that the wrong direction misses by more than 3 mm. Plausibility, not a 0.01 mm example.
  • The inverse: an independent exact-fraction computation (Python's fractions, Gauss-Jordan on the four-decimal entries) is the known answer for five points to 1e-6 mm; (-44, 31, 27) agrees with NiMARE's test row to its eight decimals.
  • Real data does not help: nm000485 lists both Talairach and MNI columns for the same contacts, rounded to integer millimeters, and the identity (mean 3.9 mm off) is closer to its Talairach column than Lancaster's transform (8.2) or Brett's (4.9), so those authors used another conversion; it cannot serve as a reference.

Deviations from the card

  1. Talairach: the stop condition above.
  2. The ACPC e2e uses nm000355, not on005415. on005415's Zarr store labels its channels LTP1-20000 where the BIDS files say LTP1; the panel joins by exact name only (IS-C1), so every on005415 recording has a disabled toggle ("None of this recording's channels match a contact") and no panel to open. nm000355 is a native-frame dataset (ACPC) whose labels join (152 of 152 located).
  3. One CSS rule in src/styles/eeg-viewer.css (outside the card's Scope): .eegv__spatial-inner > * { flex-shrink: 0 }. The two lines the template adds under the canvas pushed the panel past the scope's height beside the scope; the contact list, a scroll container and so the one flex item allowed to shrink below its content, collapsed and its rows were drawn over the activity block, which failed the existing Escape test (click intercepted). With the rule the panel scrolls inside itself, as its own comment says it should.
  4. Fixtures added (outside the card's Scope): test/fixtures/ieeg-transforms/, because the worker preamble wants the numbers a test compares against to come from a committed, sourced file. The m-files and the paper are not redistributed; only the matrix rows and two quoted sentences.
  5. Registry reading of "all standard template identifiers" (owner request, this round). The registry is exhaustive over the pinned appendix's table: each identifier is a row (the card's and ADR 0033's table) or a pending entry, and a test fails otherwise. The appendix's deprecated spellings (fsaverage3 to fsaverage6, UNCInfant0V21 and kin) stay unknown frames in geometry.ts (IS-C1's rule) and never reach the registry: they read No template placement for X. The pending sentence has no trailing period, matching the sentence it is paired with. Rulings after the delta review: fsaverageSym is placed, fsLR stays pending, IXI549Space stays placed as a documented exception; the owner may veto any of the three (placing or unplacing a family is a one-line change in the registry and its pending entry).
  6. A template frame whose units are not millimeters is not placed (nm000373: MNI152NLin6Asym, units n/a, 30 recordings): it gets the no-placement sentence and the existing "units not declared" note, as plan 6.2 says for n/a.
  7. The tests were written after the module, not before; each is proved by mutation (above).
  8. New file src/lib/ieeg-spatial/pending-templates.ts (the owner asked for the work inside transforms.ts): kept apart so its text does not ship; its entries are reached only through tests.

Not run, would check

  • Every CI job green on the final head, because the manager reads gh pr checks.
  • Safari and Firefox: only Chromium was available.
  • The surface on a phone-sized viewport with the panel stacked below the scope: the 360 px e2e passed with the new rule, but the template block was not measured there.

Card: IS-V2. Part of #537

@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: fc30ca5
Status: ✅  Deploy successful!
Preview URL: https://6d6f1608.nemar-website.pages.dev
Branch Preview URL: https://feat-is-v2-anatomy.nemar-website.pages.dev

View logs

@neuromechanist
neuromechanist merged commit bbe44a8 into feature/issue-537-epic-spatial Nov 18, 2026
5 checks passed
@neuromechanist
neuromechanist deleted the feat/is-v2-anatomy branch November 18, 2026 05:42
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