-
Notifications
You must be signed in to change notification settings - Fork 1
/
visualize_brain.py
executable file
·254 lines (250 loc) · 14.8 KB
/
visualize_brain.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
#!/usr/bin/env python3
'''
Multiple Linear regression to obtain weights for prediction
inputs: semantic features vectors for nine participants
P1-P9
output: cvi activation of voxel v for intermediate semantic feature i
(1-25) sensor-motor verbs
'''
import scipy.io
from sklearn import linear_model
from scipy.stats.stats import pearsonr
import numpy as np
from scipy import spatial
import matplotlib
import random
import numpy as np
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
import nibabel as nib
import os
from nilearn import plotting
if __name__ == '__main__':
###################################################################
######Load semantic features and handle execution requests#########
###################################################################
# skip_or_cooc='sem_feat.txt'
# if skip_or_cooc=='sem_feat_skipgram.txt':
# latent_dims=300
# elif skip_or_cooc=='sem_feat.txt':
# latent_dims=25
# sem_feat=np.loadtxt(skip_or_cooc,dtype=float)
# noun=np.loadtxt('noun.txt',dtype=bytes).astype(str)
#########################################################################
#Start computations for every participant(1-9) for every test pair(1770)#
#########################################################################
#########################################################################[ 2.27613676e-01 1.10164693e-01 1.46301869e-01 1.49869319e-01
# car=[1.62572267e-01 , 1.30988949e-01 ,1.28119447e-01 ,1.07308127e-01,
# 7.87620675e-02 ,-3.58884272e-03,, -4.82448384e-02 , 1.04316102e-01,
# 1.46301898e-01 , 9.20843698e-02 , 5.41449311e-02 ,1.67669167e-01,
# 5.83333323e-02 , 7.83012762e-02 , 9.33161485e-02 ,7.19833265e-02,
# 5.81259217e-02 ,1.13846146e-01 , 1.86100729e-01 ,1.11291494e-01,
# -3.39585254e-02 ,7.74728130e-02 , 2.50476066e-02 ,-3.96716707e-02,
# 1.12786831e-01 ,9.80347572e-02 , 1.95644163e-02 , 6.83562573e-02,
# -3.32130558e-02 ,5.60019003e-02 ,-1.58310660e-02 , 1.03524671e-01,
# 9.08043254e-02 ,-8.40872153e-02 ,-8.05581310e-02 ,-2.06739522e-02,
# 4.86046854e-02 ,-1.66826372e-02 ,-1.72042523e-01 , 5.02131204e-02,
# 1.56920373e-01 , 1.06892474e-01 ,5.39970033e-03 ,-5.26572680e-02,
# -7.89533885e-02 , 1.09079116e-01 ,-3.25494428e-02 ,-9.41311948e-02,
# 8.44137321e-02 , 5.16159072e-02 , 7.38679818e-02 , 8.68928287e-02,
# -2.50802697e-02 , 6.91854933e-02 , 5.95872546e-03 , 8.59341799e-02,
# 5.78556140e-02 ,-8.68557706e-02 , 9.01660330e-02 ,-2.79358134e-02,
# -1.43223697e-02 , 8.12883120e-02 ,-1.91459210e-02 ,-1.70034174e-02,
# 4.97939111e-02 , 6.38224586e-02 ,-4.14797527e-02 , 3.94172530e-02,
# -1.94356073e-02 , 6.86661214e-02 ,-8.11381288e-02 ,-3.37696210e-02,
# 7.63289405e-02 ,-3.73852206e-04 , 1.94643722e-02 , 4.58756712e-02,
# 2.11009446e-02 , 7.98363047e-02 , 6.72865573e-02 , 1.44753882e-02,
# -8.05049169e-02 ,-1.58435084e-01 ,-1.81316563e-02 , 1.96867632e-02,
# 1.34842670e-03 ,-3.08824168e-02 ,-8.29458806e-02 ,8.89975291e-03,
# 4.31789954e-02 ,-2.01392594e-02 , 8.41783550e-03 ,2.83166808e-03,
# -2.67077317e-02 , 3.87452319e-02 , 6.23554736e-02 ,-8.82264940e-02,
# -3.89482451e-02 ,-3.05857248e-02 ,-7.17859994e-02 ,-2.38459062e-01,
# -1.01146502e-03 , 1.13631570e-02 ,-2.41506522e-03 ,-9.32821340e-03,
# 3.69298425e-02 ,-9.94943625e-04 ,-5.22338971e-02 ,-9.32938529e-03,
# 6.77080412e-02 ,-2.93903248e-02 , 2.41397884e-02 , 1.99760616e-02,
# -6.72433986e-02 , 7.33656025e-02 ,-6.23434056e-03 ,-4.23639935e-02,
# 8.60028576e-02 , 1.52405201e-01 ,-4.43237722e-02 ,-1.15466156e-01,
# -6.35482868e-02 ,-9.78964135e-03 ,-3.78326719e-02 ,-1.89899233e-02,
# 5.09163832e-02 , 1.82108872e-01 , 2.69681962e-03 ,-3.46774842e-02,
# 3.84769696e-02 ,-5.18558680e-02 ,-1.78687648e-02 , 1.33553748e-02,
# 7.16792978e-02 ,-2.43528981e-02 ,-1.37007206e-02 ,-2.98034466e-02,
# 6.12513669e-02 , 5.08210379e-02 ,-1.10393589e-01 ,-1.06153687e-02,
# 5.56643458e-02 ,-3.95511395e-02 , 9.44676784e-02 , 8.01437075e-03,
# -3.55878735e-02 ,-1.09588290e-02 ,3.31943027e-02 ,2.95556260e-02,
# -9.05545019e-02 ,-3.89806034e-02 , 4.28863050e-03 ,-3.01421300e-02,
# -2.04345051e-02 , 2.13335730e-02 ,-1.24712943e-01 , 7.08868529e-03,
# 1.51447266e-02 ,-1.36689400e-02 ,-6.55082185e-02 , 1.07179637e-01,
# 7.73744325e-02 ,-4.61564003e-02 , 3.95375718e-03 , 1.83235326e-02,
# -8.15396485e-02 ,-5.50842561e-02 ,-1.10698072e-01 , 7.44480183e-02,
# -5.32559920e-02 , 7.50351128e-02 , 5.92799238e-02 , 6.81917781e-02,
# -4.71438754e-02 ,-1.39439779e-01 ,-4.91871149e-02 , 7.27626340e-02,
# 4.42884715e-02 ,-1.91493196e-01 , 2.25593233e-02 ,-3.08856008e-02,
# -1.11740366e-01 , 6.57864402e-02 ,-4.87202075e-02 , 4.50911896e-03,
# -3.60394820e-02 ,-9.23354426e-03 ,-3.14427983e-02 ,-4.59797442e-02,
# 1.62424763e-02 , 1.75850052e-01 , 9.50931982e-03 , 1.32727162e-01,
# 2.07693936e-02 , 4.43225603e-02 ,-2.98915132e-02 ,-3.90063850e-02,
# -2.27855587e-01 , 1.16098778e-01 , 6.38420169e-02 ,-2.01238423e-02,
# 8.11336360e-02 ,-2.90218113e-02 , 6.50367325e-02 ,-4.18093865e-02,
# -1.02578978e-02 , 3.93641365e-02 , 9.86422848e-02 , 8.69173372e-02,
# -1.46409424e-02 , 1.14828398e-01 ,-5.55168772e-02 , 9.21041332e-03,
# 6.11599507e-02 ,-7.84495675e-02 , 1.71582085e-01 ,-4.12255464e-02,
# 4.79191164e-02 ,-6.71266147e-02 ,-6.20666491e-02 ,-2.94871741e-05,
# -3.31263682e-03 ,-8.63463441e-03 , 3.20781527e-02 ,-3.12099197e-02,
# -2.69307480e-02 ,-6.74964095e-03 , 6.42874312e-02 ,-4.03776629e-02,
# 9.05978942e-02 ,-7.46198856e-02 ,-1.77783942e-02 , 8.98397944e-02,
# -2.00789845e-02 ,-6.71363617e-02 ,-5.99214262e-02 ,-1.08152818e-01,
# -1.27297990e-01 , 7.50189651e-03 , 3.95694256e-02 ,-1.09263431e-01,
# -4.99565281e-02 ,-1.62965068e-02 ,-3.83429390e-02 ,-6.71884084e-03,
# -7.37022725e-04 ,-1.25063304e-02]
# motor=[ 5.13343328e-01 , 3.78558823e-01 , 4.91808958e-01 ,4.19549369e-01,
# 4.89229805e-01 , 3.01206929e-01 , 3.29190259e-01 ,2.57797955e-02,
# 2.85427833e-02 ,-2.33707082e-02 , 6.53391948e-02 ,-6.47219590e-02,
# 4.15378917e-01 , 3.95620736e-03 ,-5.32577201e-02 , 1.79481003e-01,
# 2.88273555e-01 , 2.44602492e-01 , 1.38759181e-02 , 2.33167358e-02,
# 1.87367227e-01 ,-2.98242021e-02 , 1.04863858e-02 , 2.54486245e-02,
# -5.96184821e-02 , 2.62736268e-01 , 1.12709249e-02 , 3.78039112e-03,
# 1.36043661e-01 ,-4.73772454e-02 ,-5.18151383e-03 , 2.68496287e-01,
# -5.70690510e-02 , 2.07540318e-01 , 1.04128643e-02 , 3.60451336e-01,
# 3.91353927e-02 ,-1.39446287e-01 ,-1.73239151e-02 ,1.17319368e-01,
# 7.12441361e-02 ,-2.92192193e-02 , 5.33452234e-02 ,-2.64309900e-02,
# 2.88102787e-01 , 2.24929496e-01 ,-2.81619548e-02 ,2.55184675e-02,
# 9.81844146e-02 , 3.03656725e-01 ,-9.59676208e-04 ,7.57641197e-02,
# 1.12042514e-01 , 9.78183443e-04 , 2.88752938e-01 ,2.25142238e-01,
# -1.23290227e-02 , 2.83099084e-02 , 6.46628530e-02 ,-1.38728666e-02,
# 4.88236284e-02 , 2.88234910e-03 ,-5.55873313e-02 ,7.05058075e-02,
# -2.34284319e-02 ,-3.32623691e-02 , 1.09438650e-02 ,1.52727492e-03,
# -1.18045906e-02 , 1.97138520e-01 , 1.70851460e-02 ,1.16137230e-01,
# 5.51199100e-02 , 1.07209789e-01 ,-1.13442717e-01 ,3.41793212e-02,
# 2.51949264e-02 , 3.70735018e-03 , 8.39660405e-02 ,2.38327492e-02,
# 3.04605161e-02 ,-7.72607928e-02 , 1.71939553e-01 ,1.48368648e-01,
# -7.42088598e-02 , 3.97099858e-01 , 3.18187534e-02 ,2.09069837e-01,
# 9.33061418e-02 ,-2.26580960e-02 , 4.80756304e-02 ,4.29852268e-02,
# 2.27783982e-02 , 6.28585392e-02 , 3.26168342e-02 ,1.52608981e-02,
# 4.57874514e-03 , 2.23598803e-01 ,-9.45703065e-03 ,4.61115036e-02,
# 9.14549757e-02 , 1.07941069e-01 ,-2.43770145e-02 ,3.62310391e-01,
# 2.54608102e-02 , 7.44065095e-02 , 3.49306390e-02 ,2.29531925e-02,
# 8.95319926e-02 , 9.32347857e-02 , 2.48123427e-02 ,9.84043613e-02,
# 2.84645747e-02 , 1.19227838e-01 ,-1.80459327e-02 , 1.28635314e-01,
# 1.13677157e-01 , 2.93154137e-01 , 8.87084345e-03 ,-6.55245775e-02,
# 4.65438293e-02 , 2.33425559e-01 ,-3.69404382e-02 , 5.84703187e-02,
# -3.50067940e-02 , 7.51804295e-02 , 1.28807347e-01 ,-6.30063586e-02,
# 5.99651131e-02 ,-1.61177641e-01 , 5.00195234e-02 ,-3.18692268e-02,
# 4.73563513e-02 , 6.01540956e-03 , 5.80177063e-02 ,2.37068016e-02,
# 1.39071799e-02 ,-6.94982400e-03 , 1.29553466e-01 , 3.71629009e-03,
# -1.95731921e-02 , 8.22525574e-02 ,-7.22955495e-02 , 4.15622498e-02,
# 7.06733753e-02 , 2.74605822e-02 , 2.51887576e-01 , 4.39997508e-02,
# 4.48229326e-02 , 7.81576393e-02 , 2.88501581e-02 , 2.17101857e-01,
# 5.96889424e-02 ,-1.35702909e-02 , 6.25409462e-02 ,-2.28317871e-02,
# -2.71712061e-02 , 2.87638522e-02 ,-1.12712810e-01 ,-8.35121452e-02,
# 7.42334799e-02 , 4.80128412e-02 , 8.76736768e-02 , 1.79621184e-01,
# -4.29722051e-02 ,-3.60594446e-02 , 1.39264561e-01 , 1.04846562e-02,
# 3.67509225e-02 , 2.06796825e-02 ,-7.46546979e-02 ,-5.36048145e-02,
# -2.14820106e-02 ,-1.58368251e-01 ,-1.36078299e-02 ,-2.97377820e-02,
# -6.10721754e-02 , 1.48056153e-01 ,-7.23794739e-02 ,-2.51214105e-01,
# 8.05017192e-02 , 2.20118339e-01 , 1.82937184e-01 , 7.71953188e-02,
# -7.11945579e-02 ,-3.27558367e-02 ,-3.19841609e-02 , 8.19324489e-02,
# 1.49848289e-02 ,6.84018924e-02 , 2.39092327e-02 , 8.49818193e-02,
# 1.21746393e-01 ,1.92261729e-01 , 7.76214024e-03 ,-6.10580420e-02,
# 3.07571953e-02 ,-2.91833615e-02 , 1.93535216e-01 , 2.32569790e-02,
# 1.85695573e-01 ,-1.42080812e-04 , 2.85771377e-02 ,-1.34171622e-02,
# -1.46903077e-01 , 1.03338172e-01 ,-7.65588999e-02 , 1.51698287e-01,
# 1.10052198e-01 , 6.90087258e-03, -3.73712725e-02 , 1.25058660e-02,
# 2.50596219e-02 , 3.91891188e-01 , 4.10988262e-02 , 1.46890996e-02,
# 5.17579429e-02 ,-1.67422425e-01 , 1.61101366e-01 ,-9.52191341e-02,
# -2.65532798e-02 ,3.61597434e-03 ,-7.66435889e-02 ,-2.09788625e-02,
# 6.85702930e-03 , 7.06284646e-02 ,-8.11695874e-05 ,5.60461931e-02,
# 3.20110590e-02 , 1.01189407e-02 , 1.12053146e-01 , 1.19834810e-02,
# 4.29201966e-02 ,-6.82263861e-02 ,-4.38730906e-02 , 2.85621958e-03,
# 4.59891203e-02 ,7.37648396e-03 ,-2.97612117e-02 ,-1.08673180e-01,
# 3.32186336e-02 ,2.67038178e-02 , 2.32679584e-01 , 5.79062441e-02,
# -1.08175871e-01 ,-5.31735538e-02 ,-1.50511677e-02 , 1.01618309e-02,
# -3.04253825e-02 ,1.16598490e-02]
stable_voxels=[250]
no_parts=range(1,10)#10
for parts in no_parts:
mat = scipy.io.loadmat('../../data/FMRI/data-science-P'+str(parts)+'.mat')
for index_s,s_v in enumerate(stable_voxels):
acc=0
##############################################################
###############Data Split and merge formatting################
##############################################################
# it goes to 2nd trial and accesses i'th voxel
# trials are 60 concrete nouns*6 times=360
# extract data and noun for that data from .mat file
length=len(mat['data'][0].item()[0])
#trial data are 6x60=360-2x6=348(test words excluded)
fmri_data_for_trial=np.zeros((360,length))
fmri_data_raw=np.zeros((360,length))
noun_for_trial=[]
test_data1=np.zeros((6,length))
test_data2=np.zeros((6,length))
k=0
j=0
colToCoord=np.zeros((length,3))
coordToCol=np.zeros((mat['meta']['dimx'][0][0][0][0],mat['meta']['dimy'][0][0][0][0],mat['meta']['dimz'][0][0][0][0]))
colToCoord=mat['meta']['colToCoord'][0][0]
coordToCol=mat['meta']['coordToCol'][0][0]
axis_x=mat['meta']['dimx'][0][0][0][0]
axis_y=mat['meta']['dimy'][0][0][0][0]
axis_z=mat['meta']['dimz'][0][0][0][0]
for x in range (0,360):
k+=1
fmri_data_for_trial[j,:]=mat['data'][x][0][0]
noun_for_trial=noun_for_trial+[mat['info']['word'][0][x][0]]
j+=1
k=0
tempo=np.zeros((60,6),dtype=int)
for x in set(noun_for_trial):
tempo[k,:]=[i for i, j in enumerate(noun_for_trial) if j == x]
k+=1
########################################################################
#################Voxel Stability Selection Starts#######################
########################################################################
stab_vox=np.loadtxt('../stable_voxels/st_vox'+str(parts)+'_'+str(s_v)+'.txt',dtype=int)
#################################################################
########Data preproccesing and mean normalization################
#################################################################
voxs = mat['meta']['nvoxels'][0][0][0][0]
fmri_data_proc = np.zeros((60,voxs))
fmri_data_final = np.zeros((60,voxs))
nouns_map = []
for x in range(len(tempo)):
fmri_data_proc[x,:] = fmri_data_for_trial[tempo[x,0],:]+fmri_data_for_trial[tempo[x,1],:] \
+fmri_data_for_trial[tempo[x,3],:]+fmri_data_for_trial[tempo[x,2],:]+ \
fmri_data_for_trial[tempo[x,4],:]+fmri_data_for_trial[tempo[x,5],:]
fmri_data_proc[x,:] /= 6
print(noun_for_trial[tempo[x,0]])
nouns_map.append( noun_for_trial[tempo[x,0]])
mean_data = np.sum(fmri_data_proc,axis=0)
mean_data /= 60
fmri_data_final = np.zeros((60,voxs))
mean_data = np.tile(mean_data,(60,1))
fmri_data_final = fmri_data_proc-mean_data
# Make this bigger to generate a dense grid.
# Create some random data.
# Create the x, y, and z coordinate arrays. We use
# numpy's broadcasting to do all the hard work for us.
# We could shorten this even more by using np.meshgrid.
vol_brain = np.zeros((axis_x,axis_y,axis_z))
print(int(voxs))
for i in range(60):
#for idx,vox in enumerate(int(voxs)):
for _,vox in enumerate(stab_vox):
vol_brain[colToCoord[vox,0],colToCoord[vox,1],colToCoord[vox,2]] = fmri_data_final[i,vox]
affine=np.array([[-3.125 ,0, 0 , 81.250],
[0 ,3.125 ,0 ,-115.625],
[0 ,0 ,6 , -54.000],
[0 , 0 ,0 , 1.000]])
#affine=affine[0:3].T
if i == nouns_map.index('car') or i == nouns_map.index('truck'):
array_img = nib.Nifti1Image(vol_brain,affine)
plotting.plot_stat_map(array_img,title=nouns_map[i])
plotting.show()
plotting.plot_glass_brain(array_img,title=nouns_map[i])
plotting.show()
# img = nib.Nifti1Image(vol_brain, np.eye(4))
print(array_img.header.get_zooms())
#nib.save(array_img, os.path.join('imgfiles','test.nii.gz'))
# print(la)