-
Notifications
You must be signed in to change notification settings - Fork 0
/
06_fit.py
154 lines (122 loc) · 6.89 KB
/
06_fit.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
# LEiDA(Cabral 2017. Sci Rep.)-PART6: fit for sFC
# author: zhangjiaqi(Smile.Z), CASIA, Brainnetome
import numpy as np
from scipy.signal import hilbert
from scipy.spatial.distance import cosine
import math
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
from sklearn.metrics import davies_bouldin_score
import pandas as pd
from sklearn.decomposition import PCA
import os
from validclust import ValidClust
from mpl_toolkits.mplot3d import Axes3D
import itertools
from scipy import stats
from scipy.stats import ttest_ind
sns.set()
# p: weights for each V1
# V1: leading eigenvector
# sFC: static FC(average FC)
def Fit(p, V1, sFC):
FC = np.zeros((246, 246))
for i in range(len(p)):
FC += p[i] * np.dot(np.matrix(V1[i]).T, np.matrix(V1[i]))
inds = np.triu_indices(246, 1)
S = FC[inds]
F = sFC[inds]
return stats.pearsonr(S, F)[0], stats.pearsonr(S, F)[1]
if __name__ =='__main__':
f = open('mdd1.txt', 'r')
mdd1 = f.readlines()
f.close()
f = open('mdd2.txt', 'r')
mdd2 = f.readlines()
f.close()
f = open('hc.txt', 'r')
hc = f.readlines()
f.close()
for i in range(len(mdd1)):
sFC = np.loadtxt('/share/home/zhangjiaqi/2022Project/HOPF/00_FMRI/xinxiang/FC/'+mdd1[i].replace('\n', '')+'.txt')
os.makedirs('/share/home/zhangjiaqi/2022Project/HOPF/02_LEiDA_Empircal/step6_individual_analysis/Fit/'+mdd1[i].replace('\n', ''))
for K in range(2, 21):
fit = []
pvalue = []
# use (mdd1+mdd2+hc) V1 to fit subject FC
path = '/share/home/zhangjiaqi/2022Project/HOPF/02_LEiDA_Empircal/step2_emp_kmeans/'+str(K)+'/centroids_'+str(K)+'_cluster.txt'
V1 = np.loadtxt(path)
path = '/share/home/zhangjiaqi/2022Project/HOPF/02_LEiDA_Empircal/step3_index/subject/'+mdd1[i].replace('\n', '')+'/'+str(K)+'/FO.txt'
p = list(np.loadtxt(path))
f, p = Fit(p, V1, sFC)
fit.append(f)
pvalue.append(p)
# use subject V1 to fit subject FC
path = '/share/home/zhangjiaqi/2022Project/HOPF/02_LEiDA_Empircal/step6_individual_analysis/V1/subject/'+mdd1[i].replace('\n', '')+'/'+str(K)+'/centroids_'+str(K)+'_cluster.txt'
V1 = np.loadtxt(path)
path = '/share/home/zhangjiaqi/2022Project/HOPF/02_LEiDA_Empircal/step6_individual_analysis/V1/subject/'+mdd1[i].replace('\n', '')+'/'+str(K)+'/centroids_'+str(K)+'_count.txt'
count = np.loadtxt(path)
p = []
for j in range(K):
p.append(count[j]/np.sum(count))
f, p = Fit(p, V1, sFC)
fit.append(f)
pvalue.append(p)
np.savetxt('/share/home/zhangjiaqi/2022Project/HOPF/02_LEiDA_Empircal/step6_individual_analysis/Fit/'+mdd1[i].replace('\n', '')+'/'+str(K)+'_cluster_fit.txt', fit, delimiter=' ')
np.savetxt('/share/home/zhangjiaqi/2022Project/HOPF/02_LEiDA_Empircal/step6_individual_analysis/Fit/'+mdd1[i].replace('\n', '')+'/'+str(K)+'_cluster_p.txt', pvalue, delimiter=' ')
for i in range(len(mdd2)):
sFC = np.loadtxt('/share/home/zhangjiaqi/2022Project/HOPF/00_FMRI/xinxiang/FC/'+mdd2[i].replace('\n', '')+'.txt')
os.makedirs('/share/home/zhangjiaqi/2022Project/HOPF/02_LEiDA_Empircal/step6_individual_analysis/Fit/'+mdd2[i].replace('\n', ''))
for K in range(2, 21):
fit = []
pvalue = []
# use (mdd1+mdd2+hc) V1 to fit subject FC
path = '/share/home/zhangjiaqi/2022Project/HOPF/02_LEiDA_Empircal/step2_emp_kmeans/'+str(K)+'/centroids_'+str(K)+'_cluster.txt'
V1 = np.loadtxt(path)
path = '/share/home/zhangjiaqi/2022Project/HOPF/02_LEiDA_Empircal/step3_index/subject/'+mdd2[i].replace('\n', '')+'/'+str(K)+'/FO.txt'
p = list(np.loadtxt(path))
f, p = Fit(p, V1, sFC)
fit.append(f)
pvalue.append(p)
# use subject V1 to fit subject FC
path = '/share/home/zhangjiaqi/2022Project/HOPF/02_LEiDA_Empircal/step6_individual_analysis/V1/subject/'+mdd2[i].replace('\n', '')+'/'+str(K)+'/centroids_'+str(K)+'_cluster.txt'
V1 = np.loadtxt(path)
path = '/share/home/zhangjiaqi/2022Project/HOPF/02_LEiDA_Empircal/step6_individual_analysis/V1/subject/'+mdd2[i].replace('\n', '')+'/'+str(K)+'/centroids_'+str(K)+'_count.txt'
count = np.loadtxt(path)
p = []
for j in range(K):
p.append(count[j]/np.sum(count))
f, p = Fit(p, V1, sFC)
fit.append(f)
pvalue.append(p)
np.savetxt('/share/home/zhangjiaqi/2022Project/HOPF/02_LEiDA_Empircal/step6_individual_analysis/Fit/'+mdd2[i].replace('\n', '')+'/'+str(K)+'_cluster_fit.txt', fit, delimiter=' ')
np.savetxt('/share/home/zhangjiaqi/2022Project/HOPF/02_LEiDA_Empircal/step6_individual_analysis/Fit/'+mdd2[i].replace('\n', '')+'/'+str(K)+'_cluster_p.txt', pvalue, delimiter=' ')
for i in range(len(hc)):
sFC = np.loadtxt('/share/home/zhangjiaqi/2022Project/HOPF/00_FMRI/xinxiang/FC/'+hc[i].replace('\n', '')+'.txt')
os.makedirs('/share/home/zhangjiaqi/2022Project/HOPF/02_LEiDA_Empircal/step6_individual_analysis/Fit/'+hc[i].replace('\n', ''))
for K in range(2, 21):
fit = []
pvalue = []
# use (mdd1+mdd2+hc) V1 to fit subject FC
path = '/share/home/zhangjiaqi/2022Project/HOPF/02_LEiDA_Empircal/step2_emp_kmeans/'+str(K)+'/centroids_'+str(K)+'_cluster.txt'
V1 = np.loadtxt(path)
path = '/share/home/zhangjiaqi/2022Project/HOPF/02_LEiDA_Empircal/step3_index/subject/'+hc[i].replace('\n', '')+'/'+str(K)+'/FO.txt'
p = list(np.loadtxt(path))
f, p = Fit(p, V1, sFC)
fit.append(f)
pvalue.append(p)
# use subject V1 to fit subject FC
path = '/share/home/zhangjiaqi/2022Project/HOPF/02_LEiDA_Empircal/step6_individual_analysis/V1/subject/'+hc[i].replace('\n', '')+'/'+str(K)+'/centroids_'+str(K)+'_cluster.txt'
V1 = np.loadtxt(path)
path = '/share/home/zhangjiaqi/2022Project/HOPF/02_LEiDA_Empircal/step6_individual_analysis/V1/subject/'+hc[i].replace('\n', '')+'/'+str(K)+'/centroids_'+str(K)+'_count.txt'
count = np.loadtxt(path)
p = []
for j in range(K):
p.append(count[j]/np.sum(count))
f, p = Fit(p, V1, sFC)
fit.append(f)
pvalue.append(p)
np.savetxt('/share/home/zhangjiaqi/2022Project/HOPF/02_LEiDA_Empircal/step6_individual_analysis/Fit/'+hc[i].replace('\n', '')+'/'+str(K)+'_cluster_fit.txt', fit, delimiter=' ')
np.savetxt('/share/home/zhangjiaqi/2022Project/HOPF/02_LEiDA_Empircal/step6_individual_analysis/Fit/'+hc[i].replace('\n', '')+'/'+str(K)+'_cluster_p.txt', pvalue, delimiter=' ')