IndexError in brainvision.py

IndexError in brainvision.py when calling read_raw_brainvision

  • MNE version: e.g. 1.4.2
  • operating system: Windows 11 Pro

Hey all,
New to MNE here and having trouble importing files from this chronic pain EEG dataset. Here’s my what I’ve got:

import numpy as np
import mne
import pathlib
import re

p = pathlib.Path(r"<path>\cpCGX_BIDS")
name = (
    p / "sub-023" / "eeg" / "sub-023_task-EC_eeg.vhdr"
)
raw = mne.io.read_raw_brainvision(name,eog = [])

This is the output:

Extracting parameters from <path>\cpCGX_BIDS\sub-023\eeg\sub-023_task-EC_eeg.vhdr...
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[2], line 6
      2 p = pathlib.Path(r"C:\Users\acate\CTF_Internship\Databases\CGX\cpCGX_BIDS")
      3 name = (
      4     p / "sub-023" / "eeg" / "sub-023_task-EC_eeg.vhdr"
      5 )
----> 6 raw = mne.io.read_raw_brainvision(name,eog = [])

File ~\anaconda3\envs\mne\Lib\site-packages\mne\io\brainvision\brainvision.py:954, in read_raw_brainvision(vhdr_fname, eog, misc, scale, preload, verbose)
    914 @fill_doc
    915 def read_raw_brainvision(
    916     vhdr_fname,
   (...)
    921     verbose=None,
    922 ):
    923     """Reader for Brain Vision EEG file.
    924 
    925     Parameters
   (...)
    952     mne.io.Raw : Documentation of attributes and methods of RawBrainVision.
    953     """
--> 954     return RawBrainVision(
    955         vhdr_fname=vhdr_fname,
    956         eog=eog,
    957         misc=misc,
    958         scale=scale,
    959         preload=preload,
    960         verbose=verbose,
    961     )

File <decorator-gen-249>:12, in __init__(self, vhdr_fname, eog, misc, scale, preload, verbose)

File ~\anaconda3\envs\mne\Lib\site-packages\mne\io\brainvision\brainvision.py:88, in RawBrainVision.__init__(self, vhdr_fname, eog, misc, scale, preload, verbose)
     77 ext = op.splitext(hdr_fname)[-1]
     78 ahdr_format = True if ext == ".ahdr" else False
     79 (
     80     info,
     81     data_fname,
     82     fmt,
     83     order,
     84     n_samples,
     85     mrk_fname,
     86     montage,
     87     orig_units,
---> 88 ) = _get_hdr_info(hdr_fname, eog, misc, scale)
     90 with open(data_fname, "rb") as f:
     91     if isinstance(fmt, dict):  # ASCII, this will be slow :(

File ~\anaconda3\envs\mne\Lib\site-packages\mne\io\brainvision\brainvision.py:723, in _get_hdr_info(hdr_fname, eog, misc, scale)
    713 # Extract filter units and convert from seconds to Hz if necessary.
    714 # this cannot be done as post-processing as the inverse t-f
    715 # relationship means that the min/max comparisons don't make sense
   (...)
    720 # Current practice of clinical electroencephalography.
    721 # Lippincott Williams & Wilkins.`, page 40-41
    722 header = re.split(r"\s\s+", settings[idx])
--> 723 hp_s = "[s]" in header[hp_col]
    724 lp_s = "[s]" in header[lp_col]
    726 for i, ch in enumerate(ch_names, 1):

IndexError: list index out of range

I can load the sample MNE data fine, so I suspect it’s something to do with the header file. Each of the .vhdr files in the dataset is in the same format (example below).


Brain Vision Data Exchange Header File Version 1.0
; Data created by the BrainVision Recorder for CGX - V. 2.00.0010

[Common Infos]
Codepage=UTF-8
DataFile=sub-001_task-EC_eeg.eeg
MarkerFile=sub-001_task-EC_eeg.vmrk
DataFormat=BINARY
; Data orientation: MULTIPLEXED=ch1,pt1, ch2,pt1 ...
DataOrientation=MULTIPLEXED
NumberOfChannels=29
; Sampling interval in microseconds
SamplingInterval=2000

[Binary Infos]
BinaryFormat=IEEE_FLOAT_32

