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

**URL:** <https://mne.discourse.group/t/bug-report-an-error-in-io-eeglab-and-solution-to-it/7173>\
**Category:** Support & Discussions\
**Tags:** eeglab\
**Created:** [July 4, 2023, 4:24am UTC](https://mne.discourse.group/t/bug-report-an-error-in-io-eeglab-and-solution-to-it/7173 "2023-07-04T04:24:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![frostime](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/frostime/32/2816_2.png) [@frostime](https://mne.discourse.group/u/frostime)\
**Post date:** [July 4, 2023, 4:24am UTC](https://mne.discourse.group/t/bug-report-an-error-in-io-eeglab-and-solution-to-it/7173/1 "2023-07-04T04:24:38Z")

</div>

**❓ 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:

```py
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:

```py
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:

```py
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:

```py
annot = read_annotations(input_fname, uint16_codec=uint16_codec)

```

### Please fix it

I’ve read [Page Redirection](https://mne.tools/dev/install/contributing.html), it told me that before I lanuch a new issue or pr, it’s usually best to open a new issue [our user forum](https://mne.discourse.group/) first.

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

---

<div class="post-metadata">

**Author:** ![agramfort](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@agramfort](https://mne.discourse.group/u/agramfort)\
**Post date:** [July 4, 2023, 6:34am UTC](https://mne.discourse.group/t/bug-report-an-error-in-io-eeglab-and-solution-to-it/7173/2 "2023-07-04T06:34:04Z")

</div>

this seems to be a crash in the matlab reader from [scipy.io](http://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

---

<div class="post-metadata">

**Author:** ![frostime](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/frostime/32/2816_2.png) [@frostime](https://mne.discourse.group/u/frostime)\
**Post date:** [July 4, 2023, 6:58am UTC](https://mne.discourse.group/t/bug-report-an-error-in-io-eeglab-and-solution-to-it/7173/3 "2023-07-04T06:58:46Z")

</div>

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.

 ![image](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/e/e57eda264d8ad1fe5462cbc1860aa4dc4be20813.png)

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.

 ![image](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/c/c07fa9fc06acda3fd2ffb7d696dfcd641340063b.png)

As a result, the error raised:

```python
 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.

 ![image](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/3/3a9789f13056736c51985bfafd5a655244911687.png)

---

<div class="post-metadata">

**Author:** ![agramfort](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@agramfort](https://mne.discourse.group/u/agramfort)\
**Post date:** [July 4, 2023, 7:29am UTC](https://mne.discourse.group/t/bug-report-an-error-in-io-eeglab-and-solution-to-it/7173/4 "2023-07-04T07:29:58Z")

</div>

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

Thanks  
Alex

---

<div class="post-metadata">

**Author:** ![frostime](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/frostime/32/2816_2.png) [@frostime](https://mne.discourse.group/u/frostime)\
**Post date:** [July 4, 2023, 7:54am UTC](https://mne.discourse.group/t/bug-report-an-error-in-io-eeglab-and-solution-to-it/7173/5 "2023-07-04T07:54:57Z")

</div>

Thanks, I’ve opened a pr just now.

> <https://github.com/mne-tools/mne-python/pull/11773>
>
> In \`io.eeglab.RawEEGLab\`, read annotations should also pass argument \`uint16\_cod…ec\`.
> 
> \#### Reference issue
> 
> This problem has been reported in the group, click link for details:
> 
> https://mne.discourse.group/t/bug-report-an-error-in-io-eeglab-and-solution-to-it/7173
> 
> 
> \#### What does this implement/fix?
> 
> Merely by passing the correct codec argument to function call.
> 
> 
> \#### Additional information
> 
> No

This is really a simple and minor change of code.
