Example #1
0
def get_r2c_sim(coords):
    dims = [
        'est',
        'sim',
        'r2',
        'sig2',
        'snr',
        'ms',
        'ns',
    ]
    res = xr.DataArray(np.zeros(tuple(len(c) for c in coords)),
                       coords=coords,
                       dims=dims)
    res = res.stack(c=dims[2:])
    for i in range(res.shape[-1]):
        [r2, sig2, snr, m, n] = res[:, :, i].coords['c'].values.tolist()
        mu2y = SNR_to_mu2(snr, sig2, m, n)
        theta = [r2, sig2, mu2y, mu2y, m, n]
        x, y = rc.pds_n2m_r2c(theta, n_exps, ddof=1)
        x = x.squeeze()[np.newaxis, np.newaxis]
        r2c, r2 = rc.r2c_n2m(x, y)

        res[..., i] = np.array([r2, r2c]).squeeze()
    res = res.unstack()

    return res
Example #2
0
def get_ci(x,
           y,
           n_r2c_eval=50,
           n_r2c_sims=500,
           r2c_range=[0, 1],
           alpha_targ=0.1,
           trunc_sig2=[0, np.inf],
           trunc_d2=[0, np.inf]):
    #get confidence intervals
    r2c_hat_obs = rc.r2c_n2m(x.squeeze(), y)[0]
    #r2c_hat_obs = trunc_ud(1, 0, r2c_hat_obs)
    trace, p = sample_post_s2_d2(
        y, n_samps=1000, trunc_sig2=trunc_sig2,
        trunc_d2=trunc_d2)  # get posterior dist of params
    sig2_post = trace[1]  #trial-to-trial variance
    d2m_post = trace[0] * m  #dynamic range
    sample_inds = np.random.choice(
        len(sig2_post), size=n_r2c_sims,
        replace=True)  #randomly sample from post-dist

    #get distribution of r2er_hat
    res = np.zeros([n_r2c_eval, n_r2c_sims])
    r2cs = np.linspace(r2c_range[0], r2c_range[1], n_r2c_eval)
    for i, r2c in (enumerate(r2cs)):
        for j in range(n_r2c_sims):
            k = sample_inds[j]
            theta = [r2c, sig2_post[k], d2m_post[k], d2m_post[k], m, n]
            x, y = pds_n2m_r2c(theta, 1, ddof=1)
            res[i, j] = rc.r2c_n2m(x.squeeze(), y)[0]

    alpha_obs = np.mean(res > r2c_hat_obs, 1)

    ll_dif = np.abs(alpha_obs - alpha_targ / 2)
    ll_ind = np.argmin(ll_dif)
    if ll_dif[ll_ind] < alpha_targ:
        ll = r2cs[ll_ind]
    else:
        ll = 0

    ul_dif = np.abs(alpha_obs - (1 - alpha_targ / 2))
    ul_ind = np.argmin(ul_dif)
    if ul_dif[ul_ind] < alpha_targ:
        ul = r2cs[ul_ind]
    else:
        ul = 1
    return ll, ul, alpha_obs, r2c_hat_obs, res, trace
def r2c_n2m_ci_from_post(trace,
                         n,
                         m,
                         r2c_hat_obs,
                         alpha_targ=0.10,
                         nr2cs=50,
                         nr2c_hat=1000):

    sig2s = trace[0]
    mu2ys = trace[1] * m
    sample_inds = np.random.choice(len(sig2s), size=nr2c_hat, replace=False)
    n_exps = 1
    ress = []
    r2s = np.linspace(0, 1, nr2cs)
    for r2 in r2s:
        res = []
        for i in range(nr2c_hat):
            i = sample_inds[i]
            theta = [r2, sig2s[i], mu2ys[i], mu2ys[i], m, n]
            x, y = rc.pds_n2m_r2c(theta, n_exps, ddof=1)
            res.append(rc.r2c_n2m(x.squeeze(), y)[0])
        ress.append(np.array(res).squeeze())
    ress = np.array(ress)

    tol = alpha_targ
    alpha_obs = np.mean(ress > r2c_hat_obs, 1)

    ll_dif = np.abs(alpha_obs - alpha_targ / 2)
    ll_ind = np.argmin(ll_dif)
    if ll_dif[ll_ind] < tol:
        ll = r2s[ll_ind]
    else:
        ll = 0

    ul_dif = np.abs(alpha_obs - (1 - alpha_targ / 2))
    ul_ind = np.argmin(ul_dif)
    if ul_dif[ul_ind] < tol:
        ul = r2s[ul_ind]
    else:
        ul = 1

    return ll, ul, alpha_obs
