Skip to content

[BIDS] Read EEGCoordinateSystem by BIDS appendix orientation - #693

Merged
neuromechanist merged 5 commits into
developfrom
agent/20261125-fix-692
Nov 25, 2026
Merged

neuromechanist merged 5 commits into
developfrom
agent/20261125-fix-692

Conversation

@claude

@claude claude Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

pop_load_frombids read every EEGCoordinateSystem other than EEGLAB as RAS, so CTF, 4DBti and KitYokogawa positions, which BIDS gives as ALS, were turned by 90 degrees. Frames are now classified with the appendix table eegprep-lean already uses; a named frame with no orientation, such as Other, leaves locations unset and adds a report warning. Adds regression tests and the three frames to the lean cross-route test.

Fixes #692

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Code review skipped — your organization has no extra usage available to pay for this review.

If your organization's extra usage balance is empty, an organization admin can add extra usage credits at claude.ai/admin-settings/usage. If its monthly spend limit was reached, an admin can raise it on the same page. If neither applies, contact Anthropic support.

Once extra usage is available, someone with write access to this repository can comment @claude review on this pull request to trigger a review.

CTF, 4DBti and KitYokogawa are ALS in the BIDS appendix and were rotated
90 degrees as RAS. Named systems with no orientation now leave chanlocs
unset with a report warning.

Fixes #692
@claude
claude Bot force-pushed the agent/20261125-fix-692 branch from e2f8a38 to ee3d384 Compare November 25, 2026 17:47
@claude

claude Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

PR #693 was updated after the trigger (e2f8a38 -> ee3d384). Push again or mention Claude to run on the latest version.

@neuromechanist neuromechanist left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick fix. #693 reads CTF, 4DBti and KitYokogawa as ALS, which is the heart of #692. Four cases still differ from what #692 asks or from what develop does today.

Each repro below uses the #692 recording (one channel, Fp1, units mm), changes only _coordsystem.json and the Fp1 row of _electrodes.tsv, and loads it with pop_load_frombids(path, bidsevent=False, infer_locations=False, return_report=True). The outputs are from this PR's head, ee3d3848.

1. A missing EEGCoordinateSystem still assumes RAS

#692 asks that a frame with no stated orientation leave the locations unset instead of assuming RAS.

  • Input: {"EEGCoordinateUnits": "mm"}, Fp1 at 77, 32, 19
  • Now: X, Y, Z = 32, -77, 19, theta 67.43, no warning
  • Expected: locations unset, with a warning in report["Warnings"]

Suggested fix: drop the None case so that a missing name takes the unknown-frame path.

elif coord_system_name in BIDS_RAS_SYSTEMS:
    coord_system = 'RAS'

2. The case-insensitive "contains EEGLAB" rule was dropped

On develop, any EEGCoordinateSystem with EEGLAB in it, in any case, is read as ALS. This PR matches names exactly, so an EEGLAB-frame dataset with a miscased or padded name loses its positions.

  • Input: {"EEGCoordinateSystem": "eeglab"} (or "EEGLAB " with a trailing space), Fp1 at 77, 32, 19
  • Now: locations unset, warning Coordinate system 'eeglab' ... has no orientation in the BIDS coordinate-systems appendix
  • Expected, as on develop: X, Y, Z = 77, 32, 19, theta -22.57

Suggested fix: keep the develop rule ahead of the table lookup.

if isinstance(coord_system_name, str) and 'EEGLAB' in coord_system_name.upper():
    coord_system = 'ALS'

3. Case-sensitive matching loses MNI152NLin6Asym and captrak

The schema value for this template changed from MNI152NLin6ASym (BIDS 1.9.0 to 1.10.1) to MNI152NLin6Asym (1.11.0 onward, src/schema/objects/enums.yaml). The appendix table still prints MNI152NLin6ASym, and that is the only spelling in BIDS_RAS_SYSTEMS. A dataset that passes the current validator therefore loses its positions, and so does one that writes captrak. develop reads both as RAS.

  • Input: {"EEGCoordinateSystem": "MNI152NLin6Asym"} (or "captrak"), Fp1 at -32, 77, 19 (RAS)
  • Now: locations unset, with a warning
  • Expected, as on develop: X, Y, Z = 77, 32, 19, theta -22.57

Suggested fix: look names up without regard to case. No two frames in the table differ only in case, so this also accepts both spellings of the template.

_ORIENTATION_BY_SYSTEM = {
    **{system.lower(): 'RAS' for system in BIDS_RAS_SYSTEMS},
    **{system.lower(): 'ALS' for system in BIDS_ALS_SYSTEMS},
}

coord_system = _ORIENTATION_BY_SYSTEM.get(coord_system_name.strip().lower())

4. A frame named literally ALS or RAS is applied as that orientation

