Skip to content

Commit

Permalink
fix: support commas in BrainVision ch names
Browse files Browse the repository at this point in the history
  • Loading branch information
sappelhoff committed Nov 6, 2020
1 parent f65ec2c commit 0a47b37
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 153 deletions.
3 changes: 2 additions & 1 deletion mne/io/brainvision/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Brainvision module for conversion to FIF."""
"""BrainVision module for conversion to FIF."""

# Author: Teon Brooks <teon.brooks@gmail.com>
# Stefan Appelhoff <stefan.appelhoff@mailbox.org>
#
# License: BSD (3-clause)

Expand Down
4 changes: 3 additions & 1 deletion mne/io/brainvision/brainvision.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
"""Conversion tool from Brain Vision EEG to FIF."""
"""Conversion tool from BrainVision EEG to FIF."""
# Authors: Teon Brooks <teon.brooks@gmail.com>
# Christian Brodbeck <christianbrodbeck@nyu.edu>
# Eric Larson <larson.eric.d@gmail.com>
Expand Down Expand Up @@ -510,6 +510,8 @@ def _get_vhdr_info(vhdr_fname, eog, misc, scale):
props[3] = 'µV'

name, _, resolution, unit = props[:4]
# in BrainVision, commas in channel names are encoded as "\1"
name = name.replace(r'\1', ',')
ch_dict[chan] = name
ch_names[n] = name
if resolution == "":
Expand Down
142 changes: 0 additions & 142 deletions mne/io/brainvision/tests/data/test_comma-test.vhdr

This file was deleted.

23 changes: 14 additions & 9 deletions mne/io/brainvision/tests/test_brainvision.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# Stefan Appelhoff <stefan.appelhoff@mailbox.org>
#
# License: BSD (3-clause)
import os
import os.path as op
import shutil

Expand Down Expand Up @@ -275,8 +274,11 @@ def test_ascii(tmpdir):

def test_ch_names_comma(tmpdir):
"""Test that channel names containing commas are properly read."""
find_line = "Ch4=F4,,0.5,µV"
change_to = r"Ch4=F4\1foo,,0.5,µV" # commas in BV are encoded as \1
# commas in BV are encoded as \1
replace_dict = {
"Ch4=F4,,0.5,µV": r"Ch4=F4\1foo,,0.5,µV",
"4 F4": r"4 F4,foo",
}

# Copy existing vhdr file to tmpdir and manipulate to contain
# a channel with comma
Expand All @@ -287,19 +289,22 @@ def test_ch_names_comma(tmpdir):
comma_vhdr = tmpdir / 'test.vhdr'
with open(comma_vhdr, 'r') as fin:
lines = fin.readlines()

new_lines = []
for line in lines:
if find_line in line:
new_lines.append(change_to + '\n')
continue
new_lines.append(line)
for to_replace, replacement in replace_dict.items():
if to_replace in line:
new = line.replace(to_replace, replacement)
new_lines.append(new)
break
else:
new_lines.append(line)

with open(comma_vhdr, 'w') as fout:
fout.writelines(new_lines)

# Read the line containing a "comma channel name"
raw = read_raw_brainvision(comma_vhdr)
os.remove(comma_vhdr)

assert "F4,foo" in raw.ch_names


Expand Down

0 comments on commit 0a47b37

Please sign in to comment.