def get_emp_dist_r2er(r2c_check,
                      r2c_hat_obs,
                      trace,
                      m,
                      n,
                      p_thresh=0.01,
                      n_r2c_sims=100):
    sig2_post = trace[1]  #trial-to-trial variance
    d2m_post = trace[0] * m  #dynamic range

    sample_inds = np.random.choice(
        len(sig2_post), size=n_r2c_sims,
        replace=True)  #randomly sample from post-dist

    res = np.zeros(n_r2c_sims)
    for j in range(n_r2c_sims):
        k = sample_inds[j]
        theta = [r2c_check, sig2_post[k], d2m_post[k], d2m_post[k], m, n]
        x, y = pds_n2m_r2c(theta, 1, ddof=1)
        res[j] = (rc.r2c_n2m(x.squeeze(), y)[0]).squeeze()
    return res
def get_ci(x,
           y,
           n_r2c_sims=1000,
           alpha_targ=0.1,
           p_thresh=0.01,
           n_splits=6,
           trunc_sig2=[0, np.inf],
           trunc_d2=[0, np.inf]):
    #get confidence intervals
    n, m = y.shape
    r2c_hat_obs = rc.r2c_n2m(x.squeeze(), y)[0]
    trace, p = sample_post_s2_d2(
        y, n_samps=2000, trunc_sig2=trunc_sig2,
        trunc_d2=trunc_d2)  # get posterior dist of params

    ul, ul_alpha = find_cdf_pos(r2c_hat_obs,
                                alpha_targ / 2,
                                trace,
                                m,
                                n,
                                n_splits=n_splits,
                                p_thresh=p_thresh,
                                n_r2c_sims=n_r2c_sims,
                                int_l=0,
                                int_h=1)
    ll, ll_alpha = find_cdf_pos(r2c_hat_obs,
                                1 - alpha_targ / 2,
                                trace,
                                m,
                                n,
                                n_splits=n_splits,
                                p_thresh=p_thresh,
                                n_r2c_sims=n_r2c_sims,
                                int_l=0,
                                int_h=1)

    return ll, ul, r2c_hat_obs, trace, ll_alpha, ul_alpha
    [np.cos(theta), np.sin(theta),
     np.ones((len(theta), 1))], -1)
pred_0 = np.dot(
    a,
    np.linalg.lstsq(a, s.mean('trial_tot').sel(unit=0).values.T,
                    rcond=None)[0])
pred_1 = np.dot(
    a,
    np.linalg.lstsq(a, s.mean('trial_tot').sel(unit=1).values.T,
                    rcond=None)[0])
pred_null = np.dot(
    a,
    np.linalg.lstsq(a, resp.mean('trial').values.T, rcond=None)[0])

pred_0_fit = np.squeeze([
    rc.r2c_n2m(a_pred, aunit.values)[0]
    for a_pred, aunit in zip(pred_0.T, s.sel(unit=0))
])
pred_1_fit = np.squeeze([
    rc.r2c_n2m(a_pred, aunit.values)[0]
    for a_pred, aunit in zip(pred_1.T, s.sel(unit=1))
])

pred_null_fit = np.squeeze([
    rc.r2c_n2m(a_pred, aunit.values)[0]
    for a_pred, aunit in zip(pred_null.T, resp)
])