The else branch stores the frame's name in coord_system, and the later check asks only whether that value is 'ALS' or 'RAS'. Neither is a BIDS keyword, so both should be left unset, as Other is.

  • Input: {"EEGCoordinateSystem": "ALS"}, Fp1 at 77, 32, 19
  • Now: X, Y, Z = 77, 32, 19, no warning; "RAS" gives 32, -77, 19, also with no warning
  • Expected: locations unset, with a warning, as for Other

Suggested fix: keep the orientation separate from the name, with None for a frame the appendix gives no orientation.

else:
    coord_system = None  # no orientation in the BIDS appendix

# later, before the positions are applied
elif coord_system is None:
    warning(...)

Minor: the new warning names fo.filename, which at that point is the _channels.tsv file (for example Coordinate system 'Other' of sub-01_task-rest_channels.tsv ...), not the _coordsystem.json that states the frame.

One helper that covers all four cases
def bids_coordsystem_orientation(coordsystem: Dict[str, Any]) -> Optional[str]:
    """Return 'ALS' or 'RAS' for the EEG frame a BIDS coordsystem.json names, or None."""
    if coordsystem.get('EEGCoordinateSystemDescription') == 'EEGLAB':
        return 'ALS'
    system = coordsystem.get('EEGCoordinateSystem')
    if not isinstance(system, str):
        return None
    if 'EEGLAB' in system.upper():
        return 'ALS'
    return _ORIENTATION_BY_SYSTEM.get(system.strip().lower())

pop_load_frombids then sets coord_system = bids_coordsystem_orientation(content), adds a warning that names the coordsystem file when the result is None, and applies the positions only when it is not.

A branch with these changes exists and can be shared if wanted. It has a regression test for each case above, alongside tests for the three ALS frames, CapTrak given in RAS, Other, and unchanged EEGLAB and EEGLAB-HJ.

