Example #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)
def test_cart2pol():
    v = np.array([0., 0., 1.])
    np.testing.assert_equal(cart2pol(v), np.array([0.,0.]))

    v = np.array([0., 0., -1.])
    np.testing.assert_equal(cart2pol(v), np.array([np.pi, 0.]))

    v = np.array([1., 0., 0.])
    np.testing.assert_equal(cart2pol(v), np.array([np.pi/2., 0.]))

    v = np.array([1., 1., 0.])
    v /= norm(v)[...,None]
    np.testing.assert_equal(cart2pol(v), np.array([np.pi/2., np.pi/4.]))
    
    v = np.array([0., 1., 0.])
    v /= norm(v)[...,None]
    np.testing.assert_equal(cart2pol(v), np.array([np.pi/2., np.pi/2.]))

    v = np.array([[0., 0., -1.],[1., 0., 0.]]) # shape 2,3
    exp = np.array([[np.pi, 0.], [np.pi/2., 0.]])
    np.testing.assert_equal(cart2pol(v), exp)

    v = np.array([[0., 0., -1.],[1., 0., 0.]]).T # shape 2,3
    exp = np.array([[np.pi, 0.], [np.pi/2., 0.]]).T
    np.testing.assert_equal(cart2pol(v, axis=0), exp)
def test_pol2cart_cart2pol():
    v = np.random.random(size=(2, 3))
    v /= norm(v, axis=1)[...,None]
    np.testing.assert_almost_equal(v, pol2cart(cart2pol(v)))
Example #4
0
def plot_gem(data, order='co', fig=None, ax=None,
             clim=None, **kwargs):
    '''
    Parameters
    ----------
    data : ndarray
      shape (26,)
    order : string, optional
      one of 'co' or 'oc', for center-out or out-center data order
      determines mapping of data to polygons
      defaults to center-out
    fig : matplotlib Figure instance
      an existing figure to use, optional
    ax : SplitLambertAxes instance
      an existing axis to use, optional
    clim : tuple or list of float, optional
      minimum and maximum values for color map
      if not supplied, min and max of data are used
    '''
    if not data.shape == (26,):
        raise ValueError('data has wrong shape; should be (26,),'
                         ' actually is %s' % (str(data.shape)))
    if order == 'co':
        mapping = get_targ_co_dir_mapping()
    elif order == 'oc':
        mapping = get_targ_oc_dir_mapping()
    else:
        raise ValueError('`order` not recognized')
    if (fig != None) & ~isinstance(fig, Figure):
        raise ValueError('`fig` must be an instance of Figure')
    if (ax != None) & \
            ~isinstance(ax, split_lambert_projection.SplitLambertAxes):
        raise ValueError('`ax` must be an instance of SplitLambertAxes')
    if (clim != None):
        if (type(clim) != tuple) and (type(clim) != list):
            raise ValueError('clim must be tuple or list')
        if len(clim) != 2:
            raise ValueError('clim must have 2 elements')
    else:
        clim = (np.min(data), np.max(data))
        
    if (fig == None):
        if (ax != None):
            fig = ax.figure
        else:
            fig = plt.figure()
    if (ax == None):
        ax = fig.add_subplot(111, projection='split_lambert')
    
    pts, spts, circs = make_26_targets()
    pts = pts.astype(float)
    pts /= norm(pts, axis=1)[:,None]
    tps = cart2pol(pts)

    q = 9
    cnrs = np.zeros((32, 5, 2)) # for non-polar polys
    ends = np.zeros((2, q, 2))  # for poles
    
    # for now use boundaries suggested by square ends
    squares = np.unique(tps[circs[0]][:,0])
    mids = np.mean(rolling_window(squares, 2), axis=1)
    eps = 1e-8
    tvs = np.hstack([mids[:2], np.pi / 2. - eps, np.pi / 2., mids[-2:]])
    pvs = np.linspace(0, 2 * np.pi, q, endpoint=True) % (2 * np.pi) \
        - np.pi / 8.

    k = 0
    rings = [0,1,3,4]
    for i in rings: # 3 rings of 4-cnr polys
        for j in xrange(8): # 8 polys per ring
            xs = np.array((tvs[i], tvs[i], tvs[i+1], tvs[i+1], tvs[i]))
            ys = np.array((pvs[j], pvs[j+1], pvs[j+1], pvs[j], pvs[j]))
            cnrs[k] = np.vstack((xs, ys)).T
            k += 1

    ends[0,:,0] = np.ones(q) * tvs[0]
    ends[1,:,0] = np.ones(q) * tvs[-1]
    ends[0,:,1] = pvs
    ends[1,:,1] = pvs

    kwargs.setdefault('edgecolors', 'none')
    kwargs.setdefault('linewidths', (0.25,))
    coll_cnrs = mcoll.PolyCollection(cnrs, **kwargs)
    coll_ends = mcoll.PolyCollection(ends, **kwargs)
    
    mapped_data = data[mapping]

    coll_cnrs.set_array(mapped_data[:-2])
    coll_ends.set_array(mapped_data[-2:])

    if clim == None:
        cmin, cmax = mapped_data.min(), mapped_data.max()
    else:
        cmin, cmax = clim
    coll_cnrs.set_clim(cmin, cmax)
    coll_ends.set_clim(cmin, cmax)
    
    ax.add_collection(coll_cnrs)
    ax.add_collection(coll_ends)
    return ax