snr = ((s.mean('trial_tot').var('dir') / s.var('trial_tot').mean('dir')))

snr_null = (resp.mean('trial').var('stim') / resp.var('trial').mean('stim'))
n_splits = 10
n_r2c_sims = 1000
p_thresh = 0.001
n_r2c_sims = 300
alpha_targ = 0.2
trunc_sig2 = [0.1, 2]
trunc_d2 = [0.1, 2]
n_exps = 200
r2 = 0.5

#%%
theta = [r2, sig2, 1, mu2y, m, n]
x, y = pds_n2m_r2c(theta, 1, ddof=1)
#get confidence intervals
n, m = y.shape
r2c_hat_obs = rc.r2c_n2m(x.squeeze(), y)[0].squeeze()
trace, p = sample_post_s2_d2(y,
                             n_samps=2000,
                             trunc_sig2=trunc_sig2,
                             trunc_d2=trunc_d2)  # get posterior dist of params
#%%
r2c_check = r2c_hat_obs
res = get_emp_dist_r2er(r2c_check,
                        r2c_hat_obs,
                        trace,
                        m,
                        n,
                        p_thresh=0.01,
                        n_r2c_sims=1000)

r2_qs = np.quantile(res, [0, 0.33, 0.5, 0.66, 1])
 r2cs = []
 r2s = []   
 for l, m in enumerate(ms):
     for j, r2 in enumerate(r2sims):
         theta = [r2, sig2, mu2y, mu2y, m, n]
         [x, y] = rc.pds_n2m_r2c(theta, n_exps, ddof=1)
         res = []
         for i in range(y.shape[0]):
             a_y = y[i]
             mod = np.zeros((len(x), 2))
             mod[:,0] = 1
             mod[:, 1] = x.squeeze()
             beta = np.linalg.lstsq(mod, a_y.mean(0), rcond=-1)[0]
             y_hat = np.dot(beta[np.newaxis], mod.T).squeeze()
             
             r2c, r2 = rc.r2c_n2m(x.T, a_y)  
             r2_pc = rc.r2_SE_corrected(x.squeeze(), a_y)
             r2_upsilon = rc.upsilon(y_hat, a_y)
             r2_hsu = rc.cc_norm_split(x.squeeze(), a_y)**2
             r2_yd = rc.r2_SB_normed(x.squeeze(), a_y)
             r2_sl = rc.normalized_spe(y_hat, a_y)
             r2_sc = rc.cc_norm(x.squeeze(), a_y)**2
             r2_zyl = rc.cc_norm_bs(x.squeeze(), a_y)**2
             res.append([np.double(r2.squeeze()), 
                         np.double(r2c.squeeze()), 
                         r2_pc, 
                         r2_upsilon, 
                         r2_yd, 
                         r2_hsu, 
                         r2_sl, 
                         r2_sc,
Example #9
0
os.chdir('../../')
import r2c_common as r2c
os.chdir(cwd)
import pandas as pd


s = xr.open_dataarray('./mt_sqrt_spkcnt.nc')
snr = ((s.mean('trial_tot').var('dir')/
       s.var('trial_tot').mean('dir')))

theta = np.deg2rad(s.coords['dir'].values)[:,np.newaxis]
sin_mod = np.concatenate([np.cos(theta), np.sin(theta),np.ones((len(theta), 1))], -1)
coefs = np.linalg.lstsq(sin_mod, s.mean('trial_tot').values.T)[0]

pred = np.dot(sin_mod, coefs)
r2er = np.squeeze([r2c.r2c_n2m(a_pred, aunit.values)[0] 
              for a_pred, aunit in zip(pred.T, s)])

theta_fine = np.deg2rad(np.linspace(0,s.coords['dir'].values[-1]))[:,np.newaxis]
sin_mod_fine = np.concatenate([np.cos(theta_fine), 
                         np.sin(theta_fine),
                         np.ones((len(theta_fine), 1))], -1)

pred_fine = np.dot(sin_mod_fine,coefs)


os.chdir(cwd)
#os.chdir('../../')

p = pd.read_csv('./fits.csv', index_col=0)    
p['ciw'] = p['ul'] - p['ll']
Example #10
0
ss.to_netcdf('./mt_sqrt_spkcnt.nc')

#%%
os.chdir(cwd)
s = xr.open_dataarray('./mt_sqrt_spkcnt.nc')
snr = ((s.mean('trial_tot').var('dir') / s.var('trial_tot').mean('dir')))

theta = np.deg2rad(s.coords['dir'].values)[:, np.newaxis]
sin_mod = np.concatenate(
    [np.cos(theta), np.sin(theta),
     np.ones((len(theta), 1))], -1)
coefs = np.linalg.lstsq(sin_mod, s.mean('trial_tot').values.T, rcond=None)[0]

pred = np.dot(sin_mod, coefs)
r2er = np.squeeze(
    [r2c.r2c_n2m(a_pred, aunit.values)[0] for a_pred, aunit in zip(pred.T, s)])

dat = np.zeros((len(s.coords['rec']), 4))

p = pd.DataFrame(dat, columns=['r2er', 'r2', 'll', 'ul'])

for i in range(dat.shape[0]):
    x = pred[:, i]
    y = s[i].values
    ll, ul, r2c_hat_obs, alpha_obs = r2c.r2c_n2m_ci(x,
                                                    y,
                                                    alpha_targ=0.10,
                                                    nr2cs=100)

    r2 = np.corrcoef(y.mean(0), x)[0, 1]**2
Example #11
0
sig2 = 0.25
snr = 2
m = 30
n = 5
n_exps = 100
n_bs_samples = 500

ncps = snr * m
mu2y = mu2x = ncps * sig2
theta = [r2, sig2, mu2y, mu2y, m, n]
x, ys = pds_n2m_r2c(theta, n_exps, ddof=1)

res = []
for exp in tqdm(range(n_exps)):
    y = ys[exp]
    r2_er_obs = rc.r2c_n2m(x.squeeze(), y)[0]
    y_news = []
    for k in range(n_bs_samples):
        y_new = np.array([np.random.choice(y_obs, size=n) for y_obs in y.T]).T
        y_news.append(y_new)
    y_news = np.array(y_news)
    r2c_bs = rc.r2c_n2m(x.squeeze(), y_news)[0].squeeze()
    res.append([r2c_bs, r2_er_obs])

#%%

r2c_bs = np.array([a_res[0] for a_res in res])
cis = np.quantile(r2c_bs, [0.1, 0.9], 1).T
in_ci = (cis[:, 0] <= r2) * (cis[:, 1] >= r2) * (cis[:, 0] != cis[:, 1])
#%%
cis = np.array([r[:2] for r in res])
Example #12
0
snr = 0.5
m = 40
n = 4
ncps = snr * m
mu2y = mu2x = ncps * sig2
r2c_range = [0, 1]
alpha_targ = 0.2
n_r2c_eval = 30
n_r2c_sims = 500
r2s = np.linspace(0, 1, 5)
n_exps = 1000

r2 = 1
theta = [r2, sig2, mu2x, mu2y, m, n]
x, y = pds_n2m_r2c(theta, n_exps, ddof=1)  # simulate experiment
r2c_hat_obs = np.squeeze(rc.r2c_n2m(x.squeeze(), y)[0])
plt.subplot(212)

plt.hist(r2c_hat_obs,
         cumulative=True,
         histtype='step',
         bins=10000,
         density=True,
         range=[-1, 2],
         color='b')
plt.xlabel(r'$\hat{r}^2_{ER}$')
#plt.title(np.round(np.median(r2c_hat_obs),4))

q = np.quantile(r2c_hat_obs, 0.9)
plt.plot([q, q], [0, 1], c='b')