Exemple #1
0
 def __init__(self, C=100):
     """SVM classifier for to classes using PyML
     Arguments:
     - `C`: Penalty term for missclassified samples in SVM
     """
     #self.classifier = pymvpa.kNN(k=1, dfx=pymvpa.one_minus_correlation, voting='majority')
     self.classifier = pymvpa.LinearCSVMC(C=C)
Exemple #2
0
ds_q2.sa['targets'] = ds_q2.sa['tax']
#del ds_q2.sa['intents']
del ds_q2.sa['stats']
mv.zscore(ds_q2, chunks_attr='chunks')

n_medial = {'lh': 3486, 'rh': 3491}
medial_wall = np.where(np.sum(ds_q2.samples == 0, axis=0) == 200)[0].tolist()
cortical_vertices = np.where(
    np.sum(ds_q2.samples == 0, axis=0) < 200)[0].tolist()
assert len(medial_wall) == n_medial[hemisphere]
n_vertices = ds_q2.fa.node_indices.shape[0]
assert len(medial_wall) + len(cortical_vertices) == n_vertices

# 2. cross validation __________________________________________________________________
# setting up classifier
clf = mv.LinearCSVMC(space='targets')
cv = mv.CrossValidation(clf, mv.NFoldPartitioner(attr='chunks'))
cv_within = cv(ds_q2)
cv_within
np.mean(cv_within)
# why is the mean lower?

# 3. searchlight _______________________________________________________________________
fsaverage_gii = os.path.join(main_dir, 'fs_templates',
                             hemisphere + '.pial.gii')
surf = mv.surf.read(fsaverage_gii)
# note: surf.vertices.shape (81920, 3) and surf.faces.shape (40962, 3) surface = surf,
qe = mv.SurfaceQueryEngine(surf, radius=radii, distance_metric='dijkstra')
sl = mv.Searchlight(cv, queryengine=qe, roi_ids=cortical_vertices)
sl_q2 = sl(ds_q2)
sub_name = sys.argv[1]
hemisphere = sys.argv[2]
task_list = ['beh', 'tax']
radii = 10.0

# 1. create pymvpa dataset  ____________________________________________________________
ds_q3 = generate_dataset.create_dataset(sub_name, main_dir, task_list,
                                        hemisphere)
ds_q3.sa['chunks'] = ds_q3.sa['tax']
ds_q3.sa['targets'] = ds_q3.sa['beh']
del ds_q3.sa['intents']
mv.zscore(ds_q3, chunks_attr='chunks')

# 2. cross validation __________________________________________________________________
# setting up classifier
clf = mv.LinearCSVMC()
cv = mv.CrossValidation(clf, mv.NFoldPartitioner())
cv_within = cv(ds_q3)
cv_within
np.mean(cv_within)
# why is the mean lower?

# 3. searchlight _______________________________________________________________________
fsaverage_gii = os.path.join(main_dir, 'fs_templates',
                             hemisphere + '.pial.gii')
surf = mv.surf.read(fsaverage_gii)
# note: surf.vertices.shape (81920, 3) and surf.faces.shape (40962, 3) surface = surf,
qe = mv.SurfaceQueryEngine(surf, radius=radii, distance_metric='dijkstra')
sl = mv.Searchlight(cv, queryengine=qe, nproc=4)
sl_q3 = sl(ds_q3)
medial_wall = np.where(np.sum(ds.samples == 0, axis=0) == n_conditions *
                       5)[0].tolist()
cortical_vertices = np.where(
    np.sum(ds.samples == 0, axis=0) < n_conditions * 5)[0].tolist()
assert len(medial_wall) == n_medial[hemi]
assert len(medial_wall) + len(cortical_vertices) == n_vertices

#np.save(join(mvpa_dir, 'cortical_vertices_{0}.npy'.format(hemi)), cortical_vertices)
#cortical_vertices = = np.load(join(mvpa_dir, 'cortical_vertices_{0}.npy').tolist()

# Z-score features across samples
#mv.zscore(ds, chunks_attr='runs')
ds.samples = ((ds.samples - np.mean(ds.samples, axis=1)[:, None]) /
              np.std(ds.samples, axis=1)[:, None])

clf = mv.LinearCSVMC(space=targets)

cv = mv.CrossValidation(clf,
                        mv.NFoldPartitioner(attr=chunks),
                        errorfx=mv.mean_match_accuracy)

sl = mv.Searchlight(cv,
                    queryengine=qe,
                    enable_ca=['roi_sizes'],
                    nproc=1,
                    roi_ids=cortical_vertices)
#sl = mv.Searchlight(cv_rsa, queryengine=qe, enable_ca=['roi_sizes'],
#                    nproc=1, results_backend='native', roi_ids=cortical_vertices)
#tmp_prefix='/local/tmp/sam_sl_p{0}_{1}_'.format(participant_id, hemi)
mv.debug.active += ['SLC']
sl_result = sl(ds)
Exemple #5
0
    if ds is None:
        ds = d
    else:
        ds = mv.vstack((ds, d))
ds.fa['node_indices'] = range(ds.shape[1])

# zscore all of our samples
mv.zscore(ds, chunks_attr='chunks', dtype='float32')
# load in surgace and get searchlight query
radius = 10
surface = mv.surf.read(join(data_path, '{0}.pial.gii'.format(hemi)))
# this is an arbitrary radius and distance metric!
query = mv.SurfaceQueryEngine(surface, radius, distance_metric='dijkstra')
# based off PyMVPA tutorial
print 'building classifier'
clf = mv.LinearCSVMC(space=predict)
print 'training cross validator'
cv = mv.CrossValidation(clf,
                        mv.NFoldPartitioner(attr=train_on),
                        errorfx=mv.mean_match_accuracy)

searchlights = mv.Searchlight(cv,
                              queryengine=query,
                              postproc=mv.mean_sample(),
                              roi_ids=None)
mv.debug.active += ['SLC']
print 'applying SLs'
sl_clf_results = searchlights(ds)

outstr = save_path + 'results/sub' + sub + '_sl_clf_' + predict + '_' + hemi
res = np.array(sl_clf_results)