brainx-skill 1.0.9 → 1.0.11
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.
- package/README.md +6 -0
- package/adapters/antigravity.js +8 -0
- package/installation/lib/installer.js +60 -20
- package/installation/lib/paths.js +41 -3
- package/installation/lib/prompts.js +13 -7
- package/manifest.json +2 -1
- package/package.json +3 -2
- package/skills/braincell/SKILL.md +14 -0
- package/skills/brainevent/SKILL.md +1 -0
- package/skills/brainmass/SKILL.md +1 -1
- package/skills/brainpy-state/SKILL.md +7 -1
- package/skills/brainpy-state/references/array-creation.md +2 -2
- package/skills/brainpy-state/references/brainPy(legacy)/analysis.md +150 -0
- package/skills/brainpy-state/references/brainPy(legacy)/brainpy legacy workflow.md +373 -0
- package/skills/brainpy-state/references/brainPy(legacy)/built-in dynamic neuron model.md +150 -0
- package/skills/brainpy-state/references/brainPy(legacy)/connecting neurons.md +175 -0
- package/skills/brainpy-state/references/brainPy(legacy)/customize neuron and synpase.md +286 -0
- package/skills/brainpy-state/references/brainPy(legacy)/infrastructure/Input generation.md +111 -0
- package/skills/brainpy-state/references/brainPy(legacy)/infrastructure/More about simulation.md +163 -0
- package/skills/brainpy-state/references/brainPy(legacy)/infrastructure/Multi-device array sharding.md +113 -0
- package/skills/brainpy-state/references/brainPy(legacy)/infrastructure/Parallel experiment execution.md +136 -0
- package/skills/brainpy-state/references/brainPy(legacy)/infrastructure/array creation and mechanics.md +222 -0
- package/skills/brainpy-state/references/brainPy(legacy)/infrastructure/brainpy math environment setting.md +123 -0
- package/skills/brainpy-state/references/brainPy(legacy)/infrastructure/delays.md +107 -0
- package/skills/brainpy-state/references/brainPy(legacy)/infrastructure/object oriented transformations and control flows.md +185 -0
- package/skills/brainpy-state/references/brainPy(legacy)/integrators.md +181 -0
- package/skills/brainpy-state/references/brainPy(legacy)/route activity through connectivity.md +102 -0
- package/skills/brainpy-state/references/brainPy(legacy)/synaptic projections.md +147 -0
- package/skills/brainpy-state/references/brainPy(legacy)/synpase properties.md +159 -0
- package/skills/brainpy-state/references/brainPy(legacy)/training/loss library.md +127 -0
- package/skills/brainpy-state/references/brainPy(legacy)/training/optimizers.md +167 -0
- package/skills/brainpy-state/references/brainPy(legacy)/training/parameter initializers.md +107 -0
- package/skills/brainpy-state/references/brainPy(legacy)/training/prebuilt neural network layers.md +186 -0
- package/skills/brainpy-state/references/brainPy(legacy)/training/surrogate gradients.md +104 -0
- package/skills/brainpy-state/references/brainPy(legacy)/training/trainer library.md +89 -0
- package/skills/brainpy-state/references/brainPy(legacy)/training/trainingworkflows.md +253 -0
- package/skills/brainstate/SKILL.md +14 -10
- package/skills/brainstate/references/brainstate/transformation-grad-expansion.md +1 -1
- package/skills/brainstate/references/braintools/optimizer.md +179 -0
- package/skills/braintrace/SKILL.md +161 -0
- package/skills/braintrace/references/Drtrl.md +179 -0
- package/skills/braintrace/references/ETP operators.md +161 -0
- package/skills/braintrace/references/algorithm selection.md +120 -0
- package/skills/braintrace/references/batching.md +150 -0
- package/skills/braintrace/references/compiler_internal.md +162 -0
- package/skills/braintrace/references/custom ETP primitives.md +200 -0
- package/skills/braintrace/references/custom algorithms.md +206 -0
- package/skills/braintrace/references/customizing_primitive_transforms.md +191 -0
- package/skills/braintrace/references/pp_pprop workflow.md +196 -0
- package/skills/braintrace/references/pre-built-braintrace-layer.md +149 -0
- package/skills/brainx-general-guard/SKILL.md +45 -8
- package/skills/brainx-install/SKILL.md +29 -23
- package/skills/brainstate/agents/openai.yaml +0 -4
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# Legacy BrainPy connectivity
|
|
2
|
+
|
|
3
|
+
Use this reference to choose and materialize explicit legacy `brainpy.connect` connectivity, convert among dense/COO/CSR forms, or perform transmission with an implicit random matrix through `brainpy.math.jitconn`. Keep synapse dynamics and projection assembly in `synaptic projections.md` or `customize neuron and synpase.md`.
|
|
4
|
+
|
|
5
|
+
## Choose explicit or implicit connectivity
|
|
6
|
+
|
|
7
|
+
Explicit connectors define a reusable wiring object whose representations can be inspected; JIT connectivity generates a random matrix during each matrix-vector operation and does not store the connection graph.
|
|
8
|
+
|
|
9
|
+
| Need | Use | Constraint |
|
|
10
|
+
|---|---|---|
|
|
11
|
+
| Inspect edges, degrees, or a connection matrix | `brainpy.connect` | Materialize the required representation with `.require(...)`. |
|
|
12
|
+
| Reuse the exact graph across different operations | `brainpy.connect` | Keep the connector or extracted indices. |
|
|
13
|
+
| Apply sparse or event-driven transmission over stored CSR edges | `bm.sparse.csrmv` or `bm.event.csrmv` | Supply `indices`, `indptr`, and values that share one orientation. |
|
|
14
|
+
| Avoid storing a large random matrix | `bm.jitconn.*` | Supply shape, distribution parameters, probability, and an explicit integer seed. |
|
|
15
|
+
|
|
16
|
+
Do not use `jitconn` when downstream analysis must recover individual edges or modify a specific synapse.
|
|
17
|
+
|
|
18
|
+
## Build explicit connectivity
|
|
19
|
+
|
|
20
|
+
Call a connector with `(pre_size, post_size)` unless those sizes were supplied as `pre=` and `post=` constructor arguments.
|
|
21
|
+
|
|
22
|
+
### Construct from known edges
|
|
23
|
+
|
|
24
|
+
| API | Description |
|
|
25
|
+
|---|---|
|
|
26
|
+
| `bp.connect.MatConn(conn_mat)` | Use an existing dense connection matrix. |
|
|
27
|
+
| `bp.connect.IJConn(i, j)` | Use paired presynaptic and postsynaptic edge indices. |
|
|
28
|
+
| `bp.connect.CSRConn(indices, indptr)` | Use an existing CSR representation. |
|
|
29
|
+
| `bp.connect.SparseMatConn(sparse_mat)` | Use an existing supported sparse matrix. |
|
|
30
|
+
|
|
31
|
+
### Generate random graphs
|
|
32
|
+
|
|
33
|
+
| API | Description |
|
|
34
|
+
|---|---|
|
|
35
|
+
| `bp.connect.FixedProb(prob, pre_ratio=1.0, include_self=True, allow_multi_conn=False, seed=None)` | Connect possible pairs independently with fixed probability. |
|
|
36
|
+
| `bp.connect.FixedPreNum(num, ...)` | Give each postsynaptic neuron a fixed number of presynaptic inputs. |
|
|
37
|
+
| `bp.connect.FixedPostNum(num, ...)` | Give each presynaptic neuron a fixed number of postsynaptic targets. |
|
|
38
|
+
| `bp.connect.FixedTotalNum(num, ...)` | Generate a fixed total number of connections. |
|
|
39
|
+
| `bp.connect.GaussianProb(sigma, ...)` | Make connection probability decay with Gaussian distance inside one population. |
|
|
40
|
+
| `bp.connect.ProbDist(dist, prob, ...)` | Connect pairs within a maximum distance at a specified probability. |
|
|
41
|
+
| `bp.connect.SmallWorld(...)` | Build a Watts-Strogatz small-world graph. |
|
|
42
|
+
| `bp.connect.ScaleFreeBA(...)` | Build a Barabasi-Albert preferential-attachment graph. |
|
|
43
|
+
| `bp.connect.ScaleFreeBADual(...)` | Build the dual Barabasi-Albert variant. |
|
|
44
|
+
| `bp.connect.PowerLaw(...)` | Build the Holme-Kim power-law graph with clustering. |
|
|
45
|
+
|
|
46
|
+
### Generate regular graphs
|
|
47
|
+
|
|
48
|
+
| API | Description |
|
|
49
|
+
|---|---|
|
|
50
|
+
| `bp.connect.One2One()` | Connect equal-size groups by matching indices. |
|
|
51
|
+
| `bp.connect.All2All(include_self=True)` | Connect every presynaptic neuron to every postsynaptic neuron. |
|
|
52
|
+
| `bp.connect.GridFour(...)` | Connect four nearest grid neighbors. |
|
|
53
|
+
| `bp.connect.GridEight(...)` | Connect eight nearest grid neighbors. |
|
|
54
|
+
| `bp.connect.GridN(N, ...)` | Connect the neighborhood defined by radius `N`. |
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
import brainpy as bp
|
|
58
|
+
import brainpy.math as bm
|
|
59
|
+
|
|
60
|
+
pre_num, post_num = 4, 3
|
|
61
|
+
connector = bp.connect.FixedProb(prob=0.5, seed=123)(pre_num, post_num)
|
|
62
|
+
|
|
63
|
+
conn_mat = connector.require('conn_mat')
|
|
64
|
+
indices, indptr = connector.require('pre2post')
|
|
65
|
+
|
|
66
|
+
assert conn_mat.shape == (pre_num, post_num)
|
|
67
|
+
assert indptr.shape == (pre_num + 1,)
|
|
68
|
+
assert indices.ndim == 1
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
**Invariant:** Preserve representation orientation. In `pre2post`, `indptr` partitions edges by presynaptic neuron and `indices` stores postsynaptic indices.
|
|
72
|
+
|
|
73
|
+
## Convert connection representations
|
|
74
|
+
|
|
75
|
+
Use conversion helpers when source data and the selected communication operator require different formats.
|
|
76
|
+
|
|
77
|
+
| API | Description |
|
|
78
|
+
|---|---|
|
|
79
|
+
| `bp.connect.mat2coo(mat)` | Convert a dense matrix to paired COO indices. |
|
|
80
|
+
| `bp.connect.mat2csr(mat)` | Convert a dense matrix to CSR `indices` and `indptr`. |
|
|
81
|
+
| `bp.connect.mat2csc(mat)` | Convert a dense matrix to CSC. |
|
|
82
|
+
| `bp.connect.coo2csr(pre_ids, post_ids, ...)` | Group COO edges into presynaptic-oriented CSR. |
|
|
83
|
+
| `bp.connect.coo2csc(pre_ids, post_ids, ...)` | Group COO edges into postsynaptic-oriented CSC. |
|
|
84
|
+
| `bp.connect.coo2mat(pre_ids, post_ids, ...)` | Materialize a dense matrix from COO edges. |
|
|
85
|
+
| `bp.connect.csr2coo(indices, indptr)` | Expand CSR into paired edge indices. |
|
|
86
|
+
| `bp.connect.csr2csc(indices, indptr, ...)` | Change sparse orientation. |
|
|
87
|
+
| `bp.connect.csr2mat(indices, indptr, ...)` | Materialize a dense matrix from CSR. |
|
|
88
|
+
|
|
89
|
+
Keep edge-aligned weights in the same order as their sparse indices. If a conversion returns a permutation, apply it to the weight vector before transmission.
|
|
90
|
+
|
|
91
|
+
## Transmit through stored sparse connectivity
|
|
92
|
+
|
|
93
|
+
Use sparse matrix-vector multiplication for graded presynaptic activity and event-driven multiplication when the presynaptic vector is boolean or spike-like.
|
|
94
|
+
|
|
95
|
+
| API | Description |
|
|
96
|
+
|---|---|
|
|
97
|
+
| `bm.sparse.csrmv(data, indices, indptr, vector, shape, transpose=...)` | Multiply a CSR matrix by a dense graded vector. |
|
|
98
|
+
| `bm.event.csrmv(data, indices, indptr, events, shape, transpose=...)` | Skip inactive presynaptic events during CSR transmission. |
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
weights = bm.ones(indices.shape)
|
|
102
|
+
pre_activity = bm.asarray([1.0, 0.0, 2.0, 0.0])
|
|
103
|
+
pre_spikes = bm.asarray([True, False, True, False])
|
|
104
|
+
|
|
105
|
+
graded_post = bm.sparse.csrmv(
|
|
106
|
+
weights,
|
|
107
|
+
indices=indices,
|
|
108
|
+
indptr=indptr,
|
|
109
|
+
vector=pre_activity,
|
|
110
|
+
shape=(pre_num, post_num),
|
|
111
|
+
transpose=True,
|
|
112
|
+
)
|
|
113
|
+
event_post = bm.event.csrmv(
|
|
114
|
+
weights,
|
|
115
|
+
indices=indices,
|
|
116
|
+
indptr=indptr,
|
|
117
|
+
events=pre_spikes,
|
|
118
|
+
shape=(pre_num, post_num),
|
|
119
|
+
transpose=True,
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
assert graded_post.shape == event_post.shape == (post_num,)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Use the event operator only when zero/nonzero or boolean event semantics match the presynaptic signal. Do not convert continuous rates to booleans merely to select the event kernel.
|
|
126
|
+
|
|
127
|
+
## Generate connectivity during transmission
|
|
128
|
+
|
|
129
|
+
The six `jitconn` operators combine random connection generation with matrix-vector multiplication.
|
|
130
|
+
|
|
131
|
+
| API | Weight model and input |
|
|
132
|
+
|---|---|
|
|
133
|
+
| `bm.jitconn.mv_prob_homo(vector, weight, conn_prob, seed, *, shape, ...)` | Graded vector; one scalar weight on every generated edge. |
|
|
134
|
+
| `bm.jitconn.mv_prob_uniform(vector, w_low, w_high, conn_prob, seed, *, shape, ...)` | Graded vector; edge weights sampled uniformly. |
|
|
135
|
+
| `bm.jitconn.mv_prob_normal(vector, w_mu, w_sigma, conn_prob, seed, *, shape, ...)` | Graded vector; edge weights sampled normally. |
|
|
136
|
+
| `bm.jitconn.event_mv_prob_homo(events, weight, conn_prob, seed, *, shape, ...)` | Event vector; one scalar weight on every generated edge. |
|
|
137
|
+
| `bm.jitconn.event_mv_prob_uniform(events, w_low, w_high, conn_prob, seed, *, shape, ...)` | Event vector; edge weights sampled uniformly. |
|
|
138
|
+
| `bm.jitconn.event_mv_prob_normal(events, w_mu, w_sigma, conn_prob, seed, *, shape, ...)` | Event vector; edge weights sampled normally. |
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
post = bm.jitconn.event_mv_prob_normal(
|
|
142
|
+
pre_spikes,
|
|
143
|
+
w_mu=0.5,
|
|
144
|
+
w_sigma=0.1,
|
|
145
|
+
conn_prob=0.2,
|
|
146
|
+
seed=123,
|
|
147
|
+
shape=(pre_num, post_num),
|
|
148
|
+
transpose=True,
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
assert post.shape == (post_num,)
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Always pass an explicit integer `seed`. With `seed=None`, the implementation draws a host NumPy seed on each eager call, but a JAX trace captures that seed as a Python constant and reuses it on subsequent jitted calls.
|
|
155
|
+
|
|
156
|
+
Do not assume separate calls with `transpose=False` and `transpose=True` sample an identical matrix/transposed-matrix pair. The API documents `outdim_parallel=True` as the setting for that equivalence, with a possible speed cost; validate equivalence for the exact operator before depending on it.
|
|
157
|
+
|
|
158
|
+
## Common failures
|
|
159
|
+
|
|
160
|
+
- Do not materialize `conn_mat` for a large sparse graph unless an operation truly needs the dense matrix.
|
|
161
|
+
- Do not interpret `pre2post` indices as postsynaptic-oriented CSC data.
|
|
162
|
+
- Do not separate edge weights from the index order that defines them.
|
|
163
|
+
- Do not use an event kernel for graded activity.
|
|
164
|
+
- Do not omit the `jitconn` seed inside `jit`, `vmap`, or repeated experiment code.
|
|
165
|
+
- Do not use implicit JIT connectivity when exact edge inspection, plasticity, or edge-specific modification is required.
|
|
166
|
+
|
|
167
|
+
## Routing
|
|
168
|
+
|
|
169
|
+
Open `route activity through connectivity.md` for communication-layer selection and weighted transmission. Open `synaptic projections.md` for projection anatomy and update order. Open `synpase properties.md` for synaptic state and output semantics.
|
|
170
|
+
|
|
171
|
+
## Sources mirrored
|
|
172
|
+
|
|
173
|
+
- https://brainpy.readthedocs.io/apis/connect.html
|
|
174
|
+
- https://brainpy.readthedocs.io/tutorial_math/Dedicated_Operators.html
|
|
175
|
+
- https://brainpy.readthedocs.io/apis/brainpy.math.jitconn.html
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
# Customize legacy BrainPy neurons and synapses
|
|
2
|
+
|
|
3
|
+
Use this reference when a legacy BrainPy 2.x model needs custom neuron equations, spike/reset logic, synapse dynamics, synaptic output, communication, or projection composition. Use `built-in dynamic neuron model.md` first when a supplied `brainpy.dyn` model already matches the required mechanism.
|
|
4
|
+
|
|
5
|
+
## Choose the extension point
|
|
6
|
+
|
|
7
|
+
A custom neuron owns state evolution and event transitions; a custom projection composes presynaptic events, optional delay, communication, synapse dynamics, output current, and postsynaptic input binding.
|
|
8
|
+
|
|
9
|
+
| Need | Extend or compose |
|
|
10
|
+
|---|---|
|
|
11
|
+
| New neuron equations or spike/reset rule | `bp.dyn.NeuDyn` |
|
|
12
|
+
| New synaptic state equations | `bp.dyn.SynDyn` |
|
|
13
|
+
| New conductance-to-current rule | `bp.dyn.SynOut` |
|
|
14
|
+
| New weighted connectivity operation | `bp.dnn.Layer` or another `DynamicalSystem` used as `comm` |
|
|
15
|
+
| State aligned to postsynaptic neurons | `bp.dyn.ProjAlignPostMg2` |
|
|
16
|
+
| State aligned to presynaptic neurons | `bp.dyn.ProjAlignPreMg2` |
|
|
17
|
+
| Reusable projection wrapper | `bp.Projection` |
|
|
18
|
+
|
|
19
|
+
Keep these mechanisms separate. A projection chooses their order and ownership; it should not duplicate neuron or synapse differential equations.
|
|
20
|
+
|
|
21
|
+
## Implement a custom neuron
|
|
22
|
+
|
|
23
|
+
`NeuDyn` supplies population shape and model lifecycle, while the subclass declares every evolving value as `bm.Variable` and implements one time step in `update()`.
|
|
24
|
+
|
|
25
|
+
| API | Description |
|
|
26
|
+
|---|---|
|
|
27
|
+
| `bp.dyn.NeuDyn(size, keep_size=False, method='exp_auto', ...)` | Initialize population metadata and common neuronal-dynamics behavior. |
|
|
28
|
+
| `bm.Variable(initial_value)` | Register voltage, gating, spike, refractory, and last-event state for transformed mutation. |
|
|
29
|
+
| `bp.odeint(f, method=...)` | Build an integrator for continuous state equations. |
|
|
30
|
+
| `bp.share['t']` | Read the current simulation time supplied by the runner or `step_run`. |
|
|
31
|
+
| `bp.share['dt']` | Read the active integration step. |
|
|
32
|
+
| `bm.where(condition, x, y)` | Apply discontinuous reset or refractory transitions without Python branching on traced arrays. |
|
|
33
|
+
| `update(input=None)` | Advance exactly one step, update Variables, and return the step output if callers need it. |
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
import brainpy as bp
|
|
37
|
+
import brainpy.math as bm
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class LegacyLIF(bp.dyn.NeuDyn):
|
|
41
|
+
def __init__(
|
|
42
|
+
self,
|
|
43
|
+
size,
|
|
44
|
+
V_rest=0.0,
|
|
45
|
+
V_reset=-5.0,
|
|
46
|
+
V_th=20.0,
|
|
47
|
+
R=1.0,
|
|
48
|
+
tau=10.0,
|
|
49
|
+
tau_ref=5.0,
|
|
50
|
+
**kwargs,
|
|
51
|
+
):
|
|
52
|
+
super().__init__(size=size, **kwargs)
|
|
53
|
+
self.V_rest = V_rest
|
|
54
|
+
self.V_reset = V_reset
|
|
55
|
+
self.V_th = V_th
|
|
56
|
+
self.R = R
|
|
57
|
+
self.tau = tau
|
|
58
|
+
self.tau_ref = tau_ref
|
|
59
|
+
|
|
60
|
+
self.V = bm.Variable(bm.ones(self.num) * V_reset)
|
|
61
|
+
self.spike = bm.Variable(bm.zeros(self.num, dtype=bool))
|
|
62
|
+
self.refractory = bm.Variable(bm.zeros(self.num, dtype=bool))
|
|
63
|
+
self.t_last_spike = bm.Variable(bm.ones(self.num) * -1e7)
|
|
64
|
+
|
|
65
|
+
self.integral = bp.odeint(self.dV, method='exp_auto')
|
|
66
|
+
|
|
67
|
+
def dV(self, V, t, current):
|
|
68
|
+
return (-V + self.V_rest + self.R * current) / self.tau
|
|
69
|
+
|
|
70
|
+
def update(self, current=None):
|
|
71
|
+
t = bp.share['t']
|
|
72
|
+
dt = bp.share['dt']
|
|
73
|
+
current = 0.0 if current is None else current
|
|
74
|
+
|
|
75
|
+
refractory = (t - self.t_last_spike) <= self.tau_ref
|
|
76
|
+
next_V = self.integral(self.V, t, current, dt=dt)
|
|
77
|
+
next_V = bm.where(refractory, self.V, next_V)
|
|
78
|
+
spike = next_V >= self.V_th
|
|
79
|
+
|
|
80
|
+
self.spike.value = spike
|
|
81
|
+
self.t_last_spike.value = bm.where(spike, t, self.t_last_spike)
|
|
82
|
+
self.V.value = bm.where(spike, self.V_reset, next_V)
|
|
83
|
+
self.refractory.value = bm.logical_or(refractory, spike)
|
|
84
|
+
return self.spike.value
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
This example separates continuous integration from the discontinuous threshold/reset transition. For a continuous Hodgkin-Huxley-style model, create one integrator per equation or a joint integrator, update gating variables from the old state consistently, detect threshold crossings, then commit the new Variables.
|
|
88
|
+
|
|
89
|
+
**Invariant:** Compute the next state before assigning any dependent `Variable.value` unless the equations intentionally use a sequential update. Premature assignment silently changes which time level later equations read.
|
|
90
|
+
|
|
91
|
+
### Run the custom neuron
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
neurons = LegacyLIF(10)
|
|
95
|
+
runner = bp.DSRunner(neurons, monitors=['V', 'spike'])
|
|
96
|
+
runner.run(inputs=bm.ones(1000) * 22.0)
|
|
97
|
+
|
|
98
|
+
assert runner.mon['V'].shape == (1000, 10)
|
|
99
|
+
assert runner.mon['spike'].shape == (1000, 10)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Open `integrators.md` when choosing a solver, implementing coupled ODEs/SDEs, passing delays to integrators, or validating equation argument order.
|
|
103
|
+
|
|
104
|
+
## Decompose a custom chemical synapse
|
|
105
|
+
|
|
106
|
+
A legacy chemical projection is a chain: presynaptic event -> optional delay -> communication -> synapse state -> output current -> postsynaptic input.
|
|
107
|
+
|
|
108
|
+
| Component | Role | Typical base/API |
|
|
109
|
+
|---|---|---|
|
|
110
|
+
| `pre` | Produces spike or graded presynaptic activity. | `NeuDyn`, `SpikeTimeGroup`, or another `DynamicalSystem` |
|
|
111
|
+
| `delay` | Selects an earlier presynaptic event. | Projection `delay=` and pre delay support |
|
|
112
|
+
| `comm` | Maps pre-aligned activity to the post population with weights and connectivity. | `EventCSRLinear`, `CSRLinear`, `AllToAll`, `OneToOne`, or custom `bp.dnn.Layer` |
|
|
113
|
+
| `syn` | Evolves conductance, gating, or transmitter state. | `bp.dyn.SynDyn` descriptor |
|
|
114
|
+
| `out` | Converts synaptic state to current. | `bp.dyn.SynOut` descriptor or instance, according to the projection signature |
|
|
115
|
+
| `post` | Receives the bound current before its own update. | Postsynaptic neuron group |
|
|
116
|
+
|
|
117
|
+
Use `.desc(...)` for `syn` in both merging projections. `ProjAlignPostMg2` also takes an `out` descriptor; `ProjAlignPreMg2` takes a concrete `SynOut` instance because it binds condition data directly.
|
|
118
|
+
|
|
119
|
+
## Implement postsynaptic-aligned exponential conductance
|
|
120
|
+
|
|
121
|
+
The tutorial's postsynaptic-aligned path is for exponential synapse dynamics whose incoming events can be communicated first and accumulated into one conductance state per postsynaptic neuron.
|
|
122
|
+
|
|
123
|
+
| API | Description |
|
|
124
|
+
|---|---|
|
|
125
|
+
| `bp.dyn.SynDyn` | Base class for evolving synapse state. |
|
|
126
|
+
| `bp.mixin.AlignPost` | Mark dynamics that support postsynaptic alignment and accumulated current. |
|
|
127
|
+
| `add_current(x)` | Add communicated event increments to the postsynaptic-aligned state. |
|
|
128
|
+
| `return_info()` | Expose the state information required by projection merging. |
|
|
129
|
+
| `bp.dyn.ProjAlignPostMg2(...)` | Compose `delay -> comm -> postsynaptic synapse -> output -> post`. |
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
class ExponentialConductance(bp.dyn.SynDyn, bp.mixin.AlignPost):
|
|
133
|
+
def __init__(self, size, tau=5.0):
|
|
134
|
+
super().__init__(size=size)
|
|
135
|
+
self.tau = tau
|
|
136
|
+
self.g = bm.Variable(bm.zeros(self.num))
|
|
137
|
+
self.integral = bp.odeint(
|
|
138
|
+
lambda g, t: -g / self.tau,
|
|
139
|
+
method='exp_auto',
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
def update(self, increment=None):
|
|
143
|
+
self.g.value = self.integral(
|
|
144
|
+
self.g,
|
|
145
|
+
bp.share['t'],
|
|
146
|
+
dt=bp.share['dt'],
|
|
147
|
+
)
|
|
148
|
+
if increment is not None:
|
|
149
|
+
self.add_current(increment)
|
|
150
|
+
return self.g.value
|
|
151
|
+
|
|
152
|
+
def add_current(self, increment):
|
|
153
|
+
self.g += increment
|
|
154
|
+
|
|
155
|
+
def return_info(self):
|
|
156
|
+
return self.g
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
class COBAOutput(bp.dyn.SynOut):
|
|
160
|
+
def __init__(self, E=0.0):
|
|
161
|
+
super().__init__()
|
|
162
|
+
self.E = E
|
|
163
|
+
|
|
164
|
+
def update(self, conductance, potential):
|
|
165
|
+
return conductance * (self.E - potential)
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
class ExponentialCOBA(bp.Projection):
|
|
169
|
+
def __init__(
|
|
170
|
+
self,
|
|
171
|
+
pre,
|
|
172
|
+
post,
|
|
173
|
+
prob,
|
|
174
|
+
g_max,
|
|
175
|
+
tau=5.0,
|
|
176
|
+
E=0.0,
|
|
177
|
+
delay=None,
|
|
178
|
+
seed=None,
|
|
179
|
+
):
|
|
180
|
+
super().__init__()
|
|
181
|
+
connector = bp.conn.FixedProb(
|
|
182
|
+
prob,
|
|
183
|
+
pre=pre.num,
|
|
184
|
+
post=post.num,
|
|
185
|
+
seed=seed,
|
|
186
|
+
)
|
|
187
|
+
self.proj = bp.dyn.ProjAlignPostMg2(
|
|
188
|
+
pre=pre,
|
|
189
|
+
delay=delay,
|
|
190
|
+
comm=bp.dnn.EventCSRLinear(connector, weight=g_max),
|
|
191
|
+
syn=ExponentialConductance.desc(post.num, tau=tau),
|
|
192
|
+
out=COBAOutput.desc(E=E),
|
|
193
|
+
post=post,
|
|
194
|
+
)
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
The communication result is postsynaptic-sized, so `ExponentialConductance.desc(post.num, ...)` must use the postsynaptic population size.
|
|
198
|
+
|
|
199
|
+
## Use presynaptic alignment for event-history dynamics
|
|
200
|
+
|
|
201
|
+
Use `ProjAlignPreMg2` when each presynaptic neuron owns kinetic state that must update before communication, as in the tutorial's AMPA model with spike-arrival time.
|
|
202
|
+
|
|
203
|
+
| API | Description |
|
|
204
|
+
|---|---|
|
|
205
|
+
| `bp.dyn.ProjAlignPreMg2(pre, syn, delay, comm, out, post, ...)` | Compose `presynaptic synapse -> delay -> communication -> output -> post`. |
|
|
206
|
+
| `AMPA.desc(pre.num, ...)` | Allocate one custom AMPA state per presynaptic neuron. |
|
|
207
|
+
| `bp.dnn.CSRLinear(connector, weight=...)` | Transform graded pre-aligned synapse state into postsynaptic conductance. |
|
|
208
|
+
| `MgBlock(...)` | Pass a concrete custom output instance for postsynaptic condition binding. |
|
|
209
|
+
|
|
210
|
+
```python
|
|
211
|
+
# Given a custom AMPA SynDyn and MgBlock SynOut:
|
|
212
|
+
self.proj = bp.dyn.ProjAlignPreMg2(
|
|
213
|
+
pre=pre,
|
|
214
|
+
syn=AMPA.desc(pre.num, alpha=0.98, beta=0.18, T=0.5, T_dur=0.5),
|
|
215
|
+
delay=delay,
|
|
216
|
+
comm=bp.dnn.CSRLinear(connector, weight=g_max),
|
|
217
|
+
out=MgBlock(E=0.0),
|
|
218
|
+
post=post,
|
|
219
|
+
)
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
The tutorial restricts postsynaptic alignment to exponential synapse dynamics. Do not move AMPA-like event-history state after communication merely to reduce its size; doing so changes the represented mechanism.
|
|
223
|
+
|
|
224
|
+
## Execute projection before the postsynaptic neuron
|
|
225
|
+
|
|
226
|
+
Projection output must be bound before the postsynaptic group sums inputs and integrates its current step.
|
|
227
|
+
|
|
228
|
+
```python
|
|
229
|
+
class TwoNeuronNetwork(bp.DynSysGroup):
|
|
230
|
+
def __init__(self):
|
|
231
|
+
super().__init__()
|
|
232
|
+
self.pre = bp.dyn.SpikeTimeGroup(
|
|
233
|
+
1,
|
|
234
|
+
indices=(0, 0, 0, 0),
|
|
235
|
+
times=(10.0, 30.0, 50.0, 70.0),
|
|
236
|
+
)
|
|
237
|
+
self.post = bp.dyn.LifRef(
|
|
238
|
+
1,
|
|
239
|
+
V_rest=-60.0,
|
|
240
|
+
V_reset=-60.0,
|
|
241
|
+
V_th=-50.0,
|
|
242
|
+
tau=20.0,
|
|
243
|
+
tau_ref=5.0,
|
|
244
|
+
V_initializer=bp.init.Constant(-60.0),
|
|
245
|
+
)
|
|
246
|
+
self.syn = ExponentialCOBA(
|
|
247
|
+
self.pre,
|
|
248
|
+
self.post,
|
|
249
|
+
prob=1.0,
|
|
250
|
+
g_max=0.1,
|
|
251
|
+
E=0.0,
|
|
252
|
+
seed=123,
|
|
253
|
+
)
|
|
254
|
+
|
|
255
|
+
def update(self):
|
|
256
|
+
self.pre()
|
|
257
|
+
self.syn()
|
|
258
|
+
self.post()
|
|
259
|
+
return self.post.V.value
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
network = TwoNeuronNetwork()
|
|
263
|
+
runner = bp.DSRunner(network, monitors=['post.V', 'post.spike'])
|
|
264
|
+
runner.run(100.0)
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
**Invariant:** Call `pre`, then the chemical projection, then `post` for each step. Calling `post` first delays the new synaptic current by one integration step.
|
|
268
|
+
|
|
269
|
+
## Common failures
|
|
270
|
+
|
|
271
|
+
- Do not store evolving voltage, gating, conductance, spike, or event-time state as an ordinary array.
|
|
272
|
+
- Do not use Python `if` on traced per-neuron spike conditions; use `bm.where` or boolean array operations.
|
|
273
|
+
- Do not update one coupled state before computing derivatives that require all old states.
|
|
274
|
+
- Do not confuse postsynaptic alignment with presynaptic alignment; descriptor size and operation order must agree.
|
|
275
|
+
- Do not guess descriptor ownership: both merging projections take a `syn` descriptor, `ProjAlignPostMg2` takes an `out` descriptor, and `ProjAlignPreMg2` takes a concrete `out` instance.
|
|
276
|
+
- Do not call the postsynaptic neuron before its chemical projection.
|
|
277
|
+
- Do not embed a dense communication matrix in custom synapse dynamics; keep connectivity and kinetics independently selectable.
|
|
278
|
+
|
|
279
|
+
## Routing
|
|
280
|
+
|
|
281
|
+
Open `integrators.md` for numerical methods and coupled equation signatures. Open `connecting neurons.md` for connector representations and JIT connectivity. Open `synaptic projections.md` for built-in projection families. Open `synpase properties.md` for built-in synapse dynamics and output choices. Open `infrastructure/delays.md` for delayed presynaptic events.
|
|
282
|
+
|
|
283
|
+
## Sources mirrored
|
|
284
|
+
|
|
285
|
+
- https://brainpy.readthedocs.io/tutorial_building/customize_neuron_models.html
|
|
286
|
+
- https://brainpy.readthedocs.io/tutorial_building/how_to_customze_a_synapse.html
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# Legacy BrainPy input generation
|
|
2
|
+
|
|
3
|
+
Use this reference to construct time-major stimulus arrays with
|
|
4
|
+
`brainpy.inputs` for a legacy BrainPy simulation. Use the resulting arrays as
|
|
5
|
+
iterable `DSRunner` inputs; do not substitute Braintools input objects or
|
|
6
|
+
`brainpy.state` APIs in legacy code.
|
|
7
|
+
|
|
8
|
+
## Choose an input family
|
|
9
|
+
|
|
10
|
+
All input constructors use `dt` to discretize physical duration. Choose by the
|
|
11
|
+
shape of the intended stimulus.
|
|
12
|
+
|
|
13
|
+
### Piecewise and transient inputs
|
|
14
|
+
|
|
15
|
+
| API | Description |
|
|
16
|
+
|---|---|
|
|
17
|
+
| `bp.inputs.section_input(values, durations, dt=None, return_length=False)` | Use for consecutive sections with independently specified values and durations; set `return_length=True` to also return total duration. |
|
|
18
|
+
| `bp.inputs.constant_input(I_and_duration, dt=None)` | Use for the older `(value, duration)` piecewise-constant form. Prefer `section_input()` in new legacy-compatible code. |
|
|
19
|
+
| `bp.inputs.spike_input(sp_times, sp_lens, sp_sizes, duration, dt=None)` | Use for short current pulses at explicit times; lengths and sizes may be scalars or one value per spike. |
|
|
20
|
+
| `bp.inputs.ramp_input(c_start, c_end, duration, t_start=0, t_end=None, dt=None)` | Use for a linear ramp between `t_start` and `t_end` within a longer stimulus. |
|
|
21
|
+
|
|
22
|
+
### Stochastic inputs
|
|
23
|
+
|
|
24
|
+
| API | Description |
|
|
25
|
+
|---|---|
|
|
26
|
+
| `bp.inputs.wiener_process(duration, dt=None, n=1, t_start=0.0, t_end=None, seed=None)` | Use for Wiener increments sampled from a zero-mean normal distribution with scale `sqrt(dt)`. |
|
|
27
|
+
| `bp.inputs.ou_process(mean, sigma, tau, duration, dt=None, n=1, t_start=0.0, t_end=None, seed=None)` | Use for a mean-reverting Ornstein-Uhlenbeck process. |
|
|
28
|
+
|
|
29
|
+
Set `seed=` whenever a stochastic stimulus must be reproducible. Set `n=` to
|
|
30
|
+
generate multiple independent traces.
|
|
31
|
+
|
|
32
|
+
### Periodic inputs
|
|
33
|
+
|
|
34
|
+
| API | Description |
|
|
35
|
+
|---|---|
|
|
36
|
+
| `bp.inputs.sinusoidal_input(amplitude, frequency, duration, dt=None, t_start=0.0, t_end=None, bias=False)` | Use for a sinusoidal waveform active over the selected time interval. |
|
|
37
|
+
| `bp.inputs.square_input(amplitude, frequency, duration, dt=None, bias=False, t_start=0.0, t_end=None)` | Use for an oscillatory square waveform active over the selected time interval. |
|
|
38
|
+
|
|
39
|
+
## Construct an injection-ready stimulus
|
|
40
|
+
|
|
41
|
+
The leading axis is simulation time. Make the constructor `dt` equal to the
|
|
42
|
+
runner `dt`, then mark the array as iterable when injecting it through
|
|
43
|
+
`DSRunner`.
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
import brainpy as bp
|
|
47
|
+
|
|
48
|
+
dt = 0.1
|
|
49
|
+
current, duration = bp.inputs.section_input(
|
|
50
|
+
values=[0.0, 1.0, 0.0],
|
|
51
|
+
durations=[100.0, 300.0, 100.0],
|
|
52
|
+
dt=dt,
|
|
53
|
+
return_length=True,
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
assert duration == 500.0
|
|
57
|
+
assert current.shape == (5000,)
|
|
58
|
+
|
|
59
|
+
runner = bp.DSRunner(
|
|
60
|
+
target=model,
|
|
61
|
+
inputs=[('input', current, 'iter')],
|
|
62
|
+
monitors=['V'],
|
|
63
|
+
dt=dt,
|
|
64
|
+
)
|
|
65
|
+
runner.run(duration)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
If `dt` is omitted, the constructor uses BrainPy's global default. An array
|
|
69
|
+
created under one `dt` and consumed under another silently changes its physical
|
|
70
|
+
duration, so pass the same explicit value at both boundaries.
|
|
71
|
+
|
|
72
|
+
## Compose values and shapes
|
|
73
|
+
|
|
74
|
+
Input constructors broadcast heterogeneous section values to their maximum
|
|
75
|
+
compatible shape. For example, scalar, vector, and matrix sections become one
|
|
76
|
+
time-major array with the broadcast data shape after the leading time axis.
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
import brainpy as bp
|
|
80
|
+
import brainpy.math as bm
|
|
81
|
+
|
|
82
|
+
current = bp.inputs.section_input(
|
|
83
|
+
values=[0.0, bm.ones(10), bm.ones((3, 10))],
|
|
84
|
+
durations=[100.0, 300.0, 100.0],
|
|
85
|
+
dt=0.1,
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
assert current.shape == (5000, 3, 10)
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Build a more complex stimulus by combining compatible generated arrays with
|
|
92
|
+
ordinary BrainPy math operations. Align `duration`, `dt`, and broadcast shape
|
|
93
|
+
before addition or multiplication; the constructors do not align two already
|
|
94
|
+
generated time grids for you.
|
|
95
|
+
|
|
96
|
+
## Source-backed failures
|
|
97
|
+
|
|
98
|
+
- Treat axis 0 as time. Do not pass a batch-major array as an unbatched iterable
|
|
99
|
+
`DSRunner` input.
|
|
100
|
+
- Use `return_length=True` only with `section_input()`; `constant_input()`
|
|
101
|
+
returns its legacy current-and-duration result directly.
|
|
102
|
+
- Give `sp_times`, `sp_lens`, and `sp_sizes` compatible lengths when the latter
|
|
103
|
+
two are not scalars.
|
|
104
|
+
- Keep `rate`, probability, and event encoders out of this reference; these
|
|
105
|
+
functions construct currents and stochastic processes, not Braintools data
|
|
106
|
+
preprocessing pipelines.
|
|
107
|
+
|
|
108
|
+
## Official sources
|
|
109
|
+
|
|
110
|
+
- `https://brainpy.readthedocs.io/apis/inputs.html`
|
|
111
|
+
- `https://brainpy.readthedocs.io/tutorial_toolbox/inputs.html`
|