brainx-skill 1.0.3 → 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 (117) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +29 -64
  3. package/adapters/gemini.js +7 -0
  4. package/adapters/opencode.js +7 -0
  5. package/adapters/windsurf.js +7 -0
  6. package/{lib → installation/lib}/installer.js +8 -5
  7. package/manifest.json +7 -1
  8. package/package.json +25 -10
  9. package/skills/braincell/SKILL.md +182 -0
  10. package/skills/braincell/references/area-scaled-hh-pattern.md +54 -0
  11. package/skills/braincell/references/braincell-custom-ion-channel-authoring.md +210 -0
  12. package/skills/braincell/references/channel-library.md +170 -0
  13. package/skills/braincell/references/ion-library.md +154 -0
  14. package/skills/braincell/references/mixions-for-adaptation.md +81 -0
  15. package/skills/braincell/references/multicompartment/braincell-manual-morphology-construction.md +180 -0
  16. package/skills/braincell/references/multicompartment/cv-policy-reference.md +133 -0
  17. package/skills/braincell/references/multicompartment/filter-function-library.md +162 -0
  18. package/skills/braincell/references/multicompartment/morphology-io-loading-validation.md +135 -0
  19. package/skills/braincell/references/multicompartment/multicompartment-cell-workflow.md +391 -0
  20. package/skills/braincell/references/multicompartment/probe-reference.md +128 -0
  21. package/skills/braincell/references/multicompartment/topology-building-and-visualization.md +121 -0
  22. package/skills/braincell/references/scripts/calcium_channel_gating.py +66 -0
  23. package/skills/braincell/references/scripts/cell_multicompartment_reference.py +221 -0
  24. package/skills/braincell/references/scripts/channel_ablation.py +61 -0
  25. package/skills/braincell/references/scripts/fi_curve.py +68 -0
  26. package/skills/braincell/references/scripts/hh_neuron_basics.py +59 -0
  27. package/skills/braincell/references/scripts/spike_frequency_adaptation.py +75 -0
  28. package/skills/braincell/references/scripts/t_current_rebound.py +72 -0
  29. package/skills/braincell/references/scripts/thalamic_neurons.py +228 -0
  30. package/skills/braincell/references/solver-library-with-effects.md +188 -0
  31. package/skills/brainevent/SKILL.md +268 -0
  32. package/skills/brainevent/references/connectivity-variants.md +312 -0
  33. package/skills/brainevent/references/custom-operators-cpu.md +304 -0
  34. package/skills/brainevent/references/custom-operators-gpu.md +468 -0
  35. package/skills/brainevent/references/scripts/102_EI_net_1996.py +107 -0
  36. package/skills/brainevent/references/scripts/204_joglekar_2018_propagation.py +168 -0
  37. package/skills/brainevent/references/scripts/coba_ei_teaching.py +356 -0
  38. package/skills/brainevent/references/sparse-formats.md +182 -0
  39. package/skills/brainevent/references/synaptic-plasticity.md +285 -0
  40. package/skills/brainpy-state/SKILL.md +232 -0
  41. package/skills/brainpy-state/references/array-creation.md +158 -0
  42. package/skills/brainpy-state/references/brain-dynamics-delay-protocol.md +96 -0
  43. package/skills/brainpy-state/references/braintools/brainstate-control-flow-patterns.md +334 -0
  44. package/skills/brainpy-state/references/braintools/data-preprocessing.md +79 -0
  45. package/skills/brainpy-state/references/braintools/metric.md +152 -0
  46. package/skills/brainpy-state/references/braintools/optimizer.md +179 -0
  47. package/skills/brainpy-state/references/braintools/parameter-initializer.md +183 -0
  48. package/skills/brainpy-state/references/braintools/surrogate.md +147 -0
  49. package/skills/brainpy-state/references/component-selection.md +273 -0
  50. package/skills/brainpy-state/references/nest-compatible/devices.md +157 -0
  51. package/skills/brainpy-state/references/nest-compatible/divergence-and-parity.md +114 -0
  52. package/skills/brainpy-state/references/nest-compatible/integration-categories.md +41 -0
  53. package/skills/brainpy-state/references/nest-compatible/model-library.md +200 -0
  54. package/skills/brainpy-state/references/nest-compatible/nest-workflow.md +156 -0
  55. package/skills/brainpy-state/references/nest-compatible/network-building.md +264 -0
  56. package/skills/brainpy-state/references/nest-compatible/scripts/brette_et_al_2007.py +320 -0
  57. package/skills/brainpy-state/references/nest-compatible/scripts/brunel_alpha.py +108 -0
  58. package/skills/brainpy-state/references/nest-compatible/scripts/brunel_delta.py +93 -0
  59. package/skills/brainpy-state/references/nest-compatible/scripts/clopath_synapse_spike_pairing.py +171 -0
  60. package/skills/brainpy-state/references/nest-compatible/scripts/evaluate_tsodyks2_synapse.py +147 -0
  61. package/skills/brainpy-state/references/nest-compatible/scripts/spatial_gaussex.py +112 -0
  62. package/skills/brainpy-state/references/nest-compatible/scripts/synapsecollection.py +198 -0
  63. package/skills/brainpy-state/references/nest-compatible/synapse-and-connectivity.md +153 -0
  64. package/skills/brainpy-state/references/projection-patterns.md +125 -0
  65. package/skills/brainpy-state/references/scripts/103_COBA_2005.py +86 -0
  66. package/skills/brainpy-state/references/scripts/106_COBA_HH_2007.py +181 -0
  67. package/skills/brainpy-state/references/scripts/107_gamma_oscillation_1996.py +156 -0
  68. package/skills/brainpy-state/references/scripts/109_fast_global_oscillation.py +111 -0
  69. package/skills/brainpy-state/references/scripts/201_surrogate_grad_lif_fashion_mnist.py +221 -0
  70. package/skills/brainpy-state/references/scripts/training-snn.py +119 -0
  71. package/skills/brainstate/SKILL.md +399 -0
  72. package/skills/brainstate/agents/openai.yaml +4 -0
  73. package/skills/brainstate/references/brainstate/brainstate-control-flow-patterns.md +334 -0
  74. package/skills/brainstate/references/brainstate/brainstate-transformed-diagnostics.md +180 -0
  75. package/skills/brainstate/references/brainstate/parameter-constraints-regularization.md +213 -0
  76. package/skills/brainstate/references/brainstate/parameter-transforms-regularizers-catalog.md +171 -0
  77. package/skills/brainstate/references/brainstate/randomness-and-reproducibility.md +131 -0
  78. package/skills/brainstate/references/brainstate/transformation-grad-expansion.md +462 -0
  79. package/skills/brainstate/references/brainstate/transformation-jit-expansion.md +167 -0
  80. package/skills/brainstate/references/brainstate/transformation-vmap-expansion.md +314 -0
  81. package/skills/brainstate/references/collective_model_operations.md +195 -0
  82. package/skills/brainstate/references/extension_mechanisms.md +293 -0
  83. package/skills/brainstate/references/libraries/prebuilt-activation-library.md +115 -0
  84. package/skills/brainstate/references/libraries/prebuilt-layer-library.md +143 -0
  85. package/skills/brainstate/references/model-interop-and-migration.md +305 -0
  86. package/skills/brainstate/references/simulation-environment.md +117 -0
  87. package/skills/brainstate/references/size-inference-variations.md +279 -0
  88. package/skills/brainstate/references/state-graph-operations.md +216 -0
  89. package/skills/brainstate/references/state_collections_and_utilities.md +235 -0
  90. package/skills/brainstate/scripts/integrator_rnn.py +359 -0
  91. package/skills/brainstate/scripts/lif_neuron_model.py +52 -0
  92. package/skills/brainstate/scripts/modern_cnn.py +73 -0
  93. package/skills/brainstate/scripts/resnet.py +130 -0
  94. package/skills/brainunit/SKILL.md +287 -0
  95. package/skills/brainunit/references/array-creation.md +158 -0
  96. package/skills/brainunit/references/array-mechanics/functional-structural-api.md +90 -0
  97. package/skills/brainunit/references/array-mechanics.md +165 -0
  98. package/skills/brainunit/references/math-function-library.md +433 -0
  99. package/skills/brainunit/references/physical-constant-library.md +58 -0
  100. package/skills/brainunit/references/prefix-library.md +116 -0
  101. package/skills/brainunit/references/quantity-inspection-and-conversion.md +104 -0
  102. package/skills/brainunit/references/typing-and-runtime-validation.md +203 -0
  103. package/skills/brainunit/references/unit-structure-and-definition.md +140 -0
  104. package/skills/brainx-general-guard/SKILL.md +153 -0
  105. package/skills/brainx-general-guard/references/brainstate-randomness-reproducibility/advanced-randomness.md +303 -0
  106. package/skills/brainx-general-guard/references/brainstate-randomness-reproducibility/randomness-and-reproducibility.md +62 -0
  107. package/skills/brainx-install/SKILL.md +2 -2
  108. package/skills/brainx-install/references/compatibility-and-release-matching.md +3 -3
  109. /package/{bin → installation/bin}/brainx.js +0 -0
  110. /package/{lib → installation/lib}/adapter-transaction.js +0 -0
  111. /package/{lib → installation/lib}/bundle.js +0 -0
  112. /package/{lib → installation/lib}/cli.js +0 -0
  113. /package/{lib → installation/lib}/constants.js +0 -0
  114. /package/{lib → installation/lib}/hash.js +0 -0
  115. /package/{lib → installation/lib}/paths.js +0 -0
  116. /package/{lib → installation/lib}/prompts.js +0 -0
  117. /package/{lib → installation/lib}/receipt.js +0 -0