[Channel Infos]
; Each entry: Ch<Channel number>=<Name>,<Reference channel name>,
; <Resolution in "Unit">,<Unit>, Future extensions..
; Fields are delimited by commas, some fields might be omitted (empty).
; Commas in channel names are coded as "\1".
Ch1=Fp1,,0.0993411,μV
Ch2=Fpz,,0.0993411,μV
Ch3=Fp2,,0.0993411,μV
Ch4=AF7,,0.0993411,μV
Ch5=AF8,,0.0993411,μV
Ch6=F7,,0.0993411,μV
Ch7=F3,,0.0993411,μV
Ch8=Fz,,0.0993411,μV
Ch9=F4,,0.0993411,μV
Ch10=F8,,0.0993411,μV
Ch11=FC5,,0.0993411,μV
Ch12=FC6,,0.0993411,μV
Ch13=T7,,0.0993411,μV
Ch14=C3,,0.0993411,μV
Ch15=Cz,,0.0993411,μV
Ch16=C4,,0.0993411,μV
Ch17=T8,,0.0993411,μV
Ch18=CP5,,0.0993411,μV
Ch19=CP6,,0.0993411,μV
Ch20=P7,,0.0993411,μV
Ch21=P3,,0.0993411,μV
Ch22=Pz,,0.0993411,μV
Ch23=P4,,0.0993411,μV
Ch24=P8,,0.0993411,μV
Ch25=PO7,,0.0993411,μV
Ch26=PO8,,0.0993411,μV
Ch27=O1,,0.0993411,μV
Ch28=Oz,,0.0993411,μV
Ch29=O2,,0.0993411,μV

[Comment]
BrainVision Recorder for CGX - V. 2.00.0010
Amplifier: Quick-32r

A m p l i f i e r  S e t u p
============================

Number of channels:     29
Sampling rate [Hz]:     500
Sampling interval [µs]: 2000

Channels
--------
#     Name                Phys. chn. Resolution / unit 
1     Fp1                 8          0.0993411         μV      
2     Fpz                 3          0.0993411         μV      
3     Fp2                 26         0.0993411         μV      
4     AF7                 2          0.0993411         μV      
5     AF8                 30         0.0993411         μV      
6     F7                  4          0.0993411         μV      
7     F3                  27         0.0993411         μV      
8     Fz                  5          0.0993411         μV      
9     F4                  9          0.0993411         μV      
10    F8                  28         0.0993411         μV      
11    FC5                 29         0.0993411         μV      
12    FC6                 7          0.0993411         μV      
13    T7                  6          0.0993411         μV      
14    C3                  25         0.0993411         μV      
15    Cz                  13         0.0993411         μV      
16    C4                  10         0.0993411         μV      
17    T8                  24         0.0993411         μV      
18    CP5                 15         0.0993411         μV      
19    CP6                 12         0.0993411         μV      
20    P7                  20         0.0993411         μV      
21    P3                  18         0.0993411         μV      
22    Pz                  22         0.0993411         μV      
23    P4                  19         0.0993411         μV      
24    P8                  21         0.0993411         μV      
25    PO7                 23         0.0993411         μV      
26    PO8                 14         0.0993411         μV      
27    O1                  17         0.0993411         μV      
28    Oz                  11         0.0993411         μV      
29    O2                  16         0.0993411         μV      

==== Recording filters ==========
Disabled

Impedance [kOhm] at: 14:51:16
Fp1:  37.6353
Fpz:  36.8125
Fp2:  43.8188
AF7:  60.421
AF8:  42.1031
F7:   218.427
F3:   625.906
Fz:   384.095
F4:   99.326
F8:   175.817
FC5:  231.566
FC6:  298.465
T7:   297.604
C3:   334.587
Cz:   735.187
C4:   585.155
T8:   147.043
CP5:  413.109
CP6:  179.641
P7:   488.222
P3:   496.329
Pz:   818.33
P4:   1048.96
P8:   125.094
PO7:  394.931
PO8:  255.505
O1:   141.014
Oz:   336.76
O2:   273.144

Is there anything suspicious about this dataset that’s throwing off brainvision.py? Or could the problem lie somewhere else? Appreciate any advice!

I am using CGX AIM and had a same problem when importing CGX data in the MNE.
I found that it was due to the content of [Comment] in the header file. After the description of “Channels” was removed (or changed to another string, e.g., channels), the import worked fine.

2 Likes

Indeed, the response by @TakahiroHirao makes sense to me. MNE-Python expects a “Channels” section in the “Comment” that looks like this: https://github.com/mne-tools/mne-python/blob/4518a1a918051542ab7360ff63c529121a499417/mne/io/brainvision/tests/data/test.vhdr#L58-L98

[Comment]

A m p l i f i e r  S e t u p
============================
Number of channels: 32
Sampling Rate [Hz]: 1000
Sampling Interval [µS]: 1000

Channels
--------
#     Name      Phys. Chn.    Resolution / Unit   Low Cutoff [s]   High Cutoff [Hz]   Notch [Hz]    Series Res. [kOhm] Gradient         Offset
1     FP1         1                0.5 µV             DC              250              Off                0
2     FP2         2                0.5 µV             DC              250              Off                0
3     F3          3                0.5 µV             DC              250              Off                0
4     F4          4                0.5 µV             DC              250              Off                0

… and the “Channels” section in the “Comment” of your file looks clearly different :thinking:

Looks like something we should adjust in MNE-Python, as evidently, the rigid structure we expect from the “Comment” section may sometimes be different than initially thought.

EDIT: I file this issue:

2 Likes

@TakahiroHirao @sappelhoff thank you both so much! I’ll be following the issue closely.