-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFreeSurfer_Stats_ttest.py
423 lines (369 loc) · 11.2 KB
/
FreeSurfer_Stats_ttest.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/bin/env python
# coding: utf-8
import os
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import pyplot
from scipy.stats import spearmanr
wdir = os.getcwd()
print(wdir)
for f in os.listdir(wdir):
if f.endswith(".xlsx"):
print(f)
spreadsheet = input("Specify spreadsheet path::")
if os.path.exists(spreadsheet):
print("")
print("File path found.")
else:
print("ERROR: File path not found.")
print(spreadsheet)
data = pd.read_excel(spreadsheet, sheet_name="T1_STATS_new") # FILE INPUT
df = pd.DataFrame(
data,
columns=[
"FS ID",
"Left-Lateral-Ventricle",
"Left-Inf-Lat-Vent",
"Left-Cerebellum-White-Matter",
"Left-Cerebellum-Cortex",
"Left-Thalamus",
"Left-Caudate",
"Left-Putamen",
"Left-Pallidum",
"3rd-Ventricle",
"4th-Ventricle",
"Brain-Stem",
"Left-Hippocampus",
"Left-Amygdala",
"CSF",
"Left-Accumbens-area",
"Left-VentralDC",
"Left-vessel",
"Left-choroid-plexus",
"Right-Lateral-Ventricle",
"Right-Inf-Lat-Vent",
"Right-Cerebellum-White-Matter",
"Right-Cerebellum-Cortex",
"Right-Thalamus",
"Right-Caudate",
"Right-Putamen",
"Right-Pallidum",
"Right-Hippocampus",
"Right-Amygdala",
"Right-Accumbens-area",
"Right-VentralDC",
"Right-vessel",
"Right-choroid-plexus",
"5th-Ventricle",
"WM-hypointensities",
"Left-WM-hypointensities",
"Right-WM-hypointensities",
"non-WM-hypointensities",
"Left-non-WM-hypointensities",
"Right-non-WM-hypointensities",
"Optic-Chiasm",
"CC_Posterior",
"CC_Mid_Posterior",
"CC_Central",
"CC_Mid_Anterior",
"CC_Anterior",
"BrainSegVol",
"BrainSegVolNotVent",
"lhCortexVol",
"rhCortexVol",
"CortexVol",
"lhCerebralWhiteMatterVol",
"rhCerebralWhiteMatterVol",
"CerebralWhiteMatterVol",
"SubCortGrayVol",
"TotalGrayVol",
"SupraTentorialVol",
"SupraTentorialVolNotVent",
"MaskVol",
"BrainSegVol-to-eTIV",
"MaskVol-to-eTIV",
"lhSurfaceHoles",
"rhSurfaceHoles",
"SurfaceHoles",
"EstimatedTotalIntraCranialVol",
],
)
data2 = pd.read_excel(spreadsheet, sheet_name="SYNTH_STATS_new")
df2 = pd.DataFrame(
data2,
columns=[
"FS ID",
"Left-Lateral-Ventricle",
"Left-Inf-Lat-Vent",
"Left-Cerebellum-White-Matter",
"Left-Cerebellum-Cortex",
"Left-Thalamus",
"Left-Caudate",
"Left-Putamen",
"Left-Pallidum",
"3rd-Ventricle",
"4th-Ventricle",
"Brain-Stem",
"Left-Hippocampus",
"Left-Amygdala",
"CSF",
"Left-Accumbens-area",
"Left-VentralDC",
"Left-vessel",
"Left-choroid-plexus",
"Right-Lateral-Ventricle",
"Right-Inf-Lat-Vent",
"Right-Cerebellum-White-Matter",
"Right-Cerebellum-Cortex",
"Right-Thalamus",
"Right-Caudate",
"Right-Putamen",
"Right-Pallidum",
"Right-Hippocampus",
"Right-Amygdala",
"Right-Accumbens-area",
"Right-VentralDC",
"Right-vessel",
"Right-choroid-plexus",
"5th-Ventricle",
"WM-hypointensities",
"Left-WM-hypointensities",
"Right-WM-hypointensities",
"non-WM-hypointensities",
"Left-non-WM-hypointensities",
"Right-non-WM-hypointensities",
"Optic-Chiasm",
"CC_Posterior",
"CC_Mid_Posterior",
"CC_Central",
"CC_Mid_Anterior",
"CC_Anterior",
"CC_total",
"BrainSegVol",
"BrainSegVolNotVent",
"lhCortexVol",
"rhCortexVol",
"CortexVol",
"lhCerebralWhiteMatterVol",
"rhCerebralWhiteMatterVol",
"CerebralWhiteMatterVol",
"SubCortGrayVol",
"TotalGrayVol",
"SupraTentorialVol",
"SupraTentorialVolNotVent",
"MaskVol",
"BrainSegVol-to-eTIV",
"MaskVol-to-eTIV",
"lhSurfaceHoles",
"rhSurfaceHoles",
"SurfaceHoles",
"EstimatedTotalIntraCranialVol",
],
)
# THIS IS FOR ADDING LATERALIZED REGIONS INTO ONE FOR GROUND-TRUTH
Cerebral_White_Matter = df["lhCerebralWhiteMatterVol"] + df["rhCerebralWhiteMatterVol"]
Cerebellar_cortex = df["Left-Cerebellum-Cortex"] + df["Right-Cerebellum-Cortex"]
Corpus_callosum = (
df["CC_Posterior"]
+ df["CC_Mid_Posterior"]
+ df["CC_Central"]
+ df["CC_Mid_Anterior"]
+ df["CC_Anterior"]
)
Hippocampus = df["Left-Hippocampus"] + df["Right-Hippocampus"]
Putamen = df["Left-Putamen"] + df["Right-Putamen"]
Caudate = df["Left-Caudate"] + df["Right-Caudate"]
Globus_Pallidus = df["Left-Pallidum"] + df["Right-Pallidum"]
Cerebellar_White_matter = (
df["Left-Cerebellum-White-Matter"] + df["Right-Cerebellum-White-Matter"]
)
# THIS IS FOR ADDING LATERALIZED REGIONS INTO ONE FOR SYNTH
Cerebral_White_Matter2 = (
df2["lhCerebralWhiteMatterVol"] + df2["rhCerebralWhiteMatterVol"]
)
Cerebellar_cortex2 = df2["Left-Cerebellum-Cortex"] + df2["Right-Cerebellum-Cortex"]
Corpus_callosum2 = (
df2["CC_Posterior"]
+ df2["CC_Mid_Posterior"]
+ df2["CC_Central"]
+ df2["CC_Mid_Anterior"]
+ df2["CC_Anterior"]
)
Hippocampus2 = df2["Left-Hippocampus"] + df2["Right-Hippocampus"]
Putamen2 = df2["Left-Putamen"] + df2["Right-Putamen"]
Caudate2 = df2["Left-Caudate"] + df2["Right-Caudate"]
Globus_Pallidus2 = df2["Left-Pallidum"] + df2["Right-Pallidum"]
Cerebellar_White_matter2 = (
df2["Left-Cerebellum-White-Matter"] + df2["Right-Cerebellum-White-Matter"]
)
coef, p = spearmanr(Cerebral_White_Matter, Cerebral_White_Matter2, nan_policy="omit")
ground_truth_list = []
# ground_truth_list.append(Cerebral_White_Matter)
# ground_truth_list.append(Cerebellar_cortex)
# ground_truth_list.append(Corpus_callosum)
# ground_truth_list.append(Hippocampus)
# ground_truth_list.append(Putamen)
# ground_truth_list.append(Caudate)
# ground_truth_list.append(Globus_Pallidus)
# ground_truth_list.append(Cerebellar_White_matter)
synth_list = []
# synth_list.append(Cerebral_White_Matter2)
# synth_list.append(Cerebellar_cortex2)
# synth_list.append(Corpus_callosum2)
# synth_list.append(Hippocampus2)
# synth_list.append(Putamen2)
# synth_list.append(Caudate2)
# synth_list.append(Globus_Pallidus2)
# synth_list.append(Cerebellar_White_matter2)
# for q in ground_truth_list:
# for l in synth_list:
# coef, p = spearmanr(q, l, nan_policy='omit')
# print('Cerebellar_White_matter Mean Ground-Truth:', (round(q.mean(),2)))
# print('Mean Synth:', (round(l.mean(),2)))
# print('Standard Deviation Ground-Truth:', round(q.std(),2))
# print('Standard Deviation Synth:', round(l.std(),2))
# print('Coef:', coef)
# print('P-value:', "%.10f" % p)
# print(w, 'Coef:', coef)
# print(w, 'P-value:', p)
# print(coef)
# print('P-value:', "%.10f" % p)
# print(Cerebral_White_Matter.mean())
# print(Cerebral_White_Matter.std())
# print(Cerebral_White_Matter2.mean())
# print(Cerebral_White_Matter2.std())
regions = [
"CortexVol",
"SubCortGrayVol",
"Brain-Stem",
"TotalGrayVol",
"CerebralWhiteMatterVol",
]
for i in regions:
coef, p = spearmanr(df[i], df2[i], nan_policy="omit")
print(i, "Standard deviation GROUND-TRUTH:", df[i].std())
print(i, "Mean GROUND-TRUTH:", df[i].mean())
print(i, "Standard deviation SYNTH:", df2[i].std())
print(i, "Mean SYNTH:", df2[i].mean())
# ---------------- df = Ground-Truth
# ---------------- df = Synth
print(i, "P-value:", "%.10f" % p)
print(i, "Spearmans correlation coefficient: %.3f" % coef)
coef, p = spearmanr(df["Brain-Stem"], df2["Brain-Stem"], nan_policy="omit")
print(coef, p)
# alpha = 0.05
# if p > alpha:
# print('Samples are uncorrelated (fail to reject H0) p=%.3f' % p)
# else:
# print('Samples are correlated (reject H0) p=%.3f' % p)
# print("Standard deviation:", df['SubCortGrayVol'].std())
# print("Mean:", df['SubCortGrayVol'].mean())
# print("Standard deviation:", df2['SubCortGrayVol'].std())
# print("Mean:", df2['SubCortGrayVol'].mean())
# Bonferroni with 11 tests
# x = 0.05/11
# print(x)
data = pd.read_excel(spreadsheet, sheet_name="T1_STATS_new") # FILE INPUT
df = pd.DataFrame(
data,
columns=[
"FS ID",
"Left-Lateral-Ventricle",
"Left-Inf-Lat-Vent",
"Left-Cerebellum-White-Matter",
"Left-Cerebellum-Cortex",
"Left-Thalamus",
"Left-Caudate",
"Left-Putamen",
"Left-Pallidum",
"3rd-Ventricle",
"4th-Ventricle",
"Brain-Stem",
"Left-Hippocampus",
"Left-Amygdala",
"CSF",
"Left-Accumbens-area",
"Left-VentralDC",
"Left-vessel",
"Left-choroid-plexus",
"Right-Lateral-Ventricle",
"Right-Inf-Lat-Vent",
"Right-Cerebellum-White-Matter",
"Right-Cerebellum-Cortex",
"Right-Thalamus",
"Right-Caudate",
"Right-Putamen",
"Right-Pallidum",
"Right-Hippocampus",
"Right-Amygdala",
"Right-Accumbens-area",
"Right-VentralDC",
"Right-vessel",
"Right-choroid-plexus",
"5th-Ventricle",
"WM-hypointensities",
"Left-WM-hypointensities",
"Right-WM-hypointensities",
"non-WM-hypointensities",
"Left-non-WM-hypointensities",
"Right-non-WM-hypointensities",
"Optic-Chiasm",
"CC_Posterior",
"CC_Mid_Posterior",
"CC_Central",
"CC_Mid_Anterior",
"CC_Anterior",
"CC_total",
"BrainSegVol",
"BrainSegVolNotVent",
"lhCortexVol",
"rhCortexVol",
"CortexVol",
"lhCerebralWhiteMatterVol",
"rhCerebralWhiteMatterVol",
"CerebralWhiteMatterVol",
"SubCortGrayVol",
"TotalGrayVol",
"SupraTentorialVol",
"SupraTentorialVolNotVent",
"MaskVol",
"BrainSegVol-to-eTIV",
"MaskVol-to-eTIV",
"lhSurfaceHoles",
"rhSurfaceHoles",
"SurfaceHoles",
"EstimatedTotalIntraCranialVol",
],
) # LIST
pd.set_option("display.max_rows", None)
pd.set_option("display.max_columns", None)
l = df.corrwith(df2, method="spearman", axis=0)
print(l)
X = df2["CC_total"]
Y = df["CC_total"]
def best_fit(X, Y): # User-defined function
xbar = sum(X) / len(X)
ybar = sum(Y) / len(Y)
n = len(X) # or len(Y)
numer = sum(xi * yi for xi, yi in zip(X, Y)) - n * xbar * ybar
denum = sum(xi ** 2 for xi in X) - n * xbar ** 2
b = numer / denum
a = ybar - b * xbar
print("best fit line:\ny = {:.2f} + {:.2f}x".format(a, b))
return a, b
# solution
a, b = best_fit(X, Y)
fig = plt.figure()
ax = fig.add_subplot(111)
plt.ticklabel_format(style="plain")
yfit = [a + b * xi for xi in X]
ax.set_xlabel("Ground-Truth", color="red")
ax.set_ylabel("Synthetic", color="blue")
plt.scatter(X, Y, c="red")
plt.scatter(Y, X, c="blue")
plt.plot(X, yfit)
plt.grid()
plt.title("CC_total")
plt.savefig("Correlation.png") # Output
plt.show()