Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ValueError
If mat is not zero-one 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
>>> 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
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 = [base ** x for x in range(n_neurons)][::-1]
# calculate the binary number by use of scalar product
return np.dot(powers, m)
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)