brainstate 0.0.1__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. brainstate/__init__.py +45 -0
  2. brainstate/_module.py +1466 -0
  3. brainstate/_module_test.py +133 -0
  4. brainstate/_state.py +378 -0
  5. brainstate/_state_test.py +41 -0
  6. brainstate/_utils.py +21 -0
  7. brainstate/environ.py +375 -0
  8. brainstate/functional/__init__.py +25 -0
  9. brainstate/functional/_activations.py +754 -0
  10. brainstate/functional/_normalization.py +69 -0
  11. brainstate/functional/_spikes.py +90 -0
  12. brainstate/init/__init__.py +26 -0
  13. brainstate/init/_base.py +36 -0
  14. brainstate/init/_generic.py +175 -0
  15. brainstate/init/_random_inits.py +489 -0
  16. brainstate/init/_regular_inits.py +109 -0
  17. brainstate/math/__init__.py +21 -0
  18. brainstate/math/_einops.py +787 -0
  19. brainstate/math/_einops_parsing.py +169 -0
  20. brainstate/math/_einops_parsing_test.py +126 -0
  21. brainstate/math/_einops_test.py +346 -0
  22. brainstate/math/_misc.py +298 -0
  23. brainstate/math/_misc_test.py +58 -0
  24. brainstate/mixin.py +373 -0
  25. brainstate/mixin_test.py +73 -0
  26. brainstate/nn/__init__.py +68 -0
  27. brainstate/nn/_base.py +248 -0
  28. brainstate/nn/_connections.py +686 -0
  29. brainstate/nn/_dynamics.py +406 -0
  30. brainstate/nn/_elementwise.py +1437 -0
  31. brainstate/nn/_misc.py +132 -0
  32. brainstate/nn/_normalizations.py +389 -0
  33. brainstate/nn/_others.py +100 -0
  34. brainstate/nn/_poolings.py +1228 -0
  35. brainstate/nn/_poolings_test.py +231 -0
  36. brainstate/nn/_projection/__init__.py +32 -0
  37. brainstate/nn/_projection/_align_post.py +528 -0
  38. brainstate/nn/_projection/_align_pre.py +599 -0
  39. brainstate/nn/_projection/_delta.py +241 -0
  40. brainstate/nn/_projection/_utils.py +17 -0
  41. brainstate/nn/_projection/_vanilla.py +101 -0
  42. brainstate/nn/_rate_rnns.py +393 -0
  43. brainstate/nn/_readout.py +130 -0
  44. brainstate/nn/_synouts.py +166 -0
  45. brainstate/nn/functional/__init__.py +25 -0
  46. brainstate/nn/functional/_activations.py +754 -0
  47. brainstate/nn/functional/_normalization.py +69 -0
  48. brainstate/nn/functional/_spikes.py +90 -0
  49. brainstate/nn/init/__init__.py +26 -0
  50. brainstate/nn/init/_base.py +36 -0
  51. brainstate/nn/init/_generic.py +175 -0
  52. brainstate/nn/init/_random_inits.py +489 -0
  53. brainstate/nn/init/_regular_inits.py +109 -0
  54. brainstate/nn/surrogate.py +1740 -0
  55. brainstate/optim/__init__.py +23 -0
  56. brainstate/optim/_lr_scheduler.py +486 -0
  57. brainstate/optim/_lr_scheduler_test.py +36 -0
  58. brainstate/optim/_sgd_optimizer.py +1148 -0
  59. brainstate/random.py +5148 -0
  60. brainstate/random_test.py +576 -0
  61. brainstate/surrogate.py +1740 -0
  62. brainstate/transform/__init__.py +36 -0
  63. brainstate/transform/_autograd.py +585 -0
  64. brainstate/transform/_autograd_test.py +1183 -0
  65. brainstate/transform/_control.py +665 -0
  66. brainstate/transform/_controls_test.py +220 -0
  67. brainstate/transform/_jit.py +239 -0
  68. brainstate/transform/_jit_error.py +158 -0
  69. brainstate/transform/_jit_test.py +102 -0
  70. brainstate/transform/_make_jaxpr.py +573 -0
  71. brainstate/transform/_make_jaxpr_test.py +133 -0
  72. brainstate/transform/_progress_bar.py +113 -0
  73. brainstate/typing.py +69 -0
  74. brainstate/util.py +747 -0
  75. brainstate-0.0.1.dist-info/LICENSE +202 -0
  76. brainstate-0.0.1.dist-info/METADATA +101 -0
  77. brainstate-0.0.1.dist-info/RECORD +79 -0
  78. brainstate-0.0.1.dist-info/WHEEL +6 -0
  79. brainstate-0.0.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,528 @@
