Skip to content

Rates

Gating-variable rate functions. Every alpha/beta on a GatingVariable is a Rate.

Rate expressions describe kinetics as data: a small tree of standard forms (Exp, Sigmoid, ExpLinear, Sech, Hill, Const) joined with +, -, *, / and Max. For gates specified by a steady state and a time constant, inf_tau builds the matching pair of rates. For example, a Na⁺ inactivation gate:

from neuroclamp.channels import GatingVariable
from neuroclamp.rates import Exp, Sigmoid, inf_tau

h_alpha, h_beta = inf_tau(
    inf=Sigmoid(1.0, -45.5, -6.4),
    tau=1.0 / (Exp(0.128, -38.0, -18.0) + Sigmoid(4.0, -15.0, 5.0)),
)
h = GatingVariable("h", power=1, alpha=h_alpha, beta=h_beta)

Kinetics that no expression form covers can wrap a plain function with VoltageOnlyFn or CalciumDependentFn, which can also appear inside an expression.

The first call to an expression compiles the whole tree into one Python function, so expressions run as fast as hand-written rate functions. (A very deep tree is split into several compiled functions that call each other.) Expr.evaluate() computes a node directly from its definition and is the only method a custom Expr subclass, or a subclass of a built-in form, needs to override.

neuroclamp.rates

Gating-variable rate-function class hierarchy.

Every alpha/beta rate attached to a :class:~neuroclamp.channels.GatingVariable is an instance of :class:Rate. :attr:Rate.uses_calcium reports whether a rate's output depends on ca_i. The voltage-clamp optimizer tabulates rates that do not read ca_i over the prescribed voltage protocol; Ca²⁺-dependent rates are evaluated scalar-wise in the RK4 loop. The abstract subclasses :class:VoltageOnlyRate and :class:CalciumDependentRate fix uses_calcium to False and True respectively.

Rates can be written two ways:

  • Expressions (:class:Expr and its subclasses) describe a rate as a small tree of standard forms, for example inf_tau(Sigmoid(1.0, -40.0, 8.0), 0.2). They are plain data, so they can be compared, pickled, and inspected, and they report :attr:~Rate.uses_calcium automatically.
  • Adapters :class:VoltageOnlyFn and :class:CalciumDependentFn wrap plain (V, ca_i) -> float callables, for kinetics no expression form covers. The caller declares the Ca²⁺ dependence by choosing the adapter.

All rate types are frozen dataclasses, matching the picklability contract relied on by :func:~neuroclamp.clamp_simulations.simulate_batch.

Rate

Bases: ABC

Abstract base class for gating-variable rate functions.

Source code in neuroclamp/rates.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Rate(ABC):
    """Abstract base class for gating-variable rate functions."""

    @property
    def uses_calcium(self) -> bool:
        """Whether the rate's output depends on ca_i.

        Rates that do not read ca_i are tabulated over the prescribed voltage
        protocol in voltage clamp.  The base class conservatively reports
        ``True``, so a rate is only tabulated when it says it is safe to.

        Returns:
            ``True`` if the rate may depend on ca_i.
        """
        return True

    @abstractmethod
    def __call__(self, V: float, ca_i: float) -> float:
        """Evaluate the rate.

        Args:
            V: Membrane voltage in mV.
            ca_i: Intracellular Ca²⁺ concentration in mM.

        Returns:
            The rate in 1/ms.
        """

uses_calcium property

uses_calcium

Whether the rate's output depends on ca_i.

Rates that do not read ca_i are tabulated over the prescribed voltage protocol in voltage clamp. The base class conservatively reports True, so a rate is only tabulated when it says it is safe to.

Returns:

Type Description
bool

True if the rate may depend on ca_i.

__call__ abstractmethod

__call__(V, ca_i)

Evaluate the rate.

Parameters:

Name Type Description Default
V float

Membrane voltage in mV.

required
ca_i float

Intracellular Ca²⁺ concentration in mM.

required

Returns:

Type Description
float

The rate in 1/ms.

Source code in neuroclamp/rates.py
51
52
53
54
55
56
57
58
59
60
61
@abstractmethod
def __call__(self, V: float, ca_i: float) -> float:
    """Evaluate the rate.

    Args:
        V: Membrane voltage in mV.
        ca_i: Intracellular Ca²⁺ concentration in mM.

    Returns:
        The rate in 1/ms.
    """

VoltageOnlyRate

Bases: Rate

Base class for rates whose output is independent of ca_i.

Subclasses are safe to tabulate over a prescribed voltage protocol.

Source code in neuroclamp/rates.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class VoltageOnlyRate(Rate):
    """Base class for rates whose output is independent of ca_i.

    Subclasses are safe to tabulate over a prescribed voltage protocol.
    """

    @property
    def uses_calcium(self) -> bool:
        """Report that the rate does not read ca_i.

        Returns:
            Always ``False``.
        """
        return False

uses_calcium property

uses_calcium

Report that the rate does not read ca_i.

Returns:

Type Description
bool

Always False.

CalciumDependentRate

Bases: Rate

Base class for rates whose output depends on ca_i.

Subclasses must be evaluated scalar-wise at the current (V, ca_i).

Source code in neuroclamp/rates.py
80
81
82
83
84
85
86
87
88
89
90
91
92
93
class CalciumDependentRate(Rate):
    """Base class for rates whose output depends on ca_i.

    Subclasses must be evaluated scalar-wise at the current (V, ca_i).
    """

    @property
    def uses_calcium(self) -> bool:
        """Report that the rate reads ca_i.

        Returns:
            Always ``True``.
        """
        return True

uses_calcium property

uses_calcium

Report that the rate reads ca_i.

Returns:

Type Description
bool

Always True.

VoltageOnlyFn dataclass

Bases: VoltageOnlyRate

Adapter wrapping a plain callable as a :class:VoltageOnlyRate.

.. warning:: Wrapping a function that does depend on ca_i in VoltageOnlyFn causes silent numerical errors: the voltage-clamp precompute pass evaluates the rate with ca_i=0.0 and caches the result. Use :class:CalciumDependentFn for any rate that reads ca_i.

Attributes:

Name Type Description
fn Callable[[float, float], float]

Plain (V, ca_i) -> float callable. Must be independent of ca_i — wrap Ca²⁺-dependent functions with :class:CalciumDependentFn instead.

Source code in neuroclamp/rates.py
 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
@dataclass(frozen=True)
class VoltageOnlyFn(VoltageOnlyRate):
    """Adapter wrapping a plain callable as a :class:`VoltageOnlyRate`.

    .. warning::
        Wrapping a function that *does* depend on ``ca_i`` in
        ``VoltageOnlyFn`` causes silent numerical errors: the voltage-clamp
        precompute pass evaluates the rate with ``ca_i=0.0`` and caches the
        result.  Use :class:`CalciumDependentFn` for any rate that reads
        ``ca_i``.

    Attributes:
        fn: Plain ``(V, ca_i) -> float`` callable.  Must be independent of
            ``ca_i`` — wrap Ca²⁺-dependent functions with
            :class:`CalciumDependentFn` instead.
    """

    fn: Callable[[float, float], float]

    def __call__(self, V: float, ca_i: float) -> float:
        """Delegate to the wrapped callable.

        Args:
            V: Membrane voltage in mV.
            ca_i: Intracellular Ca²⁺ concentration in mM (ignored).

        Returns:
            The rate in 1/ms.
        """
        return self.fn(V, ca_i)

