# 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) # 4. save output _______________________________________________________________________ if not os.path.exists( os.path.join(main_dir, 'analysis', 'searchlight', sub_name)): os.makedirs(os.path.join(main_dir, 'analysis', 'searchlight', sub_name)) # save as NIML dataset niml_q1_filename = os.path.join( main_dir, 'analysis', 'searchlight', sub_name, sub_name + '_ques-02_' + hemisphere + '_searchlight_radii-' + str(radii) + '.niml.dset') mv.niml.write(niml_q1_filename, sl_q2) # save as GIFTI sub-rid000001_ques-01_lh_searchlight_radii-10
task = sys.argv[3] targets = sys.argv[4] if targets == 'taxonomy': chunks = 'behavior' elif targets == 'behavior': chunks = 'taxonomy' runs = [1, 2, 3, 4, 5] n_conditions = 20 n_vertices = 40962 #n_medial = {'lh': 3487, 'rh': 3491} n_medial = {'lh': 3486, 'rh': 3491} # Load surface and create searchlight query engine surf = mv.surf.read(join(glm_dir, '{0}.pial.gii'.format(hemi))) qe = mv.SurfaceQueryEngine(surf, 10.0, distance_metric='dijkstra') # Load in surface data sets dss = [] for run in runs: ds = mv.niml.read( join( glm_dir, 'sub-{0}_task-{1}_run-{2}_rw-glm.{3}.coefs.niml.dset'.format( participant, task, run, hemi))) ds.sa.pop('stats') ds.sa['behavior'] = np.tile(['eating', 'fighting', 'running', 'swimming'], 5) ds.sa['taxonomy'] = np.repeat( ['bird', 'insect', 'primate', 'reptile', 'ungulate'], 4) ds.sa['conditions'] = [
d = mv.gifti_dataset(files[x], chunks=chunks, targets=targets) d.sa['conditions'] = conditions d.sa['taxonomy'] = taxonomy d.sa['behavior'] = behavior 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 clf = mv.LinearNuSVMC(space=predict) cv = mv.CrossValidation(clf, mv.NFoldPartitioner(attr=train_on), errorfx=lambda p, t: np.mean(p == t), enable_ca=['stats']) searchlights = mv.Searchlight(cv, queryengine=query, postproc=mv.mean_sample(), roi_ids=None) sl_clf_results = searchlights(ds) outstr = save_path + 'results/sub' + sub + '_sl_clf_' + predict + '_' + hemi res = np.array(sl_clf_results) np.save(outstr, res)
chunks = [x + 1] * 20 else: chunks = [x - 5 + 1] * 20 d = mv.gifti_dataset(files[x], chunks=chunks, targets=conditions) d.sa['conditions'] = conditions if ds is None: ds = d else: ds = mv.vstack((ds, d)) ds.fa['node_indices'] = range(n_vertices) ds.samples = zscore(ds.samples, axis=1) mtgs = mean_group_sample(['conditions']) mtds = mtgs(ds) query = mv.SurfaceQueryEngine(surface, radius, distance_metric='dijkstra', fa_node_key='node_indices') query.train(ds) dsm = rsa.PDist(square=False) print('made dsms') sl = mv.Searchlight(dsm, query, roi_ids=query.query_byid(max_node)) slres = sl(mtds) mv.debug.active += ['SLC'] print('made our sls') slres.samples = np.nan_to_num(slres.samples) all_ROI_res.append(slres.samples) all_ROI_res = np.array(all_ROI_res) # this array is (#participants, 190, #nodes) #all_ROI_res = np.swapaxes(all_slres, 0, 2)