-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSAT-Classification-SVM-stats.py
256 lines (210 loc) · 8.82 KB
/
SAT-Classification-SVM-stats.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 23 16:42:01 2024
Classification of the Spatial Auditory Attention using SVM
----------------------------------------------------------
Feature used: Statistical features of ERP
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 scipy.io import loadmat
from scipy.stats import skew, kurtosis
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
#%% load data
rootpath = r'L:\Cloud\NeuroCFN\RESEARCH PROJECT\Research Project 02\Classification\Data'
# EEGLab file to load (.set)
filename = 'P04_SAT_AllProcessed.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)
# loading correct trials
trialsfile = 'P04Sat_CorrTrials.mat'
corrTrialsData = loadmat(op.join(rootpath, trialsfile))
# correct trials
corrTrials = [item[0] for item in corrTrialsData['event_name'][0]]
#%% epoching
tmin = -0.25
tmax = 3
# extracting event ids of correct trials from eventinfo
event_id =[eventinfo[corrTrials[idx]] for idx in range(len(corrTrials))]
# epoching
epochs = mne.Epochs(
raw,
events= events,
event_id= event_id,
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)
# event id of left attended trials
trlsLeft = [event_id[idx] for idx, trial in enumerate(corrTrials) if 'left' in trial]
# event id of right attended trials
trlsRight = [event_id[idx] for idx, trial in enumerate(corrTrials) if 'right' in trial]
#%% feature extraction
# channels to select
chan2sel = [30, 6, 41, 1, 36, 2, 31, 0, 35]
# chan2sel = [1, 4, 5, 6, 7, 8]
# extract eeg data from selected channels
eegdata = np.mean(epochs.get_data()[:,chan2sel,126:],axis= 1)
# number of tones in left stream
lefttones = 4
# number of tones in right stream
righttones = 5
# vector with left tone onsets
lefttpts = np.linspace(0,3,lefttones+1)
# vector with right tone onsets
rightttpts = np.linspace(0,3,righttones+1)
# index of left and right tone onsets except the first tone
toneidx = (np.hstack((lefttpts[1:-1] * sfreq +1, rightttpts[1:-1] * sfreq +1))).astype(int)
# time duration analysed for each tone (150ms to 300ms post tone onset)
st = .15
ed = .3
tid = [int(st * sfreq), int(ed * sfreq)]
ntrls, _ = eegdata.shape
ERPfeatures = []
# loop over trials
for itrl in range(ntrls):
# loop over time frame
feat = []
for t in toneidx:
# extracting data for current time points
data = eegdata[itrl, t+tid[0]:t+tid[1]]
# -- computing features
mean = np.mean(data) # mean
stdv = np.std(data) # standard deviation
median = np.median(data) # median
skewness = skew(data) # skewness
kurt = kurtosis(data) # kurtosis
waveform = np.sum(np.abs(np.diff(data))) # waveform length
slopesign = np.sum(np.diff(np.sign(np.diff(data)))) # slope sign change
energy = np.sum(data ** 2) # energy
# store features within each trial
feat.extend([mean, stdv, median, skewness, kurt, waveform, slopesign, energy])
# store feature for each trial
ERPfeatures.append(np.array(feat))
#%% create feature and label vector
# create labels
labels = []
for trial in corrTrials:
if 'left' in trial:
labels.append(0)
elif 'right' in trial:
labels.append(1)
# feature vector (X)
X = np.array(ERPfeatures)
# label vector (y)
y = np.array(labels)
#%% 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)
# 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}%')
#%% plotting results
stats = ['Mean', 'SD', 'Median', 'Skewness', 'Kurtosis', 'Waveform Length', 'Slope Sign Change', 'Energy']
tones = ['tone 2', 'tone 3', 'tone 4', 'tone 5']
streams = ['Left Tones', 'Right Tones']
# Create grouped bar plot
bar_width = 0.25
x = np.arange(len(tones))
# loop over stats
for istat in range(len(stats)):
fig, axs = plt.subplots(2)
fig.suptitle(f'{stats[istat]}')
for icond in range(2):
ct = istat
for itone in range(len(tones)):
if itone == 3:
axs[icond].bar(itone + bar_width, np.mean(X[:,ct+(3*len(stats))][y==icond]), bar_width, color = 'mediumslateblue', label='Left Tone')
else:
axs[icond].bar(itone, np.mean(X[:,ct][y==icond]), bar_width, label='Left Tone', color = 'darkslateblue')
axs[icond].bar(itone + bar_width, np.mean(X[:,ct+(3*len(stats))][y==icond]), bar_width, color = 'mediumslateblue', label='Right Tone')
ct = ct + len(stats)
axs[icond].set_xticks(x + bar_width / 2)
axs[icond].set_xticklabels(tones)
axs[icond].set_ylabel(f'{stats[istat]}')
# axs[icond].legend()
if icond == 0:
titlestring = 'Left Attended Condition'
else:
titlestring = 'Right Attended Condition'
axs[icond].set_title(titlestring)
#%% averaged
stats = ['Mean', 'SD', 'Median', 'Skewness', 'Kurtosis', 'Waveform Length', 'Slope Sign Change', 'Energy']
bar_width = 0.1
# loop over stats
for istat in range(len(stats)):
fig, axs = plt.subplots(2)
fig.suptitle(f'{stats[istat]}')
for icond in range(2):
ct = istat
leftst = []
rightst = []
for itone in range(len(tones)-1):
leftst.append(np.mean(X[:,ct][y==icond]))
rightst.append(np.mean(X[:,ct+(3*len(stats))][y==icond]))
ct = ct + len(stats)
axs[icond].bar(icond, np.mean(leftst), bar_width, label='Left Tone', color = 'darkslateblue')
axs[icond].bar(icond + bar_width, np.mean(rightst), bar_width, label='Right Tone', color = 'mediumslateblue')
axs[icond].set_ylabel(f'{stats[istat]}')
#%% plotting feature vector
comb = [19, 35]
plt.scatter(X[:,comb[0]][y==0], X[:,comb[1]][y==0], color = 'blue')
plt.scatter(X[:,comb[0]][y==1], X[:,comb[1]][y==1], color = 'orange')