Algorithms in PyFibers

This page details the logic behind relevant algorithms in PyFibers, including the threshold search and simulation routines. The simulation routines apply the stimulus to the model fiber and track the membrane potential over time. The threshold search determines the minimum stimulus amplitude required to evoke an action potential or block conduction in a model nerve fiber. More details on the ethos behind these algorithms can be found in the PyFibers paper [Placeholder and Placeholder, 1234].

run_sim() : Executing Simulations

Overview

The run_sim() method is the core time-stepped simulation routine in each PyFibers simulation class. It applies the specified stimulation to the model fiber at each time step and uses NEURON’s solver to update the membrane potential and gating variables. At the end, run_sim() returns:

  • The number of action potentials recorded at a designated node (by default, the one closest to 90% fiber length).

  • The time of the last action potential crossing (if any).

When find_threshold() is called, it repeatedly calls:

run_sim(mid_amplitude, fiber)

Therefore, custom run_sim() implementations should follow these constraints. For more information, see building custom simulations.

ScaledStim.run_sim()

ScaledStim is designed for extracellular stimulation. It expects that the fiber has an array of extracellular potentials (e.g., from a point source), each scaled by the provided stimulus amplitude and waveform(s) at each time step. The process is detailed below, and summarized in the diagram below.

For each source, given the spatial distribution of extracellular potentials at the center of each fiber section:

\[V_e(z)\quad\text{for}\quad z=1,2,\ldots,n_{\text{sections}}\quad(3)\]

and the waveform at each time point:

\[W(t)\quad\text{for}\quad t=1,2,\ldots,n_{\text{timesteps}}\quad(4)\]

the dot product gives the unscaled extracellular potential at each fiber section and each time point:

\[V_{e,\text{unscaled}}(z,t) = V_e(z)\cdot W(t)\quad(5)\]

which can then be scaled by the desired stimulation amplitude (a):

\[V_{e,\text{scaled}}(z,t) = a\cdot V_e(z,t)\quad(6)\]

Under the principle of linearity, the extracellular potentials from multiple sources ((m)) are summed:

\[V_{e,\text{final}}(z,t) = \sum_{k=1}^{m} a_k\cdot \Bigl(V_k(z)\cdot W_k(t)\Bigr)\quad(7)\]

The final matrix of potentials is applied to each section at each time step.

ScaledStim.run_sim() diagram

Process of calculating the spatiotemporal profile of extracellular potentials applied to the model fiber by run_sim(), which incorporates the spatial distribution of potentials in response to 1 mA stimulation (\(V_e(z)\)), the unit waveform (\(W(t)\)), and the stimulation amplitude (\(a\)). If potentials from multiple sources and the corresponding waveforms are provided, steps A–D are performed for each combination of source/waveform/stimulation amplitude, and the results are summed across sources before proceeding.

IntraStim.run_sim()

IntraStim is a simpler class for intracellular stimulation. It injects current directly into a chosen fiber section. Key points:

  • The user specifies:

    • Pulse parameters: width, frequency, duration.

    • Location along the fiber: e.g., the middle node or the first node.

  • IntraStim.run_sim(amplitude, fiber) sets up and applies a square current pulse inside the specified section. The amplitude is scaled by the given stimulation amplitude.

  • For multi‑pulse waveforms, IntraStim repeats square pulses at intervals (pulse repetition frequency) until tstop.