Ejemplo n.º 1
0
def disparity_tuning(Einfo,
                     r,
                     used_inds=None,
                     num_dlags=8,
                     fr1or3=3,
                     to_plot=False):

    if used_inds is None:
        used_inds = range(len(r))

    dmat = disparity_matrix(Einfo['dispt'], Einfo['corrt'])
    ND = (dmat.shape[1] - 2) // 2

    # Weight all by their frequency of occurance

    if (fr1or3 == 3) or (fr1or3 == 1):
        frs_valid = Einfo['frs'] == fr1or3
    else:
        frs_valid = Einfo['frs'] > 0

    to_use = frs_valid[used_inds]

    #dmatN = dmat / np.mean(dmat[used_inds[to_use],:], axis=0) * np.mean(dmat[used_inds[to_use],:])
    dmatN = dmat / np.mean(dmat[used_inds[to_use], :],
                           axis=0)  # will be stim rate
    # if every stim resulted in 1 spk, the would be 1 as is
    #nrms = np.mean(dmat[used_inds[to_use],:], axis=0) # number of stimuli of each type
    Xmat = NDNutils.create_time_embedding(dmatN[:, range(ND * 2)],
                                          [num_dlags, 2 * ND, 1])[used_inds, :]
    # uncorrelated response
    Umat = NDNutils.create_time_embedding(dmatN[:, [-2]],
                                          [num_dlags, 1, 1])[used_inds, :]

    #if len(r) > len(used_inds):
    resp = deepcopy(r[used_inds])
    #else:
    #    resp = r

    #Nspks = np.sum(resp[to_use, :], axis=0)
    Nspks = len(
        to_use
    )  # this will end up being number of spikes associated with each stim
    # at different lags, divided by number of time points used. (i.e. prob of spike per bin)
    Dsta = np.reshape(Xmat[to_use, :].T @ resp[to_use],
                      [2 * ND, num_dlags]) / Nspks
    Usta = (Umat[to_use, :].T @ resp[to_use])[:, 0] / Nspks

    # Rudimentary analysis
    best_lag = np.argmax(np.max(Dsta[range(ND), :], axis=0))
    Dtun = np.reshape(Dsta[:, best_lag], [2, ND]).T
    uncor_resp = Usta[best_lag]

    Dinfo = {
        'Dsta': Dsta,
        'Dtun': Dtun,
        'uncor_resp': uncor_resp,
        'best_lag': best_lag,
        'uncor_sta': Usta,
        'disp_list': Einfo['disp_list'][2:]
    }

    if to_plot:
        DU.subplot_setup(1, 2)
        plt.subplot(1, 2, 1)
        DU.plot_norm(Dsta.T - uncor_resp, cmap='bwr')
        plt.plot([ND - 0.5, ND - 0.5], [-0.5, num_dlags - 0.5], 'k')
        plt.plot([-0.5, 2 * ND - 0.5], [best_lag, best_lag], 'k--')
        plt.subplot(1, 2, 2)
        plt.plot(Dtun)
        plt.plot(-Dtun[:, 1] + 2 * uncor_resp, 'm--')

        plt.plot([0, ND - 1], [uncor_resp, uncor_resp], 'k')
        plt.xlim([0, ND - 1])
        plt.show()

    return Dinfo
Ejemplo n.º 2
0
def disparity_tuning(Einfo,
                     r,
                     used_inds=None,
                     num_dlags=8,
                     fr1or3=3,
                     to_plot=False):

    if used_inds is None:
        used_inds = range(len(r))

    dmat = disparity_matrix(Einfo['dispt'], Einfo['corrt'])
    ND = (dmat.shape[1] - 2) // 2

    # Weight all by their frequency of occurance

    if (fr1or3 == 3) or (fr1or3 == 1):
        frs_valid = Einfo['frs'] == fr1or3
    else:
        frs_valid = Einfo['frs'] > 0

    to_use = frs_valid[used_inds]

    dmatN = dmat / np.mean(dmat[used_inds[to_use], :], axis=0) * np.mean(
        dmat[used_inds[to_use], :])
    Xmat = NDNutils.create_time_embedding(dmatN[:, range(ND * 2)],
                                          [num_dlags, 2 * ND, 1])[used_inds, :]
    # uncorrelated response
    Umat = NDNutils.create_time_embedding(dmatN[:, [-2]],
                                          [num_dlags, 1, 1])[used_inds, :]

    #if len(r) > len(used_inds):
    resp = deepcopy(r[used_inds])
    #else:
    #    resp = r

    Nspks = np.sum(resp[to_use, :], axis=0)
    Dsta = np.reshape(Xmat[to_use, :].T @ resp[to_use],
                      [2 * ND, num_dlags]) / Nspks
    Usta = (Umat[to_use, :].T @ resp[to_use])[:, 0] / Nspks

    # Rudimentary analysis
    best_lag = np.argmax(np.max(Dsta[range(ND), :], axis=0))
    Dtun = np.reshape(Dsta[:, best_lag], [2, ND]).T
    uncor_resp = Usta[best_lag]

    Dinfo = {
        'Dsta': Dsta,
        'Dtun': Dtun,
        'uncor_resp': uncor_resp,
        'best_lag': best_lag,
        'uncor_sta': Usta
    }

    if to_plot:
        DU.subplot_setup(1, 2)
        plt.subplot(1, 2, 1)
        DU.plot_norm(Dsta.T - uncor_resp, cmap='bwr')
        plt.plot([ND - 0.5, ND - 0.5], [-0.5, num_dlags - 0.5], 'k')
        plt.plot([-0.5, 2 * ND - 0.5], [best_lag, best_lag], 'k--')
        plt.subplot(1, 2, 2)
        plt.plot(Dtun)
        plt.plot(-Dtun[:, 1] + 2 * uncor_resp, 'm--')

        plt.plot([0, ND - 1], [uncor_resp, uncor_resp], 'k')
        plt.xlim([0, ND - 1])
        plt.show()

    return Dinfo