Epochs.compute_psd doesn't working

Now I’m using compute_psd method according to this tutorial Sleep stage classification from polysomnography (PSG) data — MNE 1.2.2 documentation

And when I’m using compute_psd, an error will occur like this

AttributeError: ‘Epochs’ object has no attribute ‘compute_psd’

However, I have tested ‘plot_psd’ method, and it works fine for me.

Can you please advice how to fix this thing? Thank you so much :smiley:

folder = mne.datasets.sample.data_path()
file = os.path.join(folder, 'MEG', 'sample', 'sample_audvis_raw.fif')
raw = mne.io.read_raw_fif(file, preload=True)
samf = raw.info['sfreq']

para = dict(order=3, ftype='butter', output='sos')
h_para = mne.filter.construct_iir_filter(para, f_pass=0.3, sfreq=samf, btype='high', return_copy=False)
l_para = mne.filter.construct_iir_filter(para, f_pass=35.0, sfreq=samf, btype='low', return_copy=False)
high = raw.filter(l_freq=0.3, h_freq=None, picks=None, method='iir', iir_params=h_para)
low = high.filter(l_freq=None, h_freq=35.0, picks=None, method='iir', iir_params=l_para)

n_para = mne.filter.construct_iir_filter(para, f_stop=50.0, sfreq=samf, btype='bandstop', return_copy=False)
fil = low.notch_filter(freqs=50.0, method='iir')
epochs = mne.make_fixed_length_epochs(fil, duration=30.0, overlap=1)

def eeg_power_band(epochs):
    # specific frequency bands
    FREQ_BANDS = {"delta": [0.5, 4.5],
                  "theta": [4.5, 8.5],
                  "alpha": [8.5, 11.5],
                  "sigma": [11.5, 15.5],
                  "beta": [15.5, 30]}

    spectrum = epochs.compute_psd(picks='eeg', fmin=0.5, fmax=30.)
    psds, freqs = spectrum.get_data(return_freqs=True)
    # Normalize the PSDs
    psds /= np.sum(psds, axis=-1, keepdims=True)

    X = []
    for fmin, fmax in FREQ_BANDS.values():
        psds_band = psds[:, :, (freqs >= fmin) & (freqs < fmax)].mean(axis=-1)
        X.append(psds_band.reshape(len(psds), -1))

    return np.concatenate(X, axis=1)

power = eeg_power_band(epochs)

Platform: Windows 10 Home 64-bit
Python: 3.9.10
MNE: 1.2.2


This indicates that you’re not actually running MNE 1.2 (which is the version that introduced this functionality). You can check the output of

import mne 

to get version information,

Best wishes,

Thank you for the answer.
I have already updated MNE by using pip install -U mne but when I’m check the version by using mne.sys_info it shows 1.1.0.
And when I’m using pip list to check the version, it shows mne version 1.2.2

How could I fix this?
Thank you

I don’t know, sounds like some confusion between virtual environments … you should check you’re updating & running MNE from the same virtual environment.

Best wishes,