Ejemplo n.º 1
0
    def __call__(self, dataset):
        zstat = np.zeros(self.n_model)
        if np.count_nonzero(dataset.fa.include) < 10:
            return (*zstat, 0)
        dataset = dataset[:, dataset.fa.include]

        data_vec = st.rankdata(sd.pdist(dataset, 'correlation'))
        for i in range(self.n_model):
            stat = prsa.perm_partial(data_vec, self.perm['model_mats'][i],
                                     self.perm['model_resid'][i])
            zstat[i] = prsa.perm_z(stat)
        return (*zstat, 1)
Ejemplo n.º 2
0
def zstat_contrasts(stat):
    """Calculate z-statistics for a standard set of contrasts."""

    zstat = {
        'pos': prsa.perm_z(np.nansum(stat, 1)),
        'neg': prsa.perm_z(-np.nansum(stat, 1)),
        'block': prsa.perm_z(stat[:, 0]),
        'inter': prsa.perm_z(stat[:, 1]),
        'block_neg': prsa.perm_z(-stat[:, 0]),
        'inter_neg': prsa.perm_z(-stat[:, 1]),
        'block_inter': prsa.perm_z(stat[:, 0] - stat[:, 1]),
        'inter_block': prsa.perm_z(stat[:, 1] - stat[:, 0])
    }
    return zstat
Ejemplo n.º 3
0
def model_set_prsa(model_set, ref_name, model_names, n_perm=10000):
    """Run partial RSA on a model set."""
    # unpack into reference and other models
    ref_rdm = model_set[ref_name]
    other_set = {name: model_set[name] for name in model_names}

    # create permuted models
    model_rdms = list(other_set.values())
    model_names = list(other_set.keys())
    spec = prsa.init_pRSA(n_perm, model_rdms)

    # run partial correlation analysis
    results = {}
    ref_rdv = stats.rankdata(sd.squareform(ref_rdm))
    for i in range(len(model_rdms)):
        mat = spec['model_mats'][i]
        resid = spec['model_resid'][i]
        stat = prsa.perm_partial(ref_rdv, mat, resid)
        z = prsa.perm_z(stat)
        p = 1 - stats.norm.cdf(z)
        results[model_names[i]] = {'z': z, 'p': p, 'stat': stat[0]}
    return pd.DataFrame(results).T
Ejemplo n.º 4
0
def main(subject,
         study_dir,
         roi,
         res_name,
         models,
         category='both',
         suffix='_stim2',
         n_perm=100000,
         match_fam=False):

    # load ROI data and volume information
    input_dir = os.path.join(study_dir, 'batch', 'glm', 'prex' + suffix, 'roi',
                             roi)
    patterns, vols = rsa.load_roi_pattern(input_dir, subject)

    # get items of interest
    if category == 'face':
        cond = [1, 2]
    elif category == 'scene':
        cond = [3, 4]
    else:
        ValueError(f'Invalid category code: {category}')
    include = np.isin(vols.cond.to_numpy(), cond)

    # subsample to match familiarity between subcategories
    model_dir = os.path.join(study_dir, 'batch', 'models3')
    if match_fam:
        subsample = pd.read_csv(os.path.join(model_dir, 'subsample.csv'),
                                index_col=0)
        vol_stim_id = vols['itemno'].to_numpy() - 1
        subsample_stim_id = subsample.index.to_numpy()
        include &= np.isin(vol_stim_id, subsample_stim_id)
    else:
        subsample = None

    # get models of interest
    model_names = models.split('-')
    model_rdms_dict = model.load_category_rdms(model_dir,
                                               category,
                                               model_names,
                                               subsample=subsample)
    model_rdms = [model_rdms_dict[name] for name in model_names]

    # initialize the permutation test
    perm = prsa.init_pRSA(n_perm, model_rdms)
    data_vec = st.rankdata(sd.pdist(patterns[include, :], 'correlation'))
    n_model = len(model_rdms)

    # calculate permutation correlations
    zstat = np.zeros(n_model)
    for i in range(n_model):
        stat = prsa.perm_partial(data_vec, perm['model_mats'][i],
                                 perm['model_resid'][i])
        zstat[i] = prsa.perm_z(stat)

    # save results
    df = pd.DataFrame({'zstat': zstat}, index=model_names)
    res_dir = os.path.join(study_dir, 'batch', 'rsa', res_name, 'roi', roi)
    if not os.path.exists(res_dir):
        os.makedirs(res_dir, exist_ok=True)
    res_file = os.path.join(res_dir, f'zstat_{subject}.csv')
    df.to_csv(res_file)
Ejemplo n.º 5
0
def main(subject,
         study_dir,
         beh_dir,
         rsa_name,
         roi,
         res_name,
         block=None,
         n_perm=1000):
    # load dissimilarity matrix
    rsa_dir = os.path.join(study_dir, 'batch', 'rsa', rsa_name, roi)
    roi_rdm = 1 - np.load(os.path.join(rsa_dir,
                                       f'sub-{subject}_brsa.npz'))['C']

    # get trial pairs to test
    n_state = 21
    if block is not None:
        if block == 'walk':
            roi_rdm = roi_rdm[:n_state, :n_state]
        elif block == 'random':
            roi_rdm = roi_rdm[n_state:, n_state:]
        else:
            raise ValueError(f'Invalid block type: {block}')

    # load structure learning data
    struct = util.load_struct_bids(beh_dir, [subject])

    # simple model: items in different communities are less similar
    comm = struct.groupby('object')['community'].first().to_numpy()
    comm_rdm = (comm[:, None] != comm).astype(float)

    # learning models based on part 1
    # dissimilarity is inversely proportionate to association strength
    struct1 = struct.query('part == 1').copy()
    gamma = [0, .9]
    alpha = 0.05
    sr_mat = [cython_sr.learn_sr(struct1, g, alpha, n_state) for g in gamma]
    sr_rdm = [rsa.make_sym_matrix(1 - sr / np.sum(sr)) for sr in sr_mat]

    # create model set
    model_rdms = [comm_rdm] + sr_rdm
    model_names = ['community', 'sr0', 'sr90']

    # initialize the permutation test
    perm = prsa.init_pRSA(n_perm, model_rdms, rank=False)
    data_vec = sd.pdist(roi_rdm)
    n_model = len(model_rdms)

    # calculate permutation correlations
    rho = np.zeros(n_model)
    zstat = np.zeros(n_model)
    for i in range(n_model):
        stat = prsa.perm_partial(data_vec, perm['model_mats'][i],
                                 perm['model_resid'][i])
        rho[i] = stat[0]
        zstat[i] = prsa.perm_z(stat)

    # save results
    res_dir = os.path.join(study_dir, 'batch', 'prsa', res_name, roi)
    if not os.path.exists(res_dir):
        os.makedirs(res_dir, exist_ok=True)
    df = pd.DataFrame({'rho': rho, 'zstat': zstat}, index=model_names)
    res_file = os.path.join(res_dir, f'zstat_{subject}.csv')
    df.to_csv(res_file)