Skip to content

eegprep-lean: fill chanlocs from a NEMAR store's electrode positions in to_eeg #625

Closed

Description

@neuromechanist

eegprep_lean.to_eeg builds chanlocs from labels only (chanlocs=[{"labels": label} for label in labels] in packages/eegprep-lean/src/eegprep_lean/eeg.py), and eegprep-lean never reads a store's root attributes: it reads the index, each channel group's zarr.json and the arrays.

A recording read from a NEMAR Zarr store therefore has no channel locations, and anything that needs a scalp map cannot run on it.
ICLabel is the case at hand: ICL_feature_extractor calls topoplot for every component, and topoplot raises no channel has a finite theta and radius when no channel has both.

What a store carries: when the recording's BIDS dataset has an electrodes.tsv, NEMAR's converter writes three attributes into the store's root zarr.json; when the dataset has none, none of the three keys is present.

{
  "attributes": {
    "electrode_positions": {"Fz": [0.0, 0.0987, 0.0521], "Cz": [0.0, 0.0, 0.1105]},
    "electrode_coordinate_system": "CapTrak",
    "electrode_coordinate_units": "m"
  }
}

electrode_positions maps a label to [x, y, z], copied unchanged from electrodes.tsv (no unit or axis change), and holds every valid row of that file, including electrodes the recording does not have.
electrode_coordinate_system and electrode_coordinate_units are EEGCoordinateSystem and EEGCoordinateUnits from the matching coordsystem.json, each an empty string when there is none.

Proposal: load_recording reads these attributes into the Window (one more small request per recording, the store's root zarr.json), and to_eeg fills chanlocs from them, so that a Zarr read gives the same locations as pop_load_frombids gives for the same electrodes.tsv:

  1. Join each channel to the entry with exactly its label. A channel with no entry, or an entry that is not three finite numbers, gets no position.
  2. Convert to millimeters as coords_to_mm does: m times 1000, cm times 10, mm unchanged. Any other text, or none: no positions, reason units_unknown.
  3. Orient to EEGLAB's ALS frame by the BIDS coordinate-systems appendix. ALS is kept for EEGLAB, EEGLAB-HJ, CTF, 4DBti and KitYokogawa. RAS is turned by coords_RAS_to_ALS for CapTrak, NeuromagElektaMEGIN, ElektaNeuromag, ChietiItab and the appendix's standard template identifiers (MNI305, the MNI152* family, Talairach, fsaverage and the rest of that list). Anything else (empty, Other, individual, study, scanner, and the iEEG frames Pixels, ACPC and ScanRAS): no positions, reason coordinate_system_unknown.
  4. Fill X, Y, Z, sph_theta, sph_phi, sph_radius, theta and radius as coords_ALS_to_angular does (sph_theta = atan2(Y, X), sph_phi = atan2(Z, hypot(X, Y)) in degrees, sph_radius the norm, theta = -sph_theta, radius = 0.5 - sph_phi / 180), and set chaninfo["nosedir"] to +X.
  5. Record what was done and why in EEG["etc"]["nemar"]["locations"]: the coordinate system and units as the store states them, the count of located channels and, when there are none, the reason.

A store without the attributes behaves as today: labels only.

Open design points:

  • Reuse. The conversion is the one pop_load_frombids uses (src/eegprep/plugins/EEG_BIDS/coords.py). eegprep-lean must not import eegprep, so the pull request carries the same small NumPy steps inside eegprep-lean and locks them to eegprep's with a test that builds both routes from one set of coordinates. The alternative is for eegprep to depend on eegprep-lean and import them from there, which is a larger change and can follow.
  • Unknown system or unit: no positions plus a recorded reason, never a guess. pop_load_frombids guesses units from the magnitude of the coordinates when the coordsystem declares none, and assumes a system when there is no coordsystem file. The Zarr route should not, because a wrong guess changes the result without any error.

Differences found in pop_load_frombids while reading, not changed by the pull request, for separate decisions:

  • Every coordinate system except those containing EEGLAB is treated as RAS (lines 313 to 321), so CTF, 4DBti and KitYokogawa, which the BIDS appendix gives as ALS, are rotated by 90 degrees.
  • The unit guess for a coordsystem without units (lines 339 to 348) is off by a factor of ten for head-sized coordinates: a centimeter cap (largest coordinate near 10) is read as millimeters, so X, Y and Z are ten times too small, and a millimeter cap (near 100) gets no positions.
  • Neither route recenters template-space positions, whose origin is the anterior commissure rather than the head center, before computing theta and radius.

The pull request follows.

Activity

  1. claude commented on Nov 16, 2026

    @claude
    Contributor

    🤖 Plan:

    Research

    to_eeg builds chanlocs from window.labels only. Neither _load nor read_window opens the store's root zarr.json, so electrode attributes never reach Window, and topoplot in ICLabel has nothing to place.

    Relevant code

    Proposed approach

    1. Add electrodes: ElectrodeInfo | None = None to Window. ElectrodeInfo is a small frozen dataclass: positions: dict[str, tuple[float, float, float]], coordinate_system: str, units: str. In _load, get the store's root zarr.json with the same transport (one extra small GET). Set the field only when electrode_positions is present. read_window stays untouched.
    2. New packages/eegprep-lean/src/eegprep_lean/locations.py (~50 lines). It holds module constants ALS_SYSTEMS / RAS_SYSTEMS (BIDS appendix lists) and MM_PER_UNIT = {"m": 1000, "cm": 10, "mm": 1}. One function chanlocs_from_positions(labels, info) -> (chanlocs, report) applies steps 1-5 from the issue as plain NumPy, with the same formulas as coords.py.
    3. to_eeg calls it when window.electrodes is set, fills chaninfo["nosedir"] = "+X", and writes etc["nemar"]["locations"]. If the field is unset, the output is the same as today.
    4. Tests in packages/eegprep-lean/tests/test_eeg.py: an exact-label join with a missing and a non-finite entry, units_unknown, coordinate_system_unknown, CTF (ALS kept) vs CapTrak (rotated), and a parity test that runs the same coordinates through eegprep.plugins.EEG_BIDS.coords (main test suite, because eegprep-lean must not import eegprep). Also one ICL_feature_extractor/topoplot smoke run on the result. A test_recording.py case covers a root zarr.json with and without the attributes.

    Open questions for a human:

    • Should Window carry the electrode info, or should load_recording return it separately so read_window and Window stay signal-only?
    • The parity test imports both packages. Should it live in tests/ (eegprep) rather than packages/eegprep-lean/tests/?
    • Should the pop_load_frombids divergences (CTF/4DBti/KitYokogawa rotated as RAS, and the unit guess off by 10x) become separate issues now?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions