Exemple #1
0
def layer_ridge_regression(train_feat,
                           train_fmri,
                           val_feat,
                           val_fmri,
                           out_dir,
                           prefix,
                           with_wt=True):
    """Calculate ridge regression between features from one layer and
    the fmri responses from all voxels.
    """
    train_feat = train_feat.reshape(-1, train_feat.shape[3])
    val_feat = val_feat.reshape(-1, val_feat.shape[3])
    voxel_size = train_fmri.shape[0]
    corr_file = os.path.join(out_dir, prefix + '_corr.npy')
    wt, corr, valphas, bscores, valinds = ridge.bootstrap_ridge(
        train_feat.T,
        train_fmri.T,
        val_feat.T,
        val_fmri.T,
        alphas=np.logspace(-2, 2, 20),
        nboots=5,
        chunklen=100,
        nchunks=10,
        single_alpha=True)
    np.save(corr_file, corr)
    if with_wt:
        wt_file = os.path.join(out_dir, prefix + '_weights.npy')
        np.save(wt_file, wt)
Exemple #2
0
def cnn_pred_sugar(train_fmri, train_feat, val_fmri, val_feat, corr_mtx, idx):
    """Sugar function for cnn activation prediction."""
    row, col = idx[0], idx[1]
    print 'row %s - col %s' % (row, col)
    tfeat = train_feat[:, row, col, :]
    vfeat = val_feat[:, row, col, :]
    wt, corr, valphas, bscores, valinds = ridge.bootstrap_ridge(
        train_fmri.T,
        tfeat.T,
        val_fmri.T,
        vfeat.T,
        alphas=np.logspace(-2, 2, 20),
        nboots=5,
        chunklen=100,
        nchunks=10,
        single_alpha=False)
    corr_mtx[:, row, col] = corr
Exemple #3
0
def ridge_sugar(train_feat, train_fmri, val_feat, val_fmri, corr_mtx, idx):
    """Sugar function for ridge regression."""
    pixel_size = (train_feat.shape[1], train_feat.shape[2])
    row, col = idx[0], idx[1]
    print 'row %s - col %s' % (row, col)
    tfeat = train_feat[:, row, col, :]
    vfeat = val_feat[:, row, col, :]
    wt, corr, valphas, bscores, valinds = ridge.bootstrap_ridge(
        tfeat.T,
        train_fmri.T,
        vfeat.T,
        val_fmri.T,
        alphas=np.logspace(-2, 2, 20),
        nboots=5,
        chunklen=100,
        nchunks=10,
        single_alpha=True)
    corr_mtx[row * pixel_size[0] + col] = corr
def ridge_fitting(feat_dir, prf_dir, db_dir, subj_id, roi):
    """pRF model fitting using ridge regression.
    90% trainning data used for model tuning, and another 10% data used for
    model seletion.
    """
    # load fmri response
    vxl_idx, train_fmri_ts, val_fmri_ts = dataio.load_vim1_fmri(db_dir,
                                                                subj_id,
                                                                roi=roi)
    del val_fmri_ts
    print 'Voxel number: %s' % (len(vxl_idx))
    # load candidate models
    train_models = np.load(os.path.join(feat_dir, 'train_candidate_model.npy'),
                           mmap_mode='r')
    # output directory config
    roi_dir = os.path.join(prf_dir, roi)
    check_path(roi_dir)
    # model seletion and tuning
    ALPHA_NUM = 20
    BOOTS_NUM = 15
    paras_file = os.path.join(roi_dir, 'reg_paras.npy')
    paras = np.memmap(paras_file,
                      dtype='float64',
                      mode='w+',
                      shape=(42500, len(vxl_idx), 72))
    mcorr_file = os.path.join(roi_dir, 'reg_model_corr.npy')
    mcorr = np.memmap(mcorr_file,
                      dtype='float64',
                      mode='w+',
                      shape=(42500, len(vxl_idx)))
    alphas_file = os.path.join(roi_dir, 'reg_alphas.npy')
    alphas = np.memmap(alphas_file,
                       dtype='float64',
                       mode='w+',
                       shape=(42500, len(vxl_idx)))
    # fMRI data z-score
    print 'fmri data temporal z-score'
    m = np.mean(train_fmri_ts, axis=1, keepdims=True)
    s = np.std(train_fmri_ts, axis=1, keepdims=True)
    train_fmri_ts = (train_fmri_ts - m) / (1e-10 + s)
    # split training dataset into model tunning set and model selection set
    tune_fmri_ts = train_fmri_ts[:, :int(1750 * 0.9)]
    sel_fmri_ts = train_fmri_ts[:, int(1750 * 0.9):]
    # model testing
    for i in range(42500):
        print 'Model %s' % (i)
        # remove models which centered outside the 20 degree of visual angle
        xi = (i % 2500) / 50
        yi = (i % 2500) % 50
        x0 = np.arange(5, 500, 10)[xi]
        y0 = np.arange(5, 500, 10)[yi]
        d = np.sqrt(np.square(x0 - 250) + np.square(y0 - 250))
        if d > 249:
            print 'Model center outside the visual angle'
            paras[i, ...] = np.NaN
            mcorr[i] = np.NaN
            alphas[i] = np.NaN
            continue
        train_x = np.array(train_models[i, ...]).astype(np.float64)
        train_x = zscore(train_x.T).T
        # split training dataset into model tunning and selection sets
        tune_x = train_x[:int(1750 * 0.9), :]
        sel_x = train_x[int(1750 * 0.9):, :]
        wt, r, alpha, bscores, valinds = ridge.bootstrap_ridge(
            tune_x,
            tune_fmri_ts.T,
            sel_x,
            sel_fmri_ts.T,
            alphas=np.logspace(-2, 3, ALPHA_NUM),
            nboots=BOOTS_NUM,
            chunklen=175,
            nchunks=1,
            single_alpha=False,
            use_corr=False)
        paras[i, ...] = wt.T
        mcorr[i] = r
        alphas[i] = alpha
    # save output
    paras = np.array(paras)
    np.save(paras_file, paras)
    mcorr = np.array(mcorr)
    np.save(mcorr_file, mcorr)
    alphas = np.array(alphas)
    np.save(alphas_file, alphas)
