Simulation¶
The current- and voltage-clamp runners and their structured-array result type, plus the electrochemistry and equilibrium helpers.
Runners¶
neuroclamp.simulate_current_clamp ¶
simulate_current_clamp(neuron, current_external)
Simulate a current clamp experiment using the conductance-based neuron model.
In a current clamp experiment, current is injected into the cell membrane and the resulting voltage changes are recorded. This function simulates this process by computing the membrane voltage over time in response to the specified external current.
The simulation always uses :data:SIM_SAMPLING_FREQ (40 kHz, dt = 0.025 ms)
as the integration time step.
All ion channels — core (INa, IK, INaL, IKL) and additional — are handled via a
single unified loop. The result includes a field named after each channel
for every channel (including core channels) and "Itotal".
When the neuron has calcium_dynamics configured, a ca_i field
containing intracellular Ca²⁺ concentration in mM is included.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
neuron
|
Neuron
|
The conductance-based neuron model to simulate. |
required |
current_external
|
ndarray
|
External current in µA/cm² for a time-varying current waveform. The length of the array determines the simulation duration. |
required |
Returns:
| Type | Description |
|---|---|
SimulationResult
|
Structured array with one element per time step. Field |
SimulationResult
|
the time axis in milliseconds. Remaining fields: |
SimulationResult
|
per-channel current fields, |
SimulationResult
|
optionally |
Source code in neuroclamp/clamp_simulations.py
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 | |
neuroclamp.simulate_voltage_clamp ¶
simulate_voltage_clamp(neuron, voltage_protocol)
Simulate a voltage clamp experiment using the conductance-based neuron model.
In a voltage clamp experiment, the membrane potential is held at specified values and the current required to maintain those voltages is measured. This function simulates this process by computing the ionic currents that would flow at each voltage step in the protocol.
The simulation always uses :data:SIM_SAMPLING_FREQ (40 kHz, dt = 0.025 ms)
as the integration time step.
All ion channels — core (INa, IK, INaL, IKL) and additional — are handled via a single unified loop. The result includes a field named after each channel for every channel and a field for every gating variable.
When the neuron has calcium_dynamics configured, a ca_i field
containing intracellular Ca²⁺ concentration in mM is included.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
neuron
|
Neuron
|
The conductance-based neuron model to simulate. |
required |
voltage_protocol
|
ndarray
|
Voltage values in mV to clamp the membrane at for each time step. The length of the array determines the simulation duration. |
required |
Returns:
| Type | Description |
|---|---|
SimulationResult
|
Structured array with one element per time step. Field |
SimulationResult
|
the time axis in milliseconds. Remaining fields: |
SimulationResult
|
|
SimulationResult
|
optionally |
Source code in neuroclamp/clamp_simulations.py
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 | |
neuroclamp.simulate_current_clamp_from_state ¶
simulate_current_clamp_from_state(neuron, current_external, initial_V, initial_gating_state, initial_ca_i=0.0)
Simulate a current clamp experiment starting from an explicit neuron state.
Identical to :func:simulate_current_clamp except the initial membrane
voltage, gating variables, and Ca²⁺ concentration are provided by the
caller rather than initialized from neuron.v_rest. This is used by
continuous simulation mode to carry neuron state across loop iterations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
neuron
|
Neuron
|
The conductance-based neuron model to simulate. |
required |
current_external
|
ndarray
|
External current in µA/cm² for a time-varying current waveform. |
required |
initial_V
|
float
|
Initial membrane voltage in mV. |
required |
initial_gating_state
|
dict[str, float]
|
Gating variable values at the start of the run,
keyed by variable name (e.g. |
required |
initial_ca_i
|
float
|
Initial intracellular Ca²⁺ concentration in mM. |
0.0
|
Returns:
| Type | Description |
|---|---|
SimulationResult
|
Structured array with the same fields as :func: |
Source code in neuroclamp/clamp_simulations.py
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 | |
neuroclamp.simulate_voltage_clamp_from_state ¶
simulate_voltage_clamp_from_state(neuron, voltage_protocol, initial_gating_state, initial_ca_i=0.0)
Simulate a voltage clamp experiment starting from an explicit gating state.
Identical to :func:simulate_voltage_clamp except the initial gating
variables and Ca²⁺ concentration are provided by the caller rather than
initialized to steady-state at the first protocol voltage. This is used
by continuous simulation mode to carry neuron state across loop iterations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
neuron
|
Neuron
|
The conductance-based neuron model to simulate. |
required |
voltage_protocol
|
ndarray
|
Voltage values in mV to clamp the membrane at for each time step. |
required |
initial_gating_state
|
dict[str, float]
|
Gating variable values at the start of the run,
keyed by variable name (e.g. |
required |
initial_ca_i
|
float
|
Initial intracellular Ca²⁺ concentration in mM. |
0.0
|
Returns:
| Type | Description |
|---|---|
SimulationResult
|
Structured array with the same fields as :func: |
Source code in neuroclamp/clamp_simulations.py
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 | |
neuroclamp.simulate_batch ¶
simulate_batch(neuron, protocols, simulate_fn=simulate_voltage_clamp, max_workers=None)
Run multiple simulation protocols in parallel and yield results in order.
Each protocol is submitted to a process pool and executed independently (fresh initial conditions per protocol). Results are yielded in submission order so callers can process them progressively as simulations complete.
An empty protocols sequence yields nothing. A single-element sequence is equivalent to calling simulate_fn directly, but still routed through the pool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
neuron
|
Neuron
|
The conductance-based neuron model to simulate. |
required |
protocols
|
Sequence[ndarray]
|
Sequence of protocol arrays, each passed individually to simulate_fn. |
required |
simulate_fn
|
Callable[[Neuron, ndarray], SimulationResult]
|
The simulation function to apply to each protocol.
Defaults to :func: |
simulate_voltage_clamp
|
max_workers
|
int | None
|
Maximum number of worker processes. |
None
|
Yields:
| Type | Description |
|---|---|
SimulationResult
|
Structured arrays in the same order as protocols, one per protocol. |
Source code in neuroclamp/clamp_simulations.py
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 | |
Result¶
neuroclamp.SimulationResult ¶
Bases: ndarray[Any, Any]
A NumPy structured array with named dtype fields.
Each element represents one time step. Fields include "time",
"voltage", "Itotal", per-channel currents (e.g. "INa",
"IK"), gating variables, and optionally "ca_i".
Source code in neuroclamp/clamp_simulations.py
29 30 31 32 33 34 35 36 37 | |
Electrochemistry and equilibrium¶
neuroclamp.nernst_potential ¶
nernst_potential(z, T, ion_concentration_out, ion_concentration_in)
Calculate the Nernst potential for a given ion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
int
|
Valence of the ion (e.g., +1 for K+, +2 for Ca2+). |
required |
T
|
float
|
Temperature in Kelvin. |
required |
ion_concentration_out
|
float
|
Extracellular ion concentration (mM). |
required |
ion_concentration_in
|
float
|
Intracellular ion concentration (mM). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Nernst potential in millivolts (mV). |
Source code in neuroclamp/electrochemistry.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
neuroclamp.goldman_potential ¶
goldman_potential(T, cation_terms, anion_terms=())
Calculate the Goldman-Hodgkin-Katz (GHK) reversal potential.
Implements the GHK voltage equation for a mixture of monovalent ions:
E = (RT/F) * ln(
(sum_i P_i^cat * C_i_out + sum_j P_j^an * C_j_in)
/ (sum_i P_i^cat * C_i_in + sum_j P_j^an * C_j_out)
)
where cations contribute their extracellular concentration to the numerator and intracellular to the denominator; anions are reversed.
All ion species passed must be monovalent (|z| = 1). For divalent ions
such as Ca²⁺ use :func:nernst_potential directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
T
|
float
|
Temperature in Kelvin. |
required |
cation_terms
|
Sequence[tuple[float, float, float]]
|
Sequence of |
required |
anion_terms
|
Sequence[tuple[float, float, float]]
|
Sequence of |
()
|
Returns:
| Type | Description |
|---|---|
float
|
GHK reversal potential in millivolts (mV). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If T is not positive, if both cation_terms and anion_terms are empty, if any permeability is negative, or if any concentration is non-positive. |
Source code in neuroclamp/electrochemistry.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
neuroclamp.find_zero_current_voltage ¶
find_zero_current_voltage(neuron, v_min=-100.0, v_max=-20.0, tol=1e-06, ca_i=None)
Find the membrane voltage where total ionic current is zero.
Uses Brent's method to locate the voltage at which the sum of all ionic currents (with gating variables held at their steady-state values) is zero. This is the true zero-current resting potential — the voltage the neuron will settle to when no external current is injected.
Calcium dynamics are accounted for by using ca_i as the intracellular
Ca²⁺ concentration when evaluating calcium-dependent gating variables.
When ca_i is None (the default) the neuron's declared ca_rest
is used, consistent with how
:func:~neuroclamp.simulate_current_clamp historically initialized the
state. Pass an explicit value (e.g. from
:func:find_coupled_equilibrium) when you need to evaluate the
zero-current voltage at a specific Ca²⁺ concentration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
neuron
|
'Neuron'
|
The conductance-based neuron model. |
required |
v_min
|
float
|
Lower bound of the voltage search range in mV. Must bracket the root together with v_max. |
-100.0
|
v_max
|
float
|
Upper bound of the voltage search range in mV. |
-20.0
|
tol
|
float
|
Convergence tolerance in mV. |
1e-06
|
ca_i
|
float | None
|
Intracellular Ca²⁺ concentration in mM to use when evaluating
calcium-dependent gating variables. |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Voltage in mV at which total ionic current is zero. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If v_min and v_max do not bracket a sign change in the ionic-current function, or if v_min >= v_max. |
Source code in neuroclamp/equilibrium.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
neuroclamp.find_coupled_equilibrium ¶
find_coupled_equilibrium(neuron, v_min=-100.0, v_max=-20.0, tol=1e-06, max_iter=50)
Find the coupled (V_ss, ca_i_ss) equilibrium for a neuron.
For neurons without calcium dynamics, equivalent to
:func:find_zero_current_voltage with ca_i = 0 — returns
(V_ss, 0.0).
For neurons with calcium dynamics, uses fixed-point iteration to simultaneously satisfy:
- Voltage equilibrium: total ionic current is zero at V_ss (with gating variables at their steady-state values for that voltage and Ca²⁺ concentration).
- Calcium equilibrium: the calcium ODE is at rest —
ca_i_ss = ca_rest − α_ca · I_Ca(V_ss, ca_i_ss) · τ_ca.
Because the Ca²⁺ reversal potential depends on ca_i (dynamic E_Ca),
the two equilibrium conditions are coupled: the resting ca_i shifts
E_Ca, which shifts the zero-current voltage, which changes the Ca²⁺
window current, which shifts ca_i again. Iteration typically
converges in fewer than ten steps for physiological parameters.
The returned (V_ss, ca_i_ss) pair is the correct starting point for
:func:~neuroclamp.simulate_current_clamp so that the simulation does
not drift when no external current is applied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
neuron
|
'Neuron'
|
The conductance-based neuron model. |
required |
v_min
|
float
|
Lower bound for the voltage root search in mV. |
-100.0
|
v_max
|
float
|
Upper bound for the voltage root search in mV. |
-20.0
|
tol
|
float
|
Convergence tolerance: iteration stops when both
|
1e-06
|
max_iter
|
int
|
Maximum number of fixed-point iterations before giving up.
If reached without satisfying tol, a :class: |
50
|
Returns:
| Type | Description |
|---|---|
float
|
|
float
|
equilibrium intracellular Ca²⁺ concentration in mM. |
Raises:
| Type | Description |
|---|---|
ValueError
|
Propagated from :func: |
Warns:
| Type | Description |
|---|---|
RuntimeWarning
|
If max_iter iterations elapse without convergence. |
Source code in neuroclamp/equilibrium.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |