neuroprob/utils/neural.py
def spike_threshold(sample_bin, bin_thres, covariates, cov_bins, spiketimes, direct=False):
    """
    Only include spikes that correspond to bins with sufficient occupancy time. This is useful 
    when using histogram models to avoid counting undersampled bins, which suffer from huge 
    variance when computing the average firing rates in a histogram.
    
    :param float sample_bin: binning time
    :param float bin_thres: only count bins with total occupancy time above bin_thres
    :param list covariates: list of time series that describe animal behaviour
    :param tuple cov_bins: tuple of arrays describing bin boundary locations
    :param list spiketimes: list of arrays containing spike times of neurons
    """
    units = len(spiketimes)
    c_bins = ()
    tg_c = ()
    sg_c = [() for u in range(units)]
    for k, cov in enumerate(covariates):
        c_bins += (len(cov_bins[k])-1,)
        tg_c += (np.digitize(cov, cov_bins[k])-1,)
        for u in range(units):
            sg_c[u] += (np.digitize(cov[spiketimes[u]], cov_bins[k])-1,)

    # get time spent in each bin
    bin_time = np.zeros(tuple(len(bins)-1 for bins in cov_bins))
    np.add.at(bin_time, tg_c, sample_bin)

    # get activity of each bin per neuron
    activity = np.zeros((units,)+tuple(len(bins)-1 for bins in cov_bins))
    a = np.where(bin_time <= bin_thres) # delete spikes in thresholded bins

    # get flattened removal indices
    remove_glob_index = a[0]
    fac = 1
    for k, cc in enumerate(c_bins[:-1]):
        fac *= cc
        remove_glob_index += a[k+1]*fac
        
    # get flattened spike indices
    thres_spiketimes = []
    for u in range(units):
        s_glob_index = sg_c[u][0]
        fac = 1
        for k, cc in enumerate(c_bins[:-1]):
            fac *= cc
            s_glob_index += sg_c[u][k+1]*fac
            
        rem_ind = np.array([], dtype=np.int64)
        for rg in remove_glob_index:
            rem_ind = np.concatenate((rem_ind, np.where(s_glob_index == rg)[0]))
            
        t_spike = np.delete(spiketimes[u], rem_ind)
        thres_spiketimes.append(t_spike)
            
    return thres_spiketimes

    Only include spikes that correspond to bins with sufficient occupancy time. This is useful 
    when using histogram models to avoid counting undersampled bins, which suffer from huge 
    variance when computing the average firing rates in a histogram.
    
    :param float sample_bin: binning time
    :param float bin_thres: only count bins with total occupancy time above bin_thres
    :param list covariates: list of time series that describe animal behaviour
    :param tuple cov_bins: tuple of arrays describing bin boundary locations
    :param list spiketimes: list of arrays containing spike times of neurons
    
Only include spikes that correspond to bins with sufficient occupancy time. This is useful
when using histogram models to avoid counting undersampled bins, which suffer from huge
variance when computing the average firing rates in a histogram.
Only include spikes that correspond to bins with sufficient occupancy time . This is useful when using histogram models to avoid counting undersampled bins which suffer from huge variance when computing the average firing rates in a histogram .
def spike_threshold(sample_bin, bin_thres, covariates, cov_bins, spiketimes, direct=False):
    units = len(spiketimes)
    c_bins = ()
    tg_c = ()
    sg_c = [() for u in range(units)]
    for k, cov in enumerate(covariates):
        c_bins += (len(cov_bins[k])-1,)
        tg_c += (np.digitize(cov, cov_bins[k])-1,)
        for u in range(units):
            sg_c[u] += (np.digitize(cov[spiketimes[u]], cov_bins[k])-1,)
    bin_time = np.zeros(tuple(len(bins)-1 for bins in cov_bins))
    np.add.at(bin_time, tg_c, sample_bin)
    activity = np.zeros((units,)+tuple(len(bins)-1 for bins in cov_bins))
    a = np.where(bin_time <= bin_thres) 
    remove_glob_index = a[0]
    fac = 1
    for k, cc in enumerate(c_bins[:-1]):
        fac *= cc
        remove_glob_index += a[k+1]*fac
    thres_spiketimes = []
    for u in range(units):
        s_glob_index = sg_c[u][0]
        fac = 1
        for k, cc in enumerate(c_bins[:-1]):
            fac *= cc
            s_glob_index += sg_c[u][k+1]*fac
        rem_ind = np.array([], dtype=np.int64)
        for rg in remove_glob_index:
            rem_ind = np.concatenate((rem_ind, np.where(s_glob_index == rg)[0]))
        t_spike = np.delete(spiketimes[u], rem_ind)
        thres_spiketimes.append(t_spike)
    return thres_spiketimes
def spike_threshold ( sample_bin , bin_thres , covariates , cov_bins , spiketimes , direct = False ) : units = len ( spiketimes ) c_bins = ( ) tg_c = ( ) sg_c = [ ( ) for u in range ( units ) ] for k , cov in enumerate ( covariates ) : c_bins += ( len ( cov_bins [ k ] ) - 1 , ) tg_c += ( np . digitize ( cov , cov_bins [ k ] ) - 1 , ) for u in range ( units ) : sg_c [ u ] += ( np . digitize ( cov [ spiketimes [ u ] ] , cov_bins [ k ] ) - 1 , ) bin_time = np . zeros ( tuple ( len ( bins ) - 1 for bins in cov_bins ) ) np . add . at ( bin_time , tg_c , sample_bin ) activity = np . zeros ( ( units , ) + tuple ( len ( bins ) - 1 for bins in cov_bins ) ) a = np . where ( bin_time <= bin_thres ) remove_glob_index = a [ 0 ] fac = 1 for k , cc in enumerate ( c_bins [ : - 1 ] ) : fac *= cc remove_glob_index += a [ k + 1 ] * fac thres_spiketimes = [ ] for u in range ( units ) : s_glob_index = sg_c [ u ] [ 0 ] fac = 1 for k , cc in enumerate ( c_bins [ : - 1 ] ) : fac *= cc s_glob_index += sg_c [ u ] [ k + 1 ] * fac rem_ind = np . array ( [ ] , dtype = np . int64 ) for rg in remove_glob_index : rem_ind = np . concatenate ( ( rem_ind , np . where ( s_glob_index == rg ) [ 0 ] ) ) t_spike = np . delete ( spiketimes [ u ] , rem_ind ) thres_spiketimes . append ( t_spike ) return thres_spiketimes
Only include spikes that correspond to bins with sufficient occupancy time.
Only include spikes that correspond to bins with sufficient occupancy time .
"""
    Only include spikes that correspond to bins with sufficient occupancy time. This is useful 
    when using histogram models to avoid counting undersampled bins, which suffer from huge 
    variance when computing the average firing rates in a histogram.
    
    :param float sample_bin: binning time
    :param float bin_thres: only count bins with total occupancy time above bin_thres
    :param list covariates: list of time series that describe animal behaviour
    :param tuple cov_bins: tuple of arrays describing bin boundary locations
    :param list spiketimes: list of arrays containing spike times of neurons
    """ # get time spent in each bin # get activity of each bin per neuron # delete spikes in thresholded bins # get flattened removal indices # get flattened spike indices
sample_bin
None
bin_thres
only count bins with total occupancy time above bin_thres
only count bins with total occupancy time above bin_thres
covariates
list of time series that describe animal behaviour
list of time series that describe animal behaviour
cov_bins
tuple of arrays describing bin boundary locations
tuple of arrays describing bin boundary locations
spiketimes
list of arrays containing spike times of neurons
list of arrays containing spike times of neurons
direct