Epochs.compute_psd doesn't working

Hello,
Now I’m using compute_psd method according to this tutorial Sleep stage classification from polysomnography (PSG) data — MNE 1.6.0 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)
plt.plot(power)
plt.show()

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

Hi,

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 
mne.sys_info()

to get version information,

Best wishes,
Richard

Hi,
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,
Richard

Hi @richard

I’ve the same problem (ā€œAttributeError: ā€˜Epochs’ object has no attribute ā€˜compute_psdā€™ā€) even if my version is the 1.3.0

here the log:

mne:              1.3.0
numpy:            1.19.3 {OpenBLAS 0.3.12 with 8 threads}
scipy:            1.6.1
matplotlib:       3.4.2 {backend=Qt5Agg}

sklearn:          0.24.1
numba:            Not found
nibabel:          Not found
nilearn:          Not found
dipy:             Not found

cupy:             Not found
pandas:           1.2.3
pyvista:          Not found
pyvistaqt:        Not found
ipyvtklink:       Not found
vtk:              Not found
qtpy:             Not found
ipympl:           Not found
pyqtgraph:        Not found
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
mne_icalabel:     Not found

Some idea to fix the issue? thanks in advance

Hello @BaggioMarco, are you absolutely sure the MNE you’re using to run your script is actually the same that produces the output you shared? How do you run your script? Could you add to your script:

import mne
print(mne.__version__)

and see which version number is being printed?

2 Likes

@richard after unistalling and reinstalling 3 times finally I have the method :wink: I don’t know what was happening

1 Like