def hash_from_pattern(m, base=2):
    """
    Calculate for a spike pattern or a matrix of spike patterns
    (provide each pattern as a column) composed of N neurons a
    unique number.


    Parameters
    ----------
    m: np.ndarray or list
        2-dim ndarray
        spike patterns represented as a binary matrix (i.e., matrix of 0's and
        1's).
        Rows and columns correspond to patterns and neurons, respectively.
    base: integer
        The base for hashes calculation.
        Default: 2

    Returns
    -------
    np.ndarray
        An array containing the hash values of each pattern,
        shape: (number of patterns).

    Raises
    ------
    ValueError
        If matrix `m` has wrong orientation.

    Examples
    --------
    With `base=2`, the hash of `[0, 1, 1]` is `0*2^2 + 1*2^1 + 1*2^0 = 3`.

    >>> import numpy as np
    >>> hash_from_pattern([0, 1, 1])
    3

    >>> import numpy as np
    >>> m = np.array([[0, 1, 0, 0, 1, 1, 0, 1],
    ...               [0, 0, 1, 0, 1, 0, 1, 1],
    ...               [0, 0, 0, 1, 0, 1, 1, 1]])

    >>> hash_from_pattern(m)
    array([0, 4, 2, 1, 6, 5, 3, 7])

    """
    m = np.asarray(m)
    n_neurons = m.shape[0]

    # check the entries of the matrix
    if not is_binary(m):
        raise ValueError('Patterns should be binary: 0 or 1')

    # generate the representation
    # don't use numpy - it's upperbounded by int64
    powers = [base ** x for x in range(n_neurons)][::-1]

    # calculate the binary number by use of scalar product
    return np.dot(powers, m)
Beispiel #2
0
    def is_binary(self):
        """
        Checks and returns **True** if given input is a binary input.
        Beware, that the function does not know if the input is binary
        because e.g `to_bool_array()` was used before or if the input is just
        sparse (i.e. only one spike per bin at maximum).
        """

        return is_binary(self.lst_input)
def n_emp_mat(mat, pattern_hash, base=2):
    """
    Count the occurrences of spike coincidence patterns in the given spike
    trains.

    Parameters
    ----------
    mat : (N, M) np.ndarray
        Binned spike trains of N neurons. Rows and columns correspond
        to neurons and temporal bins, respectively.
    pattern_hash: list of int
        List of hash values, representing the spike coincidence patterns
        of which occurrences are counted.
    base: integer
        The base, used to generate the hash values.
        Default: 2

    Returns
    -------
    N_emp: np.ndarray
        The number of occurrences of the given patterns in the given
        spiketrains.
    indices: list of list
        List of lists of int.
        Indices indexing the bins where the given spike patterns are found
        in `mat`. Same length as `pattern_hash`.
        `indices[i] = N_emp[i] = pattern_hash[i]`

    Raises
    ------
    ValueError
        If `mat` is not a binary matrix.

    Examples
    --------
    >>> mat = np.array([[1, 0, 0, 1, 1],
    ...                 [1, 0, 0, 1, 0]])
    >>> pattern_hash = np.array([1,3])
    >>> n_emp, n_emp_indices = n_emp_mat(mat, pattern_hash)
    >>> print(n_emp)
    [ 0.  2.]
    >>> print(n_emp_indices)
    [array([]), array([0, 3])]

    """
    # check if the mat is zero-one matrix
    if not is_binary(mat):
        raise ValueError("entries of mat should be either one or zero")
    h = hash_from_pattern(mat, base=base)
    N_emp = np.zeros(len(pattern_hash))
    indices = []
    for idx_ph, ph in enumerate(pattern_hash):
        indices_tmp = np.where(h == ph)[0]
        indices.append(indices_tmp)
        N_emp[idx_ph] = len(indices_tmp)
    return N_emp, indices
Beispiel #4
0
    def is_binary(self):
        """
        Returns True if the sparse matrix contains binary values only.
        Beware, that the function does not know if the input was binary
        because e.g `to_bool_array()` was used before or if the input is just
        sparse (i.e. only one spike per bin at maximum).

        Returns
        -------
        bool
            True for binary input, False otherwise.
        """

        return is_binary(self.sparse_matrix.data)
