Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

amended _write_electrodes_tsv to exclude writing stim channels to electrodes.tsv #1023

Merged
merged 14 commits into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Detailed list of changes
- :func:`~mne_bids.copyfiles.copyfile_brainvision` can now deal with ``.dat`` file extension, by `Dominik Welke`_ (:gh:`1008`)

- :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`)
scott-huberty marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down
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
22 changes: 22 additions & 0 deletions mne_bids/tests/test_dig.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,25 @@ 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')
(bids_root / 'sub-01' / 'ses-01' / 'eeg').mkdir(exist_ok=True)
Copy link
Member

Choose a reason for hiding this comment

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

Why do you need to create this directory manually, anyway..?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure.. I was having a hard time understanding this, but was trying to follow the examples from the other tests in test.dig.py

Copy link
Member

Choose a reason for hiding this comment

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

write_raw_bids should create the directory tree, could you try without?

write_raw_bids(raw, bids_path)

with open(bids_path.directory /
'sub-01_ses-01_acq-01_space-CapTrak_electrodes.tsv') as sidecar:
Copy link
Member

Choose a reason for hiding this comment

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

Should specify encoding='utf-8'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, thanks for the tip!

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
.copy()
.pick_types(eeg=True)
.ch_names
)
Copy link
Member

Choose a reason for hiding this comment

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

seems my prior comment got lost: you should not need to do raw.copy() here. use raw.get_channel_types() and count the 'eeg's

Copy link
Member

Choose a reason for hiding this comment

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

Nice trick, @drammock!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

awesome, thanks @drammock!