Пример #1
0
def compute_rsc(d, chans=None):
    """
    Very simple, low level function to compute  noise correlations for a given 
    spike count dictionary. If you want to do any preprocessing (i.e. remove first
    order pupil effects, filter the data etc., this happens before building the 
    spike count dictionary)

    z-score responses in dictionary, and compute the noise correlation matrix. 
    Return a dataframe with index: neuron pair, column: ['rsc', 'pval']
    chans is list of unit names. If none, will just label the index with neuron indices
    """
    resp_dict = d.copy()
    raw_resp_dict = resp_dict.copy()
    log.info("Compute z-scores of responses for noise correlation calculation")
    resp_dict = preproc.zscore_per_stim(resp_dict, d2=resp_dict)

    log.info("Concatenate responses to all stimuli")
    eps = list(resp_dict.keys())
    nCells = resp_dict[eps[0]].shape[1]
    for i, k in enumerate(resp_dict.keys()):
        if i == 0:
            resp_matrix = np.transpose(resp_dict[k], [1, 0, -1]).reshape(nCells, -1)
            raw_resp_matrix = np.transpose(raw_resp_dict[k], [1, 0, -1]).reshape(nCells, -1)
        else:
            resp_matrix = np.concatenate((resp_matrix, np.transpose(resp_dict[k], [1, 0, -1]).reshape(nCells, -1)), axis=-1)
            raw_resp_matrix = np.concatenate((raw_resp_matrix, np.transpose(raw_resp_dict[k], [1, 0, -1]).reshape(nCells, -1)), axis=-1)
    # Note, there will be Nan bins for some neurons
    # (where there were no spikes, std is zero so zscore is nan)
    # these will be excluded in the noise corr. calculation
    combos = list(combinations(np.arange(0, nCells), 2))

    log.info("Computing pairwise rsc values / pvals and saving in df")
    if chans is None:
        df_idx = ["{0}_{1}".format(i, j) for (i, j) in combos]
    else:
        df_idx = ["{0}_{1}".format(chans[i], chans[j]) for (i, j) in combos]
    cols = ['rsc', 'pval', 'gmean']
    df = pd.DataFrame(columns=cols, index=df_idx)
    for i, pair in enumerate(combos):
        n1 = pair[0]
        n2 = pair[1]
        idx = df_idx[i]

        rr = np.isfinite(resp_matrix[n1, :] + resp_matrix[n2, :])
        if rr.sum() >= 2:
            cc, pval = ss.pearsonr(resp_matrix[n1, rr], resp_matrix[n2, rr])
            gmean = np.sqrt(raw_resp_matrix[n1, rr].mean() * raw_resp_matrix[n2, rr].mean())
            if np.isnan(gmean):
                gmean = 0
            df.loc[idx, cols] = [cc, pval, gmean]
        else:
            df.loc[idx, cols] = [np.nan, np.nan, np.nan]

    return df
Пример #2
0
rec['pupil2'] = rec['pupil']._modified_copy(rec['pupil']._data)

# ===================================== perform analysis on raw data =======================================
rec_bp = rec.copy()
ops = {'state': 'big', 'method': 'median', 'epoch': ['REFERENCE'], 'collapse': True}
rec_bp = create_pupil_mask(rec_bp, **ops)
ops = {'state': 'small', 'method': 'median', 'epoch': ['REFERENCE'], 'collapse': True}
rec_sp = rec.copy()
rec_sp = create_pupil_mask(rec_sp, **ops)

real_dict_small = rec_sp['resp'].extract_epochs(epochs, mask=rec_sp['mask'])
real_dict_big = rec_bp['resp'].extract_epochs(epochs, mask=rec_bp['mask'])
real_dict_all = rec['resp'].extract_epochs(epochs)
pred_dict_all = rec['psth'].extract_epochs(epochs)

real_dict_small = cpreproc.zscore_per_stim(real_dict_small, d2=real_dict_small, with_std=True)
real_dict_big = cpreproc.zscore_per_stim(real_dict_big, d2=real_dict_big, with_std=True)
real_dict_all = cpreproc.zscore_per_stim(real_dict_all, d2=real_dict_all, with_std=True)
pred_dict_all = cpreproc.zscore_per_stim(pred_dict_all, d2=pred_dict_all, with_std=True)

