Skip to content

Commit 33cbe67

Browse files
alexrockhillhoechenbergeradam2392
authored
[ENH, MRG] Simplify reading dig (#979)
* Simplify reading dig [skip ci] * update whatsnew * Update doc/whats_new.rst Co-authored-by: Adam Li <[email protected]> Co-authored-by: Richard Höchenberger <[email protected]> Co-authored-by: Adam Li <[email protected]>
1 parent 7343e20 commit 33cbe67

File tree

5 files changed

+47
-89
lines changed

5 files changed

+47
-89
lines changed

doc/whats_new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ Enhancements
5454

5555
- :func:`mne_bids.update_anat_landmarks` can now directly work with fiducials saved from the MNE-Python coregistration GUI or :func:`mne.io.write_fiducials`, by Richard Höchenberger`_ (:gh:`977`)
5656

57+
- All non-MNE-Python BIDS coordinate frames are now set to ``'unknown'`` on reading, by `Alex Rockhill`_ (:gh:`979`)
58+
5759
API and behavior changes
5860
^^^^^^^^^^^^^^^^^^^^^^^^
5961

mne_bids/config.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -205,18 +205,27 @@
205205
'UNCInfant2V23',
206206
]
207207

208-
coordsys_meg = ['CTF', 'ElektaNeuromag', '4DBti', 'KitYokogawa', 'ChietiItab']
209-
coordsys_eeg = ['CapTrak']
210-
coordsys_ieeg = ['Pixels', 'ACPC']
208+
# accepted BIDS formats, which may be subject to change
209+
# depending on the specification
210+
BIDS_IEEG_COORDINATE_FRAMES = ['ACPC', 'Pixels']
211+
BIDS_MEG_COORDINATE_FRAMES = ['CTF', 'ElektaNeuromag',
212+
'4DBti', 'KitYokogawa',
213+
'ChietiItab']
214+
BIDS_EEG_COORDINATE_FRAMES = ['CapTrak']
215+
216+
# accepted coordinate SI units
217+
BIDS_COORDINATE_UNITS = ['m', 'cm', 'mm']
211218
coordsys_wildcard = ['Other']
212-
coordsys_shared = (coordsys_standard_template +
213-
coordsys_standard_template_deprecated +
214-
coordsys_wildcard)
219+
BIDS_SHARED_COORDINATE_FRAMES = (coordsys_standard_template +
220+
coordsys_standard_template_deprecated +
221+
coordsys_wildcard)
215222

216223
ALLOWED_SPACES = dict()
217-
ALLOWED_SPACES['meg'] = coordsys_shared + coordsys_meg + coordsys_eeg
218-
ALLOWED_SPACES['eeg'] = coordsys_shared + coordsys_meg + coordsys_eeg
219-
ALLOWED_SPACES['ieeg'] = coordsys_shared + coordsys_ieeg
224+
ALLOWED_SPACES['meg'] = ALLOWED_SPACES['eeg'] = \
225+
BIDS_SHARED_COORDINATE_FRAMES + BIDS_MEG_COORDINATE_FRAMES + \
226+
BIDS_EEG_COORDINATE_FRAMES
227+
ALLOWED_SPACES['ieeg'] = \
228+
BIDS_SHARED_COORDINATE_FRAMES + BIDS_IEEG_COORDINATE_FRAMES
220229
ALLOWED_SPACES['anat'] = None
221230
ALLOWED_SPACES['beh'] = None
222231

@@ -235,17 +244,6 @@
235244
'extension': 'label'
236245
}
237246

238-
# accepted BIDS formats, which may be subject to change
239-
# depending on the specification
240-
BIDS_IEEG_COORDINATE_FRAMES = ['ACPC', 'Pixels', 'Other']
241-
BIDS_MEG_COORDINATE_FRAMES = ['CTF', 'ElektaNeuromag',
242-
'4DBti', 'KitYokogawa',
243-
'ChietiItab', 'Other']
244-
BIDS_EEG_COORDINATE_FRAMES = ['CapTrak']
245-
246-
# accepted coordinate SI units
247-
BIDS_COORDINATE_UNITS = ['m', 'cm', 'mm']
248-
249247
# mapping from supported BIDs coordinate frames -> MNE
250248
BIDS_TO_MNE_FRAMES = {
251249
'CTF': 'ctf_head',
@@ -256,8 +254,7 @@
256254
'CapTrak': 'head',
257255
'ACPC': 'mri', # assumes T1 is ACPC-aligned, if not the coordinates are lost # noqa
258256
'fsaverage': 'mni_tal', # XXX: note fsaverage and MNI305 are the same # noqa
259-
'MNI305': 'mni_tal',
260-
'Other': 'unknown'
257+
'MNI305': 'mni_tal'
261258
}
262259

263260
# mapping from supported MNE coordinate frames -> BIDS

mne_bids/dig.py

Lines changed: 12 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
from mne.utils import logger, warn
1616
from mne.io.pick import _picks_to_idx
1717

18-
from mne_bids.config import (BIDS_IEEG_COORDINATE_FRAMES,
19-
BIDS_MEG_COORDINATE_FRAMES,
20-
BIDS_EEG_COORDINATE_FRAMES,
18+
from mne_bids.config import (ALLOWED_SPACES,
2119
BIDS_COORDINATE_UNITS,
2220
MNE_TO_BIDS_FRAMES, BIDS_TO_MNE_FRAMES,
2321
MNE_FRAME_TO_STR, BIDS_COORD_FRAME_DESCRIPTIONS)
@@ -535,57 +533,17 @@ def _read_dig_bids(electrodes_fpath, coordsystem_fpath,
535533
bids_coord_frame, bids_coord_unit = _handle_coordsystem_reading(
536534
coordsystem_fpath, datatype)
537535

538-
if datatype == 'meg':
539-
if bids_coord_frame not in BIDS_MEG_COORDINATE_FRAMES:
540-
warn(f'MEG coordinate frame "{bids_coord_frame}" is not '
541-
f'supported. The supported coordinate frames are: '
542-
f'{BIDS_MEG_COORDINATE_FRAMES}')
543-
coord_frame = None
544-
elif bids_coord_frame == 'Other':
545-
warn("Coordinate frame of MEG data can't be determined "
546-
"when 'other'. The currently accepted keywords are: "
547-
"{}".format(BIDS_MEG_COORDINATE_FRAMES))
548-
coord_frame = None
549-
else:
550-
coord_frame = BIDS_TO_MNE_FRAMES.get(bids_coord_frame, None)
551-
elif datatype == 'ieeg':
552-
# iEEG datatype for BIDS only supports
553-
# acpc, pixels and then standard templates
554-
# iEEG datatype for mne-python only supports
555-
# mni_tal == fsaverage == MNI305
556-
if bids_coord_frame == 'Pixels':
557-
warn("Coordinate frame of iEEG data in pixels is not "
558-
"recognized by mne-python, the coordinate frame "
559-
"of the montage will be set to 'unknown'")
560-
coord_frame = 'unknown'
561-
elif bids_coord_frame == 'ACPC':
562-
coord_frame = BIDS_TO_MNE_FRAMES.get(bids_coord_frame, None)
563-
elif bids_coord_frame == 'Other':
564-
# default coordinate frames to available ones in mne-python
565-
# noqa: see https://bids-specification.readthedocs.io/en/stable/99-appendices/08-coordinate-systems.html
566-
warn(f"Defaulting coordinate frame to unknown "
567-
f"from coordinate system input {bids_coord_frame}")
568-
coord_frame = BIDS_TO_MNE_FRAMES.get(bids_coord_frame, None)
569-
else:
570-
coord_frame = BIDS_TO_MNE_FRAMES.get(bids_coord_frame, None)
571-
572-
# XXX: if the coordinate frame is not recognized, then
573-
# coordinates are stored in a system that we cannot
574-
# recognize yet.
575-
if coord_frame is None:
576-
warn(f"iEEG Coordinate frame {bids_coord_frame} is not a "
577-
f"readable BIDS keyword by mne-bids yet. The allowed "
578-
f"keywords are: {BIDS_IEEG_COORDINATE_FRAMES}")
579-
coord_frame = 'unknown'
580-
elif datatype == 'eeg':
581-
# only accept captrak
582-
if bids_coord_frame not in BIDS_EEG_COORDINATE_FRAMES:
583-
warn("EEG Coordinate frame is not accepted "
584-
"BIDS keyword. The allowed keywords are: "
585-
"{}".format(BIDS_IEEG_COORDINATE_FRAMES))
586-
coord_frame = None
587-
else:
588-
coord_frame = BIDS_TO_MNE_FRAMES.get(bids_coord_frame, None)
536+
if bids_coord_frame not in ALLOWED_SPACES[datatype]:
537+
warn(f'"{bids_coord_frame}" is not a BIDS-acceptable coordinate frame '
538+
f'for {datatype.upper()}. The supported coordinate frames are: '
539+
'{}'.format(ALLOWED_SPACES[datatype]))
540+
coord_frame = None
541+
elif bids_coord_frame in BIDS_TO_MNE_FRAMES:
542+
coord_frame = BIDS_TO_MNE_FRAMES.get(bids_coord_frame, None)
543+
else:
544+
warn(f"{bids_coord_frame} is not an MNE-Python coordinate frame "
545+
f"for {datatype.upper()} data and so will be set to 'unknown'")
546+
coord_frame = 'unknown'
589547

590548
# check coordinate units
591549
if bids_coord_unit not in BIDS_COORDINATE_UNITS:

mne_bids/tests/test_read.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
from mne.datasets import testing, somato
2121

2222
from mne_bids import BIDSPath
23-
from mne_bids.config import MNE_STR_TO_FRAME
23+
from mne_bids.config import (MNE_STR_TO_FRAME, BIDS_SHARED_COORDINATE_FRAMES,
24+
BIDS_TO_MNE_FRAMES)
2425
from mne_bids.read import (read_raw_bids,
2526
_read_raw, get_head_mri_trans,
2627
_handle_events_reading)
@@ -733,8 +734,8 @@ def test_handle_eeg_coords_reading(tmp_path):
733734
suffix='coordsystem',
734735
extension='.json')
735736
_update_sidecar(coordsystem_fname, 'EEGCoordinateSystem', 'besa')
736-
with pytest.warns(RuntimeWarning, match='EEG Coordinate frame is not '
737-
'accepted BIDS keyword'):
737+
with pytest.warns(RuntimeWarning, match='is not a BIDS-acceptable '
738+
'coordinate frame for EEG'):
738739
raw_test = read_raw_bids(bids_path)
739740
assert raw_test.info['dig'] is None
740741

@@ -842,25 +843,25 @@ def test_handle_ieeg_coords_reading(bids_path, tmp_path):
842843
_update_sidecar(coordsystem_fname, 'iEEGCoordinateSystem', coord_frame)
843844
# read in raw file w/ updated coordinate frame
844845
# and make sure all digpoints are MRI coordinate frame
845-
with pytest.warns(RuntimeWarning, match="Defaulting coordinate "
846-
"frame to unknown"):
846+
with pytest.warns(RuntimeWarning, match="not an MNE-Python "
847+
"coordinate frame"):
847848
raw_test = read_raw_bids(bids_path=bids_fname, verbose=False)
848849
assert raw_test.info['dig'] is not None
849850

850851
# check that standard template identifiers that are unsupported in
851852
# mne-python coordinate frames, still get read in, but produce a warning
852-
coordinate_frames = ['individual', 'fsnative', 'scanner',
853-
'ICBM452AirSpace', 'NIHPD']
854-
for coord_frame in coordinate_frames:
853+
for coord_frame in BIDS_SHARED_COORDINATE_FRAMES:
855854
# update coordinate units
856855
_update_sidecar(coordsystem_fname, 'iEEGCoordinateSystem', coord_frame)
857856
# read in raw file w/ updated coordinate frame
858857
# and make sure all digpoints are MRI coordinate frame
859-
with pytest.warns(
860-
RuntimeWarning, match=f"iEEG Coordinate frame {coord_frame} "
861-
f"is not a readable BIDS keyword "):
858+
if coord_frame in BIDS_TO_MNE_FRAMES:
862859
raw_test = read_raw_bids(bids_path=bids_fname, verbose=False)
863-
assert raw_test.info['dig'] is not None
860+
else:
861+
with pytest.warns(RuntimeWarning, match="not an MNE-Python "
862+
"coordinate frame"):
863+
raw_test = read_raw_bids(bids_path=bids_fname, verbose=False)
864+
assert raw_test.info['dig'] is not None
864865

865866
# ACPC should be read in as RAS for iEEG
866867
_update_sidecar(coordsystem_fname, 'iEEGCoordinateSystem', 'ACPC')

mne_bids/tests/test_write.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3642,7 +3642,7 @@ def test_write_dig(tmpdir):
36423642
coordsystem_path = bids_path.copy().update(
36433643
task=None, run=None, suffix='coordsystem', extension='.json')
36443644
with pytest.warns(RuntimeWarning,
3645-
match='recognized by mne-python'):
3645+
match='not an MNE-Python coordinate frame'):
36463646
_read_dig_bids(electrodes_path, coordsystem_path,
36473647
bids_path.datatype, raw)
36483648
montage2 = raw.get_montage()

0 commit comments

Comments
 (0)