[Bug Report] An error in `io.eeglab` and solution to it.

:question: If you have a question or issue with MNE-Python, please include the following info:

  • MNE version: 1.4.2
  • operating system: Windows 10

It sees that a bug occurs in io.read_raw_eeglab

In method io.eeglab.RawEEGLab.__init__() at line 481:

annot = read_annotations(input_fname)

only input_fname is passed into the function, while in read_annotations, if the input_fname is a eeglab *.set file, another argument about codec is also required:

def read_annotations(fname, sfreq="auto", uint16_codec=None):
    #...

    elif name.endswith("set"):
        annotations = _read_annotations_eeglab(fname, uint16_codec=uint16_codec)

My story

I met with the aforementioned problem just now, when I was trying to read a set file by using:

raw = io.read_raw_eeglab(path, preload=False, uint16_codec='latin1', verbose=True)

then I got the error buffer is too small for requested array

Traceback (most recent call last):

return self._matrix_reader.array_from_header(header, process)
File “_mio5_utils.pyx”, line 665, in scipy.io.matlab._mio5_utils.VarReader5.array_from_header
File “_mio5_utils.pyx”, line 712, in scipy.io.matlab._mio5_utils.VarReader5.array_from_header
File “_mio5_utils.pyx”, line 956, in scipy.io.matlab._mio5_utils.VarReader5.read_struct
File “_mio5_utils.pyx”, line 663, in scipy.io.matlab._mio5_utils.VarReader5.read_mi_matrix
File “_mio5_utils.pyx”, line 706, in scipy.io.matlab._mio5_utils.VarReader5.array_from_header
File “_mio5_utils.pyx”, line 870, in scipy.io.matlab._mio5_utils.VarReader5.read_char
TypeError: buffer is too small for requested array

To fix this issue, just replace line 481 with:

annot = read_annotations(input_fname, uint16_codec=uint16_codec)

Please fix it

I’ve read Page Redirection, it told me that before I lanuch a new issue or pr, it’s usually best to open a new issue our user forum first.

If the issue I mentioned above is really a bug, please fix it, thanks! :pray:

this seems to be a crash in the matlab reader from scipy.io

are you sure your file is not corrupted?

can you try to read directly the file with scipy.io.loadmat? it would confirm it’s not a problem on the mne side.

Alex

Hello! I’m quite sure for the issue. Here I give you an example.

The sample set file I use can be normally read by using scipy.io.loadmat with parameter uint16_codec specified.

But when I try to use mne.io.read_raw_eeglab, it will raise out an error at line 481 in the __init__ method in RawEEGLab.

The error’s reason is that the uint16_codec is not correctly specified and a None value is used.

As a result, the error raised:

 File "H:\SrcCode\MyResearch\eeg-annotator\chore\readdata.py", line 20, in <module>
    raw = io.read_raw_eeglab(path, preload=False, uint16_codec='latin1', verbose=True)
  File "h:\SrcCode\MyResearch\eeg-annotator\.venv\lib\site-packages\mne\io\eeglab\eeglab.py", line 311, in read_raw_eeglab
    return RawEEGLAB(
  File "<decorator-gen-305>", line 10, in __init__
  File "h:\SrcCode\MyResearch\eeg-annotator\.venv\lib\site-packages\mne\io\eeglab\eeglab.py", line 481, in __init__
    annot = read_annotations(input_fname)
  File "h:\SrcCode\MyResearch\eeg-annotator\.venv\lib\site-packages\mne\annotations.py", line 1220, in read_annotations
    annotations = _read_annotations_eeglab(fname, uint16_codec=uint16_codec)
  File "h:\SrcCode\MyResearch\eeg-annotator\.venv\lib\site-packages\mne\io\eeglab\eeglab.py", line 759, in _read_annotations_eeglab
    eeg = _check_load_mat(eeg, uint16_codec=uint16_codec)
  File "h:\SrcCode\MyResearch\eeg-annotator\.venv\lib\site-packages\mne\io\eeglab\eeglab.py", line 75, in _check_load_mat
    eeg = _readmat(fname, uint16_codec=uint16_codec)
  File "h:\SrcCode\MyResearch\eeg-annotator\.venv\lib\site-packages\mne\io\eeglab\_eeglab.py", line 82, in _readmat
    return read_mat(fname, uint16_codec=uint16_codec)
  File "h:\SrcCode\MyResearch\eeg-annotator\.venv\lib\site-packages\pymatreader\pymatreader.py", line 92, in read_mat
    raw_data = scipy.io.loadmat(fid, struct_as_record=True,
  File "h:\SrcCode\MyResearch\eeg-annotator\.venv\lib\site-packages\scipy\io\matlab\_mio.py", line 227, in loadmat
    matfile_dict = MR.get_variables(variable_names)
  File "h:\SrcCode\MyResearch\eeg-annotator\.venv\lib\site-packages\scipy\io\matlab\_mio5.py", line 330, in get_variables
    res = self.read_var_array(hdr, process)
  File "h:\SrcCode\MyResearch\eeg-annotator\.venv\lib\site-packages\scipy\io\matlab\_mio5.py", line 290, in read_var_array
    return self._matrix_reader.array_from_header(header, process)
  File "_mio5_utils.pyx", line 665, in scipy.io.matlab._mio5_utils.VarReader5.array_from_header
  File "_mio5_utils.pyx", line 712, in scipy.io.matlab._mio5_utils.VarReader5.array_from_header
  File "_mio5_utils.pyx", line 956, in scipy.io.matlab._mio5_utils.VarReader5.read_struct
  File "_mio5_utils.pyx", line 663, in scipy.io.matlab._mio5_utils.VarReader5.read_mi_matrix
  File "_mio5_utils.pyx", line 706, in scipy.io.matlab._mio5_utils.VarReader5.array_from_header
  File "_mio5_utils.pyx", line 870, in scipy.io.matlab._mio5_utils.VarReader5.read_char
TypeError: buffer is too small for requested array

I worked out to fix it , merely by passing the correct codec argument to function call.

Seems indeed a bug. Can you open a PR on GitHub with your fix ?

Thanks
Alex

Thanks, I’ve opened a pr just now.

This is really a simple and minor change of code.