__call__

__call__(V, ca_i)

Delegate to the wrapped callable.

Parameters:

Name Type Description Default
V float

Membrane voltage in mV.

required
ca_i float

Intracellular Ca²⁺ concentration in mM (ignored).

required

Returns:

Type Description
float

The rate in 1/ms.

Source code in neuroclamp/rates.py
115
116
117
118
119
120
121
122
123
124
125
def __call__(self, V: float, ca_i: float) -> float:
    """Delegate to the wrapped callable.

    Args:
        V: Membrane voltage in mV.
        ca_i: Intracellular Ca²⁺ concentration in mM (ignored).

    Returns:
        The rate in 1/ms.
    """
    return self.fn(V, ca_i)

CalciumDependentFn dataclass

Bases: CalciumDependentRate

Adapter wrapping a plain callable as a :class:CalciumDependentRate.

Attributes:

Name Type Description
fn Callable[[float, float], float]

Plain (V, ca_i) -> float callable whose output depends on ca_i.

Source code in neuroclamp/rates.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
@dataclass(frozen=True)
class CalciumDependentFn(CalciumDependentRate):
    """Adapter wrapping a plain callable as a :class:`CalciumDependentRate`.

    Attributes:
        fn: Plain ``(V, ca_i) -> float`` callable whose output depends on
            ``ca_i``.
    """

    fn: Callable[[float, float], float]

    def __call__(self, V: float, ca_i: float) -> float:
        """Delegate to the wrapped callable.

        Args:
            V: Membrane voltage in mV.
            ca_i: Intracellular Ca²⁺ concentration in mM.

        Returns:
            The rate in 1/ms.
        """
        return self.fn(V, ca_i)

__call__

__call__(V, ca_i)

Delegate to the wrapped callable.

Parameters:

Name Type Description Default
V float

Membrane voltage in mV.

required
ca_i float

Intracellular Ca²⁺ concentration in mM.

required

Returns:

Type Description
float

The rate in 1/ms.

Source code in neuroclamp/rates.py
139
140
141
142
143
144
145
146
147
148
149
def __call__(self, V: float, ca_i: float) -> float:
    """Delegate to the wrapped callable.

    Args:
        V: Membrane voltage in mV.
        ca_i: Intracellular Ca²⁺ concentration in mM.

    Returns:
        The rate in 1/ms.
    """
    return self.fn(V, ca_i)

Expr

Bases: Rate

Base class for rate expressions: rate functions described as data.

An expression is a small tree of the leaf forms below (:class:Exp, :class:Sigmoid, :class:ExpLinear, :class:Sech, :class:Hill, :class:Const) joined by arithmetic (:class:Add, :class:Sub, :class:Mul, :class:Div, :class:Max). Every node is a frozen dataclass and a callable :class:Rate, so an expression can be used anywhere a rate function can. + - * / build combinator nodes and wrap plain numbers in :class:Const, so kinetics read like their equations::

tau_h = 1.0 / (Exp(0.128, -38.0, -18.0) + Sigmoid(4.0, -15.0, 5.0))

An operand can also be a :class:VoltageOnlyFn or :class:CalciumDependentFn, so a term no form covers can be combined with the standard forms.

:attr:uses_calcium is derived from the tree: an expression reads ca_i exactly when it contains a :class:Hill node or a rate (such as a :class:CalciumDependentFn) that reports uses_calcium. Because the structure is plain data, expressions compare by value, pickle for :func:~neuroclamp.clamp_simulations.simulate_batch, and can be inspected or translated by other tools.

Source code in neuroclamp/rates.py
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
class Expr(Rate):
    """Base class for rate expressions: rate functions described as data.

    An expression is a small tree of the leaf forms below (:class:`Exp`,
    :class:`Sigmoid`, :class:`ExpLinear`, :class:`Sech`, :class:`Hill`,
    :class:`Const`) joined by arithmetic (:class:`Add`, :class:`Sub`,
    :class:`Mul`, :class:`Div`, :class:`Max`).  Every node is a frozen
    dataclass and a callable :class:`Rate`, so an expression can be used
    anywhere a rate function can.  ``+ - * /`` build combinator nodes and wrap
    plain numbers in :class:`Const`, so kinetics read like their equations::

        tau_h = 1.0 / (Exp(0.128, -38.0, -18.0) + Sigmoid(4.0, -15.0, 5.0))

    An operand can also be a :class:`VoltageOnlyFn` or
    :class:`CalciumDependentFn`, so a term no form covers can be combined
    with the standard forms.

    :attr:`uses_calcium` is derived from the tree: an expression reads ca_i
    exactly when it contains a :class:`Hill` node or a rate (such as a
    :class:`CalciumDependentFn`) that reports ``uses_calcium``.  Because the
    structure is plain data, expressions compare by value, pickle for
    :func:`~neuroclamp.clamp_simulations.simulate_batch`, and can be inspected
    or translated by other tools.
    """

    def children(self) -> tuple[Rate, ...]:
        """Return this node's direct operands.

        Returns:
            The child rates; empty for leaf forms.
        """
        return ()

    @abstractmethod
    def evaluate(self, V: float, ca_i: float) -> float:
        """Evaluate this node directly from its definition.

        This is the readable reference implementation of each form.  Calling
        the expression runs an equivalent compiled function instead (see
        :meth:`__call__`), which performs the same operations in the same
        order and so returns identical results.

        Args:
            V: Membrane voltage in mV.
            ca_i: Intracellular Ca²⁺ concentration in mM.

        Returns:
            The value of the expression.
        """

    def __call__(self, V: float, ca_i: float) -> float:
        """Evaluate the expression through its compiled function.

        On first use the whole tree is translated into one flat Python
        function, cached on this instance.  Walking the tree node by node
        costs a Python call per node; the compiled function evaluates the
        same arithmetic with none of that overhead, so expressions run as
        fast as hand-written rate functions.

        Args:
            V: Membrane voltage in mV.
            ca_i: Intracellular Ca²⁺ concentration in mM.

        Returns:
            The value of the expression.
        """
        compiled = self.__dict__.get("_compiled")
        if compiled is None:
            compiled = _compile(self)
            object.__setattr__(self, "_compiled", compiled)
        return compiled(V, ca_i)

    def __getstate__(self) -> dict[str, object]:
        """Return the pickled state, without the compiled-function cache.

        The cached function cannot be pickled; it is rebuilt on first use
        after unpickling.

        Returns:
            The instance dictionary minus the cache.
        """
        state = dict(self.__dict__)
        state.pop("_compiled", None)
        return state

    def _source(self, emit: "_Emitter") -> str:
        """Return Python source evaluating this node, for :func:`_compile`.

        The built-in forms override this with inline arithmetic equivalent
        to their :meth:`evaluate`.  A node whose class overrides ``evaluate``
        without also overriding ``_source`` is not inlined: the compiled
        function calls its ``evaluate`` instead (see :meth:`_Emitter.source`),
        so an ``Expr`` subclass defined outside this module, or a subclass of
        a built-in form, only needs ``evaluate``.  The default here does the
        same.

        Args:
            emit: Collects the names the generated source refers to.

        Returns:
            A Python expression in ``V`` and ``ca_i``.
        """
        return f"{emit.rate(self.evaluate)}(V, ca_i)"

    @property
    def uses_calcium(self) -> bool:
        """Whether any node in this expression reads ca_i.

        Returns:
            ``True`` if a child (or this node) depends on ca_i.
        """
        return any(child.uses_calcium for child in self.children())

    def __add__(self, other: object) -> "Expr":
        """Build ``self + other``.

        Args:
            other: Right operand: a :class:`Rate` or a number, which becomes
                a :class:`Const`.

        Returns:
            An :class:`Add` node, or ``NotImplemented`` for any other operand type.
        """
        o = _coerce(other)
        if o is None:
            return NotImplemented
        return Add(self, o)

    def __radd__(self, other: object) -> "Expr":
        """Build ``other + self``.

        Args:
            other: Left operand: a :class:`Rate` or a number, which becomes
                a :class:`Const`.

        Returns:
            An :class:`Add` node, or ``NotImplemented`` for any other operand type.
        """
        o = _coerce(other)
        if o is None:
            return NotImplemented
        return Add(o, self)

    def __sub__(self, other: object) -> "Expr":
        """Build ``self - other``.

        Args:
            other: Right operand: a :class:`Rate` or a number, which becomes
                a :class:`Const`.

        Returns:
            A :class:`Sub` node, or ``NotImplemented`` for any other operand type.
        """
        o = _coerce(other)
        if o is None:
            return NotImplemented
        return Sub(self, o)

    def __rsub__(self, other: object) -> "Expr":
        """Build ``other - self``.

        Args:
            other: Left operand: a :class:`Rate` or a number, which becomes
                a :class:`Const`.

        Returns:
            A :class:`Sub` node, or ``NotImplemented`` for any other operand type.
        """
        o = _coerce(other)
        if o is None:
            return NotImplemented
        return Sub(o, self)

    def __mul__(self, other: object) -> "Expr":
        """Build ``self * other``.

        Args:
            other: Right operand: a :class:`Rate` or a number, which becomes
                a :class:`Const`.

        Returns:
            A :class:`Mul` node, or ``NotImplemented`` for any other operand type.
        """
        o = _coerce(other)
        if o is None:
            return NotImplemented
        return Mul(self, o)

    def __rmul__(self, other: object) -> "Expr":
        """Build ``other * self``.

        Args:
            other: Left operand: a :class:`Rate` or a number, which becomes
                a :class:`Const`.

        Returns:
            A :class:`Mul` node, or ``NotImplemented`` for any other operand type.
        """
        o = _coerce(other)
        if o is None:
            return NotImplemented
        return Mul(o, self)

    def __truediv__(self, other: object) -> "Expr":
        """Build ``self / other``.

        Args:
            other: Right operand: a :class:`Rate` or a number, which becomes
                a :class:`Const`.

        Returns:
            A :class:`Div` node, or ``NotImplemented`` for any other operand type.
        """
        o = _coerce(other)
        if o is None:
            return NotImplemented
        return Div(self, o)

    def __rtruediv__(self, other: object) -> "Expr":
        """Build ``other / self``.

        Args:
            other: Left operand: a :class:`Rate` or a number, which becomes
                a :class:`Const`.

        Returns:
            A :class:`Div` node, or ``NotImplemented`` for any other operand type.
        """
        o = _coerce(other)
        if o is None:
            return NotImplemented
        return Div(o, self)

