brainx-skill 1.0.6 → 1.0.8

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 (91) hide show
  1. package/README.md +9 -2
  2. package/adapters/gemini.js +7 -0
  3. package/adapters/opencode.js +7 -0
  4. package/adapters/windsurf.js +7 -0
  5. package/installation/lib/installer.js +4 -1
  6. package/manifest.json +5 -1
  7. package/package.json +7 -2
  8. package/skills/braincell/SKILL.md +182 -0
  9. package/skills/braincell/references/area-scaled-hh-pattern.md +54 -0
  10. package/skills/braincell/references/braincell-custom-ion-channel-authoring.md +210 -0
  11. package/skills/braincell/references/channel-library.md +170 -0
  12. package/skills/braincell/references/ion-library.md +154 -0
  13. package/skills/braincell/references/mixions-for-adaptation.md +81 -0
  14. package/skills/braincell/references/multicompartment/braincell-manual-morphology-construction.md +180 -0
  15. package/skills/braincell/references/multicompartment/cv-policy-reference.md +133 -0
  16. package/skills/braincell/references/multicompartment/filter-function-library.md +162 -0
  17. package/skills/braincell/references/multicompartment/morphology-io-loading-validation.md +135 -0
  18. package/skills/braincell/references/multicompartment/multicompartment-cell-workflow.md +391 -0
  19. package/skills/braincell/references/multicompartment/probe-reference.md +128 -0
  20. package/skills/braincell/references/multicompartment/topology-building-and-visualization.md +121 -0
  21. package/skills/braincell/references/scripts/calcium_channel_gating.py +66 -0
  22. package/skills/braincell/references/scripts/cell_multicompartment_reference.py +221 -0
  23. package/skills/braincell/references/scripts/channel_ablation.py +61 -0
  24. package/skills/braincell/references/scripts/fi_curve.py +68 -0
  25. package/skills/braincell/references/scripts/hh_neuron_basics.py +59 -0
  26. package/skills/braincell/references/scripts/spike_frequency_adaptation.py +75 -0
  27. package/skills/braincell/references/scripts/t_current_rebound.py +72 -0
  28. package/skills/braincell/references/scripts/thalamic_neurons.py +228 -0
  29. package/skills/braincell/references/solver-library-with-effects.md +188 -0
  30. package/skills/brainevent/SKILL.md +268 -0
  31. package/skills/brainevent/references/connectivity-variants.md +312 -0
  32. package/skills/brainevent/references/custom-operators-cpu.md +304 -0
  33. package/skills/brainevent/references/custom-operators-gpu.md +468 -0
  34. package/skills/brainevent/references/scripts/102_EI_net_1996.py +107 -0
  35. package/skills/brainevent/references/scripts/204_joglekar_2018_propagation.py +168 -0
  36. package/skills/brainevent/references/scripts/coba_ei_teaching.py +356 -0
  37. package/skills/brainevent/references/sparse-formats.md +182 -0
  38. package/skills/brainevent/references/synaptic-plasticity.md +285 -0
  39. package/skills/brainpy-state/SKILL.md +232 -0
  40. package/skills/brainpy-state/references/array-creation.md +158 -0
  41. package/skills/brainpy-state/references/brain-dynamics-delay-protocol.md +96 -0
  42. package/skills/brainpy-state/references/braintools/brainstate-control-flow-patterns.md +334 -0
  43. package/skills/brainpy-state/references/braintools/data-preprocessing.md +79 -0
  44. package/skills/brainpy-state/references/braintools/metric.md +152 -0
  45. package/skills/brainpy-state/references/braintools/optimizer.md +179 -0
  46. package/skills/brainpy-state/references/braintools/parameter-initializer.md +183 -0
  47. package/skills/brainpy-state/references/braintools/surrogate.md +147 -0
  48. package/skills/brainpy-state/references/component-selection.md +273 -0
  49. package/skills/brainpy-state/references/nest-compatible/devices.md +157 -0
  50. package/skills/brainpy-state/references/nest-compatible/divergence-and-parity.md +114 -0
  51. package/skills/brainpy-state/references/nest-compatible/integration-categories.md +41 -0
  52. package/skills/brainpy-state/references/nest-compatible/model-library.md +200 -0
  53. package/skills/brainpy-state/references/nest-compatible/nest-workflow.md +156 -0
  54. package/skills/brainpy-state/references/nest-compatible/network-building.md +264 -0
  55. package/skills/brainpy-state/references/nest-compatible/scripts/brette_et_al_2007.py +320 -0
  56. package/skills/brainpy-state/references/nest-compatible/scripts/brunel_alpha.py +108 -0
  57. package/skills/brainpy-state/references/nest-compatible/scripts/brunel_delta.py +93 -0
  58. package/skills/brainpy-state/references/nest-compatible/scripts/clopath_synapse_spike_pairing.py +171 -0
  59. package/skills/brainpy-state/references/nest-compatible/scripts/evaluate_tsodyks2_synapse.py +147 -0
  60. package/skills/brainpy-state/references/nest-compatible/scripts/spatial_gaussex.py +112 -0
  61. package/skills/brainpy-state/references/nest-compatible/scripts/synapsecollection.py +198 -0
  62. package/skills/brainpy-state/references/nest-compatible/synapse-and-connectivity.md +153 -0
  63. package/skills/brainpy-state/references/projection-patterns.md +125 -0
  64. package/skills/brainpy-state/references/scripts/103_COBA_2005.py +86 -0
  65. package/skills/brainpy-state/references/scripts/106_COBA_HH_2007.py +181 -0
  66. package/skills/brainpy-state/references/scripts/107_gamma_oscillation_1996.py +156 -0
  67. package/skills/brainpy-state/references/scripts/109_fast_global_oscillation.py +111 -0
  68. package/skills/brainpy-state/references/scripts/201_surrogate_grad_lif_fashion_mnist.py +221 -0
  69. package/skills/brainpy-state/references/scripts/training-snn.py +119 -0
  70. package/skills/brainstate/SKILL.md +1 -1
  71. package/skills/brainstate/references/brainstate/brainstate-control-flow-patterns.md +250 -399
  72. package/skills/brainstate/references/brainstate/brainstate-transformed-diagnostics.md +6 -2
  73. package/skills/brainstate/references/brainstate/parameter-constraints-regularization.md +16 -2
  74. package/skills/brainstate/references/brainstate/parameter-transforms-regularizers-catalog.md +8 -3
  75. package/skills/brainstate/references/brainstate/randomness-and-reproducibility.md +64 -143
  76. package/skills/brainstate/references/brainstate/transformation-grad-expansion.md +13 -1
  77. package/skills/brainstate/references/brainstate/transformation-jit-expansion.md +11 -1
  78. package/skills/brainstate/references/brainstate/transformation-vmap-expansion.md +35 -7
  79. package/skills/brainstate/references/collective_model_operations.md +9 -9
  80. package/skills/brainstate/references/extension_mechanisms.md +12 -1
  81. package/skills/brainstate/references/libraries/prebuilt-activation-library.md +106 -97
  82. package/skills/brainstate/references/libraries/prebuilt-layer-library.md +102 -176
  83. package/skills/brainstate/references/model-interop-and-migration.md +11 -1
  84. package/skills/brainstate/references/size-inference-variations.md +11 -0
  85. package/skills/brainstate/references/state-graph-operations.md +13 -15
  86. package/skills/brainstate/references/state_collections_and_utilities.md +12 -1
  87. package/skills/brainx-general-guard/SKILL.md +153 -0
  88. package/skills/brainx-general-guard/references/brainstate-randomness-reproducibility/advanced-randomness.md +303 -0
  89. package/skills/brainx-general-guard/references/brainstate-randomness-reproducibility/randomness-and-reproducibility.md +62 -0
  90. package/skills/brainstate/references/braintools-optimizer-reference.md +0 -133
  91. package/skills/brainstate/references/libraries/solver-library-with-effects.md +0 -85
