holgern / pyedflib

pyedflib is a python library to read/write EDF+/BDF+ files based on EDFlib.
http://pyedflib.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
209 stars 121 forks source link

AssertionError when converting "mff" file to "edf" #169

Closed const7 closed 2 years ago

const7 commented 2 years ago

I got an AssertionError when converting my EGI .mff file to .edf.

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
f:\MT-EEG-dataset\2_eeg\Untitled.ipynb in <cell line: 1>()
----> 1 f.writeSamples(data_list)

File ~\miniconda3\lib\site-packages\pyedflib\edfwriter.py:706, in EdfWriter.writeSamples(self, data_list, digital)
    [704] # Check that all channels have different physical_minimum and physical_maximum
    [705] for chan in self.channels:
--> [706]     assert chan['physical_min'] != chan['physical_max'], \
    [707]     'In chan {} physical_min {} should be different from '\
    [708]     'physical_max {}'.format(chan['label'], chan['physical_min'], chan['physical_max'])
    [710] ind = []
    [711] notAtEnd = True

AssertionError: In chan E129 physical_min 0 should be different from physical_max 0

The channel E129 here is a reference channel so the value is always 0, which triggered the AssertionError. Are there any suggestions about how should I deal with it? Thank you!

I have found one merge #89 related to this error but I don't know why a channel with all zero values will be a problem.

skjerns commented 2 years ago

According to the EDF+ Specifications, the min/max need to be different. The underlying C library edflib will raise an error if this is the case, therefore we catch the error already in the Python code.

In this case you can simply change the physical max to e.g. 1 in the signal header before you write the samples to avoid this error. This will not change the signal itself.

const7 commented 2 years ago

It worked. Thank you! Would you consider putting this trick into the code? I think a lot of people may encounter this situation.

skjerns commented 2 years ago

Great to hear!

Although I understand the convenience behind it, I would rather not do that, as it might have unforseen side-effects in some cases.