Repository navigation
Description
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:
- 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.
- Convert to millimeters as
coords_to_mmdoes:mtimes 1000,cmtimes 10,mmunchanged. Any other text, or none: no positions, reasonunits_unknown. - Orient to EEGLAB's ALS frame by the BIDS coordinate-systems appendix. ALS is kept for
EEGLAB,EEGLAB-HJ,CTF,4DBtiandKitYokogawa. RAS is turned bycoords_RAS_to_ALSforCapTrak,NeuromagElektaMEGIN,ElektaNeuromag,ChietiItaband the appendix's standard template identifiers (MNI305, theMNI152*family,Talairach,fsaverageand the rest of that list). Anything else (empty,Other,individual,study,scanner, and the iEEG framesPixels,ACPCandScanRAS): no positions, reasoncoordinate_system_unknown. - Fill
X,Y,Z,sph_theta,sph_phi,sph_radius,thetaandradiusascoords_ALS_to_angulardoes (sph_theta = atan2(Y, X),sph_phi = atan2(Z, hypot(X, Y))in degrees,sph_radiusthe norm,theta = -sph_theta,radius = 0.5 - sph_phi / 180), and setchaninfo["nosedir"]to+X. - 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_frombidsuses (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_frombidsguesses 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
EEGLABis treated as RAS (lines 313 to 321), soCTF,4DBtiandKitYokogawa, 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,YandZare 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
thetaandradius.
The pull request follows.
Activity
claude commentedon Nov 16, 2026
🤖 Plan:
Window,load_recording,to_eeg, a new eegprep-lean coords module, and tests (including a cross-check against eegprep). That is a multi-module feature well over 100 lines, so I'm posting analysis only and not opening a PR. The issue says its author's PR follows.Research
to_eegbuildschanlocsfromwindow.labelsonly. Neither_loadnorread_windowopens the store's rootzarr.json, so electrode attributes never reachWindow, andtopoplotin ICLabel has nothing to place.Relevant code
packages/eegprep-lean/src/eegprep_lean/eeg.py#L107-L146-to_eeg, labels-only chanlocs at L136packages/eegprep-lean/src/eegprep_lean/recording.py#L114-L160-_load; root attrs would be fetched herepackages/eegprep-lean/src/eegprep_lean/window.py#L53-L89-Windowdataclass; needs a positions fieldsrc/eegprep/plugins/EEG_BIDS/coords.py#L17-L106-coords_to_mm,coords_RAS_to_ALS,coords_ALS_to_angularsrc/eegprep/functions/popfunc/pop_load_frombids.py#L311-L350- system→RAS default and unit guess (divergences listed in the issue)Proposed approach
electrodes: ElectrodeInfo | None = NonetoWindow.ElectrodeInfois a small frozen dataclass:positions: dict[str, tuple[float, float, float]],coordinate_system: str,units: str. In_load, get the store's rootzarr.jsonwith the same transport (one extra small GET). Set the field only whenelectrode_positionsis present.read_windowstays untouched.packages/eegprep-lean/src/eegprep_lean/locations.py(~50 lines). It holds module constantsALS_SYSTEMS/RAS_SYSTEMS(BIDS appendix lists) andMM_PER_UNIT = {"m": 1000, "cm": 10, "mm": 1}. One functionchanlocs_from_positions(labels, info) -> (chanlocs, report)applies steps 1-5 from the issue as plain NumPy, with the same formulas ascoords.py.to_eegcalls it whenwindow.electrodesis set, fillschaninfo["nosedir"] = "+X", and writesetc["nemar"]["locations"]. If the field is unset, the output is the same as today.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 througheegprep.plugins.EEG_BIDS.coords(main test suite, because eegprep-lean must not import eegprep). Also oneICL_feature_extractor/topoplotsmoke run on the result. Atest_recording.pycase covers a rootzarr.jsonwith and without the attributes.Open questions for a human:
Windowcarry the electrode info, or shouldload_recordingreturn it separately soread_windowandWindowstay signal-only?tests/(eegprep) rather thanpackages/eegprep-lean/tests/?pop_load_frombidsdivergences (CTF/4DBti/KitYokogawa rotated as RAS, and the unit guess off by 10x) become separate issues now?