mne-python icon indicating copy to clipboard operation
mne-python copied to clipboard

Error when reading an EDF file (resampling)

Open cbrnr opened this issue 1 year ago • 6 comments

From the forum:

I'm trying to read a channel from an edf file using the code below:

import mne
edf = mne.io.read_raw_edf("my/edf/file.edf")
out = edf.get_data(picks=["my_channel"], return_times=False)

But the edf.get_data line raises an AssertionError as shown in the following traceback:

my_file:35: in edf_data
    out = edf.get_data(picks=["my_channel"], return_times=False)
<decorator-gen-190>:12: in get_data
    ???
myvenv/lib/python3.12/site-packages/mne/io/base.py:956: in get_data
    getitem = self._getitem(
myvenv/lib/python3.12/site-packages/mne/io/base.py:853: in _getitem
    data = self._read_segment(start=start, stop=stop, sel=sel)
<decorator-gen-187>:12: in _read_segment
    ???
myvenv/lib/python3.12/site-packages/mne/io/base.py:472: in _read_segment
    _ReadSegmentFileProtector(self)._read_segment_file(
myvenv/lib/python3.12/site-packages/mne/io/base.py:2598: in _read_segment_file
    return self.__raw.__class__._read_segment_file(
myvenv/lib/python3.12/site-packages/mne/io/edf/edf.py:220: in _read_segment_file
    return _read_segment_file(
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
                smp_exp = data.shape[-1]
>               assert max(n_smp_read) == smp_exp
E               AssertionError

myvenv/lib/python3.12/site-packages/mne/io/edf/edf.py:442: AssertionError

I cannot share the edf file but I tested with mne==1.4 and with that version works.

This might have been introduced by #11549.

@skjerns do you have time to take a look?

cbrnr avatar Oct 13 '24 09:10 cbrnr

The bug seems to be specific to that certain EDF-file. Without a file to reproduce I'm at loss :-/

Can you create an anonymized version of the EDF with pyedflib?

from pyedflib import highlevel

signals, signal_headers, header = highlevel.read_edf('input.edf')

# scramble data, this should basically destroy any information contained
random_signals = [np.random.choice(sig, len(sig)) for sig in signals]

# Alternatively: Completely random data with same shape as existing signals
# random_signals = [np.random.unform(min(sig), max(sig), len(sig)) for sig in signals]

# Update headers for anonymization
header['patientname'] = 'anonymous'
header['patient_additional'] = 'anonymous'
header['recording_additional'] = 'anonymous'
header['equipment'] = 'anonymous'
header['admincode'] = 'anon'
header['annotations'] = []

# Create an output EDF file with random data
highlevel.write_edf('output.edf', random_signals, signal_headers, header)

skjerns avatar Oct 14 '24 14:10 skjerns

Unfortunately, I do not have the file either. The original reporter in the forum says he can't share the file, but it seems to happen with one specific channel only. However, I already mentioned that this might make it impossible for us to fix the problem.

cbrnr avatar Oct 14 '24 15:10 cbrnr

Hi, @skjerns . I made an anonymized version with two signals: "working_ch" and "failing_ch". output.zip

joseph-pq avatar Oct 14 '24 18:10 joseph-pq

Can reproduce. Will look into it.

Workaround is to use preload=True

import mne
edf = mne.io.read_raw_edf("my/edf/file.edf", preload=True)
out = edf.get_data(picks=["my_channel"], return_times=False)

skjerns avatar Oct 15 '24 09:10 skjerns

commenting to follow up on this.

h-mayorquin avatar Oct 16 '24 04:10 h-mayorquin

@h-mayorquin you don't have to comment if you want to follow a PR/issue, there's a "Subscribe" button for this purpose.

cbrnr avatar Oct 16 '24 05:10 cbrnr