Hi,
I’m trying to figure out which electrodes contribute to classification of eeg data (four classes) but having problems with interpreting the coefficients of the spatial patterns and getting the right shape.
Below is the code I’m using:
a = "Eng.*duck"
b = "Eng.*goat"
c = "Eng.*swan"
d = "Eng.*lion"
r1 = re.compile(a)
r2 = re.compile(b)
r3 = re.compile(c)
r4 = re.compile(d)
l1 = list(filter(r1.match, stim))
l2 = list(filter(r2.match, stim))
l3 = list(filter(r3.match, stim))
l4 = list(filter(r4.match, stim))
subject = SUBJECTS[0]
epoch = mne.read_epochs(f"CrossLingViz_EEG/noauto/{subject}_noauto_epo.fif")
epoch = epoch.copy().resample(100)
train1 = epoch[l1]
train2 = epoch[l2]
train3 = epoch[l3]
train4 = epoch[l4]
# Prepare train & test data
trainx = np.concatenate((train1.get_data(), train2.get_data(), train3.get_data(), train4.get_data()), axis=0)
trainy = np.concatenate((getevents(train1, 1),getevents(train2, 2), getevents(train3, 3), getevents(train4, 4)), axis=0)
train_data, train_labels = shuffle(trainx, trainy, random_state=4)
clf = make_pipeline(Xdawn(), Vectorizer(), LinearModel(LDA(shrinkage='auto', solver='eigen')))
clf.fit(train_data, train_labels)
coef = get_coef(clf, "patterns_", inverse_transform=False)
evoked = EvokedArray(coef, epoch.info, tmin=epoch.times[t])
and I get the following error message:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[307], line 60
57 clf.fit(train_data, train_labels)
59 coef = get_coef(clf, "patterns_", inverse_transform=False)
---> 60 evoked = EvokedArray(coef, epoch.info, tmin=epoch.times[t])
61 Left_respYes.update({isub: evoked})
63 YeArray[isub-1,:,:] = Left_respYes.get(isub).data
File <decorator-gen-254>:12, in __init__(self, data, info, tmin, comment, nave, kind, baseline, verbose)
File ~\AppData\Roaming\jupyterlab-desktop\jlab_server\lib\site-packages\mne\evoked.py:928, in EvokedArray.__init__(self, data, info, tmin, comment, nave, kind, baseline, verbose)
924 raise ValueError('Data must be a 2D array of shape (n_channels, '
925 'n_samples), got shape %s' % (data.shape,))
927 if len(info['ch_names']) != np.shape(data)[0]:
--> 928 raise ValueError('Info (%s) and data (%s) must have same number '
929 'of channels.' % (len(info['ch_names']),
930 np.shape(data)[0]))
932 self.data = data
934 self.first = int(round(tmin * info['sfreq']))
ValueError: Info (32) and data (4) must have same number of channels.
When I check the shape of coef (coef.shape), it is (4,2080).
I assume the number 4 corresponds to the number of classes, but what do the coefficients for each class mean then?
And how can I make this to the shape (n_channels, n_timepoints)? (n_timepoints should be 130 in this case).
Thank you!
Platform: Windows-10-10.0.22621-SP0
Python: 3.8.15 | packaged by conda-forge | (default, Nov 22 2022, 08:42:03) [MSC v.1929 64 bit (AMD64)]
Executable: C:\Users\chojh\AppData\Roaming\jupyterlab-desktop\jlab_server\python.exe
CPU: Intel64 Family 6 Model 154 Stepping 3, GenuineIntel: 16 cores
Memory: 31.7 GB
mne: 1.3.0
numpy: 1.23.5 {MKL 2022.1-Product with 12 threads}
scipy: 1.9.3
matplotlib: 3.6.2 {backend=QtAgg}