Exemple #5
0
def pls_ridge_fitting(feat_dir, prf_dir, db_dir, subj_id, roi):
    """pRF model fitting using ridge regression.
    90% trainning data used for model tuning, and another 10% data used for
    model seletion.
    """
    # load fmri response
    fmri_data = np.load(os.path.join(prf_dir, roi, 'pls_residual_fmri.npz'))
    vxl_idx = fmri_data['vxl_idx']
    train_fmri_ts = fmri_data['pls_train_residual']

    print 'Voxel number: %s' % (len(vxl_idx))
    # load candidate models
    train_models = np.load(os.path.join(feat_dir, 'train_candidate_model.npy'),
                           mmap_mode='r')
    # output directory config
    roi_dir = os.path.join(prf_dir, roi, 'pls_residual')
    check_path(roi_dir)
    # model seletion and tuning
    ALPHA_NUM = 20
    BOOTS_NUM = 15
    paras_file = os.path.join(roi_dir, 'reg_paras.npy')
    paras = np.memmap(paras_file,
                      dtype='float64',
                      mode='w+',
                      shape=(15360, len(vxl_idx), 46))
    mcorr_file = os.path.join(roi_dir, 'reg_model_corr.npy')
    mcorr = np.memmap(mcorr_file,
                      dtype='float64',
                      mode='w+',
                      shape=(15360, len(vxl_idx)))
    alphas_file = os.path.join(roi_dir, 'reg_alphas.npy')
    alphas = np.memmap(alphas_file,
                       dtype='float64',
                       mode='w+',
                       shape=(15360, len(vxl_idx)))
    # fMRI data z-score
    print 'fmri data temporal z-score'
    m = np.mean(train_fmri_ts, axis=1, keepdims=True)
    s = np.std(train_fmri_ts, axis=1, keepdims=True)
    train_fmri_ts = (train_fmri_ts - m) / (1e-10 + s)
    # split training dataset into model tunning set and model selection set
    tune_fmri_ts = train_fmri_ts[:, :int(7200 * 0.9)]
    sel_fmri_ts = train_fmri_ts[:, int(7200 * 0.9):]
    # model testing
    for i in range(15360):
        print 'Model %s' % (i)
        train_x = np.array(train_models[i, ...]).astype(np.float64)
        train_x = zscore(train_x).T
        # split training dataset into model tunning and selection sets
        tune_x = train_x[:int(7200 * 0.9), :]
        sel_x = train_x[int(7200 * 0.9):, :]
        wt, r, alpha, bscores, valinds = ridge.bootstrap_ridge(
            tune_x,
            tune_fmri_ts.T,
            sel_x,
            sel_fmri_ts.T,
            alphas=np.logspace(-2, 3, ALPHA_NUM),
            nboots=BOOTS_NUM,
            chunklen=720,
            nchunks=1,
            single_alpha=False,
            use_corr=False)
        paras[i, ...] = wt.T
        mcorr[i] = r
        alphas[i] = alpha
    # save output
    paras = np.array(paras)
    np.save(paras_file, paras)
    mcorr = np.array(mcorr)
    np.save(mcorr_file, mcorr)
    alphas = np.array(alphas)
    np.save(alphas_file, alphas)