def plscorr_eval(train_fmri_ts, train_feat_ts, val_fmri_ts, val_feat_ts, out_dir, mask_file): """Compute PLS correlation between brain activity and CNN activation.""" train_feat_ts = train_feat_ts.reshape(-1, train_feat_ts.shape[3]).T val_feat_ts = val_feat_ts.reshape(-1, val_feat_ts.shape[3]).T train_fmri_ts = train_fmri_ts.T val_fmri_ts = val_fmri_ts.T # Iteration loop for different component number #for n in range(5, 19): # print '--- Components number %s ---' %(n) # plsca = PLSCanonical(n_components=n) # plsca.fit(train_feat_ts, train_fmri_ts) # pred_feat_c, pred_fmri_c = plsca.transform(val_feat_ts, val_fmri_ts) # pred_fmri_ts = plsca.predict(val_feat_ts) # # calculate correlation coefficient between truth and prediction # r = corr2_coef(val_fmri_ts.T, pred_fmri_ts.T, mode='pair') # # get top 20% corrcoef for model evaluation # vsample = int(np.rint(0.2*len(r))) # print 'Sample size for evaluation : %s' % (vsample) # r.sort() # meanr = np.mean(r[-1*vsample:]) # print 'Mean prediction corrcoef : %s' %(meanr) # model generation based on optimized CC number cc_num = 10 plsca = PLSCanonical(n_components=cc_num) plsca.fit(train_feat_ts, train_fmri_ts) from sklearn.externals import joblib joblib.dump(plsca, os.path.join(out_dir, 'plsca_model.pkl')) plsca = joblib.load(os.path.join(out_dir, 'plsca_model.pkl')) # calculate correlation coefficient between truth and prediction pred_fmri_ts = plsca.predict(val_feat_ts) fmri_pred_r = corr2_coef(val_fmri_ts.T, pred_fmri_ts.T, mode='pair') mask = vutil.data_swap(mask_file) vxl_idx = np.nonzero(mask.flatten()==1)[0] tmp = np.zeros_like(mask.flatten(), dtype=np.float64) tmp[vxl_idx] = fmri_pred_r tmp = tmp.reshape(mask.shape) vutil.save2nifti(tmp, os.path.join(out_dir, 'pred_fmri_r.nii.gz')) pred_feat_ts = pls_y_pred_x(plsca, val_fmri_ts) pred_feat_ts = pred_feat_ts.T.reshape(96, 14, 14, 540) np.save(os.path.join(out_dir, 'pred_feat.npy'), pred_feat_ts) # get PLS-CCA weights feat_cc, fmri_cc = plsca.transform(train_feat_ts, train_fmri_ts) np.save(os.path.join(out_dir, 'feat_cc.npy'), feat_cc) np.save(os.path.join(out_dir, 'fmri_cc.npy'), fmri_cc) feat_weight = plsca.x_weights_.reshape(96, 14, 14, cc_num) #feat_weight = plsca.x_weights_.reshape(96, 11, 11, cc_num) fmri_weight = plsca.y_weights_ np.save(os.path.join(out_dir, 'feat_weights.npy'), feat_weight) np.save(os.path.join(out_dir, 'fmri_weights.npy'), fmri_weight) fmri_orig_ccs = get_pls_components(plsca.y_scores_, plsca.y_loadings_) np.save(os.path.join(out_dir, 'fmri_orig_ccs.npy'), fmri_orig_ccs)
def show_retinotopy(prf_dir, data_type): """Show estimate parameters on brain.""" #data_type = 'ecc' vol = np.empty((73728, )) vol[:] = np.NaN subj_dir = '/'.join(prf_dir.split('/')[:-2]) # get roi list roi_list = ['v1lh', 'v2lh', 'v3lh', 'v4lh', 'v1rh', 'v2rh', 'v3rh', 'v4rh'] rois = os.listdir(prf_dir) rois = [roi for roi in rois if roi in roi_list] for roi in rois: vxl_idx, train_ts, val_ts = dataio.load_fmri(subj_dir, roi=roi) del train_ts, val_ts data = np.load(os.path.join(prf_dir, roi, data_type + '.npy')) vol[vxl_idx] = data vol = vol.reshape(18, 64, 64) vutil.save2nifti(vol, os.path.join(prf_dir, data_type + '.nii.gz'))
def prf2visual_angle(prf_mtx, img_size, out_dir, base_name): """Generate retinotopic mapping based on voxels' pRF parameters. `prf_mtx` is a #voxel x pRF-features matrix, pRF features can be 2 columns (row, col) of image or 3 columns which adding a third pRF size parameters. """ feature_size = prf_mtx.shape[1] pos_mtx = prf_mtx[:, :2] # eccentricity ecc = retinotopy.coord2ecc(pos_mtx, img_size, 20) vol = ecc.reshape(18, 64, 64) vutil.save2nifti(vol, os.path.join(out_dir, base_name+'_ecc.nii.gz')) # angle angle = retinotopy.coord2angle(pos_mtx, img_size) vol = angle.reshape(18, 64, 64) vutil.save2nifti(vol, os.path.join(out_dir, base_name+'_angle.nii.gz')) # pRF size if feature_size > 2: size_angle = retinotopy.get_prf_size(prf_mtx, 55, 20) vol = size_angle.reshape(18, 64, 64) vutil.save2nifti(vol, os.path.join(out_dir, base_name+'_size.nii.gz'))