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.
- package/README.md +9 -2
- package/adapters/gemini.js +7 -0
- package/adapters/opencode.js +7 -0
- package/adapters/windsurf.js +7 -0
- package/installation/lib/installer.js +4 -1
- package/manifest.json +5 -1
- package/package.json +7 -2
- package/skills/braincell/SKILL.md +182 -0
- package/skills/braincell/references/area-scaled-hh-pattern.md +54 -0
- package/skills/braincell/references/braincell-custom-ion-channel-authoring.md +210 -0
- package/skills/braincell/references/channel-library.md +170 -0
- package/skills/braincell/references/ion-library.md +154 -0
- package/skills/braincell/references/mixions-for-adaptation.md +81 -0
- package/skills/braincell/references/multicompartment/braincell-manual-morphology-construction.md +180 -0
- package/skills/braincell/references/multicompartment/cv-policy-reference.md +133 -0
- package/skills/braincell/references/multicompartment/filter-function-library.md +162 -0
- package/skills/braincell/references/multicompartment/morphology-io-loading-validation.md +135 -0
- package/skills/braincell/references/multicompartment/multicompartment-cell-workflow.md +391 -0
- package/skills/braincell/references/multicompartment/probe-reference.md +128 -0
- package/skills/braincell/references/multicompartment/topology-building-and-visualization.md +121 -0
- package/skills/braincell/references/scripts/calcium_channel_gating.py +66 -0
- package/skills/braincell/references/scripts/cell_multicompartment_reference.py +221 -0
- package/skills/braincell/references/scripts/channel_ablation.py +61 -0
- package/skills/braincell/references/scripts/fi_curve.py +68 -0
- package/skills/braincell/references/scripts/hh_neuron_basics.py +59 -0
- package/skills/braincell/references/scripts/spike_frequency_adaptation.py +75 -0
- package/skills/braincell/references/scripts/t_current_rebound.py +72 -0
- package/skills/braincell/references/scripts/thalamic_neurons.py +228 -0
- package/skills/braincell/references/solver-library-with-effects.md +188 -0
- package/skills/brainevent/SKILL.md +268 -0
- package/skills/brainevent/references/connectivity-variants.md +312 -0
- package/skills/brainevent/references/custom-operators-cpu.md +304 -0
- package/skills/brainevent/references/custom-operators-gpu.md +468 -0
- package/skills/brainevent/references/scripts/102_EI_net_1996.py +107 -0
- package/skills/brainevent/references/scripts/204_joglekar_2018_propagation.py +168 -0
- package/skills/brainevent/references/scripts/coba_ei_teaching.py +356 -0
- package/skills/brainevent/references/sparse-formats.md +182 -0
- package/skills/brainevent/references/synaptic-plasticity.md +285 -0
- package/skills/brainpy-state/SKILL.md +232 -0
- package/skills/brainpy-state/references/array-creation.md +158 -0
- package/skills/brainpy-state/references/brain-dynamics-delay-protocol.md +96 -0
- package/skills/brainpy-state/references/braintools/brainstate-control-flow-patterns.md +334 -0
- package/skills/brainpy-state/references/braintools/data-preprocessing.md +79 -0
- package/skills/brainpy-state/references/braintools/metric.md +152 -0
- package/skills/brainpy-state/references/braintools/optimizer.md +179 -0
- package/skills/brainpy-state/references/braintools/parameter-initializer.md +183 -0
- package/skills/brainpy-state/references/braintools/surrogate.md +147 -0
- package/skills/brainpy-state/references/component-selection.md +273 -0
- package/skills/brainpy-state/references/nest-compatible/devices.md +157 -0
- package/skills/brainpy-state/references/nest-compatible/divergence-and-parity.md +114 -0
- package/skills/brainpy-state/references/nest-compatible/integration-categories.md +41 -0
- package/skills/brainpy-state/references/nest-compatible/model-library.md +200 -0
- package/skills/brainpy-state/references/nest-compatible/nest-workflow.md +156 -0
- package/skills/brainpy-state/references/nest-compatible/network-building.md +264 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/brette_et_al_2007.py +320 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/brunel_alpha.py +108 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/brunel_delta.py +93 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/clopath_synapse_spike_pairing.py +171 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/evaluate_tsodyks2_synapse.py +147 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/spatial_gaussex.py +112 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/synapsecollection.py +198 -0
- package/skills/brainpy-state/references/nest-compatible/synapse-and-connectivity.md +153 -0
- package/skills/brainpy-state/references/projection-patterns.md +125 -0
- package/skills/brainpy-state/references/scripts/103_COBA_2005.py +86 -0
- package/skills/brainpy-state/references/scripts/106_COBA_HH_2007.py +181 -0
- package/skills/brainpy-state/references/scripts/107_gamma_oscillation_1996.py +156 -0
- package/skills/brainpy-state/references/scripts/109_fast_global_oscillation.py +111 -0
- package/skills/brainpy-state/references/scripts/201_surrogate_grad_lif_fashion_mnist.py +221 -0
- package/skills/brainpy-state/references/scripts/training-snn.py +119 -0
- package/skills/brainstate/SKILL.md +1 -1
- package/skills/brainstate/references/brainstate/brainstate-control-flow-patterns.md +250 -399
- package/skills/brainstate/references/brainstate/brainstate-transformed-diagnostics.md +6 -2
- package/skills/brainstate/references/brainstate/parameter-constraints-regularization.md +16 -2
- package/skills/brainstate/references/brainstate/parameter-transforms-regularizers-catalog.md +8 -3
- package/skills/brainstate/references/brainstate/randomness-and-reproducibility.md +64 -143
- package/skills/brainstate/references/brainstate/transformation-grad-expansion.md +13 -1
- package/skills/brainstate/references/brainstate/transformation-jit-expansion.md +11 -1
- package/skills/brainstate/references/brainstate/transformation-vmap-expansion.md +35 -7
- package/skills/brainstate/references/collective_model_operations.md +9 -9
- package/skills/brainstate/references/extension_mechanisms.md +12 -1
- package/skills/brainstate/references/libraries/prebuilt-activation-library.md +106 -97
- package/skills/brainstate/references/libraries/prebuilt-layer-library.md +102 -176
- package/skills/brainstate/references/model-interop-and-migration.md +11 -1
- package/skills/brainstate/references/size-inference-variations.md +11 -0
- package/skills/brainstate/references/state-graph-operations.md +13 -15
- package/skills/brainstate/references/state_collections_and_utilities.md +12 -1
- package/skills/brainx-general-guard/SKILL.md +153 -0
- package/skills/brainx-general-guard/references/brainstate-randomness-reproducibility/advanced-randomness.md +303 -0
- package/skills/brainx-general-guard/references/brainstate-randomness-reproducibility/randomness-and-reproducibility.md +62 -0
- package/skills/brainstate/references/braintools-optimizer-reference.md +0 -133
- package/skills/brainstate/references/libraries/solver-library-with-effects.md +0 -85
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Reference script mirrored from:
|
|
3
|
+
https://brainx.chaobrain.com/braincell/examples/calcium_channel_gating.html
|
|
4
|
+
|
|
5
|
+
Purpose:
|
|
6
|
+
Channel gating diagnostic script. Use when the task asks for voltage-dependent activation/inactivation curves, low-threshold vs high-threshold calcium channel comparison, steady-state gating, or direct channel methods such as `f_p_inf` / `f_q_inf`.
|
|
7
|
+
|
|
8
|
+
Use this when:
|
|
9
|
+
- The task asks for calcium channel gating curves or steady-state activation/inactivation.
|
|
10
|
+
- The agent needs direct channel-method inspection rather than membrane-potential simulation.
|
|
11
|
+
|
|
12
|
+
Do not use this when:
|
|
13
|
+
- The task needs the default current-clamp simulation pattern for membrane traces.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
import braintools
|
|
17
|
+
import brainunit as u
|
|
18
|
+
import matplotlib.pyplot as plt
|
|
19
|
+
import braincell
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# Channel-level diagnostic: instantiate channel classes directly, no cell update loop.
|
|
23
|
+
# Create Low-Threshold T-Type Calcium Channel (ICaT)
|
|
24
|
+
cat = braincell.channel.CaT_HP1992(1)
|
|
25
|
+
|
|
26
|
+
# Create High-Threshold Calcium Channel (ICaHT)
|
|
27
|
+
caht = braincell.channel.CaHT_HM1992(1)
|
|
28
|
+
|
|
29
|
+
# Generate Voltage Sequence
|
|
30
|
+
vs = u.math.arange(-100 * u.mV, 0 * u.mV, 0.1 * u.mV)
|
|
31
|
+
|
|
32
|
+
# These channels' steady-state gates depend only on voltage, but the API
|
|
33
|
+
# requires a calcium IonInfo argument; supply a resting calcium state.
|
|
34
|
+
ca = braincell.IonInfo(Ci=5e-5 * u.mM, Co=2. * u.mM, E=120. * u.mV, valence=2)
|
|
35
|
+
|
|
36
|
+
# Create Figure and Subplot Layout
|
|
37
|
+
fig, gs = braintools.visualize.get_figure(1, 2, 3., 4.5)
|
|
38
|
+
|
|
39
|
+
# Compute steady-state values of activation and inactivation gates for low-threshold channel
|
|
40
|
+
q_inf = cat.f_q_inf(vs, ca)
|
|
41
|
+
p_inf = cat.f_p_inf(vs, ca)
|
|
42
|
+
|
|
43
|
+
# Add subplot 1
|
|
44
|
+
fig.add_subplot(gs[0, 0])
|
|
45
|
+
plt.plot(vs / u.mV, q_inf, label='q_inf (activation gate)') # Convert x-axis to mV for readability
|
|
46
|
+
plt.plot(vs / u.mV, p_inf, label='p_inf (inactivation gate)')
|
|
47
|
+
plt.legend() # Show legend
|
|
48
|
+
plt.fill_between([-80, -60], 1., alpha=0.2) # Highlight typical activation range for low-threshold channel
|
|
49
|
+
plt.title('Low-Threshold Calcium Channel (ICaT)')
|
|
50
|
+
plt.xlabel('Membrane Potential (mV)')
|
|
51
|
+
|
|
52
|
+
# Compute q_inf and p_inf for high-threshold channel
|
|
53
|
+
q_inf = caht.f_q_inf(vs, ca)
|
|
54
|
+
p_inf = caht.f_p_inf(vs, ca)
|
|
55
|
+
|
|
56
|
+
# Add subplot 2
|
|
57
|
+
fig.add_subplot(gs[0, 1])
|
|
58
|
+
plt.plot(vs / u.mV, q_inf, label='q_inf (activation gate)')
|
|
59
|
+
plt.plot(vs / u.mV, p_inf, label='p_inf (inactivation gate)')
|
|
60
|
+
plt.fill_between([-60, -40], 1., alpha=0.2) # Highlight typical activation range for high-threshold channel
|
|
61
|
+
plt.legend()
|
|
62
|
+
plt.xlabel('Membrane Potential (mV)')
|
|
63
|
+
plt.title('High-Threshold Calcium Channel (ICaHT)')
|
|
64
|
+
|
|
65
|
+
# Display figure
|
|
66
|
+
plt.show()
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This script is mirrored and reorganized from:
|
|
3
|
+
https://brainx.chaobrain.com/braincell/tutorials/cell.html
|
|
4
|
+
|
|
5
|
+
It is the primary full-script reference for BrainCell multicompartment modeling.
|
|
6
|
+
All BrainCell reference scripts live in this directory.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
# 1. Imports and CPU/JAX setup.
|
|
10
|
+
# Set the platform before importing JAX-backed BrainCell/BrainState packages.
|
|
11
|
+
import os
|
|
12
|
+
|
|
13
|
+
os.environ.setdefault("JAX_PLATFORMS", "cpu")
|
|
14
|
+
|
|
15
|
+
import braincell
|
|
16
|
+
import brainstate
|
|
17
|
+
import brainunit as u
|
|
18
|
+
import matplotlib.pyplot as plt
|
|
19
|
+
|
|
20
|
+
from braincell import Cell, Morphology
|
|
21
|
+
from braincell import CVPerBranch, MaxCVLen
|
|
22
|
+
from braincell.filter import BranchSlice, RootLocation, at
|
|
23
|
+
from braincell.mech import CableProperty, Channel, CurrentClamp, StateProbe
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
# 2. Load morphology from SWC.
|
|
27
|
+
# Canonical tutorial path. Adjust this path only if your repository layout differs.
|
|
28
|
+
MORPHOLOGY_PATH = "../../data/morphology/example_tree.swc"
|
|
29
|
+
morpho = Morphology.from_swc(MORPHOLOGY_PATH)
|
|
30
|
+
|
|
31
|
+
print("Morphology topology:")
|
|
32
|
+
print(morpho.topo())
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# 3. Build Cell(morpho) and inspect Cell summary fields.
|
|
36
|
+
cell = Cell(morpho)
|
|
37
|
+
print("\nDefault Cell summary:")
|
|
38
|
+
print(cell)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
# 4. Inspect cell.cvs.
|
|
42
|
+
# A CV is the discretized compartment interval that BrainCell solves over.
|
|
43
|
+
print("\nCV collection:")
|
|
44
|
+
print("n_cv:", cell.n_cv)
|
|
45
|
+
print("len(cell.cvs):", len(cell.cvs))
|
|
46
|
+
print("first three CV intervals:")
|
|
47
|
+
for cv in cell.cvs[:3]:
|
|
48
|
+
print(
|
|
49
|
+
f" CV {cv.id}: branch_id={cv.branch_id}, "
|
|
50
|
+
f"branch_type={cv.branch_type}, prox={cv.prox}, dist={cv.dist}"
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
# 5. Print important CV attributes.
|
|
55
|
+
# CVs carry topology, geometry, cable properties, and mechanism containers.
|
|
56
|
+
cv0 = cell.cvs[0]
|
|
57
|
+
print("\nFirst CV detail:")
|
|
58
|
+
print(cv0)
|
|
59
|
+
|
|
60
|
+
print("id:", cv0.id)
|
|
61
|
+
print("branch_id:", cv0.branch_id)
|
|
62
|
+
print("branch_type:", cv0.branch_type)
|
|
63
|
+
print("parent_cv:", cv0.parent_cv)
|
|
64
|
+
print("children_cv:", cv0.children_cv)
|
|
65
|
+
|
|
66
|
+
print("length:", cv0.length)
|
|
67
|
+
print("area:", cv0.area)
|
|
68
|
+
print("r_axial:", cv0.r_axial)
|
|
69
|
+
|
|
70
|
+
print("cm:", cv0.cm)
|
|
71
|
+
print("ra:", cv0.ra)
|
|
72
|
+
print("v:", cv0.v)
|
|
73
|
+
print("temp:", cv0.temp)
|
|
74
|
+
|
|
75
|
+
print("density_mech:", cv0.density_mech)
|
|
76
|
+
print("point_mech:", cv0.point_mech)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
# 6. Compare CV policies.
|
|
80
|
+
default_cell = Cell(morpho)
|
|
81
|
+
split_cell = Cell(morpho, cv_policy=CVPerBranch(cv_per_branch=2))
|
|
82
|
+
auto_cell = Cell(morpho, cv_policy=MaxCVLen(max_cv_len=20.0 * u.um))
|
|
83
|
+
|
|
84
|
+
print("\nCV policy comparison:")
|
|
85
|
+
print("default policy")
|
|
86
|
+
print(default_cell)
|
|
87
|
+
print()
|
|
88
|
+
print("CVPerBranch(cv_per_branch=2)")
|
|
89
|
+
print(split_cell)
|
|
90
|
+
print()
|
|
91
|
+
print("MaxCVLen(max_cv_len=20 um)")
|
|
92
|
+
print(auto_cell)
|
|
93
|
+
print()
|
|
94
|
+
print("first four CV intervals under CVPerBranch(cv_per_branch=2):")
|
|
95
|
+
for cv in split_cell.cvs[:4]:
|
|
96
|
+
print(f" CV {cv.id}: branch_id={cv.branch_id}, prox={cv.prox}, dist={cv.dist}")
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
# 7. Initialize state and inspect the node tree.
|
|
100
|
+
# init_state() builds runtime structures such as node_tree from the CV layout.
|
|
101
|
+
split_cell.init_state()
|
|
102
|
+
node_tree = split_cell.node_tree
|
|
103
|
+
print("\nNode tree after init_state():")
|
|
104
|
+
print(node_tree)
|
|
105
|
+
print("n_nodes:", len(node_tree.nodes))
|
|
106
|
+
print("n_edges:", len(node_tree.edges))
|
|
107
|
+
print(
|
|
108
|
+
"expected n_nodes = n_cv + n_branches + 1:",
|
|
109
|
+
split_cell.n_cv + len(split_cell.morpho.branches) + 1,
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
# 8. paint(...): cable properties.
|
|
114
|
+
# paint(region, CableProperty(...)) assigns passive cable values over CV midpoints.
|
|
115
|
+
cable_cell = Cell(morpho, cv_policy=CVPerBranch(cv_per_branch=2))
|
|
116
|
+
cable_cell.paint(
|
|
117
|
+
BranchSlice(branch_index=0, prox=0.0, dist=1.0),
|
|
118
|
+
CableProperty(
|
|
119
|
+
resting_potential=-70.0 * u.mV,
|
|
120
|
+
membrane_capacitance=2.0 * (u.uF / u.cm ** 2),
|
|
121
|
+
axial_resistivity=200.0 * (u.ohm * u.cm),
|
|
122
|
+
temperature=u.celsius2kelvin(20.0),
|
|
123
|
+
),
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
print("\nCable-property paint result:")
|
|
127
|
+
print(cable_cell)
|
|
128
|
+
print("painted soma CV example:")
|
|
129
|
+
print("v:", cable_cell.cvs[0].v)
|
|
130
|
+
print("cm:", cable_cell.cvs[0].cm)
|
|
131
|
+
print("ra:", cable_cell.cvs[0].ra)
|
|
132
|
+
print("temp:", cable_cell.cvs[0].temp)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
# 9. paint(...): density mechanisms.
|
|
136
|
+
# Channel("IL", ...) is declarative; init_state() lowers it into runtime layouts.
|
|
137
|
+
channel_cell = Cell(morpho)
|
|
138
|
+
channel_cell.paint(
|
|
139
|
+
BranchSlice(branch_index=[0, 1], prox=0.0, dist=1.0),
|
|
140
|
+
Channel("IL", g_max=4.0 * (u.mS / u.cm ** 2), E=-68.0 * u.mV),
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
channel_cell.init_state()
|
|
144
|
+
layout = channel_cell.layouts[0]
|
|
145
|
+
point_id = int(layout.point_index[0])
|
|
146
|
+
|
|
147
|
+
print("\nDensity mechanism layout:")
|
|
148
|
+
print("layout.kind:", layout.kind)
|
|
149
|
+
print("layout.target:", layout.target)
|
|
150
|
+
print("layout.n_active:", layout.n_active)
|
|
151
|
+
print("layout.source_cv_ids:", layout.source_cv_ids)
|
|
152
|
+
print("layout.point_index:", layout.point_index.tolist())
|
|
153
|
+
print("sample g_max at one active point:", channel_cell.get_point_state(point_id)[layout.id]["g_max"])
|
|
154
|
+
print("sample E at one active point:", channel_cell.get_point_state(point_id)[layout.id]["E"])
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
# 10. place(...): point mechanisms.
|
|
158
|
+
# place(locset, mechanism) attaches point mechanisms such as clamps and probes.
|
|
159
|
+
place_cell = Cell(morpho, cv_policy=CVPerBranch(2))
|
|
160
|
+
place_cell.place(
|
|
161
|
+
RootLocation(x=0.5),
|
|
162
|
+
CurrentClamp(delay=1.0 * u.ms, durations=2.0 * u.ms, amplitudes=0.1 * u.nA),
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
place_cell.init_state()
|
|
166
|
+
layout = place_cell.layouts[0]
|
|
167
|
+
|
|
168
|
+
print("\nPoint mechanism layout:")
|
|
169
|
+
print(place_cell)
|
|
170
|
+
print("layout.kind:", layout.kind)
|
|
171
|
+
print("layout.target:", layout.target)
|
|
172
|
+
print("layout.n_active:", layout.n_active)
|
|
173
|
+
print("layout.point_index:", layout.point_index.tolist())
|
|
174
|
+
print("amplitudes:", place_cell.get_state(layout.id, "amplitudes"))
|
|
175
|
+
print("durations:", place_cell.get_state(layout.id, "durations"))
|
|
176
|
+
print("start:", place_cell.get_state(layout.id, "start"))
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
# 11. Minimal runnable multicompartment simulation.
|
|
180
|
+
# This is the canonical Cell(morpho) simulation shape: choose CV policy and solver,
|
|
181
|
+
# paint density mechanisms, place point mechanisms/probes, initialize, reset, run.
|
|
182
|
+
sim_cell = Cell(
|
|
183
|
+
morpho,
|
|
184
|
+
cv_policy=CVPerBranch(cv_per_branch=2),
|
|
185
|
+
solver="staggered",
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
sim_cell.paint(
|
|
189
|
+
BranchSlice(branch_index=[0, 1], prox=0.0, dist=1.0),
|
|
190
|
+
Channel("IL", g_max=0.03 * (u.mS / u.cm ** 2), E=-54.387 * u.mV),
|
|
191
|
+
Channel("Na_HH1952", g_max=120.0 * (u.mS / u.cm ** 2)),
|
|
192
|
+
Channel("K_HH1952", g_max=36.0 * (u.mS / u.cm ** 2)),
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
sim_cell.place(
|
|
196
|
+
RootLocation(x=0.5),
|
|
197
|
+
CurrentClamp(delay=20.0 * u.ms, durations=60.0 * u.ms, amplitudes=0.2 * u.nA),
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
sim_cell.place(
|
|
201
|
+
at("soma", 0.5),
|
|
202
|
+
StateProbe(),
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
sim_cell.init_state()
|
|
206
|
+
sim_cell.reset_state()
|
|
207
|
+
|
|
208
|
+
dt = 0.1 * u.ms
|
|
209
|
+
duration = 100.0 * u.ms
|
|
210
|
+
result = sim_cell.run(dt=dt, duration=duration)
|
|
211
|
+
|
|
212
|
+
times_ms = u.math.concatenate([result.time])
|
|
213
|
+
soma_v_mV = u.math.concatenate([result.traces["soma(0.5)_v"]])
|
|
214
|
+
|
|
215
|
+
plt.figure(figsize=(6, 4))
|
|
216
|
+
plt.plot(times_ms, soma_v_mV, label="soma(0.5)_v")
|
|
217
|
+
plt.xlabel("Time (ms)")
|
|
218
|
+
plt.ylabel("Voltage (mV)")
|
|
219
|
+
plt.legend()
|
|
220
|
+
plt.tight_layout()
|
|
221
|
+
plt.show()
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Reference script mirrored from:
|
|
3
|
+
https://brainx.chaobrain.com/braincell/examples/channel_ablation.html
|
|
4
|
+
|
|
5
|
+
Purpose:
|
|
6
|
+
Canonical ablation script. Use when the task asks to remove, suppress, or compare a channel/current contribution, especially by setting conductance such as `gK=0` while keeping the ion/channel structure intact.
|
|
7
|
+
|
|
8
|
+
Use this when:
|
|
9
|
+
- The task asks for intact-vs-ablated point-neuron comparisons.
|
|
10
|
+
- The agent needs to suppress a current by changing `g_max` while preserving the ion/channel structure.
|
|
11
|
+
|
|
12
|
+
Do not use this when:
|
|
13
|
+
- The task asks for custom channel authoring or morphology-based mechanism removal.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
import brainstate
|
|
17
|
+
import brainunit as u
|
|
18
|
+
import matplotlib.pyplot as plt
|
|
19
|
+
import braincell
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class HH(braincell.SingleCompartment):
|
|
23
|
+
def __init__(self, size, gK=36. * (u.mS / u.cm ** 2), solver='exp_euler'):
|
|
24
|
+
super().__init__(size, V_th=20. * u.mV, solver=solver)
|
|
25
|
+
self.na = braincell.ion.SodiumFixed(size, E=50. * u.mV)
|
|
26
|
+
self.na.add(INa=braincell.channel.Na_HH1952(size))
|
|
27
|
+
self.k = braincell.ion.PotassiumFixed(size, E=-77. * u.mV)
|
|
28
|
+
# Ablation is controlled by `gK`; the potassium ion/channel path remains present.
|
|
29
|
+
self.k.add(IK=braincell.channel.K_HH1952(size, g_max=gK))
|
|
30
|
+
self.IL = braincell.channel.IL(size, E=-54.387 * u.mV,
|
|
31
|
+
g_max=0.03 * (u.mS / u.cm ** 2))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
intact = HH(1)
|
|
35
|
+
ablated = HH(1, gK=0. * (u.mS / u.cm ** 2))
|
|
36
|
+
intact.init_state()
|
|
37
|
+
ablated.init_state()
|
|
38
|
+
|
|
39
|
+
I = 5. * u.uA / u.cm ** 2
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def step(t):
|
|
43
|
+
with brainstate.environ.context(t=t):
|
|
44
|
+
intact.update(I)
|
|
45
|
+
ablated.update(I)
|
|
46
|
+
return intact.V.value, ablated.V.value
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
with brainstate.environ.context(dt=0.01 * u.ms):
|
|
50
|
+
times = u.math.arange(0. * u.ms, 80. * u.ms, brainstate.environ.get_dt())
|
|
51
|
+
v_intact, v_ablated = brainstate.transform.for_loop(step, times)
|
|
52
|
+
|
|
53
|
+
plt.figure(figsize=(8, 3))
|
|
54
|
+
plt.plot(times / u.ms, u.math.squeeze(v_intact) / u.mV, label='intact')
|
|
55
|
+
plt.plot(times / u.ms, u.math.squeeze(v_ablated) / u.mV, label='no $I_K$', linestyle='--')
|
|
56
|
+
plt.xlabel('Time (ms)')
|
|
57
|
+
plt.ylabel('Membrane potential (mV)')
|
|
58
|
+
plt.title('Effect of ablating the potassium current')
|
|
59
|
+
plt.legend()
|
|
60
|
+
plt.tight_layout()
|
|
61
|
+
plt.show()
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Reference script mirrored from:
|
|
3
|
+
https://brainx.chaobrain.com/braincell/examples/fi_curve.html
|
|
4
|
+
|
|
5
|
+
Purpose:
|
|
6
|
+
Canonical vectorized current-sweep script. Use when the task asks for FI curves, current sweeps, firing-rate extraction, warm-up discard, or using `size=N` as independent point neurons.
|
|
7
|
+
|
|
8
|
+
Use this when:
|
|
9
|
+
- The task asks for FI curves, current sweeps, spike counts, or firing-rate extraction.
|
|
10
|
+
- The agent needs a vectorized `SingleCompartment` pattern where `size=N` means independent point neurons.
|
|
11
|
+
|
|
12
|
+
Do not use this when:
|
|
13
|
+
- The task asks for morphology, network connectivity, or one detailed single-cell trace only.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
import brainstate
|
|
17
|
+
import brainunit as u
|
|
18
|
+
import numpy as np
|
|
19
|
+
import matplotlib.pyplot as plt
|
|
20
|
+
import braincell
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class HH(braincell.SingleCompartment):
|
|
24
|
+
def __init__(self, size, solver='exp_euler'):
|
|
25
|
+
super().__init__(size, V_th=20. * u.mV, solver=solver)
|
|
26
|
+
self.na = braincell.ion.SodiumFixed(size, E=50. * u.mV)
|
|
27
|
+
self.na.add(INa=braincell.channel.Na_HH1952(size))
|
|
28
|
+
self.k = braincell.ion.PotassiumFixed(size, E=-77. * u.mV)
|
|
29
|
+
self.k.add(IK=braincell.channel.K_HH1952(size))
|
|
30
|
+
self.IL = braincell.channel.IL(size, E=-54.387 * u.mV,
|
|
31
|
+
g_max=0.03 * (u.mS / u.cm ** 2))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
n_levels = 11
|
|
35
|
+
amplitudes = np.linspace(0., 20., n_levels) # uA/cm^2
|
|
36
|
+
I = amplitudes * (u.uA / u.cm ** 2)
|
|
37
|
+
|
|
38
|
+
# `HH(n_levels)` creates independent point neurons, not compartments.
|
|
39
|
+
net = HH(n_levels)
|
|
40
|
+
net.init_state()
|
|
41
|
+
|
|
42
|
+
warmup = 100. * u.ms
|
|
43
|
+
total = 600. * u.ms
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def step(t):
|
|
47
|
+
with brainstate.environ.context(t=t):
|
|
48
|
+
net.update(I)
|
|
49
|
+
return t, net.spike.value
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
with brainstate.environ.context(dt=0.01 * u.ms):
|
|
53
|
+
times = u.math.arange(0. * u.ms, total, brainstate.environ.get_dt())
|
|
54
|
+
ts, spikes = brainstate.transform.for_loop(step, times)
|
|
55
|
+
|
|
56
|
+
# Discard warm-up before converting spike counts to firing rate.
|
|
57
|
+
mask = (ts >= warmup)
|
|
58
|
+
counts = np.asarray(u.math.sum(spikes[mask], axis=0))
|
|
59
|
+
rate = counts / float((total - warmup) / u.second) # Hz
|
|
60
|
+
print('spike counts:', counts.astype(int).tolist())
|
|
61
|
+
|
|
62
|
+
plt.figure(figsize=(5, 4))
|
|
63
|
+
plt.plot(amplitudes, rate, 'o-')
|
|
64
|
+
plt.xlabel('Input current density (uA/cm$^2$)')
|
|
65
|
+
plt.ylabel('Firing rate (Hz)')
|
|
66
|
+
plt.title('F\u2013I curve of an HH neuron')
|
|
67
|
+
plt.tight_layout()
|
|
68
|
+
plt.show()
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Reference script mirrored from:
|
|
3
|
+
https://brainx.chaobrain.com/braincell/examples/hh_neuron_basics.html
|
|
4
|
+
|
|
5
|
+
Purpose:
|
|
6
|
+
Canonical minimal single-compartment HH simulation. Use as the default full-script reference for building one `SingleCompartment` neuron, adding Na/K/leak currents, initializing state, injecting current through `update(I)`, running a loop, and plotting voltage/spikes.
|
|
7
|
+
|
|
8
|
+
Use this when:
|
|
9
|
+
- The task asks for one end-to-end HH point-neuron current-clamp simulation.
|
|
10
|
+
- The agent needs the default `SingleCompartment` Na/K/leak construction pattern.
|
|
11
|
+
|
|
12
|
+
Do not use this when:
|
|
13
|
+
- The task needs morphology, network wiring, or a channel-level diagnostic instead of a membrane trace.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
import brainstate
|
|
17
|
+
import brainunit as u
|
|
18
|
+
import matplotlib.pyplot as plt
|
|
19
|
+
import braincell
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# Subclass `SingleCompartment`; `size=1` below means one point neuron.
|
|
23
|
+
class HH(braincell.SingleCompartment):
|
|
24
|
+
def __init__(self, size, solver='exp_euler'):
|
|
25
|
+
super().__init__(size, V_th=20. * u.mV, solver=solver)
|
|
26
|
+
self.na = braincell.ion.SodiumFixed(size, E=50. * u.mV)
|
|
27
|
+
self.na.add(INa=braincell.channel.Na_HH1952(size))
|
|
28
|
+
self.k = braincell.ion.PotassiumFixed(size, E=-77. * u.mV)
|
|
29
|
+
self.k.add(IK=braincell.channel.K_HH1952(size))
|
|
30
|
+
self.IL = braincell.channel.IL(size, E=-54.387 * u.mV,
|
|
31
|
+
g_max=0.03 * (u.mS / u.cm ** 2))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
neuron = HH(1)
|
|
35
|
+
neuron.init_state()
|
|
36
|
+
|
|
37
|
+
# Current clamp is direct: pass density-based injected current to `update(I)`.
|
|
38
|
+
I = 5. * u.uA / u.cm ** 2
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def step(t):
|
|
42
|
+
with brainstate.environ.context(t=t):
|
|
43
|
+
neuron.update(I)
|
|
44
|
+
return neuron.V.value, neuron.spike.value
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
with brainstate.environ.context(dt=0.01 * u.ms):
|
|
48
|
+
times = u.math.arange(0. * u.ms, 100. * u.ms, brainstate.environ.get_dt())
|
|
49
|
+
vs, spikes = brainstate.transform.for_loop(step, times)
|
|
50
|
+
|
|
51
|
+
print('number of spikes:', int(u.math.sum(spikes)))
|
|
52
|
+
|
|
53
|
+
plt.figure(figsize=(8, 3))
|
|
54
|
+
plt.plot(times / u.ms, u.math.squeeze(vs) / u.mV, linewidth=1.2)
|
|
55
|
+
plt.xlabel('Time (ms)')
|
|
56
|
+
plt.ylabel('Membrane potential (mV)')
|
|
57
|
+
plt.title('HH neuron under 5 uA/cm^2 constant current')
|
|
58
|
+
plt.tight_layout()
|
|
59
|
+
plt.show()
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Reference script mirrored from:
|
|
3
|
+
https://brainx.chaobrain.com/braincell/examples/spike_frequency_adaptation.html
|
|
4
|
+
|
|
5
|
+
Purpose:
|
|
6
|
+
Advanced single-cell mechanism script. Use when the task asks for spike-frequency adaptation, calcium-dependent afterhyperpolarization, dynamic calcium, `MixIons(k, ca)`, or AHP/KCa currents.
|
|
7
|
+
|
|
8
|
+
Use this when:
|
|
9
|
+
- The task asks for adaptation or calcium-dependent afterhyperpolarization in a point neuron.
|
|
10
|
+
- The agent needs dynamic calcium plus `MixIons(self.k, self.ca)` for AHP/KCa currents.
|
|
11
|
+
|
|
12
|
+
Do not use this when:
|
|
13
|
+
- The task only needs the minimal HH Na/K/leak current-clamp pattern.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
import brainstate
|
|
17
|
+
import braintools
|
|
18
|
+
import brainunit as u
|
|
19
|
+
import numpy as np
|
|
20
|
+
import matplotlib.pyplot as plt
|
|
21
|
+
import braincell
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class AdaptingCell(braincell.SingleCompartment):
|
|
25
|
+
"""A thalamic-relay-style cell with Ca-activated K (AHP) current."""
|
|
26
|
+
def __init__(self, size, solver='ind_exp_euler'):
|
|
27
|
+
super().__init__(size, V_initializer=braintools.init.Constant(-65. * u.mV),
|
|
28
|
+
V_th=20. * u.mV, solver=solver)
|
|
29
|
+
self.na = braincell.ion.SodiumFixed(size, E=50. * u.mV)
|
|
30
|
+
self.na.add(INa=braincell.channel.Na_Ba2002(size, V_sh=-30 * u.mV))
|
|
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(IDR=braincell.channel.KDR_Ba2002(size, V_sh=-30. * u.mV, q10=2.0, temp=u.celsius2kelvin(16.)))
|
|
34
|
+
# Dynamic calcium is an ODE state used by calcium channels and AHP coupling.
|
|
35
|
+
self.ca = braincell.ion.CalciumDetailed(size, C_rest=5e-5 * u.mM,
|
|
36
|
+
tau=10. * u.ms, d=0.5 * u.um)
|
|
37
|
+
self.ca.add(ICaL=braincell.channel.CaL_IS2008(size, g_max=0.5 * (u.mS / u.cm ** 2)))
|
|
38
|
+
self.ca.add(ICaN=braincell.channel.CaN_IS2008(size, g_max=0.5 * (u.mS / u.cm ** 2)))
|
|
39
|
+
self.ca.add(ICaT=braincell.channel.CaT_HM1992(size, g_max=2.1 * (u.mS / u.cm ** 2)))
|
|
40
|
+
self.ca.add(ICaHT=braincell.channel.CaHT_HM1992(size, g_max=3.0 * (u.mS / u.cm ** 2)))
|
|
41
|
+
# AHP current depends on both potassium reversal and calcium concentration.
|
|
42
|
+
self.kca = braincell.MixIons(self.k, self.ca)
|
|
43
|
+
self.kca.add(IAHP=braincell.channel.AHP_De1994(size, g_max=0.3 * (u.mS / u.cm ** 2)))
|
|
44
|
+
self.IL = braincell.channel.IL(size, g_max=0.0075 * (u.mS / u.cm ** 2), E=-70 * u.mV)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
cell = AdaptingCell(1)
|
|
48
|
+
cell.init_state()
|
|
49
|
+
|
|
50
|
+
I = 2. * u.uA / u.cm ** 2
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def step(t):
|
|
54
|
+
with brainstate.environ.context(t=t):
|
|
55
|
+
cell.update(I)
|
|
56
|
+
return cell.V.value, cell.spike.value
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
with brainstate.environ.context(dt=0.01 * u.ms):
|
|
60
|
+
times = u.math.arange(0. * u.ms, 500. * u.ms, brainstate.environ.get_dt())
|
|
61
|
+
vs, spikes = brainstate.transform.for_loop(step, times)
|
|
62
|
+
|
|
63
|
+
spike_times = np.asarray(times[u.math.squeeze(spikes) > 0] / u.ms)
|
|
64
|
+
isi = np.diff(spike_times)
|
|
65
|
+
print('number of spikes:', spike_times.size)
|
|
66
|
+
print('first ISIs (ms):', np.round(isi[:6], 2).tolist())
|
|
67
|
+
|
|
68
|
+
fig, axs = plt.subplots(2, 1, figsize=(8, 5))
|
|
69
|
+
axs[0].plot(times / u.ms, u.math.squeeze(vs) / u.mV, linewidth=0.8)
|
|
70
|
+
axs[0].set_xlabel('Time (ms)'); axs[0].set_ylabel('V (mV)')
|
|
71
|
+
axs[0].set_title('Membrane potential under sustained drive')
|
|
72
|
+
axs[1].plot(np.arange(1, isi.size + 1), isi, 'o-')
|
|
73
|
+
axs[1].set_xlabel('Inter-spike interval index'); axs[1].set_ylabel('ISI (ms)')
|
|
74
|
+
axs[1].set_title('ISIs lengthen over time (adaptation)')
|
|
75
|
+
plt.tight_layout(); plt.show()
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Reference script mirrored from:
|
|
3
|
+
https://brainx.chaobrain.com/braincell/examples/t_current_rebound.html
|
|
4
|
+
|
|
5
|
+
Purpose:
|
|
6
|
+
Advanced rebound-bursting script. Use when the task asks for post-inhibitory rebound, T-type calcium current, thalamic-style rebound, hyperpolarizing current steps, or release from inhibition.
|
|
7
|
+
|
|
8
|
+
Use this when:
|
|
9
|
+
- The task asks for rebound bursting after a hyperpolarizing current step.
|
|
10
|
+
- The agent needs T-type calcium and HCN currents in a single-compartment thalamic-style cell.
|
|
11
|
+
|
|
12
|
+
Do not use this when:
|
|
13
|
+
- The task asks for a generic HH current-clamp trace without rebound or calcium dynamics.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
import brainstate
|
|
17
|
+
import braintools
|
|
18
|
+
import brainunit as u
|
|
19
|
+
import numpy as np
|
|
20
|
+
import matplotlib.pyplot as plt
|
|
21
|
+
import braincell
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class ThalamicCell(braincell.SingleCompartment):
|
|
25
|
+
"""Thalamic-relay-style cell with a low-threshold T-type Ca current."""
|
|
26
|
+
def __init__(self, size, solver='ind_exp_euler'):
|
|
27
|
+
super().__init__(size, V_initializer=braintools.init.Constant(-65. * u.mV),
|
|
28
|
+
V_th=20. * u.mV, solver=solver)
|
|
29
|
+
self.na = braincell.ion.SodiumFixed(size, E=50. * u.mV)
|
|
30
|
+
self.na.add(INa=braincell.channel.Na_Ba2002(size, V_sh=-30 * u.mV))
|
|
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(IDR=braincell.channel.KDR_Ba2002(size, V_sh=-30. * u.mV, q10=2.0, temp=u.celsius2kelvin(16.)))
|
|
34
|
+
self.ca = braincell.ion.CalciumDetailed(size, C_rest=5e-5 * u.mM,
|
|
35
|
+
tau=10. * u.ms, d=0.5 * u.um)
|
|
36
|
+
# T-type calcium current supports post-inhibitory rebound.
|
|
37
|
+
self.ca.add(ICaT=braincell.channel.CaT_HM1992(size, g_max=2.1 * (u.mS / u.cm ** 2)))
|
|
38
|
+
self.ca.add(ICaHT=braincell.channel.CaHT_HM1992(size, g_max=3.0 * (u.mS / u.cm ** 2)))
|
|
39
|
+
self.Ih = braincell.channel.HCN_HM1992(size, g_max=0.01 * (u.mS / u.cm ** 2), E=-43 * u.mV)
|
|
40
|
+
self.IL = braincell.channel.IL(size, g_max=0.0075 * (u.mS / u.cm ** 2), E=-70 * u.mV)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
cell = ThalamicCell(1)
|
|
44
|
+
cell.init_state()
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def I_of_t(t):
|
|
48
|
+
return u.math.where(t < 200. * u.ms,
|
|
49
|
+
-2. * u.uA / u.cm ** 2,
|
|
50
|
+
0. * u.uA / u.cm ** 2)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def step(t):
|
|
54
|
+
with brainstate.environ.context(t=t):
|
|
55
|
+
cell.update(I_of_t(t))
|
|
56
|
+
return cell.V.value, cell.spike.value
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
with brainstate.environ.context(dt=0.01 * u.ms):
|
|
60
|
+
times = u.math.arange(0. * u.ms, 500. * u.ms, brainstate.environ.get_dt())
|
|
61
|
+
vs, spikes = brainstate.transform.for_loop(step, times)
|
|
62
|
+
|
|
63
|
+
spike_times = np.asarray(times[u.math.squeeze(spikes) > 0] / u.ms)
|
|
64
|
+
rebound = spike_times[spike_times > 200.]
|
|
65
|
+
print('spikes after release (ms):', np.round(rebound[:10], 1).tolist())
|
|
66
|
+
|
|
67
|
+
plt.figure(figsize=(8, 3))
|
|
68
|
+
plt.plot(times / u.ms, u.math.squeeze(vs) / u.mV, linewidth=0.8)
|
|
69
|
+
plt.axvline(200., color='k', linestyle=':', label='release')
|
|
70
|
+
plt.xlabel('Time (ms)'); plt.ylabel('Membrane potential (mV)')
|
|
71
|
+
plt.title('Post-inhibitory rebound burst (T-type Ca current)')
|
|
72
|
+
plt.legend(); plt.tight_layout(); plt.show()
|