@@ -0,0 +1,210 @@
1
+ # BrainCell custom ion channel authoring
2
+
3
+ Use this reference only after `references/ion-library.md` and `references/channel-library.md` confirm that no built-in or literature-derived mechanism fits. For a multicompartment task, establish the `Cell` workflow in `references/multicompartment/multicompartment-cell-workflow.md` before applying the authoring patterns here.
4
+
5
+ ## Mental model
6
+
7
+ An ion owns reversal-potential and concentration information; a channel owns gating State and computes current from voltage plus the `IonInfo` supplied by its declared `root_type`.
8
+
9
+ Keep custom dynamics inside BrainCell's lifecycle:
10
+
11
+ `declare dependency and parameters -> allocate/reset State -> compute derivatives -> compute current -> integrate through the cell -> validate against a reference`
12
+
13
+ Do not replace this lifecycle with detached ODE functions or raw JAX arrays.
14
+
15
+ ## Choose the extension level
16
+
17
+ | Need | Use | Required contract |
18
+ |---|---|---|
19
+ | Independent Hodgkin-Huxley gates | `braincell.channel._base.HH` | Declare `Gate` objects, implement one recognized kinetic form per gate, and implement `current()`. |
20
+ | Coupled probabilistic channel states | `braincell.channel._base.Markov` | Declare transitions, one dependent state, a conservation total, transition-rate methods, and current from open-state probability. |
21
+ | Dynamics not expressible by HH or Markov templates | `braincell.Channel` | Implement State initialization/reset, derivatives, lifecycle hooks when needed, and current directly. |
22
+ | Fixed custom ion species | `braincell.ion._base.FixedIon` or a matching concrete ion base | Define the species' fixed `Ci`, `Co`, `E`, and `valence`, then preserve the ion/channel ownership contract. |
23
+ | Fixed concentrations with Nernst initialization | `braincell.ion._base.InitNernstIon` | Define fixed concentrations, valence, and temperature behavior; let initialization/reset compute `E`. |
24
+ | Dynamic intracellular concentration | `braincell.ion._base.DynamicNernstIon` | Define `derivative(Ci, V, total_current=...)`, whether total owned current is required, and the concentration initialization rule. |
25
+ | Multicompartment string declaration | `register_channel` or `register_ion` | Register a unique name at import time before `mech.Channel(...)` or `mech.Ion(...)` resolves it. |
26
+
27
+ `braincell.channel._base` and `braincell.ion._base` are lower-level authoring modules. Inspect the installed version before relying on them across releases.
28
+
29
+ ## Define the channel contract
30
+
31
+ | API | Description |
32
+ |---|---|
33
+ | `root_type` | Set to the required ion class, ordered joint-ion dependency, or `HHTypedNeuron` for a root-cell channel; BrainCell uses it to validate attachment and supply the correct arguments. |
34
+ | `braincell.IonInfo` | Read `Ci`, `Co`, `E`, and `valence` from this value passed by the ion owner; do not reach into the parent ion object. |
35
+ | `init_state(V, *ion_info, batch_size=None)` | Allocate channel State for first use. |
36
+ | `reset_state(V, *ion_info, batch_size=None)` | Restore the channel's documented initial or steady-state condition without changing its declaration. |
37
+ | `compute_derivative(V, *ion_info)` | Write every integrable channel State derivative for the current voltage and ion information. |
38
+ | `current(V, *ion_info)` | Return the channel current with the same unit convention expected by the owning cell. |
39
+ | `pre_integral(V, *ion_info)` | Override only when work must occur before the solver advances State. |
40
+ | `post_integral(V, *ion_info)` | Override only when work must occur after the solver advances State. |
41
+
42
+ For a single-ion channel, the method argument order follows `root_type`. For a mixed-ion channel, it follows the ordered joint type exactly.
43
+
44
+ ## Author an HH-template channel
45
+
46
+ Use `HH` when each gate follows either an `inf/tau` or `alpha/beta` first-order equation.
47
+
48
+ | API | Description |
49
+ |---|---|
50
+ | `Gate(name, power=..., phi=...)` | Declare one gating State, its conductance exponent, and an optional direct temperature factor. |
51
+ | `f_<gate>_inf(...)` and `f_<gate>_tau(...)` | Implement the steady-state and time-constant form; `tau` returns a plain number whose natural unit is milliseconds. |
52
+ | `f_<gate>_alpha(...)` and `f_<gate>_beta(...)` | Implement the forward/backward-rate form; rates return plain numbers whose natural unit is inverse milliseconds. |
53
+ | `conductance_factor(V, *ion_info)` | Use inside `current()` to obtain the product of all declared gate values raised to their configured powers. |
54
+
55
+ ```python
56
+ import braincell
57
+ import brainunit as u
58
+ import jax.numpy as jnp
59
+ from braincell import IonInfo
60
+ from braincell.channel._base import Gate, HH
61
+ from braincell.mech import register_channel
62
+
63
+
64
+ @register_channel("DemoK")
65
+ class DemoKChannel(HH):
66
+ root_type = braincell.ion.Potassium
67
+ gates = (Gate("n", power=4, phi=2.0),)
68
+
69
+ def __init__(
70
+ self,
71
+ size,
72
+ g_max=1.0 * u.mS / u.cm**2,
73
+ ):
74
+ super().__init__(size=size)
75
+ self.g_max = g_max
76
+
77
+ def current(self, V, K: IonInfo):
78
+ return (
79
+ self.g_max
80
+ * self.conductance_factor(V, K)
81
+ * (K.E - V)
82
+ )
83
+
84
+ def f_n_inf(self, V, K: IonInfo):
85
+ _ = K
86
+ voltage_mV = V.to_decimal(u.mV)
87
+ return 1.0 / (
88
+ 1.0 + u.math.exp(-(voltage_mV + 35.0) / 10.0)
89
+ )
90
+
91
+ def f_n_tau(self, V, K: IonInfo):
92
+ _ = (V, K)
93
+ return 5.0
94
+
95
+
96
+ V = jnp.array([-55.0]) * u.mV
97
+ k_info = IonInfo(
98
+ Ci=jnp.array([54.4]) * u.mM,
99
+ Co=jnp.array([2.5]) * u.mM,
100
+ E=jnp.array([-90.0]) * u.mV,
101
+ valence=1,
102
+ )
103
+
104
+ channel = DemoKChannel(size=1)
105
+ channel.init_state(V, k_info)
106
+ channel.n.value = jnp.array([0.2])
107
+ channel.compute_derivative(V, k_info)
108
+ current = channel.current(V, k_info)
109
+
110
+ assert channel.n.derivative.shape == (1,)
111
+ assert current.shape == (1,)
112
+ channel.n.derivative.to_decimal(u.Hz)
113
+ current.to_decimal(u.mA / u.cm**2)
114
+ ```
115
+
116
+ For the `inf/tau` form, `HH` applies
117
+
118
+ `dx/dt = phi * (x_inf - x) / tau / ms`.
119
+
120
+ For the `alpha/beta` form, it applies
121
+
122
+ `dx/dt = phi * (alpha * (1 - x) - beta * x) / ms`.
123
+
124
+ Implement only one recognized form for each gate. Keep unit conversion at formula boundaries, such as `V.to_decimal(u.mV)`, and return the convention expected by the template.
125
+
126
+ ## Use Markov or the lower-level channel interface
127
+
128
+ Use `Markov` when channel-state probabilities are coupled through a transition graph rather than independent gates.
129
+
130
+ | API | Description |
131
+ |---|---|
132
+ | `Transition(src, dst, forward, backward)` | Declare one bidirectional edge and the names of its rate methods. |
133
+ | `Markov.pairs` | Declare the complete transition graph. |
134
+ | `Markov.dependent_state` | Name the probability reconstructed from the conservation law instead of integrated independently. |
135
+ | `Markov.conserve` | Set the conserved total, normally `1.0` for state probabilities. |
136
+
137
+ Use `braincell.Channel` directly only when neither HH gates nor a conserved Markov graph represent the equations. In that path, allocate every `DiffEqState`, define a reproducible reset rule, write every derivative in `compute_derivative()`, and preserve units in `current()`. Leave `pre_integral()` and `post_integral()` as inherited no-ops unless the equations require an ordering-specific operation.
138
+
139
+ Do not implement all Markov probabilities as independent States when they must sum to one. Choose one dependent state and test conservation after stepping.
140
+
141
+ ## Register and use the mechanism
142
+
143
+ Registration makes a custom class available to multicompartment declarations by name.
144
+
145
+ | API | Description |
146
+ |---|---|
147
+ | `braincell.mech.register_channel(name)` | Decorate a concrete channel class; registration occurs when its defining module is imported. |
148
+ | `braincell.mech.register_ion(name)` | Decorate a concrete ion class when a custom species or concentration model must resolve by name. |
149
+ | `braincell.mech.Channel(name, **parameters)` | Declare a registered channel on a multicompartment cell. |
150
+ | `braincell.mech.Ion(name, **parameters)` | Declare a registered ion on a multicompartment cell. |
151
+
152
+ The `DemoKChannel` example registers `"DemoK"` and can therefore be declared after its module has been imported:
153
+
154
+ ```python
155
+ import braincell.mech as mech
156
+
157
+
158
+ demo_k = mech.Channel(
159
+ "DemoK",
160
+ g_max=0.1 * u.mS / u.cm**2,
161
+ )
162
+ ```
163
+
164
+ Import registration modules before constructing a declaration. An unresolved string is a registry/import problem, not a reason to duplicate the mechanism under a built-in name.
165
+
166
+ ## Author a custom ion only when required
167
+
168
+ Use a custom ion when the missing behavior belongs to reversal potential or concentration dynamics, not when only the current equation is new.
169
+
170
+ 1. Choose `FixedIon`, `InitNernstIon`, or `DynamicNernstIon` from the concentration behavior.
171
+ 2. Define `Ci`, `Co`, `E`, `valence`, temperature, and initialization with BrainUnit quantities.
172
+ 3. For a dynamic ion, implement `derivative(Ci, V, total_current=...)` and declare whether the total current of owned channels is required.
173
+ 4. Package ion state through the inherited `IonInfo` path; do not pass the ion object directly into channels.
174
+ 5. Register the concrete class only when string-based `mech.Ion(...)` declarations need it.
175
+
176
+ Use `CalciumFixed`, `CalciumInitNernst`, and `CalciumDetailed` as the three concrete source patterns. Do not author a custom ion merely to change a channel's `g_max`, gating kinetics, or current formula.
177
+
178
+ ## Validate the extension
179
+
180
+ Validate the mechanism at three levels:
181
+
182
+ | Level | Required checks |
183
+ |---|---|
184
+ | Formula | Compare steady state, time constants or transition rates, current sign, reversal behavior, and units against the governing equations. |
185
+ | Lifecycle | Exercise `init_state()`, `reset_state()`, derivative computation, one solver step, batching, and the expected `root_type` failure. |
186
+ | Model | Compare a voltage-clamp curve, analytic limit, published trace, or trusted simulator under matched parameters, temperature, units, `dt`, and solver. |
187
+
188
+ For an ohmic current written as `g * gates * (E - V)`, verify that current approaches zero at `V == E`. For an HH channel, perturb each gate away from steady state and verify that the derivative points back toward its expected equilibrium. For a Markov channel, verify non-negative probabilities and conservation.
189
+
190
+ Add a co-located test when the extension becomes repository code. Registration success alone does not validate kinetics.
191
+
192
+ ## Common failures
193
+
194
+ - Authoring before checking the library: inspect built-in channel, ion, and template families first.
195
+ - Omitting or misordering `root_type`: declare the exact ion dependency and test wrong-owner rejection.
196
+ - Reaching into a parent ion object: consume the supplied `IonInfo`.
197
+ - Mixing kinetic forms for one HH gate: implement either `inf/tau` or `alpha/beta`.
198
+ - Returning unit-bearing HH time constants or rates when the template expects plain millisecond-based numbers: follow the installed template convention.
199
+ - Allocating Markov probabilities without conservation: define one dependent state and test the total.
200
+ - Returning raw floats from `current()`: preserve the cell's current or current-density units.
201
+ - Stripping units before the formula boundary: convert only the term that must enter a dimensionless empirical expression.
202
+ - Registering a class but never importing its module: ensure import-time registration occurs before string resolution.
203
+ - Validating only one trace at one `dt`: test formula invariants and numerical convergence separately.
204
+
205
+ ## Sources
206
+
207
+ - Ions & Channels: https://brainx.chaobrain.com/braincell/concepts/ions_channels.html
208
+ - Channels tutorial: https://brainx.chaobrain.com/braincell/tutorials/channel.html
209
+ - Ions tutorial: https://brainx.chaobrain.com/braincell/tutorials/ion.html
210
+ - Extending BrainCell: https://brainx.chaobrain.com/braincell/developer/extending.html
@@ -0,0 +1,170 @@
1
+ # BrainCell channel library
2
+
3
+ Use this reference when selecting a built-in membrane channel, matching it to its ion owner, or designing a controlled channel comparison. Author a custom channel only after the built-in library and literature-derived templates have been checked.
4
+
5
+ ## Mental model
6
+
7
+ A channel computes membrane current from voltage, its internal gating state, and the `IonInfo` supplied by its `root_type`.
8
+
9
+ Built-in names usually follow `<current>_<source><year>`, so a similar current name does not imply interchangeable kinetics. Match the requested model, paper, temperature convention, reversal-potential treatment, and ion dependency before tuning `g_max`.
10
+
11
+ ## Choose a channel family
12
+
13
+ ### Sodium channels
14
+
15
+ Use sodium channels for inward current that initiates or sustains depolarization.
16
+
17
+ | API | Description |
18
+ |---|---|
19
+ | `braincell.channel.Na_HH1952(size, ...)` | Use for the classical Hodgkin-Huxley 1952 sodium current. |
20
+ | `braincell.channel.Na_TM1991(size, ...)` | Use when reproducing the Traub-Miles 1991 sodium formulation. |
21
+ | `braincell.channel.Na_Ba2002(size, ...)` | Use for models based on the Bazhenov 2002 sodium current. |
22
+ | `braincell.channel.NaF_SU2015_DCN(size, ...)` | Use when the requested model requires the fast sodium current from the Sudhakar 2015 DCN template family. |
23
+ | `braincell.channel.NaP_SU2015_DCN(size, ...)` | Use when the requested model requires the persistent sodium current from the same template family. |
24
+
25
+ Attach these channels to a sodium ion. Do not select among them by maximal conductance alone; their gating equations and source-model conventions differ.
26
+
27
+ ### Potassium channels
28
+
29
+ Use potassium channels for repolarization, resting conductance, delayed rectification, transient A-current, M-current, or inward rectification.
30
+
31
+ | API | Description |
32
+ |---|---|
33
+ | `braincell.channel.K_HH1952(size, ...)` | Use for the classical Hodgkin-Huxley 1952 delayed-rectifier potassium current. |
34
+ | `braincell.channel.K_TM1991(size, ...)` | Use when reproducing the Traub-Miles 1991 potassium formulation. |
35
+ | `braincell.channel.K_Leak(size, ...)` | Use for an ion-specific potassium leak current owned by a potassium ion. |
36
+ | `braincell.channel.KDR_Ba2002(size, ...)` | Use for the Bazhenov 2002 delayed-rectifier potassium current. |
37
+ | `braincell.channel.KA1_HM1992(size, ...)` | Use for the Huguenard-McCormick 1992 `IA1` transient potassium current. |
38
+ | `braincell.channel.KA2_HM1992(size, ...)` | Use for the Huguenard-McCormick 1992 `IA2` transient potassium current. |
39
+
40
+ The API also exposes `KM_*`, `Kv*`, and `Kir*` template families. Use the exact literature-derived class when the user names a model or source paper; consult the installed API for the complete list.
41
+
42
+ ### Calcium channels
43
+
44
+ Use calcium channels for calcium entry, low- or high-threshold activation, rebound behavior, or calcium-dependent downstream mechanisms.
45
+
46
+ | API | Description |
47
+ |---|---|
48
+ | `braincell.channel.CaL_IS2008(size, ...)` | Use for the Inoue-Strowbridge 2008 L-type calcium current. |
49
+ | `braincell.channel.CaN_IS2008(size, ...)` | Use for the Inoue-Strowbridge 2008 calcium-activated non-selective cation current. |
50
+ | `braincell.channel.CaT_HM1992(size, ...)` | Use for the Huguenard-McCormick 1992 low-threshold T-type calcium current. |
51
+ | `braincell.channel.CaT_HP1992(size, ...)` | Use for the Huguenard-Prince 1992 T-type current for reticular-nucleus models. |
52
+ | `braincell.channel.CaHT_HM1992(size, ...)` | Use for the Huguenard-McCormick 1992 high-threshold calcium current. |
53
+
54
+ Attach calcium channels to a calcium ion. Use a dynamic calcium ion when accumulated calcium must affect reversal potential or calcium-dependent channels; otherwise a fixed calcium ion may be sufficient.
55
+
56
+ ### Mixed-ion, HCN, and leak channels
57
+
58
+ | API | Description |
59
+ |---|---|
60
+ | `braincell.channel.AHP_De1994(size, ...)` | Use for a calcium-activated potassium AHP current; attach it to `MixIons(k, ca)` in that order. |
61
+ | `braincell.channel.HCN_HM1992(size, E=..., ...)` | Use for the Huguenard-McCormick 1992 hyperpolarization-activated current and related sag or rebound behavior. |
62
+ | `braincell.channel.IL(size, E=..., g_max=...)` | Use for a root-cell leakage current with its own reversal potential; attach it directly to the cell. |
63
+ | `braincell.channel.LeakageChannel` | Use as the base class when implementing leakage-channel dynamics, not as the normal concrete leak mechanism. |
64
+
65
+ The `Kca*`, `HCN*`, and template-import families contain model-specific alternatives. Prefer the exact source match over a name that merely has the same broad current type.
66
+
67
+ ## Attach built-in channels
68
+
69
+ `root_type` determines which object supplies a channel's ion information and where the channel may be attached.
70
+
71
+ | API | Description |
72
+ |---|---|
73
+ | `channel.root_type` | Inspect before attachment; it identifies a sodium, potassium, calcium, ordered multi-ion, or root-cell dependency. |
74
+ | `ion.add(name=channel)` | Use for a single-ion channel in a `SingleCompartment`; it validates the channel against the ion owner. |
75
+ | `mixed.add(name=channel)` | Use for a channel whose `root_type` is an ordered multi-ion dependency. |
76
+ | `braincell.mech.Channel(name_or_class, **parameters)` | Use inside `Cell.paint()` for a multicompartment density-channel declaration; string names resolve through the channel registry. |
77
+
78
+ ```python
79
+ import braincell
80
+ import brainunit as u
81
+
82
+
83
+ class HH(braincell.SingleCompartment):
84
+ def __init__(self, size, solver="exp_euler"):
85
+ super().__init__(size, V_th=20.0 * u.mV, solver=solver)
86
+
87
+ self.na = braincell.ion.SodiumFixed(size, E=50.0 * u.mV)
88
+ self.na.add(INa=braincell.channel.Na_HH1952(size))
89
+
90
+ self.k = braincell.ion.PotassiumFixed(size, E=-77.0 * u.mV)
91
+ self.k.add(IK=braincell.channel.K_HH1952(size))
92
+
93
+ self.IL = braincell.channel.IL(
94
+ size,
95
+ E=-54.387 * u.mV,
96
+ g_max=0.03 * u.mS / u.cm**2,
97
+ )
98
+ ```
99
+
100
+ This pattern keeps ion-specific channels under their matching ion and the root-cell leak on the cell. Adding `K_HH1952` to `SodiumFixed`, for example, raises a type error instead of silently using the wrong reversal potential.
101
+
102
+ Open `references/ion-library.md` when choosing fixed, Nernst-initialized, or dynamic ion behavior. Open `references/mixions-for-adaptation.md` when a mixed potassium-calcium channel participates in adaptation or rebound.
103
+
104
+ ## Isolate a channel effect
105
+
106
+ Expose the target channel's parameter while holding the cell declaration, initial state, input, solver, `dt`, and recording path constant.
107
+
108
+ ```python
109
+ class AblationHH(braincell.SingleCompartment):
110
+ def __init__(
111
+ self,
112
+ size,
113
+ gK=36.0 * u.mS / u.cm**2,
114
+ solver="exp_euler",
115
+ ):
116
+ super().__init__(size, V_th=20.0 * u.mV, solver=solver)
117
+
118
+ self.na = braincell.ion.SodiumFixed(size, E=50.0 * u.mV)
119
+ self.na.add(INa=braincell.channel.Na_HH1952(size))
120
+
121
+ self.k = braincell.ion.PotassiumFixed(size, E=-77.0 * u.mV)
122
+ self.k.add(
123
+ IK=braincell.channel.K_HH1952(size, g_max=gK)
124
+ )
125
+
126
+ self.IL = braincell.channel.IL(
127
+ size,
128
+ E=-54.387 * u.mV,
129
+ g_max=0.03 * u.mS / u.cm**2,
130
+ )
131
+
132
+
133
+ intact = AblationHH(size=1)
134
+ ablated = AblationHH(
135
+ size=1,
136
+ gK=0.0 * u.mS / u.cm**2,
137
+ )
138
+ ```
139
+
140
+ The official potassium-ablation example uses `gK = 0` to remove delayed-rectifier current while preserving the rest of the model. Initialize both cells identically and drive them with the same current before attributing a trace difference to the ablated channel.
141
+
142
+ Do not interpret a waveform difference as a channel effect until it persists under an appropriate time-step refinement. Open `references/solver-library-with-effects.md` when the result may be solver-dependent.
143
+
144
+ ## Route custom authoring
145
+
146
+ Open `references/braincell-custom-ion-channel-authoring.md` only when:
147
+
148
+ - no built-in or literature-derived class implements the required kinetics;
149
+ - the requested ion dependency can be stated as a correct `root_type`;
150
+ - the governing equations, units, initialization rule, and validation target are known.
151
+
152
+ For a multicompartment task, establish the `Morphology -> Cell -> paint/place -> init_state` workflow in `references/multicompartment/multicompartment-cell-workflow.md` before reusing the custom-authoring route.
153
+
154
+ ## Common failures
155
+
156
+ - Writing a custom channel first: search `braincell.channel` and the source-model templates before authoring.
157
+ - Attaching a channel to the wrong owner: inspect `root_type`; use the matching ion, ordered `MixIons`, or root cell.
158
+ - Treating `IL` and `K_Leak` as interchangeable: `IL` is a root-cell leak with its own `E`; `K_Leak` is potassium-specific.
159
+ - Comparing source models by `g_max` alone: preserve their gating, temperature, shift, and reversal-potential conventions.
160
+ - Using a fixed calcium ion in a calcium-feedback experiment: select dynamic calcium before adding the dependent current.
161
+ - Removing a channel while changing input or numerics: hold all non-ablated conditions constant.
162
+ - Passing a bare maximal conductance: use the density or total-conductance unit expected by the surrounding cell convention.
163
+
164
+ ## Sources
165
+
166
+ - Ions & Channels: https://brainx.chaobrain.com/braincell/concepts/ions_channels.html
167
+ - Channels tutorial: https://brainx.chaobrain.com/braincell/tutorials/channel.html
168
+ - `braincell.channel` API: https://brainx.chaobrain.com/braincell/apis/braincell.channel.html
169
+ - Channel ablation: https://brainx.chaobrain.com/braincell/examples/channel_ablation.html
170
+ - Spike-frequency adaptation: https://brainx.chaobrain.com/braincell/examples/spike_frequency_adaptation.html
@@ -0,0 +1,154 @@
1
+ # BrainCell ion library
2
+
3
+ Use this reference when choosing a fixed, Nernst-initialized, or dynamic BrainCell ion, or when a channel depends on more than one ion species. Use the built-in ion classes before authoring a custom ion.
4
+
5
+ ## Mental model
6
+
7
+ An `Ion` owns one species' `Ci`, `Co`, `E`, and `valence`, packages them as `IonInfo`, and supplies that information to the channels whose currents it aggregates.
8
+
9
+ - A fixed ion keeps its concentrations and reversal potential constant.
10
+ - An initialized-Nernst ion keeps its concentrations fixed but computes `E` from them during initialization and reset.
11
+ - A dynamic-Nernst ion stores `Ci` as `DiffEqState`, derives `E` from the current `Ci`, and lets a concrete ion define `dCi/dt`.
12
+
13
+ Channels carry current; ions organize the shared electrochemical state that drives those currents. Do not put a shared reversal potential independently on every ion-specific channel.
14
+
15
+ ## Choose an ion
16
+
17
+ | API | Use when | Important behavior |
18
+ |---|---|---|
19
+ | `braincell.ion.SodiumFixed(size, E=...)` | Use for classical sodium currents with a prescribed reversal potential. | It keeps sodium electrochemical values fixed and accepts channels whose `root_type` is `Sodium`. |
20
+ | `braincell.ion.SodiumInitNernst(size, ...)` | Use when fixed sodium concentrations should determine the initial reversal potential. | It recomputes `E` from the Nernst equation during initialization and reset; it does not integrate concentration. |
21
+ | `braincell.ion.PotassiumFixed(size, E=...)` | Use for classical potassium currents with a prescribed reversal potential. | It keeps potassium electrochemical values fixed and accepts channels whose `root_type` is `Potassium`. |
22
+ | `braincell.ion.PotassiumInitNernst(size, ...)` | Use when fixed potassium concentrations should determine the initial reversal potential. | It recomputes `E` from the Nernst equation during initialization and reset; it does not integrate concentration. |
23
+ | `braincell.ion.CalciumFixed(size, E=..., ...)` | Use when calcium current matters but intracellular calcium feedback does not. | It keeps calcium concentration and reversal potential fixed. |
24
+ | `braincell.ion.CalciumInitNernst(size, ...)` | Use when fixed calcium concentrations should initialize `E`. | It derives `E` from fixed `Ci`, `Co`, temperature, and valence during initialization and reset. |
25
+ | `braincell.ion.CalciumDetailed(size, C_rest=..., tau=..., d=...)` | Use when calcium influx must change intracellular calcium and its Nernst reversal potential. | It integrates `Ci`, receives total current from its calcium channels, and derives `E` live from the current concentration. |
26
+ | `braincell.ion.CalciumFirstOrder(size, ...)` | Use when simplified first-order calcium concentration dynamics are sufficient. | It replaces the detailed calcium-removal model with a lower-complexity concentration ODE. |
27
+
28
+ The ion API also exposes literature-derived calcium-dynamics templates such as `CdpHVA_SU2015_DCN`, `CdpLVA_SU2015_DCN`, and `CdpStC_MA2020_GoC`. Select one only when the requested model or source paper matches it; use the official `braincell.ion` API for the complete installed list.
29
+
30
+ ## Attach fixed ions and channels
31
+
32
+ Construct the ion first, then add only channels whose `root_type` matches that ion.
33
+
34
+ | API | Description |
35
+ |---|---|
36
+ | `ion.add(name=channel)` | Use after constructing a single-compartment ion; it registers the channel under that ion and raises a type error when the channel's `root_type` does not match. |
37
+ | `ion.pack_info()` | Use when testing a channel directly; it returns the ion's `IonInfo(Ci, Co, E, valence)`. |
38
+ | `channel.root_type` | Inspect before attachment when the required ion owner is uncertain. |
39
+
40
+ ```python
41
+ import braincell
42
+ import brainunit as u
43
+
44
+
45
+ na = braincell.ion.SodiumFixed(size=1, E=50.0 * u.mV)
46
+ na.add(INa=braincell.channel.Na_HH1952(size=1))
47
+
48
+ k = braincell.ion.PotassiumFixed(size=1, E=-77.0 * u.mV)
49
+ k.add(IK=braincell.channel.K_HH1952(size=1))
50
+
51
+ assert braincell.channel.Na_HH1952.root_type is braincell.ion.Sodium
52
+ assert braincell.channel.K_HH1952.root_type is braincell.ion.Potassium
53
+ ```
54
+
55
+ Attach root-cell channels such as `IL` directly to the cell. They do not belong under a sodium, potassium, or calcium ion.
56
+
57
+ ## Use dynamic calcium
58
+
59
+ `CalciumDetailed` couples the summed calcium current to intracellular concentration, then feeds the resulting Nernst potential back to calcium channels.
60
+
61
+ | API | Description |
62
+ |---|---|
63
+ | `braincell.ion.CalciumDetailed(...)` | Use for calcium-dependent adaptation, rebound, signaling, or another workflow in which `Ci` changes during integration. |
64
+ | `ca.Ci.value` | Read the current intracellular concentration after initialization; it is the value carried by the ion's `DiffEqState`. |
65
+ | `ca.E` | Read the current Nernst reversal potential; it is derived from the current `Ci`, not a fixed cache. |
66
+
67
+ ```python
68
+ ca = braincell.ion.CalciumDetailed(
69
+ size=1,
70
+ C_rest=5.0e-5 * u.mM,
71
+ tau=10.0 * u.ms,
72
+ d=0.5 * u.um,
73
+ )
74
+ ca.add(
75
+ ICaT=braincell.channel.CaT_HM1992(
76
+ size=1,
77
+ g_max=2.1 * u.mS / u.cm**2,
78
+ )
79
+ )
80
+ ```
81
+
82
+ The concentration ODE receives the total current from all channels owned by this calcium ion. Do not update `Ci` manually inside the cell loop or compute a second independent reversal potential in the channel.
83
+
84
+ Open `references/mixions-for-adaptation.md` when dynamic calcium participates in an AHP/KCa adaptation or rebound workflow.
85
+
86
+ ## Combine ion dependencies
87
+
88
+ Use `MixIons` only when one channel's `root_type` requires several ion species.
89
+
90
+ | API | Description |
91
+ |---|---|
92
+ | `braincell.MixIons(*ions)` | Use to create a multi-ion owner in the exact order declared by the target channel's `root_type`. |
93
+ | `braincell.mix_ions(*ions)` | Use as the convenience function for the same combination. |
94
+ | `mixed.add(name=channel)` | Use to attach a channel after the ordered ion tuple has been constructed. |
95
+
96
+ ```python
97
+ kca = braincell.MixIons(k, ca)
98
+ kca.add(
99
+ IAHP=braincell.channel.AHP_De1994(
100
+ size=1,
101
+ g_max=0.3 * u.mS / u.cm**2,
102
+ )
103
+ )
104
+ ```
105
+
106
+ For `AHP_De1994`, the required order is potassium followed by calcium. Match `root_type` exactly; `(ca, k)` is not interchangeable with `(k, ca)`.
107
+
108
+ ## Declare ions on a multicompartment cell
109
+
110
+ `braincell.mech.Ion` and `braincell.mech.Channel` are declarations; `Cell.init_state()` lowers them into runtime ion and channel objects over the selected control volumes.
111
+
112
+ ```python
113
+ import braincell.mech as mech
114
+
115
+
116
+ cell.paint(
117
+ region,
118
+ mech.Ion(
119
+ "CalciumDetailed",
120
+ name="ca_dyn",
121
+ d=0.5 * u.um,
122
+ tau=10.0 * u.ms,
123
+ C_rest=5.0e-5 * u.mM,
124
+ Ci_initializer=2.4e-4 * u.mM,
125
+ ),
126
+ )
127
+ cell.paint(
128
+ region,
129
+ mech.Channel(
130
+ "CaT_HM1992",
131
+ ion_name="ca_dyn",
132
+ g_max=2.0 * u.mS / u.cm**2,
133
+ ),
134
+ )
135
+ ```
136
+
137
+ Use an explicit ion `name` when a channel must bind to a particular runtime ion owner. Open `references/multicompartment/multicompartment-cell-workflow.md` before applying this fragment to a `Cell`; that workflow owns morphology, regions, CV policies, initialization, and runtime inspection.
138
+
139
+ ## Common failures
140
+
141
+ - Adding a channel to the wrong ion: inspect `root_type`, then attach it to the matching ion or ordered `MixIons`.
142
+ - Treating an ion as the current-producing mechanism: attach channels; the ion aggregates their current and owns shared electrochemical information.
143
+ - Using fixed calcium when the result depends on calcium accumulation: use `CalciumDetailed`, `CalciumFirstOrder`, or a matching documented template.
144
+ - Expecting `InitNernstIon` concentration to evolve: choose a dynamic ion when `Ci` must be integrated.
145
+ - Passing bare concentrations, voltages, time constants, temperature, or shell depth: use BrainUnit quantities.
146
+ - Authoring a new ion before checking `braincell.ion`: inspect the installed API and literature-derived templates first.
147
+
148
+ Open `references/braincell-custom-ion-channel-authoring.md` only when the required ion species or concentration dynamics remain unavailable after that inspection.
149
+
150
+ ## Sources
151
+
152
+ - Ions & Channels: https://brainx.chaobrain.com/braincell/concepts/ions_channels.html
153
+ - Ions tutorial: https://brainx.chaobrain.com/braincell/tutorials/ion.html
154
+ - `braincell.ion` API: https://brainx.chaobrain.com/braincell/apis/braincell.ion.html
@@ -0,0 +1,81 @@
1
+ # MixIons for adaptation
2
+
3
+ Use this reference when adding a calcium-activated potassium current, calcium-dependent afterhyperpolarization, spike-frequency adaptation, or a thalamic/rebound variant that needs dynamic calcium plus a K/Ca mixed current.
4
+
5
+ In single-compartment models, use the class pattern below directly. In multicompartment `Cell` work, use this reference for ion/channel composition choices, but keep morphology, targeting, `paint`, `place`, CV policy, clamps, and probes in `references/multicompartment/multicompartment-cell-workflow.md`.
6
+
7
+ ## Rule
8
+
9
+ Use `braincell.MixIons(k, ca)` only when a channel depends on multiple ions. For calcium-activated potassium currents, potassium supplies the reversal potential and calcium supplies intracellular concentration dynamics. Keep ion order consistent with the channel's `root_type`.
10
+
11
+ ## Pattern
12
+
13
+ ```python
14
+ import braincell
15
+ import braintools
16
+ import brainunit as u
17
+
18
+
19
+ class AdaptingCell(braincell.SingleCompartment):
20
+ def __init__(self, size, solver="ind_exp_euler"):
21
+ super().__init__(
22
+ size,
23
+ V_initializer=braintools.init.Constant(-65. * u.mV),
24
+ V_th=20. * u.mV,
25
+ solver=solver,
26
+ )
27
+
28
+ self.na = braincell.ion.SodiumFixed(size, E=50. * u.mV)
29
+ self.na.add(INa=braincell.channel.Na_Ba2002(size, V_sh=-30. * u.mV))
30
+
31
+ self.k = braincell.ion.PotassiumFixed(size, E=-90. * u.mV)
32
+ self.k.add(IKL=braincell.channel.K_Leak(size, g_max=0.01 * (u.mS / u.cm ** 2)))
33
+ self.k.add(
34
+ IDR=braincell.channel.KDR_Ba2002(
35
+ size,
36
+ V_sh=-30. * u.mV,
37
+ q10=2.0,
38
+ temp=u.celsius2kelvin(16.),
39
+ )
40
+ )
41
+
42
+ self.ca = braincell.ion.CalciumDetailed(
43
+ size,
44
+ C_rest=5e-5 * u.mM,
45
+ tau=10. * u.ms,
46
+ d=0.5 * u.um,
47
+ )
48
+ self.ca.add(ICaL=braincell.channel.CaL_IS2008(size, g_max=0.5 * (u.mS / u.cm ** 2)))
49
+ self.ca.add(ICaN=braincell.channel.CaN_IS2008(size, g_max=0.5 * (u.mS / u.cm ** 2)))
50
+ self.ca.add(ICaT=braincell.channel.CaT_HM1992(size, g_max=2.1 * (u.mS / u.cm ** 2)))
51
+ self.ca.add(ICaHT=braincell.channel.CaHT_HM1992(size, g_max=3.0 * (u.mS / u.cm ** 2)))
52
+
53
+ self.kca = braincell.MixIons(self.k, self.ca)
54
+ self.kca.add(IAHP=braincell.channel.AHP_De1994(size, g_max=0.3 * (u.mS / u.cm ** 2)))
55
+
56
+ self.IL = braincell.channel.IL(
57
+ size,
58
+ E=-70. * u.mV,
59
+ g_max=0.0075 * (u.mS / u.cm ** 2),
60
+ )
61
+ ```
62
+
63
+ ## Rebound Input Variant
64
+
65
+ For post-inhibitory rebound, add or retain a low-threshold T-type calcium current such as `CaT_HM1992` and drive a time-dependent hyperpolarizing input.
66
+
67
+ ```python
68
+ def I_of_t(t):
69
+ return u.math.where(
70
+ t < 200. * u.ms,
71
+ -2. * u.uA / u.cm ** 2,
72
+ 0. * u.uA / u.cm ** 2,
73
+ )
74
+ ```
75
+
76
+ ## Checks
77
+
78
+ - Add AHP/KCa channels to the `MixIons` owner, not directly to `self.k` or `self.ca`.
79
+ - Use dynamic calcium such as `CalciumDetailed` when intracellular calcium should feed back into adaptation.
80
+ - Preserve a potassium ion container even when the adaptation current is calcium-dependent; the K reversal potential still matters.
81
+ - Inspect `channel.root_type` before changing ion order.