Skip to content

A2: BrainVision and NIRx text-header readers - #2381

Merged
neuromechanist merged 10 commits into
feature/issue-2182-epic-pq-evidencefrom
feature/issue-2185-text-headers
Nov 26, 2026
Merged

neuromechanist merged 10 commits into
feature/issue-2182-epic-pq-evidencefrom
feature/issue-2185-text-headers

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Part of #2365. Part of #2182.

Card A2 of the publication-queue plan. It adds BrainVision (.vhdr, .vmrk) and NIRx (.hdr, .inf) readers, which plug into the A1 registry, so datasets of those formats leave "recordings NOT screened". This revision applies the lead's rulings of 2026-11-25.

What changed

  • shared/readers/brainvision.ts and shared/readers/nirx.ts, with the helpers they share in shared/readers/text-header.ts.
    • Headers are read whole up to 1 MiB; BrainVision marker files up to 16 MiB, through the registry's new per-extension limits.
    • A longer file is partial (limit) and keeps the findings it made.
    • .eeg, .wl1, .wl2, .dat and .evt are never read; each is claimed as a companion of its header.
  • New finding kinds, each with its KIND_COPY entry:
    • brainvision-comment-name (review);
    • brainvision-segment-date (date kind, ADR 0090);
    • brainvision-marker-text (review);
    • nirx-subject-name (identifier, direct);
    • nirx-demographics: an identifier (direct) for a birth date to the day, review for a year or a year and month;
    • nirx-header-date (date kind);
    • nirx-freetext (review).
  • local-user-path now also covers BrainVision companion file names, marker descriptions, and NIRx contact and notes fields.
  • Reader contract (A1), extended minimally and documented in the docblock and the coverage contract test:
    • ReaderResult.coverage may be not-applicable. The builder then counts the file as unscreened under its extension, never as read, partial or failed, and does not look for its companions.
    • ReaderResult.notes carries informational notes. The only one is companion-name-mismatch: counted as companion_name_mismatch (an optional version-2 key, present only when it is not zero), printed with its remedy from NOTE_COPY, and never gating.
    • FormatReader.extensionLimits gives .vmrk its own limits.
  • isNameLike, hasDateFinerThanYear and datePrecision are exported from shared/identifier-scan.ts and built on the scanner's existing rules.
  • .vhdr is removed from OTHER_RECORDING_EXTENSIONS. KNOWN_FORMATS keeps .vhdr and adds .hdr, .inf, .wl1, .wl2 and .evt.
  • scripts/dev/reader-fp-measure.ts is the shared false-positive script. It runs the screen's own readRecordings, optionally against full dataset listings so that companions which were not downloaded still pair, and prints only kinds and counts.
  • The preflight tests that used .vhdr as "a format no reader parses" now use Neuroscan .cnt.

Lead rulings applied (deviations from the card)

  • Companion pairing:
    • Companions are paired by BIDS basename (NIRSport2 drops _config).
    • A missing basename companion stays companion_missing, per A1's contract.
    • A header whose DataFile, MarkerFile or FileName names another file is not a gap: it is the note companion-name-mismatch. Its remedy is to correct the entry to the BIDS file name.
  • .vmrk and .inf: both are registered and read, and read_by_format counts files, not recordings.
  • Marker files: the byte limit is 16 MiB.
  • NIRx demographics: an age alone fires nothing. A birth date follows A4's rule: to the day it is an identifier; a year or a year and month is review; an empty or placeholder value is nothing.
  • Free text:
    • NIRx contact and notes fields and BrainVision marker descriptions get the local-path rule and email and phone patterns, at review severity.
    • A phone number needs a leading + or a label such as tel or phone, because bare digit groups look like marker codes.
    • The name rule runs only in person fields. The list is A4's (name, subject, patient, operator, experimenter), defined in text-header.ts because A4 has not merged. Fields that describe the person (age, contact) or name a thing (experiment_name, a file) are excluded.
  • Files that are not the reader's format: an Analyze .hdr and a Windows .inf are not-applicable for the NIRx reader, and an AppleDouble ._ file is not-applicable for the BrainVision reader. The test uses a real Analyze header from NiBabel (MIT).
  • Fixture licenses: the MNE NIRx fixtures cite BSD-3-Clause.
  • .dat pairing: a .dat is paired by basename only, because companions() never sees file contents.
  • NIRx gate: the 200-file false-positive gate is waived for NIRx because public NIRx raw headers are scarce.

