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
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