uses_calcium property

uses_calcium

Whether any node in this expression reads ca_i.

Returns:

Type Description
bool

True if a child (or this node) depends on ca_i.

children

children()

Return this node's direct operands.

Returns:

Type Description
tuple[Rate, ...]

The child rates; empty for leaf forms.

Source code in neuroclamp/rates.py
188
189
190
191
192
193
194
def children(self) -> tuple[Rate, ...]:
    """Return this node's direct operands.

    Returns:
        The child rates; empty for leaf forms.
    """
    return ()

evaluate abstractmethod

evaluate(V, ca_i)

Evaluate this node directly from its definition.

This is the readable reference implementation of each form. Calling the expression runs an equivalent compiled function instead (see :meth:__call__), which performs the same operations in the same order and so returns identical results.

Parameters:

Name Type Description Default
V float

Membrane voltage in mV.

required
ca_i float

Intracellular Ca²⁺ concentration in mM.

required

Returns:

Type Description
float

The value of the expression.

Source code in neuroclamp/rates.py
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
@abstractmethod
def evaluate(self, V: float, ca_i: float) -> float:
    """Evaluate this node directly from its definition.

    This is the readable reference implementation of each form.  Calling
    the expression runs an equivalent compiled function instead (see
    :meth:`__call__`), which performs the same operations in the same
    order and so returns identical results.

    Args:
        V: Membrane voltage in mV.
        ca_i: Intracellular Ca²⁺ concentration in mM.

    Returns:
        The value of the expression.
    """

__call__

__call__(V, ca_i)

Evaluate the expression through its compiled function.

On first use the whole tree is translated into one flat Python function, cached on this instance. Walking the tree node by node costs a Python call per node; the compiled function evaluates the same arithmetic with none of that overhead, so expressions run as fast as hand-written rate functions.

Parameters:

Name Type Description Default
V float

Membrane voltage in mV.

required
ca_i float

Intracellular Ca²⁺ concentration in mM.

required

Returns:

Type Description
float

The value of the expression.

Source code in neuroclamp/rates.py
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
def __call__(self, V: float, ca_i: float) -> float:
    """Evaluate the expression through its compiled function.

    On first use the whole tree is translated into one flat Python
    function, cached on this instance.  Walking the tree node by node
    costs a Python call per node; the compiled function evaluates the
    same arithmetic with none of that overhead, so expressions run as
    fast as hand-written rate functions.

    Args:
        V: Membrane voltage in mV.
        ca_i: Intracellular Ca²⁺ concentration in mM.

    Returns:
        The value of the expression.
    """
    compiled = self.__dict__.get("_compiled")
    if compiled is None:
        compiled = _compile(self)
        object.__setattr__(self, "_compiled", compiled)
    return compiled(V, ca_i)

__getstate__

__getstate__()

Return the pickled state, without the compiled-function cache.

The cached function cannot be pickled; it is rebuilt on first use after unpickling.

Returns:

Type Description
dict[str, object]

The instance dictionary minus the cache.

Source code in neuroclamp/rates.py
235
236
237
238
239
240
241
242
243
244
245
246
def __getstate__(self) -> dict[str, object]:
    """Return the pickled state, without the compiled-function cache.

    The cached function cannot be pickled; it is rebuilt on first use
    after unpickling.

    Returns:
        The instance dictionary minus the cache.
    """
    state = dict(self.__dict__)
    state.pop("_compiled", None)
    return state

__add__

__add__(other)

Build self + other.

Parameters:

Name Type Description Default
other object

Right operand: a :class:Rate or a number, which becomes a :class:Const.

required

Returns:

Name Type Description
An Expr

class:Add node, or NotImplemented for any other operand type.

Source code in neuroclamp/rates.py
276
277
278
279
280
281
282
283
284
285
286
287
288
289
def __add__(self, other: object) -> "Expr":
    """Build ``self + other``.

    Args:
        other: Right operand: a :class:`Rate` or a number, which becomes
            a :class:`Const`.

    Returns:
        An :class:`Add` node, or ``NotImplemented`` for any other operand type.
    """
    o = _coerce(other)
    if o is None:
        return NotImplemented
    return Add(self, o)

__radd__

__radd__(other)

Build other + self.

Parameters:

Name Type Description Default
other object

Left operand: a :class:Rate or a number, which becomes a :class:Const.

required

Returns:

Name Type Description
An Expr

