forked from iricchi/SpinePrint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_identifiability.py
More file actions
1208 lines (977 loc) · 47.1 KB
/
utils_identifiability.py
File metadata and controls
1208 lines (977 loc) · 47.1 KB
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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
This file contains fast implementations for computing the Identifiability matrix of one
or two datasets, along with the PCA decomposition method proposed by E. Amico and J.Goni
in the paper "The quest for identifiability in human functional connectomes"
Authors: Andrea Santoro, edited by Ilaria Ricchi
# to integrate
def compute_MI_BOLD(i,j):
global data_BOLD,mine
mine.compute_score(data_BOLD[:,i], data_BOLD[:,j])
mic=np.around(mine.mic(),3)
return(i,j,mic)
"""
import numpy as np
import pandas as pd
from sklearn.decomposition import PCA
from multiprocessing import Pool
import matplotlib.pyplot as plt
import sys
from matplotlib.patches import Rectangle
import seaborn as sns
from matplotlib import colors
from matplotlib.gridspec import GridSpec
from scipy.io import loadmat
from scipy.spatial.distance import correlation as distcorr
from sklearn.feature_selection import mutual_info_regression as mi
from sklearn.metrics import mutual_info_score
from scipy.spatial.distance import cdist
from dtaidistance import dtw
from dtaidistance import dtw_ndim
from scipy import stats
import math
from math import sqrt
from scipy.linalg import sqrtm, logm, svd
import scipy.spatial.distance as dist
from utils_dtw import *
from minepy import cstats
from minepy import MINE
global mine
mine = MINE(alpha=0.6, c=15, est="mic_e")
## Style adjustements for plots
def plot_adjustments(ax=False, ticks_width=1.5, axis_width=2.5, labelsizereg=16):
if ax == False:
ax = plt.gca()
#plt.rcParams['font.family'] = "PT Serif Caption"
plt.rcParams['xtick.major.width'] = ticks_width
plt.rcParams['ytick.major.width'] = ticks_width
plt.rcParams['axes.linewidth'] = ticks_width
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines["top"].set_linewidth(axis_width)
ax.spines["right"].set_linewidth(axis_width)
ax.spines["left"].set_linewidth(axis_width)
ax.spines["bottom"].set_linewidth(axis_width)
ax.tick_params(direction='out', length=4, width=axis_width + 0.5, colors='k',
grid_color='k', grid_alpha=0.5, axis='both')
ax.tick_params(direction='out', which='minor', length=6, width=axis_width + 0.5, colors='k',
grid_color='k', grid_alpha=0.5, axis='both')
ax.tick_params(direction='out', which='major', length=6, colors='k',
grid_color='k', grid_alpha=0.5, axis='both')
ax.tick_params(axis='both', which='major', labelsize=labelsizereg)
#Taken from https://github.com/scikit-learn/scikit-learn/blob/f3f51f9b611bf873bd5836748647221480071a87/sklearn/utils/extmath.py
def svd_flip(u, v, u_based_decision=True):
"""Sign correction to ensure deterministic output from SVD.
Adjusts the columns of u and the rows of v such that the loadings in the
columns in u that are largest in absolute value are always positive.
Parameters
----------
u : ndarray
u and v are the output of `linalg.svd` or
:func:`~sklearn.utils.extmath.randomized_svd`, with matching inner
dimensions so one can compute `np.dot(u * s, v)`.
v : ndarray
u and v are the output of `linalg.svd` or
:func:`~sklearn.utils.extmath.randomized_svd`, with matching inner
dimensions so one can compute `np.dot(u * s, v)`.
The input v should really be called vt to be consistent with scipy's
output.
u_based_decision : bool, default=True
If True, use the columns of u as the basis for sign flipping.
Otherwise, use the rows of v. The choice of which variable to base the
decision on is generally algorithm dependent.
Returns
-------
u_adjusted, v_adjusted : arrays with the same dimensions as the input.
"""
if u_based_decision:
# columns of u, rows of v
max_abs_cols = np.argmax(np.abs(u), axis=0)
signs = np.sign(u[max_abs_cols, range(u.shape[1])])
u *= signs
v *= signs[:, np.newaxis]
else:
# rows of v, columns of u
max_abs_rows = np.argmax(np.abs(v), axis=1)
signs = np.sign(v[range(v.shape[0]), max_abs_rows])
u *= signs
v *= signs[:, np.newaxis]
return u, v
#https://stackoverflow.com/questions/71844846/is-there-a-faster-way-to-get-correlation-coefficents
def pairwise_correlation(A, B):
'''
Compute the Pearson correlation coefficient between two matrices
input: A -> numpy 2D array
B -> numpy 2D array
output: matrix encoding all the Pearson correlation'''
am = A - np.mean(A, axis=0, keepdims=True)
bm = B - np.mean(B, axis=0, keepdims=True)
return am.T @ bm / (np.sqrt(
np.sum(am**2, axis=0,
keepdims=True)).T * np.sqrt(
np.sum(bm**2, axis=0, keepdims=True)))
# def riemannian_distance(fc1, fc2):
# """
# Compute the Riemannian distance between two FC matrices, ensuring diagonal values are ones.
# """
# fc1 = fc1 @ fc1.T # Making it symmetric positive-definite
# np.fill_diagonal(fc1, 1) # Ensuring diagonal is ones
# fc2 = fc2 @ fc2.T # Making it symmetric positive-definite
# np.fill_diagonal(fc1, 1) # Ensuring diagonal is ones
# # np.fill_diagonal(fc1, 1)
# # np.fill_diagonal(fc2, 1)
# sqrt_fc1 = sqrtm(fc1)
# sqrt_fc2 = sqrtm(fc2)
# log_diff = logm(sqrt_fc1 @ np.linalg.inv(fc2) @ sqrt_fc1)
# return np.linalg.norm(log_diff, 'fro')
def riemannian_distance(P0, P1):
"""
Compute the Riemannian distance between two symmetric positive-definite FC matrices.
Parameters:
- P0: ndarray (n x n), Reference FC matrix
- P1: ndarray (n x n), Target FC matrix
Returns:
- distance: float, Riemannian distance
"""
# Compute the matrix square root inverse of P0
P0_sqrt_inv = np.linalg.inv(sqrtm(P0)) # Equivalent to (P0)^(-1/2)
# Compute the congruence transform
M = P0_sqrt_inv @ P1 @ P0_sqrt_inv # (P0)^(-1/2) * P1 * (P0)^(-1/2)
# Compute the matrix logarithm using SVD
U, S, Vh = svd(M) # Singular Value Decomposition
S_log = np.diag(np.log(S)) # Log of singular values
logM = U @ S_log @ Vh # Reconstruct the log matrix
# Compute the Frobenius norm
distance = np.linalg.norm(logM, 'fro')
return distance
def riemannian_similarity(fc1, fc2, alpha=1.0):
"""
Compute the equivalent of correlation using the exponential of the negative Riemannian distance.
"""
d_r = riemannian_distance(fc1, fc2)
return 1-d_r/np.max(d_r) #np.exp(-alpha * d_r)
def compute_riemannian_Imat(fc_list1, fc_list2):
"""
Compute a similarity matrix for lists of FC matrices.
"""
n = len(fc_list1)
m = len(fc_list2)
assert n == m
# FC_list1 = [fc @ fc.T for fc in fc_list1] # Making them symmetric positive-definite
# FC_list2 = [fc @ fc.T for fc in fc_list2] # Making them symmetric positive-definite
Imat = np.zeros((n, n))
print(fc_list1)
print(fc_list1[0].shape)
print(fc_list2)
print(fc_list2[0].shape)
for i in range(n):
for j in range(m):
Imat[i, j] = riemannian_similarity(fc_list1[i], fc_list2[j])
return Imat
def cohen_d(x,y):
return (np.mean(x) - np.mean(y)) / sqrt((np.std(x, ddof=1) ** 2 + np.std(y, ddof=1) ** 2) / 2.0)
def compute_Iscores(Imat):
'''
Function to compute the Idiff, Iself and Iothers from an identifiability matrix Imat
input: Identiability matrix (nsubjs x nsubjs)
output: Differential Identifiability score (%),
Iself
Iothers
'''
Iself=np.mean(Imat.diagonal())
Iothers=np.mean(Imat[~np.eye(Imat.shape[0],dtype=bool)])
Idiff=(Iself-Iothers)*100
return(Idiff,Iself,Iothers)
def from_triangle2matrix(upper_values, n):
"""Reconstruct a symmetric n x n matrix from its upper triangle values."""
full_matrix = np.zeros((n, n))
u, v = np.triu_indices(n, k=1) # Get upper triangle indicaes
full_matrix[u, v] = upper_values # Fill upper triangle
full_matrix[v, u] = upper_values # Copy to lower triangle
return full_matrix
def computing_FCs_halved(data,trimming_length=0,metric="pairwise_correlation",inter_bet_con=None):
'''
Function for computing Test and Retest Functional Connectomes from a
dictionary in the form {subjID: fMRI data} by splitting each fMRI
recording in two halves
input: data -> dictionary in the form {subjID: fMRI data}
trimming_length -> default 0 (no trimming), if different from 0 then trim
all the recordings to the same length
metric -> pairwise correlation is the default, it accepts: MI, TE, distcorr
output: a 3D array of size (nsubjects x 2 x nedges) reporting the
flattened upper triangular matrix of functional connectomes
constructed when splitting each fMRI recording in two halves
'''
nsubjects=len(data)
n_ROIs=np.shape(list(data.values())[0])[0]
u,v=np.triu_indices(n=n_ROIs,k=1,m=n_ROIs)
FC_list=[];
##Computing the functional connectomes splitting the TC in half
for x in data.values():
x=np.array(x)
if trimming_length!=0:
TC=x[:,:trimming_length]
else:
TC=x
n_timepoints=np.shape(TC)[1]
half_T=int(n_timepoints/2)
if metric == "pairwise_correlation":
FC_1=np.nan_to_num(pairwise_correlation(TC[:,:half_T].T,TC[:,:half_T].T))
FC_2=np.nan_to_num(pairwise_correlation(TC[:,half_T:].T,TC[:,half_T:].T))
elif metric == "MI":
FC_1 = np.zeros((n_ROIs,n_ROIs))
FC_2 = np.zeros((n_ROIs,n_ROIs))
for i in range(n_ROIs):
FC_1[i,:] = mi(TC[:,:half_T].T,TC[i,:half_T], n_neighbors=7)
FC_2[i,:] = mi(TC[:,half_T:].T,TC[i,half_T:], n_neighbors=7)
elif metric == "dist_correlation":
FC_1 = np.zeros((n_ROIs,n_ROIs))
FC_2 = np.zeros((n_ROIs,n_ROIs))
for i in range(n_ROIs):
for j in range(n_ROIs):
FC_1[i,j] = distcorr(TC[i,:half_T].T,TC[j,:half_T].T)
FC_2[i,j] = distcorr(TC[i,half_T:].T,TC[j,half_T:].T)
elif metric == "dtw":
dist_1 = dtw.distance_matrix_fast(TC[:,:half_T],window=2)
dist_2 = dtw.distance_matrix_fast(TC[:,half_T:],window=2)
dist_1_n = dist_1/np.max(dist_1)
dist_2_n = dist_2/np.max(dist_2)
FC_1 = 1-dist_1_n
FC_2 = 1-dist_2_n
FC_list.append([FC_1[u,v],FC_2[u,v]])
FC_list=np.array(FC_list)
return FC_list
def computing_FCs_halved_concat(data1,data2,data,trimming_length=0):
'''
Function for computing Test and Retest Functional Connectomes from a
dictionary in the form {subjID: fMRI data} by splitting each fMRI
recording in two halves
input: data1/2 -> dictionary in the form {subjID: fMRI data}
trimming_length -> default 0 (no trimming), if different from 0 then trim
all the recordings to the same length
output: a 3D array of size (nsubjects x 2 x nedges) reporting the
flattened upper triangular matrix of functional connectomes
constructed when splitting each fMRI recording in two halves
'''
assert len(data1)== len(data2)
nsubjects=len(data1)
n_ROIs1=np.shape(list(data1.values())[0])[0]
n_ROIs2=np.shape(list(data2.values())[0])[0]
u1,v1=np.triu_indices(n=n_ROIs1,k=1,m=n_ROIs1)
u2,v2=np.triu_indices(n=n_ROIs2,k=1,m=n_ROIs2)
u = np.concatenate([u1,u2+n_ROIs1])
v = np.concatenate([v1,v2+n_ROIs1])
FC_list=[];
##Computing the functional connectomes splitting the TC in half
for x in data.values():
x=np.array(x)
if trimming_length!=0:
TC=x[:,:trimming_length]
else:
TC=x
n_timepoints=np.shape(TC)[1]
half_T=int(n_timepoints/2)
FC_1=np.nan_to_num(pairwise_correlation(TC[:,:half_T].T,TC[:,:half_T].T))
FC_2=np.nan_to_num(pairwise_correlation(TC[:,half_T:].T,TC[:,half_T:].T))
FC_list.append([FC_1[u,v],FC_2[u,v]])
FC_list=np.array(FC_list)
return FC_list
def computing_FCs(data,trimming_length=0,metric="pairwise_correlation",inter_bet_con=None,W=1):
'''
Function for computing Functional Connectomes/Correlation Matrices from a
dictionary in the form {subjID: fMRI data}
input: data -> dictionary in the form {subjID: fMRI data}
trimming_length -> default 0 (no trimming), if different from 0 then trim
all the recordings to the same length
output: a 2D array of size (nsubjects x nedges) reporting the
flattened upper triangular matrix of functional connectomes
constructed from the fMRI recordings of each subject'''
nsubjects=len(data)
n_ROIs=np.shape(list(data.values())[0])[0]
u,v=np.triu_indices(n=n_ROIs,k=1,m=n_ROIs)
FC_list=[];
##Computing the functional connectomes for each subject
for x in data.values():
x=np.array(x)
FC = np.zeros((n_ROIs,n_ROIs))
if trimming_length!=0:
TC=x[:,:trimming_length]
else:
TC=x
if metric == "pairwise_correlation":
FC=np.nan_to_num(W*pairwise_correlation(TC.T,TC.T))
elif metric == "MI":
for i in range(n_ROIs):
FC[i,:] = mi(TC.T,TC[i,:], n_neighbors=7)
elif metric == "dist_correlation":
for i in range(n_ROIs):
for j in range(n_ROIs):
FC[i,j] = distcorr(TC[i,:].T,TC[j,:].T)
elif metric == "dtw":
dist = dtw.distance_matrix_fast(TC)
dist_n = dist/np.max(dist)
FC = np.exp( - dist_n)
if inter_bet_con is not None:
br = inter_bet_con['br']
sc = inter_bet_con['sp']
FC_list.append(FC[br:br+sc,:br].flatten())
else:
FC_list.append(FC[u,v])
FC_list=np.array(FC_list)
return (FC_list)
def compute_Imat_from_single_session_TC(data,trimming_length=0,nodes=None,comp_metric='pearson_correlation'):
'''
Function to compute Imat, Idiff, Iself and Iothers from a dictionary in the form
{subjID: fMRI data}.
input: data -> dictionary computing_FCs_halved in the form {subjID: fMRI data}
trimming_length -> default 0 (no trimming), if different from 0 then trim
all the recordings to the same length
output: Identifiability matrix,
Differential Identifiability score (%),
Iself
Iothers
'''
nsubjects=len(data)
n_ROIs=np.shape(list(data.values())[0])[0]
u,v=np.triu_indices(n=n_ROIs,k=1,m=n_ROIs)
FC_list=computing_FCs_halved(data,trimming_length)
##Compute the Identifiability matrix
if comp_metric == 'pearson_correlation':
I_mat=pairwise_correlation(FC_list[:,0].T,FC_list[:,1].T)
elif comp_metric == 'riemannian_sim':
FC_list_mat1 = [from_triangle2matrix(fc,n_ROIs) for fc in FC_list[:,0]]
FC_list_mat2 = [from_triangle2matrix(fc,n_ROIs) for fc in FC_list[:,1]]
I_mat=compute_riemannian_Imat(FC_list_mat1,FC_list_mat2)
elif comp_metric == 'MI':
I_mat = np.zeros((nsubjects,nsubjects))
for i in range(nsubjects):
for j in range(nsubjects):
I_mat[i,j] = mutual_info_score(FC_list[:,0][i,:],FC_list[:,0][j,:])
Idiff,Iself,Iothers=compute_Iscores(I_mat)
return(I_mat,Idiff,Iself,Iothers)
def compute_Imat_from_TC_twodatasets(data1,data2,trimming_length=0,comp_metric='pearson_correlation'):
'''
Function to compute Imat, Idiff, Iself and Iothers from two dictionaries data1 and
data2 in the form {subjID: fMRI data}, data1 is Test, data2 is Retest
input: data1 -> dictionary in the form {subjID: fMRI data}
data2 -> dictionary in the form {subjID: fMRI data}
trimming_length -> default 0 (no trimming), if different from 0 then trim
all the recordings to the same length
output: Identifiability matrix,
Differential Identifiability score (%),
Iself
Iothers
'''
nsubjects=len(data1)
n_ROIs=np.shape(list(data1.values())[0])[0]
u,v=np.triu_indices(n=n_ROIs,k=1,m=n_ROIs)
FC1_list=computing_FCs(data1,trimming_length)
FC2_list=computing_FCs(data2,trimming_length)
##Analogous command of np.array(list(zip(FC1_list,FC2_list)))) but slightly faster
FC_list=np.stack((FC1_list,FC2_list), axis=1)
##Compute the Identifiability matrix
if comp_metric == 'pearson_correlation':
I_mat=pairwise_correlation(FC_list[:,0].T,FC_list[:,1].T)
elif comp_metric == 'riemannian_sim':
FC_list_mat1 = [from_triangle2matrix(fc,n_ROIs) for fc in FC_list[:,0]]
FC_list_mat2 = [from_triangle2matrix(fc,n_ROIs) for fc in FC_list[:,1]]
I_mat=compute_riemannian_Imat(FC_list_mat1,FC_list_mat2)
elif comp_metric == 'MI':
I_mat = np.zeros((nsubjects,nsubjects))
for i in range(nsubjects):
for j in range(nsubjects):
I_mat[i,j] = mutual_info_score(FC_list[:,0][i,:],FC_list[:,0][j,:])
Idiff,Iself,Iothers=compute_Iscores(I_mat)
return(I_mat,Idiff,Iself,Iothers)
def retrieve_n(num_edges):
"""Retrieve n from the number of upper triangular edges."""
n = (1 + math.sqrt(1 + 8 * num_edges)) / 2
return int(n) if n.is_integer() else None # Ensure n is a whole number
def compute_Imat_from_single_session_FCs(FC_list,comp_metric='pearson_correlation'):
'''
Function to compute Imat, Idiff, Iself and Iothers from a 3D array of FCs
of the form (nsubjs x 2 x nedges)
input: data -> numpy array with a shape: nsubjs x 2 x nedges(upper triangular FC)
output: Identifiability matrix,
Differential Identifiability score (%),
Iself
Iothers
'''
nsubjects=len(FC_list)
FC_list=np.array(FC_list)
num_edges = len(FC_list[0][0])
n_ROIs = retrieve_n(num_edges)
##Computing the Identifiability matrix
if comp_metric == 'pearson_correlation':
I_mat=pairwise_correlation(FC_list[:,0].T,FC_list[:,1].T)
elif comp_metric == 'riemannian_sim':
FC_list_mat1 = [from_triangle2matrix(fc,n_ROIs) for fc in FC_list[:,0]]
FC_list_mat2 = [from_triangle2matrix(fc,n_ROIs) for fc in FC_list[:,1]]
I_mat=compute_riemannian_Imat(FC_list_mat1,FC_list_mat2)
elif comp_metric == 'MI':
I_mat = np.zeros((nsubjects,nsubjects))
for i in range(nsubjects):
for j in range(nsubjects):
I_mat[i,j] = mutual_info_score(FC_list[:,0][i,:],FC_list[:,0][j,:])
Idiff,Iself,Iothers=compute_Iscores(I_mat)
return(I_mat,Idiff,Iself,Iothers)
def compute_Imat_from_FCs_twodatasets(FC1_list,FC2_list, comp_metric='pearson_correlation'):
'''
Function to compute Imat, Idiff, Iself and Iothers from two 2D arrays of size (nsubjects x nedges)
reporting the flattened upper triangular matrix of functional connectomes
constructed from the fMRI recordings of each subject
input: FC1_list -> a 2D array of size (nsubjects x nedges) with flattened FCs for TEST
FC2_list -> a 2D array of size (nsubjects x nedges) with flattened FCs for RETEST
output: Identifiability matrix,
Differential Identifiability score (%),
Iself
Iothers
'''
##Analogous command of np.array(list(zip(FC1_list,FC2_list)))) but slightly faster
nsubjects=FC1_list.shape[0]
FC_list=np.stack((FC1_list,FC2_list), axis=1)
num_edges = len(FC_list[0][0])
n_ROIs = retrieve_n(num_edges)
# weighted (for KCL dataset)
if comp_metric == 'pearson_correlation':
I_mat=pairwise_correlation(FC_list[:,0].T,FC_list[:,1].T)
elif comp_metric == 'riemannian_sim':
FC_list_mat1 = [from_triangle2matrix(fc,n_ROIs) for fc in FC1_list]
FC_list_mat2 = [from_triangle2matrix(fc,n_ROIs) for fc in FC2_list]
I_mat=compute_riemannian_Imat(FC_list_mat1,FC_list_mat2)
elif comp_metric == 'MI':
I_mat = np.zeros((nsubjects,nsubjects))
for i in range(nsubjects):
for j in range(nsubjects):
I_mat[i,j] = mutual_info_score(FC1_list[i,:],FC1_list[j,:])
Idiff,Iself,Iothers=compute_Iscores(I_mat)
return(I_mat,Idiff,Iself,Iothers)
def compute_Imat_from_listFCs(data_FC,comp_metric='pearson_correlation'):
'''
Function to compute Imat, Idiff, Iself and Iothers from a 2D array of FCs
of the form (2*nsubjs x nedges), alternating Test, Retest as odd and even
rows
input: data -> numpy array with a shape: 2*nsubjs x nedges(upper triangular FC)
output: Identifiability matrix,
Differential Identifiability score (%),
Iself
Iothers
'''
nsubj_TR,nlinks=np.shape(data_FC)
##nsubj_TR is double of the number of subjects since it contains test and retes
nsubj=int(nsubj_TR/2)##These are all the number of subjects
#Reshape the matrix as a 3D array
data_FC=data_FC.reshape((nsubj,2,nlinks))
n_ROIs = retrieve_n(nlinks)
if comp_metric == 'pearson_correlation':
Imat=pairwise_correlation(data_FC[:,0].T,data_FC[:,1].T)
elif comp_metric == 'riemannian_sim':
FC_list_mat1 = [from_triangle2matrix(fc,n_ROIs) for fc in data_FC[:,0]]
FC_list_mat2 = [from_triangle2matrix(fc,n_ROIs) for fc in data_FC[:,1]]
I_mat=compute_riemannian_Imat(FC_list_mat1,FC_list_mat2)
elif comp_metric == 'MI':
I_mat = np.zeros((nsubj,nsubj))
for i in range(nsubj):
for j in range(nsubj):
I_mat[i,j] = mutual_info_score(FC_list[:,0][i,:],FC_list[:,0][j,:])
Idiff,Iself,Iothers=compute_Iscores(Imat)
return(Imat,Idiff,Iself,Iothers)
def fit_kcomponents(U,S,Vt,all_connectomes,mean_,k_components):
'''
Function that reconstruct the matrix "all_connectomes"
starting from the first k_components of the pca
(and decomposition U,S,Vt) passed as input
input: U,S,Vt-> Decomposition obtained from the command svd decomposition of all connectomes
all_connectomes -> 2d numpy array containing all the flattened FC (upper triangular)
that was passed as input for the pca._fit(all_connectomes)
mean_ -> mean of all the connectomes across rows (i.e. np.mean(all_connectomes,axis=0))
k_components -> integer from 1 to Ncomponents max
output: 2D array containing the reconstructed numpy array of "all_connectomes" using only
k_components
'''
UU = U[:, : k_components]
UU =UU* S[: k_components]
reconstructed=np.dot(UU, Vt[:k_components]) + mean_
return(reconstructed)
def fit_k_sel_components(U,S,Vt,all_connectomes,mean_,sel_components):
'''
Function that reconstruct the matrix "all_connectomes"
starting from the first k_components of the pca
(and decomposition U,S,Vt) passed as input
input: U,S,Vt-> Decomposition obtained from the command svd decomposition of all connectomes
all_connectomes -> 2d numpy array containing all the flattened FC (upper triangular)
that was passed as input for the pca._fit(all_connectomes)
mean_ -> mean of all the connectomes across rows (i.e. np.mean(all_connectomes,axis=0))
sel_components -> integer indexes of the components to consider
output: 2D array containing the reconstructed numpy array of "all_connectomes" using only
k_components
'''
UU = U[:, sel_components]
UU =UU* S[sel_components]
reconstructed=np.dot(UU, Vt[sel_components]) + mean_
return(reconstructed)
def compute_Idiff_PCA_one_dataset_from_TC(data,trimming_length=0,comp_metric="pearson_correlation"):
'''
Compute the Idiff values as a function of the number of components
as proposed in the paper by Amico and Goni "The quest for identifiability
in human functional connectomes"
input: data -> dictionary in the form {subjID: fMRI data}
trimming_length -> default 0 (no trimming), if different from 0 then trim
all the recordings to the same length
output: numpy array containing the following rows:
(k_components,Differential Identifiability score (%))
'''
nsubjects=len(data)
list_FCs=computing_FCs_halved(data,trimming_length=trimming_length)
all_connectomes=list_FCs.reshape((nsubjects*2,len(list_FCs[0][0])))
N_total=len(all_connectomes)
mean_ = np.mean(all_connectomes, axis=0)
all_connectomes -=mean_
U, S, Vt= np.linalg.svd(all_connectomes, full_matrices=False)
U, Vt = svd_flip(U, Vt)
# pca = PCA(n_components=N_total)
# U, S, Vt=pca._fit(all_connectomes)
## Doing the PCA reconstruction keeping k components
list_Idiff=np.array([(k_components,
compute_Imat_from_listFCs(fit_kcomponents(U,S,Vt,all_connectomes,mean_,k_components),comp_metric)[1])
for k_components in range(1,len(all_connectomes)+1)])
# #The previous lines are equivalent to these
# list_Idiff=[]
# for k_components in range(1,len(all_connectomes)+1):
# reconstructed_connectomes=fit_kcomponents(pca,U,S,Vt,all_connectomes,k_components)
# _,current_Idiff,_,_=compute_Imat_from_listFCs(reconstructed_connectomes)
# list_Idiff.append([k_components,current_Idiff])
# list_Idiff=np.array(list_Idiff)
return(list_Idiff)
def compute_Idiff_PCA_one_dataset_from_FCs(list_FCs,optimal=False,comp_metric="pearson_correlation"):
'''
Compute the Idiff values as a function of the number of components
as proposed in the paper by Amico and Goni "The quest for identifiability
in human functional connectomes"
input: list_FCs -> a 3D array of size (nsubjects x 2 x nedges) reporting the
flattened upper triangular matrix of functional connectomes
constructed when splitting each fMRI recording in two halves
optimal -> bool value, if optimal is set to True, the function returns
the optimial Identifiability matrix obtained from the PCA
decomposition method
output: numpy array containing the following rows:
(k_components,Differential Identifiability score (%))
if optimal is True, the returns also the optimal Identifiability matrix
'''
nsubjects=len(list_FCs)
all_connectomes=list_FCs.reshape((nsubjects*2,len(list_FCs[0][0])))
N_total=len(all_connectomes)
mean_ = np.mean(all_connectomes, axis=0)
all_connectomes -=mean_
U, S, Vt= np.linalg.svd(all_connectomes, full_matrices=False)
U, Vt = svd_flip(U, Vt)
# pca = PCA(n_components=N_total)
# U, S, Vt=pca._fit(all_connectomes)
## Doing the PCA reconstruction keeping k components
list_Idiff=np.array([(k_components,
compute_Imat_from_listFCs(fit_kcomponents(U,S,Vt,all_connectomes,mean_,k_components),comp_metric)[1])
for k_components in range(1,len(all_connectomes)+1)])
# opt_Imat = None
# if optimal:
# print(list_Idiff)
# ncomp_opt=int(np.argmax(list_Idiff[:,1]))
# ncomp_opt=int(list_Idiff[ncomp_opt,0])
# opt_Imat,_,_,_=compute_Imat_from_listFCs(fit_kcomponents(U,S,Vt,all_connectomes,mean_,ncomp_opt))
opt_Imat = None
ncomp_opt= None
print(optimal, type(optimal))
if isinstance(optimal,int):
ncomp_opt=optimal
elif optimal:
print(list_Idiff)
ncomp_opt=int(np.argmax(list_Idiff[:,1]))
ncomp_opt=int(list_Idiff[ncomp_opt,0])
if optimal != None:
opt_Imat,_,_,_=compute_Imat_from_listFCs(fit_kcomponents(U,S,Vt,all_connectomes,mean_,ncomp_opt),comp_metric)
return(list_Idiff,opt_Imat)
def compute_Idiff_PCA_two_datasets_from_TC(data1,data2,trimming_length=0,comp_metric="pearson_correlation"):
'''
Compute the Idiff values as a function of the number of components
as proposed in the paper by Amico and Goni "The quest for identifiability
in human functional connectomes"
input: data1 -> dictionary in the form {subjID: fMRI data} %% TEST
data2 -> dictionary in the form {subjID: fMRI data} %% RETEST
trimming_length -> default 0 (no trimming), if different from 0 then trim
all the recordings to the same length
output: numpy array containing the following rows:
(k_components,Differential Identifiability score (%))
'''
nsubjects=len(data1)
n_ROIs=np.shape(list(data1.values())[0])[0]
u,v=np.triu_indices(n=n_ROIs,k=1,m=n_ROIs)
FC1_list=computing_FCs(data1,trimming_length)
FC2_list=computing_FCs(data2,trimming_length)
##Analogous command of np.array(list(zip(FC1_list,FC2_list)))) but slightly faster
list_FCs=np.stack((FC1_list,FC2_list), axis=1)
#list_FCs=computing_FCs_halved(data,trimming_length=trimming_length)
all_connectomes=list_FCs.reshape((nsubjects*2,len(list_FCs[0][0])))
N_total=len(all_connectomes)
# pca = PCA(n_components=N_total)
# U, S, Vt=pca._fit(all_connectomes)
mean_ = np.mean(all_connectomes, axis=0)
all_connectomes -=mean_
U, S, Vt= np.linalg.svd(all_connectomes, full_matrices=False)
U, Vt = svd_flip(U, Vt)
## Doing the PCA reconstruction keeping k components
list_Idiff=np.array([(k_components,
compute_Imat_from_listFCs(fit_kcomponents(U,S,Vt,all_connectomes,mean_,k_components),comp_metric)[1])
for k_components in range(1,len(all_connectomes)+1)])
return(list_Idiff)
def Idiff_from_FC_parallel(k_components,comp_metric="pearson_correlation"):
reconstructed_connectome=fit_kcomponents(U,S,Vt,list_connectomes,mean_,k_components)
_,Idiff,_,_=compute_Imat_from_listFCs(reconstructed_connectome,comp_metric)
return(k_components,Idiff)
def create_pca_approach_parallel(all_connectomes,N_total):
global list_connectomes, U, S, Vt,mean_
list_connectomes=all_connectomes
mean_ = np.mean(list_connectomes, axis=0)
list_connectomes -= mean_
U, S, Vt= np.linalg.svd(list_connectomes, full_matrices=False)
U, Vt = svd_flip(U, Vt)
def compute_Idiff_PCA_two_datasets_from_FCs(FC1_list,FC2_list,parallel=None, optimal=False,comp_metric="pearson_correlation"):
'''
Compute the Idiff values as a function of the number of components
as proposed in the paper by Amico and Goni "The quest for identifiability
in human functional connectomes" from two lists of FCs
input: FC1_list -> a 2D array of size (nsubjects x nedges) with flattened FCs for TEST
FC2_list -> a 2D array of size (nsubjects x nedges) with flattened FCs for RETEST
output: numpy array containing the following rows:
(k_components,Differential Identifiability score (%))
'''
nsubjects=len(FC1_list)
##Analogous command of np.array(list(zip(FC1_list,FC2_list)))) but slightly faster
list_FCs=np.stack((FC1_list,FC2_list), axis=1)
all_connectomes=list_FCs.reshape((nsubjects*2,len(FC1_list[0])))
N_total=len(all_connectomes)
varexp = None
if parallel==None:
#Doing the PCA
mean_ = np.mean(all_connectomes, axis=0)
all_connectomes -=mean_
U, S, Vt= np.linalg.svd(all_connectomes, full_matrices=False)
U, Vt = svd_flip(U, Vt)
## Doing the PCA reconstruction keeping k components
## the original here
list_Idiff=np.array([(k_components,
compute_Imat_from_listFCs(fit_kcomponents(U,S,Vt,all_connectomes,mean_,k_components),comp_metric)[1])
for k_components in range(1,len(all_connectomes)+1)])
varexp = (np.diag(S)**2 / np.sum(np.diag(S)**2))
else:
###Parallel processing is slower than sequential (due to to )
results=[]
pool = Pool(processes=parallel, initializer=create_pca_approach_parallel,
initargs=(all_connectomes, N_total))
for k_components in range(1,len(all_connectomes)+1):
results.append(pool.apply_async(Idiff_from_FC_parallel,args=(k_components,)))
pool.close()
pool.join()
list_Idiff=np.array(sorted([list(i.get()) for i in results],key=lambda x: x[0],reverse=False))
opt_Imat = None
if isinstance(optimal,int):
ncomp_opt=optimal
elif optimal==True:
ncomp_opt=int(np.argmax(list_Idiff[:,1]))
ncomp_opt=int(list_Idiff[ncomp_opt,0])
if optimal != None:
opt_Imat,_,_,_=compute_Imat_from_listFCs(fit_kcomponents(U,S,Vt,all_connectomes,mean_,ncomp_opt),comp_metric)
return (list_Idiff), varexp, opt_Imat
def plot_classic_Imat_and_PCAcurve_concat(data1,data2,data,trimming_length=0,vmin=-0.2,vmax=1,condition='Rest',parallel=None,
ticks_width=1.5, axis_width=2.5, labelsizereg=16,comp_metric="pearson_correlation"):
'''
Function for brain+spine appending the 2 matrixes discarding the between connections
'''
FC_list=computing_FCs_halved_concat(data1,data2,data,trimming_length=trimming_length)
I_mat,Idiff,Iself,Iothers=compute_Imat_from_single_session_FCs(FC_list,comp_metric)
cohend = cohen_d(I_mat.diagonal(),I_mat[~np.eye(I_mat.shape[0],dtype=bool)])
fig = plt.figure(dpi=150,figsize=(12,6))
ax= plt.subplot(121)
im=plt.imshow(I_mat,vmin=vmin,vmax=vmax)
cbar=plt.colorbar(im,fraction=0.046, pad=0.04)
plt.xlabel('Subjects Test (day 1)')
plt.ylabel('Subjects Retest (day 2)')
cbar.set_label(r"Pearson's $\rho$", rotation=90,labelpad=-5)
num_title=list([np.round(i,2) for i in [Idiff,cohend,Iself,Iothers]])
plt.title('%s \n Idiff: %.2f %%, Cohen D: %.2f\nIself: %.2f, Iothers: %.2f' %(condition,num_title[0],num_title[1],num_title[2],num_title[3]));
plot_adjustments(ax,ticks_width=ticks_width, axis_width=axis_width, labelsizereg=labelsizereg)
ax.spines["top"].set_visible(True)
ax.spines["right"].set_visible(True)
ax= plt.subplot(122)
PCA_list_idiff=compute_Idiff_PCA_one_dataset_from_FCs(FC_list)
ax.plot(PCA_list_idiff[:,0],PCA_list_idiff[:,1],'ro-')
asp = np.diff(ax.get_xlim())[0] / np.diff(ax.get_ylim())[0]
ax.set_aspect(asp)
plt.xlabel('Number of PCA components')
plt.ylabel(r'$I_{diff}$ (%)',fontsize=14,labelpad=-1)
plt.subplots_adjust(wspace=0.35)
plot_adjustments(ax,ticks_width=ticks_width, axis_width=axis_width, labelsizereg=labelsizereg)
return(I_mat,PCA_list_idiff)
def plot_classic_Imat_and_PCAcurve(data_test,data_retest=None,trimming_length=0,vmin=-0.2,vmax=1,condition='Rest',parallel=None,
ticks_width=1.5, axis_width=2.5,labelsizereg=16,
metric="pairwise_correlation",optimal=False, inter_bet_con=None,W=1,comp_metric="pearson_correlation"):
'''
Function that plot Imat and the Idiff values as a function of the number of components
as proposed in the paper by Amico and Goni "The quest for identifiability
in human functional connectomes".
If only one dataset is present, then do the procedure when splitting the recordings in half
input: data1 -> dictionary in the form {subjID: fMRI data} %% TEST
data2 -> dictionary in the form {subjID: fMRI data} %% RETEST
trimming_length -> default 0 (no trimming), if different from 0 th
'''
if data_retest==None:
FC_list=computing_FCs_halved(data_test,trimming_length=trimming_length,metric=metric)
I_mat,Idiff,Iself,Iothers=compute_Imat_from_single_session_FCs(FC_list,comp_metric)
else:
FC1_list=computing_FCs(data_test,trimming_length=trimming_length,metric=metric,inter_bet_con=inter_bet_con, W=W)
FC2_list=computing_FCs(data_retest,trimming_length=trimming_length,metric=metric,inter_bet_con=inter_bet_con, W=W)
I_mat,Idiff,Iself,Iothers=compute_Imat_from_FCs_twodatasets(FC1_list,FC2_list,comp_metric)
cohend = cohen_d(I_mat.diagonal(),I_mat[~np.eye(I_mat.shape[0],dtype=bool)])
fig = plt.figure(dpi=150,figsize=(12,6))
ax= plt.subplot(121)
im=plt.imshow(I_mat,vmin=vmin,vmax=vmax)
cbar=plt.colorbar(im,fraction=0.046, pad=0.04)
if data_retest==None:
plt.xlabel('Subjects Test (first half)')
plt.ylabel('Subjects Retest (second half)')
else:
plt.xlabel('Subjects Test (day 1)')
plt.ylabel('Subjects Retest (day 2)')
cbar.set_label(r"Pearson's $\rho$", rotation=90,labelpad=-5)
num_title=list([np.round(i,2) for i in [Idiff,cohend,Iself,Iothers]])
plt.title('%s \n Idiff: %.2f %%, Cohen D: %.2f \nIself: %.2f, Iothers: %.2f' %(condition,num_title[0],num_title[1],num_title[2],num_title[3]));
plot_adjustments(ax,ticks_width=ticks_width, axis_width=axis_width, labelsizereg=labelsizereg)
ax.spines["top"].set_visible(True)
ax.spines["right"].set_visible(True)
ax= plt.subplot(122)
varexp = None
if data_retest==None:
PCA_list_idiff,Imat_opt=compute_Idiff_PCA_one_dataset_from_FCs(FC_list,optimal=optimal)
else:
PCA_list_idiff,varexp,Imat_opt=compute_Idiff_PCA_two_datasets_from_FCs(FC1_list,FC2_list,parallel=parallel,optimal=optimal)
ax.plot(PCA_list_idiff[:,0],PCA_list_idiff[:,1],'ro-')
asp = np.diff(ax.get_xlim())[0] / np.diff(ax.get_ylim())[0]
ax.set_aspect(asp)
plt.xlabel('Number of PCA components')
plt.ylabel(r'$I_{diff}$ (%)',fontsize=14,labelpad=-1)
plt.subplots_adjust(wspace=0.35)
plot_adjustments(ax,ticks_width=ticks_width, axis_width=axis_width, labelsizereg=labelsizereg)
return(I_mat,PCA_list_idiff,varexp, Imat_opt)
## ICC functions
def ICC(matrix, alpha=0.05, r0=0):
'''Intraclass correlation
matrix is matrix of observations. Each row is an object of measurement and
each column is a judge or measurement.
'1-1' is implement: The degree of absolute agreement among measurements made on
randomly seleted objects. It estimates the correlation of any two
measurements.
ICC is the estimated intraclass correlation. LB and UB are upper
and lower bounds of the ICC with alpha level of significance.
In addition to estimation of ICC, a hypothesis test is performed
with the null hypothesis that ICC = r0. The F value, degrees of
freedom and the corresponding p-value of the this test are reported.
Translated in python from the matlab code of Arash Salarian, 2008
Reference: McGraw, K. O., Wong, S. P., "Forming Inferences About
Some Intraclass Correlation Coefficients", Psychological Methods,
Vol. 1, No. 1, pp. 30-46, 1996'''
M = np.array(matrix)
n, k = np.shape(M)
SStotal = np.var(M.flatten(), ddof=1) * (n * k - 1)
MSR = np.var(np.mean(M, 1), ddof=1) * k
MSW = np.sum(np.var(M, 1, ddof=1)) / n
MSC = np.var(np.mean(M, 0), ddof=1) * n
MSE = (SStotal - MSR * (n - 1) - MSC * (k - 1)) / ((n - 1) * (k - 1))
r = (MSR - MSW) / (MSR + (k - 1) * MSW)
F = (MSR / MSW) * (1 - r0) / (1 + (k - 1) * r0)
df1 = n - 1
df2 = n * (k - 1)
p = 1 - stats.f.cdf(F, df1, df2)
FL = (MSR / MSW) * (stats.f.isf(1 - alpha / 2, n * (k - 1), n - 1))
FU = (MSR / MSW) / (stats.f.isf(1 - alpha / 2, n - 1, n * (k - 1)))
LB = (FL - 1) / (FL + (k - 1))
UB = (FU - 1) / (FU + (k - 1))
return (r, LB, UB, F, df1, df2, p)
def compute_ICC_parallel(i,mat):
return(i,ICC(mat)[0])
def compute_ICC_idenfiability_from_FCs(FC_lists,parallel=None):
'''
Compute the ICC matrix '1-1' considering the list of flattened functional connectomes