EDF Export Issue - Time Point Discrepancy

Hello MNE Forum,

I’m encountering a time point mismatch when exporting EEG data to EDF format. My original EEG data is 8 seconds long, but the EDF file has 9000 time points instead of the expected 8000. The initial data points appears consistent when I compare the MNE data (io.brainvision.brainvision.raw) . Any insights on this discrepancy and how to ensure accurate time points during the export would be greatly appreciated.

Thank you.

Best regards,

Can you provide more details? What is the sampling frequency? What do the trailing (1000) data points look like? Can you maybe post some plots here so that we can compare the signals visually? We’d also appreciate if you posted the code you used to export your raw object to EDF. To let us fully reproduce the issue, you might want to share your data as well.

2 Likes

Thank you very much!

My sampling frequency is: 1000 Hz

pause_segment_5 = pause_segments[i]
for i in range(len(pause_segments)):
    mne.export.export_raw(
        f'pause_segment_{i}.edf',
        pause_segments[i],
        fmt='edf',
        physical_range='auto',
        add_ch_type=False,
        overwrite=True,
        verbose=None
    )
edf5 = mne.io.read_raw_edf(
    f'pause_segment_5.edf',
    eog=None,
    misc=None,
    stim_channel='auto',
    exclude=(),
    include=None
)

The segmented data size looks 8001 but EDF file is 9000.



Can you plot the entire duration of the EDF signal (just one channel)? I would like to see what the last second of data looks like.

2 Likes

Hello,

Thank you very much!
I am sending the screenshots of EDF and MNE raw data.


Also, this link you can access the EDF and FIF files.
https://drive.google.com/drive/folders/13lm7EWK7KMlmc2xdVLxceCQsxNsHjY6T?usp=sharing

It has probably to do with how the record length is chosen for exporting to EDF. This is a technical implementation detail of EDF, which users should not have to care about, but apparently this trips you up here.

Do you really need 8001 samples in your exported file? I think it would work if you had only 8000 samples.

OK, so here’s the full answer. Exporting to EDF currently means that signals must have an integer duration (in seconds). If this is not the case, like in your example which is 8.001s long, the exported data is zero-padded. MNE actually issues a warning in this case:

RuntimeWarning: EDF format requires equal-length data blocks, so 0.999 seconds of zeros were appended to all channels when writing the final block.

So if creating signals with integer durations is not an option for you, you might want to try edfio instead with the following code snippet:

import mne
from edfio import Edf, EdfSignal

raw = mne.io.read_raw_fif("pause_segment_5.fif")
nchan = raw.info["nchan"]
fs = raw.info["sfreq"]
labels = raw.info["ch_names"]

edf = Edf(
    [
        EdfSignal(raw.get_data(ch).flatten(), label=labels[ch], sampling_frequency=fs)
        for ch in range(nchan)
    ],
    data_record_duration=8.001,
)
edf.write("pause_segment_5.edf")
1 Like