@@ -0,0 +1,268 @@
1
+ ---
2
+ name: brainevent
3
+ description: BrainEvent is the event-driven communication and plasticity layer for BrainX spiking models. Use this skill to translate neural projections, synaptic efficacy, binary firing events, spike-driven postsynaptic input, fixed or probabilistic fan-in or fan-out, and activity-dependent weight changes into `BinaryArray`, dense, CSR, CSC, JITC, fixed-degree connectivity, and plasticity operators, or to route custom event operators.
4
+ ---
5
+
6
+ ## Purpose and boundary
7
+
8
+ Use BrainEvent to represent binary spikes and communicate them through dense, explicit sparse, generated, or fixed-degree connectivity. Route neuron, synapse, channel, state, and neural-mass dynamics to their owning BrainX packages; never use `BinaryArray` for analog activity or BrainEvent as a complete simulator.
9
+
10
+ ## Underlying principle of BrainEvent
11
+
12
+ `BinaryArray` represents neuron spikes as boolean or 0/1 data. In `spikes @ connectivity`, BrainEvent processes only active presynaptic spikes and accumulates their weighted contributions into postsynaptic input.
13
+
14
+ Connectivity represents synaptic wiring. It states which presynaptic neurons project to which postsynaptic neurons; its weights state each synapse's sign and strength.
15
+
16
+ Plasticity operators represent activity-dependent changes in synaptic strength. Pre- and postsynaptic spikes and spike traces update stored weights.
17
+
18
+ ## Represent and multiply binary events
19
+
20
+ `BinaryArray` wraps boolean or 0/1 event data so multiplication visits active presynaptic rows and preserves a uniform call site across connectivity representations.
21
+
22
+ | API | Description |
23
+ |---|---|
24
+ | `BinaryArray(value)` | Use at the binary event boundary; it wraps a boolean or 0/1 vector or matrix, dispatches later products to event-driven implementations, and remains compatible with JAX transformations. |
25
+ | `spikes @ connectivity` | Use after the spike dimension and connectivity input dimension agree; it accumulates the connectivity rows selected by active events and returns the weighted postsynaptic input. |
26
+
27
+ ```python
28
+ import brainevent
29
+ import jax.numpy as jnp
30
+
31
+ pre_spikes = brainevent.BinaryArray([1, 0, 1, 0, 1])
32
+ weights = jnp.array([
33
+ [0.5, 0.2, 0.1],
34
+ [0.3, 0.4, 0.2],
35
+ [0.1, 0.5, 0.3],
36
+ [0.2, 0.1, 0.4],
37
+ [0.4, 0.3, 0.5],
38
+ ])
39
+ post_input = pre_spikes @ weights
40
+
41
+ # Only rows 0, 2, and 4 contribute.
42
+ active_rows = jnp.array([0, 2, 4])
43
+ assert jnp.allclose(post_input, weights[active_rows].sum(axis=0))
44
+ ```
45
+
46
+ Use a dense array only when its storage cost is acceptable. Open `references/sparse-formats.md` when explicit edges should be compressed, or `references/connectivity-variants.md` when random or fixed-degree structure should not be materialized densely.
47
+
48
+ ## API structure overview
49
+
50
+ ### Python API
51
+
52
+ | Category | Responsibility |
53
+ |---|---|
54
+ | Event Array Types | Represent binary events with `EventRepresentation` and `BinaryArray`. |
55
+ | Sparse Matrix Data Structures | Store explicit, generated, or fixed-degree connectivity with `CSR`, `CSC`, `JITC*`, and fixed-connection structures. |
56
+ | Matrix Operations | Apply event-driven dense, sparse, generated, fixed-connectivity, and plasticity operations. |
57
+ | Custom Kernel Framework | Define and load JAX-compatible custom kernels for CPU and accelerator backends. |
58
+ | Error Classes | Diagnose mathematical, kernel availability, compilation, fallback, execution, and CUDA installation failures. |
59
+ | Utility Functions | Convert sparse indices, generate LFSR random values, benchmark operations, and define transformation or type-conversion helpers. |
60
+ | Configuration API | Configure Numba parallelism, LFSR selection, and backend behavior. |
61
+
62
+ ### Custom kernels
63
+
64
+ | Category | Responsibility |
65
+ |---|---|
66
+ | `arg_spec` System | Declare kernel arguments, returned buffers, streams, and scalar attributes. |
67
+ | C++ API | Use BrainEvent tensors, dtypes, validation macros, and dispatch macros in C++ kernels. |
68
+ | Compiler Options | Control optimization, fast math, extra compiler flags, and CUDA graph support. |
69
+ | Caching | Control compiled-kernel cache keys, storage, rebuilds, and reuse. |
70
+
71
+ ## Choose a connectivity representation
72
+
73
+ Choose the representation before constructing it; every family supports the same `BinaryArray @ connectivity` call site but has a different storage and mutation contract.
74
+
75
+ | Representation | Use when | Avoid when |
76
+ |---|---|---|
77
+ | Dense JAX/NumPy array | The matrix is small or genuinely dense, roughly more than 25% nonzero, or arbitrary per-edge values require the simplest storage. | A large matrix is mostly zero. |
78
+ | `CSR` / `CSC` | Edges are explicit, fixed, reusable, inspectable, or mutable. Use CSR for row-oriented work and CSC for column-oriented work. | Random connectivity is too large to materialize. |
79
+ | `JITC*` | Connectivity is random with fixed probability and must be regenerated from compact parameters and a stable seed. | Individual edges must be inspected, mutated, or learned. |
80
+ | `FixedNumPerPre` / `FixedNumPerPost` | Each neuron has a fixed number of outputs or inputs and that topology should be encoded directly. | Connection counts vary per neuron. |
81
+
82
+ Use explicit `CSR` or `CSC` for stored edges, `JITC*` for random and huge connectivity, and fixed-degree structures for constant fan-in or fan-out.
83
+
84
+ ## Build explicit sparse connectivity
85
+
86
+ `CSR` and `CSC` store explicit sparse connectivity for row- or column-oriented operations.
87
+
88
+ | API | Description |
89
+ |---|---|
90
+ | `CSR(data, indices=None, indptr=None, *, shape, ...)` | Use for explicit row-oriented sparse connectivity and the normal forward `BinaryArray @ connectivity` path; it stores nonzero values, column indices, and row pointers. |
91
+ | `CSC(data, indices=None, indptr=None, *, shape, ...)` | Use for explicit column-oriented sparse connectivity or transpose-centered work; it stores nonzero values, row indices, and column pointers. |
92
+
93
+ ```python
94
+ import brainevent
95
+ import jax.numpy as jnp
96
+
97
+ connectivity = brainevent.CSR(
98
+ (
99
+ jnp.array([0.5, 0.2, 0.7, 0.4]),
100
+ jnp.array([0, 1, 0, 1]),
101
+ jnp.array([0, 1, 2, 3, 4]),
102
+ ),
103
+ shape=(4, 2),
104
+ )
105
+ spikes = brainevent.BinaryArray([1, 0, 1, 0])
106
+ postsynaptic_input = spikes @ connectivity
107
+
108
+ assert postsynaptic_input.shape == (2,)
109
+ ```
110
+
111
+ This example constructs only `CSR`; do not duplicate it for every format. Open `references/sparse-formats.md` when explicit edges must be imported, CSC orientation is required, or stored formats must be converted.
112
+
113
+ ## Generate random connectivity
114
+
115
+ JITC matrices store a probability, weight-distribution parameters, and a seed, then regenerate the required connections during computation instead of storing individual edges.
116
+
117
+ | API | Description |
118
+ |---|---|
119
+ | `JITCScalarR(weight, prob=None, seed=None, *, shape, ...)` | Use for row-oriented connectivity with one shared nonzero weight; it regenerates a reproducible graph from `prob` and `seed`. |
120
+ | `JITCScalarC(weight, prob=None, seed=None, *, shape, ...)` | Use for the column-oriented form of shared-weight generated connectivity. |
121
+ | `JITCNormalR(loc, scale=None, prob=None, seed=None, *, shape, ...)` | Use for row-oriented generated weights drawn from a normal distribution. |
122
+ | `JITCNormalC(loc, scale=None, prob=None, seed=None, *, shape, ...)` | Use for the column-oriented normal-weight form. |
123
+ | `JITCUniformR(low, high=None, prob=None, seed=None, *, shape, ...)` | Use for row-oriented generated weights bounded by a uniform distribution. |
124
+ | `JITCUniformC(low, high=None, prob=None, seed=None, *, shape, ...)` | Use for the column-oriented uniform-weight form. |
125
+
126
+ ```python
127
+ import brainevent
128
+ import jax.numpy as jnp
129
+
130
+ n_pre = 100_000
131
+ n_post = 100_000
132
+ connectivity = brainevent.JITCScalarR(
133
+ (0.5, 0.01, 7),
134
+ shape=(n_pre, n_post),
135
+ )
136
+ spikes = brainevent.BinaryArray(
137
+ jnp.zeros(n_pre, dtype=bool).at[::1000].set(True)
138
+ )
139
+ postsynaptic_input = spikes @ connectivity
140
+
141
+ assert postsynaptic_input.shape == (n_post,)
142
+ ```
143
+
144
+ This example constructs only `JITCScalarR`. Keep the seed stable when the realized graph must remain reproducible. Open `references/connectivity-variants.md` when choosing a weight distribution, row/column orientation, or benchmarking an uncertain contraction.
145
+
146
+ ## Encode fixed-degree connectivity
147
+
148
+ Fixed-degree structures store one connection count per relevant neuron population, so choose the class by whether the invariant is fan-in or fan-out.
149
+
150
+ | API | Description |
151
+ |---|---|
152
+ | `FixedNumPerPre(data, indices=None, *, shape, ...)` | Use when every presynaptic neuron has the same number of outputs; it stores data and target indices with shape `(num_pre, connections_per_pre)`. |
153
+ | `FixedNumPerPost(data, indices=None, *, shape, ...)` | Use when every postsynaptic neuron receives the same number of inputs; it stores data and source indices with shape `(num_post, connections_per_post)`. |
154
+ | `FixedPostNumConn` | Recognize as the deprecated alias of `FixedNumPerPre`; migrate new code to the current name. |
155
+ | `FixedPreNumConn` | Recognize as the deprecated alias of `FixedNumPerPost`; migrate new code to the current name. |
156
+
157
+ ```python
158
+ import brainevent
159
+ import jax.numpy as jnp
160
+
161
+ connectivity = brainevent.FixedNumPerPre(
162
+ (
163
+ jnp.array([
164
+ [0.5, 0.2],
165
+ [0.4, 0.1],
166
+ [0.3, 0.6],
167
+ [0.7, 0.2],
168
+ ]),
169
+ jnp.array([
170
+ [0, 2],
171
+ [1, 2],
172
+ [0, 1],
173
+ [1, 2],
174
+ ]),
175
+ ),
176
+ shape=(4, 3),
177
+ )
178
+ spikes = brainevent.BinaryArray([1, 0, 1, 0])
179
+ postsynaptic_input = spikes @ connectivity
180
+
181
+ assert postsynaptic_input.shape == (3,)
182
+ ```
183
+
184
+ This example constructs only `FixedNumPerPre`. Open `references/connectivity-variants.md` when fixed fan-in is required, a deprecated alias appears in existing code, or the stored index shape must be checked.
185
+
186
+ ## Apply event-driven synaptic plasticity
187
+
188
+ Plasticity combines binary spike triggers with decaying activity traces to update only stored synaptic weights; keep the connectivity topology fixed and choose the operator by storage format and trigger direction.
189
+
190
+ | API | Description |
191
+ |---|---|
192
+ | `update_csr_on_binary_pre(...)` | Use when presynaptic spikes trigger updates to explicit CSR weights; it visits stored outgoing synapses, applies the postsynaptic trace and bounds, and returns updated CSR data without changing `indices` or `indptr`. |
193
+ | `update_csr_on_binary_post(...)` | Use when postsynaptic spikes trigger updates to weights stored in CSR order; provide a CSC view plus `weight_indices` mapping CSC positions back to CSR data, and it returns the updated CSR-ordered values. |
194
+ | `update_dense_on_binary_pre(...)` | Use for a small dense matrix when presynaptic spikes trigger updates; it returns the updated dense weights. |
195
+ | `update_dense_on_binary_post(...)` | Use for a small dense matrix when postsynaptic spikes trigger updates; it returns the updated dense weights. |
196
+
197
+ ```python
198
+ import brainevent
199
+ import jax.numpy as jnp
200
+
201
+ weights = brainevent.CSR(
202
+ (
203
+ jnp.array([0.2, 0.4, 0.3, 0.5]),
204
+ jnp.array([0, 1, 1, 0]),
205
+ jnp.array([0, 2, 3, 4]),
206
+ ),
207
+ shape=(3, 2),
208
+ )
209
+ new_data = brainevent.update_csr_on_binary_pre(
210
+ weight=weights.data,
211
+ indices=weights.indices,
212
+ indptr=weights.indptr,
213
+ pre_spike=jnp.array([True, False, True]),
214
+ post_trace=jnp.array([0.02, 0.01]),
215
+ w_min=0.0,
216
+ w_max=1.0,
217
+ shape=weights.shape,
218
+ )
219
+ updated = brainevent.CSR(
220
+ (new_data, weights.indices, weights.indptr),
221
+ shape=weights.shape,
222
+ )
223
+
224
+ assert updated.data.shape == weights.data.shape
225
+ assert jnp.array_equal(updated.indices, weights.indices)
226
+ assert jnp.array_equal(updated.indptr, weights.indptr)
227
+ ```
228
+
229
+ Open `references/synaptic-plasticity.md` when implementing decaying traces, a CSR STDP loop, or a complete adaptive network; open the operations API linked there for exact postsynaptic and dense update signatures.
230
+
231
+ ## Transform and verify the product
232
+
233
+ Keep the complete event-driven product inside the JAX transform, then verify shape, orientation, and reproducibility rather than relying on a class suffix alone.
234
+
235
+ | API | Description |
236
+ |---|---|
237
+ | `jax.jit(function)` | Use to compile a complete event-driven product; it traces `BinaryArray` and connectivity PyTrees and returns a compiled callable for compatible shapes. |
238
+ | `jax.vmap(function, ...)` | Use to batch independent event-driven products; it maps the same communication rule over the selected array axis. |
239
+ | `brainevent.benchmark_function(function, ...)` | Use when row- versus column-oriented performance is uncertain; it benchmarks the actual workload and returns timing statistics. |
240
+
241
+ ```python
242
+ import jax
243
+
244
+ @jax.jit
245
+ def communicate(spikes, connectivity):
246
+ return spikes @ connectivity
247
+
248
+ postsynaptic_input = communicate(spikes, connectivity)
249
+ ```
250
+
251
+ Verify `postsynaptic_input.shape == connectivity.shape[1:]` for vector input and confirm that repeated JITC runs with the same seed reproduce the intended graph.
252
+
253
+ ## Reference routing
254
+
255
+ | Reference | Open when |
256
+ |---|---|
257
+ | `references/sparse-formats.md` | Open when explicit connectivity must be imported, converted between CSR and CSC, or oriented for row or column access; it contains the construction, conversion, and storage invariants. |
258
+ | `references/connectivity-variants.md` | Open when choosing among all six JITC distribution/orientation variants or between fixed fan-in and fan-out; it contains constructor semantics, index shapes, deprecated alias mapping, seed rules, and benchmarking guidance. |
259
+ | `references/synaptic-plasticity.md` | Open when pre- or postsynaptic events must update stored CSR or dense weights; it contains all four public update variants and one CSR STDP pattern. |
260
+ | `references/custom-operators-cpu.md` | Open when a custom operation targets CPU; it contains Numba CPU, raw C++, CPU registration and transformation rules, the C++ ABI, compilation, caching, diagnostics, and verification workflows. |
261
+ | `references/custom-operators-gpu.md` | Open when a custom operation targets GPU; it contains Numba CUDA, Warp, raw CUDA, GPU and multi-backend registration, stream and ABI rules, compiler controls, caching, diagnostics, and verification workflows. |
262
+
263
+ ## Application script examples
264
+ | Reference | Open when |
265
+ |---|---|
266
+ | `references/scripts/coba_ei_teaching.py` | Open for the shared BrainEvent and BrainPy teaching example; it uses `BinaryArray` with interchangeable `FixedNumPerPre`, `CSR`, and dense connectivity for efficient event-driven communication into BrainPy `LIFRef`, `Expon`, and `COBA` dynamics inside one compiled BrainState loop. |
267
+ | `references/scripts/102_EI_net_1996.py` | Open for a complete high-level E/I network already using `brainpy.state.AlignPostProj` and `brainstate.nn.EventFixedProb`; it preserves unit-aware weights, initialization, compiled time loops, and visualization. |
268
+ | `references/scripts/204_joglekar_2018_propagation.py` | Open for delayed spikes, area mapping, and vmapped `JITCScalarC` communication; it preserves delays, seeds, external-data assumptions, and BrainPy compatibility details. |
@@ -0,0 +1,312 @@
1
+ # BrainEvent connectivity variants
2
+
3
+ Use this reference after choosing generated or fixed-degree connectivity in the root skill. Select the exact API from the tables; each code block constructs only one representative variation.
4
+
5
+ ## JITC distribution and orientation variants
6
+
7
+ JITC classes regenerate sparse connectivity from compact distribution parameters, probability, and seed; `R` is row-oriented and `C` is column-oriented.
8
+
9
+ | API | Description |
10
+ |---|---|
11
+ | `JITCScalarR(weight, prob=None, seed=None, *, shape, ...)` | Use for row-oriented connectivity when every realized edge has one shared weight. |
12
+ | `JITCScalarC(weight, prob=None, seed=None, *, shape, ...)` | Use for the column-oriented shared-weight variation. |
13
+ | `JITCNormalR(loc, scale=None, prob=None, seed=None, *, shape, ...)` | Use for row-oriented connectivity whose realized weights follow a normal distribution. |
14
+ | `JITCNormalC(loc, scale=None, prob=None, seed=None, *, shape, ...)` | Use for the column-oriented normal-distribution variation. |
15
+ | `JITCUniformR(low, high=None, prob=None, seed=None, *, shape, ...)` | Use for row-oriented connectivity whose realized weights stay within uniform bounds. |
16
+ | `JITCUniformC(low, high=None, prob=None, seed=None, *, shape, ...)` | Use for the column-oriented uniform-distribution variation. |
17
+
18
+ ```python
19
+ import brainevent
20
+ import brainstate
21
+ import jax.numpy as jnp
22
+
23
+ n_pre = 1000
24
+ n_post = 500
25
+
26
+ # Shared weight, connection probability, and seed.
27
+ connectivity = brainevent.JITCScalarR(
28
+ (0.1, 0.1, 12345),
29
+ shape=(n_pre, n_post),
30
+ )
31
+ print("shape:", connectivity.shape)
32
+ print("weight:", connectivity.weight)
33
+ print("probability:", connectivity.prob)
34
+ print("seed:", connectivity.seed)
35
+
36
+ brainstate.random.seed(0)
37
+ spikes = brainevent.BinaryArray(
38
+ brainstate.random.bernoulli(0.05, size=(n_pre,))
39
+ )
40
+ postsynaptic_input = spikes @ connectivity
41
+
42
+ print("active inputs:", jnp.sum(spikes.value))
43
+ print("output shape:", postsynaptic_input.shape)
44
+ print("nonzero outputs:", jnp.sum(postsynaptic_input > 0))
45
+
46
+ # A stable constructor seed reproduces the same generated graph.
47
+ first = brainevent.JITCScalarR(
48
+ (0.1, 0.1, 999),
49
+ shape=(100, 50),
50
+ )
51
+ replay = brainevent.JITCScalarR(
52
+ (0.1, 0.1, 999),
53
+ shape=(100, 50),
54
+ )
55
+ different = brainevent.JITCScalarR(
56
+ (0.1, 0.1, 888),
57
+ shape=(100, 50),
58
+ )
59
+ test_spikes = brainevent.BinaryArray(jnp.ones(100, dtype=bool))
60
+
61
+ first_result = test_spikes @ first
62
+ replay_result = test_spikes @ replay
63
+ different_result = test_spikes @ different
64
+
65
+ assert postsynaptic_input.shape == (n_post,)
66
+ assert jnp.allclose(first_result, replay_result)
67
+ print("different seed matches:", jnp.allclose(first_result, different_result))
68
+ ```
69
+
70
+ This example mirrors the official homogeneous JITC construction, event multiplication, and seed-replay cells while constructing only `JITCScalarR`. Keep `seed` stable for reproducible connectivity. Do not use any JITC variation when individual edges must be inspected, persistently mutated, or learned.
71
+
72
+ ## Application: ultra-large JITC network
73
+
74
+ The official practice network composes two generated layers whose storage remains a few distribution parameters even when their logical connection counts reach millions.
75
+
76
+ ```python
77
+ import brainevent
78
+ import brainstate
79
+ import jax
80
+ import jax.numpy as jnp
81
+
82
+
83
+ class MassiveJITCNetwork:
84
+ def __init__(
85
+ self,
86
+ n_input,
87
+ n_hidden,
88
+ n_output,
89
+ connection_probability=0.01,
90
+ seed=0,
91
+ ):
92
+ self.n_input = n_input
93
+ self.n_hidden = n_hidden
94
+ self.n_output = n_output
95
+ self.w1 = brainevent.JITCNormalR(
96
+ (0.0, 0.05, connection_probability, seed),
97
+ shape=(n_input, n_hidden),
98
+ )
99
+ self.w2 = brainevent.JITCNormalR(
100
+ (0.0, 0.1, connection_probability * 2, seed + 1),
101
+ shape=(n_hidden, n_output),
102
+ )
103
+
104
+ def forward(self, input_spikes):
105
+ hidden_input = input_spikes @ self.w1
106
+ hidden_spikes = brainevent.BinaryArray(hidden_input > 0.2)
107
+ output = hidden_spikes @ self.w2
108
+ return output, hidden_spikes
109
+
110
+
111
+ network = MassiveJITCNetwork(
112
+ n_input=100_000,
113
+ n_hidden=50_000,
114
+ n_output=1_000,
115
+ connection_probability=0.01,
116
+ seed=2024,
117
+ )
118
+
119
+ brainstate.random.seed(999)
120
+ input_spikes = brainevent.BinaryArray(
121
+ brainstate.random.bernoulli(0.001, size=(100_000,))
122
+ )
123
+ output, hidden_spikes = network.forward(input_spikes)
124
+ output = jax.block_until_ready(output)
125
+
126
+ assert output.shape == (1_000,)
127
+ print("active inputs:", jnp.sum(input_spikes.value))
128
+ print("active hidden neurons:", jnp.sum(hidden_spikes.value))
129
+ print("active outputs:", jnp.sum(output > 0.5))
130
+ ```
131
+
132
+ This adapts “Practice: Ultra-large-scale spiking neural network.” Use it only when generated weights are acceptable: neither layer exposes persistent individual edges for inspection or learning.
133
+
134
+ ## Choose row or column orientation
135
+
136
+ Use the orientation that keeps the indexed contraction dimension contiguous; do not assume an `R` or `C` suffix alone determines the fastest implementation for every workload.
137
+
138
+ | API | Description |
139
+ |---|---|
140
+ | `*R` JITC classes | Use for the normal forward row-oriented event contraction, conceptually following CSR. |
141
+ | `*C` JITC classes | Use for column-oriented or transpose-centered contraction, conceptually following CSC. |
142
+ | `brainevent.benchmark_function()` | Use when orientation performance is uncertain; benchmark the complete representative product rather than construction alone. |
143
+
144
+ Keep the same shape, event pattern, distribution parameters, seed, backend, warmup, and trial count when comparing orientations.
145
+
146
+ ## Fixed-degree variants
147
+
148
+ Fixed-degree structures encode whether a constant connection count belongs to each presynaptic or postsynaptic neuron.
149
+
150
+ | API | Description |
151
+ |---|---|
152
+ | `FixedNumPerPre(data, indices=None, *, shape, ...)` | Use for fixed fan-out: every presynaptic neuron has the same number of target indices, and `data` and `indices` use shape `(num_pre, connections_per_pre)`. |
153
+ | `FixedNumPerPost(data, indices=None, *, shape, ...)` | Use for fixed fan-in: every postsynaptic neuron has the same number of source indices, and `data` and `indices` use shape `(num_post, connections_per_post)`. |
154
+ | `FixedPostNumConn` | Recognize as the deprecated alias of `FixedNumPerPre`; replace it when maintaining older code. |
155
+ | `FixedPreNumConn` | Recognize as the deprecated alias of `FixedNumPerPost`; replace it when maintaining older code. |
156
+
157
+ ```python
158
+ import brainevent
159
+ import brainstate
160
+ import jax.numpy as jnp
161
+
162
+ n_pre = 100
163
+ n_post = 50
164
+ connections_per_pre = 10
165
+
166
+ brainstate.random.seed(42)
167
+ indices = brainstate.random.randint(
168
+ 0,
169
+ n_post,
170
+ size=(n_pre, connections_per_pre),
171
+ )
172
+ weights = (
173
+ brainstate.random.normal(size=(n_pre, connections_per_pre))
174
+ * 0.1
175
+ )
176
+
177
+ connectivity = brainevent.FixedNumPerPre(
178
+ (weights, indices),
179
+ shape=(n_pre, n_post),
180
+ )
181
+
182
+ print("shape:", connectivity.shape)
183
+ print("weight shape:", connectivity.data.shape)
184
+ print("index shape:", connectivity.indices.shape)
185
+ print("neuron 0 targets:", connectivity.indices[0])
186
+
187
+ brainstate.random.seed(999)
188
+ spikes = brainevent.BinaryArray(
189
+ brainstate.random.bernoulli(0.1, size=(n_pre,))
190
+ )
191
+ postsynaptic_input = spikes @ connectivity
192
+
193
+ print("active inputs:", jnp.sum(spikes.value))
194
+ print("output shape:", postsynaptic_input.shape)
195
+ print("nonzero outputs:", jnp.sum(postsynaptic_input != 0))
196
+
197
+ assert connectivity.data.shape == (n_pre, connections_per_pre)
198
+ assert postsynaptic_input.shape == (n_post,)
199
+ ```
200
+
201
+ This example mirrors the official fixed fan-out construction and propagation workflow, replacing its deprecated `FixedPostNumConn` alias with current `FixedNumPerPre`. Reverse the storage orientation and index meaning when selecting `FixedNumPerPost`; do not reuse the fan-out array shape unchanged.
202
+
203
+ ## Application: fixed-degree cortical E/I network
204
+
205
+ The official cortical practice composes fixed fan-out E-to-E, E-to-I, and I-to-E pathways, then repeatedly thresholds their event-driven inputs into new population spikes.
206
+
207
+ ```python
208
+ import brainevent
209
+ import brainstate
210
+ import jax.numpy as jnp
211
+ import numpy as np
212
+
213
+
214
+ class CorticalNetwork:
215
+ def __init__(
216
+ self,
217
+ n_exc=800,
218
+ n_inh=200,
219
+ exc_fanout=50,
220
+ inh_fanout=30,
221
+ seed=0,
222
+ ):
223
+ self.n_exc = n_exc
224
+ self.n_inh = n_inh
225
+ brainstate.random.seed(seed)
226
+
227
+ ee_indices = brainstate.random.randint(
228
+ 0,
229
+ n_exc,
230
+ size=(n_exc, exc_fanout),
231
+ )
232
+ ee_weights = (
233
+ brainstate.random.normal(size=(n_exc, exc_fanout))
234
+ * 0.05
235
+ )
236
+ self.w_ee = brainevent.FixedNumPerPre(
237
+ (ee_weights, ee_indices),
238
+ shape=(n_exc, n_exc),
239
+ )
240
+
241
+ ei_indices = brainstate.random.randint(
242
+ 0,
243
+ n_inh,
244
+ size=(n_exc, exc_fanout // 2),
245
+ )
246
+ ei_weights = (
247
+ brainstate.random.normal(size=(n_exc, exc_fanout // 2))
248
+ * 0.08
249
+ )
250
+ self.w_ei = brainevent.FixedNumPerPre(
251
+ (ei_weights, ei_indices),
252
+ shape=(n_exc, n_inh),
253
+ )
254
+
255
+ ie_indices = brainstate.random.randint(
256
+ 0,
257
+ n_exc,
258
+ size=(n_inh, inh_fanout),
259
+ )
260
+ ie_weights = (
261
+ -jnp.abs(
262
+ brainstate.random.normal(size=(n_inh, inh_fanout))
263
+ )
264
+ * 0.15
265
+ )
266
+ self.w_ie = brainevent.FixedNumPerPre(
267
+ (ie_weights, ie_indices),
268
+ shape=(n_inh, n_exc),
269
+ )
270
+
271
+ def forward(self, exc_spikes, inh_spikes):
272
+ exc_input = (
273
+ exc_spikes @ self.w_ee
274
+ + inh_spikes @ self.w_ie
275
+ )
276
+ inh_input = exc_spikes @ self.w_ei
277
+ return exc_input, inh_input
278
+
279
+
280
+ network = CorticalNetwork(seed=2024)
281
+ brainstate.random.seed(0)
282
+ exc_spikes = brainevent.BinaryArray(
283
+ brainstate.random.bernoulli(0.1, size=(800,))
284
+ )
285
+ inh_spikes = brainevent.BinaryArray(jnp.zeros(200, dtype=bool))
286
+
287
+ exc_counts = []
288
+ inh_counts = []
289
+ for _ in range(100):
290
+ exc_input, inh_input = network.forward(exc_spikes, inh_spikes)
291
+ external_input = brainstate.random.normal(size=(800,)) * 0.3
292
+ exc_spikes = brainevent.BinaryArray(
293
+ exc_input + external_input > 0.3
294
+ )
295
+ inh_spikes = brainevent.BinaryArray(inh_input > 0.25)
296
+ exc_counts.append(int(jnp.sum(exc_spikes.value)))
297
+ inh_counts.append(int(jnp.sum(inh_spikes.value)))
298
+
299
+ assert exc_spikes.value.shape == (800,)
300
+ assert inh_spikes.value.shape == (200,)
301
+ print("mean E/I spikes:", np.mean(exc_counts), np.mean(inh_counts))
302
+ ```
303
+
304
+ This adapts “Practice: Biologically realistic cortical network,” replaces deprecated fixed-degree aliases, removes plotting, and makes inhibitory weights strictly non-positive. Treat the threshold loop as the tutorial’s connectivity demonstration, not as a complete biophysical neuron model.
305
+
306
+ ## Sources
307
+
308
+ - Tutorial 3, JIT Connection Matrices: https://brainx.chaobrain.com/brainevent/tutorials/data-structures/03_jit_connectivity.html
309
+ - Tutorial 4, Fixed Connection Count Structures: https://brainx.chaobrain.com/brainevent/tutorials/data-structures/04_fixed_connections.html
310
+ - Choose a connectivity format: https://brainx.chaobrain.com/brainevent/how-to/data-structures/choosing-a-sparse-format.html
311
+ - Sparse Matrix Data Structures API: https://brainx.chaobrain.com/brainevent/reference/apis/sparsedata.html
312
+ - Utility Functions API: https://brainx.chaobrain.com/brainevent/reference/apis/utilities.html