class:Add node, or NotImplemented for any other operand type.

Source code in neuroclamp/rates.py
291
292
293
294
295
296
297
298
299
300
301
302
303
304
def __radd__(self, other: object) -> "Expr":
    """Build ``other + self``.

    Args:
        other: Left operand: a :class:`Rate` or a number, which becomes
            a :class:`Const`.

    Returns:
        An :class:`Add` node, or ``NotImplemented`` for any other operand type.
    """
    o = _coerce(other)
    if o is None:
        return NotImplemented
    return Add(o, self)

__sub__

__sub__(other)

Build self - other.

Parameters:

Name Type Description Default
other object

Right operand: a :class:Rate or a number, which becomes a :class:Const.

required

Returns:

Name Type Description
A Expr

class:Sub node, or NotImplemented for any other operand type.

Source code in neuroclamp/rates.py
306
307
308
309
310
311
312
313
314
315
316
317
318
319
def __sub__(self, other: object) -> "Expr":
    """Build ``self - other``.

    Args:
        other: Right operand: a :class:`Rate` or a number, which becomes
            a :class:`Const`.

    Returns:
        A :class:`Sub` node, or ``NotImplemented`` for any other operand type.
    """
    o = _coerce(other)
    if o is None:
        return NotImplemented
    return Sub(self, o)

__rsub__

__rsub__(other)

Build other - self.

Parameters:

Name Type Description Default
other object

Left operand: a :class:Rate or a number, which becomes a :class:Const.

required

Returns:

Name Type Description
A Expr

class:Sub node, or NotImplemented for any other operand type.

Source code in neuroclamp/rates.py
321
322
323
324
325
326
327
328
329
330
331
332
333
334
def __rsub__(self, other: object) -> "Expr":
    """Build ``other - self``.

    Args:
        other: Left operand: a :class:`Rate` or a number, which becomes
            a :class:`Const`.

    Returns:
        A :class:`Sub` node, or ``NotImplemented`` for any other operand type.
    """
    o = _coerce(other)
    if o is None:
        return NotImplemented
    return Sub(o, self)

__mul__

__mul__(other)

Build self * other.

Parameters:

Name Type Description Default
other object

Right operand: a :class:Rate or a number, which becomes a :class:Const.

required

Returns:

Name Type Description
A Expr

class:Mul node, or NotImplemented for any other operand type.

Source code in neuroclamp/rates.py
336
337
338
339
340
341
342
343
344
345
346
347
348
349
def __mul__(self, other: object) -> "Expr":
    """Build ``self * other``.

    Args:
        other: Right operand: a :class:`Rate` or a number, which becomes
            a :class:`Const`.

    Returns:
        A :class:`Mul` node, or ``NotImplemented`` for any other operand type.
    """
    o = _coerce(other)
    if o is None:
        return NotImplemented
    return Mul(self, o)

__rmul__

__rmul__(other)

Build other * self.

Parameters:

Name Type Description Default
other object

Left operand: a :class:Rate or a number, which becomes a :class:Const.

required

Returns:

Name Type Description
A Expr

class:Mul node, or NotImplemented for any other operand type.

Source code in neuroclamp/rates.py
351
352
353
354
355
356
357
358
359
360
361
362
363
364
def __rmul__(self, other: object) -> "Expr":
    """Build ``other * self``.

    Args:
        other: Left operand: a :class:`Rate` or a number, which becomes
            a :class:`Const`.

    Returns:
        A :class:`Mul` node, or ``NotImplemented`` for any other operand type.
    """
    o = _coerce(other)
    if o is None:
        return NotImplemented
    return Mul(o, self)

__truediv__

__truediv__(other)

Build self / other.

Parameters:

Name Type Description Default
other object

Right operand: a :class:Rate or a number, which becomes a :class:Const.

required

Returns:

Name Type Description
A Expr

class:Div node, or NotImplemented for any other operand type.

Source code in neuroclamp/rates.py
366
367
368
369
370
371
372
373
374
375
376
377
378
379
def __truediv__(self, other: object) -> "Expr":
    """Build ``self / other``.

    Args:
        other: Right operand: a :class:`Rate` or a number, which becomes
            a :class:`Const`.

    Returns:
        A :class:`Div` node, or ``NotImplemented`` for any other operand type.
    """
    o = _coerce(other)
    if o is None:
        return NotImplemented
    return Div(self, o)

__rtruediv__

__rtruediv__(other)

Build other / self.

Parameters:

Name Type Description Default
other object

Left operand: a :class:Rate or a number, which becomes a :class:Const.

required

Returns:

Name Type Description
A Expr

class:Div node, or NotImplemented for any other operand type.

Source code in neuroclamp/rates.py
381
382
383
384
385
386
387
388
389
390
391
392
393
394
def __rtruediv__(self, other: object) -> "Expr":
    """Build ``other / self``.

    Args:
        other: Left operand: a :class:`Rate` or a number, which becomes
            a :class:`Const`.

    Returns:
        A :class:`Div` node, or ``NotImplemented`` for any other operand type.
    """
    o = _coerce(other)
    if o is None:
        return NotImplemented
    return Div(o, self)

Const dataclass

Bases: Expr

A constant value.

Attributes:

Name Type Description
value float

The constant, in the units of the surrounding expression.

Source code in neuroclamp/rates.py
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
@dataclass(frozen=True)
class Const(Expr):
    """A constant value.

    Attributes:
        value: The constant, in the units of the surrounding expression.
    """

    value: float

    def evaluate(self, V: float, ca_i: float) -> float:
        """Return the constant.

        Args:
            V: Membrane voltage in mV (ignored).
            ca_i: Intracellular Ca²⁺ concentration in mM (ignored).

        Returns:
            :attr:`value`.
        """
        return self.value

    def _source(self, emit: "_Emitter") -> str:
        """Return inline source for the constant.

        Args:
            emit: Collects the names the generated source refers to.

        Returns:
            A Python expression in ``V`` and ``ca_i``.
        """
        return emit.const(self.value)

evaluate

evaluate(V, ca_i)

Return the constant.

Parameters:

Name Type Description Default
V float

Membrane voltage in mV (ignored).

required
ca_i float

Intracellular Ca²⁺ concentration in mM (ignored).

required

Returns:

Type Description
float

attr:value.

Source code in neuroclamp/rates.py
632
633
634
635
636
637
638
639
640
641
642
def evaluate(self, V: float, ca_i: float) -> float:
    """Return the constant.

    Args:
        V: Membrane voltage in mV (ignored).
        ca_i: Intracellular Ca²⁺ concentration in mM (ignored).

    Returns:
        :attr:`value`.
    """
    return self.value

Exp dataclass

Bases: Expr

Exponential in voltage: k · exp((V − vh) / s).

Attributes:

Name Type Description
k float

Amplitude (the value at V = vh).

vh float

Reference voltage in mV.

s float

Voltage scale in mV; negative for a rate that falls with depolarization.

