HOW USE MNE RAW WITH YASA TO GENERATE SPECTOGRAMS. Just in case it can help anyone.

I was having a dificult time in:

Bandpower of an EEG signal.

I was trying to use an mne EDf to get power spectrum, as in the tutorial but it returns an error

data = raw.get_data()
Define sampling frequency and time vector
sf = 100.
time = np.arange(data.size) / sf
Plot the signal

fig, ax = plt.subplots(1, 1, figsize=(12, 4))
plt.plot(time, data, lw=1.5, color='k')
plt.xlabel('Time (seconds)')
plt.ylabel('Voltage')
plt.xlim([time.min(), time.max()])
plt.title('N3 sleep EEG data (F3)')
sns.despine()

AttributeError: ‘RawEDF’ object has no attribute ‘get’
my mne raw file is a edf file and data = raw.get_data() doesnot work:
ValueError: x and y must have same first dimension, but have shapes (1616000,) and (20, 80800)

---------------------

> data = raw.get_data()

doesnot work because :

(1)  Yasa works with 1 EEG channel in numpy array format not mne forma. As it is said in:

https://raphaelvallat.com/yasa/build/html/_modules/yasa/plotting.html



quote:

def plot_spectrogram(data, sf, hypno=None, …

 data : :py:class:`numpy.ndarray`
    Single-channel EEG data. Must be a 1D NumPy array.

(2) my mne raw EEG is a 20 channels signal and as is said in the tutorial
(3) data must be Single-channel EEG data and Must be a 1D NumPy array.

How to solve this problem?

Lets use mne raw EEG from edf to build spectogram:

sample_data_raw_file = ‘C:\000_tmp\00000034_s001_t002.edf’
raw = mne.io.read_raw_edf(sample_data_raw_file, preload=True, verbose=False)

raw_temp = raw.copy()
raw_temp.pick_channels([‘O1’]) #getting 1 channel (mne raw format)

data3d = raw_temp.get_data() #transform to 3D np array (we need 1d)(np format)

data1d = data3d.flatten() #transform to 1D np array:

now it will work:

sf = 100.
time = np.arange(data1d.size) / sf

Plot the signal:

fig, ax = plt.subplots(1, 1, figsize=(12, 4))
plt.plot(time, data1d, lw=1.5, color=‘k’)
plt.xlabel(‘Time (seconds)’)
plt.ylabel(‘Voltage’)
plt.xlim([time.min(), time.max()])
plt.title(‘N3 sleep EEG data (F3)’)
sns.despine()
print(data1d)

The “catch” is to get just 1 raw channel convert to np3D and finally np1D before trying to plot.

1 Like