Description of problem
I’m trying to convert model from the article Computing with Neural Synchrony. I’dont understand how is used with brian2 connectivity which is used at this model.
Minimal code to reproduce problem
from brian2 import *
bmin,bmax=-7,-1
def odor(N):
# Returns a random vector of binding constants
return 10**(rand(N)*(bmax-bmin)+bmin)
def hill_function(c,K=1.,n=3.):
'''
Hill function:
* c = concentration
* K = half activation constant (choose K=1 for relative concentrations)
* n = Hill coefficient
'''
return (c**n)/(c**n+K**n)
N=5000 # number of receptors
# Odors
seed(31415) # Get the same neurons every time
intensity=3000.
c1=odor(N)
c2=odor(N)
c0=c1
I1,I2=intensity,intensity
# Odor plumes (fluctuating concentrations)
tau_plume=75*ms
eq_plumes='''
dx/dt=-x/tau_plume+(2./tau_plume)**.5*xi : 1
y=clip(x,0,inf) : 1
'''
plume=NeuronGroup(2,model=eq_plumes) # 2 odors
# Receptor neurons
Fmax=40*Hz # maximum firing rate
tau=20*ms
Imax=1/(1-exp(-1/(Fmax*tau))) # maximum input current
eq_receptors='''
dv/dt=(Imax*hill_function(c)-v)/tau : 1
c : 1 # concentrations (relative to activation constant)
'''
receptors=NeuronGroup(N,model=eq_receptors,threshold='v > 1',reset='v = 0')
receptors.c=c1
@network_operation
def odor_to_nose():
# Send odor plume to the receptors
receptors.c=I1*c1*clip(plume.x[0],0,Inf)+I2*c2*clip(plume.x[1],0,Inf)
# Decoder neurons
M=200
taud=8*ms
sigma=.15
eq_decoders='''
dv/dt=-v/taud + sigma*(2/taud)**.5*xi : 1
'''
decoders=NeuronGroup(2*M,model=eq_decoders,threshold='v > 1',reset='v = 0')
# First M neurons encode odor A, next M neurons encode odor B
# Synapses
syn = Synapses(receptors,decoders)
#syn=Connection(receptors,decoders,'v')
syn.connect()
# Connectivity according to synchrony partitions
bhalf=.5*(bmin+bmax) # select only those that are well activated
u=2*(log(c1)/log(10)-bhalf)/(bmax-bmin) # normalized binding constants for odor A
for i in range(M):
which=((u>=i*1./M) & (u<(i+1)*1./M)) # we divide in M groups with similar values
if sum(which)>0:
w=1./sum(which) # total synaptic weight for a postsynaptic neuron is 1
syn[:,i]=w*which
u=2*(log(c2)/log(10)-bhalf)/(bmax-bmin)
for i in range(M): # normalized binding constants for odor B
which=((u>=i*1./M) & (u<(i+1)*1./M))
if sum(which)>0:
w=1./sum(which)
syn[:,2*M-1-i]=w*which
# Record odor concentration and output spikes
O=StateMonitor(plume,'y',record=True)
S=SpikeMonitor(receptors)
S2=SpikeMonitor(decoders)
print("Odor A")
I1,I2=intensity,0
run(2*second)
print("Odor B")
I1,I2=0,intensity
run(2*second)
print("Odor B x2")
I1,I2=0,2*intensity
run(2*second)
print("Odor A + odor C")
I1,I2=intensity,intensity
old_c2=c2
c2=odor(N) # different odor
run(2*second)
print("Odor A + odor B")
I1,I2=intensity,intensity
c2=old_c2
run(2*second)
t=O.times/ms
# Figure (9B)
subplot(311) # odor fluctuations
plot(t[t<2000],O[0][t<2000],'b')
plot(t[(t>=2000) & (t<4000)],O[1][(t>=2000) & (t<4000)],'r')
plot(t[(t>=4000) & (t<6000)],2*O[1][(t>=4000) & (t<6000)],'r')
plot(t[(t>=6000) & (t<8000)],O[0][(t>=6000) & (t<8000)],'b')
plot(t[(t>=6000) & (t<8000)],O[1][(t>=6000) & (t<8000)],'k')
plot(t[(t>=8000) & (t<10000)],O[1][(t>=8000) & (t<10000)],'r')
plot(t[(t>=8000) & (t<10000)],O[0][(t>=8000) & (t<10000)],'b')
xlim(0,10000)
xticks([])
subplot(312)
raster_plot(S)
xlim(0,10000)
ylim(2500,2600) # 100 random neurons
xticks([])
subplot(313)
raster_plot(S2)
ylim(100,300)
xlim(0,10000)
show()
from brian2 import *
import numpy
N=5000
# Coincidence detectors
sigma=.15
taud=8*ms
# Connections
Nsynapses=50
w0=150./(0.02*N)
# STDP
factor=0.05
a_pre=0.06*factor
b_post=-1.*factor
tau_pre=3*ms
# Intrinsic plasticity: non-specific weight increase
IP_period=10*ms
IP_rate=-b_post*5*Hz # target firing rate = 5 Hz
# Simulation control
record_period=1*second
duration=100*second
bmin,bmax=-7,-1
def odor(N):
# Returns a random vector of binding constants
return 10**(rand(N)*(bmax-bmin)+bmin)
def hill_function(c,K=1.,n=3.):
'''
Hill function:
* c = concentration
* K = half activation constant (choose K=1 for relative concentrations)
* n = Hill coefficient
'''
return (c**n)/(c**n+K**n)
N=5000 # number of receptors
seed(31415) # Get the same neurons every time
intensity=3000.
# Odor plumes
tau_plume=75*ms
eq_plumes='''
dx/dt=-x/tau_plume+(2./tau_plume)**.5*xi : 1
y=clip(x,0,inf) : 1
'''
plume=NeuronGroup(1,model=eq_plumes) # 1 odor
# Receptor neurons
Fmax=40*Hz # maximum firing rate
tau=20*ms
Imax=1/(1-exp(-1/(Fmax*tau))) # maximum input current
eq_receptors='''
dv/dt=(Imax*hill_function(c)-v)/tau : 1
c : 1 # concentrations (relative to activation constant)
'''
receptors=NeuronGroup(N,model=eq_receptors,threshold='v>1',reset='v=0')
@network_operation
def odor_to_nose():
# Send odor plume to the receptors
receptors.c=I1*c1*clip(plume.x[0],0,Inf)
odors=[odor(N),odor(N)] # two odors
c1=odors[0]
stimuli=[]
# A random odor is presented every 200 ms
@network_operation(clock=EventClock(dt=200*ms))
def change_odor():
global c1
nodor=randint(len(odors))
c1=odors[nodor]
stimuli.append((float(defaultclock.t),float(nodor)))
# Decoder neurons
M=30
eq_decoders='''
dv/dt=-v/taud + sigma*(2/taud)**.5*xi : 1
'''
decoders=NeuronGroup(M,model=eq_decoders,threshold='v>1',reset='v=0')
S2=SpikeMonitor(decoders)
# Random synapses
syn=Connection(receptors,decoders,'v',sparseness=Nsynapses*1./N,weight=w0)
# STDP
eqs_stdp='''
dApre/dt=-Apre/tau_pre : 1
Apost : 1
'''
pre='''
Apre+=a_pre
#w+=0
'''
post='''
Apost+=0
w+=Apre+b_post*w
'''
stdp=STDP(syn,eqs_stdp,pre=pre,post=post,wmax=Inf)
MC=ConnectionMonitor(syn,store=True,clock=EventClock(dt=record_period))
@network_operation(EventClock(dt=IP_period))
def intrinsic_plasticity(): # synaptic scaling
# Increases weights of all synapses
syn.W.alldata+=syn.W.alldata*IP_rate*IP_period
# Record the evolution of weights
weights=[]
@network_operation(EventClock(dt=record_period))
def recordW():
Z=syn.W[:,0].copy()
weights.append(Z)
I1=intensity
print "Started"
run(duration,report="text")
# Save data
wsave=[(t,M.todense()) for (t,M) in MC.values]
numpy.save("weights.npy",array(zip(*wsave)[1])) # 3D array (t,i,j)
numpy.save("spikesout.npy",array(S2.spikes))
numpy.save("stimuli.npy",array(stimuli))