Skip to content

Commit

Permalink
amended _write_electrodes_tsv to exclude writing stim channels to e…
Browse files Browse the repository at this point in the history
…lectrodes.tsv (#1023)

* amended mne_bids.dig._write_electrodes_tsv to skip stim channels so they are not written to electrodes.tsv

* added test for properly writing to electrodes.tsv

* Update mne_bids/tests/test_dig.py

Co-authored-by: Richard Höchenberger <richard.hoechenberger@gmail.com>

* Update mne_bids/tests/test_dig.py

Co-authored-by: Richard Höchenberger <richard.hoechenberger@gmail.com>

* Update mne_bids/tests/test_dig.py

Co-authored-by: Richard Höchenberger <richard.hoechenberger@gmail.com>

* removed second test from test_electrodes_io

* updated change log

* updated test_electrodes_io per reviewer comments

* Update mne_bids/tests/test_dig.py

Co-authored-by: Mathieu Scheltienne <mathieu.scheltienne@gmail.com>

* Update doc/whats_new.rst

Co-authored-by: Mathieu Scheltienne <mathieu.scheltienne@gmail.com>

* Update mne_bids/tests/test_dig.py

Co-authored-by: Mathieu Scheltienne <mathieu.scheltienne@gmail.com>

* specifying encoding=utf-8 in  per reviewer suggestion

* Rework test a little

Co-authored-by: Richard Höchenberger <richard.hoechenberger@gmail.com>
Co-authored-by: Mathieu Scheltienne <mathieu.scheltienne@gmail.com>
  • Loading branch information
3 people committed Jul 30, 2022
1 parent 7c08e3b commit fc28eae
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ Detailed list of changes

- :func:`~mne_bids.print_dir_tree` now correctly expands ``~`` to the user's home directory, by `Richard Höchenberger`_ (:gh:`1013`)

- :func:`~mne_bids.write_raw_bids` now correctly excludes stim channels when writing to electrodes.tsv, by `Scott Huberty_` (:gh:`1023`)

:doc:`Find out what was new in previous releases <whats_new_previous_releases>`

.. include:: authors.rst
6 changes: 5 additions & 1 deletion mne_bids/dig.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ def _write_electrodes_tsv(raw, fname, datatype, overwrite=False):
# create list of channel coordinates and names
x, y, z, names = list(), list(), list(), list()
for ch in raw.info['chs']:
if (
if ch['kind'] == FIFF.FIFFV_STIM_CH:
logger.debug(f"Not writing stim chan {ch['ch_name']} "
f"to electrodes.tsv")
continue
elif (
np.isnan(ch['loc'][:3]).any() or
np.allclose(ch['loc'][:3], 0)
):
Expand Down
25 changes: 25 additions & 0 deletions mne_bids/tests/test_dig.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,28 @@ def test_convert_montage():
assert pos['coord_frame'] == 'mri'
assert_almost_equal(pos['ch_pos']['EEG 001'],
[-0.0313669, 0.0540269, 0.0949191])


def test_electrodes_io(tmp_path):
"""Ensure only electrodes end up in *_electrodes.json."""
raw = _load_raw()
raw.pick_types(eeg=True, stim=True) # we don't need meg channels
bids_root = tmp_path / 'bids1'
bids_path = _bids_path.copy().update(root=bids_root, datatype='eeg')
write_raw_bids(raw=raw, bids_path=bids_path)

electrodes_path = (
bids_path.copy()
.update(
task=None,
run=None,
space='CapTrak',
suffix='electrodes',
extension='.tsv'
)
)
with open(electrodes_path, encoding='utf-8') as sidecar:
n_entries = len([line for line in sidecar
if 'name' not in line]) # don't need the header
# only eeg chs w/ electrode pos should be written to electrodes.tsv
assert n_entries == len(raw.get_channel_types('eeg'))

0 comments on commit fc28eae

Please sign in to comment.