Source code in neuroclamp/rates.py
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
@dataclass(frozen=True)
class Exp(Expr):
    """Exponential in voltage: ``k · exp((V − vh) / s)``.

    Attributes:
        k: Amplitude (the value at ``V = vh``).
        vh: Reference voltage in mV.
        s: Voltage scale in mV; negative for a rate that falls with
            depolarization.
    """

    k: float
    vh: float
    s: float

    def __post_init__(self) -> None:
        """Reject a zero voltage scale, which would divide by zero.

        Raises:
            ValueError: If ``s`` is zero.
        """
        _check_scale(self.s)

    def evaluate(self, V: float, ca_i: float) -> float:
        """Evaluate the exponential (argument clipped by ``safe_exp``).

        Args:
            V: Membrane voltage in mV.
            ca_i: Intracellular Ca²⁺ concentration in mM (ignored).

        Returns:
            The value at ``V``.
        """
        return self.k * safe_exp((V - self.vh) / self.s)

    def _source(self, emit: "_Emitter") -> str:
        """Return inline source for the exponential.

        Args:
            emit: Collects the names the generated source refers to.

        Returns:
            A Python expression in ``V`` and ``ca_i``.
        """
        k, vh, s = emit.const(self.k), emit.const(self.vh), emit.const(self.s)
        return f"({k} * _safe_exp((V - {vh}) / {s}))"

__post_init__

__post_init__()

Reject a zero voltage scale, which would divide by zero.

Raises:

Type Description
ValueError

If s is zero.

Source code in neuroclamp/rates.py
671
672
673
674
675
676
677
def __post_init__(self) -> None:
    """Reject a zero voltage scale, which would divide by zero.

    Raises:
        ValueError: If ``s`` is zero.
    """
    _check_scale(self.s)

evaluate

evaluate(V, ca_i)

Evaluate the exponential (argument clipped by safe_exp).

Parameters:

Name Type Description Default
V float

Membrane voltage in mV.

required
ca_i float

Intracellular Ca²⁺ concentration in mM (ignored).

required

Returns:

Type Description
float

The value at V.

Source code in neuroclamp/rates.py
679
680
681
682
683
684
685
686
687
688
689
def evaluate(self, V: float, ca_i: float) -> float:
    """Evaluate the exponential (argument clipped by ``safe_exp``).

    Args:
        V: Membrane voltage in mV.
        ca_i: Intracellular Ca²⁺ concentration in mM (ignored).

    Returns:
        The value at ``V``.
    """
    return self.k * safe_exp((V - self.vh) / self.s)

Sigmoid dataclass

Bases: Expr

Boltzmann sigmoid: k / (1 + exp(−(V − vh) / s)).

With k = 1 this is a steady-state activation curve; a negative s gives an inactivation (decreasing) curve.

Attributes:

Name Type Description
k float

Maximum value.

vh float

Half-point voltage in mV.

s float

Slope factor in mV.

Source code in neuroclamp/rates.py
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
@dataclass(frozen=True)
class Sigmoid(Expr):
    """Boltzmann sigmoid: ``k / (1 + exp(−(V − vh) / s))``.

    With ``k = 1`` this is a steady-state activation curve; a negative ``s``
    gives an inactivation (decreasing) curve.

    Attributes:
        k: Maximum value.
        vh: Half-point voltage in mV.
        s: Slope factor in mV.
    """

    k: float
    vh: float
    s: float

    def __post_init__(self) -> None:
        """Reject a zero voltage scale, which would divide by zero.

        Raises:
            ValueError: If ``s`` is zero.
        """
        _check_scale(self.s)

    def evaluate(self, V: float, ca_i: float) -> float:
        """Evaluate the sigmoid (argument clipped by ``safe_exp``).

        Args:
            V: Membrane voltage in mV.
            ca_i: Intracellular Ca²⁺ concentration in mM (ignored).

        Returns:
            The value at ``V``.
        """
        return self.k / (1.0 + safe_exp(-(V - self.vh) / self.s))

    def _source(self, emit: "_Emitter") -> str:
        """Return inline source for the sigmoid.

        Args:
            emit: Collects the names the generated source refers to.

        Returns:
            A Python expression in ``V`` and ``ca_i``.
        """
        k, vh, s = emit.const(self.k), emit.const(self.vh), emit.const(self.s)
        return f"({k} / (1.0 + _safe_exp(-(V - {vh}) / {s})))"

__post_init__

__post_init__()

Reject a zero voltage scale, which would divide by zero.

Raises:

Type Description
ValueError

If s is zero.

Source code in neuroclamp/rates.py
721
722
723
724
725
726
727
def __post_init__(self) -> None:
    """Reject a zero voltage scale, which would divide by zero.

    Raises:
        ValueError: If ``s`` is zero.
    """
    _check_scale(self.s)

evaluate

evaluate(V, ca_i)

Evaluate the sigmoid (argument clipped by safe_exp).

Parameters:

Name Type Description Default
V float

Membrane voltage in mV.

required
ca_i float

Intracellular Ca²⁺ concentration in mM (ignored).

required

Returns:

Type Description
float

The value at V.

Source code in neuroclamp/rates.py
729
730
731
732
733
734
735
736
737
738
739
def evaluate(self, V: float, ca_i: float) -> float:
    """Evaluate the sigmoid (argument clipped by ``safe_exp``).

    Args:
        V: Membrane voltage in mV.
        ca_i: Intracellular Ca²⁺ concentration in mM (ignored).

    Returns:
        The value at ``V``.
    """
    return self.k / (1.0 + safe_exp(-(V - self.vh) / self.s))

ExpLinear dataclass

Bases: Expr

Exp-linear (linoid) form: k · (V − vh) / (1 − exp(−(V − vh) / s)).

The classic Hodgkin-Huxley activation rate. It has a removable singularity at V = vh, where its analytic limit k · s is returned when |V − vh| < tol. A rate written as k′ · (V − vh) / (exp((V − vh) / s′) − 1) is ExpLinear(-k′, vh, -s′): negating s alone flips the sign of the whole rate, so k must be negated with it.

Attributes:

Name Type Description
k float

Slope of the linear asymptote in 1/(ms·mV).

vh float

Singular-point voltage in mV.

s float

Voltage scale in mV.

tol float

Distance from vh within which the limit is returned.

Source code in neuroclamp/rates.py
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
786
787
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
@dataclass(frozen=True)
class ExpLinear(Expr):
    """Exp-linear (linoid) form: ``k · (V − vh) / (1 − exp(−(V − vh) / s))``.

    The classic Hodgkin-Huxley activation rate.  It has a removable
    singularity at ``V = vh``, where its analytic limit ``k · s`` is returned
    when ``|V − vh| < tol``.  A rate written as
    ``k′ · (V − vh) / (exp((V − vh) / s′) − 1)`` is ``ExpLinear(-k′, vh, -s′)``:
    negating ``s`` alone flips the sign of the whole rate, so ``k`` must be
    negated with it.

    Attributes:
        k: Slope of the linear asymptote in 1/(ms·mV).
        vh: Singular-point voltage in mV.
        s: Voltage scale in mV.
        tol: Distance from ``vh`` within which the limit is returned.
    """

    k: float
    vh: float
    s: float
    tol: float = SINGULARITY_THRESHOLD

    def __post_init__(self) -> None:
        """Reject a zero voltage scale, which would divide by zero.

        Raises:
            ValueError: If ``s`` is zero.
        """
        _check_scale(self.s)

    def evaluate(self, V: float, ca_i: float) -> float:
        """Evaluate the exp-linear form.

        Args:
            V: Membrane voltage in mV.
            ca_i: Intracellular Ca²⁺ concentration in mM (ignored).

        Returns:
            The value at ``V``.
        """
        x = V - self.vh
        if abs(x) < self.tol:
            return self.k * self.s
        return self.k * x / (1.0 - safe_exp(-x / self.s))

    def _source(self, emit: "_Emitter") -> str:
        """Return inline source for the exp-linear form.

        Args:
            emit: Collects the names the generated source refers to.

        Returns:
            A Python expression in ``V`` and ``ca_i``.
        """
        k, vh, s = emit.const(self.k), emit.const(self.vh), emit.const(self.s)
        tol, x = emit.const(self.tol), emit.temp()
        # The condition runs first, so it binds x before either branch uses it.
        return (
            f"({k} * {s} if abs({x} := V - {vh}) < {tol}"
            f" else {k} * {x} / (1.0 - _safe_exp(-{x} / {s})))"
        )

