/usr/share/pyshared/brian/experimental/multilinearstateupdater.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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | from brian import *
from brian.stateupdater import get_linear_equations_solution_numerically, get_linear_equations
from scipy import linalg, weave
import numpy
import inspect
from itertools import count
import re
__all__ = ['MultiLinearStateUpdater', 'get_multilinear_state_updater', 'MultiLinearNeuronGroup']
__all__ = ['MultiLinearStateUpdater', 'get_multilinear_state_updater', 'MultiLinearNeuronGroup']
def get_multilinear_matrices(eqs, subs, clock=None):
'''
Returns the matrices M and B for the linear model dX/dt = M(X-B),
where eqs is an Equations object.
'''
if clock is None: clock = guess_clock()
nsubs = len(subs[subs.keys()[0]])
# Otherwise assumes it is given in functional form
n = len(eqs._diffeq_names) # number of state variables
dynamicvars = eqs._diffeq_names
# Calculate B
AB = zeros((n, 1, nsubs))
d = dict.fromkeys(dynamicvars)
for j in range(n):
if dynamicvars[j] in subs:
d[dynamicvars[j]] = subs[j]
else:
d[dynamicvars[j]] = 0. * eqs._units[dynamicvars[j]]
for var, i in zip(dynamicvars, count()):
AB[i, 0, :] = -eqs.apply(var, d)
# Calculate A
M = zeros((n, n, nsubs))
for i in range(n):
for j in range(n):
if dynamicvars[j] in subs:
d[dynamicvars[j]] = subs[j]
else:
d[dynamicvars[j]] = 0. * eqs._units[dynamicvars[j]]
if dynamicvars[i] in subs:
d[dynamicvars[i]] = subs[i]
else:
if isinstance(eqs._units[dynamicvars[i]], Quantity):
d[dynamicvars[i]] = Quantity.with_dimensions(1., eqs._units[dynamicvars[i]].get_dimensions())
else:
d[dynamicvars[i]] = 1.
for var, j in zip(dynamicvars, count()):
M[j, i, :] = eqs.apply(var, d) + AB[j]
allM = M
allAB = AB
AiBi = []
for i in xrange(nsubs):
try:
M = reshape(allM[:, :, i], allM.shape[:2])
AB = reshape(allAB[:, :, i], allAB.shape[:2])
C = linalg.solve(M, AB)
A = linalg.expm(M * clock.dt)
B = -dot(A, C) + C
except LinAlgError:
numeulersteps = 100
deltat = clock.dt / numeulersteps
E = eye(n) + deltat * M
C = eye(n)
D = zeros((n, 1))
for step in xrange(numeulersteps):
C, D = dot(E, C), dot(E, D) - AB * deltat
A, B = C, D
AiBi.append((A, B))
return AiBi
def get_multilinear_state_updater(eqs, subs, level=0, clock=None):
'''
Make a multilinear state updater
Arguments:
``eqs``
should be the equations, and must be a string not an :class:`Equations` object.
``subs``
A dictionary of arrays, each key k is a variable name, each value is an equally
sized array of values it takes, this array should have the size of the number
of neurons in the group.
``level``
How many levels up to look for the equations' namespace.
``clock``
If you want.
'''
neweqs = Equations(eqs, level=level + 1)
neweqs.prepare()
AiBi = get_multilinear_matrices(neweqs, subs, clock=clock)
n = AiBi[0][0].shape[0]
nsubs = len(subs[subs.keys()[0]])
A = numpy.zeros((n, n, nsubs))
B = numpy.zeros((n, nsubs))
for i, (Ai, Bi) in enumerate(AiBi):
A[:, :, i] = Ai
B[:, i] = Bi.squeeze()
return MultiLinearStateUpdater(A, B)
class MultiLinearStateUpdater(StateUpdater):
'''
A StateUpdater with one differential equation for each neuron
Initialise with:
``A``, ``B``
Arrays, see below.
The update step for neuron i is S[:,i] = dot(A[:,:,i], S[:,i]) + B[:, i].
'''
def __init__(self, A, B=None):
self.A = A
self.B = B
self._useaccel = get_global_preference('useweave')
self._cpp_compiler = get_global_preference('weavecompiler')
self._extra_compile_args = ['-O3']
if self._cpp_compiler == 'gcc':
self._extra_compile_args += get_global_preference('gcc_options') # ['-march=native', '-ffast-math']
def __call__(self, P):
if self._useaccel:
n = len(P)
m = len(self)
S = P._S
A = self.A
B = self.B
code = '''
double x[m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
x[j] = B(j,i);
for(int k=0;k<m;k++)
x[j] += A(j,k,i) * S(k,i);
}
for(int j=0;j<m;j++)
S(j,i) = x[j];
}
'''
weave.inline(code, ['n', 'm', 'S', 'A', 'B'],
compiler=self._cpp_compiler,
type_converters=weave.converters.blitz,
extra_compile_args=self._extra_compile_args)
else:
if self.A.shape[2] < self.A.shape[1]:
for i in xrange(self.A.shape[2]):
P._S[:, i] = dot(self.A[:, :, i], P._S[:, i])
else:
# this is equivalent to the above but the loop is smaller if the
# number of neurons is large
AS = self.A[:, 0, :]*P._S[0, :]
for i in xrange(1, self.A.shape[1]):
AS += self.A[:, i, :]*P._S[i, :]
P._S[:] = AS
if self.B is not None:
add(P._S, self.B, P._S)
def __len__(self):
return self.A.shape[0]
def __repr__(self):
return 'MultiLinearStateUpdater'
__str__ = __repr__
class MultiLinearNeuronGroup(NeuronGroup):
'''
Make a NeuronGroup with a linear differential equation for each neuron
You give a single set of differential equations with parameters, the
variables you want substituted should be defined as parameters in the equations,
but they will not be treated as parameters, instead they will be substituted.
You also pass a list of variables to have their values substituted, and these
names should exist in the namespace initialising the MultiLinearNeuronGroup.
Arguments:
``eqs``
should be the equations, and must be a string not an :class:`Equations` object.
``subs``
A list of variables to be substituted with values.
``level``
How many levels up to look for the equations' namespace.
``clock``
If you want.
``kwds``
Any additonal arguments to pass to :class:`NeuronGroup` init.
'''
#TODO: for consistency put the units in the equations, and multilinearneurongroup can extract them.
def __init__(self, eqs, subs, clock=None, level=0, **kwds):
subs_set = set(subs)
param_pattern = re.compile('\s*(\w+)\s*:\s*(.*)')
eqs = re.sub('\\\s*?\n', ' ', eqs) # compact multiline equations
neweqs = ''
subs = {}
for line in eqs.splitlines():
line = re.sub('#.*', '', line) # remove comments
result = param_pattern.match(line)
added = False
if result is not None:
name, unit = result.groups()
if name in subs_set:
subs[name] = unit
added = True
if not added:
neweqs += line + '\n'
eqs = neweqs
frame = inspect.stack()[level + 1][0]
ns_global, ns_local = frame.f_globals, frame.f_locals
k0 = subs.keys()[0]
if k0 in ns_local:
nsubs = len(ns_local[k0])
else:
nsubs = len(ns_global[k0])
neweqs = eqs
for k, u in subs.iteritems():
neweqs = neweqs.replace(k, '(0.*(' + u + '))')
neweqs = Equations(neweqs, level=level + 1)
NeuronGroup.__init__(self, nsubs, neweqs, clock=clock, **kwds)
subs2 = {}
for k, u in subs.iteritems():
if k in ns_local:
subs2[k] = ns_local[k]
else:
subs2[k] = ns_global[k]
self._state_updater = get_multilinear_state_updater(eqs, subs2, level=level + 1, clock=clock)
if __name__ == '__main__':
eqs = '''
dv/dt = k*v/(1*second) : 1
dw/dt = k*w/(1*second) : 1
k : 1
'''
k = array([-1, -2, -3])
subs = ['k']
G = MultiLinearNeuronGroup(eqs, subs)
G.v = 1
G.w = 0
M = StateMonitor(G, 'v', record=True)
run(1 * second)
for i in range(len(G)):
plot(M.times, M[i])
show()
|