1
+ # Copyright 2024 BDP Ecosystem Limited. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # ==============================================================================
15
+
16
+ from typing import Optional, Union
17
+
18
+ from brainstate._module import (register_delay_of_target,
19
+ Projection,
20
+ Module,
21
+ Dynamics,
22
+ ReceiveInputProj,
23
+ ExtendedUpdateWithBA)
24
+ from brainstate._utils import set_module_as
25
+ from brainstate.mixin import (Mode, AllOfTypes, DelayedInitializer, BindCondData, AlignPost, UpdateReturn)
26
+ from ._utils import is_instance
27
+
28
+ __all__ = [
29
+ 'HalfProjAlignPostMg', 'FullProjAlignPostMg',
30
+ 'HalfProjAlignPost', 'FullProjAlignPost',
31
+ ]
32
+
33
+
34
+ def get_post_repr(out_label, syn, out):
35
+ return f'{out_label} // {syn.identifier} // {out.identifier}'
36
+
37
+
38
+ def align_post_add_bef_update(
39
+ out_label: str,
40
+ syn_desc,
41
+ out_desc,
42
+ post: AllOfTypes[ReceiveInputProj, ExtendedUpdateWithBA],
43
+ proj_name: str
44
+ ):
45
+ # synapse and output initialization
46
+ _post_repr = get_post_repr(out_label, syn_desc, out_desc)
47
+ if not post.has_before_update(_post_repr):
48
+ syn_cls = syn_desc()
49
+ out_cls = out_desc()
50
+
51
+ # synapse and output initialization
52
+ post.add_input_fun(proj_name, out_cls, label=out_label)
53
+ post.add_before_update(_post_repr, _AlignPost(syn_cls, out_cls))
54
+ syn = post.get_before_update(_post_repr).syn
55
+ out = post.get_before_update(_post_repr).out
56
+ return syn, out
57
+
58
+
59
+ class _AlignPost(Module):
60
+ def __init__(
61
+ self,
62
+ syn: Module,
63
+ out: AllOfTypes[Dynamics, BindCondData]
64
+ ):
65
+ super().__init__()
66
+ self.syn = syn
67
+ self.out = out
68
+
69
+ def update(self, *args, **kwargs):
70
+ self.out.bind_cond(self.syn(*args, **kwargs))
71
+
72
+
73
+ @set_module_as('brainstate.nn')
74
+ class HalfProjAlignPostMg(Projection):
75
+ r"""
76
+ Defining the half part of synaptic projection with the align-post reduction and the automatic synapse merging.
77
+
78
+ The ``half-part`` means that the model only needs to provide half information needed for a projection,
79
+ including ``comm`` -> ``syn`` -> ``out`` -> ``post``. Therefore, the model's ``update`` function needs
80
+ the manual providing of the spiking input.
81
+
82
+ The ``align-post`` means that the synaptic variables have the same dimension as the post-synaptic neuron group.
83
+
84
+ The ``merging`` means that the same delay model is shared by all synapses, and the synapse model with same
85
+ parameters (such like time constants) will also share the same synaptic variables.
86
+
87
+ All align-post projection models prefer to use the event-driven computation mode. This means that the
88
+ ``comm`` model should be the event-driven model.
89
+
90
+ **Code Examples**
91
+
92
+ To define an E/I balanced network model.
93
+
94
+ .. code-block:: python
95
+
96
+ import brainstate as bp
97
+ import brainstate.math as bm
98
+
99
+ class EINet(bp.DynSysGroup):
100
+ def __init__(self):
101
+ super().__init__()
102
+ self.N = bp.dyn.LifRef(4000, V_rest=-60., V_th=-50., V_reset=-60., tau=20., tau_ref=5.,
103
+ V_initializer=bp.init.Normal(-55., 2.))
104
+ self.delay = bp.VarDelay(self.N.spike, entries={'I': None})
105
+ self.E = bp.dyn.HalfProjAlignPostMg(comm=bp.dnn.EventJitFPHomoLinear(3200, 4000, prob=0.02, weight=0.6),
106
+ syn=bp.dyn.Expon.desc(size=4000, tau=5.),
107
+ out=bp.dyn.COBA.desc(E=0.),
108
+ post=self.N)
109
+ self.I = bp.dyn.HalfProjAlignPostMg(comm=bp.dnn.EventJitFPHomoLinear(800, 4000, prob=0.02, weight=6.7),
110
+ syn=bp.dyn.Expon.desc(size=4000, tau=10.),
111
+ out=bp.dyn.COBA.desc(E=-80.),
112
+ post=self.N)
113
+
114
+ def update(self, input):
115
+ spk = self.delay.at('I')
116
+ self.E(spk[:3200])
117
+ self.I(spk[3200:])
118
+ self.delay(self.N(input))
119
+ return self.N.spike.value
120
+
121
+ model = EINet()
122
+ indices = bm.arange(1000)
123
+ spks = bm.for_loop(lambda i: model.step_run(i, 20.), indices)
124
+ bp.visualize.raster_plot(indices, spks, show=True)
125
+
126
+ Args:
127
+ comm: The synaptic communication.
128
+ syn: The synaptic dynamics.
129
+ out: The synaptic output.
130
+ post: The post-synaptic neuron group.
131
+ out_label: str. The prefix of the output function.
132
+ name: str. The projection name.
133
+ mode: Mode. The computing mode.
134
+ """
135
+
136
+ _invisible_nodes = ['syn', 'out', 'post']
137
+
138
+ def __init__(
139
+ self,
140
+ comm: Module,
141
+ syn: DelayedInitializer[AlignPost],
142
+ out: DelayedInitializer[BindCondData],
143
+ post: AllOfTypes[ReceiveInputProj, ExtendedUpdateWithBA],
144
+ out_label: Optional[str] = None,
145
+ name: Optional[str] = None,
146
+ mode: Optional[Mode] = None,
147
+ ):
148
+ super().__init__(name=name, mode=mode)
149
+
150
+ # synaptic models
151
+ is_instance(syn, DelayedInitializer[AlignPost])
152
+ is_instance(out, DelayedInitializer[BindCondData])
153
+ is_instance(post, AllOfTypes[ReceiveInputProj, ExtendedUpdateWithBA])
154
+
155
+ # synapse and output initialization
156
+ syn, out = align_post_add_bef_update(out_label, syn_desc=syn, out_desc=out, post=post, proj_name=self.name)
157
+
158
+ # references
159
+ self.post = post
160
+ self.syn = syn
161
+ self.out = out
162
+ self.comm = comm
163
+
164
+ def update(self, x):
165
+ current = self.comm(x)
166
+ self.syn.align_post_input_add(current) # synapse post current
167
+ return current
168
+
169
+
170
+ @set_module_as('brainstate.nn')
171
+ class FullProjAlignPostMg(Projection):
172
+ """Full-chain synaptic projection with the align-post reduction and the automatic synapse merging.
173
+
174
+ The ``full-chain`` means that the model needs to provide all information needed for a projection,
175
+ including ``pre`` -> ``delay`` -> ``comm`` -> ``syn`` -> ``out`` -> ``post``.
176
+
177
+ The ``align-post`` means that the synaptic variables have the same dimension as the post-synaptic neuron group.
178
+
179
+ The ``merging`` means that the same delay model is shared by all synapses, and the synapse model with same
180
+ parameters (such like time constants) will also share the same synaptic variables.
181
+
182
+ All align-post projection models prefer to use the event-driven computation mode. This means that the
183
+ ``comm`` model should be the event-driven model.
184
+
185
+ Moreover, it's worth noting that ``FullProjAlignPostMg`` has a different updating order with all align-pre
186
+ projection models. The updating order of align-post projections is ``spikes`` -> ``comm`` -> ``syn`` -> ``out``.
187
+ While, the updating order of all align-pre projection models is usually ``spikes`` -> ``syn`` -> ``comm`` -> ``out``.
188
+
189
+ **Code Examples**
190
+
191
+ To define an E/I balanced network model.
192
+
193
+ .. code-block:: python
194
+
195
+ import brainstate as bp
196
+ import brainstate.math as bm
197
+
198
+ class EINet(bp.DynSysGroup):
199
+ def __init__(self):
200
+ super().__init__()
201
+ ne, ni = 3200, 800
202
+ self.E = bp.dyn.LifRef(ne, V_rest=-60., V_th=-50., V_reset=-60., tau=20., tau_ref=5.,
203
+ V_initializer=bp.init.Normal(-55., 2.))
204
+ self.I = bp.dyn.LifRef(ni, V_rest=-60., V_th=-50., V_reset=-60., tau=20., tau_ref=5.,
205
+ V_initializer=bp.init.Normal(-55., 2.))
206
+ self.E2E = bp.dyn.FullProjAlignPostMg(pre=self.E,
207
+ delay=0.1,
208
+ comm=bp.dnn.EventJitFPHomoLinear(ne, ne, prob=0.02, weight=0.6),
209
+ syn=bp.dyn.Expon.desc(size=ne, tau=5.),
210
+ out=bp.dyn.COBA.desc(E=0.),
211
+ post=self.E)
212
+ self.E2I = bp.dyn.FullProjAlignPostMg(pre=self.E,
213
+ delay=0.1,
214
+ comm=bp.dnn.EventJitFPHomoLinear(ne, ni, prob=0.02, weight=0.6),
215
+ syn=bp.dyn.Expon.desc(size=ni, tau=5.),
216
+ out=bp.dyn.COBA.desc(E=0.),
217
+ post=self.I)
218
+ self.I2E = bp.dyn.FullProjAlignPostMg(pre=self.I,
219
+ delay=0.1,
220
+ comm=bp.dnn.EventJitFPHomoLinear(ni, ne, prob=0.02, weight=6.7),
221
+ syn=bp.dyn.Expon.desc(size=ne, tau=10.),
222
+ out=bp.dyn.COBA.desc(E=-80.),
223
+ post=self.E)
224
+ self.I2I = bp.dyn.FullProjAlignPostMg(pre=self.I,
225
+ delay=0.1,
226
+ comm=bp.dnn.EventJitFPHomoLinear(ni, ni, prob=0.02, weight=6.7),
227
+ syn=bp.dyn.Expon.desc(size=ni, tau=10.),
228
+ out=bp.dyn.COBA.desc(E=-80.),
229
+ post=self.I)
230
+
231
+ def update(self, inp):
232
+ self.E2E()
233
+ self.E2I()
234
+ self.I2E()
235
+ self.I2I()
236
+ self.E(inp)
237
+ self.I(inp)
238
+ return self.E.spike
239
+
240
+ model = EINet()
241
+ indices = bm.arange(1000)
242
+ spks = bm.for_loop(lambda i: model.step_run(i, 20.), indices)
243
+ bp.visualize.raster_plot(indices, spks, show=True)
244
+
245
+ Args:
246
+ pre: The pre-synaptic neuron group.
247
+ delay: The synaptic delay.
248
+ comm: The synaptic communication.
249
+ syn: The synaptic dynamics.
250
+ out: The synaptic output.
251
+ post: The post-synaptic neuron group.
252
+ name: str. The projection name.
253
+ mode: Mode. The computing mode.
254
+ """
255
+
256
+ _invisible_nodes = ['syn', 'out', 'post', 'pre', 'delay']
257
+
258
+ def __init__(
259
+ self,
260
+ pre: AllOfTypes[ExtendedUpdateWithBA, UpdateReturn],
261
+ delay: Union[None, int, float],
262
+ comm: Module,
263
+ syn: DelayedInitializer[AlignPost],
264
+ out: DelayedInitializer[BindCondData],
265
+ post: AllOfTypes[ReceiveInputProj, ExtendedUpdateWithBA],
266
+ out_label: Optional[str] = None,
267
+ name: Optional[str] = None,
268
+ mode: Optional[Mode] = None,
269
+ ):
270
+ super().__init__(name=name, mode=mode)
271
+
272
+ # synaptic models
273
+ is_instance(pre, AllOfTypes[ExtendedUpdateWithBA, UpdateReturn])
274
+ is_instance(comm, Module)
275
+ is_instance(syn, DelayedInitializer[AlignPost])
276
+ is_instance(out, DelayedInitializer[BindCondData])
277
+ is_instance(post, AllOfTypes[ReceiveInputProj, ExtendedUpdateWithBA])
278
+ self.comm = comm
279
+
280
+ # delay initialization
281
+ if delay is not None and delay > 0.:
282
+ delay_cls = register_delay_of_target(pre)
283
+ delay_cls.register_entry(self.name, delay)
284
+ self.delay = delay_cls
285
+ self.has_delay = True
286
+ else:
287
+ self.delay = None
288
+ self.has_delay = False
289
+
290
+ # synapse and output initialization
291
+ syn, out = align_post_add_bef_update(out_label, syn_desc=syn, out_desc=out, post=post, proj_name=self.name)
292
+
293
+ # references
294
+ self.pre = pre
295
+ self.post = post
296
+ self.syn = syn
297
+ self.out = out
298
+
299
+ def update(self):
300
+ if self.has_delay:
301
+ x = self.delay.at(self.name)
302
+ else:
303
+ x = self.pre.update_return()
304
+ current = self.comm(x)
305
+ self.syn.align_post_input_add(current) # synapse post current
306
+ return current
307
+
308
+
309
+ @set_module_as('brainstate.nn')
310
+ class HalfProjAlignPost(Projection):
311
+ """Defining the half-part of synaptic projection with the align-post reduction.
312
+
313
+ The ``half-part`` means that the model only needs to provide half information needed for a projection,
314
+ including ``comm`` -> ``syn`` -> ``out`` -> ``post``. Therefore, the model's ``update`` function needs
315
+ the manual providing of the spiking input.
316
+
317
+ The ``align-post`` means that the synaptic variables have the same dimension as the post-synaptic neuron group.
318
+
319
+ All align-post projection models prefer to use the event-driven computation mode. This means that the
320
+ ``comm`` model should be the event-driven model.
321
+
322
+ To simulate an E/I balanced network:
323
+
324
+ .. code-block::
325
+
326
+ class EINet(bp.DynSysGroup):
327
+ def __init__(self):
328
+ super().__init__()
329
+ self.N = bp.dyn.LifRef(4000, V_rest=-60., V_th=-50., V_reset=-60., tau=20., tau_ref=5.,
330
+ V_initializer=bp.init.Normal(-55., 2.))
331
+ self.delay = bp.VarDelay(self.N.spike, entries={'I': None})
332
+ self.E = bp.dyn.HalfProjAlignPost(comm=bp.dnn.EventJitFPHomoLinear(3200, 4000, prob=0.02, weight=0.6),
333
+ syn=bp.dyn.Expon(size=4000, tau=5.),
334
+ out=bp.dyn.COBA(E=0.),
335
+ post=self.N)
336
+ self.I = bp.dyn.HalfProjAlignPost(comm=bp.dnn.EventJitFPHomoLinear(800, 4000, prob=0.02, weight=6.7),
337
+ syn=bp.dyn.Expon(size=4000, tau=10.),
338
+ out=bp.dyn.COBA(E=-80.),
339
+ post=self.N)
340
+
341
+ def update(self, input):
342
+ spk = self.delay.at('I')
343
+ self.E(spk[:3200])
344
+ self.I(spk[3200:])
345
+ self.delay(self.N(input))
346
+ return self.N.spike.value
347
+
348
+ model = EINet()
349
+ indices = bm.arange(1000)
350
+ spks = bm.for_loop(lambda i: model.step_run(i, 20.), indices)
351
+ bp.visualize.raster_plot(indices, spks, show=True)
352
+
353
+
354
+ Args:
355
+ comm: The synaptic communication.
356
+ syn: The synaptic dynamics.
357
+ out: The synaptic output.
358
+ post: The post-synaptic neuron group.
359
+ name: str. The projection name.
360
+ mode: Mode. The computing mode.
361
+ """
362
+
363
+ _invisible_nodes = ['out', 'post']
364
+
365
+ def __init__(
366
+ self,
367
+ comm: Module,
368
+ syn: AlignPost,
369
+ out: BindCondData,
370
+ post: ReceiveInputProj,
371
+ out_label: Optional[str] = None,
372
+ name: Optional[str] = None,
373
+ mode: Optional[Mode] = None,
374
+ ):
375
+ super().__init__(name=name, mode=mode)
376
+
377
+ # synaptic models
378
+ is_instance(comm, Module)
379
+ is_instance(syn, AlignPost)
380
+ is_instance(out, BindCondData)
381
+ is_instance(post, Module)
382
+ self.comm = comm
383
+ self.syn = syn
384
+
385
+ # synapse and output initialization
386
+ post.add_input_fun(self.name, out, label=out_label)
387
+
388
+ # reference
389
+ self.post = post
390
+ self.out = out
391
+
392
+ def update(self, x):
393
+ current = self.comm(x)
394
+ g = self.syn(current)
395
+ self.out.bind_cond(g) # synapse post current
396
+ return current
397
+
398
+
399
+ @set_module_as('brainstate.nn')
400
+ class FullProjAlignPost(Projection):
401
+ """Full-chain synaptic projection with the align-post reduction.
402
+
403
+ The ``full-chain`` means that the model needs to provide all information needed for a projection,
404
+ including ``pre`` -> ``delay`` -> ``comm`` -> ``syn`` -> ``out`` -> ``post``.
405
+
406
+ The ``align-post`` means that the synaptic variables have the same dimension as the post-synaptic neuron group.
407
+
408
+ All align-post projection models prefer to use the event-driven computation mode. This means that the
409
+ ``comm`` model should be the event-driven model.
410
+
411
+ Moreover, it's worth noting that ``FullProjAlignPost`` has a different updating order with all align-pre
412
+ projection models. The updating order of align-post projections is ``spikes`` -> ``comm`` -> ``syn`` -> ``out``.
413
+ While, the updating order of all align-pre projection models is usually ``spikes`` -> ``syn`` -> ``comm`` -> ``out``.
414
+
415
+ To simulate and define an E/I balanced network model:
416
+
417
+ .. code-block:: python
418
+
419
+ class EINet(bp.DynSysGroup):
420
+ def __init__(self):
421
+ super().__init__()
422
+ ne, ni = 3200, 800
423
+ self.E = bp.dyn.LifRef(ne, V_rest=-60., V_th=-50., V_reset=-60., tau=20., tau_ref=5.,
424
+ V_initializer=bp.init.Normal(-55., 2.))
425
+ self.I = bp.dyn.LifRef(ni, V_rest=-60., V_th=-50., V_reset=-60., tau=20., tau_ref=5.,
426
+ V_initializer=bp.init.Normal(-55., 2.))
427
+ self.E2E = bp.dyn.FullProjAlignPost(pre=self.E,
428
+ delay=0.1,
429
+ comm=bp.dnn.EventJitFPHomoLinear(ne, ne, prob=0.02, weight=0.6),
430
+ syn=bp.dyn.Expon(size=ne, tau=5.),
431
+ out=bp.dyn.COBA(E=0.),
432
+ post=self.E)
433
+ self.E2I = bp.dyn.FullProjAlignPost(pre=self.E,
434
+ delay=0.1,
435
+ comm=bp.dnn.EventJitFPHomoLinear(ne, ni, prob=0.02, weight=0.6),
436
+ syn=bp.dyn.Expon(size=ni, tau=5.),
437
+ out=bp.dyn.COBA(E=0.),
438
+ post=self.I)
439
+ self.I2E = bp.dyn.FullProjAlignPost(pre=self.I,
440
+ delay=0.1,
441
+ comm=bp.dnn.EventJitFPHomoLinear(ni, ne, prob=0.02, weight=6.7),
442
+ syn=bp.dyn.Expon(size=ne, tau=10.),
443
+ out=bp.dyn.COBA(E=-80.),
444
+ post=self.E)
445
+ self.I2I = bp.dyn.FullProjAlignPost(pre=self.I,
446
+ delay=0.1,
447
+ comm=bp.dnn.EventJitFPHomoLinear(ni, ni, prob=0.02, weight=6.7),
448
+ syn=bp.dyn.Expon(size=ni, tau=10.),
449
+ out=bp.dyn.COBA(E=-80.),
450
+ post=self.I)
451
+
452
+ def update(self, inp):
453
+ self.E2E()
454
+ self.E2I()
455
+ self.I2E()
456
+ self.I2I()
457
+ self.E(inp)
458
+ self.I(inp)
459
+ return self.E.spike
460
+
461
+ model = EINet()
462
+ indices = bm.arange(1000)
463
+ spks = bm.for_loop(lambda i: model.step_run(i, 20.), indices)
464
+ bp.visualize.raster_plot(indices, spks, show=True)
465
+
466
+
467
+ Args:
468
+ pre: The pre-synaptic neuron group.
469
+ delay: The synaptic delay.
470
+ comm: The synaptic communication.
471
+ syn: The synaptic dynamics.
472
+ out: The synaptic output.
473
+ post: The post-synaptic neuron group.
474
+ name: str. The projection name.
475
+ mode: Mode. The computing mode.
476
+ """
477
+
478
+ _invisible_nodes = ['post', 'pre', 'delay']
479
+
480
+ def __init__(
481
+ self,
482
+ pre: UpdateReturn,
483
+ delay: Union[None, int, float],
484
+ comm: Module,
485
+ syn: AlignPost,
486
+ out: BindCondData,
487
+ post: ReceiveInputProj,
488
+ out_label: Optional[str] = None,
489
+ name: Optional[str] = None,
490
+ mode: Optional[Mode] = None,
491
+ ):
492
+ super().__init__(name=name, mode=mode)
493
+
494
+ # synaptic models
495
+ is_instance(pre, UpdateReturn)
496
+ is_instance(comm, Module)
497
+ is_instance(syn, AllOfTypes[Dynamics, AlignPost])
498
+ is_instance(out, AllOfTypes[Dynamics, BindCondData])
499
+ is_instance(post, ReceiveInputProj)
500
+ self.comm = comm
501
+ self.syn = syn
502
+ self.out = out
503
+
504
+ # delay initialization
505
+ if delay is not None and delay > 0.:
506
+ delay_cls = register_delay_of_target(pre)
507
+ delay_cls.register_entry(self.name, delay)
508
+ self.delay = delay_cls
509
+ self.has_delay = True
510
+ else:
511
+ self.delay = None
512
+ self.has_delay = False
513
+
514
+ # synapse and output initialization
515
+ post.add_input_fun(self.name, out, label=out_label)
516
+
517
+ # references
518
+ self.post = post
519
+ self.pre = pre
520
+
521
+ def update(self):
522
+ if self.has_delay:
523
+ x = self.delay.at(self.name)
524
+ else:
525
+ x = self.pre.update_return()
526
+ g = self.syn(self.comm(x))
527
+ self.out.bind_cond(g) # synapse post current
528
+ return g