Skip to content

MAT v5 walker; EEGLAB, Homer .nirs, .mat readers (A5) - #2395

Merged
neuromechanist merged 24 commits into
feature/issue-2182-epic-pq-evidencefrom
feature/issue-2187-mat-eeglab
Nov 26, 2026
Merged

neuromechanist merged 24 commits into
feature/issue-2182-epic-pq-evidencefrom
feature/issue-2187-mat-eeglab

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Part of #2187. Part of #2182.

Card A5 of the publication-queue plan: EEGLAB .set (classic and v7.3), Homer2 .nirs and generic .mat files are now screened by readers that never read signal arrays. All three are registered beside A6's ZIP reader. A .set inside a ZIP is read by the EEGLAB reader, and its .fdt is looked for in the archive.

What changed

  • shared/mat-meta.ts: a bounded, dependency-free MAT-file Level 5 walker. It walks struct, cell and char arrays. A numeric array over 64 elements is skipped by its declared size: in an uncompressed element only its tag, flags, dimensions and name are read, and the entry records the unread range. miCOMPRESSED is inflated as a stream through DecompressionStream, with fetches starting at 4 KiB and growing to 64 KiB. A top-level compressed variable that turns out to be a large signal is left after its first bytes. Inflation is capped at 256 MiB per file (limit), with walk caps of 1,000,000 arrays and depth 32. Sparse, object, function-handle and opaque arrays are skipped after their flags as unsupported-datatype. A Level 4 file is unsupported-version; damage is corrupt.
  • shared/hdf5-meta.ts (A3): an opt-in matlabChars option reads a uint16 dataset whose MATLAB_class is char as text, one string per MATLAB row, under the 64 KiB string rule. It is off by default, so SNIRF and HDF5 walks are unchanged. walkForReader passes the option through.
  • shared/readers/mat-generic.ts: the shared MATLAB layer. It dispatches on the header (v5, v7.3 to A3's walker through walkForReader and HDF5_INCOMPLETE_REASON, v4, or another format). It converts v7.3 entries to the same arrays (MATLAB_class, MATLAB_empty, canonical empty, /#refs#). It applies the unscanned-text rule: a skipped char array, a line over 4 KiB, a skipped identifier-key numeric or an unsupported class makes the file partial. scanMatText is A4's scanEntryText (identifier keys, scanNumericIdentifier, person fields, home paths, A2's contactIn and the 4 KiB cap) under the MATLAB kinds. Findings are folded to one per kind and field per file, with the count in Finding.occurrences. The generic .mat reader is in this file too.
  • shared/readers/eeglab.ts: the EEGLAB rules. The subject field uses readSubjectId (eeglab-subject-name is identifier, eeglab-subject-freetext is review). history and setname get the person-name rule only. In the event tables, type gets the person-name rule only and every other column is not scanned. Channel labels never fire, datfile/data name the .fdt, and EEG.BIDS gets the free-text rules without the person-field rule. Everything else gets the free-text rules.
  • shared/readers/homer-nirs.ts: every array goes through the free-text rules.
  • shared/readers/person-fields.ts: the qualified-name rule, description as a describing word, and isLocationField. All three are shared by every format.
  • ReaderResult.needs: companions named by the file's content, as bare file names only, resolved in the recording's own directory. namedCompanionsMissing counts them in readRecordings, in A9's local screen and in A6's ZIP reader.
  • Eight new kinds with KIND_COPY entries: eeglab-subject-name, eeglab-subject-freetext, eeglab-event-name, eeglab-history-name, eeglab-setname-name, mat-freetext, mat-freetext-name and mat-identifier-field. The direct kinds are eeglab-subject-name, mat-freetext-name and mat-identifier-field. The report's kind map cap is now FINDING_KINDS.length instead of 40.

Deviations (lead rulings of 2026-11-25)

  1. maxElements is 1,000,000, not 20,000. At 20,000, 73 of 218 classic public files were partial: event tables hold one array per field per event (p50 46k and max 741k among those). The 741,517-array file (ds003825) took 3.4 s of CPU from a local copy and about 100 s over a home connection, fetching 45 MB in 686 reads. That is under the 180 s cap and its 80% walk share, and the timer test is kept.
  2. The person-name rule for event types, history and setname fires on two capitalized name-like words in a row, after path-like text is removed. The scanner's single-word test flags almost every event word (141 of 246 files).
  3. Location fields (isLocationField: a last name word of path, filepath, file, filename, dir or folder, or a name starting with original_) are not scanned in any MATLAB reader. The ruling on filepath covers chaninfo.filename (home path in 38 of 246 files), spline, dipfit, Homer systemInfo.SDfilepath and FieldTrip-style *_dir. setname gets the person-name rule and no path rule.
  4. ReaderResult.needs is a small additive contract change (registry, builder, ZIP kit), with a test that a datfile with a separator is not followed and the file is partial.
  5. EEG.BIDS.* mirrors the BIDS sidecars, so it gets no person-field rule. In the shared person-field rule, a name after a word that is neither a person word nor a describing word names a thing (LongName, DepartmentName), and description is a describing word. The SNIRF, HDF5, NIRx and BrainVision fixtures give identical findings before and after (25 files).
  6. In the event, urevent and epoch tables, only type is scanned; every other column is not, as with events.tsv.
  7. Findings are folded to one per kind and field per file, with occurrences.
  8. Free text is read line by line before the 4 KiB cap, so a long history of short commands is not partial.
  9. v7.3 cell members and struct-array fields (event types, labels) sit behind object references under /#refs# without their field names, so they get the free-text rules (paths, contacts) rather than the person-name rule.
  10. Existing tests that used .set or .mat as unscreened examples now use .nwb or .xdf.

False-positive gate

scripts/dev/reader-fp-measure-remote.ts, anonymous HTTP range reads of OpenNeuro through the registry's page cache; nothing is written to disk.

Reader Files (datasets) Complete Partial Failed
eeglab 246 (83) 228 18 (limit 9, unsupported-datatype 9) 0
homer-nirs 20 (1, the only public .nirs) 20 0 0
mat 254 (29) 201 53 (unsupported-datatype 20, time 16, limit 12, corrupt 5) 0

Findings per 100 files (identifier / review), before the rulings and after:

Kind .set before .set after .nirs before .nirs after .mat after
mat-freetext-name 3004.07 / 0 (57 files) 0 / 0 0 / 0 0 / 0 0 / 0
mat-freetext 0 / 238.21 0 / 6.10 0 / 100.00 0 / 0 0 / 7.09
mat-identifier-field 0 / 0 0 / 0 0 / 0 0 / 0 0 / 1.97
eeglab-subject-name 0.81 / 0 0.81 / 0
eeglab-subject-freetext 0 / 1.22 0 / 1.22
eeglab-event-name 0 / 3.66 0 / 3.66
eeglab-setname-name 0 / 4.88
  • Identifier: 2 files (ds004350) whose subject is two capital letters, probably initials. This is an explained true positive.
  • eeglab-event-name (3 datasets): ds003739, ds004295 and ds004588. Each hit is a pair of dictionary words in an event type, not a name (word classes checked, values not printed).
  • eeglab-setname-name (4 datasets: ds004105, ds004106, ds004117, ds004119): task words in the set name, not names.
  • mat-freetext on .set (5 datasets): home paths in comments (ds002158, ds003061, ds003670, ds004348) and in the BIDS README copy (ds003602). The path rule stays on both by ruling.
  • mat-freetext on .mat: FieldTrip provenance (cfg.callinfo.pwd, cfg.version.name, cfg.toolbox.cleanup) in ds003702 and ds004475, plus ds004563. mat-identifier-field (review) is ds004563 only.
  • time partials on .mat come from a home connection with 6 files in parallel.

Bounds

  • Uncompressed .set with its signal inside (ds003800 fixture): no range the walker asks for overlaps the data payload (2.28 MB of the 2.33 MB file), and under 64 KiB is asked in all.
  • The inflation cap, lowered for the test, ends the walk with limit; a cap set to exactly the bytes needed does not.
  • None of the real fixtures, and no public file in the gate's sample, is partial because the signal precedes the metadata. No public .set in the sample was compressed with its signal inside one variable; the two compressed single-variable files had an .fdt. So the stop condition did not arise.

Fixtures

test/fixtures/mat/README.md records source, commit or dataset, license, SHA-256 and the edits for each fixture: eegprep's eeglab_data.set (BSD-2), ds003800's embedded .set (CC0), Homer3's Simple_Probe.nirs (BSD-2), ds003490's ONOFF_4Oddball.mat (CC0), and two edited copies with a made-up subject and a home path in filepath (classic, written by scipy; v7.3, edited with h5py). Each fixture is checked against a scipy or h5py oracle (scripts/dev/mat-oracle.py).

Tests

  • test/mat-meta.test.ts: the walker against scipy for every Level 5 fixture, the v7.3 char arrays against h5py, bounds, caps in both directions, opaque and sparse classes built byte for byte, damaged input and source failures.
  • test/readers-eeglab.test.ts, test/readers-homer-nirs.test.ts, test/readers-mat-generic.test.ts, test/readers-person-fields.test.ts: every rule with a twin; the companion through readRecordings and through the local screen (screenLocalDataset, a .set with and without its .fdt); a .set inside a ZIP in test/readers-zip.test.ts.
  • Revert checks: 44 one-line mutations (walker caps in both directions, chunk size, top-level skip, opaque skip, v7.3 transpose and option, every unscanned rule, limit, time, not-applicable, every EEGLAB rule, location predicate, folding, companions, registration, person-field changes and direct kinds). Each was applied and each fails the targeted tests.

Gates (head 2a0b7b2)

bun run typecheck and bun run lint clean; bunx biome check on every touched file: no fixes. Targeted tests (the five the card names plus zip, coverage contract, scan, screen report, local screen, preflight and the other readers): 662 pass, 0 fail. Pure tier: 28,129 pass, 3 fail; the failures (website account-copy parity, which reads a sibling website checkout, and the git-annex UUID retry test) fail the same way on the base branch.

Review round 1

  • No declared count drives a loop. A cell or struct must be able to hold its members in the bytes left, at 8 bytes each, and the members are charged to the element cap before the walk starts. A count past 2^53 is corrupt. A struct with no fields walks nothing: MATLAB writes struct() as 1 x 1 with no fields, so this is not corrupt; failing it made 21 public files corrupt. A 1e9 x 1e9 struct returns at once as corrupt.
  • A numeric data tag may declare at most count * element size + 8 bytes, otherwise corrupt, and at most the numeric cap is converted.
  • Cells and structs must end exactly at their last member, top level included. Field-name width must be greater than 0 and the names length a multiple of it.
  • Every top-level element counts toward the element cap. A top-level element that runs past the end of the file is corrupt and is not read. Direct reads check for overflow.
  • unscannedMat skips location fields and FieldTrip provenance, so a long filepath does not make a file partial.
  • Deviation 11 (FieldTrip ruling): pwd and cwd are location words, and path joins the run-together suffixes. nosedir, redir and profile are not locations. cfg.version, cfg.callinfo and cfg.toolbox are provenance and are not read at any depth; cfg.previous is walked. Re-measured .mat gate (254 files): mat-freetext 0/1.97 and mat-identifier-field 0/1.97, both ds004563; this was 0/7.09 for mat-freetext.
  • Finding.count (reader-internal, not reported) is now the single count field for the ZIP and MATLAB readers, replacing entries and occurrences.
  • Docblocks: the location predicate is for every MATLAB reader. eeglab.ts now states the label skips, that non-char subject, history and setname fire nothing, and how ALLEEG and other variables are handled.
  • Character codes are converted in slices.
  • 16 new mutations (count, bytes, charging, fieldless loop, tag bound in both directions, leftovers, width, ragged slots, top-level counting, past-EOF, unscanned locations, provenance, nosedir). Each was applied and each fails the targeted tests.
  • .set gate re-measured after these fixes: unchanged (228 complete, 18 partial: limit 9, unsupported-datatype 9; no new corrupt).

@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR2395

Version: 0.10.40-PR2395.0016124
Commit: 0016124


🤖 Updated on each push.

@neuromechanist

Copy link
Copy Markdown
Contributor Author

Merge gate (lead): head 0016124, every remote check green. Round 1 (two Sonnet reviewers): 1 critical (synchronous loop on a fieldless struct array with a huge count), 5 important and 5 suggestions, all fixed in e401e6a. Round 2 (delta): items resolved; 1 important (text() row-count loop) fixed in 0016124 and verified by the lead with the reviewer's repro (0 ms, corrupt) and a mutation removing the check (fails a test). Accepted deviations: a fieldless struct with count > 0 and a 1x0 char array are valid MATLAB and walk nothing.

@neuromechanist
neuromechanist merged commit 098d30a into feature/issue-2182-epic-pq-evidence Nov 26, 2026
31 checks passed

This branch was previously deployed

1 inactive deployment
npm — 00161241 Deployed Nov 26, 2026 by neuromechanist via publish #4969
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