In [128]:
import pandas as pd
import numpy as np
import pyneurovault.api as pnv
import subprocess
import urllib2
import urllib
import nibabel as nib
import os.path
import nilearn
import sklearn.svm
import sklearn.feature_selection
import sklearn.pipeline
import sklearn.metrics
import matplotlib.pyplot as plt
import sklearn.cross_validation
In [2]:
nv = pd.read_csv('neurovault_labeled.csv')
In [3]:
cols = ['id','file', 'collection_id', 'map_type', 'modality','not_mni',
'cognitive_contrast_cogatlas','cognitive_contrast_cogatlas_id',
'cognitive_paradigm_cogatlas', 'cognitive_paradigm_cogatlas_id',
'contrast_definition', 'contrast_definition_cogatlas']
nv = nv.loc[:,cols]
In [4]:
for col in nv.columns:
print col
print nv.shape
In [5]:
nv.rename(columns={"id":"image_id"},inplace=True)
In [6]:
nv['not_mni'].value_counts()
Out[6]:
In [10]:
nv = pnv.download_images("./images/",nv,resample=True,target='/home/kesslerd/repos/cogfusion/nv_tagging/images/space.nii.gz')
In [11]:
template = os.path.join('.','images','resampled','{0:06d}.nii.gz')
nv['handle'] = [nib.load(template.format(path)) for path in nv['image_id'] ]
Out[11]:
In [98]:
nv['shape']=[handle.shape for handle in nv['handle']]
nv['shape'].value_counts()
Out[98]:
In [99]:
X = np.array( [handle.get_data().ravel() for handle in nv['handle']])
In [110]:
y2 = pd.DataFrame(y)
In [111]:
newmask = np.array(y2.isin(["0-back","Math"]))
In [112]:
newmask.squeeze().shape
Out[112]:
In [113]:
X = X[newmask.squeeze(),:]
y = y[newmask.squeeze()]
In [118]:
y.value_counts()
Out[118]:
In [14]:
X.shape
Out[14]:
In [15]:
y = nv['cognitive_contrast_cogatlas']
In [119]:
filt = sklearn.feature_selection.SelectKBest(k=1000)
In [120]:
pd.value_counts(y)
Out[120]:
In [121]:
clf = sklearn.svm.SVC(kernel='linear',class_weight='balanced')
clf = sklearn.svm.LinearSVC(class_weight={"0-back":1e4,"Math":1e4},C=1)
In [122]:
nv_pipe = sklearn.pipeline.Pipeline([('selector',filt),('classifier',clf)])
In [123]:
nv_pipe.fit(X,y)
Out[123]:
In [124]:
nv_pipe.score(X,y)
Out[124]:
In [ ]:
In [125]:
pd.value_counts( nv_pipe.predict(X))
Out[125]:
In [127]:
%matplotlib inline
plt.matshow( sklearn.metrics.confusion_matrix(y,nv_pipe.predict(X)), cmap = 'bone')
Out[127]:
In [129]:
sklearn.cross_validation.cross_val_score(nv_pipe,X,y)
Out[129]:
In [133]:
mycv = sklearn.cross_validation.StratifiedKFold(y)
In [132]:
sklearn.cross_validation.permutation_test_score(nv_pipe,X,y,cv=mycv)