False-positive measurement (final head, after review round 1)

Every downloaded file was deleted afterwards. The per-100 denominator is the files the readers counted: a file a reader found to be another format (not-applicable) is counted as unscreened and is not in it, so the BrainVision denominator is 668 of the 670 files downloaded. The numbers did not change in review round 1.

BrainVision: 670 files (338 .vhdr and 332 .vmrk) from 162 public OpenNeuro EEG datasets, at most 3 recordings each, measured against each dataset's full listing. Of those, 668 are BrainVision files; the other two are AppleDouble ._ files, counted as unscreened.

kind (severity) findings per 100 files datasets
brainvision-comment-name (review) 16 2.4 8
brainvision-segment-date (date kind) 162 24.25 87
brainvision-marker-text (review) 0 0 0
  • There are no identifier findings.
  • 62 files carry the companion-name-mismatch note.
  • Datasets whose reads are incomplete (they would be unchecked): 6 of 162, all because a basename companion is missing from the dataset. Before the rulings it was measured without listings (every dataset), and the earlier rule also made every dataset with a mismatched name unchecked.
  • No marker file reaches the 16 MiB limit.
  • The comment rule's vocabulary was tuned on half the datasets; on the held-out half it flags 5.6 per 100 headers.

NIRx (gate waived): 21 files, all of the public NIRx raw headers found: 19 from the MNE testing data and 2 from MNE's fnirs_motor (OSF).

kind (severity) findings per 100 files
nirx-subject-name (identifier) 3 14.29
nirx-demographics 0 0
nirx-header-date (date kind) 12 57.14
nirx-freetext (review) 0 0
  • The 3 identifier findings are test-condition labels in MNE test recordings: a saturation case, a channel-overlap case and an escaped montage label. None is a person's name.
  • Datasets with an incomplete read: 1 of 2. The exception is fnirs_motor, for which there is no full listing, so its companions were not visible to the pairing.

Tests

  • test/readers-brainvision.test.ts and test/readers-nirx.test.ts run real headers through the registry, the page cache and readRecordings.
  • test/identifier-coverage-contract.test.ts covers not-applicable and notes through the fleet scan, the door and the description.
  • Fixtures and their provenance are in test/fixtures/text-headers/README.md. One .vhdr and one .inf are edited to hold a made-up name.
  • Revert check:
    • First round: 32 mutations, every one caught.
    • This round: 20 mutations, each verified as applied, every one caught. They cover the note (dropped, turned back into a gap, uncounted, unprinted), the marker limit (dropped, widened), the birth-date rule (age restored, a day date demoted, everything promoted, placeholders firing, the kind made non-direct), the free-text rules (contact off, the name rule in notes, bare digits read as a phone number) and not-applicable (NIRx and BrainVision back to partial, the builder counting the file as read, the fleet scan not counting it unscreened).
  • Gates: bun run typecheck, bun run lint, bunx biome check on every touched file, and the targeted tests: readers-brainvision, readers-nirx and identifier-fleet, 236 pass and 0 fail.
  • The screen tests touched by the contract also pass: coverage contract, scan, screen-report, screen-ci, preflight contract, upload preflight, publication-review-evidence, screen-cli, and the backend preflight route and sweep.
  • Pure tier on the first head: 27,914 tests, with 3 pre-existing failures that also fail on the unchanged base. Two are website-parity tests that read a stale local website checkout; one is the git-annex UUID retry test. It was not rerun for this revision because the machine is loaded; it would check the new contract fields against every other screen surface.

