Skip to content

Commit 683e5cb

Browse files
authored
update contro flow, integrators, operators, and docs (#205)
update contro flow, integrators, operators, and docs
2 parents fc16951 + 8ef3a56 commit 683e5cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+3129
-3848
lines changed

brainpy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
__version__ = "2.1.10"
3+
__version__ = "2.1.11"
44

55

66
try:

brainpy/datasets/chaotic_systems.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import jax.numpy as jnp
44

55
from brainpy import math as bm, dyn
6-
from brainpy.integrators import odeint, ddeint, JointEq, IntegratorRunner
6+
from brainpy.integrators import odeint, JointEq, IntegratorRunner
77

88
__all__ = [
99
'henon_map_series',
@@ -172,8 +172,7 @@ def mackey_glass_series(duration, dt=0.1, beta=2., gamma=1., tau=2., n=9.65,
172172
xdelay = bm.TimeDelay(inits, tau, dt=dt, interp_method='round')
173173
xdelay.data.value = inits + 0.2 * (rng.random((xdelay.num_delay_step,) + inits.shape) - 0.5)
174174

175-
@ddeint(method=method,
176-
state_delays={'x': xdelay})
175+
@odeint(method=method, state_delays={'x': xdelay})
177176
def mg_eq(x, t):
178177
xtau = xdelay(t - tau)
179178
return beta * xtau / (1 + xtau ** n) - gamma * x

brainpy/dyn/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
"""
66

77

8+
from .base import *
89
from .neurons import *
910
from .synapses import *
1011
from .channels import *
11-
from .base import *
12+
from .others import *
1213
from .utils import *
1314
from .runners import *
1415

15-
from . import neurons, synapses, channels, rates, utils, runners
16+
from . import neurons, synapses, channels, rates, others, utils, runners

brainpy/dyn/networks/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# -*- coding: utf-8 -*-

brainpy/dyn/neurons/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22

33
from .biological_models import *
44
from .fractional_models import *
5-
from .input_models import *
65
from .reduced_models import *

brainpy/dyn/neurons/biological_models.py

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
'HH',
1515
'MorrisLecar',
1616
'PinskyRinzelModel',
17+
'WangBuzsakiModel',
1718
]
1819

1920

@@ -204,6 +205,7 @@ def __init__(
204205
h_initializer: Union[Initializer, Callable, Tensor] = OneInit(0.6),
205206
n_initializer: Union[Initializer, Callable, Tensor] = OneInit(0.32),
206207
method: str = 'exp_auto',
208+
keep_size: bool = False,
207209
name: str = None
208210
):
209211
# initialization
@@ -287,6 +289,7 @@ def update(self, t, dt):
287289
self.input[:] = 0.
288290

289291

292+
290293
class MorrisLecar(NeuGroup):
291294
r"""The Morris-Lecar neuron model.
292295
@@ -383,6 +386,7 @@ def __init__(
383386
W_initializer: Union[Callable, Initializer, Tensor] = OneInit(0.02),
384387
V_initializer: Union[Callable, Initializer, Tensor] = Uniform(-70., -60.),
385388
method: str = 'exp_auto',
389+
keep_size: bool = False,
386390
name: str = None
387391
):
388392
# initialization
@@ -626,6 +630,7 @@ def __init__(
626630
Ca_initializer: Union[Initializer, Callable, Tensor] = OneInit(0.2),
627631
# others
628632
method: str = 'exp_auto',
633+
keep_size: bool = False,
629634
name: str = None,
630635
):
631636
# initialization
@@ -805,3 +810,185 @@ def inf_q(self, Ca):
805810
alpha = self.alpha_q(Ca)
806811
beta = self.beta_q(Ca)
807812
return alpha / (alpha + beta)
813+
814+
815+
class WangBuzsakiModel(NeuGroup):
816+
r"""Wang-Buzsaki model [9]_, an implementation of a modified Hodgkin-Huxley model.
817+
818+
Each model is described by a single compartment and obeys the current balance equation:
819+
820+
.. math::
821+
822+
C_{m} \frac{d V}{d t}=-I_{\mathrm{Na}}-I_{\mathrm{K}}-I_{\mathrm{L}}-I_{\mathrm{syn}}+I_{\mathrm{app}}
823+
824+
where :math:`C_{m}=1 \mu \mathrm{F} / \mathrm{cm}^{2}` and :math:`I_{\mathrm{app}}` is the
825+
injected current (in :math:`\mu \mathrm{A} / \mathrm{cm}^{2}` ). The leak current
826+
:math:`I_{\mathrm{L}}=g_{\mathrm{L}}\left(V-E_{\mathrm{L}}\right)` has a conductance
827+
:math:`g_{\mathrm{L}}=0.1 \mathrm{mS} / \mathrm{cm}^{2}`, so that the passive time constant
828+
:math:`\tau_{0}=C_{m} / g_{\mathrm{L}}=10 \mathrm{msec} ; E_{\mathrm{L}}=-65 \mathrm{mV}`.
829+
830+
The spike-generating :math:`\mathrm{Na}^{+}` and :math:`\mathrm{K}^{+}` voltage-dependent ion
831+
currents :math:`\left(I_{\mathrm{Na}}\right.` and :math:`I_{\mathrm{K}}` ) are of the
832+
Hodgkin-Huxley type (Hodgkin and Huxley, 1952). The transient sodium current
833+
:math:`I_{\mathrm{Na}}=g_{\mathrm{Na}} m_{\infty}^{3} h\left(V-E_{\mathrm{Na}}\right)`,
834+
where the activation variable :math:`m` is assumed fast and substituted by its steady-state
835+
function :math:`m_{\infty}=\alpha_{m} /\left(\alpha_{m}+\beta_{m}\right)` ;
836+
:math:`\alpha_{m}(V)=-0.1(V+35) /(\exp (-0.1(V+35))-1), \beta_{m}(V)=4 \exp (-(V+60) / 18)`.
837+
The inactivation variable :math:`h` obeys a first-order kinetics:
838+
839+
.. math::
840+
841+
\frac{d h}{d t}=\phi\left(\alpha_{h}(1-h)-\beta_{h} h\right)
842+
843+
where :math:`\alpha_{h}(V)=0.07 \exp (-(V+58) / 20)` and
844+
:math:`\beta_{h}(V)=1 /(\exp (-0.1(V+28)) +1) \cdot g_{\mathrm{Na}}=35 \mathrm{mS} / \mathrm{cm}^{2}` ;
845+
:math:`E_{\mathrm{Na}}=55 \mathrm{mV}, \phi=5 .`
846+
847+
The delayed rectifier :math:`I_{\mathrm{K}}=g_{\mathrm{K}} n^{4}\left(V-E_{\mathrm{K}}\right)`,
848+
where the activation variable :math:`n` obeys the following equation:
849+
850+
.. math::
851+
852+
\frac{d n}{d t}=\phi\left(\alpha_{n}(1-n)-\beta_{n} n\right)
853+
854+
with :math:`\alpha_{n}(V)=-0.01(V+34) /(\exp (-0.1(V+34))-1)` and
855+
:math:`\beta_{n}(V)=0.125\exp (-(V+44) / 80)` ; :math:`g_{\mathrm{K}}=9 \mathrm{mS} / \mathrm{cm}^{2}`, and
856+
:math:`E_{\mathrm{K}}=-90 \mathrm{mV}`.
857+
858+
859+
Parameters
860+
----------
861+
size: sequence of int, int
862+
The size of the neuron group.
863+
ENa: float, JaxArray, ndarray, Initializer, callable
864+
The reversal potential of sodium. Default is 50 mV.
865+
gNa: float, JaxArray, ndarray, Initializer, callable
866+
The maximum conductance of sodium channel. Default is 120 msiemens.
867+
EK: float, JaxArray, ndarray, Initializer, callable
868+
The reversal potential of potassium. Default is -77 mV.
869+
gK: float, JaxArray, ndarray, Initializer, callable
870+
The maximum conductance of potassium channel. Default is 36 msiemens.
871+
EL: float, JaxArray, ndarray, Initializer, callable
872+
The reversal potential of learky channel. Default is -54.387 mV.
873+
gL: float, JaxArray, ndarray, Initializer, callable
874+
The conductance of learky channel. Default is 0.03 msiemens.
875+
V_th: float, JaxArray, ndarray, Initializer, callable
876+
The threshold of the membrane spike. Default is 20 mV.
877+
C: float, JaxArray, ndarray, Initializer, callable
878+
The membrane capacitance. Default is 1 ufarad.
879+
phi: float, JaxArray, ndarray, Initializer, callable
880+
The temperature regulator constant.
881+
V_initializer: JaxArray, ndarray, Initializer, callable
882+
The initializer of membrane potential.
883+
h_initializer: JaxArray, ndarray, Initializer, callable
884+
The initializer of h channel.
885+
n_initializer: JaxArray, ndarray, Initializer, callable
886+
The initializer of n channel.
887+
method: str
888+
The numerical integration method.
889+
name: str
890+
The group name.
891+
892+
References
893+
----------
894+
.. [9] Wang, X.J. and Buzsaki, G., (1996) Gamma oscillation by synaptic
895+
inhibition in a hippocampal interneuronal network model. Journal of
896+
neuroscience, 16(20), pp.6402-6413.
897+
898+
"""
899+
900+
def __init__(
901+
self,
902+
size: Shape,
903+
ENa: Union[float, Tensor, Initializer, Callable] = 55.,
904+
gNa: Union[float, Tensor, Initializer, Callable] = 35.,
905+
EK: Union[float, Tensor, Initializer, Callable] = -90.,
906+
gK: Union[float, Tensor, Initializer, Callable] = 9.,
907+
EL: Union[float, Tensor, Initializer, Callable] = -65,
908+
gL: Union[float, Tensor, Initializer, Callable] = 0.1,
909+
V_th: Union[float, Tensor, Initializer, Callable] = 20.,
910+
phi: Union[float, Tensor, Initializer, Callable] = 5.0,
911+
C: Union[float, Tensor, Initializer, Callable] = 1.0,
912+
V_initializer: Union[Initializer, Callable, Tensor] = OneInit(-65.),
913+
h_initializer: Union[Initializer, Callable, Tensor] = OneInit(0.6),
914+
n_initializer: Union[Initializer, Callable, Tensor] = OneInit(0.32),
915+
method: str = 'exp_auto',
916+
keep_size: bool = False,
917+
name: str = None
918+
):
919+
# initialization
920+
super(WangBuzsakiModel, self).__init__(size=size, name=name)
921+
922+
# parameters
923+
self.ENa = init_param(ENa, self.num, allow_none=False)
924+
self.EK = init_param(EK, self.num, allow_none=False)
925+
self.EL = init_param(EL, self.num, allow_none=False)
926+
self.gNa = init_param(gNa, self.num, allow_none=False)
927+
self.gK = init_param(gK, self.num, allow_none=False)
928+
self.gL = init_param(gL, self.num, allow_none=False)
929+
self.C = init_param(C, self.num, allow_none=False)
930+
self.phi = init_param(phi, self.num, allow_none=False)
931+
self.V_th = init_param(V_th, self.num, allow_none=False)
932+
933+
# initializers
934+
check_initializer(h_initializer, 'h_initializer', allow_none=False)
935+
check_initializer(n_initializer, 'n_initializer', allow_none=False)
936+
check_initializer(V_initializer, 'V_initializer', allow_none=False)
937+
self._h_initializer = h_initializer
938+
self._n_initializer = n_initializer
939+
self._V_initializer = V_initializer
940+
941+
# variables
942+
self.h = bm.Variable(init_param(self._h_initializer, (self.num,)))
943+
self.n = bm.Variable(init_param(self._n_initializer, (self.num,)))
944+
self.V = bm.Variable(init_param(self._V_initializer, (self.num,)))
945+
self.input = bm.Variable(bm.zeros(self.num))
946+
self.spike = bm.Variable(bm.zeros(self.num, dtype=bool))
947+
948+
# integral
949+
self.integral = odeint(method=method, f=self.derivative)
950+
951+
def reset(self):
952+
self.h.value = init_param(self._h_initializer, (self.num,))
953+
self.n.value = init_param(self._n_initializer, (self.num,))
954+
self.V.value = init_param(self._V_initializer, (self.num,))
955+
self.input[:] = 0
956+
self.spike[:] = False
957+
958+
def m_inf(self, V):
959+
alpha = -0.1 * (V + 35) / (bm.exp(-0.1 * (V + 35)) - 1)
960+
beta = 4. * bm.exp(-(V + 60.) / 18.)
961+
return alpha / (alpha + beta)
962+
963+
def dh(self, h, t, V):
964+
alpha = 0.07 * bm.exp(-(V + 58) / 20)
965+
beta = 1 / (bm.exp(-0.1 * (V + 28)) + 1)
966+
dhdt = alpha * (1 - h) - beta * h
967+
return self.phi * dhdt
968+
969+
def dn(self, n, t, V):
970+
alpha = -0.01 * (V + 34) / (bm.exp(-0.1 * (V + 34)) - 1)
971+
beta = 0.125 * bm.exp(-(V + 44) / 80)
972+
dndt = alpha * (1 - n) - beta * n
973+
return self.phi * dndt
974+
975+
def dV(self, V, t, h, n, I_ext):
976+
INa = self.gNa * self.m_inf(V) ** 3 * h * (V - self.ENa)
977+
IK = self.gK * n ** 4 * (V - self.EK)
978+
IL = self.gL * (V - self.EL)
979+
dVdt = (- INa - IK - IL + I_ext) / self.C
980+
return dVdt
981+
982+
@property
983+
def derivative(self):
984+
return JointEq([self.dV, self.dh, self.dn])
985+
986+
def update(self, t, dt):
987+
V, h, n = self.integral(self.V, self.h, self.n, t, self.input, dt=dt)
988+
self.spike.value = bm.logical_and(self.V < self.V_th, V >= self.V_th)
989+
self.V.value = V
990+
self.h.value = h
991+
self.n.value = n
992+
self.input[:] = 0.
993+
994+

brainpy/dyn/neurons/fractional_models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ def __init__(
9393
V_initializer: Union[Initializer, Callable, Tensor] = OneInit(2.5),
9494
w_initializer: Union[Initializer, Callable, Tensor] = ZeroInit(),
9595
y_initializer: Union[Initializer, Callable, Tensor] = ZeroInit(),
96-
name: str = None
96+
name: str = None,
97+
keep_size: bool = False,
9798
):
9899
super(FractionalFHR, self).__init__(size, name=name)
99100

@@ -128,7 +129,7 @@ def __init__(
128129
# integral function
129130
self.integral = GLShortMemory(self.derivative,
130131
alpha=alpha,
131-
num_memory=num_memory,
132+
num_step=num_memory,
132133
inits=[self.V, self.w, self.y])
133134

134135
def reset(self):
@@ -233,6 +234,7 @@ def __init__(
233234
V_th: Union[float, Tensor, Initializer, Callable] = 30.,
234235
V_initializer: Union[Initializer, Callable, Tensor] = OneInit(-65.),
235236
u_initializer: Union[Initializer, Callable, Tensor] = OneInit(0.20 * -65.),
237+
keep_size: bool = False,
236238
name: str = None
237239
):
238240
# initialization

brainpy/dyn/neurons/reduced_models.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def __init__(
8989
V_initializer: Union[Initializer, Callable, Tensor] = ZeroInit(),
9090
noise: Union[float, Tensor, Initializer, Callable] = None,
9191
noise_type: str = 'value',
92-
keep_size: bool=False,
92+
keep_size: bool = False,
9393
method: str = 'exp_auto',
9494
name: str = None
9595
):
@@ -99,18 +99,15 @@ def __init__(
9999
# parameters
100100
self.keep_size = keep_size
101101
self.noise_type = noise_type
102-
if noise_type not in ['func', 'value']:
103-
raise ValueError(f'noise_type only supports `func` and `value`, but we got {noise_type}')
104102
size = self.size if keep_size else self.num
105103
self.V_rest = init_param(V_rest, size, allow_none=False)
106104
self.V_reset = init_param(V_reset, size, allow_none=False)
107105
self.V_th = init_param(V_th, size, allow_none=False)
108106
self.tau = init_param(tau, size, allow_none=False)
109107
self.tau_ref = init_param(tau_ref, size, allow_none=False)
110-
if noise_type == 'func':
111-
self.noise = noise
112-
else:
113-
self.noise = init_param(noise, size, allow_none=True)
108+
if noise_type not in ['func', 'value']:
109+
raise ValueError(f'noise_type only supports `func` and `value`, but we got {noise_type}')
110+
self.noise = noise if (noise_type == 'func') else init_param(noise, size, allow_none=True)
114111

115112
# initializers
116113
check_initializer(V_initializer, 'V_initializer')
@@ -260,6 +257,7 @@ def __init__(
260257
tau: Union[float, Tensor, Initializer, Callable] = 10.,
261258
tau_ref: Union[float, Tensor, Initializer, Callable] = 1.7,
262259
V_initializer: Union[Initializer, Callable, Tensor] = ZeroInit(),
260+
keep_size: bool = False,
263261
method: str = 'exp_auto',
264262
name: str = None
265263
):
@@ -403,6 +401,7 @@ def __init__(
403401
V_initializer: Union[Initializer, Callable, Tensor] = ZeroInit(),
404402
w_initializer: Union[Initializer, Callable, Tensor] = ZeroInit(),
405403
method: str = 'exp_auto',
404+
keep_size: bool = False,
406405
name: str = None
407406
):
408407
super(AdExIF, self).__init__(size=size, name=name)
@@ -543,6 +542,7 @@ def __init__(
543542
tau: Union[float, Tensor, Initializer, Callable] = 10.,
544543
tau_ref: Union[float, Tensor, Initializer, Callable] = 0.,
545544
V_initializer: Union[Initializer, Callable, Tensor] = ZeroInit(),
545+
keep_size: bool = False,
546546
method: str = 'exp_auto',
547547
name: str = None
548548
):
@@ -688,6 +688,7 @@ def __init__(
688688
V_initializer: Union[Initializer, Callable, Tensor] = ZeroInit(),
689689
w_initializer: Union[Initializer, Callable, Tensor] = ZeroInit(),
690690
method: str = 'exp_auto',
691+
keep_size: bool = False,
691692
name: str = None
692693
):
693694
super(AdQuaIF, self).__init__(size=size, name=name)
@@ -851,6 +852,7 @@ def __init__(
851852
I2_initializer: Union[Initializer, Callable, Tensor] = ZeroInit(),
852853
Vth_initializer: Union[Initializer, Callable, Tensor] = OneInit(-50.),
853854
method: str = 'exp_auto',
855+
keep_size: bool = False,
854856
name: str = None
855857
):
856858
# initialization
@@ -1013,6 +1015,7 @@ def __init__(
10131015
V_initializer: Union[Initializer, Callable, Tensor] = ZeroInit(),
10141016
u_initializer: Union[Initializer, Callable, Tensor] = OneInit(),
10151017
method: str = 'exp_auto',
1018+
keep_size: bool = False,
10161019
name: str = None
10171020
):
10181021
# initialization
@@ -1185,6 +1188,7 @@ def __init__(
11851188
y_initializer: Union[Initializer, Callable, Tensor] = OneInit(-10.),
11861189
z_initializer: Union[Initializer, Callable, Tensor] = ZeroInit(),
11871190
method: str = 'exp_auto',
1191+
keep_size: bool = False,
11881192
name: str = None
11891193
):
11901194
# initialization
@@ -1340,6 +1344,7 @@ def __init__(
13401344
V_initializer: Union[Initializer, Callable, Tensor] = ZeroInit(),
13411345
w_initializer: Union[Initializer, Callable, Tensor] = ZeroInit(),
13421346
method: str = 'exp_auto',
1347+
keep_size: bool = False,
13431348
name: str = None
13441349
):
13451350
# initialization

0 commit comments

Comments
 (0)