Skip to content

Commit dfb6a69

Browse files
update state neuron and synapse (#794)
* Update version to 2.7.1 and refactor Dynamics class: enhance input handling and add new properties * Update version to 2.7.1 and refactor Dynamics class: enhance input handling and add new properties * Add state version implementations for neuron and synapse models This commit implements state versions of several neuron and synapse models using the brainstate framework, following BrainPy v2.7+ architecture. Neuron Models Added: - LIF (Leaky Integrate-and-Fire) neurons with multiple variants: * LIF: Basic LIF neuron with exponential synaptic input * LifRef: LIF with refractory period * ExpIF: Exponential Integrate-and-Fire * ExpIFRef: ExpIF with refractory period * AdExIF: Adaptive Exponential Integrate-and-Fire * AdExIFRef: AdExIF with refractory period * QuaIF: Quadratic Integrate-and-Fire * QuaIFRef: QuaIF with refractory period * AdQuaIF: Adaptive Quadratic Integrate-and-Fire * AdQuaIFRef: AdQuaIF with refractory period * GifRef: Generalized Integrate-and-Fire with refractory - Izhikevich neuron model with variants: * Izhikevich: Basic Izhikevich neuron * IzhikevichRef: With refractory period - Hodgkin-Huxley (HH) neuron model: * HH: Classic Hodgkin-Huxley model with Na+ and K+ channels Synapse Models Added: - BioNMDA: Biological NMDA receptor with second-order kinetics * Implements two-state cascade dynamics (x and g variables) * Slower rise time compared to AMPA (biologically realistic) * Comprehensive documentation with mathematical formulation Testing: - Comprehensive test suites added for all models - AMPA and GABAa synapse tests added - All tests passing with proper unit handling Key Features: - Uses brainstate ecosystem (HiddenState, ShortTermState, LongTermState) - Proper unit support with brainunit - Exponential Euler integration for numerical stability - Batch processing support - Consistent API design across all models Files Modified: - brainpy/state/_lif.py: Added LIF variants - brainpy/state/_izhikevich.py: Added Izhikevich variants (new file) - brainpy/state/_hh.py: Added HH model (new file) - brainpy/state/_synapse.py: Added BioNMDA model - brainpy/state/_synapse_test.py: Added comprehensive tests - brainpy/state/_lif_test.py: Added LIF tests - brainpy/state/__init__.py: Updated exports - brainpy/dyn/neurons/lif.py: Minor documentation updates * Add Examples sections to neuron classes in _lif.py Added comprehensive Examples sections to 5 neuron classes that were missing them: - QuaIF: Quadratic Integrate-and-Fire neuron - AdQuaIF: Adaptive Quadratic Integrate-and-Fire neuron - AdQuaIFRef: AdQuaIF with refractory period - Gif: Generalized Integrate-and-Fire neuron - GifRef: Gif with refractory period Each Examples section includes: - Import statements for required modules - Basic usage with parameter specifications - State initialization examples - Update and spike generation examples - Network integration with brainstate.nn.Sequential - Additional Notes highlighting key features All 13 neuron classes in _lif.py now have complete documentation with Examples sections following consistent format and style. * Refactor module assignments to 'brainpy.state' for consistency across files * Update import paths in _base.py: change references from brainstate to brainpy for consistency * fix tests * Update import paths in _inputs.py: change references from brainpy to brainpy.state for consistency --------- Co-authored-by: Chaoming Wang <[email protected]> Co-authored-by: Chaoming Wang <[email protected]>
1 parent cd22e00 commit dfb6a69

20 files changed

+4239
-256
lines changed

brainpy/dyn/neurons/lif.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,10 @@ class ExpIF(ExpIFLTC):
857857
conductance-based synaptic drive." Physical Review E 76, no. 2 (2007): 021919.
858858
.. [5] https://en.wikipedia.org/wiki/Exponential_integrate-and-fire
859859
860+
.. seealso::
861+
862+
:class:`brainpy.state.ExpIF` provides the state-based formulation of this neuron.
863+
860864
**Examples**
861865
862866
There is a simple usage example::
@@ -978,6 +982,10 @@ class ExpIFRefLTC(ExpIFLTC):
978982
conductance-based synaptic drive." Physical Review E 76, no. 2 (2007): 021919.
979983
.. [5] https://en.wikipedia.org/wiki/Exponential_integrate-and-fire
980984
985+
.. seealso::
986+
987+
:class:`brainpy.state.ExpIFRef` provides the state-based formulation of this neuron.
988+
981989
**Examples**
982990
983991
There is a simple usage example::
@@ -1319,6 +1327,10 @@ class AdExIFLTC(GradNeuDyn):
13191327
inputs." Journal of Neuroscience 23.37 (2003): 11628-11640.
13201328
.. [2] http://www.scholarpedia.org/article/Adaptive_exponential_integrate-and-fire_model
13211329
1330+
.. seealso::
1331+
1332+
:class:`brainpy.state.AdExIF` provides the state-based formulation of this model.
1333+
13221334
**Examples**
13231335
13241336
An example usage:

brainpy/state/__init__.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818
from ._base import __all__ as base_all
1919
from ._exponential import *
2020
from ._exponential import __all__ as exp_all
21+
from ._hh import *
22+
from ._hh import __all__ as hh_all
2123
from ._inputs import *
2224
from ._inputs import __all__ as inputs_all
25+
from ._izhikevich import *
26+
from ._izhikevich import __all__ as izh_all
2327
from ._lif import *
2428
from ._lif import __all__ as neuron_all
2529
from ._projection import *
@@ -34,13 +38,8 @@
3438
from ._synaptic_projection import __all__ as synproj_all
3539
from ._synouts import *
3640
from ._synouts import __all__ as synout_all
37-
from .. import mixin
3841

39-
__main__ = ['version2', 'mixin'] + inputs_all + neuron_all + readout_all + stp_all + synapse_all
42+
__main__ = inputs_all + neuron_all + izh_all + hh_all + readout_all + stp_all + synapse_all
4043
__main__ = __main__ + synout_all + base_all + exp_all + proj_all + synproj_all
41-
del inputs_all, neuron_all, readout_all, stp_all, synapse_all, synout_all, base_all
44+
del inputs_all, neuron_all, izh_all, hh_all, readout_all, stp_all, synapse_all, synout_all, base_all
4245
del exp_all, proj_all, synproj_all
43-
44-
if __name__ == '__main__':
45-
mixin
46-

brainpy/state/_base.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ def _input_label_repr(name: str, label: Optional[str] = None):
4040

4141

4242
class Dynamics(brainstate.nn.Dynamics):
43+
__module__ = 'brainpy.state'
44+
4345
def __init__(self, in_size: Size, name: Optional[str] = None):
4446
# initialize
4547
super().__init__(name=name, in_size=in_size)
@@ -401,21 +403,21 @@ def align_pre(self, dyn: Union[ParamDescriber[T], T]) -> T:
401403
Examples
402404
--------
403405
>>> import brainstate
404-
>>> n1 = brainstate.nn.LIF(10)
405-
>>> n1.align_pre(brainstate.nn.Expon.desc(n1.varshape)) # n2 will run after n1
406+
>>> n1 = brainpy.state.LIF(10)
407+
>>> n1.align_pre(brainpy.state.Expon.desc(n1.varshape)) # n2 will run after n1
406408
"""
407409
if isinstance(dyn, Dynamics):
408-
self._add_after_update(id(dyn), dyn)
410+
self.add_after_update(id(dyn), dyn)
409411
return dyn
410412
elif isinstance(dyn, ParamDescriber):
411413
if not issubclass(dyn.cls, Dynamics):
412414
raise TypeError(f'The input {dyn} should be an instance of {Dynamics}.')
413-
if not self._has_after_update(dyn.identifier):
414-
self._add_after_update(
415+
if not self.has_after_update(dyn.identifier):
416+
self.add_after_update(
415417
dyn.identifier,
416418
dyn() if ('in_size' in dyn.kwargs or len(dyn.args) > 0) else dyn(in_size=self.varshape)
417419
)
418-
return self._get_after_update(dyn.identifier)
420+
return self.get_after_update(dyn.identifier)
419421
else:
420422
raise TypeError(f'The input {dyn} should be an instance of {Dynamics} or a delayed initializer.')
421423

@@ -425,7 +427,7 @@ class Neuron(Dynamics):
425427
Base class for all spiking neuron models.
426428
427429
This abstract class serves as the foundation for implementing various spiking neuron
428-
models in the BrainPy framework. It extends the ``brainstate.nn.Dynamics`` class and
430+
models in the BrainPy framework. It extends the ``brainpy.state.Dynamics`` class and
429431
provides common functionality for spike generation, membrane potential dynamics, and
430432
surrogate gradient handling required for training spiking neural networks.
431433
@@ -595,7 +597,7 @@ class Neuron(Dynamics):
595597
.. [3] Gerstner, W., Kistler, W. M., Naud, R., & Paninski, L. (2014). Neuronal dynamics:
596598
From single neurons to networks and models of cognition. Cambridge University Press.
597599
"""
598-
__module__ = 'brainpy'
600+
__module__ = 'brainpy.state'
599601

600602
def __init__(
601603
self,
@@ -849,4 +851,4 @@ class Synapse(Dynamics):
849851
.. [3] Gerstner, W., Kistler, W. M., Naud, R., & Paninski, L. (2014). Neuronal dynamics:
850852
From single neurons to networks and models of cognition. Cambridge University Press.
851853
"""
852-
__module__ = 'brainpy'
854+
__module__ = 'brainpy.state'

brainpy/state/_base_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def test_neuron_soft_vs_hard_reset(self):
148148
def test_neuron_module_attribute(self):
149149
"""Test __module__ attribute is correctly set."""
150150
neuron = Neuron(in_size=self.in_size)
151-
self.assertEqual(neuron.__module__, 'brainpy')
151+
self.assertEqual(neuron.__module__, 'brainpy.state')
152152

153153

154154
class TestSynapseBaseClass(unittest.TestCase):
@@ -247,7 +247,7 @@ def update(self, x=None):
247247
def test_synapse_module_attribute(self):
248248
"""Test __module__ attribute is correctly set."""
249249
synapse = Synapse(in_size=self.in_size)
250-
self.assertEqual(synapse.__module__, 'brainpy')
250+
self.assertEqual(synapse.__module__, 'brainpy.state')
251251

252252
def test_synapse_varshape_attribute(self):
253253
"""Test varshape attribute is correctly set."""

brainpy/state/_exponential.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class Expon(Synapse, AlignPost):
7171
where synaptic variables are aligned with post-synaptic neurons, enabling event-driven
7272
computation and more efficient handling of sparse connectivity patterns.
7373
"""
74-
__module__ = 'brainpy'
74+
__module__ = 'brainpy.state'
7575

7676
def __init__(
7777
self,
@@ -156,7 +156,7 @@ class DualExpon(Synapse, AlignPost):
156156
where synaptic variables are aligned with post-synaptic neurons, enabling event-driven
157157
computation and more efficient handling of sparse connectivity patterns.
158158
"""
159-
__module__ = 'brainpy'
159+
__module__ = 'brainpy.state'
160160

161161
def __init__(
162162
self,

0 commit comments

Comments
 (0)