<jupyter_start><jupyter_text>Step 6 Decode the gradients with NeuroSynth topicsHere we take another approach for interpreting the gradients - decode them using spatial maps from automatic, meta-analytic database (Yarkoni et al. 2011, Poldrack et al. 2012) References- <NAME>., <NAME>., <NAME>., <NAME>. and <NAME>., 2011. Large-scale automated synthesis of human functional neuroimaging data. Nature methods, 8(8), pp.665-670.- <NAME>., <NAME>., <NAME>., <NAME>., <NAME>. and <NAME>., 2012. Discovering relations between mind, brain, and mental disorders using topic mapping. PLoS Comput Biol, 8(10), p.e1002707.<jupyter_code>%matplotlib inline

from nilearn.datasets import load_mni152_template
from nilearn import plotting, image
import nibabel as nib

import statsmodels.api as sm
import seaborn as sns

import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['pdf.fonttype'] = 42
mpl.rcParams['font.size'] = 18

import numpy as np
import glob<jupyter_output>/usr/local/lib/python2.7/site-packages/nibabel/cifti2/parse_cifti2.py:24: FutureWarning: We no longer carry a copy of the 'six' package in nibabel; Please import the 'six' package directly
  from ..externals.six import BytesIO
/usr/local/lib/python2.7/site-packages/sklearn/cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)<jupyter_text>Convert 50-topic solution maps to CIFTI For this step, we need to combine the topic files into a 4D image and convert them to a CIFTI file.To convert NIFTI to CIFTI, you need to run the 'nifti_to_cifti.sh topics_25.nii.gz topics25.dscalar.nii' script from ../data/cifti folder. It takes the topic-maps concatenated into a 4D file and produces a CIFTI version of them. In the following steps, we relate the gradients to these maps.  Map gradients to Neurosynth topics<jupyter_code>tfiles = glob.glob('../data/Neurosynth_topic_maps/*.nii.gz')

topicname = np.array(['_'.join(t.split('/')[-1].split('_')[2:5]) for t in tfiles])
np.savetxt('../data/cifti/topics.txt', topicname, fmt = '%s')
gradients = nib.load('../data/rsFC_eigenvectors.dscalar.nii').get_data().T
gradients = sm.add_constant(gradients[:,0:3])

topics = nib.load('../data/cifti/topics25.dscalar.nii').get_data().T
topicname = np.genfromtxt('../data/cifti/topics.txt', dtype = str)<jupyter_output>pixdim[1,2,3] should be non-zero; setting 0 dims to 1
pixdim[1,2,3] should be non-zero; setting 0 dims to 1<jupyter_text>Fit gradients to topic maps<jupyter_code>coefs = np.zeros([topics.shape[1],5])

print "Topic                                         G1         G2          G3        F         p"
print "==============================================================================================="
for i in range(topics.shape[1]): 
    ols = sm.OLS(topics[:,i], gradients)
    ols = ols.fit(cov_type="HAC", cov_kwds={'maxlags':1000})
    #ols_null = sm.OLS(topics[:,i], np.ones(gradients.shape[0]))
    #ols_null = ols_null.fit(cov_type="HAC", cov_kwds={'maxlags':5000})
    coefs[i,0:3] = ols.params[1:4]
    coefs[i,3] = ols.fvalue
    coefs[i,4] = ols.f_pvalue
    print "%-40s      %-6.2f     %-6.2f      %-6.2f    %-6.2f    %-6.4f" % (topicname[i], 
                                                                  ols.params[1], ols.params[2], ols.params[3],
                                                                  ols.fvalue, ols.f_pvalue)<jupyter_output>Topic                                         G1         G2          G3        F         p
