-
Notifications
You must be signed in to change notification settings - Fork 1
/
06a_statistical_analysis.py
175 lines (144 loc) · 5.42 KB
/
06a_statistical_analysis.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
# %%
# Load all libraries
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.model_selection import GroupKFold, KFold, cross_validate, train_test_split
from sklearn.utils import shuffle
import sklearn.svm
import sklearn.tree
import sklearn.linear_model
import sklearn.neighbors
from galib import ChromosomeBase, ChromosomeChannels, FeaturesException, GAops, DataParser
from galib import ChromosomeChannels, DataParser, GAops
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Patch, Rectangle
import seaborn as sns
from matplotlib import rcParams
rcParams['savefig.facecolor'] = 'white'
# %%
# Loading data
data_parser = DataParser()
data = data_parser.get_data()
ga_ops = GAops(channels=data["features"], freqs=data["freqs"])
# Limit the frequency to 50 Hz
ga_ops.limit_frequency(50)
chrom = ChromosomeChannels(ga_ops)
#%%%
chrom_1 = "(EEG F8-LE, 8.0, 12.0, DS8)(EEG F4-LE, 4.0, 8.0, DS8)(EEG P4-LE, 12.0, 20.0, DS2)(EEG T5-LE, 30.0, 49.75, AGG)"
chrom_2 = "(EEG T5-LE, 30.0, 49.75, AGG)(EEG F8-LE, 8.0, 12.0, DS8)(EEG Cz-LE, 4.0, 8.0, AGG)(EEG F8-LE, 8.0, 12.0, DS8)"
ch1 = ChromosomeChannels(ga_ops).from_str(chrom_1)
ch2 = ChromosomeChannels(ga_ops).from_str(chrom_2)
chJoin = ChromosomeChannels(ga_ops).from_str(chrom_1 + chrom_2)
class chromAll:
def execute(self, x):
return x.reshape(61, 19*513)
chAll = chromAll()
def sas_score(estimator, X_test, y_test):
y_pred = estimator.predict(X_test)
conf_m = confusion_matrix(y_test, y_pred).ravel()
if len(conf_m) != 4: # wrong test data
return {
"specificity": 0,
"accuracy": 0,
"sensitivity": 0,
}
tn, fp, fn, tp = conf_m
r = {
"specificity": 0 if tn == 0 else (tn) / (fp + tn), # specificity"
"accuracy": (tp + tn) / (tp + fp + tn + fn), # accuracy
"sensitivity": 0 if tp == 0 else (tp) / (tp + fn), # sensitivity"
}
return r
#%%
##
# Note: works only if the data are not groupped, e.g. data["id"] contains unique elements
# otherwise change KFold to GroupKFold
assert len(np.unique(data["id"])) == len(data["id"])
# %%
alld = []
from tqdm.auto import tqdm
for run in tqdm(range(100)):
for mname in ["KNN", "SVM"]:
for cht, chrom in [("ch1", ch1), ("ch2", ch2), ("chJoin", chJoin), ("chAll", chAll)]:
if mname == "KNN":
model = sklearn.neighbors.KNeighborsClassifier(3)
elif mname == "SVM":
model = sklearn.svm.SVC()
else:
raise Exception("Unknown model")
shuffled_idx = np.random.permutation(len(data["id"]))
x_data, labels, ids = shuffle(
chrom.execute(data["x"]),
data["y"],
data["id"]
)
# target model
cv = cross_validate(model, x_data, labels,
groups=ids, #cv=GroupKFold(5),
cv=KFold(5),
scoring=sas_score
)
row = {
"model": mname,
"chrom": cht,
"run": run,
"accuracy": cv["test_accuracy"],
"sensitivity": cv["test_sensitivity"],
"specificity": cv["test_specificity"],
}
alld.append(row)
# %%
# alld = []
# for run in range(10):
# for mname in ["KNN", "SVM"]:
# for cht, chrom in [("ch1", ch1), ("ch2", ch2), ("chJoin", chJoin), ("chAll", chAll)]:
# if mname == "KNN":
# model = sklearn.neighbors.KNeighborsClassifier(3)
# elif mname == "SVM":
# model = sklearn.svm.SVC()
# else:
# raise Exception("Unknown model")
# X_train, X_test, y_train, y_test = train_test_split(
# chrom.execute(data["x"]), data["y"], train_size=0.6)
# model.fit(X_train, y_train)
# print("Score", sas_score(model, X_test, y_test))
# row = {
# "model": mname,
# "chrom": cht,
# "run": run,
# **sas_score(model, X_test, y_test)
# }
# alld.append(row)
# %%
df = pd.DataFrame(alld)
df
#%%%
# reuse the results
df = pd.read_pickle("res/stat.pkl.gz")
# %%
dfc = df.copy().drop(columns=["run"])
dfc["accuracy"] = dfc["accuracy"].apply(lambda x: x.mean())
dfc["sensitivity"] = dfc["sensitivity"].apply(lambda x: x.mean())
dfc["specificity"] = dfc["specificity"].apply(lambda x: x.mean())
dfc = dfc.groupby(["model", "chrom"]).agg(["min", "mean", "max"])
dfc2 = dfc["accuracy"].unstack(level=0)
dfc2 = dfc["specificity"].unstack(level=0)
# swap two levels of multilevel column index
dfc2 = dfc2.swaplevel(axis=1).sort_index(axis=1)
dfc2
# %%
from scipy.stats import ttest_rel
dfc = df.copy().drop(columns=["run"])
alld = {}
for (m1, c1), d1 in dfc.groupby(["model", "chrom"]):
row = {}
for (m2, c2), d2 in dfc.groupby(["model", "chrom"]):
if m1 != m2:
continue
#row[c2] = ttest_rel(np.concatenate(list(d1["accuracy"])), np.concatenate(list(d2["accuracy"]))).pvalue
#row[c2] = ttest_rel(d1["accuracy"], d2["accuracy"]).pvalue
row[c2] = ttest_rel(d1["accuracy"].mean(), d2["accuracy"].mean()).pvalue
alld[(m1, c1)] = row
pd.DataFrame(alld)
# %%