Error reading raw bdf data

Hello,

I am new to using MNE-Python and will be working with bdf files. Using mne.io.read_raw_bdf(“file”) does not work and gives me the following error which I wish to fix:

runfile('/Users/mikewijnhoven/data_exploration', wdir='/Users/mikewijnhoven')
Extracting EDF parameters from /Users/mikewijnhoven/Documents/ThesisData/sub-002/ses-01/eeg/sub-002_ses-01_task-meditation_eeg.bdf...
BDF file detected

Traceback (most recent call last):

  File "/Users/mikewijnhoven/data_exploration", line 15, in <module>
    raw = mne.io.read_raw_bdf("/Users/mikewijnhoven/Documents/ThesisData/sub-002/ses-01/eeg/sub-002_ses-01_task-meditation_eeg.bdf")

  File "/Users/mikewijnhoven/opt/anaconda3/lib/python3.8/site-packages/mne/io/edf/edf.py", line 1449, in read_raw_bdf
    return RawEDF(input_fname=input_fname, eog=eog, misc=misc,

  File "<decorator-gen-580>", line 24, in __init__

  File "/Users/mikewijnhoven/opt/anaconda3/lib/python3.8/site-packages/mne/io/edf/edf.py", line 140, in __init__
    info, edf_info, orig_units = _get_info(input_fname, stim_channel, eog,

  File "/Users/mikewijnhoven/opt/anaconda3/lib/python3.8/site-packages/mne/io/edf/edf.py", line 384, in _get_info
    edf_info, orig_units = _read_header(fname, exclude, infer_types)

  File "/Users/mikewijnhoven/opt/anaconda3/lib/python3.8/site-packages/mne/io/edf/edf.py", line 371, in _read_header
    return _read_edf_header(fname, exclude, infer_types)

  File "/Users/mikewijnhoven/opt/anaconda3/lib/python3.8/site-packages/mne/io/edf/edf.py", line 632, in _read_edf_header
    day, month, year = [int(x) for x in meas_date.split('.')]

  File "/Users/mikewijnhoven/opt/anaconda3/lib/python3.8/site-packages/mne/io/edf/edf.py", line 632, in <listcomp>
    day, month, year = [int(x) for x in meas_date.split('.')]

ValueError: invalid literal for int() with base 10: ';\n  posi'

How would I fix this error? I am using macOS Monterey 12.1, Python 3.8.8., and the latest stable version of MNE.

It seems like your BDF file contains an invalid date. How did you record the file? Can you open it with other programs (e.g. EDFbrowser)?

I have not recorded it myself but I comes from this dataset (https://openneuro.org/datasets/ds001787/versions/1.0.3). It was recorded using Biosemi.
I will check other programs.

Can you try reading the data via MNE-BIDS? (as you should always do with BIDS data!)

Hello! Did you finally fix your problem? I have the same issue here with my data. I try to read a bdf file, but it does not work at all.

There is the code:

raw = mne.io.read_raw_bdf(raw_fname, preload=True)
print(raw)

There is the output:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-25-39e7a886b21b> in <module>
----> 1 raw = mne.io.read_raw_bdf(raw_fname, preload=True)
      2 print(raw)
      3 

~/anaconda3/lib/python3.7/site-packages/mne/io/edf/edf.py in read_raw_bdf(input_fname, eog, misc, stim_channel, exclude, infer_types, include, preload, units, encoding, verbose)
   1516                   stim_channel=stim_channel, exclude=exclude,
   1517                   infer_types=infer_types, preload=preload, include=include,
-> 1518                   units=units, encoding=encoding, verbose=verbose)
   1519 
   1520 

<decorator-gen-366> in __init__(self, input_fname, eog, misc, stim_channel, exclude, infer_types, preload, include, units, encoding, verbose)

~/anaconda3/lib/python3.7/site-packages/mne/io/edf/edf.py in __init__(self, input_fname, eog, misc, stim_channel, exclude, infer_types, preload, include, units, encoding, verbose)
    141         info, edf_info, orig_units = _get_info(input_fname, stim_channel, eog,
    142                                                misc, exclude, infer_types,
--> 143                                                preload, include)
    144         logger.info('Creating raw.info structure...')
    145 

~/anaconda3/lib/python3.7/site-packages/mne/io/edf/edf.py in _get_info(fname, stim_channel, eog, misc, exclude, infer_types, preload, include)
    428     misc = misc if misc is not None else []
    429 
--> 430     edf_info, orig_units = _read_header(fname, exclude, infer_types, include)
    431 
    432     # XXX: `tal_ch_names` to pass to `_check_stim_channel` should be computed

~/anaconda3/lib/python3.7/site-packages/mne/io/edf/edf.py in _read_header(fname, exclude, infer_types, include)
    414     logger.info('%s file detected' % ext.upper())
    415     if ext in ('bdf', 'edf'):
--> 416         return _read_edf_header(fname, exclude, infer_types, include)
    417     elif ext == 'gdf':
    418         return _read_gdf_header(fname, exclude, include), None

~/anaconda3/lib/python3.7/site-packages/mne/io/edf/edf.py in _read_edf_header(fname, exclude, infer_types, include)
    680         else:
    681             meas_date = fid.read(8).decode('latin-1')
--> 682             day, month, year = [int(x) for x in meas_date.split('.')]
    683             year = year + 2000 if year < 85 else year + 1900
    684 

~/anaconda3/lib/python3.7/site-packages/mne/io/edf/edf.py in <listcomp>(.0)
    680         else:
    681             meas_date = fid.read(8).decode('latin-1')
--> 682             day, month, year = [int(x) for x in meas_date.split('.')]
    683             year = year + 2000 if year < 85 else year + 1900
    684 

ValueError: invalid literal for int() with base 10: '\x00\x00\x00\x01\x00\x00\x00\x04'

Is it really because my BDF file contains an invalid date? If it’s the case, how can it be possible and how do we fix it?

I recorded the data by myself. The software was Actiview (Biosemi). For the coding, I am on ubuntu 20.04 and I use jupyter notebook.
Thanks everyone.

Can you open the file with other tools such as SigViewer, EEGLAB, or BioSig?

2 Likes

Thank you for asking. Yes I am able to open the file with EEGLAB, for instance.

So far, I did’nt solve my problem of “reading a bdf file” with MNE python.

Can you share the file so we can have a look?

Just wanted to draw the team’s attention to a very similar traceback as this one when I tried reading in an EDF file from the CHB-MIT public database.(available here:CHB-MIT Scalp EEG Database v1.0.0 )

See this post of mine:Unable to read .edf file - #11 by monkeyontiger
Please disregard if irrelevant; I thought that edf and bdf files share the underlying format and the error thrown is almost identical, so this may be relevant.

@monkeyontiger which file from that database did not work for you? I tried chb01_01.edf, which works fine.

1 Like

Thanks for looking into this. Indeed I tried the same file and others from the database. I had used wget to download all files from chb01. All throw the same error. Did you download using the http link provided in the browser? But good to know the file is not the problem. Most likely my setup then; I am still learning. Just in case its totally obvious to you, my sys_info below.
Platform: macOS-12.6-arm64-arm-64bit
Python: 3.9.16 | packaged by conda-forge | (main, Feb 1 2023, 21:38:11) [Clang 14.0.6 ]
Executable: ~/miniconda3/envs/mne1_3/bin/python
CPU: arm: 8 cores
Memory: 16.0 GB

mne: 1.3.1
numpy: 1.23.5 {OpenBLAS 0.3.21 with 8 threads}
scipy: 1.10.1
matplotlib: 3.7.1 {backend=Qt5Agg}

sklearn: 1.2.2
numba: 0.56.4
nibabel: 5.0.1
nilearn: 0.10.0
dipy: 1.6.0
openmeeg: 2.5.5
cupy: Not found
pandas: 1.5.3
pyvista: 0.38.5 {OpenGL 4.1 Metal - 76.3 via Apple M1}
pyvistaqt: 0.9.1
ipyvtklink: 0.2.3
vtk: 9.2.6
qtpy: 2.3.0 {PyQt5=5.15.6}
ipympl: 0.9.3
pyqtgraph: 0.13.2
pooch: v1.7.0

mne_bids: 0.12
mne_nirs: Not found
mne_features: Not found
mne_qt_browser: 0.4.0
mne_connectivity: Not found
mne_icalabel: Not found

Yes, I downloaded them directly with my browser and not wget.

1 Like

I downloaded the chb01_01.edf file using the browser link and could successfully open it in mne using the mne_io_read_raw_edf function. This means error with my wget download. Appreciate the quick feedback @cbrnr

2 Likes