Hello,
Your responses are greatly appreciated. Here is the full code:
# Sampling frequency
sampling_freq = 500
# Import data
mat_data = read_mat('filepath')
data = np.array(mat_data['EEG']['data'])
#Info and raw objects
ch_names = mat_data['EEG']['chanlocs']['labels']
sampling_freq = 500
info = mne.create_info(ch_names, ch_types=['eeg']*64 , sfreq=sampling_freq)
raw = mne.io.RawArray(data,info)
# Define function
def eeg_power_band(epochs):
"""EEG relative power band feature extraction.
This function takes an ``mne.Epochs`` object and creates EEG features based
on relative power in specific frequency bands that are compatible with
scikit-learn.
Parameters
----------
epochs : Epochs
The data.
Returns
-------
X : numpy array of shape [n_samples, 5]
Transformed data.
"""
# specific frequency bands
FREQ_BANDS = {"delta": [0.5, 5.0],
"theta": [5.0, 8.0],
"alpha": [8.0, 13.0],
"sigma": [13.0, 16.0],
"beta": [16.0, 30.0]}
psds, freqs = psd_welch(epochs, picks='eeg', fmin=0.5, fmax=30)
# 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)
# Run calculation and average across electrodes
power = eeg_power_band(raw)
avg_power = np.mean(power, axis=0).reshape(1,5)
A further question is that I see very different relationships between variables when running this script with or without psd normalization. I am quite new to this so I could be mistaken, but shouldn’t the scaling change but the relationships remain stable? Without modifying the code at all, I get a graph that looks like this:
After commenting out the psd normalization but keeping everything else the same, I see this:
# psds /= np.sum(psds, axis=-1, keepdims=True)
Thank you

