Skip to content
This repository was archived by the owner on Jan 27, 2023. It is now read-only.

Commit 4163c8b

Browse files
author
Antonio Ulloa
committed
Wrote scripts to perform parameter exploration in Resting State simulations
1 parent 5a4781b commit 4163c8b

File tree

107 files changed

+42624
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+42624
-0
lines changed

analysis/compare_TVB_998_vs_Emp_FC.py

Lines changed: 446 additions & 0 deletions
Large diffs are not rendered by default.

analysis/compare_TVB_998_vs_Emp_FC.py.old

Lines changed: 435 additions & 0 deletions
Large diffs are not rendered by default.

analysis/compare_TVB_vs_Emp_FC.py

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
# ============================================================================
2+
#
3+
# PUBLIC DOMAIN NOTICE
4+
#
5+
# National Institute on Deafness and Other Communication Disorders
6+
#
7+
# This software/database is a "United States Government Work" under the
8+
# terms of the United States Copyright Act. It was written as part of
9+
# the author's official duties as a United States Government employee and
10+
# thus cannot be copyrighted. This software/database is freely available
11+
# to the public for use. The NIDCD and the U.S. Government have not placed
12+
# any restriction on its use or reproduction.
13+
#
14+
# Although all reasonable efforts have been taken to ensure the accuracy
15+
# and reliability of the software and data, the NIDCD and the U.S. Government
16+
# do not and cannot warrant the performance or results that may be obtained
17+
# by using this software or data. The NIDCD and the U.S. Government disclaim
18+
# all warranties, express or implied, including warranties of performance,
19+
# merchantability or fitness for any particular purpose.
20+
#
21+
# Please cite the author in any work or product based on this material.
22+
#
23+
# ==========================================================================
24+
25+
26+
27+
# ***************************************************************************
28+
#
29+
# Large-Scale Neural Modeling software (LSNM)
30+
#
31+
# Section on Brain Imaging and Modeling
32+
# Voice, Speech and Language Branch
33+
# National Institute on Deafness and Other Communication Disorders
34+
# National Institutes of Health
35+
#
36+
# This file (compare_TVB_vs_Emp_FC.py) was created on March 7 2017.
37+
#
38+
#
39+
# Author: Antonio Ulloa
40+
#
41+
# Last updated by Antonio Ulloa on March 7 2017
42+
#
43+
# **************************************************************************/
44+
#
45+
# compare_TVB_vs_Emp_FC.py
46+
#
47+
# Reads several simulated Resting State BOLD fMRI simulations, computes functional
48+
# connectivity matrices for each simulation, and compares them with an
49+
# empirical functional connectivity (from Sporns and Honey) using a simple
50+
# correlation value (between simulated and empirical FCs).
51+
52+
import numpy as np
53+
import matplotlib.pyplot as plt
54+
55+
import scipy.io
56+
57+
from matplotlib import cm as CM
58+
59+
from scipy.stats import itemfreq
60+
61+
62+
63+
# define name of input file where Hagmann empirical data is stored (matlab file given to us
64+
# by Olaf Sporns and Chris Honey
65+
hagmann_data = 'DSI_release2_2011.mat'
66+
67+
# define the names of the input files where the correlation coefficients were stored
68+
TVB_RS_FC = ['output.RestingState.198_1_0.0242/xcorr_matrix_66_regions.npy',
69+
'output.RestingState.198_5_0.0242/xcorr_matrix_66_regions.npy'
70+
]
71+
72+
# declare ROI labels as ordered in the simulated FC files
73+
labels = [' rLOF',
74+
'rPORB',
75+
' rFP',
76+
' rMOF',
77+
'rPTRI',
78+
'rPOPE',
79+
' rRMF',
80+
' rSF',
81+
' rCMF',
82+
'rPREC',
83+
'rPARC',
84+
' rRAC',
85+
' rCAC',
86+
' rPC',
87+
'rISTC',
88+
'rPSTC',
89+
'rSMAR',
90+
' rSP',
91+
' rIP',
92+
'rPCUN',
93+
' rCUN',
94+
'rPCAL',
95+
'rLOCC',
96+
'rLING',
97+
' rFUS',
98+
'rPARH',
99+
' rENT',
100+
' rTP',
101+
' rIT',
102+
' rMT',
103+
'rBSTS',
104+
' rST',
105+
' rTT',
106+
' lLOF',
107+
'lPORB',
108+
' lFP',
109+
' lMOF',
110+
'lPTRI',
111+
'lPOPE',
112+
' lRMF',
113+
' lSF',
114+
' lCMF',
115+
'lPREC',
116+
'lPARC',
117+
' lRAC',
118+
' lCAC',
119+
' lPC',
120+
'lISTC',
121+
'lPSTC',
122+
'lSMAR',
123+
' lSP',
124+
' lIP',
125+
'lPCUN',
126+
' lCUN',
127+
'lPCAL',
128+
'lLOCC',
129+
'lLING',
130+
' lFUS',
131+
'lPARH',
132+
' lENT',
133+
' lTP',
134+
' lIT',
135+
' lMT',
136+
'lBSTS',
137+
' lST',
138+
' lTT'
139+
]
140+
141+
142+
# open matlab file that contains hagmann empirical data
143+
hagmann_empirical = scipy.io.loadmat(hagmann_data)
144+
145+
# open files that contain functional connectivities
146+
i = 0
147+
tvb_rs_fc = np.zeros([len(TVB_RS_FC), ROIs, ROIs])
148+
for file in TVB_RS_FC:
149+
tvb_rs_fc[i] = np.load(TVB_RS_FC[i])
150+
i = i + 1
151+
152+
# extract total number of ROIs from one of the FC matrices
153+
ROIs = tvb_rs_fc[0].shape[0]
154+
155+
# we need to apply a Fisher Z transformation to the correlation coefficients,
156+
# prior to averaging.
157+
empirical_fc_mat_Z = np.arctanh(hagmann_empirical['COR_fMRI_average'])
158+
159+
# initialize 66x66 matrix
160+
empirical_fc_lowres_Z = np.zeros([ROIs,ROIs])
161+
162+
# subtract 1 from labels array bc numpy arrays start with zero
163+
hagmann_empirical['roi_lbls'][0] = hagmann_empirical['roi_lbls'][0] - 1
164+
165+
# compress 998x998 empirical FC matrix to 66x66 FC matrix by averaging
166+
for i in range(0,998):
167+
for j in range(0,998):
168+
169+
# extract low-res coordinates from hi-res labels matrix
170+
x = hagmann_empirical['roi_lbls'][0][i]
171+
y = hagmann_empirical['roi_lbls'][0][j]
172+
173+
empirical_fc_lowres_Z[x, y] += empirical_fc_mat_Z[i, j]
174+
175+
# count the number of times each lowres label appears in the hires matrix
176+
freq_array = itemfreq(hagmann_empirical['roi_lbls'][0])
177+
178+
# divide each sum by the number of hires ROIs within each lowres ROI
179+
for i in range(0,66):
180+
for j in range(0,66):
181+
total_freq = freq_array[i][1] * freq_array[j][1]
182+
empirical_fc_lowres_Z[i,j] = empirical_fc_lowres_Z[i,j] / total_freq
183+
184+
# now, convert back to from Z to R correlation coefficients
185+
empirical_fc_lowres = np.tanh(empirical_fc_lowres_Z)
186+
187+
# initialize figure to plot simulated FC
188+
fig=plt.figure('Functional Connectivity Matrix of empirical BOLD (66 ROIs)')
189+
ax = fig.add_subplot(111)
190+
cmap = CM.get_cmap('jet', 10)
191+
empirical_fc_lowres = np.asarray(empirical_fc_lowres)
192+
cax = ax.imshow(empirical_fc_lowres, vmin=-0.4, vmax=1.0, interpolation='nearest', cmap=cmap)
193+
ax.grid(False)
194+
color_bar=plt.colorbar(cax)
195+
196+
# simulated RS FC needs to be rearranged prior to scatter plotting
197+
new_TVB_RS_FC = np.zeros([tvb_rs_fc.shape[0], ROIs, ROIs])
198+
for fc in range(0, tvb_rs_fc.shape[0]):
199+
for i in range(0, ROIs):
200+
for j in range(0, ROIs):
201+
202+
# extract labels of current ROI label from simulated labels list of simulated FC
203+
label_i = labels[i]
204+
label_j = labels[j]
205+
# extract index of corresponding ROI label from empirical FC labels list
206+
emp_i = np.where(hagmann_empirical['anat_lbls'] == label_i)[0][0]
207+
emp_j = np.where(hagmann_empirical['anat_lbls'] == label_j)[0][0]
208+
new_TVB_RS_FC[fc, emp_i, emp_j] = tvb_rs_fc[fc, i, j]
209+
210+
# apply mask to get rid of upper triangle, including main diagonal
211+
mask = np.tri(new_TVB_RS_FC.shape[1], k=0)
212+
mask = np.transpose(mask)
213+
214+
# apply mask to empirical FC matrix
215+
empirical_fc_lowres = np.ma.array(empirical_fc_lowres, mask=mask) # mask out upper triangle
216+
217+
# apply mask to all simulated FC matrices
218+
sim_rs_fc = np.ma.zeros([tvb_rs_fc.shape[0], ROIs, ROIs])
219+
for fc in range(0, tvb_rs_fc.shape[0]):
220+
sim_rs_fc[fc] = np.ma.array(new_TVB_RS_FC[fc], mask=mask) # mask out upper triangle
221+
222+
# flatten the empirical FC matriz
223+
corr_mat_emp_FC = np.ma.ravel(empirical_fc_lowres)
224+
corr_mat_emp_FC = np.ma.compressed(corr_mat_emp_FC)
225+
226+
# flatten all simulated FC matrices
227+
print 'Before ravel: ', sim_rs_fc[0].shape
228+
print 'After ravel: ', np.ma.ravel(sim_rs_fc[0]).shape
229+
print 'After compress: ', np.ma.compressed(np.ma.ravel(sim_rs_fc[0])).shape
230+
flat_sim_rs_fc = np.zeros([tvb_rs_fc.shape[0], corr_mat_emp_FC.size])
231+
for fc in range(0, tvb_rs_fc.shape[0]):
232+
# remove masked elements from cross-correlation matrix
233+
flat_sim_rs_fc[fc] = np.ma.compressed(np.ma.ravel(sim_rs_fc[fc]))
234+
235+
# calculate correlation coefficients for empirical FC matrix vs each simulated FC matrix
236+
r = np.zeros(tvb_rs_fc.shape[0])
237+
for fc in range(0, tvb_rs_fc.shape[0]):
238+
r[fc] = np.corrcoef(corr_mat_emp_FC, flat_sim_rs_fc[fc])[1,0]
239+
240+
# print all correlations:
241+
print 'Correlations empirical vs simulated: '
242+
print r
243+
244+
# calculates the index of the maximum correlation value
245+
max_r = np.argmax(r)
246+
print ' Best correlation found was: ', r[max_r], ', element number ', max_r
247+
248+
# plot the simulated FC with the highest correlation coefficient
249+
fig=plt.figure('Functional Connectivity Matrix of simulated BOLD (66 ROIs)')
250+
ax = fig.add_subplot(111)
251+
cmap = CM.get_cmap('jet', 10)
252+
cax = ax.imshow(new_TVB_RS_FC[max_r], vmin=-0.4, vmax=1.0, interpolation='nearest', cmap=cmap)
253+
ax.grid(False)
254+
color_bar=plt.colorbar(cax)
255+
256+
# scatter plot of empirical FC vs best matched simulated FC
257+
# initialize figure to plot correlations between empirical and simulated FC
258+
fig=plt.figure('Empirical vs Simulated FC')
259+
plt.scatter(corr_mat_emp_FC, flat_sim_rs_fc[max_r])
260+
plt.xlabel('Empirical FC')
261+
plt.ylabel('Model FC')
262+
263+
# fit scatter plot with np.polyfit
264+
m, b = np.polyfit(corr_mat_emp_FC, flat_sim_rs_fc[max_r], 1)
265+
plt.plot(corr_mat_emp_FC, m*corr_mat_emp_FC + b, '-', color='red')
266+
267+
# calculate correlation coefficient and display it on plot
268+
cc = np.corrcoef(corr_mat_emp_FC, flat_sim_rs_fc[max_r])[1,0]
269+
plt.text(0.5, 0.3, 'r=' + '{:.2f}'.format(cc))
270+
271+
272+
# finally, show all figures!
273+
plt.show()

0 commit comments

Comments
 (0)