Esempio n. 1
0
def test_glm_any_model():
    nbin = 10
    align = 'hold'
    lag = 0.1 # s

    ds = datasets['small']
    dc = DataCollection(ds.get_files())
    dc.add_unit(ds.get_units()[0], lag)
    bnd = dc.make_binned(nbin=nbin, align=align)
    ntask, nrep, nunit, nbin = bnd.shape

    # make perfect test counts
    # direction-only model
    tp = radians([20, 10]) # degrees
    b0 = log(10) # log(Hz)
    pd = pol2cart(tp)

    drn = kinematics.get_idir(bnd.pos, axis=2)
    rate = exp(dot(drn, pd) + b0)
    window_size = diff(bnd.bin_edges, axis=2)
    count_mean = rate * window_size
    count = poisson(count_mean)
    count = count[:,:,None]
    bnd.set_PSTHs(count)

    # now fit data for pd
    count, pos, time = bnd.get_for_regress()
    bnom, bse_nom = glm_any_model(count, pos, time, model='kd')
    pd_exp = unitvec(bnom['d'])
    tp_exp = cart2pol(pd_exp)
    
    acceptable_err = 0.05 # about 3 degrees absolute error
    err = abs(tp - tp_exp)
    assert_array_less(err, acceptable_err)
Esempio n. 2
0
def test_gam_runs_on_simple():
    ds = datasets['small']
    unit = ds.get_units()[0]

    threshold = 5. # Hz
    lag = 0.1 # seconds
    nbin = 10 # bins
    align = 'hold'
    ncv = 4

    dc = DataCollection(ds.get_files())
    dc.add_unit(unit, lag)
    bnd = dc.make_binned(nbin=nbin, align=align, do_rate=True)
    # need to change this to do_rate=False once running on counts is fixed

    data = gam.format_data_wrap(bnd.unbiased_rate, bnd.bin_edges, bnd.pos, drop=True)
    gamres = gam.GAMResult(gam.fit_CV(data, 4, 50))
Esempio n. 3
0
def test_gam_predict_cv():
    dsname = 'frank-osmd'
    align = 'hold'
    lag = 0.1 # s
    b0 = 20 # Hz
    noise_sd = 1. # Hz
    b_scale = 10.
    nbin = 10

    ds = datasets[dsname]
    dc = DataCollection(ds.get_files()[:5])
    unit = ds.get_units()[0]
    dc.add_unit(unit, lag)
    bnd = dc.make_binned(nbin=nbin, align=align, do_count=True)
    ntask, nrep, nedge, ndim = bnd.pos.shape
    
    pos = bnd.pos.reshape(ntask * nrep, nedge, -1)
    drn = kin.get_dir(pos)
    
    # simplest model
    # y = b0 + Bd.D
    B = np.array([.2, .6, .4]) * b_scale
    y = b0 + np.dot(drn, B)
    
    noise = np.random.normal(0, noise_sd, size=y.shape)
    y += noise
    
    bnd.set_count_from_flat(y[:,None])
    out = gam_predict_cv(bnd, ['kd'], [dsname, unit, lag, align], \
        family='gaussian')
    
    have = np.mean(out.coef[0], axis=0)[:4]
    want = np.array([b0, B[0], B[1], B[2]])

    np.testing.assert_array_almost_equal(have, want, decimal=1)

    # changing Bdt model, gaussian noise model
    # y = b0 + Bdt.D
    gc_type = [('A', float), ('fwhm', float), ('c', float)]
    gcoeff = np.array([[(1., 1.5, 3.), (.75, 2.5, 6.)],
                       [(.4, 4.,  2.), (.1,  2.,  5.)],
                       [(.8, 3.,  5.), (.6, 4.5, 6.5)]], dtype=gc_type)
    gcoeff = gcoeff.view(np.recarray)

    x  = np.linspace(0, 10, nbin)
    Bt = np.zeros((3, nbin))

    for Bkt, gc in zip(Bt, gcoeff):
        g0 = gauss1d(x, gc.A[0], gc.fwhm[0], gc.c[0])
        g1 = gauss1d(x, gc.A[1], gc.fwhm[1], gc.c[1])
        Bkt[:] = g0 + g1
        
    Bt *= b_scale
    y = b0 + np.sum(Bt.T[None] * drn, axis=-1)
    y = np.random.normal(y, noise_sd)
    bnd.set_count_from_flat(y[:,None])
    out = gam_predict_cv(bnd, ['kdX'], [dsname, unit, lag, align], \
        family='gaussian')
        
    # check that actual and predicted are correlated
    x = out.actual.flatten()
    y = out.pred[8].flatten()
    mc, rp = pearsonr(x,y)
    np.testing.assert_approx_equal(mc, 1, significant=1)