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
|
"""
Check that the features of `Synapses` are available and correct.
"""
import numpy
from brian2 import *
from brian2.tests.features import FeatureTest, InaccuracyError
class SynapsesPre(FeatureTest):
category = "Synapses"
name = "Presynaptic code"
tags = ["NeuronGroup", "run", "Synapses", "Presynaptic code"]
def run(self):
tau = 5 * ms
eqs = """
dV/dt = k/tau : 1
k : 1
"""
G = NeuronGroup(10, eqs, threshold="V>1", reset="V=0")
G.k = linspace(1, 5, len(G))
H = NeuronGroup(10, "V:1")
S = Synapses(G, H, on_pre="V += 1")
S.connect(j="i")
self.H = H
run(101 * ms)
def results(self):
return self.H.V[:]
compare = FeatureTest.compare_arrays
class SynapsesPost(FeatureTest):
category = "Synapses"
name = "Postsynaptic code"
tags = ["NeuronGroup", "run", "Synapses", "Postsynaptic code"]
def run(self):
tau = 5 * ms
eqs = """
dV/dt = k/tau : 1
k : 1
"""
G = NeuronGroup(10, eqs, threshold="V>1", reset="V=0")
G.k = linspace(1, 5, len(G))
H = NeuronGroup(10, "V:1")
S = Synapses(H, G, on_post="V_pre += 1")
S.connect(j="i")
self.H = H
run(101 * ms)
def results(self):
return self.H.V[:]
compare = FeatureTest.compare_arrays
class SynapsesSTDP(FeatureTest):
category = "Synapses"
name = "STDP"
tags = [
"NeuronGroup",
"Threshold",
"Reset",
"Refractory",
"run",
"Synapses",
"Postsynaptic code",
"Presynaptic code",
"SpikeMonitor",
"StateMonitor",
"SpikeGeneratorGroup",
]
def run(self):
n_cells = 100
n_recorded = 10
numpy.random.seed(42)
taum = 20 * ms
taus = 5 * ms
Vt = -50 * mV
Vr = -60 * mV
El = -49 * mV
fac = 60 * 0.27 / 10
gmax = 20 * fac
dApre = 0.01
taupre = 20 * ms
taupost = taupre
dApost = -dApre * taupre / taupost * 1.05
dApost *= 0.1 * gmax
dApre *= 0.1 * gmax
connectivity = numpy.random.randn(n_cells, n_cells)
sources = numpy.random.random_integers(0, n_cells - 1, 10 * n_cells)
# Only use one spike per time step (to rule out that a single source neuron
# has more than one spike in a time step)
times = (
numpy.random.choice(numpy.arange(10 * n_cells), 10 * n_cells, replace=False)
* ms
)
v_init = Vr + numpy.random.rand(n_cells) * (Vt - Vr)
eqs = Equations(
"""
dv/dt = (g-(v-El))/taum : volt
dg/dt = -g/taus : volt
"""
)
P = NeuronGroup(
n_cells, model=eqs, threshold="v>Vt", reset="v=Vr", refractory=5 * ms
)
Q = SpikeGeneratorGroup(n_cells, sources, times)
P.v = v_init
P.g = 0 * mV
S = Synapses(
P,
P,
model="""dApre/dt=-Apre/taupre : 1 (event-driven)
dApost/dt=-Apost/taupost : 1 (event-driven)
w : 1""",
pre="""g += w*mV
Apre += dApre
w = w + Apost""",
post="""Apost += dApost
w = w + Apre""",
)
S.connect()
S.w = fac * connectivity.flatten()
T = Synapses(Q, P, model="w : 1", on_pre="g += w*mV")
T.connect(j="i")
T.w = 10 * fac
spike_mon = SpikeMonitor(P)
state_mon = StateMonitor(S, "w", record=np.arange(n_recorded))
v_mon = StateMonitor(P, "v", record=np.arange(n_recorded))
self.state_mon = state_mon
self.spike_mon = spike_mon
self.v_mon = v_mon
run(0.2 * second, report="text")
def results(self):
return self.state_mon.w[:], self.v_mon.v[:], self.spike_mon.num_spikes
def compare(self, maxrelerr, res1, res2):
w1, v1, n1 = res1
w2, v2, n2 = res2
FeatureTest.compare_arrays(self, maxrelerr, w1, w2)
FeatureTest.compare_arrays(self, maxrelerr, v1, v2)
FeatureTest.compare_arrays(
self, maxrelerr, array([n1], dtype=float), array([n2], dtype=float)
)
if __name__ == "__main__":
for ftc in [SynapsesPre, SynapsesPost]:
ft = ftc()
ft.run()
print(ft.results())
|