Open questions

  • Label inference fallback: with the default infer_locations=None, a frame left unset then falls back to positions inferred from the channel labels (in the #692 recording, Fp1 lands at theta -17.93). Is that intended, or should an unknown frame leave the positions empty?
  • Missing coordsystem file: the importer still assumes ALS for a .set file and RAS otherwise. Should that case also stop assuming a frame?
  • iEEGCoordinateSystem: iEEG data states its frame there, and the importer reads only EEGCoordinateSystem. With fix 1, iEEG positions are left unset; without it, they are read as RAS.
  • eegprep-lean table spelling: packages/eegprep-lean/src/eegprep_lean/locations.py lists only MNI152NLin6ASym, so the Zarr route also refuses the current schema value.
  • Structured report key: the reason is recorded only in report["Warnings"]. Would a key giving the stated system and the orientation read from it help callers that need to tell missing positions from positions withheld?

Agentic AI tools were used. Workflows follow the Research Skills (doi:10.5281/zenodo.20696515). The author assumes full responsibility for this content and associated changes.

Fixes four cases #693 left: a coordsystem that names no system was
still read as RAS; names with EEGLAB in them were no longer ALS unless
spelled exactly; MNI152NLin6Asym, the schema spelling from BIDS 1.11.0,
and miscased names such as captrak lost their positions; and a frame
named ALS or RAS was applied as that orientation. The unknown-frame
warning now names the coordsystem file, not the channels file.

Tested: uv run pytest tests/test_bids_load_frombids_helpers.py
The BIDS schema spells this template MNI152NLin6Asym from 1.11.0, while
the appendix's table and lean's frame table had only MNI152NLin6ASym,
so the Zarr route refused a current dataset's frame. The lean docs now
describe the import as it reads frames after the review fixes, and the
changelog no longer says the import turns CTF, 4DBti or KitYokogawa.

Tested: eegprep-lean tests -m "not network" (PYTHONPATH=src, from
packages/eegprep-lean, as CI runs them); the four new MNI152NLin6Asym
cases fail against the old table.
The changelog entry and BIDS guide no longer say a coordsystem with no
EEGCoordinateSystem is read as RAS, and now state the case-insensitive
match, the EEGLAB rules and the label-inference fallback. The older
eegprep_lean.to_eeg entry drops its CTF clause, and the
pop_load_frombids help gains the frame rule.

Tested: docutils parse of the edited RST; pytest
tests/test_guifunc_pophelp_chansel.py.
Review fixes for #693: frame orientation cases
@neuromechanist
neuromechanist merged commit df89e80 into develop Nov 25, 2026
18 of 19 checks passed
neuromechanist added a commit to nemarOrg/nemar-process that referenced this pull request Nov 27, 2026
) (#101)

* Re-pin eegprep to 871bc7f8 for eeg_mir

Adds the iclabel extra (onnxruntime), hashes the ONNX network the
classification names, follows clean_channels' seed argument and the
AMICA thresholds fix, and records the re-pin effects in upstream.md.
Tested: tests/recipes (runtime env).

* Route half-located and NaN positions past RANSAC

clean_channels at the new pin needs more than half of the channels
located and refuses a NaN coordinate; the floor's guard and the copy it
hands RANSAC follow, so neither fails a recording. Tested: test_qc,
test_locations, test_golden_decisions; both mutations fail them.

* Lift the frame_rotated withholding (#94)

eegprep at the new pin reads CTF, 4DBti and KitYokogawa as ALS
(sccn/eegprep#693), so nemar-ica writes and draws their positions as
stated; a frame with no BIDS orientation now gives no positions. The
generic Host.withheld mechanism stays for cap_exceeded. Tested:
test_nemar_ica_views, test_quality_plugins, test_nemar_ica_serving_input.

* Let a plugin say why a metric is null

PluginResult.metrics_skipped maps a null metric to a word the module
declares in METRIC_SKIP_WORDS; the plugin ran, its entry lists the
metric and word, and a fixed line is logged. Undeclared words or a word
beside a value skip the plugin. Tested: test_plugin_runner.

* nemar-quality 0.2.0: MIR via eegprep's eeg_mir

Adds nemar-quality:mutualInformationReduction (nat, fixed nbins 256
checked against MIR_NBINS; a refusal is mir_refused, a bin change
mir_bins_changed), hands it icaweights, icasphere and icachansind,
adds mir_nats to nemar-ica's table with its distribution, and pins the
new tree digest. Tested: recipes suite; three miscomputations fail.

* Regenerate the ica-views product

Made with NEMAR_ICA_VIEWS_PRODUCT=tests/fixtures/ica-views on arm64
macOS: eegprep 871bc7f8, nemar-quality 0.2.0 (MIR, mir_nats), ICLabel's
ONNX network, positions without withheld. Tested: the views e2e test
holds the copy to a fresh run.

* Hold the image to eegprep's iclabel extra

tests/images pins the extras: add iclabel (onnxruntime, ICLabel's
runtime since sccn/eegprep#386) and check onnxruntime is locked.
Tested: tests/images (479 passed).

* Pin onnxruntime and stamp it in every record

ICLabel's probabilities depend on onnxruntime since sccn/eegprep#386:
pin 1.32.0 in the runtime group (EXACT_PINS, the smoke imports it),
add it to OPTIONAL_ENGINES and nemar-ica's versions, and hold
steps.ONNXRUNTIME_VERSION to the lock. Tested: tests/images,
test_nemar_ica.

* Record ICLabel's onnxruntime threads

eegprep builds ICLabel's session with no thread count, so it runs on
onnxruntime's default pools outside the run's pin; the iclabel block
now says so (onnxruntime_threads, read from the session) and the
determinism docs no longer claim one thread. Tested:
test_nemar_ica_views.

* Check MIR's shape contract before eeg_mir

A decomposition that breaks eeg_mir's shape contract is the host's
fault (DecompositionMismatch, plugin_failed), never mir_refused; a
non-finite MIR is mir_refused. The tree digest moves (0.2.0 is not
yet merged). Tested: test_quality_plugins.

* Clear every position of a NaN-located channel

A channel the guard counts as unlocated but that gives a coordinate
now loses every position field (as the loaders give none), theta and
radius included, and the clearing persists, so _chanlocs.tsv writes
n/a for it. Tested: test_qc, test_locations, test_nemar_ica_views.

* Fail closed on frames eegprep cannot orient

A dataset directory whose coordsystem names a frame eegprep gives no
orientation ends at iclabel_locations with iclabel_frame_unoriented
(owner's ruling); position_facts keeps the stated frame. Also tests
that ICLabel needs two distinct located channels, the ONNX network
and digest, and a NaN channel's n/a layout row. Tested:
test_nemar_ica_views (mutation-checked).

* Docs: re-pin effects, MIR row, location rule

upstream.md gains #674 (closing #671), #649 and the ICLabel thread
issue to be filed, #669 merged without moving eegprep-lean's pin; the
MIR table row has no pipes; the pamica key list and the shape-contract
refusal are documented. Docs only.

* Regenerate the ica-views product again

The record now stamps onnxruntime (engine and ica versions) and
ICLabel's onnxruntime_threads, and nemar-quality's tree digest moved.
Made per the README on arm64 macOS; the copy check passes.

* Cite sccn/eegprep#763 for ICLabel's threads

The thread-count issue is filed; replace the to-be-filed references in
upstream.md, recipes.md and iclabel_threads' docstring. Docs and a
docstring only; ruff and ty pass.
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.

pop_load_frombids turns CTF, 4DBti and KitYokogawa electrode positions by 90 degrees

1 participant