__post_init__

__post_init__()

Reject a zero voltage scale, which would divide by zero.

Raises:

Type Description
ValueError

If s is zero.

Source code in neuroclamp/rates.py
777
778
779
780
781
782
783
def __post_init__(self) -> None:
    """Reject a zero voltage scale, which would divide by zero.

    Raises:
        ValueError: If ``s`` is zero.
    """
    _check_scale(self.s)

evaluate

evaluate(V, ca_i)

Evaluate the exp-linear form.

Parameters:

Name Type Description Default
V float

Membrane voltage in mV.

required
ca_i float

Intracellular Ca²⁺ concentration in mM (ignored).

required

Returns:

Type Description
float

The value at V.

Source code in neuroclamp/rates.py
785
786
787
788
789
790
791
792
793
794
795
796
797
798
def evaluate(self, V: float, ca_i: float) -> float:
    """Evaluate the exp-linear form.

    Args:
        V: Membrane voltage in mV.
        ca_i: Intracellular Ca²⁺ concentration in mM (ignored).

    Returns:
        The value at ``V``.
    """
    x = V - self.vh
    if abs(x) < self.tol:
        return self.k * self.s
    return self.k * x / (1.0 - safe_exp(-x / self.s))

Sech dataclass

Bases: Expr

Hyperbolic secant: k / cosh((V − vh) / s).

The bell-shaped voltage dependence used for gating time constants.

Attributes:

Name Type Description
k float

Peak value (at V = vh).

vh float

Center voltage in mV.

s float

Voltage scale in mV.

Source code in neuroclamp/rates.py
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
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
@dataclass(frozen=True)
class Sech(Expr):
    """Hyperbolic secant: ``k / cosh((V − vh) / s)``.

    The bell-shaped voltage dependence used for gating time constants.

    Attributes:
        k: Peak value (at ``V = vh``).
        vh: Center voltage in mV.
        s: Voltage scale in mV.
    """

    k: float
    vh: float
    s: float

    def __post_init__(self) -> None:
        """Reject a zero voltage scale, which would divide by zero.

        Raises:
            ValueError: If ``s`` is zero.
        """
        _check_scale(self.s)

    def evaluate(self, V: float, ca_i: float) -> float:
        """Evaluate the secant (argument clipped by ``safe_cosh``).

        Args:
            V: Membrane voltage in mV.
            ca_i: Intracellular Ca²⁺ concentration in mM (ignored).

        Returns:
            The value at ``V``.
        """
        return self.k / safe_cosh((V - self.vh) / self.s)

    def _source(self, emit: "_Emitter") -> str:
        """Return inline source for the secant.

        Args:
            emit: Collects the names the generated source refers to.

        Returns:
            A Python expression in ``V`` and ``ca_i``.
        """
        k, vh, s = emit.const(self.k), emit.const(self.vh), emit.const(self.s)
        return f"({k} / _safe_cosh((V - {vh}) / {s}))"

__post_init__

__post_init__()

Reject a zero voltage scale, which would divide by zero.

Raises:

Type Description
ValueError

If s is zero.

Source code in neuroclamp/rates.py
834
835
836
837
838
839
840
def __post_init__(self) -> None:
    """Reject a zero voltage scale, which would divide by zero.

    Raises:
        ValueError: If ``s`` is zero.
    """
    _check_scale(self.s)

evaluate

evaluate(V, ca_i)

Evaluate the secant (argument clipped by safe_cosh).

Parameters:

Name Type Description Default
V float

Membrane voltage in mV.

required
ca_i float

Intracellular Ca²⁺ concentration in mM (ignored).

required

Returns:

Type Description
float

The value at V.

Source code in neuroclamp/rates.py
842
843
844
845
846
847
848
849
850
851
852
def evaluate(self, V: float, ca_i: float) -> float:
    """Evaluate the secant (argument clipped by ``safe_cosh``).

    Args:
        V: Membrane voltage in mV.
        ca_i: Intracellular Ca²⁺ concentration in mM (ignored).

    Returns:
        The value at ``V``.
    """
    return self.k / safe_cosh((V - self.vh) / self.s)

Hill dataclass

Bases: Expr

Hill function of intracellular Ca²⁺: caⁿ / (caⁿ + kdⁿ).

The only node that reads ca_i, so any expression containing it reports :attr:uses_calcium as True.

Attributes:

Name Type Description
kd float

Half-activation concentration in mM.

n int

Hill coefficient.

Source code in neuroclamp/rates.py
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
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
922
923
924
925
926
927
@dataclass(frozen=True)
class Hill(Expr):
    """Hill function of intracellular Ca²⁺: ``caⁿ / (caⁿ + kdⁿ)``.

    The only node that reads ca_i, so any expression containing it reports
    :attr:`uses_calcium` as ``True``.

    Attributes:
        kd: Half-activation concentration in mM.
        n: Hill coefficient.
    """

    kd: float
    n: int

    def __post_init__(self) -> None:
        """Reject parameters that make the Hill function undefined.

        Raises:
            ValueError: If ``kd`` or ``n`` is not positive.
        """
        if not self.kd > 0.0:
            raise ValueError(f"Hill kd must be positive, got {self.kd}")
        if not self.n > 0:
            raise ValueError(f"Hill coefficient n must be positive, got {self.n}")

    @property
    def uses_calcium(self) -> bool:
        """Report that this node reads ca_i.

        Returns:
            Always ``True``.
        """
        return True

    def evaluate(self, V: float, ca_i: float) -> float:
        """Evaluate the Hill function.

        Args:
            V: Membrane voltage in mV (ignored).
            ca_i: Intracellular Ca²⁺ concentration in mM.

        Returns:
            Fractional activation in [0, 1].
        """
        ca_n = ca_i**self.n
        return ca_n / (ca_n + self.kd**self.n)

    def _source(self, emit: "_Emitter") -> str:
        """Return inline source for the Hill function.

        Args:
            emit: Collects the names the generated source refers to.

        Returns:
            A Python expression in ``V`` and ``ca_i``.
        """
        n, ca_n = emit.const(self.n), emit.temp()
        # kd**n is the same constant on every call, so it is computed once.
        kd_n = emit.const(self.kd**self.n)
        return f"(({ca_n} := ca_i ** {n}) / ({ca_n} + {kd_n}))"

uses_calcium property

uses_calcium

Report that this node reads ca_i.

Returns:

Type Description
bool

Always True.

__post_init__

__post_init__()

Reject parameters that make the Hill function undefined.

Raises:

Type Description
ValueError

If kd or n is not positive.

Source code in neuroclamp/rates.py
882
883
884
885
886
887
888
889
890
891
def __post_init__(self) -> None:
    """Reject parameters that make the Hill function undefined.

    Raises:
        ValueError: If ``kd`` or ``n`` is not positive.
    """
    if not self.kd > 0.0:
        raise ValueError(f"Hill kd must be positive, got {self.kd}")
    if not self.n > 0:
        raise ValueError(f"Hill coefficient n must be positive, got {self.n}")

