Unable to read .edf file

import numpy as np
import mne
from mne.datasets import sample

from mne.preprocessing import ICA
from mne.preprocessing import create_eog_epochs, create_ecg_epochs
import pandas as pd
raw=mne.io.read_raw_edf(‘C:/Users/Admin/Downloads/titus_annot2022.03.11_16.17.39.edf’,preload=True, verbose=False)
print(raw)

it shows error like this

ValueError Traceback (most recent call last)
in
6 from mne.preprocessing import create_eog_epochs, create_ecg_epochs
7 import pandas as pd
----> 8 raw=mne.io.read_raw_edf(‘C:/Users/Admin/Downloads/titus_annot2022.03.11_16.17.39.edf’, preload=True, verbose=False)
9 print(raw)

~\anaconda3\lib\site-packages\mne\io\edf\edf.py in read_raw_edf(input_fname, eog, misc, stim_channel, exclude, preload, verbose)
1218 raise NotImplementedError(
1219 ‘Only EDF files are supported by read_raw_edf, got %s’ % (ext,))
→ 1220 return RawEDF(input_fname=input_fname, eog=eog, misc=misc,
1221 stim_channel=stim_channel, exclude=exclude, preload=preload,
1222 verbose=verbose)

in init(self, input_fname, eog, misc, stim_channel, exclude, preload, verbose)

~\anaconda3\lib\site-packages\mne\io\edf\edf.py in init(self, input_fname, eog, misc, stim_channel, exclude, preload, verbose)
112 logger.info(‘Extracting EDF parameters from {}…’.format(input_fname))
113 input_fname = os.path.abspath(input_fname)
→ 114 info, edf_info, orig_units = _get_info(input_fname,
115 stim_channel, eog, misc,
116 exclude, preload)

~\anaconda3\lib\site-packages\mne\io\edf\edf.py in _get_info(fname, stim_channel, eog, misc, exclude, preload)
356 misc = misc if misc is not None else []
357
→ 358 edf_info, orig_units = _read_header(fname, exclude)
359
360 # XXX: tal_ch_names to pass to _check_stim_channel should be computed

~\anaconda3\lib\site-packages\mne\io\edf\edf.py in _read_header(fname, exclude)
343 logger.info(’%s file detected’ % ext.upper())
344 if ext in (‘bdf’, ‘edf’):
→ 345 return _read_edf_header(fname, exclude)
346 elif ext == ‘gdf’:
347 return _read_gdf_header(fname, exclude), None

~\anaconda3\lib\site-packages\mne\io\edf\edf.py in _read_edf_header(fname, exclude)
569 else:
570 meas_date = fid.read(8).decode(‘latin-1’)
→ 571 day, month, year = [int(x) for x in meas_date.split(’.’)]
572 year = year + 2000 if year < 85 else year + 1900
573

~\anaconda3\lib\site-packages\mne\io\edf\edf.py in (.0)
569 else:
570 meas_date = fid.read(8).decode(‘latin-1’)
→ 571 day, month, year = [int(x) for x in meas_date.split(’.’)]
572 year = year + 2000 if year < 85 else year + 1900
573

ValueError: invalid literal for int() with base 10: ‘’

can you try to load this file with edfbrowser or some other python EDF readers to see if it’s a problem
with the file? cf PyEDF, PyEDFlib

ALex

I tried reading with other method: PyEDFlib, I was unable to read. Problem is with my .edf file only. Thank you for the response.

Some EEG data recording programs write the measurement date in a different format than the official EDF one. Hence, readers stumble over it. There is a case to br made for MNE to just print a warning that the measurement date could not be read, but still return the data.

Where did your data come from exactly? I have seen this error before. If there is some EEG system out there thatis consistently outputting wrong dates we could perhaps find a workaround for it.

1 Like

Could it be that this file does not contain any continuous data, but only annotations (based on the filename)?

Hi! Is this something the MNE developers is still interested in exploring? I have received data from a neurowerk system. The data files end with .edf but when trying to open them I get

Extracting EDF parameters from filename.EDF...
EDF file detected
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
File ~/Desktop/ICU_EEG_python/view_data2.py:18, in <module>
     15 file = 'filename.EDF'
     17 if file.endswith('.EDF'):
---> 18     raw = mne.io.read_raw_edf(file, preload=True)
     19 elif file.endswith('.vhdr'):
     20     raw = mne.io.read_raw_brainvision(file, preload=True)

File ~/anaconda3/envs/mne/lib/python3.10/site-packages/mne/io/edf/edf.py:1331, in read_raw_edf(input_fname, eog, misc, stim_channel, exclude, infer_types, preload, verbose)
   1329 if ext != 'edf':
   1330     raise NotImplementedError(f'Only EDF files are supported, got {ext}.')
-> 1331 return RawEDF(input_fname=input_fname, eog=eog, misc=misc,
   1332               stim_channel=stim_channel, exclude=exclude,
   1333               infer_types=infer_types, preload=preload, verbose=verbose)

