def random_svm_cope_searchlight(root_dir, subj): """SVM based searchlight analysis.""" # dir config work_dir = os.path.join(root_dir, 'workshop', 'searchlight') # read mask file print 'Load mask data ...' mask_file = os.path.join(work_dir, 'mask', 'func_mask.nii.gz') mask_data = nib.load(mask_file).get_data() mask_data = mask_data > 0 # load nii data list print 'Load nii files ...' cope_list = get_subj_cope_list(root_dir, subj) # get trial sequence info print 'Load trial sequence info ...' tag_list = get_subj_cope_tag(root_dir, subj) for i in range(100): # svm results var clf_results = np.zeros((91, 109, 91, 4)) # for loop for voxel-wise searchlight mask_coord = niroi.get_roi_coord(mask_data) ccount = 0 for c in mask_coord: ccount += 1 print ccount cube_roi = np.zeros((91, 109, 91)) cube_roi = niroi.cube_roi(cube_roi, c[0], c[1], c[2], 2, 1) cube_coord = niroi.get_roi_coord(cube_roi) [train_x, train_y, test_x, test_y] = get_roi_cope_mvps(cope_list, tag_list, cube_coord) clf = svm.SVC(kernel='sigmoid') train_y = np.random.permutation(train_y) clf.fit(train_x, train_y) pred = clf.predict(test_x) for e in range(4): acc = np.sum(pred[test_y == (e + 1)] == (e + 1)) * 1.0 / np.sum(test_y == (e + 1)) print acc clf_results[c[0], c[1], c[2], e] = acc # save to nifti fsl_dir = os.getenv('FSL_DIR') template_file = os.path.join(fsl_dir, 'data', 'standard', 'MNI152_T1_2mm_brain.nii.gz') aff = nib.load(template_file).affine nibase.save2nifti( clf_results, aff, os.path.join( work_dir, 'random_' + subj + '_svm_acc_cope_%s.nii.gz' % (i + 1)))
def get_mvp_group_roi(root_dir): """Get multivoxel activity pattern for each srimulus from each ROI.""" # directory config nii_dir = os.path.join(root_dir, 'nii') ppi_dir = os.path.join(root_dir, 'ppi') # load rois #mask_data = nib.load(os.path.join(ppi_dir, 'cube_rois.nii.gz')).get_data() mask_data = nib.load( os.path.join(root_dir, 'group-level', 'rois', 'neurosynth', 'cube_rois.nii.gz')).get_data() roi_num = int(mask_data.max()) # get scan info from scanlist scanlist_file = os.path.join(root_dir, 'doc', 'scanlist.csv') [scan_info, subj_list] = pyunpack.readscanlist(scanlist_file) for subj in subj_list: # get run infor for emo task sid = subj.sess_ID subj_dir = os.path.join(nii_dir, sid, 'emo') # get run index if not 'emo' in subj.run_info: continue [run_idx, par_idx] = subj.getruninfo('emo') # var for MVP mvp_dict = {} for r in range(roi_num): mvp_dict['roi_%s' % (r + 1)] = [] for i in range(10): if str(i + 1) in par_idx: print 'Run %s' % (i + 1) # load cope data ipar = par_idx.index(str(i + 1)) run_dir = os.path.join(subj_dir, '00' + run_idx[ipar]) print run_dir trn_file = os.path.join(run_dir, 'train_merged_cope.nii.gz') test_file = os.path.join(run_dir, 'test_merged_cope.nii.gz') trn_cope = nib.load(trn_file).get_data() test_cope = nib.load(test_file).get_data() run_cope = np.concatenate((trn_cope, test_cope), axis=3) # XXX: remove mean cope from each trial mean_cope = np.mean(run_cope, axis=3, keepdims=True) run_cope = run_cope - mean_cope # get MVP for each ROI for r in range(roi_num): roi_mask = mask_data.copy() roi_mask[roi_mask != (r + 1)] = 0 roi_mask[roi_mask == (r + 1)] = 1 roi_coord = niroi.get_roi_coord(roi_mask) for j in range(run_cope.shape[3]): vtr = niroi.get_voxel_value(roi_coord, run_cope[..., j]) mvp_dict['roi_%s' % (r + 1)].append(vtr.tolist()) for roi in mvp_dict: mvp_dict[roi] = np.array(mvp_dict[roi]) outfile = r'%s_roi_mvp.mat' % (sid) sio.savemat(outfile, mvp_dict)
def get_vxl_trial_rsp(root_dir): """Get multivoxel activity pattern for each srimulus from whole brain mask. """ # directory config nii_dir = os.path.join(root_dir, 'prepro') rsp_dir = os.path.join(root_dir, 'workshop', 'trial_rsp', 'whole_brain') # load rois mask_data = nib.load( os.path.join(root_dir, 'group-level', 'rois', 'neurosynth', 'cube_rois_r2.nii.gz')).get_data() mask_data = mask_data > 0 # get scan info from scanlist scanlist_file = os.path.join(root_dir, 'doc', 'scanlist.csv') [scan_info, subj_list] = pyunpack.readscanlist(scanlist_file) for subj in subj_list: # get run infor for emo task sid = subj.sess_ID print sid subj_dir = os.path.join(nii_dir, sid) # get run index if not 'emo' in subj.run_info: continue [run_idx, par_idx] = subj.getruninfo('emo') # var for MVP for i in range(10): if not str(i + 1) in par_idx: continue print 'Run %s' % (i + 1) mvp_data = [] # load cope data ipar = par_idx.index(str(i + 1)) run_dir = os.path.join(subj_dir, '00' + run_idx[ipar]) print run_dir rsp_file = os.path.join(run_dir, 'mni_sfunc_data_mcf_hp.nii.gz') rsp = nib.load(rsp_file).get_data() # derive trial-wise response trsp = np.zeros((91, 109, 91, 88)) for t in range(88): trsp[..., t] = (rsp[..., 4 * t + 5] + rsp[..., 4 * t + 6]) / 2 # get MVP of mask vxl_coord = niroi.get_roi_coord(mask_data) for j in range(trsp.shape[3]): vtr = niroi.get_voxel_value(vxl_coord, trsp[..., j]) mvp_data.append(vtr.tolist()) outfile = os.path.join(rsp_dir, '%s_r%s_mvp.npy' % (sid[:2], i + 1)) np.save(outfile, np.array(mvp_data))
def get_mvp_group_roi(scanlist_file, mask_file): """Get multivoxel activity pattern for each srimulus from each ROI.""" # get scan info from scanlist [scan_info, subj_list] = pyunpack.readscanlist(scanlist_file) # directory config nii_dir = scan_info['sessdir'] # load rois mask_data = nib.load(mask_file).get_data() roi_dict = {'rOFA': 1, 'lOFA': 2, 'rFFA': 3, 'lFFA': 4} #output_file = os.path.join(roi_dir, 'neo_group_roi_mvpa.csv') #f = open(output_file, 'wb') #f.write('SID,rOFA,lOFA,rFFA,lFFA,rpcSTS,lpcSTS\n') for subj in subj_list: # get run infor for emo task sid = subj.sess_ID subj_dir = os.path.join(nii_dir, sid, 'emo') # get par index for each emo run if not 'emo' in subj.run_info: continue [run_idx, par_idx] = subj.getruninfo('emo') # var for MVP mvp_dict = {} for roi in roi_dict: mvp_dict[roi] = [] for i in range(len(run_idx)): run_dir = os.path.join(subj_dir, '00'+run_idx[i]) trn_file = os.path.join(run_dir, 'train_merged_cope.nii.gz') test_file = os.path.join(run_dir, 'test_merged_cope.nii.gz') trn_cope = nib.load(trn_file).get_data() test_cope = nib.load(test_file).get_data() run_cope = np.concatenate((trn_cope, test_cope), axis=3) # XXX: remove mean cope from each trial #mean_cope = np.mean(run_cope, axis=3, keepdims=True) #run_cope = run_cope - mean_cope # get MVP for each ROI for roi in roi_dict: roi_mask = mask_data.copy() roi_mask[roi_mask!=roi_dict[roi]] = 0 roi_mask[roi_mask==roi_dict[roi]] = 1 roi_coord = niroi.get_roi_coord(roi_mask) for j in range(run_cope.shape[3]): trl_vtr = niroi.get_voxel_value(roi_coord, run_cope[..., j]) f.write(','.join(temp)+'\n') print 'cost %s s'%(time.time() - start_time)
def get_roi_bold_ts(root_dir, subj, roi): """Get one ROI's BOLD time course.""" # load nii data list print 'load nii files ...' nii_list = get_subj_nii_list(root_dir, subj) # output data shape: #time x #voxel bold_ts = np.zeros((3550, roi.sum())) c = 0 roi_coord = niroi.get_roi_coord(roi) for i in range(10): nii = nii_list[i] for j in range(nii.shape[3]): vtr = niroi.get_voxel_value(roi_coord, nii[..., j]) bold_ts[c] = vtr c += 1 return bold_ts
def get_roi_mvp_data(root_dir, subj, roi_file): """Get MVP data from specific ROI.""" # dir config work_dir = os.path.join(root_dir, 'workshop', 'searchlight') # read ROI file print 'Load ROI data ...' roi_data = nib.load(roi_file).get_data() # load cope data list print 'Load cope files ...' cope_list = get_subj_cope_list(root_dir, subj) # get trial sequence info print 'Load trial sequence info ...' tag_list = get_subj_cope_tag(root_dir, subj) # get ROI mvp data roi_coord = niroi.get_roi_coord(roi_data) [train_x, train_y, test_x, test_y] = get_roi_cope_mvps(cope_list, tag_list, roi_coord) out_file = os.path.join(work_dir, 'cingulate_mvp_data.npz') np.savez(out_file, train_x=train_x, train_y=train_y, test_x=test_x, test_y=test_y)