RuntimeWarning: EDF Header Record Mismatch and File Size Issue in MNE

import mne
import matplotlib.pyplot as plt

filename = non_excluded_data['Filename'].iloc[0]
filepath ="../datasets/raw/PSG/case03_sub102.EDF"

raw = mne.io.read_raw_edf(filepath, preload=False)


print(raw.info)
duration_seconds = raw.n_times/raw.info['sfreq']
print("Duration (s) : ", duration_seconds)

outputs


Extracting EDF parameters from /home/jovyan/Lucid-Dream-Detection/datasets/raw/PSG/case03_sub102.EDF...
EDF file detected
Setting channel info structure...
Creating raw.info structure...
<Info | 8 non-empty values
 bads: []
 ch_names: Fpz, Fz, Cz, Pz, Oz, Fp1, Fp2, F3, F4, C3, C4, P3, P4, O1, O2, ...
 chs: 27 EEG, 1 Stimulus
 custom_ref_applied: False
 highpass: 0.0 Hz
 lowpass: 500.0 Hz
 meas_date: 2021-06-07 15:17:04 UTC
 nchan: 28
 projs: []
 sfreq: 1000.0 Hz
 subject_info: 3 items (dict)
>
Duration (s) :  805.0
/tmp/ipykernel_80/2266911936.py:7: RuntimeWarning: Number of records from the header does not match the file size (perhaps the recording was not stopped before exiting). Inferring from the file size.
  raw = mne.io.read_raw_edf(filepath, preload=False)

Along with this error, the printed duration isn’t correct since the actual duration is 1050 seconds.

Things that I have tried:

  1. Opened the file in EDFBrowser, which worked correctly. I saw the full length of the data.
  2. Ran a check on EDF compliance and that stated that there was no issue with the EDF file.
  3. Made a trivial annotation and exported the file again, which did not help.

Any help on how to fix this file would be greatly appreciated.

Here are my system details:

Platform             Linux-5.15.0-124-generic-x86_64-with-glibc2.35
Python               3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]
Executable           /usr/bin/python
CPU                  x86_64 (128 cores)
Memory               4031.7 GB

Core
β”œβ˜‘ mne               1.8.0 (latest release)
β”œβ˜‘ numpy             1.24.3 (unknown linalg bindings (threadpoolctl module not found: No module named 'threadpoolctl'))
β”œβ˜‘ scipy             1.14.1
β””β˜‘ matplotlib        3.9.3 (backend=module://matplotlib_inline.backend_inline)

Numerical (optional)
β”œβ˜‘ pandas            2.2.3
β””β˜ unavailable       sklearn, numba, nibabel, nilearn, dipy, openmeeg, cupy, h5io, h5py

Visualization (optional)
β”œβ˜‘ ipywidgets        8.0.6
β””β˜ unavailable       pyvista, pyvistaqt, vtk, qtpy, ipympl, pyqtgraph, mne-qt-browser, trame_client, trame_server, trame_vtk, trame_vuetify

Ecosystem (optional)
β””β˜ unavailable       mne-bids, mne-nirs, mne-features, mne-connectivity, mne-icalabel, mne-bids-pipeline, neo, eeglabio, edfio, mffpy, pybv
None

The file in question is here: case03_sub102.EDF

Hi, I am bumping this thread hoping that someone has the answer.

Maybe @cbrnr has an idea? :slight_smile:

Yeah, I already saw this question. Although I see this message from time to time in my own recordings, I have never had any problems importing them (or at least I think so, because I did not explicitly check if the length is correct).

I will take a look, thanks for the detailed description and the example file!

1 Like

I will not be able to look into this in detail before the holidays, but in the meantime you could use edfio to read the data. I tried this:

from edfio import read_edf

edf = read_edf("case03_sub102.EDF")

And it gives the correct duration (I assume 1051 seconds is correct, or is it really 1050 seconds as you mention?):

>>> edf.duration
1051.0

It shouldn’t be too difficult to convert the returned Edf object to a Raw object (see Creating MNE-Python data structures from scratch β€” MNE 1.9.0 documentation).

1 Like

OK, now maybe all of this is not even necessary, because I cannot reproduce with MNE 1.9.0 (which has been released recently):

>>> import mne
>>> raw = mne.io.read_raw("case03_sub102.EDF")
Extracting EDF parameters from case03_sub102.EDF...
EDF file detected
Setting channel info structure...
Creating raw.info structure...
>>> raw
<RawEDF | case03_sub102.EDF, 28 x 1051000 (1051.0 s), ~26 KiB, data not loaded>

Can you see if upgrading to MNE 1.9.0 fixes the issue for you as well?

1 Like

I appreciate your help! The problem seems to have gone away when I updated to 1.9.0 :smiling_face: Thanks.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.