eps = list(real_dict_big.keys())
nCells = real_dict_big[eps[0]].shape[1]
for i, k in enumerate(real_dict_big.keys()):
    if i == 0:
        resp_matrix = np.transpose(real_dict_all[k], [1, 0, -1]).reshape(nCells, -1)
        resp_matrix_small = np.transpose(real_dict_small[k], [1, 0, -1]).reshape(nCells, -1)
        resp_matrix_big = np.transpose(real_dict_big[k], [1, 0, -1]).reshape(nCells, -1)
        pred_matrix = np.transpose(pred_dict_all[k], [1, 0, -1]).reshape(nCells, -1)
    else:
        resp_matrix = np.concatenate((resp_matrix, np.transpose(real_dict_all[k], [1, 0, -1]).reshape(nCells, -1)), axis=-1)
        resp_matrix_small = np.concatenate((resp_matrix_small, np.transpose(real_dict_small[k], [1, 0, -1]).reshape(nCells, -1)), axis=-1)
Пример #3
0
    fs)
path = '/auto/users/hellerc/results/nat_pupil_ms/pr_recordings/'
low = 0.5
high = 2

cells, _ = parse_cellid(ops)
rec = generate_state_corrected_psth(batch=batch,
                                    modelname=xmodel,
                                    cellids=cells,
                                    siteid=site,
                                    cache_path=path,
                                    recache=False)

epochs = [e for e in rec.epochs.name.unique() if 'STIM' in e]
dresp = rec['resp'].extract_epochs(epochs)
zresp = cpreproc.zscore_per_stim(dresp, d2=None)
zresp = rec['resp'].replace_epochs(zresp, mask=rec['mask'])
rec['zresp'] = zresp

ff_residuals = rec['zresp']._data.copy()
nan_idx = np.isnan(ff_residuals[0, :])
ff_residuals[:, nan_idx] = 0
ff_residuals = bandpass_filter_resp(ff_residuals,
                                    low,
                                    high,
                                    fs=fs,
                                    boxcar=True)
rec['ff_residuals'] = rec['resp']._modified_copy(ff_residuals)
rec = rec.apply_mask(reset_epochs=True)

raw_residual = rec['zresp']._data
    for e in epochs
}

if get_first_order_residual:
    rec['resp'] = rec['resp']._modified_copy(rec['resp']._data -
                                             rec['pred0']._data)

# raw data
real_dict_small = rec['resp'].extract_epochs(epochs,
                                             mask=rec['mask_small'],
                                             allow_incomplete=True)
real_dict_big = rec['resp'].extract_epochs(epochs,
                                           mask=rec['mask_large'],
                                           allow_incomplete=True)
real_dict_small = cpreproc.zscore_per_stim(real_dict_small,
                                           d2=real_dict_small,
                                           with_std=zscore)
real_dict_big = cpreproc.zscore_per_stim(real_dict_big,
                                         d2=real_dict_big,
                                         with_std=zscore)
eps = list(real_dict_big.keys())
nCells = real_dict_big[eps[0]].shape[1]
for i, k in enumerate(real_dict_big.keys()):
    if i == 0:
        resp_matrix_small = np.transpose(real_dict_small[k][:, :, bins[k]],
                                         [1, 0, -1]).reshape(nCells, -1)
        resp_matrix_big = np.transpose(real_dict_big[k][:, :, bins[k]],
                                       [1, 0, -1]).reshape(nCells, -1)
    else:
        resp_matrix_small = np.concatenate(
            (resp_matrix_small,
Пример #5
0
    # remove epochs
    if batch in [294, 331]:
        epochs = [
            epoch for epoch in rec['resp'].epochs.name.unique()
            if ('STIM_' in epoch) & ('Pips' not in epoch)
        ]
    else:
        epochs = [
            epoch for epoch in rec['resp'].epochs.name.unique()
            if 'STIM_00' in epoch
        ]
    rec = rec.and_mask(epochs)

    all_dict = rec['resp'].extract_epochs(epochs, mask=rec['mask'])
    all_dict = cpreproc.zscore_per_stim(all_dict, d2=all_dict, with_std=zscore)
    rec['resp'] = rec['resp'].replace_epochs(all_dict, mask=rec['mask'])

    # remove spont
    rec = rec.and_mask(['PreStimSilence', 'PostStimSilence'], invert=True)

    # delta noise corr. axes
    rec_bp = rec.copy()
    ops = {
        'state': 'big',
        'method': 'median',
        'epoch': ['REFERENCE'],
        'collapse': True
    }
    rec_bp = create_pupil_mask(rec_bp, **ops)
    ops = {