View
216
Download
3
Category
Preview:
Citation preview
Romain Brette & Dan Goodman Ecole Normale SupérieureEquipe Audition
http://www.briansimulator.org
romain.brette@ens.fr dan.goodman@ens.fr
The spirit of “A simulator should not only save
the time of processors, but also the time of scientists”
scientist computer
Writing code often takes more time than running it
Goals: Quick model coding Flexible
models are defined by equations (rather than pre-defined)
briansimulator.org, click Download
We have Windows versions on USB sticks (ask Dan)We have Windows versions on USB sticks (ask Dan)
Installation
Download slides and examples: click Blog and Brian at NeuroComp
Download slides and examples: click Blog and Brian at NeuroComp
does an adaptive threshold
from brian import *
eqs = '''dv/dt = -v/(10*ms) : voltdvt/dt = (10*mV-vt)/(15*ms) : volt'''
reset = '''v = 0*mVvt += 3*mV'''
IF = NeuronGroup(1, eqs, reset=reset, threshold='v>vt')IF.rest()PG = PoissonGroup(1, 500*Hz)
C = Connection(PG, IF, 'v', weight=3*mV)
run(100*ms)
does synaptic plasticity
Song, S., K. D. Miller, and L. F. Abbott (2000). Competitive hebbian learning through spike timing-dependent synaptic plasticity. Nature Neurosci 3, 919-26.
does systems neuroscience
Sturzl, W., R. Kempter, and J. L. van Hemmen (2000). Theory of arachnid prey localization. Physical Review Letters 84 (24), 5668
Work in progress Parallel computing with graphics processing units
(GPUs) Up to 240 processors Up to 100x speed improvement Cheap hardware (a few €100) Good for vectorised code
If you want to contribute:http://groups.google.fr/group/brian-on-gpu
The Python console On Windows, open IDLE. On Linux: type python
interpreted language dynamic typing garbage collector space matters (signals structure) object-oriented many libraries
Writing a script If you use IDLE, click on File>New window.
Otherwise use any text editor.
Press F5 to execute
A simple program
# Factorial functiondef factorial(x): if x == 0: return 1 else: return x * factorial(x-1)
print factorial(5)
commentfunction definitionuntyped argument
condition
function calldisplay
structure by indentation(block = aligned instructions)
Numerical objects Base types: int, long, float, complex
Other numerical types (vectors, matrices) defined in the Numpy library (in a few minutes)
x=3+2x+=1y=100Lz=x*(1+2j)u=2.3/7x,y,z = 1,2,3a = b = 123
Control structures
x = 12if x < 5 or (x > 10 and x < 20): print "Good value."
if x < 5 or 10 < x < 20: print ‘Good value as well.'
for i in [0,1,2,3,4]: print "Number", i
for i in range(5): print "Number", i
while x >= 0: print "x is not always negative." x = x-1
list
the same list
Lists
mylist = [1,7,8,3]name = ["Jean","Michel"]x = [1,2,3,[7,8],"fin"]
print name[0]name[1]="Robert"print mylist[1:3]print mylist[:3],mylist[:]print mylist[-1]print x[3][1]
name.append("Georges")print mylist+nameprint mylist*2
concatenate
heterogeneous list
first element = index 0
« slice »: index 3 not included
last element
x[3] is a list
method (list = object)
Other methods: extend, insert, reverse, sort, remove…
List comprehensions
carres=[x**2 for i in range(10)]
pairs=[x for i in range(10) if i % 2 == 0]
= list of squares of integers from 0 to 9
= list of even integers between 0 and 9
Stringsa="Bonjour"b='hello'c="""Une phrasequi n'en finit pas"""
print a+bprint a[3:7]
print b.capitalize()
multiline string
≈ list of characters
many methods (find, replace, split…)
Dictionariesdico={‘one':1,‘two':2,‘three':‘several'}
print dico[‘three']
dico[‘four']=‘many‘del dico[‘one']
key value
for key in dico: print key,'->',dico[key]
iterate all keys
Functions
def power(x,exponent=2): return x**exponent
print power(3,2)
print power(7)
print power(exponent=3,x=2)
carre=lambda x:x**2 inline definition
default value
call with named arguments
Modulesimport mathprint math.exp(1)
from math import expprint exp(1)
from math import *print exp(1)
import only the exp object
import everything
You can work with several files (= modules), each one can define any number of objects (= variables, functions, classes)
loads the file ‘math.py’ or ‘math.pyc’ (compiled)
Scipy & Numpy Scientific libraries
Syntax ≈ Matlab Many mathematical functions
from scipy import *x=array([1,2,3])M=array([[1,2,3],[4,5,6]])M=ones((3,3))z=2*xy=dot(M,x)
from scipy.optimize import *print fsolve(lambda x:(x-1)*(x-3),2)
vector
matrix
matrix product
Vectors et matrices Base type in SciPy: array
(= vector or matrix)
from scipy import *x=array([1,2,3])M=array([[1,2,3],[4,5,6]])
M=ones((3,2))
z=2*x+1
y=dot(M,x)
vector (1,2,3)
matrix1 2 34 5 6
matrix1 11 11 1
matrix product
Operationsx+yx-yx*yx/yx**2exp(x)sqrt(x)
dot(x,y)dot(M,x)
M.TM.max()M.sum()
size(x)M.shape
element-wise
dot productmatrix product
transpose
x²
total number of elements
Indexing
x[i]M[i,j]x[i:j]M[i,:]M[:,i]x[[1,3]]
x[1:3]=[0,1]M[1,:]+=x
Vector indexing lists (first element= 0)
(i+1)th element
slice from x[i] to x[j-1](i+1)th row(i+1)th columnelements x[1] and x[3]
x[1]=0, x[3]=1add vector x to the 2nd row of M
M[i,:] is a « view » on matrix M copy ( reference)
y=M[0,:]y[2]=5
x=zx[1]=3
M[0,2] is 5
z[1] is 3 x=z.copy()copy:
Construction
x=array([1,2,3])M=array([[1,2,3],[4,5,6]])
x=ones(5)M=zeros((3,2))M=eye(3)M=diag([1,3,7])
x=rand(5)x=randn(5)
x=arange(10)
x=linspace(0,1,100)
from lists
vector of 1szero matrixidentity matrixdiagonal matrix
random vector in (0,1)random gaussian vector
0,1,2,...,9
100 numbers between 0 and 1
Vectorisation How to write efficient programs? Replace loops by vector operations
for i in range(1000000): X[i]=1
X=ones(1000000)
for i in range(1000000): X[i]=X[i]*2
X=X*2
for i in range(999999): Y[i]=X[i+1]-X[i]
Y=X[1:]-X[:-1]
for i in range(1000000): if X[i]>0.5: Y[i]=1
Y[X>.5]=1
Pylab Plotting library Syntax ≈ Matlab
Many plotting functions
from pylab import *plot([1,2,3],[4,5,6])show()
x y
last instruction of script
plot
polar
more examples:http://matplotlib.sourceforge.net/gallery.html
Plotting with PyLab
hist(randn(1000)) contour(gaussian_filter(randn(100, 100), 5))
imshow(gaussian_filter(randn(100, 100), 5))
specgram(sin(10000*2*pi*linspace(0,1,44100)),
Fs=44100)
Anatomy of a Brian scriptImport the Brian package+ Scipy and Pylab
Define some parameters, you can use physical unitsDefine neuron equations, the “volt” term says that V has units of volts, and Brian checks the consistency of your equations.Create neurons with given equations and fixed threshold and reset value.
Create synapses – here recurrent random connectivity with probability .1 for each pair of neurons and given synaptic weight, spikes cause an instantaneous increase of amount weight in variable V of the target neuron
Record spiking activity
Initialise state variables to random values.Run simulationAnalyse results – do a raster plot
Model equationstau=10*mseqs='''dv/dt = (v0-v)/tau : voltu=v*v : volt**2x=uv0 : volt'''
units
differential equationequationaliasparameter
Non-autonomous equations, use the reserved symbol “t” for time
Stochastic DEs Use the term “xi” for noise with: Has units s-1/2. Use typically as “sigma*xi*(2/tau)**0.5”
Solvers: Exact for linear DEs (detected automatically, method=‘linear’) Euler for nonlinear DEs (default, method=‘Euler’) 2nd order Runge-Kutta (default, method=‘RK’) Exponential Euler (implicit=True, method=‘exponential_Euler’)
)()(),( tsts
Neuron groupsgroup=NeuronGroup(N, model=’dv/dt = -v/tau : volt’,
threshold=’v>15*mV’,
reset=’v=0*mV’,
refractory=2*ms,
method=’implicit’)
model equations
threshold condition
what happens after spiking
refractory period
integration method
optional
units of v
group.v is a vector with values of v for all neurons
group.v[5] is the value of v for neuron 5
P=group[:100]P=group.subgroup(100)
defines a subgroup of 100 neurons
Special groups:
PoissonGroup(N,rates=10*Hz)
SpikeGeneratorGroup(N,spikes)
N Poisson processes
fires at specific times
spikes=[(3,2*ms),(1,10*ms),(2,15*ms)]
Connections
synapses=Connection(P,Q,’v’)
synapses[5,7]=2*mV
modified variable
P Q
synapses.connect_full(P,Q,weight=2*mV)synapses.connect_random(P,Q,sparseness=.02,weight=2*mV)synapses.connect_full(P[50:100],Q[20:40], weight=lambda i,j:w0*exp(-(i-j)**2))
when neuron 5 spikes: Q.v[7]+=2*mV
Building connections
synapses=Connection(P,Q,’v’,sparseness=.02,weight=2*mV)
Delays
synapses=Connection(P,Q,’v’,delay=2*ms)
Homogeneous delays
synapses=Connection(P,Q,’v’,delay=True,max_delay=10*ms)synapses.delay[5,7]=2*ms
synapses.connect_full(P,Q,weight=2*mV,delay=(1*ms,2*ms))synapses.connect_full(P,Q,weight=2*mV, delay=lambda i,j:w0*exp(-(i-j)**2))
Heterogeneous delays
Monitors Recording spike trains
Recording variables
M=SpikeMonitor(P)run(100*ms)raster_plot(M)show()
M=StateMonitor(P,'v',record=True)run(100*ms)plot(M.times/ms,M[0]/mV)show()
Network operations…M=SpikeMonitor(G)…@network_operationdef check_too_many_spikes(): if M.nspikes>1000: stop()…run(10*second)
this function is called at every time step
Integrate-and-fire model Stimulate an integrate-and-fire model with
regularly spaced spikes
Tip: use SpikeGeneratorGroup
Current-frequency curve Plot the firing rate of an IF model as a function
of input current
Tip: counter=SpikeCounter(group) counts spikes for every neuron
Cortical column Ring of noisy IF neurons with distance-
dependent connections
dv/dt=(v0-v)/ + / (t)
w(i,j)=cos(2(i-j)/N)w0
Interactive session Try writing your own models with Python,
NumPy, SciPy and Brian We’ll be around to help solve any problems Take a look at the online documentation too:
http://www.python.org/doc/2.5.4/tut/tut.html http://www.python.org/doc/2.5.4/ http://docs.scipy.org/doc/ http://matplotlib.sourceforge.net/contents.html http://www.briansimulator.org/docs/
Recommended