-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMIClassification_CSPlogvar_SVM_Baseline_vs_Movement.py
206 lines (161 loc) · 6.35 KB
/
MIClassification_CSPlogvar_SVM_Baseline_vs_Movement.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# -*- coding: utf-8 -*-
"""
Created on Tue May 7 11:14:20 2024
Classification of the MI vs Baeline using SVM
----------------------------------------------
The script is used for the offline classification of the MI EEG data between
right hand ME/MI vs the baseline period (no movement or imagined movement).
Feature used: log-var of CSP spatial filtred data
Classification: SVM classifier with 5-Fold crossvalidation
- spliting data using train_test_split
- scaling using StandarScalar
- hyperparameter tuning using GridSearchCV
@author: Abin Jacob
Carl von Ossietzky University Oldenburg
"""
#%% libraries
import mne
import numpy as np
import matplotlib.pyplot as plt
import os.path as op
from matplotlib import mlab
from sklearn.metrics import confusion_matrix, accuracy_score, PrecisionRecallDisplay, precision_score, recall_score, f1_score
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.svm import SVC
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from scipy.linalg import eigh
from mne.decoding import CSP
import math
#%% load data
rootpath = r'/Users/abinjacob/Documents/02. NeuroCFN/Research Module/RM02/Data'
# EEGLab file to load (.set)
filename = 'MAD_MI_Betaband.set'
filepath = op.join(rootpath,filename)
# load file in mne
raw = mne.io.read_raw_eeglab(filepath, eog= 'auto', preload= True)
# eeg paramters
sfreq = raw.info['sfreq']
# eeg signal
EEG = raw.get_data()
nchannels, nsamples = EEG.shape
# channel names
chnames = raw.info['ch_names']
# extracting events
events, eventinfo = mne.events_from_annotations(raw, verbose= False)
#%% epoching
# epoch duration
tmin = -5
tmax = 4
# events
event_id = {'S 1': 1}
# event_id = {'right_execution': 13}
event_names = list(event_id.keys())
# epoching
epochs = mne.Epochs(
raw,
events= events,
event_id= [event_id['S 1']],
# event_id= [event_id['right_execution']],
tmin=tmin, tmax=tmax,
baseline= (tmin, 0),
preload= True,
event_repeated = 'merge',
reject={'eeg': 4.0}) # Reject epochs based on maximum peak-to-peak signal amplitude (PTP)
#%% preparing data for classification
# for each epoch -5s to -1s will be the baseline (no movement/ imagined movement)
# and from 0s to 4s will be the period for movement/imagined movement
# baseline period
tminBase = -5
tmaxBase = -1
# movement/ imagined movement
tminImg = 0
tmaxImg = 4
# finding samples of baseline and movement period
epochDuration = list(range(tmin, tmax+1))
idminBase = int((epochDuration.index(tminBase)) * sfreq)
idmaxBase = int((epochDuration.index(tmaxBase)) * sfreq)
idminImg = int((epochDuration.index(tminImg)) * sfreq)
idmaxImg = int((epochDuration.index(tmaxImg)) * sfreq)
# extracting baseline period EEG
eegBase = epochs.get_data()[:,:,idminBase:idmaxBase]
labelBase = np.zeros(eegBase.shape[0], int)
eegImg = epochs.get_data()[:,:,idminImg:idmaxImg]
labelImg = np.ones(eegImg.shape[0], int)
# create feature vector (X)
X = np.concatenate((eegBase, eegImg), axis=0)
# create label vector (y)
y = np.concatenate((labelBase, labelImg))
#%% compute CSP on whole data and plot components
# compute CSP on train set (using MNE csp)
ncomps = X.shape[1]
cspALL = CSP(n_components=ncomps,reg=None, log=None, transform_into = 'csp_space', norm_trace=False)
cspALL.fit(X, y)
# calculating the number of cols and rows for subplot
ncols = int(math.ceil(np.sqrt(ncomps)))
nrows = int(math.ceil(ncomps / ncols))
# setting figure title
figtitle = 'Motor Execution CSP Patterns'
# creating figure
fig, ax = plt.subplots(nrows,ncols)
fig.suptitle(figtitle, fontsize=16)
ax = ax.flatten()
for icomp in range(ncomps):
# csp patterns
patterns = cspALL.patterns_[icomp].reshape(epochs.info['nchan'],-1)
# creating a mne structure
evoked = mne.EvokedArray(patterns, epochs.info)
# plotting topoplot
evoked.plot_topomap(times=0, axes=ax[icomp], show=False, colorbar=False)
ax[icomp].set_title(f'Comp {icomp + 1}', fontsize=10)
# setting empty axes to false
for i in range(ncomps, len(ax)):
ax[i].set_visible(False)
#%% SVM classifier with 5 fold cross-validation
# split the dataset into trainning and testing set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# compute CSP on train set (using MNE csp)
ncomps = 10
csp = CSP(n_components=ncomps,reg=None, log=None, transform_into = 'csp_space', norm_trace=False)
trainCSP = csp.fit_transform(X_train, y_train)
testCSP = csp.transform(X_test)
# using log-var of CSP weights as features
X_train = np.log(np.var(trainCSP, axis=2))
X_test = np.log(np.var(testCSP, axis=2))
# define a pipeline with preprocessing (scaling) and SVM classifier
pipeline = make_pipeline(StandardScaler(), SVC())
# parameter grid for SVM
param_grid = {
'svc__C': [0.1, 1, 10, 100], # SVM regularization parameter
'svc__gamma': [0.001, 0.01, 0.1, 1], # Kernel coefficient for 'rbf'
'svc__kernel': ['linear', 'rbf', 'poly', 'sigmoid'] # Kernel type
}
# apply cros-validaion on training set to find best SVM parameters
clf = GridSearchCV(pipeline, param_grid, cv=5)
# train the pipeline
clf.fit(X_train, y_train)
# display best parameters found by GridSearchCV
print(f'Best Parameters Found: {clf.best_params_}')
# make predictions
y_pred = clf.predict(X_test)
# generate the confusion matrix
cm = confusion_matrix(y_test, y_pred)
tn, fp, fn, tp = cm.ravel()
# calculate model performance
# accuracy
accuracy = accuracy_score(y_test, y_pred)
# precision (positive predictive value)
precision = precision_score(y_test, y_pred, labels=[0,1], average= 'weighted')
# recall (sensitivy or true positive rate)
recall = recall_score(y_test, y_pred, labels=[0,1], average= 'weighted')
# f1 score (equillibrium between precision and recall)
f1score = f1_score(y_test, y_pred, labels=[0,1], average= 'weighted')
# print model performance
print('Confusion Matrix')
print(cm)
print('Model Performance Metrics')
print(f'Accuracy: {accuracy*100:.2f}%')
print(f'Precision: {precision*100:.2f}%')
print(f'Recall: {recall*100:.2f}%')
print(f'F1 Score: {f1score*100:.2f}%')