File <decorator-gen-224>:12, in __init__(self, input_fname, eog, misc, stim_channel, exclude, infer_types, preload, verbose)

File ~/anaconda3/envs/mne/lib/python3.10/site-packages/mne/io/edf/edf.py:132, in RawEDF.__init__(self, input_fname, eog, misc, stim_channel, exclude, infer_types, preload, verbose)
    130 logger.info('Extracting EDF parameters from {}...'.format(input_fname))
    131 input_fname = os.path.abspath(input_fname)
--> 132 info, edf_info, orig_units = _get_info(input_fname, stim_channel, eog,
    133                                        misc, exclude, infer_types,
    134                                        preload)
    135 logger.info('Creating raw.info structure...')
    137 # Raw attributes

File ~/anaconda3/envs/mne/lib/python3.10/site-packages/mne/io/edf/edf.py:376, in _get_info(fname, stim_channel, eog, misc, exclude, infer_types, preload)
    373 eog = eog if eog is not None else []
    374 misc = misc if misc is not None else []
--> 376 edf_info, orig_units = _read_header(fname, exclude, infer_types)
    378 # XXX: `tal_ch_names` to pass to `_check_stim_channel` should be computed
    379 #      from `edf_info['ch_names']` and `edf_info['tal_idx']` but 'tal_idx'
    380 #      contains stim channels that are not TAL.
    381 stim_channel_idxs, _ = _check_stim_channel(
    382     stim_channel, edf_info['ch_names'])

File ~/anaconda3/envs/mne/lib/python3.10/site-packages/mne/io/edf/edf.py:363, in _read_header(fname, exclude, infer_types)
    361 logger.info('%s file detected' % ext.upper())
    362 if ext in ('bdf', 'edf'):
--> 363     return _read_edf_header(fname, exclude, infer_types)
    364 elif ext == 'gdf':
    365     return _read_gdf_header(fname, exclude), None

File ~/anaconda3/envs/mne/lib/python3.10/site-packages/mne/io/edf/edf.py:624, in _read_edf_header(fname, exclude, infer_types)
    622 else:
    623     meas_date = fid.read(8).decode('latin-1')
--> 624     day, month, year = [int(x) for x in meas_date.split('.')]
    625     year = year + 2000 if year < 85 else year + 1900
    627 meas_time = fid.read(8).decode('latin-1')

File ~/anaconda3/envs/mne/lib/python3.10/site-packages/mne/io/edf/edf.py:624, in <listcomp>(.0)
    622 else:
    623     meas_date = fid.read(8).decode('latin-1')
--> 624     day, month, year = [int(x) for x in meas_date.split('.')]
    625     year = year + 2000 if year < 85 else year + 1900
    627 meas_time = fid.read(8).decode('latin-1')

ValueError: invalid literal for int() with base 10: ''

Is it possible to some how open the raw object and manually change file headers to solve this problem?

I’m on a Ubuntu 20.04 system.

Best and thank you in advance,
Karin

You could try if EDFbrowser can fix the header (I think it has an option to edit/fix the header).

It’s not possible to fix through MNE alone?

Best

It’s hard to say without having the file. It seems like the header does not contain any date, which is not in line with the EDF specifications. We try to be a bit lenient with non-standard EDF files already, but a mandatory missing header field can result in all kinds of problems. Specifically, we need the recording date in our Info structure. That said, if that’s the only issue it should be possible to ignore it and still access the data. I’m just not sure if that’s something we should do in our reader.

I am a newbie in MNE but I hope this is doable:
Reading file in python 3.10 possible with the use of EDFRD module (pip install ,).

Suggest to read all files signal by signal (20 channels) and append them to pandas dataframe

Each file took up to 20 seconds, then I saved the pickled dataframe in a separate file thus later reloading was perfect and fast.

BTW python import:
from edfrd import read_header, read_data_records

1 Like

This is a useful hint; though I don’t know how to fix it. I am trying to read in EDF files available for download from the CHB-MIT database.

I get this:
EDF file detected

the traceback shows these errors:

File ~/miniconda3/envs/mne1_3/lib/python3.9/site-packages/mne/io/edf/edf.py:1412, in read_raw_edf
→ 1412 return RawEDF

File ~/miniconda3/envs/mne1_3/lib/python3.9/site-packages/mne/io/edf/edf.py:141], in RawEDF.init(…)

→ 682 day, month, year = [int(x) for x in meas_date.split(‘.’)]
683 year = year + 2000 if year < 85 else year + 1900
685 meas_time = fid.read(8).decode(‘latin-1’)

ValueError: invalid literal for int() with base 10: '="text/c

This error seems to be due to the measurement date format. Hoping there is a solution or a workaround.

Just a followup, my issue seems to be with downloading files programmatically rather than any issue with mne + edf.py. See here:Error reading raw bdf data - #13 by monkeyontiger