Exemplo n.º 1
0
    def get_projected_speed(self, tno):
        '''calculate speed of trial number `tno`, projected along task direction

        Parameters
        ----------
        tno : int
          index of trial to calculate

        Returns
        -------
        proj : ndarray
          projected speed values, in m/s
          
        Notes
        -----
        we have to do them one-at-a-time because positions has different
        numbers of elements in each trial.        
        '''
        
        trial = self.positions[tno]
        pos = trial[:,0:3]
        time = trial[:,3]
        vel = kin.get_vel(pos, time, tax=0, spax=-1)
        task_dir = unitvec(self.TargetPos[tno] - self.StartPos[tno])
        proj = np.dot(vel, task_dir)
        return proj
Exemplo n.º 2
0
def _format_for_gam2(count, time, pos):
    assert np.rank(count) == np.rank(time) == (np.rank(pos) - 1)
    assert count.shape[0] == time.shape[0] == pos.shape[0]
    assert (count.shape[1] + 1) == time.shape[1] == pos.shape[1]

    y = count.flatten()[:,None]
    npt = y.size
    tax, spax = 1, -1
    
    # don't use real time as won't compare equivalent portions of trials
    # t  = edge2cen(time, axis=tax) # (ntrial, nbin)
    # subtract offset to get all relative times
    #t = (t - t[:,:1]).flatten()[:,None]
    
    # instead use bin numbers
    ntrial, nbin = count.shape
    t = np.tile(np.arange(nbin, dtype=float)[None], (ntrial, 1))
    t = t.flatten()[:,None]

    # also create trial numbers for pulling out appropriate later on
    # these don't correspond to original trial numbers because original trials
    # have been permuted before getting here
    tr = np.tile(np.arange(ntrial), (nbin, 1)).T.flatten()
    
    d  = kin.get_dir(pos, tax=tax, spax=spax).reshape(npt, 3)
    p  = edge2cen(pos, axis=tax).reshape(npt, 3)
    v  = kin.get_vel(pos, time, tax=tax, spax=spax).reshape(npt, 3)
    sp = kin.get_speed(pos, time, tax=tax, spax=spax).flatten()[:,None]

    # q is a second direction set-of-columns for deviance calculation
    q  = kin.get_dir(pos, tax=tax, spax=spax).reshape(npt, 3)
    return np.concatenate([y,t,d,p,v,sp,q], axis=1), tr
Exemplo n.º 3
0
def _format_for_gam(count, time, pos):
    '''
    Format data for gam_predict_cv, i.e. an (n, 12) array
    
    Parameters
    ----------
    count : ndarray
      spike counts, shape (ntrial, nbin)
    time : ndarray
      bin_edges, shape (ntrial, nbin + 1)
    pos : ndarray
      positions at `bin_edges`, shape (ntrial, nbin + 1, 3)
    
    Returns
    -------
    formatted_data : ndarray
      shape (ntrial * nbin, 12)
      dimension 1 is [count, t, dx, dy, dz, px, py, pz, vx, vy, vz, sp]
    '''        
    assert np.rank(count) == np.rank(time) == (np.rank(pos) - 1)
    assert count.shape[0] == time.shape[0] == pos.shape[0]
    assert (count.shape[1] + 1) == time.shape[1] == pos.shape[1]

    y  = count.flatten()[:,None]
    npt = y.size
    tax, spax = 1, -1
    
    # don't use real time as won't compare equivalent portions of trials
    # t  = edge2cen(time, axis=tax) # (ntrial, nbin)
    # subtract offset to get all relative times
    #t = (t - t[:,:1]).flatten()[:,None]
    
    # instead use bin numbers
    ntrial, nbin = count.shape
    t = np.tile(np.arange(nbin, dtype=float)[None], (ntrial, 1))
    t = t.flatten()[:,None]
    
    d  = kin.get_dir(pos, tax=tax, spax=spax).reshape(npt, 3)
    p  = edge2cen(pos, axis=tax).reshape(npt, 3)
    v  = kin.get_vel(pos, time, tax=tax, spax=spax).reshape(npt, 3)
    sp = kin.get_speed(pos, time, tax=tax, spax=spax).flatten()[:,None]

    # q is a second direction set-of-columns for deviance calculation
    q  = kin.get_dir(pos, tax=tax, spax=spax).reshape(npt, 3)
    return np.concatenate([y,t,d,p,v,sp,q], axis=1)
Exemplo n.º 4
0
def prepare_regressors(count, pos, time, model=''):
    '''
    Parameters
    ----------
    count : array_like
      firing rates, shape (ntrial, nbin)
    pos : array_like
      positional data, shape (ntrial, nbin + 1, ndim)
      these are positions at bin edges
    time : array_like
      bin edge times, shape (ntrial, nbin + 1)
    model : string
      model

    Returns
    -------
    reg_vars : ndarray

    reg_frs : ndarray
    '''
    count = np.asarray(count)
    assert(np.rank(count) == 2)
    ntrial, nbin = count.shape
    assert(not np.any(np.isnan(count)))
    pos = np.asarray(pos)
    assert(np.rank(pos) == 3)
    assert(pos.shape[0:2] == (ntrial, nbin + 1))
    time = np.asarray(time)
    assert(np.rank(time) == 2)
    assert(time.shape == (ntrial, nbin + 1))
    assert(type(model) == str)
                      
    #ntrial, nbin = count.shape
    ndim = pos.shape[-1]
    nvar = ntrial * nbin
    endog = count.reshape(nvar) # should be equivalent to flatten
    vels = None
    exog = None

    # add the time-invariant variables
    if 'p' in model:
        pos_flat = pos[:,1:,:].reshape(nvar, ndim)
        exog = add_column(exog, pos_flat)
    if 's' in model:
        if vels == None:
            vels = get_vel(pos, time, tax=1, spax=2)
        spds = np.sqrt(np.sum(vels**2, axis=2))
        spds_flat = spds.reshape(nvar)
        exog = add_column(exog, spds_flat)

    # do the potentially time-variant variables
    if ('d' in model or 'v' in model):
        if 'd' in model:
            var = get_dir(pos, tax=1, spax=2)
        elif 'v' in model:
            if vels == None:
                vels = get_vel(pos, time, tax=1, spax=2)
            var = vels
        if not 'X' in model: # time-static variable
            var_flat = np.reshape(var, (nvar, ndim))
        else: # time-dynamic variable
            mask = np.eye(nbin)
            var_flat = np.reshape(mask[None,...,None] * var[...,None,:],
                                  (nvar, nbin * ndim))
        exog = add_column(exog, var_flat)

    # do the constant + dynamic direction model
    if 'q' in model:
        assert ('X' in model)
        # for simplicity's sake, let us call this model 'kqX'
        assert ('d' not in model)
        assert ('v' not in model)

    if 'k' in model:
        exog = sm.tools.add_constant(exog, prepend=False)

    offset = np.log(np.diff(time, axis=-1).reshape(nvar))

    return endog, exog, offset