Plot TFR_Epochs

Hello everyone,

I am doing a time-frequency analysis using tfr.morlet and I am setting average=False. As output, I obtain an EpochsTFR object with 4 dimensions (epochs, channels, freqs and samples). Does anyone know how can I plot this object?

Hello @Res and welcome to the forum!

There is a list of examples demonstrating how to use and plot EpochsTFR, maybe some of them have what you’re looking for?

https://mne.tools/stable/generated/mne.time_frequency.EpochsTFR.html#examples-using-mne-time-frequency-epochstfr

Good luck,

Richard

1 Like

Thank you! I have already tried them but in these examples they use trf.epochs to perform some other statistics and do not plot directly this output. I tried with tfr.epochs.plot() and I get an error that plot is not supported, then I tried selecting only one channel typing epochs_pow= trf.epochs.data[:, 1, :, :] and plt.plot(epochs_pow) but I get as error: “x and y can be no greater than 2-D, but have shapes (288,) and (500, 8, 3000)”. Basically after I apply trf.morlet on my epochs, I have an output with four dimensions: 500, 64 (channels), 8, 3000 and I would like to plot a spectrogram of each channel, but I can’t see how to do it!

Have a look at this section and the following sections of the time-frequency tutorial, which – I think – describes exactly what you want:

https://mne.tools/stable/auto_tutorials/time-freq/20_sensors_time_frequency.html#inspect-power

Here they are using Average=True (while I’m doing Average=False) so their output has less dimension than mine and they can just plot each channel easily!

Not for power and ITC analysis with Morlet wavelets. See Frequency and time-frequency sensor analysis — MNE 0.23.0 documentation again

Are you referring to this line: power, itc= tfr_morlet(epochs, freqs=freqs, n_cycles=ncycles, use_fft=True, return_itc=True, decim=3, n_jobs=1) ? Here average is True by default! (from here: mne.time_frequency.tfr_morlet — MNE 0.23.0 documentation)

You are right. Ok I have an idea for a workaround.

  1. You generate your TFR epochs with return_itc=False, average=False. This returns an EpochsTFR object, for which there are not built-in plotting functions.
  2. You can index / subset an EpochsTFR object just like Epochs. For example, to get the first EpochsTFR, you can do power[1].
  3. Lastly, if you call the method average(), we will return an AverageTFR, which has the various plotting functions mentioned in the tutorial.

Together, what I propose to do is something like the following:

power  = tfr_morlet(epochs, freqs=freqs, n_cycles=n_cycles, use_fft=True,
                    return_itc=False, decim=3, n_jobs=1, average=False)

epochs_idx = 50
ch_name = 'MEG 1142'

(power[epochs_idx]
 .average()
 .plot(
     picks=ch_name,
     baseline=(-0.5, 0),
     mode='logratio',
     title=f'TFR epoch {epochs_idx}, channel {ch_name}'
 )
)

which produces something like:

1 Like

Thank you so much, really appreciated it!!