Error load fif file

Hello,

I am facing a weird issue. For a few runs, I am unable to load the raw fif file. It returns the following error message. Any idea on how to resolve this is highly appreciated.

  raw = mne.io.read_raw_fif(os.path.join(fpath, fname), allow_maxshield = True)
Traceback (most recent call last):

  File "C:\Users\Aakash\AppData\Local\Temp\ipykernel_6452\68822043.py", line 2, in <cell line: 2>
    raw = mne.io.read_raw_fif(os.path.join(fpath, fname), allow_maxshield = True)

  File "C:\Users\Aakash\AppData\Roaming\Python\Python39\site-packages\mne\io\fiff\raw.py", line 540, in read_raw_fif
    return Raw(

  File "<decorator-gen-271>", line 12, in __init__

  File "C:\Users\Aakash\AppData\Roaming\Python\Python39\site-packages\mne\io\fiff\raw.py", line 93, in __init__
    raw, next_fname, buffer_size_sec = self._read_raw_file(

  File "<decorator-gen-272>", line 12, in _read_raw_file

  File "C:\Users\Aakash\AppData\Roaming\Python\Python39\site-packages\mne\io\fiff\raw.py", line 185, in _read_raw_file
    ff, tree, _ = fiff_open(fname, preload=whole_file)

  File "<decorator-gen-10>", line 12, in fiff_open

  File "C:\Users\Aakash\AppData\Roaming\Python\Python39\site-packages\mne\io\open.py", line 126, in fiff_open
    return _fiff_open(fname, fid, preload)

  File "C:\Users\Aakash\AppData\Roaming\Python\Python39\site-packages\mne\io\open.py", line 185, in _fiff_open
    tree, _ = make_dir_tree(fid, directory)

  File "<decorator-gen-9>", line 12, in make_dir_tree

  File "C:\Users\Aakash\AppData\Roaming\Python\Python39\site-packages\mne\io\tree.py", line 55, in make_dir_tree
    if directory[start].kind == FIFF_BLOCK_START:

TypeError: 'NoneType' object is not subscriptable

can you share a link to one of the files that has this problem? Please also post the output of mne.sys_info()

Below is the output of mne.sys_info() and here is the link to an example file.

Platform             Windows-10-10.0.22621-SP0
Python               3.9.12 (main, Apr  4 2022, 05:22:27) [MSC v.1916 64 bit (AMD64)]
Executable           C:\ProgramData\Anaconda3\python.exe
CPU                  Intel64 Family 6 Model 140 Stepping 1, GenuineIntel (8 cores)
Memory               31.7 GB

Core
β”œβ˜‘ mne               1.4.2
β”œβ˜‘ numpy             1.21.6 (OpenBLAS 0.3.17 with 8 threads)
β”œβ˜‘ scipy             1.9.3
β”œβ˜‘ matplotlib        3.5.3 (backend=module://matplotlib_inline.backend_inline)
C:\ProgramData\Anaconda3\lib\site-packages\paramiko\transport.py:219: CryptographyDeprecationWarning: Blowfish has been deprecated
  "class": algorithms.Blowfish,
β”œβ˜‘ pooch             1.6.0
β””β˜‘ jinja2            3.0.1

Numerical (optional)
β”œβ˜‘ sklearn           1.0.2
β”œβ˜‘ numba             0.56.3
β”œβ˜‘ nibabel           4.0.1
β”œβ˜‘ nilearn           0.9.1
β”œβ˜‘ pandas            1.4.4
β””β˜ unavailable       dipy, openmeeg, cupy

Visualization (optional)
β”œβ˜‘ vtk               9.1.0
β”œβ˜‘ qtpy              2.1.0 (PyQt5=5.15.2)
β””β˜ unavailable       pyvista, pyvistaqt, ipyvtklink, ipympl, pyqtgraph, mne-qt-browser

Ecosystem (optional)
β”œβ˜‘ mne-bids          0.11
β””β˜ unavailable       mne-nirs, mne-features, mne-connectivity, mne-icalabel, mne-bids-pipeline

OK, I can reproduce the error. It seems like the cause is that the FIFF tag directory is not what we expect:

ipdb> p dir_tag
<Tag | kind 0 - type 0 - size 0 - next 0 - pos 0 - data None>

@agramfort or @larsoner I wasn’t able to determine if this is a case of β€œvalid file we can’t handle” or β€œcorrupted file we don’t catch and give informative error about”. Can one of you take a look? Link to the file is in the prior post.

See MAINT: Cleaner reading and warnings by larsoner Β· Pull Request #11801 Β· mne-tools/mne-python Β· GitHub, but on your file with an improved show_fiff I get:

/home/larsoner/python/virtualenvs/base/bin/mne:8: RuntimeWarning: FIF tag directory missing at the end of the file (at byte 547940689), possibly corrupted file: EC_sub17_run03.fif
  sys.exit(main())
/home/larsoner/python/virtualenvs/base/bin/mne:8: RuntimeWarning: Invalid tag with only 6/16 bytes at position 547957371
  sys.exit(main())
999  = FIFFB_ROOT
    100  = FIFF_FILE_ID (20b ids)  = {'version': 65538, 'machid': a ... dict len=4
    101  = FIFF_DIR_POINTER (4b >i4)  = [547940689]
    106  = FIFF_FREE_LIST (4b >i4)  = [-1]
    100  = FIFFB_MEAS
        103  = FIFF_BLOCK_ID (20b ids)  = {'version': 65538, 'machid': a ... dict len=4
        101  = FIFFB_MEAS_INFO
            x1047: 0    = ??? (0b nul)

so indeed it seems like the file is incomplete

Ah, I see. Thanks a lot for checking this out. I will ask my friends to send the data again and confirm. In any case, is it possible to extract the partial data that is present in this file?