Can I save a PSD spectrum in an mne .fif file?

  • MNE version: e.g. 1.3.1
  • operating system: e.g. Windows 10

I am trying to save some epochs as a psd spectrum in a .fif file, so that my colleague can just load them into their computations. I can create the object and save it just fine, but when I try to load it, it gives me the error: ‘[filename] does not start with a file id tag’.

Here is my code:


# make epochs
epochs = mne.Epochs(raw, events=myevents, event_id=events_dict, tmin=ep_start, tmax=ep_end, baseline=(None,None), reject= None) 

# Compute PSD
PSD_epochs = epochs['ML'].compute_psd(method='multitaper', fmin=frqs[0], fmax=frqs[1], picks=sensors[i_sens] ) 

# save psd epochs
fname=PATHOUT+'PSD_MLpochs/' + SUBS[i_sub] + '_'+ CONDS[i_con] + sens[i_sens] + 'ML_psd_epo.fif'
PSD_epochs.save(fname, overwrite=True)

# load to check
loadpochs = mne.read_epochs(fname, preload=True)

Will I need to simply save the extracted data as a numpy array, or is there a way to save the object as it is?

Thanks for reading!

This saves an HDF5 file, so you shouldn’t use the .fif extension.

You can read the spectrum object back via:

spectrum = mne.time_frequency.read_spectrum(fname)

Best wishes,
Richard

cc @drammock, I wonder if providing an “unsuitable” filename extension during saving should raise an error or a warning.

1 Like

as @richard says, EpochsSpectrum.save() does not save in .fif format, it saves in HDF5 format. If your colleague uses MNE-Python, Richard’s suggestion will work fine: they can use mne.time_frequency.read_spectrum(). If your colleague uses MATLAB it should also be possible to read HDF5 files using h5read()

an error seems too strict, but PR welcome to add a warning.

2 Likes

Thanks! This works perfectly