neuroprob/utils/neural.py
def KDE_behaviour(bins_tuple, covariates, sm_size, L, smooth_modes, dev='cpu'):
    """
    Kernel density estimation of the covariates, with Gaussian kernels.
    """
    dim = len(bins_tuple)
    assert (dim == len(covariates)) and (dim == len(sm_size)) and (dim == len(smooth_modes))
    time_samples = covariates[0].shape[0]
    c_bins = ()
    tg_c = ()
    for k, cov in enumerate(covariates):
        c_bins += (len(bins_tuple[k])-1,)
        tg_c += (np.digitize(cov, bins_tuple[k])-1,)

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

    sm_centre = np.array(sm_size) // 2
    sm_filter = np.ones(tuple(sm_size))
    ones_arr = np.ones_like(sm_filter)
    for d in range(dim):
        size = sm_size[d]
        centre = sm_centre[d]
        L_ = L[d]
        
        if d > 0:
            bin_time = np.swapaxes(bin_time, 0, d)
            ones_arr = np.swapaxes(ones_arr, 0, d)
            sm_filter = np.swapaxes(sm_filter, 0, d)
        
        for k in range(size):
                sm_filter[k, ...] *= np.exp(-0.5*(((k-centre)/L_)**2))*ones_arr[k, ...]
 
        if d > 0:
            bin_time = np.swapaxes(bin_time, 0, d)
            ones_arr = np.swapaxes(ones_arr, 0, d)
            sm_filter = np.swapaxes(sm_filter, 0, d)

    smth_time = smooth_hist(bin_time[None, ...], sm_filter, smooth_modes, dev=dev)
    smth_time /= smth_time.sum() # normalize
    return smth_time[0, ...], bin_time

    Kernel density estimation of the covariates, with Gaussian kernels.
    
Kernel density estimation of the covariates, with Gaussian kernels.
Kernel density estimation of the covariates with Gaussian kernels .
def KDE_behaviour(bins_tuple, covariates, sm_size, L, smooth_modes, dev='cpu'):
    dim = len(bins_tuple)
    assert (dim == len(covariates)) and (dim == len(sm_size)) and (dim == len(smooth_modes))
    time_samples = covariates[0].shape[0]
    c_bins = ()
    tg_c = ()
    for k, cov in enumerate(covariates):
        c_bins += (len(bins_tuple[k])-1,)
        tg_c += (np.digitize(cov, bins_tuple[k])-1,)
    bin_time = np.zeros(tuple(len(bins)-1 for bins in bins_tuple))
    np.add.at(bin_time, tg_c, 1)
    bin_time /= bin_time.sum() 
    sm_centre = np.array(sm_size) // 2
    sm_filter = np.ones(tuple(sm_size))
    ones_arr = np.ones_like(sm_filter)
    for d in range(dim):
        size = sm_size[d]
        centre = sm_centre[d]
        L_ = L[d]
        if d > 0:
            bin_time = np.swapaxes(bin_time, 0, d)
            ones_arr = np.swapaxes(ones_arr, 0, d)
            sm_filter = np.swapaxes(sm_filter, 0, d)
        for k in range(size):
                sm_filter[k, ...] *= np.exp(-0.5*(((k-centre)/L_)**2))*ones_arr[k, ...]
        if d > 0:
            bin_time = np.swapaxes(bin_time, 0, d)
            ones_arr = np.swapaxes(ones_arr, 0, d)
            sm_filter = np.swapaxes(sm_filter, 0, d)
    smth_time = smooth_hist(bin_time[None, ...], sm_filter, smooth_modes, dev=dev)
    smth_time /= smth_time.sum() 
    return smth_time[0, ...], bin_time
def KDE_behaviour ( bins_tuple , covariates , sm_size , L , smooth_modes , dev = 'cpu' ) : dim = len ( bins_tuple ) assert ( dim == len ( covariates ) ) and ( dim == len ( sm_size ) ) and ( dim == len ( smooth_modes ) ) time_samples = covariates [ 0 ] . shape [ 0 ] c_bins = ( ) tg_c = ( ) for k , cov in enumerate ( covariates ) : c_bins += ( len ( bins_tuple [ k ] ) - 1 , ) tg_c += ( np . digitize ( cov , bins_tuple [ k ] ) - 1 , ) bin_time = np . zeros ( tuple ( len ( bins ) - 1 for bins in bins_tuple ) ) np . add . at ( bin_time , tg_c , 1 ) bin_time /= bin_time . sum ( ) sm_centre = np . array ( sm_size ) // 2 sm_filter = np . ones ( tuple ( sm_size ) ) ones_arr = np . ones_like ( sm_filter ) for d in range ( dim ) : size = sm_size [ d ] centre = sm_centre [ d ] L_ = L [ d ] if d > 0 : bin_time = np . swapaxes ( bin_time , 0 , d ) ones_arr = np . swapaxes ( ones_arr , 0 , d ) sm_filter = np . swapaxes ( sm_filter , 0 , d ) for k in range ( size ) : sm_filter [ k , ... ] *= np . exp ( - 0.5 * ( ( ( k - centre ) / L_ ) ** 2 ) ) * ones_arr [ k , ... ] if d > 0 : bin_time = np . swapaxes ( bin_time , 0 , d ) ones_arr = np . swapaxes ( ones_arr , 0 , d ) sm_filter = np . swapaxes ( sm_filter , 0 , d ) smth_time = smooth_hist ( bin_time [ None , ... ] , sm_filter , smooth_modes , dev = dev ) smth_time /= smth_time . sum ( ) return smth_time [ 0 , ... ] , bin_time
Kernel density estimation of the covariates, with Gaussian kernels.
Kernel density estimation of the covariates with Gaussian kernels .
"""
    Kernel density estimation of the covariates, with Gaussian kernels.
    """ # get time spent in each bin # normalize # normalize
bins_tuple
covariates
sm_size
L
smooth_modes
dev