/usr/share/pyshared/brian/connections/construction.py is in python-brian 1.4.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | from base import *
from sparsematrix import *
__all__ = ['random_row_func', 'random_matrix',
'random_matrix_fixed_column', 'eye_lil_matrix',
]
def random_row_func(N, p, weight=1., initseed=None):
'''
Returns a random connectivity ``row_func`` for use with :class:`UserComputedConnectionMatrix`
Gives equivalent output to the :meth:`Connection.connect_random` method.
Arguments:
``N``
The number of target neurons.
``p``
The probability of a synapse.
``weight``
The connection weight (must be a single value).
``initseed``
The initial seed value (for reproducible results).
'''
if initseed is None:
initseed = pyrandom.randint(100000, 1000000) # replace this
cur_row = numpy.zeros(N)
myrange = numpy.arange(N, dtype=int)
def row_func(i):
pyrandom.seed(initseed + int(i))
scirandom.seed(initseed + int(i))
k = scirandom.binomial(N, p, 1)[0]
cur_row[:] = 0.0
cur_row[pyrandom.sample(myrange, k)] = weight
return cur_row
return row_func
# Generation of matrices
def random_matrix(n, m, p, value=1.):
'''
Generates a sparse random matrix with size (n,m).
Entries are 1 (or optionnally value) with probability p.
If value is a function, then that function is called for each
non zero element as value() or value(i,j).
'''
# TODO:
# Simplify (by using valuef)
W = sparse.lil_matrix((n, m))
if callable(value) and callable(p):
if value.func_code.co_argcount == 0:
valuef = lambda i, j:[value() for _ in j] # value function
elif value.func_code.co_argcount == 2:
try:
failed = (array(value(0, arange(m))).size != m)
except:
failed = True
if failed: # vector-based not possible
log_debug('connections', 'Cannot build the connection matrix by rows')
valuef = lambda i, j:[value(i, k) for k in j]
else:
valuef = value
else:
raise AttributeError, "Bad number of arguments in value function (should be 0 or 2)"
if p.func_code.co_argcount == 2:
# Check if p(i,j) is vectorisable
try:
failed = (array(p(0, arange(m))).size != m)
except:
failed = True
if failed: # vector-based not possible
log_debug('connections', 'Cannot build the connection matrix by rows')
for i in xrange(n):
W.rows[i] = [j for j in range(m) if rand() < p(i, j)]
W.data[i] = list(valuef(i, array(W.rows[i])))
else: # vector-based possible
for i in xrange(n):
W.rows[i] = list((rand(m) < p(i, arange(m))).nonzero()[0])
W.data[i] = list(valuef(i, array(W.rows[i])))
elif p.func_code.co_argcount == 0:
for i in xrange(n):
W.rows[i] = [j for j in range(m) if rand() < p()]
W.data[i] = list(valuef(i, array(W.rows[i])))
else:
raise AttributeError, "Bad number of arguments in p function (should be 2)"
elif callable(value):
if value.func_code.co_argcount == 0: # TODO: should work with partial objects
for i in xrange(n):
k = random.binomial(m, p, 1)[0]
W.rows[i] = sample(xrange(m), k)
W.rows[i].sort()
W.data[i] = [value() for _ in xrange(k)]
elif value.func_code.co_argcount == 2:
try:
failed = (array(value(0, arange(m))).size != m)
except:
failed = True
if failed: # vector-based not possible
log_debug('connections', 'Cannot build the connection matrix by rows')
for i in xrange(n):
k = random.binomial(m, p, 1)[0]
W.rows[i] = sample(xrange(m), k)
W.rows[i].sort()
W.data[i] = [value(i, j) for j in W.rows[i]]
else:
for i in xrange(n):
k = random.binomial(m, p, 1)[0]
W.rows[i] = sample(xrange(m), k)
W.rows[i].sort()
W.data[i] = list(value(i, array(W.rows[i])))
else:
raise AttributeError, "Bad number of arguments in value function (should be 0 or 2)"
elif callable(p):
if p.func_code.co_argcount == 2:
# Check if p(i,j) is vectorisable
try:
failed = (array(p(0, arange(m))).size != m)
except:
failed = True
if failed: # vector-based not possible
log_debug('connections', 'Cannot build the connection matrix by rows')
for i in xrange(n):
W.rows[i] = [j for j in range(m) if rand() < p(i, j)]
W.data[i] = [value] * len(W.rows[i])
else: # vector-based possible
for i in xrange(n):
W.rows[i] = list((rand(m) < p(i, arange(m))).nonzero()[0])
W.data[i] = [value] * len(W.rows[i])
elif p.func_code.co_argcount == 0:
for i in xrange(n):
W.rows[i] = [j for j in range(m) if rand() < p()]
W.data[i] = [value] * len(W.rows[i])
else:
raise AttributeError, "Bad number of arguments in p function (should be 2)"
else:
for i in xrange(n):
k = random.binomial(m, p, 1)[0]
# Not significantly faster to generate all random numbers in one pass
# N.B.: the sample method is implemented in Python and it is not in Scipy
W.rows[i] = sample(xrange(m), k)
W.rows[i].sort()
W.data[i] = [value] * k
return W
def random_matrix_fixed_column(n, m, p, value=1.):
'''
Generates a sparse random matrix with size (n,m).
Entries are 1 (or optionnally value) with probability p.
The number of non-zero entries by per column is fixed: (int)(p*n)
If value is a function, then that function is called for each
non zero element as value() or value(i,j).
'''
W = sparse.lil_matrix((n, m))
k = (int)(p * n)
for j in xrange(m):
# N.B.: the sample method is implemented in Python and it is not in Scipy
for i in sample(xrange(n), k):
W.rows[i].append(j)
if callable(value):
if value.func_code.co_argcount == 0:
for i in xrange(n):
W.data[i] = [value() for _ in xrange(len(W.rows[i]))]
elif value.func_code.co_argcount == 2:
for i in xrange(n):
W.data[i] = [value(i, j) for j in W.rows[i]]
else:
raise AttributeError, "Bad number of arguments in value function (should be 0 or 2)"
else:
for i in xrange(n):
W.data[i] = [value] * len(W.rows[i])
return W
def eye_lil_matrix(n):
'''
Returns the identity matrix of size n as a lil_matrix
(sparse matrix).
'''
M = sparse.lil_matrix((n, n))
M.setdiag([1.] * n)
return M
|