Review round 1

  • Bounded free text. FREE_TEXT_MAX_CHARS (4 KiB) and capText live in shared/readers/text-header.ts, so later readers inherit them.
    • Every value is capped before the path, contact and name rules; a value that was cut makes the read partial (limit).
    • The email pattern runs only on a window around each @ (64 characters before, 255 after).
    • The phone rule reads at most 40 characters after each cue, in one linear pass.
    • Timing tests: a 1 MiB single-word line, in a BrainVision [Comment] and in a NIRx contact field, and a 16 MiB .vmrk with one huge description each finish within 1 s, with adversarial words (x@, +1 , tel 1 , a home path).
  • No not-applicable evasion.
    • NIRx returns not-applicable only when no [GeneralInfo] (for .inf, [Subject Demographics]) section exists anywhere and the file starts binary (startsBinary: more than 5% NUL or 10% control bytes in the first 512, never for UTF-16).
    • Text without the section is screened with the free-text rules and is partial/unsupported-version; a Windows driver .inf is now that.
    • BrainVision scans any file that has the first line or a [Common Infos] section.
    • Tests cover a leading comment line, a section after another section, a leading NUL and UTF-16 in both byte orders, for both readers.
  • Decoding.
    • decodeHeader decodes UTF-16 by its byte-order mark (FF FE, FE FF), or by a NUL in every other byte when there is none.
    • It tries UTF-8 and falls back to Windows-1252 whatever a Codepage line declares.
    • It drops every NUL.
  • Contract docblock. not-applicable is decided from the content alone, only when it shows another format, and never for a decode, limit or parse failure.
  • Mutation check: 14 mutations, each confirmed applied, every one caught:
    • Item 1: the cap off or widened; a cut not reported by either reader; the email window off; marker descriptions uncapped.
    • Item 2: NIRx or BrainVision text returned as not-applicable; binary NIRx scanned; the BrainVision [Common Infos] rule dropped; NULs kept; the binary threshold widened.
    • Item 3: the UTF-16 byte-order mark ignored; declared UTF-8 trusted.

Review round 2

  • Control bytes: decodeHeader drops every C0 control character except tab, line feed and carriage return (NUL included) before anything parses the text.
  • not-applicable needs all three of the following (isAnotherFormat in text-header.ts):
    • (a) the raw bytes start binary;
    • (b) no section name of the format occurs anywhere in the decoded text, in any letter case;
    • (c) no Name=, Contact...=, Notes=, Comment= or Mk1= key occurs anywhere.
  • Anything else is scanned: every rule the reader has runs on every section, plus the free-text rules on every line, and the read is partial/unsupported-version.
  • Tests cover the reviewer's probes in both readers: 100 control bytes before [SUBJECT DEMOGRAPHICS]; a binary start with [Marker Infos] and contact details; a [Comment] name or a dated New Segment without [Common Infos]; an .hdr with only [ExperimentNotes]; key-only and lowercase section names. The Analyze and AppleDouble files stay not-applicable.
  • Mutation check: 9 mutations, all caught. They cover the strip narrowed to NUL, each of (a), (b) and (c) removed, (b) made case-sensitive, and the fallback's person, comment, New Segment and free-text rules removed. The fallback's marker-description pass was redundant with the free-text pass over every line, so it was removed.

@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR2381

Version: 0.10.40-PR2381.7840826
Commit: 7840826


🤖 Updated on each push.

* Add the h5py oracle for HDF5 metadata

* Add HDF5 fixtures and their h5py oracle output

* Add a bounded HDF5 metadata walker

* Test the HDF5 walker against h5py

* Count logical reads; default cap 20,000

* Review round 1: never throw, shuffle, zero sizes

* Bound each fuzz walk at 200 ms
@neuromechanist

Copy link
Copy Markdown
Contributor Author

Merge gate (lead): head 7840826. Every remote check passes after one rerun of unit-pure shard 1, which was cancelled at the job time limit on its first run. Review: round 1 panel, 2 important findings (regex backtracking on long values; not-applicable evasion) and 3 more fixed; round 2 delta review found 2 further evasions (control bytes on a section line; BrainVision sections without [Common Infos]), fixed in 7840826 and verified by the lead by rerunning the reviewer's probes (each yields findings, none comes back not-applicable, about 5 ms for the long-line probes) and 247 targeted tests. No third round, per the two-round rule. The lead's rulings on companion naming, the marker limit, ages, NIRx free text and the not-applicable contract are in the PR body.

@neuromechanist
neuromechanist merged commit eb6a76a into feature/issue-2182-epic-pq-evidence Nov 26, 2026
59 of 61 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-2185-text-headers branch November 26, 2026 00:43

This branch was previously deployed

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