forked from dagush/WholeBrain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AD_Prepro.py
663 lines (581 loc) · 30.6 KB
/
AD_Prepro.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
# --------------------------------------------------------------------------------------
# Full pipeline from:
# [StefanovskiEtAl2019] Stefanovski, L., P. Triebkorn, A. Spiegler, M.-A. Diaz-Cortes, A. Solodkin, V. Jirsa,
# R. McIntosh and P. Ritter; for the Alzheimer's disease Neuromigang Initiative (2019).
# "Linking molecular pathways and large-scale computational modeling to assess candidate
# disease mechanisms and pharmacodynamics in Alzheimer's disease." bioRxiv: 600205.
# Taken from the code at:
# https://github.com/BrainModes/TVB_EducaseAD_molecular_pathways_TVB/blob/master/Educase_AD_study-LS-Surrogate.ipynb
#
# --------------------------------------------------------------------------------------
import numpy as np
import scipy.io as sio
import os, csv, sys
# from pathlib import Path
import matplotlib.pyplot as plt
# import time
# from functions.Utils.decorators import loadOrCompute
import AD_Auxiliar
# --------------------------------------------------------------------------
# Begin setup...
# --------------------------------------------------------------------------
import functions.Utils.plotSC as plotSC
# from functions.Models import Abeta_StefanovskiEtAl2019 as Abeta
# from functions.Models import JansenRit as JR
import functions.Models.DynamicMeanField as DMF
neuronalModel = DMF
import functions.Integrator_EulerMaruyama
integrator = functions.Integrator_EulerMaruyama
integrator.neuronalModel = neuronalModel
integrator.verbose = False
# Integration parms...
# dt = 5e-5
# tmax = 20.
# ds = 1e-4
# Tmaxneuronal = int((tmax+dt))
import functions.BOLDHemModel_Stephan2007 as Stephan2007
import functions.simulate_SimAndBOLD as simulateBOLD
simulateBOLD.integrator = integrator
simulateBOLD.BOLDModel = Stephan2007
from functions import BalanceFIC
BalanceFIC.integrator = integrator
import functions.Observables.FC as FC
import functions.Observables.swFCD as swFCD
import functions.Observables.phFCD as phFCD
import functions.G_optim as G_optim
import functions.Utils.plotFitting as plotFitting
G_optim.simulateBOLD = simulateBOLD
G_optim.integrator = integrator
# --------------------------------------------------------------------------
# End setup...
# --------------------------------------------------------------------------
base_folder = "./Data_Raw/from_Ritter"
save_folder = "./Data_Produced/AD"
def displayResults(gc_range, psp_baseline, psp_peak_freq, eeg_peak_freq):
import matplotlib
from matplotlib.gridspec import GridSpec
# define colormap
lower = plt.cm.jet(np.linspace(0,1,200))
colors = np.vstack(([0,0,0,0],lower))
tmap = matplotlib.colors.LinearSegmentedColormap.from_list('test', colors)
# plot results
plt.figure(figsize=(18, 4))
grid = GridSpec(nrows=1, ncols=3)
x_coord = gc_range.repeat(379)
x_coord_eeg = gc_range.repeat(64)
plt.suptitle("Diagnosis : "+DX, fontweight="bold", fontsize="18", y = 1.05)
# plot psp frequency
plt.subplot(grid[0,0])
plt.hist2d(x_coord, psp_peak_freq.flatten(), bins=[len(gc_range),40], cmap=tmap,
range=[[np.min(gc_range),np.max(gc_range)],[-1,14]] ) #, vmax=100)
plt.colorbar(label="Number of regions")
plt.grid()
plt.ylabel(' Frequency in Hz')
plt.xlabel(' global coupling ')
# plot psp baseline
plt.subplot(grid[0,1])
plt.hist2d(x_coord, psp_baseline.flatten(), bins=[len(gc_range),40], cmap=tmap,
range=[[np.min(gc_range),np.max(gc_range)],[-1,40]])#, vmax=100)
plt.colorbar(label="Number of regions")
plt.grid()
plt.ylabel(' PSP in mV')
plt.xlabel(' global coupling ')
# plot eeg frequency
plt.subplot(grid[0,2])
plt.hist2d(x_coord_eeg, eeg_peak_freq.flatten(), bins=[len(gc_range),40], cmap=tmap,
range=[[np.min(gc_range),np.max(gc_range)],[-1,14]] )#, vmax=100)
plt.colorbar(label="Number of regions")
plt.grid()
plt.ylabel(' Frequency in Hz')
plt.xlabel(' global coupling ')
plt.tight_layout()
plt.show()
# =====================================================================================
# Methods for visualizing AD parameters
# =====================================================================================
def plotAllAbetaHistograms(subjects, classification):
for subject in subjects:
ADClass = classification[subject]
print("plotting subject: {} ({})".format(subject, ADClass))
# pet_path=base_folder+"/PET_loads/"+subject+"/PET_PVC_MG/" + modality
# RH_pet = np.loadtxt(pet_path+"/"+"L.Amyloid_load_MSMAll.pscalar.txt")
# LH_pet = np.loadtxt(pet_path+"/"+"R.Amyloid_load_MSMAll.pscalar.txt")
# subcort_pet = np.loadtxt(pet_path+"/"+"Amyloid_load.subcortical.txt")[-19:]
# abeta_burden = np.concatenate((LH_pet,RH_pet,subcort_pet))
SCnorm, abeta_burden, tau_burden, fullSeries = AD_Auxiliar.loadSubjectData(subject)
# plt.rcParams["figure.figsize"] = (7,5)
# plt.rcParams["figure.dpi"] = 300
plt.figure(num=None, figsize=(8, 6), dpi=200, facecolor='w', edgecolor='k')
n, bins, patches = plt.hist(abeta_burden, bins='auto', color='#0504aa',
alpha=0.7, rwidth=0.85)
plt.grid(axis='y', alpha=0.75)
plt.xlabel('Abeta SUVR')
plt.ylabel('Regions')
plt.suptitle("Abeta histogram ({} ({}))".format(subject, ADClass), fontweight="bold", fontsize="18")
# plt.show()
plt.savefig("./Results/AD/Abeta/"+ADClass+'_'+subject+".png", dpi=200)
plt.close()
def plotAllAbetaTauGraphs(subjects, classification):
for subject in subjects:
ADClass = classification[subject]
print("plotting subject: {} ({})".format(subject, ADClass))
# pet_path=base_folder+"/PET_loads/"+subject+"/PET_PVC_MG/" + modality
# RH_pet = np.loadtxt(pet_path+"/"+"L.Amyloid_load_MSMAll.pscalar.txt")
# LH_pet = np.loadtxt(pet_path+"/"+"R.Amyloid_load_MSMAll.pscalar.txt")
# subcort_pet = np.loadtxt(pet_path+"/"+"Amyloid_load.subcortical.txt")[-19:]
# abeta_burden = np.concatenate((LH_pet,RH_pet,subcort_pet))
SCnorm, ABeta, tau, fullSeries = AD_Auxiliar.loadSubjectData(subject)
# plt.rcParams["figure.figsize"] = (7,5)
# plt.rcParams["figure.dpi"] = 300
plt.figure(num=None, figsize=(8, 6), dpi=200, facecolor='w', edgecolor='k')
plt.scatter(ABeta, tau, color='b', label='Tau')
plt.grid(axis='y', alpha=0.75)
plt.xlabel('Abeta SUVR')
plt.ylabel('Tau SUVR')
plt.suptitle("Abeta vs. Tau ({} ({}))".format(subject, ADClass), fontweight="bold", fontsize="18")
# plt.show()
plt.savefig("./Results/AD/AbetaTau/"+ADClass+'_'+subject+".png", dpi=200)
plt.close()
# =====================================================================================
# Methods to input AD data
# =====================================================================================
def computeAvgSC_HC_Matrix(classification, baseFolder):
HC = [subject for subject in classification.keys() if classification[subject] == 'HC']
print("SC + HC: {} (0)".format(HC[0]))
sc_folder = baseFolder+'/'+HC[0]+"/DWI_processing"
SC = np.loadtxt(sc_folder+"/connectome_weights.csv")
sumMatrix = SC
for subject in HC[1:]:
print("SC + HC: {}".format(subject))
sc_folder = baseFolder+'/'+subject+"/DWI_processing"
SC = np.loadtxt(sc_folder+"/connectome_weights.csv")
sumMatrix += SC
return sumMatrix / len(HC) # but we normalize it afterwards, so we probably do not need this...
def load_all_HC_fMRI(classification, baseFolder):
HC = [subject for subject in classification.keys() if classification[subject] == 'HC']
all_fMRI = {}
for subject in HC:
print("fMRI HC: {}".format(subject))
fMRI_path = base_folder + "/fMRI/" + subject + "/MNINonLinear/Results/Restingstate"
series = np.loadtxt(fMRI_path+"/"+subject+"_Restingstate_Atlas_MSMAll_hp2000_clean.ptseries.txt")
subcSeries = np.loadtxt(fMRI_path+"/"+subject+"_Restingstate_Atlas_MSMAll_hp2000_clean_subcort.ptseries.txt")
fullSeries = np.concatenate((series,subcSeries))
all_fMRI[subject] = fullSeries
return all_fMRI
# def getClassifications(subjects):
# # ============================================================================
# # This code is to check whether we have the information of the type of subject
# # They can be one of:
# # Healthy Controls (HC), Mild Cognitive Impairment (MCI), Alzheimer Disease (AD) or Significant Memory Concern (SMC)
# # ============================================================================
# input_classification = csv.reader(open(base_folder+"/subjects.csv", 'r'))
# classification = dict(filter(None,input_classification))
# mistery = []
# for subject in subjects:
# if subject in classification:
# print('Subject {} classified as {}'.format(subject, classification[subject]))
# else:
# print('Subject {} NOT classified'.format(subject))
# mistery.append(subject)
# print("Misisng {} subjects:".format(len(mistery)), mistery)
# print()
# return classification
# modality = "Amyloid" # Amyloid or Tau
# def loadSubjectData(subject, correctSCMatrix=True):
# sc_folder = base_folder+'/connectomes/'+subject+"/DWI_processing"
# SC = np.loadtxt(sc_folder+"/connectome_weights.csv")
# if correctSCMatrix:
# SCnorm = AD_Auxiliar.correctSC(SC)
# else:
# SCnorm = np.log(SC + 1)
#
# pet_path = base_folder+"/PET_loads/"+subject+"/PET_PVC_MG/" + modality
# RH_pet = np.loadtxt(pet_path+"/"+"L."+modality+"_load_MSMAll.pscalar.txt")
# LH_pet = np.loadtxt(pet_path+"/"+"R."+modality+"_load_MSMAll.pscalar.txt")
# subcort_pet = np.loadtxt(pet_path+"/"+modality+"_load.subcortical.txt")[-19:]
# abeta_burden = np.concatenate((LH_pet,RH_pet,subcort_pet))
#
# fMRI_path = base_folder+"/fMRI/"+subject+"/MNINonLinear/Results/Restingstate"
# series = np.loadtxt(fMRI_path+"/"+subject+"_Restingstate_Atlas_MSMAll_hp2000_clean.ptseries.txt")
# subcSeries = np.loadtxt(fMRI_path+"/"+subject+"_Restingstate_Atlas_MSMAll_hp2000_clean_subcort.ptseries.txt")
# fullSeries = np.concatenate((series,subcSeries))
#
# return SCnorm, abeta_burden, fullSeries
def loadXData(dataset=1):
x_path = "Data_Raw/from_Xenia/"
if dataset == 0:
xfile = 'sc_fromXenia.mat'
M = sio.loadmat(x_path + xfile); print('{} File contents:'.format(xfile), [k for k in M.keys()])
mat0 = M['mat_zero']; print('mat_zero.shape={}'.format(mat0.shape))
SCnorm = AD_Auxiliar.correctSC(mat0)
fc = M['fc']; print('fc.shape={}'.format(fc.shape))
ts = M['timeseries']; print('timeseries.shape={}'.format(ts.shape))
return SCnorm, fc, ts
elif dataset == 1:
xfile = '002_S_0413-reduced-sc.mat'
M = sio.loadmat(x_path + xfile); print('{} File contents:'.format(xfile), [k for k in M.keys()])
xfile_ts = '002_S_0413-reduced-timeseries.mat'
ts = sio.loadmat(x_path + xfile_ts); print('{} File contents:'.format(xfile_ts), [k for k in ts.keys()])
mat0 = M['mat_zero']; print('mat_zero.shape={}'.format(mat0.shape))
SCnorm = AD_Auxiliar.correctSC(mat0)
mat = M['mat']; print('mat.shape={}'.format(mat.shape))
FC = ts['FC']; print('FC.shape={}'.format(FC.shape))
timeseries = ts['timeseries']; print('timeseries.shape={}'.format(timeseries.shape))
return SCnorm, FC, timeseries
elif dataset == 2:
xfile = 'timeseries.mat'
ts = sio.loadmat(x_path + xfile); print('{} File contents:'.format(xfile), [k for k in ts.keys()])
timeseries = ts['timeseries']; print('timeseries.shape={}'.format(timeseries.shape))
return None, None, timeseries
elif dataset == 3:
xfile = '52reg.mat'
ts = sio.loadmat(x_path + xfile); print('{} File contents:'.format(xfile), [k for k in ts.keys()])
# timeseries = ts['timeseries']; print('timeseries.shape={}'.format(timeseries.shape))
# return None, None, timeseries
else:
print("ERROR, no dataset recognized!!!")
exit()
# def thresholdSCMatrix(SC):
# SC[SC > 0.05] = 0.05
# normalizationFactor = 0.2
# avgHuman66 = 0.0035127188987848714
# areasHuman66 = 66 # yeah, a bit redundant... ;-)
# maxNodeInput66 = 0.7275543904602363
# def correctSC(SC):
# N = SC.shape[0]
# logMatrix = np.log(SC+1)
# # areasSC = logMatrix.shape[0]
# # avgSC = np.average(logMatrix)
# # === Normalization ===
# # finalMatrix = normalizationFactor * logMatrix / logMatrix.max() # normalize to the maximum
# # finalMatrix = logMatrix * avgHuman66/avgSC * (areasHuman66*areasHuman66)/(areasSC * areasSC) # normalize to the avg AND the number of connections...
# maxNodeInput = np.max(np.sum(logMatrix, axis=0)) # This is the same as np.max(logMatrix @ np.ones(N))
# finalMatrix = logMatrix * maxNodeInput66 / maxNodeInput
# return finalMatrix
# def analyzeMatrix(name, C):
# max, min, avg, std, maxNodeInput, avgNodeInput = FC.characterizeConnectivityMatrix(C)
# print(name + " => Shape:{}, Max:{}, Min:{}, Avg:{}, Std:{}".format(C.shape, max, min, avg, std), end='')
# print(" => impact=Avg*#:{}".format(avg*C.shape[0]), end='')
# print(" => maxNodeInputs:{}".format(maxNodeInput), end='')
# print(" => avgNodeInputs:{}".format(avgNodeInput))
# =====================================================================================
# Methods to check a few properties AD data
# =====================================================================================
def checkSubjectSCDiff(ax, subject, SCnorm, finalAvgMatrix):
deltaSC = SCnorm - finalAvgMatrix
plotSC(ax, deltaSC, subject)
def checkSubjectSC(ax, subject):
SCnorm, abeta, tau, fullSeries = AD_Auxiliar.loadSubjectData(subject)
plotSC(ax, SCnorm, subject)
def compareTwoSC_WRT_Ref(subjectA, subjectB, refSC=None):
SCnormA, abetaA, tauA, fullSeriesA = AD_Auxiliar.loadSubjectData(subjectA)
SCnormB, abetaB, tauB, fullSeriesB = AD_Auxiliar.loadSubjectData(subjectB)
plt.rcParams.update({'font.size': 15})
fig = plt.figure()
grid = plt.GridSpec(1, 2)
ax1 = fig.add_subplot(grid[0,0])
if refSC is not None:
checkSubjectSCDiff(ax1, subjectA, SCnormA, refSC)
else:
checkSubjectSC(ax1, subjectA)
ax2 = fig.add_subplot(grid[0,1])
if refSC is not None:
checkSubjectSCDiff(ax2, subjectB, SCnormB, refSC)
else:
checkSubjectSC(ax2, subjectB)
plt.suptitle("Structural Connectivity diff ({},{})".format(subjectA, subjectB), fontweight="bold", fontsize="18", y=1.05)
fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.85, 0.2, 0.01, 0.6])
img = ax1.get_images()[0]
fig.colorbar(img, cax=cbar_ax)
plt.show()
# def plotFC_for_G(SCnorm, fMRI):
# # First, load the empirical data
# # SCnorm, abeta, tau, fMRI = AD_Auxiliar.loadSubjectData(subject)
# empFC = FC.from_fMRI(fMRI)
# empFCD = FCD.from_fMRI(fMRI)
#
# # Set the interval of G values to compute
# wStart = 0
# wStep = 0.05 # 0.05
# wEnd = 6 + wStep
# wes = np.arange(wStart + wStep, wEnd, wStep) # warning: the range of wes depends on the conectome.
#
# # now set some simulation variables we need to function...
# # simulateBOLD.Tmax = 20; simulateBOLD.recomputeTmaxneuronal()
# integrator.neuronalModel.initJ(SCnorm.shape[0])
#
# currentValFC = np.inf; currentWeFC = -1
# ccFCempFCsim = np.zeros(len(wes))
# currentValFCD = np.inf; currentWeFCD = -1
# ksFCDempFCDsim = np.zeros(len(wes))
# for kk, we in enumerate(wes): # iterate over the weight range (G in the paper, we here)
# print("Processing: {} >> ".format(we), end='')
# DMF.we = we
# simBDS = simulateBOLD.simulateSingleSubject(SCnorm, warmup=True)
#
# simFC = FC.from_fMRI(simBDS.T)
# ccFCempFCsim[kk] = FC.FC_Similarity(empFC, simFC)
# print(' -> cc[FC_emp, FC_sim] = {}'.format(ccFCempFCsim[kk]), end='')
# if ccFCempFCsim[kk] < currentValFC:
# currentValFC = ccFCempFCsim[kk]
# currentWeFC = we
# print(" >> new min FC !!!")
# else:
# print()
#
# simFCD = FCD.from_fMRI(simBDS)
# ksFCDempFCDsim[kk] = FCD.KolmogorovSmirnovStatistic(empFCD, simFCD)
# print(' -> KS[FCD_emp, FCD_sim] = {}'.format(ksFCDempFCDsim[kk]), end='')
# if ksFCDempFCDsim[kk] < currentValFCD:
# currentValFCD = ccFCempFCsim[kk]
# currentWeFCD = we
# print(" >> new min FCD !!!")
# else:
# print()
#
# plt.plot(wes, ccFCempFCsim, label="FC")
# plt.plot(wes, ksFCDempFCDsim, label="FCD")
# # for line, color in zip([1.47, 4.45], ['r','b']):
# plt.axvline(x=currentWeFC, label='min FC at {}'.format(currentWeFC), c='g')
# plt.axvline(x=currentWeFCD, label='min FCD at {}'.format(currentWeFCD), c='r')
# plt.title("Large-scale network (DMF)")
# plt.ylabel("cc[FC(D)emp,FC(D)sim]")
# plt.xlabel("Global Coupling (G = we)")
# plt.legend()
# plt.show()
def plot_cc_empSC_empFC(subjects):
results = []
for subject in subjects:
empSCnorm, abeta, tau, fMRI = AD_Auxiliar.loadSubjectData(subject)
empFC = FC.from_fMRI(fMRI)
corr_SC_FCemp = FC.pearson_r(empFC, empSCnorm)
print("{} -> Pearson_r(SCnorm, empFC) = {}".format(subject, corr_SC_FCemp))
results.append(corr_SC_FCemp)
plt.figure()
n, bins, patches = plt.hist(results, bins=6, color='#0504aa', alpha=0.7) #, histtype='step') #, rwidth=0.85)
plt.grid(axis='y', alpha=0.75)
plt.xlabel('SC weights')
plt.ylabel('Counts')
plt.title("SC histogram", fontweight="bold", fontsize="18")
plt.show()
# =====================================================================
# =====================================================================
# Single Subject Pipeline
# =====================================================================
# =====================================================================
def preprocessingPipeline(subject, SCnorm, all_fMRI, #, abeta,
distanceSettings, # This is a dictionary of {name: (distance module, apply filters bool)}
WEs, # wStart=0.0, wEnd=6.0, wStep=0.05,
plotMaxFrecForAllWe=False):
fileName = 'Data_Produced/AD/FICWeights-'+subject+'/BenjiBalancedWeights-{}.mat'
print("###################################################################\n"*2+"#")
print(f"## Pre-processing pipeline on {subject}!!!\n#")
print("###################################################################\n"*2+"\n")
print(f"# Compute BalanceFIC ({wStart} to {wEnd} with step {wStep})")
print("###################################################################")
# BalanceFIC.useDeterministicIntegrator = useDeterministicIntegrator
BalanceFIC.verbose = True
balancedParms = BalanceFIC.Balance_AllJ9(SCnorm, WEs=WEs, # wStart=wStart, wEnd=wEnd, wStep=wStep,
baseName=fileName)
# Let's plot it as a verification measure...
if plotMaxFrecForAllWe:
import Fig_DecoEtAl2014_Fig2 as Fig2
Fig2.plotMaxFrecForAllWe(SCnorm, wStart=WEs[0], wEnd=WEs[-1], wStep=WEs[1]-WEs[0],
extraTitle='', precompute=False, fileName=fileName) # We already precomputed everything, right?
# Now, optimize all we (G) values: determine optimal G to work with
print("\n\n###################################################################")
print("# Compute G_Optim")
print("###################################################################\n")
outFilePath = 'Data_Produced/AD/'+subject+'-temp'
fitting = G_optim.distanceForAll_G(SCnorm, all_fMRI, balancedParms, NumSimSubjects=len(all_fMRI),
distanceSettings=distanceSettings,
WEs=WEs, # wStart=wStart, wEnd=wEnd, wStep=wStep,
outFilePath=outFilePath)
optimal = {sd: distanceSettings[sd][0].findMinMax(fitting[sd]) for sd in distanceSettings}
return optimal
# =====================================================================
# =====================================================================
# main
# =====================================================================
# =====================================================================
def processRangeValues(argv):
import getopt
try:
opts, args = getopt.getopt(argv,'',["wStart=","wEnd=","wStep="])
except getopt.GetoptError:
print('AD_Prepro.py --wStart <wStartValue> --wEnd <wEndValue> --wStep <wStepValue>')
sys.exit(2)
wStart = 0.; wEnd = 6.0; wStep = 0.05
for opt, arg in opts:
if opt == '-h':
print('AD_Prepro.py -wStart <wStartValue> -wEnd <wEndValue> -wStep <wStepValue>')
sys.exit()
elif opt in ("--wStart"):
wStart = float(arg)
elif opt in ("--wEnd"):
wEnd = float(arg)
elif opt in ("--wStep"):
wStep = float(arg)
print(f'Input values are: wStart={wStart}, wEnd={wEnd}, wStep={wStep}')
return wStart, wEnd, wStep
visualizeAll = True
if __name__ == '__main__':
import sys
wStart, wEnd, wStep = processRangeValues(sys.argv[1:])
plt.rcParams.update({'font.size': 22})
# =====================================
# Print Human 66 info (as reference)
# =====================================
CFile = sio.loadmat('Data_Raw/Human_66.mat') # load Human_66.mat C
C = CFile['C']
AD_Auxiliar.analyzeMatrix("Unnormalized Human 66", C)
C = 0.2 * C / np.max(C)
AD_Auxiliar.analyzeMatrix(" Norm Human 66", C)
# plotSC.plotSC_and_Histogram("Human 66", C)
# ################################################################################################
# Result:
# Human 66 => Shape:(66, 66), Max:0.19615559289837184, Min:0.0, Avg:0.0035127188987848714, Std:0.01523519221725181
# => impact=Avg*#:0.23183944731980152
# => maxNodeInputs:0.7135693141327057
# ################################################################################################
# --------------------------------------------------
# Classify subject information into {HC, MCI, AD}
# --------------------------------------------------
subjects = [os.path.basename(f.path) for f in os.scandir(base_folder+"/connectomes/") if f.is_dir()]
classification = AD_Auxiliar.checkClassifications(subjects)
# plotAllAbetaHistograms(subjects, classification) # generates a series of histograms of the Abeta burden...
# plotAllAbetaTauGraphs(subjects, classification) # generates a series of Abeta vs. Tau plots...
HCSubjects = [s for s in classification if classification[s] == 'HC']
ADSubjects = [s for s in classification if classification[s] == 'AD']
MCISubjects = [s for s in classification if classification[s] == 'MCI']
print("HCSubjects:", HCSubjects)
print("ADSubjects", ADSubjects)
print("MCISubjects", MCISubjects)
# exit()
# --------------------------------------------------
# Compute the average SC for the HC subjects
# --------------------------------------------------
avgSCMatrix = computeAvgSC_HC_Matrix(classification, base_folder + "/connectomes")
AD_Auxiliar.analyzeMatrix("AvgHC", avgSCMatrix)
finalAvgMatrixHC = AD_Auxiliar.correctSC(avgSCMatrix)
sio.savemat('Data_Produced/AD/AvHC_SC.mat', {'SC':finalAvgMatrixHC})
AD_Auxiliar.analyzeMatrix("AvgHC norm", finalAvgMatrixHC)
print("# of elements in AVG connectome: {}".format(finalAvgMatrixHC.shape))
# plotSC.justPlotSC('AVG<HC>', finalMatrix, plotSC.plotSCHistogram)
# plot_cc_empSC_empFC(HCSubjects)
# --------------------------------------------------
# load all HC fMRI data
# --------------------------------------------------
all_HC_fMRI = load_all_HC_fMRI(classification, base_folder)
# print("# of elements in fMRI: {}".format(fMRI_HC.shape))
# plotSC.justPlotSC(HCSubject, SCnorm_HCSubject, plotSC.plotSC) # plotSC.plotSCHistogram
# plotSC.plotSC_and_Histogram(HCSubject, SCnorm_HCSubject)
# --------------------------------------------------
# play a little bit with one HC subject...
# --------------------------------------------------
# HCSubject = '002_S_0413' # this is HCSubjects[0]
# SCnorm_HCSubject, abeta_HCSubject, tau_HCSubject, fMRI_HCSubject = AD_Auxiliar.loadSubjectData(HCSubject)
# AD_Auxiliar.analyzeMatrix("SC norm HC (log({}))".format(HCSubject), SCnorm_HCSubject)
# # plotSC.plotSC_and_Histogram("SC norm HC", SCnorm_HCSubject)
# empFC = FC.from_fMRI(fMRI_HCSubject)
# AD_Auxiliar.analyzeMatrix("EmpiricalFC", empFC)
# C norm HC (log(002_S_0413)) => Shape:(379, 379), Max:14.250680446001775, Min:0.0, Avg:3.513063979447963, Std:2.418758388149712
# => impact=Avg*#:1331.451248210778
# => maxNodeInputs:2655.478582698918
# plotSC.plotSC_and_Histogram("EmpiricalFC", empFC)
# # sio.savemat(save_folder+'/empFC_{}.mat'.format(HCSubject), {'SC': SCnorm_HCSubject, 'FC': empFC})
# print("# of elements in {}'s connectome: {}".format(HCSubject, SCnorm_HCSubject.shape))
# print("# of elements in "+modality+": {}".format(abeta_HCSubject.shape))
# --------------------------------------------------
# compute the G vs. MaxFiringRate for the subject
# --------------------------------------------------
# Also, compute the FIC params for all G
# testDeco2014_Fig2(SCnorm_HC, HCSubject)
# --------------------------------------------------
# Just a quick check for an AD subject...
# --------------------------------------------------
# ADSubject = ADSubjects[0]
# print("Processing Subjects {} and {}".format(HCSubject, ADSubject))
# compareTwoSC_WRT_Ref(HCSubject, ADSubject) # Plot the two SC at the same time, for comparison...
# Configure and compute Simulation
# ------------------------------------------------
# subjectName = 'AvgHC-N'
subjectName = 'AvgHC'
distanceSettings = {'FC': (FC, False), 'swFCD': (swFCD, True), 'phFCD': (phFCD, True)}
# ----------- Plot whatever results we have collected ------------
# quite useful to peep at intermediate results
# G_optim.loadAndPlot(outFilePath='Data_Produced/AD/'+subjectName+'-temp', distanceSettings=distanceSettings)
# import functions.BalanceFIC as FIC;
BalanceFIC.use_N_algorithm = False # To make sure we use Gus' algorithm
WEs = np.arange(wStart, wEnd++0.01, wStep)
optimal = preprocessingPipeline(subjectName, finalAvgMatrixHC, all_HC_fMRI,
distanceSettings,
WEs=WEs, # wStart=wStart, wEnd=wEnd+0.01, wStep=wStep,
plotMaxFrecForAllWe=False)
print (f"Last info: Optimal in the CONSIDERED INTERVAL only: {wStart}, {wEnd}, {wStep} (not in the whole set of results!!!)")
print("".join(f" - Optimal {k}({optimal[k][1]})={optimal[k][0]}\n" for k in optimal))
print("\n\n###################################################################")
print("# Plot !!!")
print("###################################################################\n")
outFilePath = 'Data_Produced/AD/'+subjectName+'-temp'
plotFitting.loadAndPlot(outFilePath+'/fitting_{}.mat', distanceSettings, WEs=np.arange(0.0, 6.001, 0.001),
empFilePath=outFilePath+'/fNeuro_emp.mat')
#####################################################################################################
# Results (in (0.0, 5.4)):
# - Optimal FC = 0.2937951863392448 @ 3.1
# - Optimal swFCD = 0.1480175847479508 @ 3.55
# - Optimal phFCD = 0.030982182261673485 @ 3.15
#####################################################################################################
# ================================================
# ================================================
# ================================================
# Xenia's test block
# Code to perform tests 4 Xenia
# ================================================
# ================================================
# sc_folder = base_folder+'/connectomes/'+HCSubject+"/DWI_processing"
# SC = np.loadtxt(sc_folder+"/connectome_weights.csv")
# logMatrix = np.log(SC+1)
# SCnorm = normalizationFactor * logMatrix / logMatrix.max() # Normalization
#
# import functions.BOLDFilters as BOLDFilters
# notUsedSC, abeta_HC, tau_HC, fMRI_HC = AD_Auxiliar.loadSubjectData(HCSubject)
# signal_filt = BOLDFilters.BandPassFilter(fMRI_HC)
# sfiltT = signal_filt.T
# FC = np.corrcoef(sfiltT, rowvar=False) # Pearson correlation coefficients
#
# corr0 = np.corrcoef(SC.flatten(), FC.flatten())
# corr1 = np.corrcoef(SCnorm.flatten(), FC.flatten())
# corr2 = FC.pearson_r(SCnorm, FC)
# print("corrcoef(SC,FC)={}".format(corr0))
# print("corrcoef(SCnorm,FC)={}".format(corr1))
# print("FC.pearson_r(SCnorm, FC)={}".format(corr2))
# AD_Auxiliar.analyzeMatrix(subject_X, SCnorm_X)
# empFC = FC.from_fMRI(fMRI_X.T)
# print("KS(empFC, FC(fMRI))=", FCD.KolmogorovSmirnovStatistic(FC_X, empFC))
# =================================== do DMF FIC pre-processing
# subject_X = '002_S_0413-reduced'
# import DecoEtAl2014_Fig2 as DecoEtAl2014
# SCnorm_X, FC_X, fMRI_X = loadXData(dataset=1)
# DecoEtAl2014.filePath = 'Data_Produced/X/FICWeights/BenjiBalancedWeights-'+subject_X+'-{}.mat'
# DMF.initJ(SCnorm_X.shape[0])
# DecoEtAl2014.plotMaxFrecForAllWe(SCnorm_X)
# =================================== test the phFCD code...
# import functions.phaseFCD as phFCD
# subject_X = 'timeseries'
# SCnorm_X, FC_X, fMRI_X = loadXData(dataset=2)
# print("Starting phFCD calculus")
# # start_time = time.clock()
# resFCD = FCD.from_fMRI(fMRI_X)
# # print("\n\n--- TOTAL TIME: {} seconds ---\n\n".format(time.clock() - start_time))
#
# print("FCD sahpe={}".format(resFCD.shape))
# # start_time = time.clock()
# resu = phFCD.from_fMRI(fMRI_X)
# # print("\n\n--- TOTAL TIME: {} seconds ---\n\n".format(time.clock() - start_time))
# print("phFCD shape={}".format(resu.shape))
# print(resu)
# ================================================================================================================
# ================================================================================================================
# ================================================================================================================EOF