Instantiating EpochsSpectrum and Spectrum Class Objects

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

Hello, I would like to compute Power Spectral Density of variable sized epochs (from stimulus onset to response onset for e.g.), for this, I am using the following code:


        epochs = read_epochs(thisCleanFile, preload=True)
        for i_ep in range(len(epochs)):
            tmax =  np.asarray(epochs[i_ep].metadata['response_time'])[0]
            epochs_cut = epochs[i_ep].copy().crop(tmin=0, tmax = tmax) 
            epochs_cut._data = 1e6*epochs_cut.get_data()
            psd_this_epoch= epochs_cut.compute_psd(method = 'welch', fmin = 2, 
                                         fmax = 30, tmin = 0, tmax=tmax, n_per_seg = 125)
            power_dB = 10*np.log10(psd_this_epoch.get_data()) #Convert in dB. 
            power_thisSub[i_ep] = power_dB 
        power_stacked = np.stack(np.squeeze(power_thisSub))
        evoked_power = np.nanmean(power_stacked, axis=0)

Now ideally, I would like to store power_stacked and evoked_power in the EpochsSpectrum and Spectrum objects respectively, but direct class instantiation is not supported. In that case, what would be the clean way to store these objects? (I want to do it so that I can have interactive PSD plots with topography and so on).

Thank you in advance.

there’s an example of how to create a Spectrum object from a NumPy array, at the end of this tutorial:

https://mne.tools/dev/auto_tutorials/simulation/10_array_objs.html#sphx-glr-auto-tutorials-simulation-10-array-objs-py

As noted in the tutorial, support for this is experimental, but it currently works and should also work for EpochsSpectrum too. Eventually we’ll probably make this easier (maybe a SpectrumArray class, maybe a Spectrum.from_array method…) but for now please try to adapt the tutorial to your situation and let us know how it goes.

cc @Denis

1 Like

@drammock , Thanks for this tutorial, it should make life much easier. I eventually ended up creating TFR objects with a single timepoint. It wasn’t any good for plotting PSDs, but still made channel selection and topography plots easy.

Also, would it be a good idea to implement plot_psd function for TFR objects, with a give time-resolution (collapsing the time axis to plot PSDs)? If that makes sense, I would try it out.

a plot_psd method for TFR objects is on my personal TODO list as part of updating TFR classes by drammock · Pull Request #11282 · mne-tools/mne-python · GitHub

1 Like