#!pip install ANNarchy
PyNN and Brian examples
import numpy as np
import matplotlib.pyplot as plt
import ANNarchy as ann
ANNarchy 5.0 (5.0.0) on darwin (posix).
IF_curr_alpha
Simple network with a Poisson spike source projecting to a pair of IF_curr_alpha neurons.
This is a reimplementation of the PyNN example:
http://www.neuralensemble.org/trac/PyNN/wiki/Examples/simpleNetwork
class IFCurrAlpha (ann.Network):
def __init__(self, spike_times):
# Input population
= self.create(ann.SpikeSourceArray(spike_times))
inp
# Output population
= self.create(2, ann.IF_curr_alpha)
pop = 2.0
pop.tau_refrac = -50.0
pop.v_thresh = 2.0
pop.tau_syn_E = 2.0
pop.tau_syn_I
# Excitatory projection
= self.connect(inp, pop, 'exc')
proj =1.0)
proj.all_to_all(weights
# Monitor
self.m = self.monitor(pop, ['spike', 'v'])
# Parameters
= 1000.0
tstop = 100.0
rate
# Create the Poisson spikes
= int(2 * tstop * rate / 1000.0)
number 26278342)
np.random.seed(= list(np.add.accumulate(np.random.exponential(1000.0/rate, size=number)))
spike_times
# Create the network
= IFCurrAlpha(spike_times, dt=0.1)
net
# Compile the network
compile()
net.
# Simulate
net.simulate(tstop)= net.m.get()
data
# Plot the results
=(12, 8))
plt.figure(figsize*np.arange(tstop/net.dt), data['v'][:, 0])
plt.plot(net.dt'Time (ms)')
plt.xlabel('Vm (mV)')
plt.ylabel(-66.0, -48.0])
plt.ylim(['Simple Network')
plt.title( plt.show()
Compiling network 1... OK
IF_cond_exp
A single IF neuron with exponential, conductance-based synapses, fed by two spike sources.
This is a reimplementation of the PyNN example:
http://www.neuralensemble.org/trac/PyNN/wiki/Examples/IF_cond_exp
class IFCondExp (ann.Network):
def __init__(self):
# Input populations with predetermined spike times
= self.create(ann.SpikeSourceArray(
spike_sourceE = [float(i) for i in range(5, 105, 10)]
spike_times
)
)= self.create(ann.SpikeSourceArray(
spike_sourceI = [float(i) for i in range(155,255,10)]
spike_times
)
)
# Population with one IF_cond_exp neuron
= self.create(1, ann.IF_cond_exp)
ifcell set(
ifcell.'i_offset' : 0.1, 'tau_refrac' : 3.0,
{ 'v_thresh' : -51.0, 'tau_syn_E' : 2.0,
'tau_syn_I': 5.0, 'v_reset' : -70.0,
'e_rev_E' : 0., 'e_rev_I' : -80.0 } )
# Projections
= self.connect(spike_sourceE, ifcell, 'exc')
connE =0.006, delays=2.0)
connE.all_to_all(weights
= self.connect(spike_sourceI, ifcell, 'inh')
connI =0.02, delays=4.0)
connI.all_to_all(weights
# Monitor
self.n = self.monitor(spike_sourceE, ['spike'])
self.m = self.monitor(ifcell, ['spike', 'v'])
# Parameters
= 200.0
tstop
# Compile the network
= IFCondExp(dt=0.1)
net compile()
net.
# Simulate
net.simulate(tstop)= net.m.get()
data
# Show the result
=(12, 8))
plt.figure(figsize* np.arange(tstop/net.dt), data['v'][:, 0])
plt.plot(net.dt 'Time (ms)')
plt.xlabel('Vm (mV)')
plt.ylabel(-66.0, -61.0])
plt.ylim(['IF_cond_exp')
plt.title( plt.show()
Compiling network 2... OK
EIF_cond_exp
Network of EIF neurons with exponentially decreasing conductance-based synapses.
This is a reimplementation of the Brian example:
http://brian.readthedocs.org/en/1.4.1/examples-misc_expIF_network.html
= ann.Neuron(
EIF = dict(
parameters = -70.0,
v_rest = 0.2,
cm = 10.0,
tau_m = 5.0,
tau_syn_E = 10.0,
tau_syn_I = 0.0,
e_rev_E = -80.0,
e_rev_I = 3.0,
delta_T = -55.0,
v_thresh = -70.0,
v_reset = -20.0,
v_spike
),= [
equations 'dv/dt = (v_rest - v + delta_T * exp( (v-v_thresh)/delta_T) )/tau_m + ( g_exc * (e_rev_E - v) + g_inh * (e_rev_I - v) )/cm',
'tau_syn_E * dg_exc/dt = - g_exc',
'tau_syn_I * dg_inh/dt = - g_inh',
],= "v > v_spike",
spike = "v = v_reset",
reset = 2.0
refractory
)
# Create the network
= ann.Network(dt=0.1)
net
# Poisson inputs
= net.create(ann.PoissonPopulation(geometry=200, rates="if t < 200.0 : 2000.0 else : 0.0"))
i_exc = net.create(ann.PoissonPopulation(geometry=200, rates="if t < 100.0 : 2000.0 else : 0.0"))
i_inh
# Main population
= net.create(geometry=4000, neuron=EIF)
P
# Subpopulations
= P[:3200]
Pe = P[3200:]
Pi
# Projections
= 1.5 / 1000.0 # excitatory synaptic weight
we = 2.5 * we # inhibitory synaptic weight
wi
= net.connect(Pe, P, 'exc')
Ce =we, probability=0.05)
Ce.fixed_probability(weights
= net.connect(Pi, P, 'inh')
Ci =wi, probability=0.05)
Ci.fixed_probability(weights
= net.connect(i_exc, P[:200], 'exc') # inputs to excitatory cells
Ie =we)
Ie.one_to_one(weights
= net.connect(i_inh, P[3200:3400], 'exc')# inputs to inhibitory cells
Ii =we)
Ii.one_to_one(weights
# Initialization of variables
= -70.0 + 10.0 * np.random.rand(P.size)
P.v = (np.random.randn(P.size) * 2.0 + 5.0) * we
P.g_exc = (np.random.randn(P.size) * 2.0 + 5.0) * wi
P.g_inh
# Monitor
= net.monitor(P, 'spike')
m
# Compile the Network
compile()
net.
# Simulate
500.0, measure_time=True)
net.simulate(
# Retrieve recordings
= m.get()
data = m.raster_plot(data['spike'])
t, n
=(12, 8))
plt.figure(figsize'.', markersize=0.2)
plt.plot(t, n, plt.show()
Compiling network 3... OK
Simulating 0.5 seconds of the network 3 took 0.32831788063049316 seconds.
AEIF_cond_exp
Adaptive exponential integrate-and-fire model.
http://www.scholarpedia.org/article/Adaptive_exponential_integrate-and-fire_model
Model introduced in:
Brette R. and Gerstner W. (2005), Adaptive Exponential Integrate-and-Fire Model as an Effective Description of Neuronal Activity, J. Neurophysiol. 94: 3637 - 3642.
This is a reimplementation of the Brian example:
https://brian.readthedocs.io/en/stable/examples-frompapers_Brette_Gerstner_2005.html
= ann.Neuron(
AEIF = dict(
parameters = -70.6,
v_rest = 0.281,
cm = 9.3667,
tau_m = 5.0,
tau_syn_E = 5.0,
tau_syn_I = 0.0,
e_rev_E = -80.0,
e_rev_I = ann.Parameter(144.0),
tau_w = ann.Parameter(4.0),
a = ann.Parameter(0.0805),
b = 0.0,
i_offset = 2.0,
delta_T = -50.4,
v_thresh = ann.Parameter(-70.6),
v_reset = -40.0,
v_spike
), = """
equations I = g_exc * (e_rev_E - v) + g_inh * (e_rev_I - v) + i_offset
tau_m * dv/dt = (v_rest - v + delta_T * exp((v-v_thresh)/delta_T)) + tau_m/cm*(I - w) : init=-70.6
tau_w * dw/dt = a * (v - v_rest) / 1000.0 - w
tau_syn_E * dg_exc/dt = - g_exc : exponential
tau_syn_I * dg_inh/dt = - g_inh : exponential
""",
= "v > v_spike",
spike = """
reset v = v_reset
w += b
""",
= 0.1
refractory
)
# Create the network
= ann.Network(dt=0.1)
net
# Population of 3 neurons (regular, bursting, fast spiking)
= net.create(geometry=3, neuron=AEIF)
pop = [144., 20., 144.]
pop.tau_w = [4. ,4., 2000.0*pop.cm/144.0]
pop.a = [0.0805, 0.5, 0.0]
pop.b = [-70.6, pop.v_thresh + 5.0, -70.6]
pop.v_reset
= net.monitor(pop, ['spike', 'v', 'w'])
m
# Compile the network
compile()
net.
# Add current of 1 nA and simulate
20.0)
net.simulate(= 1.0
pop.i_offset 100.0)
net.simulate(= 0.0
pop.i_offset 20.0)
net.simulate(
# Retrieve the results
= m.get()
data
# Make spikes nicer
for i in range(3):
if len(data['spike'][i]) > 0:
'v'][:, i][data['spike'][i]] = 20.0
data[
# Plot the activity
=(15, 8))
plt.figure(figsize2,3,1)
plt.subplot(*np.arange(140.0/net.dt), data['v'][:, 0])
plt.plot(net.dt"Regular")
plt.title('v')
plt.ylabel(2,3,2)
plt.subplot(*np.arange(140.0/net.dt), data['v'][:, 1])
plt.plot(net.dt"Bursting")
plt.title(2,3,3)
plt.subplot(*np.arange(140.0/net.dt), data['v'][:, 2])
plt.plot(net.dt'Fast spiking')
plt.title(2,3,4)
plt.subplot(*np.arange(140.0/net.dt), data['w'][:, 0])
plt.plot(net.dt'w')
plt.ylabel(2,3,5)
plt.subplot(*np.arange(140.0/net.dt), data['w'][:, 1])
plt.plot(net.dt2,3,6)
plt.subplot(*np.arange(140.0/net.dt), data['w'][:, 2])
plt.plot(net.dt'Time (ms)')
plt.xlabel( plt.show()
Compiling network 4... OK
Non-linear synapses
A single IF neuron with two non-linear NMDA synapses.
This is a reimplementation of the Brian example:
http://brian.readthedocs.org/en/latest/examples-synapses_nonlinear_synapses.html
# Neurons
= ann.Neuron(equations="dv/dt = 0.1", spike="v>1.0", reset="v=0.0")
Linear = ann.Neuron(equations="dv/dt = 0.1*(g_exc -v)", spike="v>2.0", reset="v=0.0")
Integrator
# Non-linear synapse
= ann.Synapse(
NMDA = dict(tau = 10.0),
parameters = [
equations 'tau * dx/dt = -x',
'tau * dg/dt = -g + x * (1 -g)',
],= "x += w",
pre_spike = "g"
psp
)
# Network
= ann.Network(dt=0.1)
net
# Populations
input = net.create(geometry=2, neuron=Linear)
input.v = [0.0, 0.5]
= net.create(geometry=1, neuron=Integrator)
pop
# Projection
= net.connect(pre=input, post=pop, target='exc', synapse=NMDA)
proj =[[1.0, 10.0]])
proj.from_matrix(weights
# Monitors
= net.monitor(pop, 'v')
m = net.monitor(proj, 'g')
w
# Compile the network
compile()
net.
# Simulate for 100 ms
100.0)
net.simulate(
# Retrieve recordings
= m.get('v')[:, 0]
v = w.get('g')[:, 0, :]
s
# Plot the recordings
=(12, 8))
plt.figure(figsize
2,1,1)
plt.subplot(0])
plt.plot(s[:, 1])
plt.plot(s[:, "Time (ms)")
plt.xlabel("Conductance")
plt.xlabel(
2,1,2)
plt.subplot(
plt.plot(v)"Time (ms)")
plt.xlabel("Membrane potential")
plt.xlabel( plt.show()
WARNING: Monitor(): it is a bad idea to record synaptic variables of a projection at each time step!
Compiling network 5... OK
STP
Network (CUBA) with short-term synaptic plasticity for excitatory synapses (depressing at long timescales, facilitating at short timescales).
Adapted from :
https://brian.readthedocs.io/en/stable/examples-synapses_short_term_plasticity2.html
= ann.Neuron(
LIF = dict(
parameters = 20.0,
tau_m = 5.0,
tau_e = 10.0,
tau_i = -49.0,
E_rest = -50.0,
E_thresh = -60.0,
E_reset
),= [
equations 'tau_m * dv/dt = E_rest -v + g_exc - g_inh',
'tau_e * dg_exc/dt = -g_exc',
'tau_i * dg_inh/dt = -g_inh',
],= "v > E_thresh",
spike = "v = E_reset"
reset
)
= ann.Synapse(
STP = dict(
parameters = 200.0,
tau_rec = 20.0,
tau_facil = 0.2,
U
),= [
equations 'dx/dt = (1 - x)/tau_rec', init = 1.0, method='event-driven'),
ann.Variable('du/dt = (U - u)/tau_facil', init = 0.2, method='event-driven'),
ann.Variable(
],= """
pre_spike g_target += w * u * x
x *= (1 - u)
u += U * (1 - u)
"""
)
# Network
= ann.Network(dt=0.1)
net
# Population
= net.create(geometry=4000, neuron=LIF)
P = ann.Uniform(-60.0, -50.0)
P.v = P[:3200]
Pe = P[3200:]
Pi
# Projections
= net.connect(pre=Pe, post=P, target='exc', synapse = STP)
con_e =1.62, probability=0.02)
con_e.fixed_probability(weights
= net.connect(pre=Pi, post=P, target='inh')
con_i =9.0, probability=0.02)
con_i.fixed_probability(weights
# Monitor
= net.monitor(P, 'spike')
m
# Compile the network
compile()
net.
# Simulate without plasticity
1000.0, measure_time=True)
net.simulate(
= m.get()
data = m.raster_plot(data['spike'])
t, n = m.population_rate(data['spike'], 5.0)
rates print('Total number of spikes: ' + str(len(t)))
=(12, 8))
plt.figure(figsize211)
plt.subplot('.')
plt.plot(t, n, 'Time (ms)')
plt.xlabel('Neuron number')
plt.ylabel(212)
plt.subplot(*net.dt, rates)
plt.plot(np.arange(rates.size) plt.show()
Compiling network 6... OK
Simulating 1.0 seconds of the network 6 took 0.0765390396118164 seconds.
Total number of spikes: 14453