evaluate

evaluate(V, ca_i)

Evaluate the Hill function.

Parameters:

Name Type Description Default
V float

Membrane voltage in mV (ignored).

required
ca_i float

Intracellular Ca²⁺ concentration in mM.

required

Returns:

Type Description
float

Fractional activation in [0, 1].

Source code in neuroclamp/rates.py
902
903
904
905
906
907
908
909
910
911
912
913
def evaluate(self, V: float, ca_i: float) -> float:
    """Evaluate the Hill function.

    Args:
        V: Membrane voltage in mV (ignored).
        ca_i: Intracellular Ca²⁺ concentration in mM.

    Returns:
        Fractional activation in [0, 1].
    """
    ca_n = ca_i**self.n
    return ca_n / (ca_n + self.kd**self.n)

Add dataclass

Bases: _Binary

Sum of two expressions: a + b.

Source code in neuroclamp/rates.py
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
@dataclass(frozen=True)
class Add(_Binary):
    """Sum of two expressions: ``a + b``."""

    def evaluate(self, V: float, ca_i: float) -> float:
        """Evaluate ``a + b``.

        Args:
            V: Membrane voltage in mV.
            ca_i: Intracellular Ca²⁺ concentration in mM.

        Returns:
            The sum.
        """
        return _ref(self.a, V, ca_i) + _ref(self.b, V, ca_i)

    def _source(self, emit: "_Emitter") -> str:
        """Return inline source for ``a + b``.

        Args:
            emit: Collects the names the generated source refers to.

        Returns:
            A Python expression in ``V`` and ``ca_i``.
        """
        return f"({emit.source(self.a)} + {emit.source(self.b)})"

evaluate

evaluate(V, ca_i)

Evaluate a + b.

Parameters:

Name Type Description Default
V float

Membrane voltage in mV.

required
ca_i float

Intracellular Ca²⁺ concentration in mM.

required

Returns:

Type Description
float

The sum.

Source code in neuroclamp/rates.py
955
956
957
958
959
960
961
962
963
964
965
def evaluate(self, V: float, ca_i: float) -> float:
    """Evaluate ``a + b``.

    Args:
        V: Membrane voltage in mV.
        ca_i: Intracellular Ca²⁺ concentration in mM.

    Returns:
        The sum.
    """
    return _ref(self.a, V, ca_i) + _ref(self.b, V, ca_i)

Sub dataclass

Bases: _Binary

Difference of two expressions: a − b.

Source code in neuroclamp/rates.py
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
@dataclass(frozen=True)
class Sub(_Binary):
    """Difference of two expressions: ``a − b``."""

    def evaluate(self, V: float, ca_i: float) -> float:
        """Evaluate ``a − b``.

        Args:
            V: Membrane voltage in mV.
            ca_i: Intracellular Ca²⁺ concentration in mM.

        Returns:
            The difference.
        """
        return _ref(self.a, V, ca_i) - _ref(self.b, V, ca_i)

    def _source(self, emit: "_Emitter") -> str:
        """Return inline source for ``a − b``.

        Args:
            emit: Collects the names the generated source refers to.

        Returns:
            A Python expression in ``V`` and ``ca_i``.
        """
        return f"({emit.source(self.a)} - {emit.source(self.b)})"

evaluate

evaluate(V, ca_i)

Evaluate a − b.

Parameters:

Name Type Description Default
V float

Membrane voltage in mV.

required
ca_i float

Intracellular Ca²⁺ concentration in mM.

required

Returns:

Type Description
float

The difference.

Source code in neuroclamp/rates.py
983
984
985
986
987
988
989
990
991
992
993
def evaluate(self, V: float, ca_i: float) -> float:
    """Evaluate ``a − b``.

    Args:
        V: Membrane voltage in mV.
        ca_i: Intracellular Ca²⁺ concentration in mM.

    Returns:
        The difference.
    """
    return _ref(self.a, V, ca_i) - _ref(self.b, V, ca_i)

Mul dataclass

Bases: _Binary

Product of two expressions: a · b.

Source code in neuroclamp/rates.py
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
@dataclass(frozen=True)
class Mul(_Binary):
    """Product of two expressions: ``a · b``."""

    def evaluate(self, V: float, ca_i: float) -> float:
        """Evaluate ``a · b``.

        Args:
            V: Membrane voltage in mV.
            ca_i: Intracellular Ca²⁺ concentration in mM.

        Returns:
            The product.
        """
        return _ref(self.a, V, ca_i) * _ref(self.b, V, ca_i)

    def _source(self, emit: "_Emitter") -> str:
        """Return inline source for ``a · b``.

        Args:
            emit: Collects the names the generated source refers to.

        Returns:
            A Python expression in ``V`` and ``ca_i``.
        """
        return f"({emit.source(self.a)} * {emit.source(self.b)})"

evaluate

evaluate(V, ca_i)

Evaluate a · b.

Parameters:

Name Type Description Default
V float

Membrane voltage in mV.

required
ca_i float

Intracellular Ca²⁺ concentration in mM.

required

Returns:

Type Description
float

The product.

Source code in neuroclamp/rates.py
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
def evaluate(self, V: float, ca_i: float) -> float:
    """Evaluate ``a · b``.

    Args:
        V: Membrane voltage in mV.
        ca_i: Intracellular Ca²⁺ concentration in mM.

    Returns:
        The product.
    """
    return _ref(self.a, V, ca_i) * _ref(self.b, V, ca_i)

Div dataclass

Bases: _Binary

Quotient of two expressions: a / b.

Source code in neuroclamp/rates.py
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
@dataclass(frozen=True)
class Div(_Binary):
    """Quotient of two expressions: ``a / b``."""

    def evaluate(self, V: float, ca_i: float) -> float:
        """Evaluate ``a / b``.

        Args:
            V: Membrane voltage in mV.
            ca_i: Intracellular Ca²⁺ concentration in mM.

        Returns:
            The quotient.
        """
        return _ref(self.a, V, ca_i) / _ref(self.b, V, ca_i)

    def _source(self, emit: "_Emitter") -> str:
        """Return inline source for ``a / b``.

        Args:
            emit: Collects the names the generated source refers to.

        Returns:
            A Python expression in ``V`` and ``ca_i``.
        """
        return f"({emit.source(self.a)} / {emit.source(self.b)})"

evaluate

evaluate(V, ca_i)

Evaluate a / b.

Parameters:

Name Type Description Default
V float

Membrane voltage in mV.

required
ca_i float

Intracellular Ca²⁺ concentration in mM.

required

Returns:

Type Description
float

The quotient.

Source code in neuroclamp/rates.py
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
def evaluate(self, V: float, ca_i: float) -> float:
    """Evaluate ``a / b``.

    Args:
        V: Membrane voltage in mV.
        ca_i: Intracellular Ca²⁺ concentration in mM.

    Returns:
        The quotient.
    """
    return _ref(self.a, V, ca_i) / _ref(self.b, V, ca_i)

Max dataclass

Bases: _Binary

Larger of two expressions, e.g. a time constant with a floor.

