scripts/model_utils.py
def compute_count_stats(glm, ll_mode, tbin, spktrain, behav_list, neuron, traj_len=None, traj_spikes=None,
                        start=0, T=100000, bs=5000):
    """
    Compute the dispersion statistics, per neuron in a population.
    
    :param string mode: *single* mode refers to computing separate single neuron quantities, *population* mode 
                        refers to computing over a population indicated by neurons, *peer* mode involves the 
                        peer predictability i.e. conditioning on all other neurons given a subset
    """
    N = int(np.ceil(T/bs))
    rate_model = []
    shape_model = []
    spktrain = spktrain[:, start:start+T]
    behav_list = [b[start:start+T] for b in behav_list]
    
    for k in range(N):
        covariates_ = [b[k*bs:(k+1)*bs] for b in behav_list]
        
        if glm.likelihood.filter_len > 1:
            ini_train = spktrain[None, :, :glm.likelihood.filter_len-1]
        else:
            ini_train = np.zeros((1, glm.likelihood.neurons, 1)) # used only for trial count
        
        ospktrain = spktrain[None, :, glm.likelihood.filter_len-1:]

        rate = glm.mapping.eval_rate(covariates_, neuron, n_samp=1000)
        rate_model += [rate[0, ...]]
        
        if glm.likelihood.dispersion_mapping is not None: # heteroscedastic
            disp = glm.likelihood.dispersion_mapping.eval_rate(covariates_, neuron, n_samp=1000)
            shape_model += [disp[0, ...]]
                

    rate_model = np.concatenate(rate_model, axis=1)
    if glm.likelihood.dispersion_mapping is not None: # heteroscedastic
        shape_model = np.concatenate(shape_model, axis=1)
    
    if ll_mode == 'IP':
        shape_model = None
        f_p = lambda c, avg, shape, t: utils.stats.poiss_count_prob(c, avg, shape, t)
    elif ll_mode[-2:] == 'NB':
        if glm.likelihood.dispersion_mapping is None:
            shape_model = glm.likelihood.r_inv.data.cpu().numpy()[:, None].repeat(T, axis=-1)
        f_p = lambda c, avg, shape, t: utils.stats.nb_count_prob(c, avg, shape, t)
    elif ll_mode[-3:] == 'CMP':
        if glm.likelihood.dispersion_mapping is None:
            shape_model = glm.likelihood.nu.data.cpu().numpy()[:, None].repeat(T, axis=-1)
        f_p = lambda c, avg, shape, t: utils.stats.cmp_count_prob(c, avg, shape, t)
    elif ll_mode[-3:] == 'ZIP':
        if glm.likelihood.dispersion_mapping is None:
            shape_model = glm.likelihood.alpha.data.cpu().numpy()[:, None].repeat(T, axis=-1)
        f_p = lambda c, avg, shape, t: utils.stats.zip_count_prob(c, avg, shape, t)
    elif ll_mode == 'U':
        f_p = lambda c, avg, shape, t: utils.stats.zip_count_prob(c, avg, shape, t)
    else:
        raise ValueError
    m_f = lambda x: x

    # compute the activity quantiles
    if shape_model is not None:
        assert traj_len == 1
    if traj_len is not None:
        traj_lens = (T // traj_len) * [traj_len]
        
    q_ = []
    for k, ne in enumerate(neuron):
        if traj_spikes is not None:
            avg_spikecnt = np.cumsum(rate_model[k]*tbin)
            nc = 1
            traj_len = 0
            for tt in range(T):
                if avg_spikecnt >= traj_spikes*nc:
                    nc += 1
                    traj_lens.append(traj_len)
                    traj_len = 0
                    continue
                traj_len += 1
                
        if shape_model is not None:
            sh = shape_model[k]
            spktr = spktrain[ne]
            rm = rate_model[k]
        else:
            sh = None
            spktr = []
            rm = []
            start = np.cumsum(traj_lens)
            for tt, traj_len in enumerate(traj_lens):
                spktr.append(spktrain[ne][start[tt]:start[tt]+traj_len].sum())
                rm.append(rate_model[k][start[tt]:start[tt]+traj_len].sum())
            spktr = np.array(spktr)
            rm = np.array(rm)
                    
        q_.append(utils.stats.count_KS_method(f_p, m_f, tbin, spktr, rm, shape=sh))

    return q_

    Compute the dispersion statistics, per neuron in a population.
    
    :param string mode: *single* mode refers to computing separate single neuron quantities, *population* mode 
                        refers to computing over a population indicated by neurons, *peer* mode involves the 
                        peer predictability i.e. conditioning on all other neurons given a subset
    
Compute the dispersion statistics, per neuron in a population.
Compute the dispersion statistics per neuron in a population .
def compute_count_stats(glm, ll_mode, tbin, spktrain, behav_list, neuron, traj_len=None, traj_spikes=None,
                        start=0, T=100000, bs=5000):
    N = int(np.ceil(T/bs))
    rate_model = []
    shape_model = []
    spktrain = spktrain[:, start:start+T]
    behav_list = [b[start:start+T] for b in behav_list]
    for k in range(N):
        covariates_ = [b[k*bs:(k+1)*bs] for b in behav_list]
        if glm.likelihood.filter_len > 1:
            ini_train = spktrain[None, :, :glm.likelihood.filter_len-1]
        else:
            ini_train = np.zeros((1, glm.likelihood.neurons, 1)) 
        ospktrain = spktrain[None, :, glm.likelihood.filter_len-1:]
        rate = glm.mapping.eval_rate(covariates_, neuron, n_samp=1000)
        rate_model += [rate[0, ...]]
        if glm.likelihood.dispersion_mapping is not None: 
            disp = glm.likelihood.dispersion_mapping.eval_rate(covariates_, neuron, n_samp=1000)
            shape_model += [disp[0, ...]]
    rate_model = np.concatenate(rate_model, axis=1)
    if glm.likelihood.dispersion_mapping is not None: 
        shape_model = np.concatenate(shape_model, axis=1)
    if ll_mode == 'IP':
        shape_model = None
        f_p = lambda c, avg, shape, t: utils.stats.poiss_count_prob(c, avg, shape, t)
    elif ll_mode[-2:] == 'NB':
        if glm.likelihood.dispersion_mapping is None:
            shape_model = glm.likelihood.r_inv.data.cpu().numpy()[:, None].repeat(T, axis=-1)
        f_p = lambda c, avg, shape, t: utils.stats.nb_count_prob(c, avg, shape, t)
    elif ll_mode[-3:] == 'CMP':
        if glm.likelihood.dispersion_mapping is None:
            shape_model = glm.likelihood.nu.data.cpu().numpy()[:, None].repeat(T, axis=-1)
        f_p = lambda c, avg, shape, t: utils.stats.cmp_count_prob(c, avg, shape, t)
    elif ll_mode[-3:] == 'ZIP':
        if glm.likelihood.dispersion_mapping is None:
            shape_model = glm.likelihood.alpha.data.cpu().numpy()[:, None].repeat(T, axis=-1)
        f_p = lambda c, avg, shape, t: utils.stats.zip_count_prob(c, avg, shape, t)
    elif ll_mode == 'U':
        f_p = lambda c, avg, shape, t: utils.stats.zip_count_prob(c, avg, shape, t)
    else:
        raise ValueError
    m_f = lambda x: x
    if shape_model is not None:
        assert traj_len == 1
    if traj_len is not None:
        traj_lens = (T // traj_len) * [traj_len]
    q_ = []
    for k, ne in enumerate(neuron):
        if traj_spikes is not None:
            avg_spikecnt = np.cumsum(rate_model[k]*tbin)
            nc = 1
            traj_len = 0
            for tt in range(T):
                if avg_spikecnt >= traj_spikes*nc:
                    nc += 1
                    traj_lens.append(traj_len)
                    traj_len = 0
                    continue
                traj_len += 1
        if shape_model is not None:
            sh = shape_model[k]
            spktr = spktrain[ne]
            rm = rate_model[k]
        else:
            sh = None
            spktr = []
            rm = []
            start = np.cumsum(traj_lens)
            for tt, traj_len in enumerate(traj_lens):
                spktr.append(spktrain[ne][start[tt]:start[tt]+traj_len].sum())
                rm.append(rate_model[k][start[tt]:start[tt]+traj_len].sum())
            spktr = np.array(spktr)
            rm = np.array(rm)
        q_.append(utils.stats.count_KS_method(f_p, m_f, tbin, spktr, rm, shape=sh))
    return q_
def compute_count_stats ( glm , ll_mode , tbin , spktrain , behav_list , neuron , traj_len = None , traj_spikes = None , start = 0 , T = 100000 , bs = 5000 ) : N = int ( np . ceil ( T / bs ) ) rate_model = [ ] shape_model = [ ] spktrain = spktrain [ : , start : start + T ] behav_list = [ b [ start : start + T ] for b in behav_list ] for k in range ( N ) : covariates_ = [ b [ k * bs : ( k + 1 ) * bs ] for b in behav_list ] if glm . likelihood . filter_len > 1 : ini_train = spktrain [ None , : , : glm . likelihood . filter_len - 1 ] else : ini_train = np . zeros ( ( 1 , glm . likelihood . neurons , 1 ) ) ospktrain = spktrain [ None , : , glm . likelihood . filter_len - 1 : ] rate = glm . mapping . eval_rate ( covariates_ , neuron , n_samp = 1000 ) rate_model += [ rate [ 0 , ... ] ] if glm . likelihood . dispersion_mapping is not None : disp = glm . likelihood . dispersion_mapping . eval_rate ( covariates_ , neuron , n_samp = 1000 ) shape_model += [ disp [ 0 , ... ] ] rate_model = np . concatenate ( rate_model , axis = 1 ) if glm . likelihood . dispersion_mapping is not None : shape_model = np . concatenate ( shape_model , axis = 1 ) if ll_mode == 'IP' : shape_model = None f_p = lambda c , avg , shape , t : utils . stats . poiss_count_prob ( c , avg , shape , t ) elif ll_mode [ - 2 : ] == 'NB' : if glm . likelihood . dispersion_mapping is None : shape_model = glm . likelihood . r_inv . data . cpu ( ) . numpy ( ) [ : , None ] . repeat ( T , axis = - 1 ) f_p = lambda c , avg , shape , t : utils . stats . nb_count_prob ( c , avg , shape , t ) elif ll_mode [ - 3 : ] == 'CMP' : if glm . likelihood . dispersion_mapping is None : shape_model = glm . likelihood . nu . data . cpu ( ) . numpy ( ) [ : , None ] . repeat ( T , axis = - 1 ) f_p = lambda c , avg , shape , t : utils . stats . cmp_count_prob ( c , avg , shape , t ) elif ll_mode [ - 3 : ] == 'ZIP' : if glm . likelihood . dispersion_mapping is None : shape_model = glm . likelihood . alpha . data . cpu ( ) . numpy ( ) [ : , None ] . repeat ( T , axis = - 1 ) f_p = lambda c , avg , shape , t : utils . stats . zip_count_prob ( c , avg , shape , t ) elif ll_mode == 'U' : f_p = lambda c , avg , shape , t : utils . stats . zip_count_prob ( c , avg , shape , t ) else : raise ValueError m_f = lambda x : x if shape_model is not None : assert traj_len == 1 if traj_len is not None : traj_lens = ( T // traj_len ) * [ traj_len ] q_ = [ ] for k , ne in enumerate ( neuron ) : if traj_spikes is not None : avg_spikecnt = np . cumsum ( rate_model [ k ] * tbin ) nc = 1 traj_len = 0 for tt in range ( T ) : if avg_spikecnt >= traj_spikes * nc : nc += 1 traj_lens . append ( traj_len ) traj_len = 0 continue traj_len += 1 if shape_model is not None : sh = shape_model [ k ] spktr = spktrain [ ne ] rm = rate_model [ k ] else : sh = None spktr = [ ] rm = [ ] start = np . cumsum ( traj_lens ) for tt , traj_len in enumerate ( traj_lens ) : spktr . append ( spktrain [ ne ] [ start [ tt ] : start [ tt ] + traj_len ] . sum ( ) ) rm . append ( rate_model [ k ] [ start [ tt ] : start [ tt ] + traj_len ] . sum ( ) ) spktr = np . array ( spktr ) rm = np . array ( rm ) q_ . append ( utils . stats . count_KS_method ( f_p , m_f , tbin , spktr , rm , shape = sh ) ) return q_
Compute the dispersion statistics, per neuron in a population.
Compute the dispersion statistics per neuron in a population .
"""
    Compute the dispersion statistics, per neuron in a population.
    
    :param string mode: *single* mode refers to computing separate single neuron quantities, *population* mode 
                        refers to computing over a population indicated by neurons, *peer* mode involves the 
                        peer predictability i.e. conditioning on all other neurons given a subset
    """ # used only for trial count # heteroscedastic # heteroscedastic # compute the activity quantiles
glm
ll_mode
tbin
spktrain
behav_list
neuron
traj_len
traj_spikes
start
T
bs
mode
single* mode refers to computing separate single neuron quantities, *population* mode
refers to computing over a population indicated by neurons, *peer* mode involves the
peer predictability i.e. conditioning on all other neurons given a subset
single * mode refers to computing separate single neuron quantities * population * mode refers to computing over a population indicated by neurons * peer * mode involves the peer predictability i . e . conditioning on all other neurons given a subset