Exemple #1
0
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)
ff_residuals = rec['resp']._data - rec['psth_sp']._data
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()

pupil = rec['pupil']._data
raw_residual = rec['resp']._data - rec['psth_sp']._data
cor_residual = rec['resp']._data - rec['psth']._data

# first, do full PCA on residuals
pca = PCA()
pca.fit(raw_residual.T)
pca_transform = raw_residual.T.dot(pca.components_.T).T

# do first order regression
                'Regress first order pupil by subtracting model pred using pupil pred alone'
            )
            # set the LV to 0 by setting encoding weights to 0 and compute prediction
            ms = ctx['modelspec']
            ms.phi[1]['e'] = np.zeros(ms.phi[1]['e'].shape)
            pred = ms.evaluate(rec)
            mod_data = rec['resp']._data - pred['pred']._data + rec[
                'psth_sp']._data
            rec['resp'] = rec['resp']._modified_copy(mod_data)
        else:
            raise ValueError("No regression method specified!")

    if filt:
        log.info("Band-pass filter spike counts between {0} and {1} Hz".format(
            low_c, high_c))
        rec = preproc.bandpass_filter_resp(rec, low_c, high_c)

    # USE XFORMS PUPIL MASK
    rec_bp = rec.copy()
    rec_bp['mask'] = rec_bp['p_mask']
    rec_sp = rec.copy()
    rec_sp['mask'] = rec_sp['p_mask']._modified_copy(~rec_sp['p_mask']._data)

    rec_bp = rec_bp.apply_mask(reset_epochs=True)
    rec_sp = rec_sp.apply_mask(reset_epochs=True)

    # Determine if need to mask pupil balanced epochs only, or if the
    # xforms model already did this
    if not balanced:
        if batch == 289:
            balanced_eps = preproc.get_pupil_balanced_epochs(
Exemple #3
0
pca = PCA()
pca.fit(raw_residual.T)

f, ax = plt.subplots(1, 3, figsize=(12, 4))

# scree plot
ax[0].plot(pca.explained_variance_ratio_, 'o-', label='raw')
ax[0].set_ylabel('% Variance explained')
ax[0].set_xlabel('PC')
ax[0].legend(frameon=False)

# correlation with pupil
transform = raw_residual.T.dot(pca.components_.T)
shuf_transform = shuff_residual.T.dot(pca.components_.T)
corr = [abs(np.corrcoef(transform[:, i], pupil)[0, 1]) for i in range(residual.shape[0])]
corr_noise_floor = [abs(np.corrcoef(shuf_transform[:, i], pupil)[0, 1]) for i in range(residual.shape[0])]
ax[1].plot(corr, 'o-', label='raw')
ax[1].plot(corr_noise_floor, 'o-', label='shuffled')
ax[1].set_ylabel('Corr. Coef w/ pupil')
ax[1].set_xlabel('PC')
ax[1].legend(frameon=False)

# correlation of power with pupil
transform_filter = bandpass_filter_resp(transform.T, low, high, fs=100)
# TODO convert to power over sliding window...

f.tight_layout()

plt.show()

Exemple #4
0
# compute PCs over all the data
pca = PCA()
pca.fit(resp_matrix.T)

# compute variance of rank1 matrix along each PC
var = np.zeros(resp_matrix.shape[0])
fo_var = np.zeros(pred_matrix.shape[0])
for pc in range(0, resp_matrix.shape[0]):
    var[pc] = np.var(r1_resp.T.dot(pca.components_[pc])) / np.sum(pca.explained_variance_)
    fo_var[pc] = np.var(pred_matrix.T.dot(pca.components_[pc])) / np.sum(pca.explained_variance_)

# project onto first evec and filter
#rm = cpreproc.bandpass_filter_resp(resp_matrix, low_c=low, high_c=high, fs=fs)
proj = resp_matrix.T.dot(evecs[:, 0])[np.newaxis]
proj_filt = proj.copy()
proj_filt = cpreproc.bandpass_filter_resp(proj, low_c=low, high_c=high, fs=fs)

# caculate power in the filtered residual
t, ffsw = sliding_window(proj_filt, fs=fs, window_size=4, step_size=1) 
power = np.sum(ffsw**2, axis=-1) / ffsw.shape[-1] 

t, pds = sliding_window(pupil[np.newaxis, :], fs=fs, window_size=4, step_size=1)
pds = pds.mean(axis=-1)

# plot results
ncells = resp_matrix.shape[0]
f = plt.figure(figsize=(15, 6))
espec = plt.subplot2grid((2, 5), (0, 0))
pcvar = plt.subplot2grid((2, 5), (0, 3), colspan=2)
pax = plt.subplot2grid((2, 5), (1, 0), colspan=3)
betaax = plt.subplot2grid((2, 5), (0, 1))