Source code in neuroclamp/rates.py
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
@dataclass(frozen=True)
class Max(_Binary):
    """Larger of two expressions, e.g. a time constant with a floor."""

    def evaluate(self, V: float, ca_i: float) -> float:
        """Evaluate ``max(a, b)``.

        Args:
            V: Membrane voltage in mV.
            ca_i: Intracellular Ca²⁺ concentration in mM.

        Returns:
            The larger operand.
        """
        return max(_ref(self.a, V, ca_i), _ref(self.b, V, ca_i))

    def _source(self, emit: "_Emitter") -> str:
        """Return inline source for ``max(a, b)``.

        Args:
            emit: Collects the names the generated source refers to.

        Returns:
            A Python expression in ``V`` and ``ca_i``.
        """
        return f"_max({emit.source(self.a)}, {emit.source(self.b)})"

evaluate

evaluate(V, ca_i)

Evaluate max(a, b).

Parameters:

Name Type Description Default
V float

Membrane voltage in mV.

required
ca_i float

Intracellular Ca²⁺ concentration in mM.

required

Returns:

Type Description
float

The larger operand.

Source code in neuroclamp/rates.py
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
def evaluate(self, V: float, ca_i: float) -> float:
    """Evaluate ``max(a, b)``.

    Args:
        V: Membrane voltage in mV.
        ca_i: Intracellular Ca²⁺ concentration in mM.

    Returns:
        The larger operand.
    """
    return max(_ref(self.a, V, ca_i), _ref(self.b, V, ca_i))

InfTauAlpha dataclass

Bases: _Binary

Forward rate from a steady state and time constant: inf / tau.

Built by :func:inf_tau. Paired with :class:InfTauBeta on the same inf and tau, the gate relaxes toward inf with time constant tau. The explicit node type keeps that structure visible to tools that translate expressions, which can evaluate inf and tau once for both rates.

Attributes:

Name Type Description
a Rate

Steady-state open probability inf (dimensionless).

b Rate

Time constant tau in ms.

Source code in neuroclamp/rates.py
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
@dataclass(frozen=True)
class InfTauAlpha(_Binary):
    """Forward rate from a steady state and time constant: ``inf / tau``.

    Built by :func:`inf_tau`.  Paired with :class:`InfTauBeta` on the same
    ``inf`` and ``tau``, the gate relaxes toward ``inf`` with time constant
    ``tau``.  The explicit node type keeps that structure visible to tools
    that translate expressions, which can evaluate ``inf`` and ``tau`` once
    for both rates.

    Attributes:
        a: Steady-state open probability ``inf`` (dimensionless).
        b: Time constant ``tau`` in ms.
    """

    def evaluate(self, V: float, ca_i: float) -> float:
        """Evaluate ``inf / tau``.

        Args:
            V: Membrane voltage in mV.
            ca_i: Intracellular Ca²⁺ concentration in mM.

        Returns:
            The forward rate in 1/ms.
        """
        return _ref(self.a, V, ca_i) / _ref(self.b, V, ca_i)

    def _source(self, emit: "_Emitter") -> str:
        """Return inline source for ``inf / tau``.

        Args:
            emit: Collects the names the generated source refers to.

        Returns:
            A Python expression in ``V`` and ``ca_i``.
        """
        return f"({emit.source(self.a)} / {emit.source(self.b)})"

evaluate

evaluate(V, ca_i)

Evaluate inf / tau.

Parameters:

Name Type Description Default
V float

Membrane voltage in mV.

required
ca_i float

Intracellular Ca²⁺ concentration in mM.

required

Returns:

Type Description
float

The forward rate in 1/ms.

Source code in neuroclamp/rates.py
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
def evaluate(self, V: float, ca_i: float) -> float:
    """Evaluate ``inf / tau``.

    Args:
        V: Membrane voltage in mV.
        ca_i: Intracellular Ca²⁺ concentration in mM.

    Returns:
        The forward rate in 1/ms.
    """
    return _ref(self.a, V, ca_i) / _ref(self.b, V, ca_i)

InfTauBeta dataclass

Bases: _Binary

Backward rate from a steady state and time constant: (1 − inf) / tau.

Built by :func:inf_tau; see :class:InfTauAlpha.

Attributes:

Name Type Description
a Rate

Steady-state open probability inf (dimensionless).

b Rate

Time constant tau in ms.

Source code in neuroclamp/rates.py
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
@dataclass(frozen=True)
class InfTauBeta(_Binary):
    """Backward rate from a steady state and time constant: ``(1 − inf) / tau``.

    Built by :func:`inf_tau`; see :class:`InfTauAlpha`.

    Attributes:
        a: Steady-state open probability ``inf`` (dimensionless).
        b: Time constant ``tau`` in ms.
    """

    def evaluate(self, V: float, ca_i: float) -> float:
        """Evaluate ``(1 − inf) / tau``.

        Args:
            V: Membrane voltage in mV.
            ca_i: Intracellular Ca²⁺ concentration in mM.

        Returns:
            The backward rate in 1/ms.
        """
        return (1.0 - _ref(self.a, V, ca_i)) / _ref(self.b, V, ca_i)

    def _source(self, emit: "_Emitter") -> str:
        """Return inline source for ``(1 − inf) / tau``.

        Args:
            emit: Collects the names the generated source refers to.

        Returns:
            A Python expression in ``V`` and ``ca_i``.
        """
        return f"((1.0 - {emit.source(self.a)}) / {emit.source(self.b)})"

evaluate

evaluate(V, ca_i)

Evaluate (1 − inf) / tau.

Parameters:

Name Type Description Default
V float

Membrane voltage in mV.

required
ca_i float

Intracellular Ca²⁺ concentration in mM.

required

Returns:

Type Description
float

The backward rate in 1/ms.

Source code in neuroclamp/rates.py
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
def evaluate(self, V: float, ca_i: float) -> float:
    """Evaluate ``(1 − inf) / tau``.

    Args:
        V: Membrane voltage in mV.
        ca_i: Intracellular Ca²⁺ concentration in mM.

    Returns:
        The backward rate in 1/ms.
    """
    return (1.0 - _ref(self.a, V, ca_i)) / _ref(self.b, V, ca_i)

inf_tau

inf_tau(inf, tau)

Build a gate's (alpha, beta) rates from its steady state and time constant.

Many channel models specify a gate by x_inf(V) and tau_x(V) rather than by rates. The equivalent rates are alpha = inf / tau and beta = (1 − inf) / tau, so that dx/dt = (inf − x) / tau.

Parameters:

Name Type Description Default
inf Rate | float

Steady-state open probability, as a rate or a number.

required
tau Rate | float

Time constant in ms, as a rate or a number.

required

Returns:

Type Description
tuple[InfTauAlpha, InfTauBeta]

(alpha, beta) for a :class:~neuroclamp.channels.GatingVariable.

Source code in neuroclamp/rates.py
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
def inf_tau(inf: "Rate | float", tau: "Rate | float") -> tuple[InfTauAlpha, InfTauBeta]:
    """Build a gate's (alpha, beta) rates from its steady state and time constant.

    Many channel models specify a gate by ``x_inf(V)`` and ``tau_x(V)``
    rather than by rates.  The equivalent rates are ``alpha = inf / tau`` and
    ``beta = (1 − inf) / tau``, so that ``dx/dt = (inf − x) / tau``.

    Args:
        inf: Steady-state open probability, as a rate or a number.
        tau: Time constant in ms, as a rate or a number.

    Returns:
        ``(alpha, beta)`` for a :class:`~neuroclamp.channels.GatingVariable`.
    """
    inf_expr = _as_expr(inf)
    tau_expr = _as_expr(tau)
    return InfTauAlpha(inf_expr, tau_expr), InfTauBeta(inf_expr, tau_expr)