Beispiel #5
0
    def is_binary(self):
        """
        Checks and returns `True` if given input is a binary input.
        Beware, that the function does not know if the input is binary
        because e.g `to_bool_array()` was used before or if the input is just
        sparse (i.e. only one spike per bin at maximum).

        Returns
        -------
        bool
            True for binary input, False otherwise.
        """

        return is_binary(self.sparse_matrix.data)
def n_emp_mat_sum_trial(mat, pattern_hash):
    """
    Calculates empirical number of observed patterns summed across trials

    Parameters
    -----------
    mat: np.ndarray
        3d numpy array or elephant BinnedSpikeTrain object
        Binned spike trains represented as a binary matrix (i.e., matrix of
        0's and 1's), segmented into trials. Trials should contain an identical
        number of neurons and an identical number of time bins.
         the entries are zero or one
         0-axis --> trials
         1-axis --> neurons
         2-axis --> time bins
    pattern_hash: list
         Array of hash values, length: number of patterns.

    Returns
    --------
    N_emp: np.ndarray
        numbers of occurences of the given spike patterns in the given spike
        trains, summed across trials. Same length as `pattern_hash`.
    idx_trials: list
        list of indices of mat for each trial in which
        the specific pattern has been observed.
        0-axis --> trial
        1-axis --> list of indices for the chosen trial per
        entry of `pattern_hash`

    Raises
    -------
       ValueError: if matrix mat has wrong orientation
       ValueError: if mat is not zero-one matrix

    Examples
    ---------
    >>> mat = np.array([[[1, 1, 1, 1, 0],
                 [0, 1, 1, 1, 0],
                 [0, 1, 1, 0, 1]],

                 [[1, 1, 1, 1, 1],
                  [0, 1, 1, 1, 1],
                  [1, 1, 0, 1, 0]]])

    >>> pattern_hash = np.array([4,6])
    >>> N = 3
    >>> n_emp_sum_trial, n_emp_sum_trial_idx = \
    >>>                   n_emp_mat_sum_trial(mat, N,pattern_hash)
    >>> n_emp_sum_trial
        array([ 1.,  3.])
    >>> n_emp_sum_trial_idx
        [[array([0]), array([3])], [array([], dtype=int64), array([2, 4])]]
    """
    num_patt = len(pattern_hash)
    N_emp = np.zeros(num_patt)

    idx_trials = []
    # check if the mat is zero-one matrix
    if not is_binary(mat):
        raise ValueError("entries of mat should be either one or zero")

    for mat_tr in mat:
        N_emp_tmp, indices_tmp = n_emp_mat(mat_tr, pattern_hash, base=2)
        idx_trials.append(indices_tmp)
        N_emp += N_emp_tmp

    return N_emp, idx_trials
Beispiel #7
0
def hash_from_pattern(m, base=2):
    """
    Calculate for a spike pattern or a matrix of spike patterns
    (provide each pattern as a column) composed of N neurons a
    unique number.


    Parameters
    -----------
    m: np.ndarray
        2-dim ndarray
        spike patterns represented as a binary matrix (i.e., matrix of 0's and
        1's).
        Rows and columns correspond to patterns and neurons, respectively.
    base: integer
        base for calculation of hash values from binary
        sequences (= pattern).
        Default is 2

    Returns
    --------
    np.ndarray
        An array containing the hash values of each pattern,
        shape: (number of patterns).

    Raises
    -------
    ValueError
        if matrix `m` has wrong orientation

    Examples
    ---------
    descriptive example:
    m = [0
         1
         1]
    N = 3
    base = 2
    hash = 0*2^2 + 1*2^1 + 1*2^0 = 3

    second example:
    >>> import numpy as np
    >>> m = np.array([[0, 1, 0, 0, 1, 1, 0, 1],
    >>>               [0, 0, 1, 0, 1, 0, 1, 1],
    >>>               [0, 0, 0, 1, 0, 1, 1, 1]])

    >>> hash_from_pattern(m)
        array([0, 4, 2, 1, 6, 5, 3, 7])
    """
    n_neurons = m.shape[0]

    # check the entries of the matrix
    if not is_binary(m):
        raise ValueError('Patterns should be binary: 0 or 1')

    # generate the representation
    # don't use numpy - it's upperbounded by int64
    powers = np.array([base**x for x in range(n_neurons)])
    powers = sorted(powers, reverse=True)

    # calculate the binary number by use of scalar product
    return np.dot(powers, m)