===============================================================================================
action_actions_motor                          -0.31      0.06        0.01      9.65      0.0000
object_objects_category                       -0.09      0.29        -0.06     5.34      0.0011
task_performance_cognitive                    0.04       0.10        0.25      8.58      0.0000
visual_auditory_sensory                       -0.15      0.11        -0.11     13.25     0.0000
face_faces_recognition                        -0.10      0.33        -0.11     4.62      0.0031
language_hemispheric_lateralization           -0.00      -0.00       0.00      3.11      0.0253
inhibition_response_inhibitory                0.02       -0.02       0.05      6.76      0.0001
motion_visual_perception                      -0.27      0.51        -0.04     11.03     0.0000
memory_retrieval_encoding                    [...]<jupyter_text>Select topics that can be explained by gradientsWe select only the topics, for which at least gradient receives |weight| > 0.05. An alternative would be to select them using p-values, but that would be trading one arbitrary criterion for another. In the end, we preserve 23 of 50 topics.<jupyter_code>max_coefs = np.max(coefs[:,0:3], axis = 1)
good = np.where(np.logical_or(max_coefs > 0.05, max_coefs < -0.05))[0]
topicname[good]
topicname_clean = ['action','object',
                   'performance','sensory',
                   'face', 'inhibition',
                   'perception','retrieval',
                   'eye','social',
                   'language','working_memory',
                   'reward','auditory',
                   'somatosensory','comprehension', 
                   'emotion', 'attention', 
                   'number','semantic',
                   'response_conflict','pain',
                   'movement']<jupyter_output><empty_output><jupyter_text>Compare original topic names with simplified version<jupyter_code>zip(topicname[good], topicname_clean)<jupyter_output><empty_output><jupyter_text>Plot the topics<jupyter_code>coefs_thr = coefs[good,:]
topicname_thr = topicname[good]
sns.set_style('white')

fig = plt.figure(figsize = (15,15))
ax = fig.add_subplot(111)
#ax.scatter(coefs_thr[:,0], coefs_thr[:,1], s = 100, c = 'red')

for i in range(len(topicname_clean)):
    ax.annotate(topicname_clean[i], xy=(coefs_thr[i,0], coefs_thr[i,1]), 
                xytext=(coefs_thr[i,0], coefs_thr[i,1]),
                #arrowprops=dict(facecolor='black', shrink=0.05),
                )


ax.set_xlim(-0.6,0.6)
ax.set_ylim(-0.6,0.6)

ax.set_title('Gradients 1 & 2', fontsize = 24)
ax.set_xlabel('Gradient 1', fontsize = 18)
ax.set_ylabel('Gradient 2', fontsize = 18)

plt.tight_layout()

plt.savefig('../figures/terms_G12.pdf')
sns.set_style('white')

fig = plt.figure(figsize = (15,15))
ax = fig.add_subplot(111)

for i in range(len(topicname_clean)):
    ax.annotate(topicname_clean[i], xy=(coefs_thr[i,0], coefs_thr[i,2]), 
                xytext=(coefs_thr[i,0], coefs_thr[i,2]),
                #arrowprops=dict(facecolor='black', shrink=0.05),
                )

ax.set_ylim(-0.5,0.5)
ax.set_xlim(-0.5,0.5)

ax.set_title('Gradients 1 & 3', fontsize = 24)
ax.set_xlabel('Gradient 1', fontsize = 18)
ax.set_ylabel('Gradient 3', fontsize = 18)

plt.tight_layout()

plt.savefig('../figures/terms_G13.pdf')
sns.set_style('white')

fig = plt.figure(figsize = (15,15))
ax = fig.add_subplot(111)
#ax.scatter(coefs_thr[:,0], coefs_thr[:,1], s = 100, c = 'red')

for i in range(len(topicname_clean)):
    ax.annotate(topicname_clean[i], xy=(coefs_thr[i,1], coefs_thr[i,2]), 
                xytext=(coefs_thr[i,1], coefs_thr[i,2]),
                #arrowprops=dict(facecolor='black', shrink=0.05),
                )


ax.set_xlim(-0.6,0.6)
ax.set_ylim(-0.6,0.6)

ax.set_title('Gradients 2 & 3', fontsize = 24)
ax.set_xlabel('Gradient 2', fontsize = 18)
ax.set_ylabel('Gradient 3', fontsize = 18)

plt.tight_layout()

plt.savefig('../figures/terms_G23.pdf')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15342290467050543, 'fraction_numerical': 0.05015994881637876, 'mean_word_length': 2.8294953454189122, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '635907', 'license': 'Apache-2.0', 'n_tokens_mistral': 2952, 'n_tokens_neox': 2618, 'n_words': 609, 'path': 'scripts/06_Map_Neurosynth_terms.ipynb', 'repo_name': 'mfalkiewicz/functional_gradients', 'size': '7872'}