def try1(): num = 10 neu = HH(num, monitors=['spikes', 'V']) neu.V = -70. + bp.math.random.normal(size=num) * 20 syn = GABAa(pre=neu, post=neu, conn=bp.connect.All2All(include_self=False)) syn.g_max = 0.1 / num net = bp.Network(neu=neu, syn=syn) for method in ['relative', 'absolute']: print(f'Method: {method}\n') print('vars:') print('-----') print('neu.vars()', list(neu.vars(method).keys())) print('syn.vars()', list(syn.vars(method).keys())) print('net.vars()', list(net.vars(method).keys())) print() print('ints:') print('-----') print('neu.ints()', list(neu.ints(method).keys())) print('syn.ints()', list(syn.ints(method).keys())) print('net.ints()', list(net.ints(method).keys())) print() print('nodes:') print('------') print('neu.nodes()', list(neu.nodes(method).keys())) print('syn.nodes()', list(syn.nodes(method).keys())) print('net.nodes()', list(net.nodes(method).keys())) print()
def compare(): num = 4000 * 10 num_exc = int(num * 0.75) num_inh = int(num * 0.25) for bk in [ 'numba-cuda', 'numba', ]: print(f'Backend = {bk}') bp.backend.set(bk) E_group = LIF(num_exc, ) E_group.V = np.random.randn(num_exc) * 5. - 55. I_group = LIF(num_inh, ) I_group.V = np.random.randn(num_inh) * 5. - 55. E2E = ExcSyn(pre=E_group, post=E_group, conn=bp.connect.FixedProb(0.02)) E2I = ExcSyn(pre=E_group, post=I_group, conn=bp.connect.FixedProb(0.02)) I2E = InhSyn(pre=I_group, post=E_group, conn=bp.connect.FixedProb(0.02)) I2I = InhSyn(pre=I_group, post=I_group, conn=bp.connect.FixedProb(0.02)) net = bp.Network(E_group, I_group, E2E, E2I, I2E, I2I) net.run(100., report=True)
def try_cuda(num=4000 * 10): print(f'Number of neurons: {num}') num_exc = int(num * 0.75) num_inh = int(num * 0.25) # bp.backend.set('numba-cuda') bp.backend.set('numba') E_group = LIF(num_exc, ) E_group.V = np.random.randn(num_exc) * 5. - 55. I_group = LIF(num_inh, ) I_group.V = np.random.randn(num_inh) * 5. - 55. E2E = ExcSyn(pre=E_group, post=E_group, conn=bp.connect.FixedProb(0.02, method='vector')) E2I = ExcSyn(pre=E_group, post=I_group, conn=bp.connect.FixedProb(0.02, method='vector')) I2E = InhSyn(pre=I_group, post=E_group, conn=bp.connect.FixedProb(0.02, method='vector')) I2I = InhSyn(pre=I_group, post=I_group, conn=bp.connect.FixedProb(0.02, method='vector')) net = bp.Network(E_group, I_group, E2E, E2I, I2E, I2I) return net.run(100., report=True, report_percent=0.5)
def test_lif(num, device): print('Scale:{}, Model:LIF, Device:{}, '.format(num, device), end='') st_build = time.time() bp.profile.set(jit=True, device=device, dt=0.1, numerical_method='exponential') V_rest = -52.0 V_reset = -60.0 V_th = -50.0 neu = bpmodels.neurons.get_LIF(V_rest=V_rest, V_reset=V_reset, V_th=V_th, noise=0., mode='scalar') syn = bpmodels.synapses.get_exponential(tau_decay=2.0, mode='scalar') num_exc = int(num / 2) num_inh = int(num / 2) prob = 0.01 JE = 1 / np.sqrt(prob * num_exc) JI = 1 / np.sqrt(prob * num_inh) group = bp.NeuGroup(neu, geometry=num_exc + num_inh, monitors=['spike']) group.ST['V'] = np.random.random(num_exc + num_inh) * (V_th - V_rest) + V_rest exc_conn = bp.SynConn(syn, pre_group=group[:num_exc], post_group=group, conn=bp.connect.FixedProb(prob=prob)) exc_conn.ST['g'] = JE inh_conn = bp.SynConn(syn, pre_group=group[num_exc:], post_group=group, conn=bp.connect.FixedProb(prob=prob)) exc_conn.ST['g'] = -JI net = bp.Network(group, exc_conn, inh_conn) ed_build = time.time() st_run = time.time() net.run(duration=1000., inputs=(group, 'ST.input', 3.)) ed_run = time.time() build_time = float(ed_build - st_build) run_time = float(ed_run - st_run) print('BuildT:{:.2f}s, RunT:{:.2f}s'.format(build_time, run_time)) return run_time, build_time
def test_ints(): A = HH(1, name='X2') B = HH(1, name='Y2') A.pre = B B.pre = A net = bp.Network(A, B) abs_ints = net.ints(method='absolute') rel_ints = net.ints(method='relative') print() pprint(abs_ints.keys()) pprint(rel_ints.keys())
def test_nodes(): A = HH(1, name='X') B = HH(1, name='Y') A.pre = B B.pre = A net = bp.Network(A, B) abs_nodes = net.nodes(method='absolute') rel_nodes = net.nodes(method='relative') print() pprint(abs_nodes) pprint(rel_nodes) assert len(abs_nodes) == 3 assert len(rel_nodes) == 5
def uniform_delay(): # numba_cuda.set_monitor_done_in('cpu') size = 4000 for bk in [ 'numba-cuda', 'numba', ]: print(f'Backend = {bk}') bp.backend.set(backend=bk, dt=0.05) hh = HH(size) ampa = AMPA1(pre=hh, post=hh, conn=bp.connect.All2All(), delay=1.) ampa.g_max /= hh.num net = bp.Network(hh, ampa) net.run(100., inputs=(hh, 'input', 10.), report=True)
def non_uniform_delay(): hh = HH(4000, monitors=['V']) ampa = AMPA1(pre=hh, post=hh, conn=bp.connect.All2All(), delay=lambda: np.random.random() * 1., monitors=['s0']) ampa.g_max /= hh.num net = bp.Network(hh, ampa) net.run(100., inputs=(hh, 'input', 10.), report=True) fig, gs = bp.visualize.get_figure( row_num=2, col_num=1, ) fig.add_subplot(gs[0, 0]) bp.visualize.line_plot(hh.mon.ts, hh.mon.V) fig.add_subplot(gs[1, 0]) bp.visualize.line_plot(ampa.mon.ts, ampa.mon.s0, show=True)
pre.ST['V'] = -65. post = bp.NeuGroup(LIF_neuron, geometry=(10, ), monitors=['V', 'input', 'spike']) post.runner.set_schedule(['input', 'update', 'monitor', 'reset']) pre.ST['V'] = -65. # Set synapse connection & network exponential = bp.SynConn(model=exponential_syn, pre_group=pre, post_group=post, conn=bp.connect.All2All(), monitors=['g'], delay=10.) exponential.runner.set_schedule(['input', 'update', 'output', 'monitor']) net = bp.Network(pre, exponential, post) current = bp.inputs.spike_current([5., 10., 15., 20.], bp.profile._dt, 1., duration=duration) net.run(duration=duration, inputs=[exponential, 'pre.spike', current, "="], report=True) # Figure ts = net.ts fig, gs = bp.visualize.get_figure(2, 1, 3, 7) fig.add_subplot(gs[0, 0]) plt.plot(ts, exponential.mon.g[:, 0], label='exponential.g')
post = bp.NeuGroup(LIF_neuron, geometry=(10, ), monitors=['V', 'input', 'spike']) post.runner.set_schedule(['input', 'update', 'monitor', 'reset']) pre.ST['V'] = -65. # Set synapse connection & network two_exponentials = bp.SynConn(model=two_exponentials_syn, pre_group=pre, post_group=post, conn=bp.connect.All2All(), monitors=['g'], delay=10.) two_exponentials.runner.set_schedule( ['input', 'update', 'output', 'monitor']) net = bp.Network(pre, two_exponentials, post) current = bp.inputs.spike_current([5., 10., 15., 20.], bp.profile._dt, 1., duration=duration) net.run(duration=duration, inputs=[two_exponentials, 'pre.spike', current, "="], report=True) # Figure ts = net.ts fig, gs = bp.visualize.get_figure(2, 1, 3, 7) fig.add_subplot(gs[0, 0]) plt.plot(ts, two_exponentials.mon.g[:, 0], label='two_exponentials.g')
def test_hh_ampa_net1(): class HH(bp.NeuGroup): def __init__(self, size, ENa=50., EK=-77., EL=-54.387, C=1.0, gNa=120., gK=36., gL=0.03, V_th=20., **kwargs): # parameters self.ENa = ENa self.EK = EK self.EL = EL self.C = C self.gNa = bp.math.numpy.Variable(gNa) self.gK = gK self.gL = gL self.V_th = bp.math.numpy.Variable(V_th) # variables self.V = bp.math.numpy.Variable(np.ones(size) * -65.) self.m = bp.math.numpy.Variable(np.ones(size) * 0.5) self.h = bp.math.numpy.Variable(np.ones(size) * 0.6) self.n = bp.math.numpy.Variable(np.ones(size) * 0.32) self.spike = bp.math.numpy.Variable(np.zeros(size, dtype=bool)) self.input = bp.math.numpy.Variable(np.zeros(size)) super(HH, self).__init__(size=size, **kwargs) @bp.odeint(method='rk4') def integral(self, V, m, h, n, t, Iext): alpha = 0.1 * (V + 40) / (1 - np.exp(-(V + 40) / 10)) beta = 4.0 * np.exp(-(V + 65) / 18) dmdt = alpha * (1 - m) - beta * m alpha = 0.07 * np.exp(-(V + 65) / 20.) beta = 1 / (1 + np.exp(-(V + 35) / 10)) dhdt = alpha * (1 - h) - beta * h alpha = 0.01 * (V + 55) / (1 - np.exp(-(V + 55) / 10)) beta = 0.125 * np.exp(-(V + 65) / 80) dndt = alpha * (1 - n) - beta * n I_Na = (self.gNa * m**3.0 * h) * (V - self.ENa) I_K = (self.gK * n**4.0) * (V - self.EK) I_leak = self.gL * (V - self.EL) dVdt = (-I_Na - I_K - I_leak + Iext) / self.C return dVdt, dmdt, dhdt, dndt def update(self, _t, _i): V, m, h, n = self.integral(self.V, self.m, self.h, self.n, _t, self.input) self.spike[:] = (self.V < self.V_th) * (V >= self.V_th) self.V[:] = V self.m[:] = m self.h[:] = h self.n[:] = n self.input[:] = 0. class AMPA_vec(bp.TwoEndConn): def __init__(self, pre, post, conn, delay=0., g_max=0.10, E=0., tau=2.0, **kwargs): super(AMPA_vec, self).__init__(pre=pre, post=post, **kwargs) # parameters self.g_max = g_max self.E = E self.tau = tau self.delay = delay # connections self.conn = conn(pre.size, post.size) self.pre_ids, self.post_ids = conn.requires('pre_ids', 'post_ids') self.size = len(self.pre_ids) # data self.s = bp.math.numpy.Variable(np.zeros(self.size)) self.g = self.register_constant_delay('g', size=self.size, delay=delay) @bp.odeint def int_s(self, s, t): return -s / self.tau def update(self, _t, _i): for i in range(self.size): pre_id = self.pre_ids[i] self.s[i] = self.int_s(self.s[i], _t) self.s[i] += self.pre.spike[pre_id] self.g.push(i, self.g_max * self.s[i]) post_id = self.post_ids[i] self.post.input[post_id] -= self.g.pull(i) * ( self.post.V[post_id] - self.E) hh = HH(10) ampa = AMPA_vec(pre=hh, post=hh, conn=bp.connect.All2All(), delay=10.) net = bp.Network(hh, ampa) bp.math.jit(net, show_code=True)
#mode = 'matrix' mode = 'vector' print(mode) bcm1 = get_BCM(learning_rate=0.005, w_max=w_max, mode=mode) bcm = bp.SynConn(model=bcm1, pre_group=pre, post_group=post, conn=bp.connect.All2All(), monitors=['w', 'dwdt'], delay=0) bcm.r_th = np.zeros(n_post) bcm.post_r = np.zeros(n_post) bcm.sum_post_r = np.zeros(n_post) net = bp.Network(pre, bcm, post) # group selection group1, duration = bp.inputs.constant_current(([1.5, 1], [0, 1]) * 20) group2, duration = bp.inputs.constant_current(([0, 1], [1., 1]) * 20) input_r = np.vstack(((group1, ) * 10, (group2, ) * 10)) net.run(duration, inputs=(pre, 'ST.r', input_r.T, "="), report=True) if mode == 'matrix': w1 = np.mean(bcm.mon.w[:, :10, 0], 1) w2 = np.mean(bcm.mon.w[:, 10:, 0], 1) else: w1 = np.mean(bcm.mon.w[:, :10], 1)
def test_hh(num, device): print('Scale:{}, Model:HH, Device:{}, '.format(num, device), end='') st_build = time.time() bp.profile.set(jit=True, device=device, dt=0.1, numerical_method='exponential') num_exc = int(num * 0.8) num_inh = int(num * 0.2) num = num_exc + num_inh Cm = 200 # Membrane Capacitance [pF] gl = 10. # Leak Conductance [nS] El = -60. # Resting Potential [mV] g_Na = 20. * 1000 ENa = 50. # reversal potential (Sodium) [mV] g_Kd = 6. * 1000 # K Conductance [nS] EK = -90. # reversal potential (Potassium) [mV] VT = -63. Vt = -20. # Time constants taue = 5. # Excitatory synaptic time constant [ms] taui = 10. # Inhibitory synaptic time constant [ms] # Reversal potentials Ee = 0. # Excitatory reversal potential (mV) Ei = -80. # Inhibitory reversal potential (Potassium) [mV] # excitatory synaptic weight we = 6.0 * np.sqrt(3200) / np.sqrt( num_exc) # excitatory synaptic conductance [nS] # inhibitory synaptic weight wi = 67.0 * np.sqrt(800) / np.sqrt( num_inh) # inhibitory synaptic conductance [nS] inf = 0.05 neu_ST = bp.types.NeuState('V', 'm', 'n', 'h', 'sp', 'ge', 'gi') @bp.integrate def int_ge(ge, t): return -ge / taue @bp.integrate def int_gi(gi, t): return -gi / taui @bp.integrate def int_m(m, t, V): a = 13.0 - V + VT b = V - VT - 40.0 m_alpha = 0.32 * a / (exp(a / 4.) - 1.) m_beta = 0.28 * b / (exp(b / 5.) - 1.) dmdt = (m_alpha * (1. - m) - m_beta * m) return dmdt @bp.integrate def int_m_zeroa(m, t, V): b = V - VT - 40.0 m_alpha = 0.32 m_beta = 0.28 * b / (exp(b / 5.) - 1.) dmdt = (m_alpha * (1. - m) - m_beta * m) return dmdt @bp.integrate def int_m_zerob(m, t, V): a = 13.0 - V + VT m_alpha = 0.32 * a / (exp(a / 4.) - 1.) m_beta = 0.28 dmdt = (m_alpha * (1. - m) - m_beta * m) return dmdt @bp.integrate def int_h(h, t, V): h_alpha = 0.128 * exp((17. - V + VT) / 18.) h_beta = 4. / (1. + exp(-(V - VT - 40.) / 5.)) dhdt = (h_alpha * (1. - h) - h_beta * h) return dhdt @bp.integrate def int_n(n, t, V): c = 15. - V + VT n_alpha = 0.032 * c / (exp(c / 5.) - 1.) n_beta = .5 * exp((10. - V + VT) / 40.) dndt = (n_alpha * (1. - n) - n_beta * n) return dndt @bp.integrate def int_n_zero(n, t, V): n_alpha = 0.032 n_beta = .5 * exp((10. - V + VT) / 40.) dndt = (n_alpha * (1. - n) - n_beta * n) return dndt @bp.integrate def int_V(V, t, m, h, n, ge, gi): g_na_ = g_Na * (m * m * m) * h g_kd_ = g_Kd * (n * n * n * n) dvdt = (gl * (El - V) + ge * (Ee - V) + gi * (Ei - V) - g_na_ * (V - ENa) - g_kd_ * (V - EK)) / Cm return dvdt def neu_update(ST, _t): ST['ge'] = int_ge(ST['ge'], _t) ST['gi'] = int_gi(ST['gi'], _t) if abs(ST['V'] - (40.0 + VT)) < inf: ST['m'] = int_m_zerob(ST['m'], _t, ST['V']) elif abs(ST['V'] - (13.0 + VT)) < inf: ST['m'] = int_m_zeroa(ST['m'], _t, ST['V']) else: ST['m'] = int_m(ST['m'], _t, ST['V']) ST['h'] = int_h(ST['h'], _t, ST['V']) if abs(ST['V'] - (15.0 + VT)) > inf: ST['n'] = int_n(ST['n'], _t, ST['V']) else: ST['n'] = int_n_zero(ST['n'], _t, ST['V']) V = int_V(ST['V'], _t, ST['m'], ST['h'], ST['n'], ST['ge'], ST['gi']) sp = ST['V'] < Vt and V >= Vt ST['sp'] = sp ST['V'] = V neuron = bp.NeuType(name='CUBA-HH', ST=neu_ST, steps=neu_update, mode='scalar') requires_exc = { 'pre': bp.types.NeuState( ['sp'], help='Pre-synaptic neuron state must have "spike" item.'), 'post': bp.types.NeuState( ['ge'], help='Post-synaptic neuron state must have "V" and "input" item.') } def update_syn_exc(ST, pre, post): if pre['sp']: post['ge'] += we exc_syn = bp.SynType(name='exc_syn', ST=bp.types.SynState(), requires=requires_exc, steps=update_syn_exc, mode='scalar') requires_inh = { 'pre': bp.types.NeuState( ['sp'], help='Pre-synaptic neuron state must have "spike" item.'), 'post': bp.types.NeuState( ['gi'], help='Post-synaptic neuron state must have "V" and "input" item.') } def update_syn_inh(ST, pre, post): if pre['sp']: post['gi'] -= wi inh_syn = bp.SynType(name='inh_syn', ST=bp.types.SynState(), requires=requires_inh, steps=update_syn_inh, mode='scalar') group = bp.NeuGroup(neuron, geometry=num) group.ST['V'] = El + (np.random.randn(num_exc + num_inh) * 5. - 5.) group.ST['ge'] = (np.random.randn(num_exc + num_inh) * 1.5 + 4.) * 10. group.ST['gi'] = (np.random.randn(num_exc + num_inh) * 12. + 20.) * 10. exc_conn = bp.SynConn(exc_syn, pre_group=group[:num_exc], post_group=group, conn=bp.connect.FixedProb(prob=0.02)) inh_conn = bp.SynConn(inh_syn, pre_group=group[num_exc:], post_group=group, conn=bp.connect.FixedProb(prob=0.02)) net = bp.Network(group, exc_conn, inh_conn) ed_build = time.time() st_run = time.time() net.run(duration=1000.0) ed_run = time.time() build_time = float(ed_build - st_build) run_time = float(ed_run - st_run) print('BuildT:{:.2f}s, RunT:{:.2f}s'.format(build_time, run_time)) return run_time, build_time
post['gi'][i] += wi inh_syn = bp.SynType('inh_syn', steps=update2, ST=bp.types.SynState()) group = bp.NeuGroup(neuron, geometry=num_exc + num_inh, monitors=['sp']) group.ST['V'] = Vr + np.random.rand(num_exc + num_inh) * (Vt - Vr) exc_conn = bp.SynConn(exc_syn, pre_group=group[:num_exc], post_group=group, conn=bp.connect.FixedProb(prob=0.02)) inh_conn = bp.SynConn(inh_syn, pre_group=group[num_exc:], post_group=group, conn=bp.connect.FixedProb(prob=0.02)) net = bp.Network(group, exc_conn, inh_conn, mode='repeat') t0 = time.time() # net.run(5 * 1000., report_percent=1., report=True) net.run(1250., report=True) net.run((1250., 2500.), report=True) net.run((2500., 3750.), report=True) net.run((3750., 5000.), report=True) print('Used time {} s.'.format(time.time() - t0)) bp.visualize.raster_plot(net.ts, group.mon.sp, show=True)
# build network fr_neu = get_fr_neu() oja_synapse = bpmodels.learning_rules.get_Oja() pre_neu = bp.NeuGroup(fr_neu, geometry=(pre_neu_num, ), monitors=['r']) post_neu = bp.NeuGroup(fr_neu, geometry=(post_neu_num, ), monitors=['r']) pre_neu.ST['r'] = 1. post_neu.ST['r'] = 1. syn = bp.SynConn(oja_synapse, pre_group=pre_neu, post_group=post_neu, conn=bp.connect.All2All(), monitors=['w']) net = bp.Network(pre_neu, syn, post_neu) # create input current_mat_in = [] current_mat_out = [] current1, _ = bp.inputs.constant_current([(2., 20.), (0., 20.)] * 3 + [(0., 20.), (0., 20.)] * 2) current2, _ = bp.inputs.constant_current([(0., 20.), (2., 20.)] * 5) current3, _ = bp.inputs.constant_current([(2., 20.), (0., 20.)] * 5) current_mat_in = np.vstack((current1, current2)) #current_mat_out = np.vstack((current3, current3)) current_mat_out = current3 # simulate network net.run(duration=200., inputs=[(pre_neu, 'ST.r', current_mat_in.T, '='),
target_backend = ['numpy', 'numba'] def __init__(self, pre, post, conn, **kwargs): self.conn = conn(pre.size, post.size) self.pre2post = self.conn.requires('pre2post') super(InhSyn, self).__init__(pre=pre, post=post, **kwargs) def update(self, _t): for pre_id, spike in enumerate(self.pre.spike): if spike > 0: for post_i in self.pre2post[pre_id]: self.post.gi[post_i] += wi E_group = LIF(num_exc, monitors=['spike']) E_group.V = np.random.randn(num_exc) * 5. - 55. I_group = LIF(num_inh, monitors=['spike']) I_group.V = np.random.randn(num_inh) * 5. - 55. E2E = ExcSyn(pre=E_group, post=E_group, conn=bp.connect.FixedProb(0.02)) E2I = ExcSyn(pre=E_group, post=I_group, conn=bp.connect.FixedProb(0.02)) I2E = InhSyn(pre=I_group, post=E_group, conn=bp.connect.FixedProb(0.02)) I2I = InhSyn(pre=I_group, post=I_group, conn=bp.connect.FixedProb(0.02)) net = bp.Network(E_group, I_group, E2E, E2I, I2E, I2I) t0 = time.time() net.run(5000., report=True) print('Used time {} s.'.format(time.time() - t0)) bp.visualize.raster_plot(net.ts, E_group.mon.spike, show=True)
return -s / tau def update(self, _t): self.s = self.int_s(self.s, _t, self.tau) for i in range(self.pre.size[0]): if self.pre.spike[i] > 0: self.s[i] += self.conn_mat[i] self.g.push(self.g_max * self.s) g = self.g.pull() self.post.input -= bp.backend.sum(g, axis=0) * (self.post.V - self.E) if __name__ == '__main__': hh = HH(100, monitors=['V']) ampa = AMPA1_vec(pre=hh, post=hh, conn=bp.connect.All2All(), delay=10., monitors=['s']) net = bp.Network(hh, ampa) net.run(100., inputs=(hh, 'input', 10.), report=True) fig, gs = bp.visualize.get_figure( row_num=2, col_num=1, ) fig.add_subplot(gs[0, 0]) bp.visualize.line_plot(hh.mon.ts, hh.mon.V) fig.add_subplot(gs[1, 0]) bp.visualize.line_plot(ampa.mon.ts, ampa.mon.s, show=True)
def run_brianpy(num_neu, duration, device='cpu'): num_inh = int(num_neu / 5) num_exc = num_neu - num_inh bp.profile.set(jit=True, device=device, dt=dt) # Parameters taum = 20 taue = 5 taui = 10 Vt = -50 Vr = -60 El = -60 Erev_exc = 0. Erev_inh = -80. I = 20. we = 0.6 # excitatory synaptic weight (voltage) wi = 6.7 # inhibitory synaptic weight ref = 5.0 neu_ST = bp.types.NeuState({ 'sp_t': -1e7, 'V': Vr, 'spike': 0., 'ge': 0., 'gi': 0. }) @bp.integrate def int_ge(ge, t): return -ge / taue @bp.integrate def int_gi(gi, t): return -gi / taui @bp.integrate def int_V(V, t, ge, gi): return (ge * (Erev_exc - V) + gi * (Erev_inh - V) + (El - V) + I) / taum def neu_update(ST, _t): ST['ge'] = int_ge(ST['ge'], _t) ST['gi'] = int_gi(ST['gi'], _t) ST['spike'] = 0. if (_t - ST['sp_t']) > ref: V = int_V(ST['V'], _t, ST['ge'], ST['gi']) ST['spike'] = 0. if V >= Vt: ST['V'] = Vr ST['spike'] = 1. ST['sp_t'] = _t else: ST['V'] = V neuron = bp.NeuType(name='COBA', ST=neu_ST, steps=neu_update, mode='scalar') def syn_update1(pre, post, pre2post): for pre_id in range(len(pre2post)): if pre['spike'][pre_id] > 0.: post_ids = pre2post[pre_id] for i in post_ids: post['ge'][i] += we exc_syn = bp.SynType('exc_syn', steps=syn_update1, ST=bp.types.SynState([]), mode='vector') def syn_update2(pre, post, pre2post): for pre_id in range(len(pre2post)): if pre['spike'][pre_id] > 0.: post_ids = pre2post[pre_id] for i in post_ids: post['gi'][i] += wi inh_syn = bp.SynType('inh_syn', steps=syn_update2, ST=bp.types.SynState([]), mode='vector') group = bp.NeuGroup(neuron, geometry=num_exc + num_inh) group.ST['V'] = np.random.randn(num_exc + num_inh) * 5. - 55. exc_conn = bp.SynConn(exc_syn, pre_group=group[:num_exc], post_group=group, conn=bp.connect.FixedProb(prob=0.02)) inh_conn = bp.SynConn(inh_syn, pre_group=group[num_exc:], post_group=group, conn=bp.connect.FixedProb(prob=0.02)) net = bp.Network(group, exc_conn, inh_conn) t0 = time.time() net.run(duration) t = time.time() - t0 print(f'BrainPy ({device}) used time {t} s.') return t
post = bp.NeuGroup(LIF_neuron, geometry=(10, ), monitors=['V', 'input', 'spike']) post.runner.set_schedule(['input', 'update', 'monitor', 'reset']) post.pars['V_rest'] = -65. post.ST['V'] = -65. gabaa = bp.SynConn(model=GABAa_syn, pre_group=pre, post_group=post, conn=bp.connect.All2All(), monitors=['s'], delay=10.) gabaa.runner.set_schedule(['input', 'update', 'output', 'monitor']) net = bp.Network(pre, gabaa, post) current = bp.inputs.spike_current([10, 110, 210, 300, 305, 310, 315, 320], bp.profile._dt, 1., duration=duration) net.run(duration=duration, inputs=[gabaa, 'pre.spike', current, "="], report=True) # paint gabaa ts = net.ts fig, gs = bp.visualize.get_figure(2, 2, 5, 6) fig.add_subplot(gs[0, 0]) plt.plot(ts, gabaa.mon.s[:, 0], label='s')
# post['gi'][post_ids] += wi for p_id in post_ids: post['gi'][p_id] += wi exc_syn = bp.SynType('exc_syn', steps=exc_update, ST=bp.types.SynState()) inh_syn = bp.SynType('inh_syn', steps=inh_update, ST=bp.types.SynState()) group = bp.NeuGroup(neuron, size=num_exc + num_inh, monitors=['sp']) group.ST['V'] = El + (np.random.randn(num_exc + num_inh) * 5 - 5) group.ST['ge'] = (np.random.randn(num_exc + num_inh) * 1.5 + 4) * 10. group.ST['gi'] = (np.random.randn(num_exc + num_inh) * 12 + 20) * 10. exc_conn = bp.TwoEndConn(exc_syn, pre=group[:num_exc], post=group, conn=bp.connect.FixedProb(prob=0.02)) inh_conn = bp.TwoEndConn(inh_syn, pre=group[num_exc:], post=group, conn=bp.connect.FixedProb(prob=0.02)) net = bp.Network(group, exc_conn, inh_conn) t0 = time.time() net.run(duration, report=True) print('Used time {} s.'.format(time.time() - t0)) bp.visualize.raster_plot(net.ts, group.mon.sp, show=True)
self.gNa, self.ENa, self.gK, self.EK, self.gL, self.EL, self.C, self.phi) self.spike = (self.V < self.V_th) * (V >= self.V_th) self.V = V self.h = h self.n = n self.input[:] = 0 num = 100 neu = HH(num, monitors=['spike', 'V']) neu.V = -70. + bp.ops.normal(size=num) * 20 syn = GABAa(pre=neu, post=neu, conn=bp.connect.All2All(include_self=False)) syn.g_max = 0.1 / num net = bp.Network(neu, syn) net.run(duration=500., inputs=[neu, 'input', 1.], report=True) fig, gs = bp.visualize.get_figure(2, 1, 3, 8) xlim = (net.t_start - 0.1, net.t_end + 0.1) fig.add_subplot(gs[0, 0]) bp.visualize.line_plot(net.ts, neu.mon.V, xlim=xlim, ylabel='Membrane potential (N0)') fig.add_subplot(gs[1, 0]) bp.visualize.raster_plot(net.ts, neu.mon.spike, xlim=xlim, show=True)