|
| 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 (avg_FC_diffs_across_subjs.py) was created on December 3, 2016. |
| 37 | +# |
| 38 | +# |
| 39 | +# Author: Antonio Ulloa |
| 40 | +# |
| 41 | +# Last updated by Antonio Ulloa on December 3 2016 |
| 42 | +# |
| 43 | +# **************************************************************************/ |
| 44 | +# |
| 45 | +# avg_FC_diffs_across_subjs.py |
| 46 | +# |
| 47 | +# Reads functional connectivity differences from input files corresponding to |
| 48 | +# difference subjects and it calculates an average, after which it displays |
| 49 | +# a average in matrix form as well as a histogram. We also |
| 50 | +# calculate kurtosis and skewness of the histogram. |
| 51 | + |
| 52 | + |
| 53 | +import numpy as np |
| 54 | +import matplotlib.pyplot as plt |
| 55 | + |
| 56 | +from scipy.stats import kurtosis |
| 57 | + |
| 58 | +from scipy.stats import skew |
| 59 | + |
| 60 | +from matplotlib import cm as CM |
| 61 | + |
| 62 | +# declare ROI labels |
| 63 | +labels = ['rLOF', |
| 64 | + 'rPORB', |
| 65 | + 'rFP' , |
| 66 | + 'rMOF' , |
| 67 | + 'rPTRI', |
| 68 | + 'rPOPE', |
| 69 | + 'rRMF' , |
| 70 | + 'rSF' , |
| 71 | + 'rCMF' , |
| 72 | + 'rPREC', |
| 73 | + 'rPARC', |
| 74 | + 'rRAC' , |
| 75 | + 'rCAC' , |
| 76 | + 'rPC' , |
| 77 | + 'rISTC', |
| 78 | + 'rPSTC', |
| 79 | + 'rSMAR', |
| 80 | + 'rSP' , |
| 81 | + 'rIP' , |
| 82 | + 'rPCUN', |
| 83 | + 'rCUN' , |
| 84 | + 'rPCAL', |
| 85 | + 'rLOCC', |
| 86 | + 'rLING', |
| 87 | + 'rFUS' , |
| 88 | + 'rPARH', |
| 89 | + 'rENT' , |
| 90 | + 'rTP' , |
| 91 | + 'rIT' , |
| 92 | + 'rMT' , |
| 93 | + 'rBSTS', |
| 94 | + 'rST' , |
| 95 | + 'rTT'] |
| 96 | + |
| 97 | + |
| 98 | +# define the names of the input files where the correlation coefficients were stored |
| 99 | +FC_diff_subj1 = 'subject_11/xcorr_diffs_TB_minus_RS.npy' |
| 100 | +FC_diff_subj2 = 'subject_12/xcorr_diffs_TB_minus_RS.npy' |
| 101 | +FC_diff_subj3 = 'subject_13/xcorr_diffs_TB_minus_RS.npy' |
| 102 | +FC_diff_subj4 = 'subject_14/xcorr_diffs_TB_minus_RS.npy' |
| 103 | +FC_diff_subj5 = 'subject_15/xcorr_diffs_TB_minus_RS.npy' |
| 104 | +FC_diff_subj6 = 'subject_16/xcorr_diffs_TB_minus_RS.npy' |
| 105 | +FC_diff_subj7 = 'subject_17/xcorr_diffs_TB_minus_RS.npy' |
| 106 | +FC_diff_subj8 = 'subject_18/xcorr_diffs_TB_minus_RS.npy' |
| 107 | +FC_diff_subj9 = 'subject_19/xcorr_diffs_TB_minus_RS.npy' |
| 108 | +FC_diff_subj10 = 'subject_20/xcorr_diffs_TB_minus_RS.npy' |
| 109 | + |
| 110 | +# define the names of the average fucntional connectivies are stored (for both |
| 111 | +# task-based and resting state) |
| 112 | +xcorr_rs_avg_file = 'rs_fc_avg.npy' |
| 113 | +xcorr_tb_avg_file = 'tb_fc_avg.npy' |
| 114 | + |
| 115 | +# open files that contain correlation coefficients |
| 116 | +fc_diff_subj1 = np.load(FC_diff_subj1) |
| 117 | +fc_diff_subj2 = np.load(FC_diff_subj2) |
| 118 | +fc_diff_subj3 = np.load(FC_diff_subj3) |
| 119 | +fc_diff_subj4 = np.load(FC_diff_subj4) |
| 120 | +fc_diff_subj5 = np.load(FC_diff_subj5) |
| 121 | +fc_diff_subj6 = np.load(FC_diff_subj6) |
| 122 | +fc_diff_subj7 = np.load(FC_diff_subj7) |
| 123 | +fc_diff_subj8 = np.load(FC_diff_subj8) |
| 124 | +fc_diff_subj9 = np.load(FC_diff_subj9) |
| 125 | +fc_diff_subj10 = np.load(FC_diff_subj10) |
| 126 | + |
| 127 | +# open files that contain functional connectivity averages |
| 128 | +xcorr_rs_avg = np.load(xcorr_rs_avg_file) |
| 129 | +xcorr_tb_avg = np.load(xcorr_tb_avg_file) |
| 130 | + |
| 131 | +# construct numpy array containing functional connectivity arrays |
| 132 | +fc_diff = np.array([fc_diff_subj1, fc_diff_subj2, fc_diff_subj3, |
| 133 | + fc_diff_subj4, fc_diff_subj5, fc_diff_subj6, |
| 134 | + fc_diff_subj7, fc_diff_subj8, fc_diff_subj9, |
| 135 | + fc_diff_subj10 ]) |
| 136 | + |
| 137 | +# now, we need to apply a Fisher Z transformation to the correlation coefficients, |
| 138 | +# prior to averaging. |
| 139 | +fc_diff_z = np.arctanh(fc_diff) |
| 140 | +fc_diff_z = np.arctanh(fc_diff) |
| 141 | + |
| 142 | +# calculate the mean of correlation coefficients across all given subjects |
| 143 | +fc_diff_z_mean = np.mean(fc_diff_z, axis=0) |
| 144 | +fc_diff_z_mean = np.mean(fc_diff_z, axis=0) |
| 145 | + |
| 146 | +# now, convert back to from Z to R correlation coefficients, prior to plotting |
| 147 | +fc_diff_mean = np.tanh(fc_diff_z_mean) |
| 148 | +fc_diff_mean = np.tanh(fc_diff_z_mean) |
| 149 | + |
| 150 | +#initialize new figure for correlations of Resting State mean |
| 151 | +fig = plt.figure('Across-subject average of FC differences (TB-RS)') |
| 152 | +ax = fig.add_subplot(111) |
| 153 | + |
| 154 | +# apply mask to get rid of upper triangle, including main diagonal |
| 155 | +mask = np.tri(fc_diff_mean.shape[0], k=0) |
| 156 | +mask = np.transpose(mask) |
| 157 | +fc_diff_mean = np.ma.array(fc_diff_mean, mask=mask) # mask out upper triangle |
| 158 | + |
| 159 | +# plot correlation matrix as a heatmap |
| 160 | +cmap = CM.get_cmap('jet', 10) |
| 161 | +cmap.set_bad('w') |
| 162 | +cax = ax.imshow(fc_diff_mean, vmin=-1, vmax=1.0, interpolation='nearest', cmap=cmap) |
| 163 | +ax.grid(False) |
| 164 | +plt.colorbar(cax) |
| 165 | + |
| 166 | +# change frequency of ticks to match number of ROI labels |
| 167 | +plt.xticks(np.arange(0, len(labels))) |
| 168 | +plt.yticks(np.arange(0, len(labels))) |
| 169 | + |
| 170 | +# display labels for brain regions |
| 171 | +ax.set_xticklabels(labels, rotation=90) |
| 172 | +ax.set_yticklabels(labels) |
| 173 | + |
| 174 | +# Turn off all the ticks |
| 175 | +ax = plt.gca() |
| 176 | + |
| 177 | +for t in ax.xaxis.get_major_ticks(): |
| 178 | + t.tick1On = False |
| 179 | + t.tick2On = False |
| 180 | +for t in ax.yaxis.get_major_ticks(): |
| 181 | + t.tick1On = False |
| 182 | + t.tick2On = False |
| 183 | + |
| 184 | +# initialize new figure for histogram |
| 185 | +fig = plt.figure('Average of FC differences (TB-RS)') |
| 186 | +ax = fig.add_subplot(111) |
| 187 | + |
| 188 | +# flatten the numpy cross-correlation matrix |
| 189 | +corr_mat = np.ma.ravel(fc_diff_mean) |
| 190 | + |
| 191 | +# remove masked elements from cross-correlation matrix |
| 192 | +corr_mat = np.ma.compressed(corr_mat) |
| 193 | + |
| 194 | +# plot a histogram to show the frequency of correlations |
| 195 | +plt.hist(corr_mat, 25) |
| 196 | + |
| 197 | +plt.xlabel('Correlation Coefficient') |
| 198 | +plt.ylabel('Number of occurrences') |
| 199 | +plt.axis([-1, 1, 0, 130]) |
| 200 | + |
| 201 | +# initialize new figure scatter plot of xcorr_rs average vs xcorr_tb average |
| 202 | +fig = plt.figure() |
| 203 | +ax = fig.add_subplot(111) |
| 204 | + |
| 205 | +# apply mask to get rid of upper triangle, including main diagonal |
| 206 | +mask = np.tri(xcorr_rs_avg.shape[0], k=0) |
| 207 | +mask = np.transpose(mask) |
| 208 | +xcorr_rs_avg = np.ma.array(xcorr_rs_avg, mask=mask) # mask out upper triangle |
| 209 | +xcorr_tb_avg = np.ma.array(xcorr_tb_avg, mask=mask) # mask out upper triangle |
| 210 | + |
| 211 | +# flatten the numpy cross-correlation arrays |
| 212 | +corr_mat_RS = np.ma.ravel(xcorr_rs_avg) |
| 213 | +corr_mat_TB = np.ma.ravel(xcorr_tb_avg) |
| 214 | + |
| 215 | +# remove masked elements from cross-correlation arrays |
| 216 | +corr_mat_RS = np.ma.compressed(xcorr_rs_avg) |
| 217 | +corr_mat_TB = np.ma.compressed(xcorr_tb_avg) |
| 218 | + |
| 219 | + |
| 220 | +# plot a scatter plot to show how averages of xcorr1 and xcorr2 correlate |
| 221 | +plt.scatter(corr_mat_RS, corr_mat_TB) |
| 222 | +plt.xlabel('Resting-State') |
| 223 | +plt.ylabel('Task-Based') |
| 224 | +plt.axis([-1,1,-1,1]) |
| 225 | + |
| 226 | +# calculate and print kurtosis |
| 227 | +print '\nResting-State Fishers kurtosis: ', kurtosis(corr_mat, fisher=True) |
| 228 | +print 'Resting-State Skewness: ', skew(corr_mat) |
| 229 | + |
| 230 | + |
| 231 | +# Show the plots on the screen |
| 232 | +plt.show() |
0 commit comments