read_raw_fif function how does it work?

Hello!

I am a neuroscience student trying to read and plot my first raw meg data using mne and jupyterlab. I have 3 split files named “0116_tsss_raw.fif”, “0116_tsss_raw-1.fif”, “0116_tsss_raw-2.fif”. When I use the mne.io.read_raw_fif (0116_tsss_raw.fif) it automatically reads the first and second file (" “0116_tsss_raw.fif” “0116_tsss_raw-1.fif”) but not the third file ( “0116_tsss_raw-2.fif”).

I do not understand why it always reads the second one but not the third? I thought that the read_raw_fif function always reads all the fif-files in the same directory that ends with “-1” or “-2”? But for some reason it cannot find the third one…

I have also tried to use:
raw_con= mne.io.concatenate_raws([mne.io.read_raw_fif(fname1), mne.io.read_raw_fif(fname2), mne.io.read_raw_fif(fname3)])

But this only results in the fname2- file being read twice.

Thanks for the help!

Hello @kakallander and welcome to the forum!

No, it actually reads the first file and then checks raw.filenames for how to proceed.

Can you please share the output of

raw = mne.io.read_raw('0116_tsss_raw.fif') 
print(raw.filenames)

Please also share the output of

import mne
mne.sys_info()

Lastly, to make your code snippets and output more readable, please select the pasted lines, and click on the “Preformatted text” button in the toolbar here when creating your posts. This will also enable syntax highlighting, which makes reading much easier.

Thanks,
Richard

raw = mne.io.read_raw('0116_tss_raw.fif')
print(raw.filenames)

OUTPUT:


FileNotFoundError Traceback (most recent call last)
Input In [32], in <cell line: 1>()
----> 1 raw = mne.io.read_raw(‘0116_tss_raw.fif’)
2 print(raw.filenames)

File ~\anaconda3\lib\site-packages\mne\io_read_raw.py:96, in read_raw(fname, preload, verbose, **kwargs)
94 ext = “”.join(Path(fname).suffixes)
95 if ext in readers:
—> 96 return readers[ext](fname, preload=preload, verbose=verbose, **kwargs)
97 else:
98 _read_unsupported(fname)

File ~\anaconda3\lib\site-packages\mne\io\fiff\raw.py:482, in read_raw_fif(fname, allow_maxshield, preload, on_split_missing, verbose)
442 @fill_doc
443 def read_raw_fif(fname, allow_maxshield=False, preload=False,
444 on_split_missing=‘raise’, verbose=None):
445 “”“Reader function for Raw FIF data.
446
447 Parameters
(…)
480 are updated accordingly.
481 “””
→ 482 return Raw(fname=fname, allow_maxshield=allow_maxshield,
483 preload=preload, verbose=verbose,
484 on_split_missing=on_split_missing)

File :12, in init(self, fname, allow_maxshield, preload, on_split_missing, verbose)

File ~\anaconda3\lib\site-packages\mne\io\fiff\raw.py:81, in Raw.init(self, fname, allow_maxshield, preload, on_split_missing, verbose)
78 next_fname = fname
79 while next_fname is not None:
80 raw, next_fname, buffer_size_sec =
—> 81 self._read_raw_file(next_fname, allow_maxshield,
82 preload, do_check_ext)
83 do_check_ext = False
84 raws.append(raw)

File :12, in _read_raw_file(self, fname, allow_maxshield, preload, do_check_ext, verbose)

File ~\anaconda3\lib\site-packages\mne\io\fiff\raw.py:146, in Raw._read_raw_file(self, fname, allow_maxshield, preload, do_check_ext, verbose)
144 check_fname(fname, ‘raw’, endings)
145 # filename
→ 146 fname = _check_fname(fname, ‘read’, True, ‘fname’)
147 ext = os.path.splitext(fname)[1].lower()
148 whole_file = preload if ‘.gz’ in ext else False

File :12, in _check_fname(fname, overwrite, must_exist, name, need_dir, verbose)

File ~\anaconda3\lib\site-packages\mne\utils\check.py:244, in _check_fname(fname, overwrite, must_exist, name, need_dir, verbose)
241 raise PermissionError(
242 f’{name} does not have read permissions: {fname}’)
243 elif must_exist:
→ 244 raise FileNotFoundError(f’{name} does not exist: {fname}’)
246 return fname

FileNotFoundError: fname does not exist: C:\Users\test\Documents\Karin_kandidat\0116_tss_raw.fif

import mne
mne.sys_info()

OUTPUT:

Platform: Windows-10-10.0.19042-SP0
Python: 3.9.12 (main, Apr 4 2022, 05:22:27) [MSC v.1916 64 bit (AMD64)]
Executable: C:\Users\test\anaconda3\python.exe
CPU: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel: 4 cores
Memory: 7.4 GB

mne: 1.0.3
numpy: 1.21.5 {blas=mkl_rt, lapack=mkl_rt}
scipy: 1.7.3
matplotlib: 3.5.1 {backend=module://ipympl.backend_nbagg}

sklearn: 1.0.2
numba: 0.55.1
nibabel: Not found
nilearn: Not found
dipy: Not found
cupy: Not found
pandas: 1.4.2
pyvista: Not found
pyvistaqt: Not found
ipyvtklink: Not found
vtk: Not found
PyQt5: 5.9.2
ipympl: 0.9.1
pooch: v1.6.0

mne_bids: Not found
mne_nirs: Not found
mne_features: Not found
mne_qt_browser: Not found
mne_connectivity: Not found

I can see that there is a folder missing in the directory. The files are located at:

fname1 =‘C:/Users/test/Documents/Karin_kandidat/Raw_data/0116/0116_tsss_raw.fif’
fname2=‘C:/Users/test/Documents/Karin_kandidat/Raw_data/0116/0116_tsss_raw-1.fif’
fname3 =‘C:/Users/test/Documents/Karin_kandidat/Raw_data/0116/0116_tsss_raw-2.fif’

raw = mne.io.read_raw('C:/Users/test/Documents/Karin_kandidat/Raw_data/0116/0116_tsss_raw.fif')
print(raw.filenames)

OUTPUT:

Opening raw data file C:/Users/test/Documents/Karin_kandidat/Raw_data/0116/0116_tsss_raw.fif…
Range : 16000 … 1507999 = 16.000 … 1507.999 secs
Ready.
Opening raw data file C:\Users\test\Documents\Karin_kandidat\Raw_data\0116\0116_tsss_raw-1.fif…
Range : 1508000 … 1547999 = 1508.000 … 1547.999 secs
Ready.
(‘C:\Users\test\Documents\Karin_kandidat\Raw_data\0116\0116_tsss_raw.fif’, ‘C:\Users\test\Documents\Karin_kandidat\Raw_data\0116\0116_tsss_raw-1.fif’)

Here you can see that there is no information in the file that describes that it actually consists of 3 parts.

How were these files created? You cannot just manually take several files, name them according to the split naming convention, and expect things to work…

Okay, so for now it thinks it only consists of two files and not three?

I got them from my supervisor and they were named a bit differently when I got them:

  • 0116_tsss_mc.fif
  • 0116-1_tsss_mc.fif
  • 0116-2_tsss_mc.fif

However, when I used the original names. There was an error that the naming konventions are wrong and that it needs to end with raw.fif.

ERROR:
RuntimeWarning: This filename (C:/Users/test/Documents/Karin_kandidat/Raw_data/0116/0116_tsss_raw-1.fif) does not conform to MNE naming conventions. All raw files should end with raw.fif, raw_sss.fif, raw_tsss.fif, _meg.fif, _eeg.fif, _ieeg.fif, raw.fif.gz, raw_sss.fif.gz, raw_tsss.fif.gz, _meg.fif.gz, _eeg.fif.gz or _ieeg.fif.gz

That’s a warning and not an error, meaning that it doesn’t negatively affect operation.

I don’t understand how you got your new filenames into raw.filenames (But only two of them). Simply renaming the files like you said is not sufficient for that. So either you did some additional steps, or your supervisor manually changed the names after initial file creation….

if you renamed manually the files it’s normal you broke the reading of the split files

in the file there is the name of the next file. If it’s not present as it was renamed it cannot find it.

Alex

1 Like

Yep, exactly that is the problem

Okay! Thank you very much for the help! :slight_smile: